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.

314 lines
6.5 KiB

  1. #ifndef _WPRECYCLER_HXX_
  2. #define _WPRECYCLER_HXX_
  3. /*++
  4. Copyright (c) 1998 Microsoft Corporation
  5. Module Name :
  6. wprecycler.hxx
  7. Abstract:
  8. Definition of WP_RECYCLER. Object handles worker process recycling
  9. - Memory based recycling
  10. - Schedule based recycling
  11. - Elapsed time based recycling
  12. Author:
  13. Jaroslav Dunajsky (JaroslaD) 07-Dec-2000
  14. Environment:
  15. Win32 - User Mode
  16. Project:
  17. W3DT.DLL
  18. --*/
  19. // memory usage will be monitored once a minute - hardcoded value
  20. const DWORD CHECK_MEMORY_TIME_PERIOD = 60000;
  21. class WP_RECYCLER
  22. {
  23. public:
  24. static
  25. HRESULT
  26. Initialize(
  27. VOID
  28. )
  29. {
  30. DBG_ASSERT(FALSE == sm_fCritSecInit);
  31. InitializeCriticalSection( &WP_RECYCLER::sm_CritSec );
  32. sm_fCritSecInit = TRUE;
  33. return S_OK;
  34. };
  35. static
  36. VOID
  37. Terminate(
  38. VOID
  39. )
  40. /*++
  41. Routine Description:
  42. Terminate all recycling
  43. Note:
  44. Caller must assure that no more Start*() methods will be called
  45. after Terminate() is called
  46. Arguments:
  47. none
  48. Return Value:
  49. VOID
  50. --*/
  51. {
  52. TerminateScheduleBased();
  53. TerminateTimeBased();
  54. TerminateMemoryBased();
  55. TerminateRequestBased();
  56. DBG_ASSERT(TRUE == sm_fCritSecInit);
  57. DeleteCriticalSection( &WP_RECYCLER::sm_CritSec );
  58. sm_fCritSecInit = FALSE;
  59. return ;
  60. }
  61. static
  62. HRESULT
  63. StartScheduleBased(
  64. IN const WCHAR * pwszScheduleTimes
  65. );
  66. static
  67. VOID
  68. TerminateScheduleBased(
  69. VOID
  70. );
  71. static
  72. VOID
  73. WINAPI
  74. TimerCallbackForScheduleBased(
  75. PVOID pParam,
  76. BOOLEAN TimerOrWaitFired
  77. );
  78. static
  79. HRESULT
  80. StartMemoryBased(
  81. IN DWORD dwMaxVirtualMemoryUsageInKB,
  82. IN DWORD dwMaxPrivateBytesUsageInKB
  83. );
  84. static
  85. VOID
  86. TerminateMemoryBased(
  87. VOID
  88. );
  89. static
  90. VOID
  91. WINAPI
  92. TimerCallbackForMemoryBased(
  93. PVOID pParam,
  94. BOOLEAN TimerOrWaitFired
  95. );
  96. static
  97. HRESULT
  98. StartTimeBased(
  99. IN DWORD dwPeriodicRestartTimeInMinutes
  100. );
  101. static
  102. VOID
  103. TerminateTimeBased(
  104. VOID
  105. );
  106. static
  107. VOID
  108. WINAPI
  109. TimerCallbackForTimeBased(
  110. PVOID pParam,
  111. BOOLEAN TimerOrWaitFired
  112. );
  113. static
  114. HRESULT
  115. StartRequestBased(
  116. IN DWORD dwRequests
  117. );
  118. static
  119. VOID
  120. TerminateRequestBased(
  121. VOID
  122. );
  123. static
  124. BOOL
  125. IsRequestCountLimitReached(
  126. IN DWORD dwCurrentRequests
  127. )
  128. /*++
  129. Routine Description:
  130. if Request count limit has been reached then
  131. this routine will inform WAS that process is ready
  132. to be recycled
  133. Note:
  134. It is called for each request!!!
  135. Arguments:
  136. dwCurrentRequests - number of requests processed so far
  137. Return Value:
  138. BOOL
  139. --*/
  140. {
  141. if ( !WP_RECYCLER::sm_fIsStartedRequestBased )
  142. {
  143. return TRUE;
  144. }
  145. if ( dwCurrentRequests > sm_dwMaxValueForRequestBased )
  146. {
  147. IF_DEBUG( WPRECYCLER )
  148. {
  149. if ( dwCurrentRequests == sm_dwMaxValueForRequestBased + 1 )
  150. {
  151. DBGPRINTF(( DBG_CONTEXT,
  152. "WP_RECYCLER::CheckRequestBased - limit has been reached"
  153. "- tell WAS to recycle\n" ));
  154. }
  155. }
  156. WP_RECYCLER::SendRecyclingMsg( IPM_WP_RESTART_COUNT_REACHED );
  157. return FALSE;
  158. }
  159. return TRUE;
  160. }
  161. static
  162. BOOL
  163. NeedToSendRecyclingMsg(
  164. VOID
  165. )
  166. /*++
  167. Routine Description:
  168. returns true if recycling request hasn't been sent yet
  169. Return Value:
  170. BOOL
  171. --*/
  172. {
  173. if( sm_RecyclingMsgSent == 1 )
  174. {
  175. return FALSE;
  176. }
  177. return InterlockedExchange( &sm_RecyclingMsgSent, 1 ) == 0;
  178. }
  179. static
  180. VOID
  181. SendRecyclingMsg(
  182. IN enum IPM_WP_SHUTDOWN_MSG Reason
  183. )
  184. /*++
  185. Routine Description:
  186. sends recycling request to WAS
  187. Return Value:
  188. VOID
  189. --*/
  190. {
  191. HRESULT hr = E_FAIL;
  192. if( WP_RECYCLER::NeedToSendRecyclingMsg() )
  193. {
  194. DBG_ASSERT( g_pwpContext != NULL );
  195. hr = g_pwpContext->SendMsgToAdminProcess( Reason );
  196. if ( FAILED( hr ) )
  197. {
  198. DBGPRINTF((
  199. DBG_CONTEXT,
  200. "failed to send recycling request message to WAS: hr=0x%x\n",
  201. hr));
  202. }
  203. }
  204. return;
  205. }
  206. private:
  207. //
  208. // We maintain separate timers so that in the case of change in
  209. // parameter for one type of recycling we don't have to drop
  210. // all scheduled events for all recycling types
  211. //
  212. static CRITICAL_SECTION sm_CritSec;
  213. static HANDLE sm_hTimerForMemoryBased;
  214. static BOOL sm_fIsStartedMemoryBased;
  215. static SIZE_T sm_MaxValueForVirtualMemoryBasedInKB;
  216. static SIZE_T sm_MaxValueForPrivateBytesBasedInKB;
  217. static DWORD sm_CurrentPID;
  218. static BUFFER sm_buffSystemProcessInfo;
  219. static DWORD sm_cbSystemProcessInfo;
  220. static HANDLE sm_hTimerForTimeBased;
  221. static BOOL sm_fIsStartedTimeBased;
  222. static HANDLE sm_hTimerQueueForScheduleBased;
  223. static BOOL sm_fIsStartedScheduleBased;
  224. static BOOL sm_fIsStartedRequestBased;
  225. static DWORD sm_dwMaxValueForRequestBased;
  226. // indicate that message requesting process recycling
  227. // has been sent already (we will send only one recycling request)
  228. // even if multiple conditions are hit
  229. //
  230. static LONG sm_RecyclingMsgSent;
  231. static BOOL sm_fCritSecInit;
  232. WP_RECYCLER();
  233. ~WP_RECYCLER();
  234. };
  235. #endif