Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995.
  5. //
  6. // File: Idle.cxx
  7. //
  8. // Contents: Idle time tracker.
  9. //
  10. // Classes: CIdleTime
  11. //
  12. // History: 15-Nov-95 KyleP Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include "idle.hxx"
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Member: CIdleTime::CIdleTime, private
  21. //
  22. // Effects: Initialize idle time tracker.
  23. //
  24. // History: 15-Nov-95 KyleP Created
  25. //
  26. //----------------------------------------------------------------------------
  27. CIdleTime::CIdleTime()
  28. : _liLastIdleTime( 0 ),
  29. _liLastTotalTime( 0 ),
  30. _cProcessors( 0 )
  31. {
  32. NTSTATUS status = NtQuerySystemInformation(
  33. SystemProcessorPerformanceInformation,
  34. _aProcessorTime,
  35. sizeof(_aProcessorTime),
  36. &_cProcessors );
  37. if ( !NT_ERROR( status ) )
  38. {
  39. Win4Assert( _cProcessors % sizeof(_aProcessorTime[0]) == 0 );
  40. _cProcessors = _cProcessors / sizeof(_aProcessorTime[0]);
  41. for ( unsigned i = 0; i < _cProcessors; i++ )
  42. {
  43. _liLastIdleTime += _aProcessorTime[i].IdleTime.QuadPart;
  44. _liLastTotalTime += _aProcessorTime[i].KernelTime.QuadPart + _aProcessorTime[i].UserTime.QuadPart;
  45. }
  46. //
  47. // Don't check for overflow. This will overflow in about
  48. // 58K years!
  49. //
  50. Win4Assert( _liLastIdleTime < 0x0FFFFFFFFFFFFFFF );
  51. Win4Assert( _liLastTotalTime < 0x0FFFFFFFFFFFFFFF );
  52. }
  53. else
  54. {
  55. ciDebugOut(( DEB_ERROR, "NtQuerySystemInformation returned 0x%x\n", status ));
  56. _cProcessors = 0;
  57. }
  58. }
  59. //+---------------------------------------------------------------------------
  60. //
  61. // Member: CIdleTime::PercentIdle, private
  62. //
  63. // Returns: Percent idle time since last call to this method.
  64. //
  65. // History: 15-Nov-95 KyleP Created
  66. //
  67. //----------------------------------------------------------------------------
  68. unsigned CIdleTime::PercentIdle()
  69. {
  70. unsigned pctIdle;
  71. NTSTATUS status = NtQuerySystemInformation (
  72. SystemProcessorPerformanceInformation,
  73. _aProcessorTime,
  74. sizeof(_aProcessorTime[0]) * _cProcessors,
  75. 0 );
  76. if ( !NT_ERROR( status ) )
  77. {
  78. LONGLONG liIdleTime = 0;
  79. LONGLONG liTotalTime = 0;
  80. for ( unsigned i = 0; i < _cProcessors; i++ )
  81. {
  82. liIdleTime += _aProcessorTime[i].IdleTime.QuadPart;
  83. liTotalTime += _aProcessorTime[i].KernelTime.QuadPart + _aProcessorTime[i].UserTime.QuadPart;
  84. }
  85. Win4Assert( liIdleTime >= _liLastIdleTime );
  86. Win4Assert( liTotalTime >= _liLastTotalTime );
  87. pctIdle = (unsigned)((liIdleTime - _liLastIdleTime) * 100 / (1 + liTotalTime - _liLastTotalTime));
  88. _liLastIdleTime = liIdleTime;
  89. _liLastTotalTime = liTotalTime;
  90. //
  91. // Don't check for overflow. This will overflow in about
  92. // 58K years!
  93. //
  94. Win4Assert( _liLastIdleTime < 0x0FFFFFFFFFFFFFFF );
  95. Win4Assert( _liLastTotalTime < 0x0FFFFFFFFFFFFFFF );
  96. }
  97. else
  98. {
  99. ciDebugOut(( DEB_ERROR, "NtQuerySystemInformation returned 0x%x\n", status ));
  100. pctIdle = 0;
  101. }
  102. Win4Assert( pctIdle <= 100 );
  103. return pctIdle;
  104. }