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.

310 lines
6.0 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 every 5 minutes - hardcoded value
  20. const DWORD CHECK_MEMORY_TIME_PERIOD = 300000;
  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 dwMaxVirtualMemoryKbUsage
  82. );
  83. static
  84. VOID
  85. TerminateMemoryBased(
  86. VOID
  87. );
  88. static
  89. VOID
  90. WINAPI
  91. TimerCallbackForMemoryBased(
  92. PVOID pParam,
  93. BOOLEAN TimerOrWaitFired
  94. );
  95. static
  96. HRESULT
  97. StartTimeBased(
  98. IN DWORD dwPeriodicRestartTimeInMinutes
  99. );
  100. static
  101. VOID
  102. TerminateTimeBased(
  103. VOID
  104. );
  105. static
  106. VOID
  107. WINAPI
  108. TimerCallbackForTimeBased(
  109. PVOID pParam,
  110. BOOLEAN TimerOrWaitFired
  111. );
  112. static
  113. HRESULT
  114. StartRequestBased(
  115. IN DWORD dwRequests
  116. );
  117. static
  118. VOID
  119. TerminateRequestBased(
  120. VOID
  121. );
  122. static
  123. BOOL
  124. IsRequestCountLimitReached(
  125. IN DWORD dwCurrentRequests
  126. )
  127. /*++
  128. Routine Description:
  129. if Request count limit has been reached then
  130. this routine will inform WAS that process is ready
  131. to be recycled
  132. Note:
  133. It is called for each request!!!
  134. Arguments:
  135. dwCurrentRequests - number of requests processed so far
  136. Return Value:
  137. BOOL
  138. --*/
  139. {
  140. if ( !WP_RECYCLER::sm_fIsStartedRequestBased )
  141. {
  142. return TRUE;
  143. }
  144. if ( dwCurrentRequests > sm_dwMaxValueForRequestBased )
  145. {
  146. IF_DEBUG( WPRECYCLER )
  147. {
  148. if ( dwCurrentRequests == sm_dwMaxValueForRequestBased + 1 )
  149. {
  150. DBGPRINTF(( DBG_CONTEXT,
  151. "WP_RECYCLER::CheckRequestBased - limit has been reached"
  152. "- tell WAS to recycle\n" ));
  153. }
  154. }
  155. WP_RECYCLER::SendRecyclingMsg( IPM_WP_RESTART_COUNT_REACHED );
  156. return FALSE;
  157. }
  158. return TRUE;
  159. }
  160. static
  161. BOOL
  162. NeedToSendRecyclingMsg(
  163. VOID
  164. )
  165. /*++
  166. Routine Description:
  167. returns true if recycling request hasn't been sent yet
  168. Return Value:
  169. BOOL
  170. --*/
  171. {
  172. if( sm_RecyclingMsgSent == 1 )
  173. {
  174. return FALSE;
  175. }
  176. return InterlockedExchange( &sm_RecyclingMsgSent, 1 ) == 0;
  177. }
  178. static
  179. VOID
  180. SendRecyclingMsg(
  181. IN enum IPM_WP_SHUTDOWN_MSG Reason
  182. )
  183. /*++
  184. Routine Description:
  185. sends recycling request to WAS
  186. Return Value:
  187. VOID
  188. --*/
  189. {
  190. HRESULT hr = E_FAIL;
  191. if( WP_RECYCLER::NeedToSendRecyclingMsg() )
  192. {
  193. DBG_ASSERT( g_pwpContext != NULL );
  194. hr = g_pwpContext->SendMsgToAdminProcess( Reason );
  195. if ( FAILED( hr ) )
  196. {
  197. DBGPRINTF((
  198. DBG_CONTEXT,
  199. "failed to send recycling request message to WAS: hr=0x%x\n",
  200. hr));
  201. }
  202. }
  203. return;
  204. }
  205. private:
  206. //
  207. // We maintain separate timers so that in the case of change in
  208. // parameter for one type of recycling we don't have to drop
  209. // all scheduled events for all recycling types
  210. //
  211. static CRITICAL_SECTION sm_CritSec;
  212. static HANDLE sm_hTimerForMemoryBased;
  213. static BOOL sm_fIsStartedMemoryBased;
  214. static SIZE_T sm_MaxValueForMemoryBased;
  215. static HANDLE sm_hCurrentProcess;
  216. static HANDLE sm_hTimerForTimeBased;
  217. static BOOL sm_fIsStartedTimeBased;
  218. static HANDLE sm_hTimerQueueForScheduleBased;
  219. static BOOL sm_fIsStartedScheduleBased;
  220. static BOOL sm_fIsStartedRequestBased;
  221. static DWORD sm_dwMaxValueForRequestBased;
  222. // indicate that message requesting process recycling
  223. // has been sent already (we will send only one recycling request)
  224. // even if multiple conditions are hit
  225. //
  226. static LONG sm_RecyclingMsgSent;
  227. static BOOL sm_fCritSecInit;
  228. WP_RECYCLER();
  229. ~WP_RECYCLER();
  230. };
  231. #endif