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.

290 lines
6.8 KiB

  1. /*++
  2. Copyright (C) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. utils.h
  5. Abstract:
  6. <abstract>
  7. --*/
  8. #ifndef _WBEMPERF_UTILS_H_
  9. #define _WBEMPERF_UTILS_H_
  10. #ifdef __cplusplus
  11. /*
  12. #define Macro_CloneLPWSTR(x) \
  13. (x ? wcscpy(new wchar_t[wcslen(x) + 1], x) : 0)
  14. */
  15. __inline wchar_t* Macro_CloneLPWSTR(wchar_t *x)
  16. {
  17. wchar_t *s;
  18. if (!x) return NULL;
  19. s = new wchar_t[wcslen(x) + 1];
  20. if (!s) return NULL;
  21. return wcscpy(s, x);
  22. }
  23. extern "C" {
  24. #endif
  25. #include <windows.h>
  26. #include <winperf.h>
  27. #ifndef _OUTPUT_DEBUG_STRINGS
  28. #define _OUTPUT_DEBUG_STRINGS 0
  29. #endif
  30. #ifndef _DEBUG_MUTEXES
  31. #define _DEBUG_MUTEXES 0 // for debugging locks
  32. #endif // _DEBUG_MUTEXES undefined
  33. extern HANDLE hEventLog;
  34. #define ALLOCMEM(heap, flags, size) HeapAlloc (heap, flags, size)
  35. #define REALLOCMEM(heap, flags, pointer, newsize) \
  36. HeapReAlloc(heap, flags, pointer, newsize)
  37. #define FREEMEM(heap, flags, pointer) HeapFree (heap, flags, pointer)
  38. // convert mS to relative time
  39. #define MakeTimeOutValue(ms) ((LONGLONG)((LONG)(ms) * -10000L))
  40. #define CLOSE_WAIT_TIME 5000L // wait time for query mutex (in ms)
  41. #define QUERY_WAIT_TIME 2000L // wait time for query mutex (in ms)
  42. #define OPEN_PROC_WAIT_TIME 10000L // default wait time for open proc to finish (in ms)
  43. // query types
  44. #define QUERY_GLOBAL 1
  45. #define QUERY_ITEMS 2
  46. #define QUERY_FOREIGN 3
  47. #define QUERY_COSTLY 4
  48. #define QUERY_COUNTER 5
  49. #define QUERY_HELP 6
  50. #define QUERY_ADDCOUNTER 7
  51. #define QUERY_ADDHELP 8
  52. extern const WCHAR GLOBAL_STRING[];
  53. extern const WCHAR COSTLY_STRING[];
  54. extern const DWORD VALUE_NAME_LENGTH;
  55. //
  56. // Utility macro. This is used to reserve a DWORD multiple of
  57. // bytes for Unicode strings embedded in the definitional data,
  58. // viz., object instance names.
  59. //
  60. #define DWORD_MULTIPLE(x) (((x+sizeof(DWORD)-1)/sizeof(DWORD))*sizeof(DWORD))
  61. // (assumes dword is 4 bytes)
  62. #define ALIGN_ON_DWORD(x) ((VOID *)(((DWORD_PTR)x & 3) ? (((DWORD_PTR)x & ~3) + 4) : ((DWORD_PTR)x)))
  63. #define QWORD_MULTIPLE(x) (((x+sizeof(LONGLONG)-1)/sizeof(LONGLONG))*sizeof(LONGLONG))
  64. // (assumes quadword is 8 bytes)
  65. #define ALIGN_ON_QWORD(x) ((VOID *)(((DWORD_PTR)x & 7) ? (((DWORD_PTR)x & ~7) + 8) : ((DWORD_PTR)x)))
  66. //
  67. // function prototypes
  68. //
  69. BOOL
  70. MonBuildPerfDataBlock(
  71. PERF_DATA_BLOCK *pBuffer,
  72. PVOID *pBufferNext,
  73. DWORD NumObjectTypes,
  74. DWORD DefaultObject
  75. );
  76. LONG
  77. GetPerflibKeyValue (
  78. LPCWSTR szItem,
  79. DWORD dwRegType,
  80. DWORD dwMaxSize, // ... of pReturnBuffer in bytes
  81. LPVOID pReturnBuffer,
  82. DWORD dwDefaultSize, // ... of pDefault in bytes
  83. LPVOID pDefault
  84. );
  85. BOOL
  86. MatchString (
  87. IN LPCWSTR lpValueArg,
  88. IN LPCWSTR lpNameArg
  89. );
  90. DWORD
  91. GetQueryType (
  92. IN LPWSTR lpValue
  93. );
  94. DWORD
  95. GetNextNumberFromList (
  96. IN LPWSTR szStartChar,
  97. IN LPWSTR *szNextChar
  98. );
  99. BOOL
  100. IsNumberInUnicodeList (
  101. IN DWORD dwNumber,
  102. IN LPWSTR lpwszUnicodeList
  103. );
  104. LPWSTR
  105. ConvertProcName(
  106. IN LPSTR strProcName
  107. );
  108. #if _DEBUG_MUTEXES
  109. #include <stdio.h>
  110. __inline
  111. void
  112. PdhiLocalWaitForMutex (
  113. LPCSTR szSourceFileName,
  114. DWORD dwLineNo,
  115. HANDLE hMutex
  116. )
  117. {
  118. DWORD dwReturnValue;
  119. CHAR szOutputString[MAX_PATH];
  120. FILETIME ft;
  121. if (hMutex != NULL) {
  122. GetSystemTimeAsFileTime (&ft);
  123. dwReturnValue = WaitForSingleObject (hMutex, 10000);
  124. sprintf (szOutputString, "\n[%8.8x] Mutex [%8.8x] %s by (%d) at: %s (%d)",
  125. ft.dwLowDateTime,
  126. (DWORD)hMutex,
  127. (dwReturnValue == 0 ? "Locked" : "Lock Failed"),
  128. GetCurrentThreadId(),
  129. szSourceFileName, dwLineNo);
  130. } else {
  131. sprintf (szOutputString, "\nLock of NULL Mutex attmpted at: %s (%d)",
  132. szSourceFileName, dwLineNo);
  133. }
  134. OutputDebugStringA (szOutputString);
  135. }
  136. #define WAIT_FOR_AND_LOCK_MUTEX(h) PdhiLocalWaitForMutex (__FILE__, __LINE__, h);
  137. __inline
  138. void
  139. PdhiLocalReleaseMutex (
  140. LPCSTR szSourceFileName,
  141. DWORD dwLineNo,
  142. HANDLE hMutex
  143. )
  144. {
  145. BOOL bSuccess;
  146. CHAR szOutputString[MAX_PATH];
  147. LONG lPrevCount = 0;
  148. FILETIME ft;
  149. if (hMutex != NULL) {
  150. GetSystemTimeAsFileTime (&ft);
  151. bSuccess = ReleaseMutex (hMutex);
  152. sprintf (szOutputString, "\n[%8.8x] Mutex [%8.8x] %s by (%d) at: %s (%d)",
  153. ft.dwLowDateTime,
  154. (DWORD)hMutex,
  155. (bSuccess ? "Released" : "Release Failed"),
  156. GetCurrentThreadId(),
  157. szSourceFileName, dwLineNo);
  158. } else {
  159. sprintf (szOutputString, "\nRelease of NULL Mutex attempted at: %s (%d)",
  160. szSourceFileName, dwLineNo);
  161. }
  162. OutputDebugStringA (szOutputString);
  163. }
  164. #define RELEASE_MUTEX(h) PdhiLocalReleaseMutex (__FILE__, __LINE__, h);
  165. #else
  166. // 10 second wait timeout
  167. #define WAIT_FOR_AND_LOCK_MUTEX(h) (h != NULL ? WaitForSingleObject(h, 10000) : WAIT_TIMEOUT)
  168. #define RELEASE_MUTEX(h) (h != NULL ? ReleaseMutex(h) : FALSE)
  169. #endif
  170. __inline
  171. DWORD
  172. RegisterExtObjListAccess ()
  173. {
  174. #if 0
  175. LONG Status;
  176. if (hGlobalDataMutex != NULL) {
  177. // wait for access to the list of ext objects
  178. Status = WaitForSingleObject (
  179. hGlobalDataMutex,
  180. QUERY_WAIT_TIME);
  181. if (Status != WAIT_TIMEOUT) {
  182. if (hExtObjListIsNotInUse != NULL) {
  183. // indicate that we are going to use the list
  184. InterlockedIncrement (&dwExtObjListRefCount);
  185. if (dwExtObjListRefCount > 0) {
  186. ResetEvent (hExtObjListIsNotInUse); // indicate list is busy
  187. } else {
  188. SetEvent (hExtObjListIsNotInUse); // indicate list is not busy
  189. }
  190. Status = ERROR_SUCCESS;
  191. } else {
  192. Status = ERROR_NOT_READY;
  193. }
  194. ReleaseMutex (hGlobalDataMutex);
  195. } // else return status;
  196. } else {
  197. Status = ERROR_LOCK_FAILED;
  198. }
  199. return Status;
  200. #else
  201. return ERROR_SUCCESS;
  202. #endif
  203. }
  204. __inline
  205. DWORD
  206. DeRegisterExtObjListAccess ()
  207. {
  208. #if 0
  209. LONG Status;
  210. if (hGlobalDataMutex != NULL) {
  211. // wait for access to the list of ext objects
  212. Status = WaitForSingleObject (
  213. hGlobalDataMutex, QUERY_WAIT_TIME);
  214. if (Status != WAIT_TIMEOUT) {
  215. if (hExtObjListIsNotInUse != NULL) {
  216. assert (dwExtObjListRefCount > 0);
  217. // indicate that we are going to use the list
  218. InterlockedDecrement (&dwExtObjListRefCount);
  219. if (dwExtObjListRefCount > 0) {
  220. ResetEvent (hExtObjListIsNotInUse); // indicate list is busy
  221. } else {
  222. SetEvent (hExtObjListIsNotInUse); // indicate list is not busy
  223. }
  224. Status = ERROR_SUCCESS;
  225. } else {
  226. Status = ERROR_NOT_READY;
  227. }
  228. ReleaseMutex (hGlobalDataMutex);
  229. } // else return status;
  230. } else {
  231. Status = ERROR_LOCK_FAILED;
  232. }
  233. return Status;
  234. #else
  235. return ERROR_SUCCESS;
  236. #endif
  237. }
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif //_PERFLIB_UTILS_H_