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.

283 lines
9.3 KiB

  1. #ifndef __PERFAPI_H__
  2. #define __PERFAPI_H__
  3. #include <winperf.h>
  4. typedef DWORD INSTANCE_ID;
  5. typedef DWORD COUNTER_ID;
  6. #define MAX_COUNTERS 128
  7. #define MAX_INSTANCES_PER_OBJECT 32
  8. #define MAX_PERF_OBJECTS 64
  9. #define BITS_IN_DWORD (sizeof(DWORD) << 3)
  10. #ifdef __PERF_APP_API
  11. #define PERF_APP_API __declspec(dllexport)
  12. #else
  13. #define PERF_APP_API __declspec(dllimport)
  14. #endif
  15. /* NOTE: The member functions for all classes are in-line */
  16. #ifdef __cplusplus
  17. // CPerfObject Class
  18. class PERF_APP_API CPerfObject {
  19. private:
  20. HANDLE hObject; // Handle to Performance Object
  21. BOOL bWithInstances; // True, if object has instances
  22. BOOL bValid; // True, if the PerfMon object is OK. False, if there had been an exception thrown.
  23. DWORD cCounters; // # of created counters for the object
  24. DWORD dwCounterOffsets[MAX_COUNTERS]; // The object's counter offsets
  25. DWORD bCounterSize[MAX_COUNTERS / BITS_IN_DWORD + 1]; // bitmap of counter sizes. If bit is 0, counter is a DWORD, otherwise (bit is 1), it's a LARGE_INTEGER.
  26. INSTANCE_ID iidInstances[MAX_INSTANCES_PER_OBJECT]; // The object's instance ids
  27. BOOL bOriginal[MAX_INSTANCES_PER_OBJECT]; // True, if the instance is original. Then, it needs to be destroyed.
  28. PBYTE lpInstanceAddr[MAX_INSTANCES_PER_OBJECT]; // The object's instances' start addresses. If the object has no instances, the 0-th address is the object counter data start
  29. friend class CPerfCounter;
  30. public:
  31. CPerfObject (void) { bValid = FALSE; };
  32. BOOL Create (char *pTitle, BOOL bHasInstances = TRUE, char *pHelp = NULL, DWORD nSize = 0);
  33. BOOL Create (WCHAR *pTitle, BOOL bHasInstances = TRUE, WCHAR *pHelp = NULL, DWORD nSize = 0);
  34. ~CPerfObject (void);
  35. COUNTER_ID CreateCounter (char *pCounterName, DWORD dwType = PERF_COUNTER_COUNTER, DWORD dwScale = 0, DWORD dwSize = sizeof(DWORD), char *pHelp = NULL);
  36. COUNTER_ID CreateCounter (WCHAR *pCounterName, DWORD dwType = PERF_COUNTER_COUNTER, DWORD dwScale = 0, DWORD dwSize = sizeof(DWORD), WCHAR *pHelp = NULL);
  37. INSTANCE_ID CreateInstance (char *pInstanceName);
  38. INSTANCE_ID CreateInstance (WCHAR *pInstanceName);
  39. BOOL DestroyInstance (INSTANCE_ID iid);
  40. };
  41. // CPerfCounter Class
  42. class PERF_APP_API CPerfCounter {
  43. private:
  44. LPDWORD pAddr; // Read address of the counter
  45. BOOL bDword; // If TRUE, the counter is a DWORD, otherwise, it's a LARGE_INTEGER
  46. public:
  47. CPerfCounter (void) { pAddr = NULL; };
  48. BOOL Create (CPerfObject &cpoObject, COUNTER_ID idCounter, INSTANCE_ID idInstance = (INSTANCE_ID) -1);
  49. ~CPerfCounter (void) { };
  50. CPerfCounter & operator = (DWORD nNewValue)
  51. {
  52. if (pAddr)
  53. *pAddr = nNewValue;
  54. return *this;
  55. };
  56. CPerfCounter & operator = (LARGE_INTEGER nNewValue)
  57. {
  58. if (pAddr)
  59. ((LARGE_INTEGER *) pAddr)->QuadPart = nNewValue.QuadPart;
  60. return *this;
  61. };
  62. CPerfCounter & operator = (const CPerfCounter &PerfCtr)
  63. {
  64. if (pAddr && PerfCtr.pAddr) {
  65. if (bDword != PerfCtr.bDword)
  66. return *this;
  67. if (bDword)
  68. *pAddr = *(PerfCtr.pAddr);
  69. else
  70. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) PerfCtr.pAddr)->QuadPart;
  71. }
  72. return *this;
  73. };
  74. CPerfCounter & operator += (DWORD nValue)
  75. {
  76. if (pAddr)
  77. *pAddr += nValue;
  78. return *this;
  79. };
  80. CPerfCounter & operator += (LARGE_INTEGER nValue)
  81. {
  82. if (pAddr)
  83. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart + nValue.QuadPart;
  84. return *this;
  85. };
  86. CPerfCounter & operator += (const CPerfCounter &PerfCtr)
  87. {
  88. if (pAddr && PerfCtr.pAddr) {
  89. if (bDword != PerfCtr.bDword)
  90. return *this;
  91. if (bDword)
  92. *pAddr += *(PerfCtr.pAddr);
  93. else
  94. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart + ((LARGE_INTEGER *) PerfCtr.pAddr)->QuadPart;
  95. }
  96. return *this;
  97. };
  98. CPerfCounter & operator -= (DWORD nValue)
  99. {
  100. if (pAddr)
  101. *pAddr -= nValue;
  102. return *this;
  103. };
  104. CPerfCounter & operator -= (LARGE_INTEGER nValue)
  105. {
  106. if (pAddr)
  107. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart - nValue.QuadPart;
  108. return *this;
  109. };
  110. CPerfCounter & operator -= (const CPerfCounter &PerfCtr)
  111. {
  112. if (pAddr && PerfCtr.pAddr) {
  113. if (bDword != PerfCtr.bDword)
  114. return *this;
  115. if (bDword)
  116. *pAddr -= *(PerfCtr.pAddr);
  117. else
  118. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart - ((LARGE_INTEGER *) PerfCtr.pAddr)->QuadPart;
  119. }
  120. return *this;
  121. };
  122. CPerfCounter & operator ++ (void)
  123. {
  124. if (pAddr) {
  125. if (bDword)
  126. (*pAddr)++;
  127. else
  128. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart + 1;
  129. }
  130. return *this;
  131. };
  132. CPerfCounter & operator ++ (int dummy)
  133. {
  134. if (pAddr) {
  135. if (bDword)
  136. (*pAddr)++;
  137. else
  138. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart + 1;
  139. }
  140. return *this;
  141. };
  142. CPerfCounter & operator -- (void)
  143. {
  144. if (pAddr) {
  145. if (bDword)
  146. (*pAddr)--;
  147. else
  148. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart - 1;
  149. }
  150. return *this;
  151. };
  152. CPerfCounter & operator -- (int dummy)
  153. {
  154. if (pAddr) {
  155. if (bDword)
  156. (*pAddr)--;
  157. else
  158. ((LARGE_INTEGER *) pAddr)->QuadPart = ((LARGE_INTEGER *) pAddr)->QuadPart - 1;
  159. }
  160. return *this;
  161. };
  162. operator DWORD ()
  163. {
  164. if (pAddr == NULL)
  165. return 0;
  166. return (*pAddr);
  167. };
  168. operator LARGE_INTEGER ()
  169. {
  170. if (pAddr == NULL) {
  171. LARGE_INTEGER li;
  172. li.QuadPart = 0;
  173. return li;
  174. }
  175. return (* (LARGE_INTEGER *) pAddr);
  176. };
  177. };
  178. #endif
  179. #ifdef __cplusplus
  180. extern "C" {
  181. #endif
  182. /*
  183. MakeAPerfObject:: Creates a logical perf object, under which one can create
  184. counters. This perf object shows up as an object in Perfmon and the counters
  185. showup under it. A maximum number of instances can be specified. (Currently
  186. this is hardcoded to 16). And instances can be dynamically created and
  187. destroyed.
  188. An object can be removed with the DestroyObject API. The handle returned by
  189. the MakeObject is used to denote the object. Note this removes all the
  190. instances (and all the counters) that were associated with the Object.
  191. All pointers returned by the MakeCounter become invalidated and the risk is on
  192. the API user not to reference them anymore.
  193. */
  194. PERF_APP_API HANDLE _stdcall MakeAPerfObjectW(PWCHAR pTitle, PWCHAR pHelp, DWORD nSize, BOOL bHasInstances, PVOID *lppObjectStart) ;
  195. PERF_APP_API HANDLE _stdcall MakeAPerfObjectA(char * pTitle, char *pHelp, DWORD nSize, BOOL bHasInstances, PVOID *lppObjectStart) ;
  196. PERF_APP_API BOOL _stdcall DestroyPerfObject(HANDLE) ;
  197. /*
  198. MakeAPerfCounter:: Returns the Offset of the counter from the start of the
  199. object.
  200. */
  201. PERF_APP_API DWORD _stdcall MakeAPerfCounterW(DWORD dwType, DWORD dwScale, DWORD dwSize, HANDLE hObject, PWCHAR pDesc, PWCHAR pHelp);
  202. PERF_APP_API DWORD _stdcall MakeAPerfCounterA(DWORD dwType, DWORD dwScale, DWORD dwSize, HANDLE hObject, char * pDesc, char * pHelp);
  203. PERF_APP_API HANDLE _stdcall MakeAPerfCounterHandleW(DWORD dwType, DWORD dwScale, DWORD dwSize, HANDLE hObject, PWSTR pCounterName, PWSTR pHelp);
  204. PERF_APP_API HANDLE _stdcall MakeAPerfCounterHandleA(DWORD dwType, DWORD dwScale, DWORD dwSize, HANDLE hObject,char * pCounterName, char * pHelp);
  205. PERF_APP_API BOOL _stdcall DestroyPerfCounterHandle(HANDLE pCounterInfo);
  206. /*
  207. MakeAPerfInstance - creates a new instance of an already existing Object and
  208. duplicates all its counters, further any counter created for the object gets
  209. duplicated too.
  210. If the return HANDLE is NULL then the API failed else success.
  211. The returned HANDLE can be used to Destroy the object instance.
  212. The final form of this api should be
  213. MakeAPerfInstanceW(HANDLE, PWCHAR pInstName) ;
  214. The object name has to be replaced by the object handle returned by MakeObject
  215. */
  216. PERF_APP_API INSTANCE_ID _stdcall MakeAPerfInstanceW(HANDLE hObject, PWCHAR pInstName, PVOID *lppInstanceStart) ;
  217. PERF_APP_API INSTANCE_ID _stdcall MakeAPerfInstanceA(HANDLE hObject, char *pInstName, PVOID *lppInstanceStart) ;
  218. PERF_APP_API HANDLE _stdcall MakeAPerfInstanceHandleW(HANDLE hObject, PWCHAR pInstName);
  219. PERF_APP_API HANDLE _stdcall MakeAPerfInstanceHandleA(HANDLE hObject, char *pInstName);
  220. PERF_APP_API BOOL _stdcall DestroyPerfInstance(INSTANCE_ID iID);
  221. PERF_APP_API BOOL _stdcall DestroyPerfInstanceHandle(HANDLE pInstanceInfo);
  222. PERF_APP_API BOOL _stdcall SetCounterValueByHandle(HANDLE hCounter, HANDLE hParent, PBYTE pbNewValue);
  223. PERF_APP_API BOOL _stdcall GetCounterValueByHandle(HANDLE hCounter, HANDLE hParent, PBYTE pbCounterValue);
  224. PERF_APP_API BOOL _stdcall IncrementCounterByHandle(HANDLE hCounter, HANDLE hParent, PBYTE pbIncrement);
  225. PERF_APP_API BOOL _stdcall DecrementCounterByHandle(HANDLE hCounter, HANDLE hParent, PBYTE pbDecrement);
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #ifdef UNICODE
  230. #define MakeAPerfObject MakeAPerfObjectW
  231. #define MakeAPerfCounter MakeAPerfCounterW
  232. #define MakeAPerfCounterHandle MakeAPerfCounterHandleW
  233. #define MakeAPerfInstance MakeAPerfInstanceW
  234. #define MakeAPerfInstanceHandle MakeAPerfInstanceHandleW
  235. #else
  236. #define MakeAPerfObject MakeAPerfObjectA
  237. #define MakeAPerfCounter MakeAPerfCounterA
  238. #define MakeAPerfCounterHandle MakeAPerfCounterHandleA
  239. #define MakeAPerfInstance MakeAPerfInstanceA
  240. #define MakeAPerfInstanceHandle MakeAPerfInstanceHandleA
  241. #endif //UNICODE
  242. #endif // __PERFAPI_H__