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.

308 lines
8.2 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name :
  4. w3job.hxx
  5. Abstract:
  6. This file contains class definition for w3 job objects.
  7. Author:
  8. Michael Thomas (michth) Jan-02-1998
  9. Revision History:
  10. --*/
  11. #ifndef _W3JOBOBJ_H_
  12. #define _W3JOBOBJ_H_
  13. #include "iistypes.hxx"
  14. #define MINUTESTOMILISECONDS (1000 * 60)
  15. #define SECONDSTO100NANOSECONDS 10000000
  16. #define MINUTESTO100NANOSECONDS (SECONDSTO100NANOSECONDS * 60)
  17. //
  18. // Job Object Limit Actions
  19. //
  20. typedef enum _SET_LIMIT_ACTION {
  21. SLA_PROCESS_CPU_LIMIT, // Set Per Process CPU Limit
  22. SLA_PROCESS_PRIORITY_CLASS, // Set Priority Class
  23. SLA_TERMINATE_ALL_PROCESSES, // Terminate all processes in job
  24. SLA_JOB_CPU_LIMIT
  25. } SET_LIMIT_ACTION, *PSET_LIMIT_ACTION;
  26. //
  27. // CPU Logging Fields
  28. //
  29. typedef enum _JOB_OBJECT_LOGGING_FIELDS {
  30. JOLF_EVENT,
  31. JOLF_INFO_TYPE,
  32. JOLF_USER_TIME,
  33. JOLF_KERNEL_TIME,
  34. JOLF_PAGE_FAULT,
  35. JOLF_TOTAL_PROCS,
  36. JOLF_ACTIVE_PROCS,
  37. JOLF_TERMINATED_PROCS,
  38. JOLF_NUM_ELEMENTS
  39. } JOB_OBJECT_LOGGING_FIELDS, *PJOB_OBJECT_LOGGING_FIELDS;
  40. //
  41. // CPU Logging Events
  42. // Must be kept in sync with pszarrayJOLE in w3jobobj.cxx
  43. //
  44. typedef enum _JOB_OBJECT_LOG_EVENTS {
  45. JOLE_SITE_START,
  46. JOLE_SITE_STOP,
  47. JOLE_SITE_PAUSE,
  48. JOLE_PERIODIC_LOG,
  49. JOLE_RESET_INT_START,
  50. JOLE_RESET_INT_STOP,
  51. JOLE_RESET_INT_CHANGE,
  52. JOLE_LOGGING_INT_START,
  53. JOLE_LOGGING_INT_STOP,
  54. JOLE_LOGGING_INT_CHANGE,
  55. JOLE_EVENTLOG_LIMIT,
  56. JOLE_PRIORITY_LIMIT,
  57. JOLE_PROCSTOP_LIMIT,
  58. JOLE_PAUSE_LIMIT,
  59. JOLE_EVENTLOG_LIMIT_RESET,
  60. JOLE_PRIORITY_LIMIT_RESET,
  61. JOLE_PROCSTOP_LIMIT_RESET,
  62. JOLE_PAUSE_LIMIT_RESET,
  63. JOLE_NUM_ELEMENTS
  64. } JOB_OBJECT_LOG_EVENTS;
  65. #define JOLE_SITE_START_STR "Site-Start"
  66. #define JOLE_SITE_STOP_STR "Site-Stop"
  67. #define JOLE_SITE_PAUSE_STR "Site-Pause"
  68. #define JOLE_PERIODIC_LOG_STR "Periodic-Log"
  69. #define JOLE_RESET_INT_START_STR "Reset-Interval-Start"
  70. #define JOLE_RESET_INT_STOP_STR "Reset-Interval-Stop"
  71. #define JOLE_RESET_INT_CHANGE_STR "Reset-Interval-Change"
  72. #define JOLE_LOGGING_INT_START_STR "Logging-Interval-Start"
  73. #define JOLE_LOGGING_INT_STOP_STR "Logging-Interval-Stop"
  74. #define JOLE_LOGGING_INT_CHANGE_STR "Logging_Interval-Change"
  75. #define JOLE_EVENTLOG_LIMIT_STR "Eventlog-Limit"
  76. #define JOLE_PRIORITY_LIMIT_STR "Priority-Limit"
  77. #define JOLE_PROCSTOP_LIMIT_STR "Process-Stop-Limit"
  78. #define JOLE_PAUSE_LIMIT_STR "Site-Pause-Limit"
  79. #define JOLE_EVENTLOG_LIMIT_RESET_STR "Eventlog-Limit-Reset"
  80. #define JOLE_PRIORITY_LIMIT_RESET_STR "Priority-Limit-Reset"
  81. #define JOLE_PROCSTOP_LIMIT_RESET_STR "Process-Stop-Limit-Reset"
  82. #define JOLE_PAUSE_LIMIT_RESET_STR "Site-Pause-Limit-Reset"
  83. //
  84. // Process type for CPU Logging
  85. // Must be kept in sync with pszarrayJOPT in w3jobobj.cxx
  86. //
  87. typedef enum _JOB_OBJECT_PROCESS_TYPE {
  88. JOPT_CGI,
  89. JOPT_APP,
  90. JOPT_ALL,
  91. JOPT_NUM_ELEMENTS
  92. } JOB_OBJECT_PROCESS_TYPE;
  93. #define JOPT_CGI_STR "CGI"
  94. #define JOPT_APP_STR "Application"
  95. #define JOPT_ALL_STR "All"
  96. //
  97. // The main class for handling job objects.
  98. // All job object interactions are via this class.
  99. //
  100. class W3_JOB_OBJECT {
  101. private:
  102. //
  103. // Job Object
  104. //
  105. HANDLE m_hJobObject; // Handle to job object
  106. DWORD m_dwJobCGICPULimit; // The CGI CPU limit, in seconds
  107. CRITICAL_SECTION m_csLock;
  108. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION m_jobaiPrevInfo; // The last reset information
  109. HRESULT m_dwError; // Initialization error code
  110. public:
  111. W3_JOB_OBJECT( DWORD dwJobCGICPULimit = NO_W3_CPU_CGI_LIMIT );
  112. ~W3_JOB_OBJECT( void );
  113. //
  114. // Job Object
  115. //
  116. DWORD AddProcessToJob( HANDLE hProcess );
  117. BOOL QueryJobInfo( JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo,
  118. BOOL bResetCounters );
  119. //
  120. // Data access protection methods
  121. //
  122. DWORD SetCompletionPort(HANDLE hCompletionPort,
  123. PVOID pvCompletionKey);
  124. VOID SetJobLimit(IN SET_LIMIT_ACTION slaAction,
  125. IN DWORD dwValue,
  126. IN LONGLONG llJobCPULimit = 0);
  127. dllexp VOID LockThis( VOID ) { EnterCriticalSection(&m_csLock);}
  128. dllexp VOID UnlockThis( VOID ) { LeaveCriticalSection(&m_csLock);}
  129. VOID ResetCounters( JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo );
  130. VOID IncrementStoppedProcs( VOID );
  131. static VOID SumJobInfo(JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiSumInfo,
  132. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo1,
  133. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo2);
  134. static VOID SubtractJobInfo(JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiResultInfo,
  135. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo1,
  136. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION *pjobaiInfo2);
  137. HRESULT GetInitError() {return m_dwError;};
  138. };
  139. typedef W3_JOB_OBJECT *PW3_JOB_OBJECT;
  140. //
  141. // Class to set up a completion port and a thread
  142. // to monitor it. The monitoring function LimitThreadProc
  143. // is hardcoded to handle job object limits.
  144. //
  145. class W3_LIMIT_JOB_THREAD {
  146. private:
  147. HANDLE m_hLimitThread; // Handle to thread
  148. HANDLE m_hCompletionPort; // Handle to Completion Port
  149. DWORD m_dwInitError;
  150. W3_LIMIT_JOB_THREAD( void );
  151. VOID TerminateLimitJobThreadThread( void );
  152. DWORD LimitThreadProc ( void );
  153. static W3_LIMIT_JOB_THREAD *m_pljtLimitJobs;
  154. public:
  155. ~W3_LIMIT_JOB_THREAD( void );
  156. DWORD GetInitError( void ) { return m_dwInitError; };
  157. static DWORD GetLimitJobThread( W3_LIMIT_JOB_THREAD ** ppljtLimitJobs );
  158. static DWORD LimitThreadProcStub ( LPVOID pljtClass )
  159. { return ((W3_LIMIT_JOB_THREAD *)pljtClass)->LimitThreadProc(); };
  160. static VOID StopLimitJobThread( void );
  161. static VOID TerminateLimitJobThread( void );
  162. HANDLE GetCompletionPort ( void ) { return m_hCompletionPort; }
  163. };
  164. typedef W3_LIMIT_JOB_THREAD *PW3_LIMIT_JOB_THREAD;
  165. //
  166. // Class to set up a work item queue for job objects and a thread
  167. // to monitor it. The main purpose of this thread is to handle
  168. // work items that must not hold the job lock.
  169. //
  170. typedef enum _JOB_QUEUE_ACTION {
  171. JQA_RESTART_ALL_APPS,
  172. JQA_TERMINATE_SITE_APPS,
  173. JQA_TERMINATE_THREAD
  174. } JOB_QUEUE_ACTION, *PJOB_QUEUE_ACTION;
  175. typedef struct _JOB_WORK_ITEM {
  176. LIST_ENTRY ListEntry;
  177. JOB_QUEUE_ACTION jqaAction;
  178. PVOID pwsiInstance;
  179. PVOID pvParam;
  180. } JOB_WORK_ITEM, *PJOB_WORK_ITEM;
  181. class W3_JOB_QUEUE {
  182. private:
  183. HANDLE m_hQueueThread; // Handle to thread
  184. DWORD m_dwInitError;
  185. CRITICAL_SECTION m_csLock;
  186. LIST_ENTRY m_leJobQueue;
  187. HANDLE m_hQueueEvent;
  188. W3_JOB_QUEUE( void );
  189. dllexp VOID LockThis( VOID ) { EnterCriticalSection(&m_csLock);}
  190. dllexp VOID UnlockThis( VOID ) { LeaveCriticalSection(&m_csLock);}
  191. static DWORD GetJobQueue( W3_JOB_QUEUE ** ppjqJobQueue );
  192. static W3_JOB_QUEUE *m_pjqJobQueue;
  193. DWORD QueueWorkItem_Worker( JOB_QUEUE_ACTION jqaAction,
  194. PVOID pwsiInstance,
  195. PVOID pvParam,
  196. BOOL fQueueAtTail = TRUE);
  197. VOID TerminateJobQueueThread( void );
  198. public:
  199. ~W3_JOB_QUEUE( void );
  200. DWORD GetInitError( void ) { return m_dwInitError; };
  201. static DWORD QueueThreadProcStub ( LPVOID pjqClass )
  202. { return ((W3_JOB_QUEUE *)pjqClass)->QueueThreadProc(); };
  203. DWORD QueueThreadProc ( void );
  204. static DWORD QueueWorkItem( JOB_QUEUE_ACTION jqaAction,
  205. PVOID pwsiInstance,
  206. PVOID pvParam);
  207. static VOID TerminateJobQueue( void );
  208. static VOID StopJobQueue( void );
  209. };
  210. typedef W3_JOB_QUEUE *PW3_JOB_QUEUE;
  211. VOID
  212. DeferredQueryAndLogJobInfo(
  213. VOID * pContext
  214. );
  215. VOID
  216. DeferredJobResetInterval(
  217. VOID * pContext
  218. );
  219. VOID
  220. DeferredRestartChangedApplications(
  221. VOID * pContext
  222. );
  223. // CPU accounting/limits globals
  224. //
  225. extern DWORD g_dwNumProcessors;
  226. #endif // _W3JOBOBJ_H_