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.

668 lines
14 KiB

  1. /*++
  2. Copyright (c) 1998-2002 Microsoft Corporation
  3. Module Name:
  4. counters.h
  5. Abstract:
  6. Contains the performance monitoring counter management
  7. function declarations
  8. Author:
  9. Eric Stenson (ericsten) 25-Sep-2000
  10. Revision History:
  11. --*/
  12. #ifndef __COUNTERS_H__
  13. #define __COUNTERS_H__
  14. //
  15. // structure to hold info for Site Counters.
  16. //
  17. typedef struct _UL_SITE_COUNTER_ENTRY {
  18. //
  19. // Signature is UL_SITE_COUNTER_ENTRY_POOL_TAG
  20. //
  21. ULONG Signature;
  22. //
  23. // Ref count for this Site Counter Entry
  24. //
  25. LONG RefCount;
  26. //
  27. // links Control Channel site counter entries
  28. //
  29. LIST_ENTRY ListEntry;
  30. //
  31. // links Global site counter entries
  32. //
  33. LIST_ENTRY GlobalListEntry;
  34. //
  35. // Lock protets counter data; used primarily when touching
  36. // 64-bit counters and reading counters
  37. //
  38. FAST_MUTEX EntryMutex;
  39. //
  40. // the block which actually contains the counter data to be
  41. // passed back to WAS
  42. //
  43. HTTP_SITE_COUNTERS Counters;
  44. } UL_SITE_COUNTER_ENTRY, *PUL_SITE_COUNTER_ENTRY;
  45. #define IS_VALID_SITE_COUNTER_ENTRY( entry ) \
  46. HAS_VALID_SIGNATURE(entry, UL_SITE_COUNTER_ENTRY_POOL_TAG)
  47. //
  48. // Private globals
  49. //
  50. extern BOOLEAN g_InitCountersCalled;
  51. extern HTTP_GLOBAL_COUNTERS g_UlGlobalCounters;
  52. extern FAST_MUTEX g_SiteCounterListMutex;
  53. extern LIST_ENTRY g_SiteCounterListHead;
  54. extern LONG g_SiteCounterListCount;
  55. extern HTTP_PROP_DESC aIISULGlobalDescription[];
  56. extern HTTP_PROP_DESC aIISULSiteDescription[];
  57. //
  58. // Init
  59. //
  60. NTSTATUS
  61. UlInitializeCounters(
  62. VOID
  63. );
  64. VOID
  65. UlTerminateCounters(
  66. VOID
  67. );
  68. //
  69. // Site Counter Entry
  70. //
  71. NTSTATUS
  72. UlCreateSiteCounterEntry(
  73. IN OUT PUL_CONFIG_GROUP_OBJECT pConfigGroup,
  74. IN ULONG SiteId
  75. );
  76. //
  77. // DO NOT CALL THESE REF FUNCTIONS DIRECTLY:
  78. // These are the backing implementations; use the
  79. // REFERENCE_*/DEREFERENCE_* macros instead.
  80. //
  81. __inline
  82. LONG
  83. UlReferenceSiteCounterEntry(
  84. IN PUL_SITE_COUNTER_ENTRY pEntry
  85. )
  86. {
  87. return InterlockedIncrement(&pEntry->RefCount);
  88. }
  89. __inline
  90. LONG
  91. UlDereferenceSiteCounterEntry(
  92. IN PUL_SITE_COUNTER_ENTRY pEntry
  93. )
  94. {
  95. LONG ref;
  96. ref = InterlockedDecrement(&pEntry->RefCount);
  97. if ( 0 == ref )
  98. {
  99. //
  100. // Remove from Site Counter List(s)
  101. //
  102. ExAcquireFastMutex(&g_SiteCounterListMutex);
  103. // we should already be removed from the control channel's list
  104. ASSERT(NULL == pEntry->ListEntry.Flink);
  105. RemoveEntryList(&(pEntry->GlobalListEntry));
  106. pEntry->GlobalListEntry.Flink = pEntry->GlobalListEntry.Blink = NULL;
  107. g_SiteCounterListCount--;
  108. ExReleaseFastMutex(&g_SiteCounterListMutex);
  109. UL_FREE_POOL_WITH_SIG(pEntry, UL_SITE_COUNTER_ENTRY_POOL_TAG);
  110. }
  111. return ref;
  112. } // UlDereferenceSiteCounterEntry
  113. #if REFERENCE_DEBUG
  114. VOID
  115. UlDbgDereferenceSiteCounterEntry(
  116. IN PUL_SITE_COUNTER_ENTRY pEntry
  117. REFERENCE_DEBUG_FORMAL_PARAMS
  118. );
  119. #define DEREFERENCE_SITE_COUNTER_ENTRY( pSC ) \
  120. UlDbgDereferenceSiteCounterEntry( \
  121. (pSC) \
  122. REFERENCE_DEBUG_ACTUAL_PARAMS \
  123. )
  124. VOID
  125. UlDbgReferenceSiteCounterEntry(
  126. IN PUL_SITE_COUNTER_ENTRY pEntry
  127. REFERENCE_DEBUG_FORMAL_PARAMS
  128. );
  129. #define REFERENCE_SITE_COUNTER_ENTRY( pSC ) \
  130. UlDbgReferenceSiteCounterEntry( \
  131. (pSC) \
  132. REFERENCE_DEBUG_ACTUAL_PARAMS \
  133. )
  134. #else
  135. #define REFERENCE_SITE_COUNTER_ENTRY (VOID)UlReferenceSiteCounterEntry
  136. #define DEREFERENCE_SITE_COUNTER_ENTRY (VOID)UlDereferenceSiteCounterEntry
  137. #endif
  138. /*++
  139. Routine Description:
  140. Removes Site Counter from UL_CONFIG_GROUP_OBJECT, removing it
  141. fromt the Control Channel's Site Counter List.
  142. Object should remain on Global list if there are still references
  143. Arguments:
  144. pConfigGroup - the UL_CONFIG_GROUP_OBJECT from which we should
  145. decouple the UL_SITE_COUNTER_ENTRY
  146. --*/
  147. __inline
  148. VOID
  149. UlDecoupleSiteCounterEntry(
  150. IN PUL_CONFIG_GROUP_OBJECT pConfigGroup
  151. )
  152. {
  153. ASSERT(IS_VALID_CONFIG_GROUP(pConfigGroup));
  154. ASSERT(IS_VALID_CONTROL_CHANNEL(pConfigGroup->pControlChannel));
  155. if (pConfigGroup->pSiteCounters)
  156. {
  157. // Remove from Control Channel's list
  158. ExAcquireFastMutex(&g_SiteCounterListMutex);
  159. RemoveEntryList(&pConfigGroup->pSiteCounters->ListEntry);
  160. pConfigGroup->pSiteCounters->ListEntry.Flink = NULL;
  161. pConfigGroup->pSiteCounters->ListEntry.Blink = NULL;
  162. pConfigGroup->pControlChannel->SiteCounterCount--;
  163. ExReleaseFastMutex(&g_SiteCounterListMutex);
  164. // Remove Config Group's reference outside of Mutex : we
  165. // might take ref to 0 and need to remove from Global
  166. // Site Counter list
  167. DEREFERENCE_SITE_COUNTER_ENTRY(pConfigGroup->pSiteCounters);
  168. pConfigGroup->pSiteCounters = NULL;
  169. }
  170. }
  171. //
  172. // Global (cache) counters
  173. //
  174. __inline
  175. VOID
  176. UlIncCounterRtl(
  177. IN HTTP_GLOBAL_COUNTER_ID CounterId
  178. )
  179. {
  180. PCHAR pCounter;
  181. pCounter = (PCHAR)&g_UlGlobalCounters;
  182. pCounter += aIISULGlobalDescription[CounterId].Offset;
  183. if (sizeof(ULONG) == aIISULGlobalDescription[CounterId].Size)
  184. {
  185. InterlockedIncrement((PLONG)pCounter);
  186. }
  187. else
  188. {
  189. UlInterlockedIncrement64((PLONGLONG)pCounter);
  190. }
  191. }
  192. __inline
  193. VOID
  194. UlDecCounterRtl(
  195. IN HTTP_GLOBAL_COUNTER_ID CounterId
  196. )
  197. {
  198. PCHAR pCounter;
  199. pCounter = (PCHAR)&g_UlGlobalCounters;
  200. pCounter += aIISULGlobalDescription[CounterId].Offset;
  201. if (sizeof(ULONG) == aIISULGlobalDescription[CounterId].Size)
  202. {
  203. InterlockedDecrement((PLONG)pCounter);
  204. }
  205. else
  206. {
  207. UlInterlockedDecrement64((PLONGLONG)pCounter);
  208. }
  209. }
  210. __inline
  211. VOID
  212. UlAddCounterRtl(
  213. IN HTTP_GLOBAL_COUNTER_ID CounterId,
  214. IN ULONG Value
  215. )
  216. {
  217. PCHAR pCounter;
  218. pCounter = (PCHAR)&g_UlGlobalCounters;
  219. pCounter += aIISULGlobalDescription[CounterId].Offset;
  220. if (sizeof(ULONG) == aIISULGlobalDescription[CounterId].Size)
  221. {
  222. InterlockedExchangeAdd((PLONG)pCounter, Value);
  223. }
  224. else
  225. {
  226. UlInterlockedAdd64((PLONGLONG)pCounter, Value);
  227. }
  228. }
  229. __inline
  230. VOID
  231. UlResetCounterRtl(
  232. IN HTTP_GLOBAL_COUNTER_ID CounterId
  233. )
  234. {
  235. PCHAR pCounter;
  236. pCounter = (PCHAR)&g_UlGlobalCounters;
  237. pCounter += aIISULGlobalDescription[CounterId].Offset;
  238. if (sizeof(ULONG) == aIISULGlobalDescription[CounterId].Size)
  239. {
  240. InterlockedExchange((PLONG)pCounter, 0);
  241. }
  242. else
  243. {
  244. UlInterlockedExchange64((PLONGLONG)pCounter, 0);
  245. }
  246. }
  247. #if DBG
  248. VOID
  249. UlIncCounterDbg(
  250. HTTP_GLOBAL_COUNTER_ID Ctr
  251. );
  252. VOID
  253. UlDecCounterDbg(
  254. HTTP_GLOBAL_COUNTER_ID Ctr
  255. );
  256. VOID
  257. UlAddCounterDbg(
  258. HTTP_GLOBAL_COUNTER_ID Ctr,
  259. ULONG Value
  260. );
  261. VOID
  262. UlResetCounterDbg(
  263. HTTP_GLOBAL_COUNTER_ID Ctr
  264. );
  265. #define UlIncCounter UlIncCounterDbg
  266. #define UlDecCounter UlDecCounterDbg
  267. #define UlAddCounter UlAddCounterDbg
  268. #define UlResetCounter UlResetCounterDbg
  269. #else // DBG
  270. #define UlIncCounter UlIncCounterRtl
  271. #define UlDecCounter UlDecCounterRtl
  272. #define UlAddCounter UlAddCounterRtl
  273. #define UlResetCounter UlResetCounterRtl
  274. #endif // DBG
  275. //
  276. // Instance (site) counters
  277. //
  278. __inline
  279. VOID
  280. UlIncSiteNonCriticalCounterUlong(
  281. PUL_SITE_COUNTER_ENTRY pEntry,
  282. HTTP_SITE_COUNTER_ID CounterId
  283. )
  284. {
  285. PCHAR pCounter;
  286. PLONG plValue;
  287. pCounter = (PCHAR) &(pEntry->Counters);
  288. pCounter += aIISULSiteDescription[CounterId].Offset;
  289. plValue = (PLONG) pCounter;
  290. ++(*plValue);
  291. }
  292. __inline
  293. VOID
  294. UlIncSiteNonCriticalCounterUlonglong(
  295. PUL_SITE_COUNTER_ENTRY pEntry,
  296. HTTP_SITE_COUNTER_ID CounterId
  297. )
  298. {
  299. PCHAR pCounter;
  300. PLONGLONG pllValue;
  301. pCounter = (PCHAR) &(pEntry->Counters);
  302. pCounter += aIISULSiteDescription[CounterId].Offset;
  303. pllValue = (PLONGLONG) pCounter;
  304. ++(*pllValue);
  305. }
  306. //
  307. // NOTE: DO NOT CALL *Rtl vesrions directly!
  308. //
  309. __inline
  310. LONGLONG
  311. UlIncSiteCounterRtl(
  312. IN PUL_SITE_COUNTER_ENTRY pEntry,
  313. IN HTTP_SITE_COUNTER_ID CounterId
  314. )
  315. {
  316. PCHAR pCounter;
  317. pCounter = (PCHAR)&pEntry->Counters;
  318. pCounter += aIISULSiteDescription[CounterId].Offset;
  319. switch(aIISULSiteDescription[CounterId].Size)
  320. {
  321. case sizeof(ULONG):
  322. return (LONGLONG)InterlockedIncrement((PLONG)pCounter);
  323. case sizeof(LONGLONG):
  324. return UlInterlockedIncrement64((PLONGLONG)pCounter);
  325. default:
  326. ASSERT(!"UlIncSiteCounterRtl: ERROR: Invalid counter size!\n");
  327. }
  328. return 0L;
  329. }
  330. __inline
  331. VOID
  332. UlDecSiteCounterRtl(
  333. IN PUL_SITE_COUNTER_ENTRY pEntry,
  334. IN HTTP_SITE_COUNTER_ID CounterId
  335. )
  336. {
  337. PCHAR pCounter;
  338. pCounter = (PCHAR)&pEntry->Counters;
  339. pCounter += aIISULSiteDescription[CounterId].Offset;
  340. switch(aIISULSiteDescription[CounterId].Size)
  341. {
  342. case sizeof(ULONG):
  343. InterlockedDecrement((PLONG)pCounter);
  344. break;
  345. case sizeof(ULONGLONG):
  346. UlInterlockedDecrement64((PLONGLONG)pCounter);
  347. break;
  348. default:
  349. ASSERT(!"UlDecSiteCounterRtl: ERROR: Invalid counter size!\n");
  350. }
  351. }
  352. __inline
  353. VOID
  354. UlAddSiteCounterRtl(
  355. IN PUL_SITE_COUNTER_ENTRY pEntry,
  356. IN HTTP_SITE_COUNTER_ID CounterId,
  357. IN ULONG Value
  358. )
  359. {
  360. PCHAR pCounter;
  361. pCounter = (PCHAR)&pEntry->Counters;
  362. pCounter += aIISULSiteDescription[CounterId].Offset;
  363. InterlockedExchangeAdd((PLONG)pCounter, Value);
  364. }
  365. __inline
  366. VOID
  367. UlAddSiteCounter64Rtl(
  368. IN PUL_SITE_COUNTER_ENTRY pEntry,
  369. IN HTTP_SITE_COUNTER_ID CounterId,
  370. IN ULONGLONG Value
  371. )
  372. {
  373. PCHAR pCounter;
  374. pCounter = (PCHAR)&pEntry->Counters;
  375. pCounter += aIISULSiteDescription[CounterId].Offset;
  376. UlInterlockedAdd64((PLONGLONG)pCounter, Value);
  377. }
  378. __inline
  379. VOID
  380. UlResetSiteCounterRtl(
  381. IN PUL_SITE_COUNTER_ENTRY pEntry,
  382. IN HTTP_SITE_COUNTER_ID CounterId
  383. )
  384. {
  385. PCHAR pCounter;
  386. pCounter = (PCHAR)&pEntry->Counters;
  387. pCounter += aIISULSiteDescription[CounterId].Offset;
  388. switch(aIISULSiteDescription[CounterId].Size)
  389. {
  390. case sizeof(ULONG):
  391. InterlockedExchange((PLONG)pCounter, 0);
  392. break;
  393. case sizeof(ULONGLONG):
  394. UlInterlockedExchange64((PLONGLONG)pCounter, 0);
  395. break;
  396. default:
  397. ASSERT(!"UlResetSiteCounterRtl: ERROR: Invalid counter size!\n");
  398. }
  399. }
  400. __inline
  401. VOID
  402. UlMaxSiteCounterRtl(
  403. IN PUL_SITE_COUNTER_ENTRY pEntry,
  404. IN HTTP_SITE_COUNTER_ID CounterId,
  405. IN ULONG Value
  406. )
  407. {
  408. PCHAR pCounter;
  409. LONG LocalMaxed;
  410. pCounter = (PCHAR)&pEntry->Counters;
  411. pCounter += aIISULSiteDescription[CounterId].Offset;
  412. do {
  413. LocalMaxed = *((volatile LONG *)pCounter);
  414. if ((LONG)Value <= LocalMaxed)
  415. {
  416. break;
  417. }
  418. PAUSE_PROCESSOR;
  419. } while (LocalMaxed != InterlockedCompareExchange(
  420. (PLONG)pCounter,
  421. Value,
  422. LocalMaxed
  423. ));
  424. }
  425. __inline
  426. VOID
  427. UlMaxSiteCounter64Rtl(
  428. IN PUL_SITE_COUNTER_ENTRY pEntry,
  429. IN HTTP_SITE_COUNTER_ID CounterId,
  430. IN LONGLONG Value
  431. )
  432. {
  433. PCHAR pCounter;
  434. LONGLONG LocalMaxed;
  435. pCounter = (PCHAR)&pEntry->Counters;
  436. pCounter += aIISULSiteDescription[CounterId].Offset;
  437. do {
  438. LocalMaxed = *((volatile LONGLONG *)pCounter);
  439. if (Value <= LocalMaxed)
  440. {
  441. break;
  442. }
  443. PAUSE_PROCESSOR;
  444. } while (LocalMaxed != InterlockedCompareExchange64(
  445. (PLONGLONG)pCounter,
  446. Value,
  447. LocalMaxed
  448. ));
  449. }
  450. #if DBG
  451. LONGLONG
  452. UlIncSiteCounterDbg(
  453. PUL_SITE_COUNTER_ENTRY pEntry,
  454. HTTP_SITE_COUNTER_ID Ctr
  455. );
  456. VOID
  457. UlDecSiteCounterDbg(
  458. PUL_SITE_COUNTER_ENTRY pEntry,
  459. HTTP_SITE_COUNTER_ID Ctr
  460. );
  461. VOID
  462. UlAddSiteCounterDbg(
  463. PUL_SITE_COUNTER_ENTRY pEntry,
  464. HTTP_SITE_COUNTER_ID Ctr,
  465. ULONG Value
  466. );
  467. VOID
  468. UlAddSiteCounter64Dbg(
  469. PUL_SITE_COUNTER_ENTRY pEntry,
  470. HTTP_SITE_COUNTER_ID Ctr,
  471. ULONGLONG llValue
  472. );
  473. VOID
  474. UlResetSiteCounterDbg(
  475. PUL_SITE_COUNTER_ENTRY pEntry,
  476. HTTP_SITE_COUNTER_ID Ctr
  477. );
  478. VOID
  479. UlMaxSiteCounterDbg(
  480. PUL_SITE_COUNTER_ENTRY pEntry,
  481. HTTP_SITE_COUNTER_ID Ctr,
  482. ULONG Value
  483. );
  484. VOID
  485. UlMaxSiteCounter64Dbg(
  486. PUL_SITE_COUNTER_ENTRY pEntry,
  487. HTTP_SITE_COUNTER_ID Ctr,
  488. LONGLONG llValue
  489. );
  490. #define UlIncSiteCounter UlIncSiteCounterDbg
  491. #define UlDecSiteCounter UlDecSiteCounterDbg
  492. #define UlAddSiteCounter UlAddSiteCounterDbg
  493. #define UlAddSiteCounter64 UlAddSiteCounter64Dbg
  494. #define UlResetSiteCounter UlResetSiteCounterDbg
  495. #define UlMaxSiteCounter UlMaxSiteCounterDbg
  496. #define UlMaxSiteCounter64 UlMaxSiteCounter64Dbg
  497. #else // !DBG
  498. #define UlIncSiteCounter UlIncSiteCounterRtl
  499. #define UlDecSiteCounter UlDecSiteCounterRtl
  500. #define UlAddSiteCounter UlAddSiteCounterRtl
  501. #define UlAddSiteCounter64 UlAddSiteCounter64Rtl
  502. #define UlResetSiteCounter UlResetSiteCounterRtl
  503. #define UlMaxSiteCounter UlMaxSiteCounterRtl
  504. #define UlMaxSiteCounter64 UlMaxSiteCounter64Rtl
  505. #endif // DBG
  506. //
  507. // Collection
  508. //
  509. NTSTATUS
  510. UlGetGlobalCounters(
  511. IN OUT PVOID pCounter,
  512. IN ULONG BlockSize,
  513. OUT PULONG pBytesWritten
  514. );
  515. NTSTATUS
  516. UlGetSiteCounters(
  517. IN PUL_CONTROL_CHANNEL pControlChannel,
  518. IN OUT PVOID pCounter,
  519. IN ULONG BlockSize,
  520. OUT PULONG pBytesWritten,
  521. OUT PULONG pBlocksWritten OPTIONAL
  522. );
  523. #endif // __COUNTERS_H__