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.

641 lines
18 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. perflib.h
  5. Abstract:
  6. Private functions and data structures used by perflib only
  7. Author:
  8. JeePang 09/27/2000
  9. Revision History:
  10. --*/
  11. #ifndef _PERFLIB_H_
  12. #define _PERFLIB_H_
  13. #define _WMI_SOURCE_
  14. #include <wmistr.h>
  15. #include <evntrace.h>
  16. //
  17. // Private registry function to prevent query within RegQueryValue
  18. // This needs to preceed winperfp.h since it needs this function
  19. //
  20. LONG
  21. PrivateRegQueryValueExT (
  22. HKEY hKey,
  23. LPVOID lpValueName,
  24. LPDWORD lpReserved,
  25. LPDWORD lpType,
  26. LPBYTE lpData,
  27. LPDWORD lpcbData,
  28. BOOL bUnicode
  29. );
  30. #define StartTraceW EtwStartTraceW
  31. #define QueryTraceW EtwQueryTraceW
  32. #define TraceEvent EtwTraceEvent
  33. extern
  34. ULONG
  35. WMIAPI
  36. EtwStartTraceW(
  37. OUT PTRACEHANDLE LoggerHandle,
  38. IN LPCWSTR LoggerName,
  39. IN OUT PEVENT_TRACE_PROPERTIES Properties
  40. );
  41. extern
  42. ULONG
  43. WMIAPI
  44. EtwQueryTraceW(
  45. IN TRACEHANDLE TraceHandle,
  46. IN LPCWSTR InstanceName,
  47. IN OUT PEVENT_TRACE_PROPERTIES Properties
  48. );
  49. extern
  50. ULONG
  51. WMIAPI
  52. EtwTraceEvent(
  53. IN TRACEHANDLE LoggerHandle,
  54. IN PEVENT_TRACE_HEADER EventTrace
  55. );
  56. #include <winperfp.h>
  57. //
  58. // Commonly used macros
  59. //
  60. #define HEAP_PROBE() ; // Not implemented
  61. #define ALLOCMEM(size) RtlAllocateHeap (RtlProcessHeap(), HEAP_ZERO_MEMORY, size)
  62. #define REALLOCMEM(pointer, newsize) \
  63. RtlReAllocateHeap (RtlProcessHeap(), 0, pointer, newsize)
  64. #define FREEMEM(pointer) if ((pointer)) { RtlFreeHeap (RtlProcessHeap(), 0, pointer); }
  65. #define MAX_KEY_NAME_LENGTH 256*sizeof(WCHAR)
  66. #define MAX_VALUE_NAME_LENGTH 256*sizeof(WCHAR)
  67. #define MAX_VALUE_DATA_LENGTH 256*sizeof(WCHAR)
  68. //
  69. // Use this constant to include "\\Performance" and terminator as well
  70. // NOTE: This must be 8-byte multiple, which is 280 here
  71. //
  72. #define MAX_NAME_PATH MAX_VALUE_DATA_LENGTH + 11 * sizeof(WCHAR) + sizeof(UNICODE_NULL)
  73. // flag to determine the "noisiness" of the event logging
  74. // this value is read from the system registry when the extensible
  75. // objects are loaded and used for the subsequent calls.
  76. //
  77. //
  78. // Levels: LOG_UNDEFINED = registry log level not read yet
  79. // LOG_NONE = No event log messages ever
  80. // LOG_USER = User event log messages (e.g. errors)
  81. // LOG_DEBUG = Minimum Debugging (warnings & errors)
  82. // LOG_VERBOSE = Maximum Debugging (informational, success,
  83. // error and warning messages
  84. //
  85. #define LOG_UNDEFINED ((LONG)-1)
  86. #define LOG_NONE 0
  87. #define LOG_USER 1
  88. #define LOG_DEBUG 2
  89. #define LOG_VERBOSE 3
  90. //
  91. // define configurable extensible counter buffer testing
  92. //
  93. // Test Level Event that will prevent data buffer
  94. // from being returne in PerfDataBlock
  95. //
  96. // EXT_TEST_NOMEMALLOC Collect Fn. writes directly to calling fn's buffer
  97. //
  98. // all the following test levels have the collect fn. write to a
  99. // buffer allocated separately from the calling fn's buffer
  100. //
  101. // EXT_TEST_NONE Collect Fn. Returns bad status or generates exception
  102. // EXT_TEST_BASIC Collect Fn. has buffer overflow or violates guard page
  103. // EXT_TEST_ALL Collect Fn. object or instance lengths are not conistent
  104. //
  105. //
  106. #define EXT_TEST_UNDEFINED 0
  107. #define EXT_TEST_ALL 1
  108. #define EXT_TEST_BASIC 2
  109. #define EXT_TEST_NONE 3
  110. #define EXT_TEST_NOMEMALLOC 4
  111. #define PERFLIB_DISABLE_NONE 0
  112. #define PERFLIB_DISABLE_ALL 1
  113. #define PERFLIB_DISABLE_X32 2
  114. #define PERFLIB_DISABLE_IA64 4
  115. // Misc. configuration flags used by lPerflibConfigFlags
  116. //
  117. // PLCF_NO_ALIGN_ERRORS if set inhibit alignment error messages
  118. // PLCF_NO_DISABLE_DLLS if set, auto disable of bad perf DLL's is inhibited
  119. // PLCF_NO_DLL_TESTING disable all DLL testing for ALL dll's (overrides lExtCounterTestLevel)
  120. // PLCF_ENABLE_TIMEOUT_DISABLE if set then disable when timeout errors occur (unless PLCF_NO_DISABLE_DLLS is set)
  121. // PLCF_ENABLE_PERF_SECTION enable the perflib performance data memory section
  122. //
  123. #define PLCF_NO_ALIGN_ERRORS ((DWORD)0x00000001)
  124. #define PLCF_NO_DISABLE_DLLS ((DWORD)0x00000002)
  125. #define PLCF_NO_DLL_TESTING ((DWORD)0x00000004)
  126. #define PLCF_ENABLE_TIMEOUT_DISABLE ((DWORD)0x00000008)
  127. #define PLCF_ENABLE_PERF_SECTION ((DWORD)0x00000010)
  128. // #define PLCF_DEFAULT PLCF_ENABLE_PERF_SECTION
  129. #define PLCF_DEFAULT PLCF_NO_DLL_TESTING
  130. #define COLL_FLAG_USE_SEPARATE_THREAD 1
  131. #define CTD_AF_NO_ACTION ((DWORD)0x00000000)
  132. #define CTD_AF_CLOSE_THREAD ((DWORD)0x00000001)
  133. #define CTD_AF_OPEN_THREAD ((DWORD)0x00000002)
  134. //
  135. // Constants & Flags used for EXT_OBJECT->dwFlags
  136. //
  137. // use query proc
  138. #define PERF_EO_QUERY_FUNC ((DWORD)0x00000001)
  139. // true when DLL ret. error
  140. #define PERF_EO_BAD_DLL ((DWORD)0x00000002)
  141. // true if lib should not be trimmed
  142. #define PERF_EO_KEEP_RESIDENT ((DWORD)0x00000004)
  143. // true when in query list
  144. #define PERF_EO_OBJ_IN_QUERY ((DWORD)0x80000000)
  145. // set if alignment error has been posted to event log
  146. #define PERF_EO_ALIGN_ERR_POSTED ((DWORD)0x00000008)
  147. // set of the "Disable Performance Counters" value is set
  148. #define PERF_EO_DISABLED ((DWORD)0x00000010)
  149. // set when the DLL is deemed trustworthy
  150. #define PERF_EO_TRUSTED ((DWORD)0x00000020)
  151. // set when the DLL has been replaced with a new file
  152. #define PERF_EO_NEW_FILE ((DWORD)0x00000040)
  153. typedef struct _DLL_VALIDATION_DATA {
  154. FILETIME CreationDate;
  155. LONGLONG FileSize;
  156. } DLL_VALIDATION_DATA, *PDLL_VALIDATION_DATA;
  157. #define EXT_OBJ_INFO_NAME_LENGTH 32
  158. typedef struct _PERFDATA_SECTION_HEADER {
  159. DWORD dwEntriesInUse;
  160. DWORD dwMaxEntries;
  161. DWORD dwMissingEntries;
  162. DWORD dwInitSignature;
  163. BYTE reserved[112];
  164. } PERFDATA_SECTION_HEADER, *PPERFDATA_SECTION_HEADER;
  165. #define PDSH_INIT_SIG ((DWORD)0x01234567)
  166. #define PDSR_SERVICE_NAME_LEN 32
  167. typedef struct _PERFDATA_SECTION_RECORD {
  168. WCHAR szServiceName[PDSR_SERVICE_NAME_LEN];
  169. LONGLONG llElapsedTime;
  170. DWORD dwCollectCount; // number of times Collect successfully called
  171. DWORD dwOpenCount; // number of Loads & opens
  172. DWORD dwCloseCount; // number of Unloads & closes
  173. DWORD dwLockoutCount; // count of lock timeouts
  174. DWORD dwErrorCount; // count of errors (other than timeouts)
  175. DWORD dwLastBufferSize; // size of the last buffer returned
  176. DWORD dwMaxBufferSize; // size of MAX buffer returned
  177. DWORD dwMaxBufferRejected; // size of largest buffer returned as too small
  178. BYTE Reserved[24]; // reserved to make structure 128 bytes
  179. } PERFDATA_SECTION_RECORD, *PPERFDATA_SECTION_RECORD;
  180. //
  181. // Default wait times for perf procs
  182. //
  183. #define CLOSE_WAIT_TIME 5000L // wait time for query mutex (in ms)
  184. #define QUERY_WAIT_TIME 2000L // wait time for query mutex (in ms)
  185. #define OPEN_PROC_WAIT_TIME 10000L // default wait time for open proc to finish (in ms)
  186. #define THROTTLE_PERFLIB(X) PerfpThrottleError( (DWORD)(X), NULL, &PerfpErrorLog )
  187. #define THROTTLE_PERFDLL(X,Y) PerfpThrottleError((DWORD)(X), (Y)->hPerfKey, &(Y)->ErrorLog)
  188. typedef struct _ERROR_LOG {
  189. LIST_ENTRY Entry;
  190. ULONG ErrorNumber;
  191. ULONG ErrorCount;
  192. LONG64 LastTime;
  193. } ERROR_LOG, *PERROR_LOG;
  194. typedef struct _EXT_OBJECT {
  195. struct _EXT_OBJECT *pNext; // pointer to next item in list
  196. HANDLE hMutex; // sync mutex for this function
  197. OPENPROC OpenProc; // address of the open routine
  198. LPSTR szOpenProcName; // open procedure name
  199. LPWSTR szLinkageString; // param for open proc
  200. COLLECTPROC CollectProc; // address of the collect routine
  201. QUERYPROC QueryProc; // address of query proc
  202. LPSTR szCollectProcName; // collect procedure name
  203. DWORD dwCollectTimeout; // wait time in MS for collect proc
  204. DWORD dwOpenTimeout; // wait time in MS for open proc
  205. CLOSEPROC CloseProc; // address of the close routine
  206. LPSTR szCloseProcName; // close procedure name
  207. HANDLE hLibrary ; // handle returned by LoadLibraryW
  208. LPWSTR szLibraryName; // full path of library
  209. HKEY hPerfKey; // handle to performance sub key fo this service
  210. DWORD dwNumObjects; // number of supported objects
  211. DWORD dwObjList[MAX_PERF_OBJECTS_IN_QUERY_FUNCTION]; // address of array of supported objects
  212. DWORD dwFlags; // flags
  213. DWORD dwValidationLevel; // collect function validation/test level
  214. LPWSTR szServiceName; // service name
  215. LONGLONG llLastUsedTime; // FILETIME of last access
  216. DLL_VALIDATION_DATA LibData; // validation data
  217. FILETIME ftLastGoodDllFileDate; // creation date of last successfully accessed DLL
  218. // Performance statistics
  219. PPERFDATA_SECTION_RECORD pPerfSectionEntry; // pointer to entry in global section
  220. LONGLONG llElapsedTime; // time spent in call
  221. DWORD dwCollectCount; // number of times Collect successfully called
  222. DWORD dwOpenCount; // number of Loads & opens
  223. DWORD dwCloseCount; // number of Unloads & closes
  224. DWORD dwLockoutCount; // count of lock timeouts
  225. DWORD dwErrorCount; // count of errors (other than timeouts)
  226. DWORD dwLastBufferSize; // size of the last buffer returned
  227. DWORD dwMaxBufferSize; // size of MAX buffer returned
  228. DWORD dwMaxBufferRejected; // size of largest buffer returned as too small
  229. DWORD dwErrorLimit;
  230. DWORD dwOpenFail; // number of open failures
  231. ERROR_LOG ErrorLog;
  232. DWORD ThreadId; // ThreadId of last failed caller
  233. } EXT_OBJECT, *PEXT_OBJECT;
  234. #define PERF_EOL_ITEM_FOUND ((DWORD)0x00000001)
  235. typedef struct _COLLECT_THREAD_DATA {
  236. DWORD dwQueryType;
  237. LPWSTR lpValueName;
  238. LPBYTE lpData;
  239. LPDWORD lpcbData;
  240. LPVOID *lppDataDefinition;
  241. PEXT_OBJECT pCurrentExtObject;
  242. LONG lReturnValue;
  243. DWORD dwActionFlags;
  244. } COLLECT_THREAD_DATA, * PCOLLECT_THREAD_DATA;
  245. // convert mS to relative time
  246. #define MakeTimeOutValue(ms) ((LONGLONG)((LONG)(ms) * -10000L))
  247. extern DWORD dwThreadAndLibraryTimeout;
  248. extern LONG lEventLogLevel;
  249. extern HANDLE hEventLog;
  250. extern LPVOID lpPerflibSectionAddr;
  251. extern DWORD NumExtensibleObjects;
  252. extern LONG lExtCounterTestLevel;
  253. extern PEXT_OBJECT ExtensibleObjects;
  254. extern HKEY ghKeyPerflib;
  255. extern HANDLE hCollectThread;
  256. extern DWORD dwCollectionFlags;
  257. extern DWORD ComputerNameLength;
  258. extern LPWSTR pComputerName;
  259. extern LONG lPerflibConfigFlags;
  260. extern HANDLE hGlobalDataMutex;
  261. extern HANDLE hExtObjListIsNotInUse;
  262. extern DWORD dwExtObjListRefCount;
  263. extern DWORD dwErrorCount;
  264. //
  265. // Inline functions used by all
  266. //
  267. __inline
  268. LONGLONG
  269. GetTimeAsLongLong ()
  270. /*++
  271. Returns time performance timer converted to ms.
  272. -*/
  273. {
  274. LARGE_INTEGER liCount, liFreq;
  275. LONGLONG llReturn;
  276. if (NtQueryPerformanceCounter (&liCount, &liFreq) == STATUS_SUCCESS) {
  277. llReturn = liCount.QuadPart * 1000 / liFreq.QuadPart;
  278. } else {
  279. llReturn = 0;
  280. }
  281. return llReturn;
  282. }
  283. //
  284. // From utils.h
  285. //
  286. #define LAST_BASE_INDEX 1847
  287. // query types
  288. #define QUERY_GLOBAL 1
  289. #define QUERY_ITEMS 2
  290. #define QUERY_FOREIGN 3
  291. #define QUERY_COSTLY 4
  292. #define QUERY_COUNTER 5
  293. #define QUERY_HELP 6
  294. #define QUERY_ADDCOUNTER 7
  295. #define QUERY_ADDHELP 8
  296. // structure for passing to extensible counter open procedure wait thread
  297. typedef struct _OPEN_PROC_WAIT_INFO {
  298. struct _OPEN_PROC_WAIT_INFO *pNext;
  299. LPWSTR szLibraryName;
  300. LPWSTR szServiceName;
  301. DWORD dwWaitTime;
  302. DWORD dwEventMsg;
  303. LPVOID pData;
  304. } OPEN_PROC_WAIT_INFO, FAR * LPOPEN_PROC_WAIT_INFO;
  305. #define PERFLIB_TIMING_THREAD_TIMEOUT 120000 // 2 min (in milliseconds)
  306. // #define PERFLIB_TIMING_THREAD_TIMEOUT 30000 // 30 sec (for debugging)
  307. extern const WCHAR GLOBAL_STRING[];
  308. extern const WCHAR COSTLY_STRING[];
  309. extern const DWORD VALUE_NAME_LENGTH;
  310. extern const WCHAR DisablePerformanceCounters[];
  311. //
  312. // Registry settings/values supported by Perflib
  313. //
  314. extern const WCHAR DLLValue[];
  315. extern const CHAR OpenValue[];
  316. extern const CHAR CloseValue[];
  317. extern const CHAR CollectValue[];
  318. extern const CHAR QueryValue[];
  319. extern const WCHAR ObjListValue[];
  320. extern const WCHAR LinkageKey[];
  321. extern const WCHAR ExportValue[];
  322. extern const WCHAR PerflibKey[];
  323. extern const WCHAR HKLMPerflibKey[];
  324. extern const WCHAR CounterValue[];
  325. extern const WCHAR HelpValue[];
  326. extern const WCHAR PerfSubKey[];
  327. extern const WCHAR ExtPath[];
  328. extern const WCHAR OpenTimeout[];
  329. extern const WCHAR CollectTimeout[];
  330. extern const WCHAR EventLogLevel[];
  331. extern const WCHAR ExtCounterTestLevel[];
  332. extern const WCHAR OpenProcedureWaitTime[];
  333. extern const WCHAR TotalInstanceName[];
  334. extern const WCHAR LibraryUnloadTime[];
  335. extern const WCHAR KeepResident[];
  336. extern const WCHAR NULL_STRING[];
  337. extern const WCHAR UseCollectionThread[];
  338. extern const WCHAR cszLibraryValidationData[];
  339. extern const WCHAR cszSuccessfulFileData[];
  340. extern const WCHAR cszPerflibFlags[];
  341. extern const WCHAR FirstCounter[];
  342. extern const WCHAR LastCounter[];
  343. extern const WCHAR FirstHelp[];
  344. extern const WCHAR LastHelp[];
  345. extern const WCHAR cszFailureCount[];
  346. extern const WCHAR cszFailureLimit[];
  347. //
  348. // From perfsec.h
  349. //
  350. //
  351. // Value to decide if process names should be collected from:
  352. // the SystemProcessInfo structure (fastest)
  353. // -- or --
  354. // the process's image file (slower, but shows Unicode filenames)
  355. //
  356. #define PNCM_NOT_DEFINED ((LONG)-1)
  357. #define PNCM_SYSTEM_INFO 0L
  358. #define PNCM_MODULE_FILE 1L
  359. //
  360. // Value to decide if the SE_PROFILE_SYSTEM_NAME priv should be checked
  361. //
  362. #define CPSR_NOT_DEFINED ((LONG)-1)
  363. #define CPSR_EVERYONE 0L
  364. #define CPSR_CHECK_ENABLED 1L
  365. #define CPSR_CHECK_PRIVS 1L
  366. //
  367. // Common functions
  368. //
  369. VOID
  370. OpenExtensibleObjects(
  371. );
  372. DWORD
  373. OpenExtObjectLibrary (
  374. PEXT_OBJECT pObj
  375. );
  376. DWORD
  377. CloseExtObjectLibrary (
  378. PEXT_OBJECT pObj,
  379. BOOL bCloseNow
  380. );
  381. LONG
  382. QueryExtensibleData (
  383. COLLECT_THREAD_DATA * pArgs
  384. );
  385. #ifdef _WIN64
  386. DWORD
  387. ExtpAlignBuffer(
  388. PCHAR lpLastBuffer,
  389. PCHAR *lpNextBuffer,
  390. DWORD lpBytesLeft
  391. );
  392. #endif
  393. //
  394. // From perfname.c
  395. //
  396. NTSTATUS
  397. PerfGetNames (
  398. DWORD QueryType,
  399. PUNICODE_STRING lpValueName,
  400. LPBYTE lpData,
  401. LPDWORD lpcbData,
  402. LPDWORD lpcbLen,
  403. LPWSTR lpLangId
  404. );
  405. VOID
  406. PerfGetLangId(
  407. WCHAR *FullLangId
  408. );
  409. VOID
  410. PerfGetPrimaryLangId(
  411. DWORD dwLangId,
  412. WCHAR * PrimaryLangId
  413. );
  414. //
  415. // From utils.c
  416. //
  417. NTSTATUS
  418. GetPerflibKeyValue (
  419. IN LPCWSTR szItem,
  420. IN DWORD dwRegType,
  421. IN DWORD dwMaxSize, // ... of pReturnBuffer in bytes
  422. OUT LPVOID pReturnBuffer,
  423. IN DWORD dwDefaultSize, // ... of pDefault in bytes
  424. IN LPVOID pDefault,
  425. IN OUT PHKEY pKey
  426. );
  427. BOOL
  428. MatchString (
  429. IN LPCWSTR lpValueArg,
  430. IN LPCWSTR lpNameArg
  431. );
  432. DWORD
  433. GetQueryType (
  434. IN LPWSTR lpValue
  435. );
  436. DWORD
  437. GetNextNumberFromList (
  438. IN LPWSTR szStartChar,
  439. IN LPWSTR *szNextChar
  440. );
  441. BOOL
  442. IsNumberInUnicodeList (
  443. IN DWORD dwNumber,
  444. IN LPWSTR lpwszUnicodeList
  445. );
  446. BOOL
  447. MonBuildPerfDataBlock(
  448. PERF_DATA_BLOCK *pBuffer,
  449. PVOID *pBufferNext,
  450. DWORD NumObjectTypes,
  451. DWORD DefaultObject
  452. );
  453. //
  454. // Timer functions
  455. //
  456. HANDLE
  457. StartPerflibFunctionTimer (
  458. IN LPOPEN_PROC_WAIT_INFO pInfo
  459. );
  460. DWORD
  461. KillPerflibFunctionTimer (
  462. IN HANDLE hPerflibTimer
  463. );
  464. DWORD
  465. DestroyPerflibFunctionTimer (
  466. );
  467. LONG
  468. GetPerfDllFileInfo (
  469. LPCWSTR szFileName,
  470. PDLL_VALIDATION_DATA pDllData
  471. );
  472. #define PrivateRegQueryValueExW(a,b,c,d,e,f) \
  473. PrivateRegQueryValueExT(a,(LPVOID)b,c,d,e,f,TRUE)
  474. #define PrivateRegQueryValueExA(a,b,c,d,e,f) \
  475. PrivateRegQueryValueExT(a,(LPVOID)b,c,d,e,f,FALSE)
  476. DWORD
  477. PerfpDosError(
  478. IN NTSTATUS Status
  479. );
  480. #ifdef DBG
  481. VOID
  482. PerfpDebug(
  483. ULONG DebugPrintLevel,
  484. PCCHAR DebugMessage,
  485. ...
  486. );
  487. #define DebugPrint(x) PerfpDebug x
  488. #else
  489. #define DebugPrint(x)
  490. #endif // DBG
  491. DWORD
  492. DisablePerfLibrary (
  493. PEXT_OBJECT pObj,
  494. DWORD dwValue
  495. );
  496. DWORD
  497. DisableLibrary(
  498. IN HKEY hPerfKey,
  499. IN LPWSTR szServiceName,
  500. IN DWORD dwValue
  501. );
  502. /*
  503. DWORD
  504. PerfCheckRegistry(
  505. IN HKEY hPerfKey,
  506. IN LPCWSTR szServiceName
  507. );
  508. */
  509. extern RTL_CRITICAL_SECTION PerfpCritSect;
  510. extern ERROR_LOG PerfpErrorLog;
  511. VOID
  512. PerfpDeleteErrorLogs(
  513. IN PERROR_LOG ErrorLog
  514. );
  515. ULONG
  516. PerfpThrottleError(
  517. IN DWORD ErrorNumber,
  518. IN HKEY hKey,
  519. IN PERROR_LOG ErrorLog
  520. );
  521. //
  522. // From perfsec.c
  523. //
  524. BOOL
  525. TestClientForPriv (
  526. BOOL *pbThread,
  527. LPTSTR szPrivName
  528. );
  529. BOOL
  530. TestClientForAccess (
  531. VOID
  532. );
  533. LONG
  534. GetProcessNameColMeth (
  535. VOID
  536. );
  537. LONG
  538. GetPerfDataAccess (
  539. VOID
  540. );
  541. #endif // _PERFLIB_H_