Leaked source code of windows server 2003
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.

187 lines
4.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998-2000.
  5. //
  6. // File: LowRes.hxx
  7. //
  8. // Contents: Default low-resource detection
  9. //
  10. // Classes: CLowRes
  11. // CUserActivityMonitor
  12. //
  13. // History: 21-Jul-98 KyleP Created
  14. //
  15. //----------------------------------------------------------------------------
  16. #pragma once
  17. class CCiFrameworkParams;
  18. //***
  19. //
  20. // Activity monitor that stores samples in a circular buffer, discarding
  21. // uninteresting events
  22. //
  23. //***
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Class: CUserActivityMonitor
  27. //
  28. // Purpose: Monitors user keyboard and mouse activity
  29. //
  30. // History: 30 Jul 1998 AlanW Created
  31. //
  32. //----------------------------------------------------------------------------
  33. const unsigned cUserActivitySamples = 100;
  34. class CUserActivityMonitor
  35. {
  36. public:
  37. CUserActivityMonitor() :
  38. _dwLastInputTime(0),
  39. _dwLastSampleTime(0),
  40. _iHead(0),
  41. #if CIDBG
  42. _iSnap(0),
  43. _tid (0),
  44. #endif // CIDBG
  45. _iTail(0)
  46. {
  47. }
  48. void SampleUserActivity();
  49. ULONG GetUserActivity( ULONG dwTicks );
  50. DWORD GetLastSampleTime( )
  51. {
  52. return Ticks(_dwLastSampleTime);
  53. }
  54. private:
  55. // Get the tick count part of a sample
  56. DWORD Ticks( DWORD dw )
  57. {
  58. return dw & ~1;
  59. }
  60. void SetInputFlag( DWORD & dw )
  61. {
  62. dw |= 1;
  63. }
  64. void SetSampleFlag( DWORD & dw )
  65. {
  66. dw &= ~1;
  67. }
  68. BOOL IsInput( DWORD dw )
  69. {
  70. return (dw & 1) != 0;
  71. }
  72. BOOL IsSample( DWORD dw )
  73. {
  74. return (dw & 1) == 0;
  75. }
  76. unsigned Next( unsigned i )
  77. {
  78. i++;
  79. return (i == cUserActivitySamples) ? 0 : i;
  80. }
  81. unsigned Prev( unsigned i )
  82. {
  83. return i==0 ? cUserActivitySamples-1 : i-1;
  84. }
  85. void Add( DWORD dw )
  86. {
  87. _iTail = Next(_iTail);
  88. if (_iTail == _iHead)
  89. _iHead = Next(_iHead);
  90. _adwSamples[_iTail] = dw;
  91. }
  92. BOOL IsBufferEmpty()
  93. {
  94. return _iHead == _iTail;
  95. }
  96. #if CIDBG == 1
  97. void Analyze( ULONG infolevel );
  98. #endif // CIDBG == 1
  99. DWORD _dwLastSampleTime; // last sample time
  100. DWORD _dwLastInputTime; // last user input time
  101. unsigned _iHead; // head of circular buffer
  102. unsigned _iTail; // tail of circular buffer
  103. #if CIDBG == 1
  104. unsigned _iSnap; // start of buffer snapshot
  105. DWORD _tid; // sampling thread ID
  106. #endif // CIDBG == 1
  107. DWORD _adwSamples[cUserActivitySamples];
  108. };
  109. //+---------------------------------------------------------------------------
  110. //
  111. // Class: CLowRes
  112. //
  113. // Purpose: Monitors system-wide resource usage
  114. //
  115. // Interface: ICiResourceMonitor
  116. //
  117. // History: 21-Jul-1998 KyleP Created
  118. //
  119. //----------------------------------------------------------------------------
  120. class CLowRes : public ICiCResourceMonitor
  121. {
  122. public:
  123. CLowRes( CCiFrameworkParams & params );
  124. //
  125. // IUnknown methods
  126. //
  127. SCODE STDMETHODCALLTYPE QueryInterface( REFIID riid, void **ppvObject );
  128. ULONG STDMETHODCALLTYPE AddRef();
  129. ULONG STDMETHODCALLTYPE Release();
  130. //
  131. // ICiCResourceMonitor methods
  132. //
  133. SCODE STDMETHODCALLTYPE IsMemoryLow();
  134. SCODE STDMETHODCALLTYPE IsBatteryLow();
  135. SCODE STDMETHODCALLTYPE IsIoHigh( BOOL * pfAbort );
  136. SCODE STDMETHODCALLTYPE IsUserActive( BOOL fCheckLongTermActivity );
  137. SCODE STDMETHODCALLTYPE SampleUserActivity();
  138. SCODE STDMETHODCALLTYPE IsOnBatteryPower();
  139. private:
  140. long _cRefs;
  141. CCiFrameworkParams & _params;
  142. ULONG _ulPagesPerMeg; // System pages per Meg (varies with system page size)
  143. ULONG _ulPageSize; // System page size
  144. CUserActivityMonitor _UserMon; // User idle detection
  145. };