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.

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