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.

322 lines
8.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // perflib.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares classes for implementing a PerfMon DLL.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 09/06/1998 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef _PERLIB_H_
  19. #define _PERLIB_H_
  20. #if _MSC_VER >= 1000
  21. #pragma once
  22. #endif
  23. #include <winperf.h>
  24. #include <vector>
  25. // nonstandard extension used : zero-sized array in struct/union
  26. #pragma warning(disable:4200)
  27. // Forward declaration.
  28. class PerfObjectType;
  29. //////////
  30. // Callback function that populates a PerfObjectType prior to collection.
  31. //////////
  32. typedef VOID (WINAPI *PerfDataSource)(PerfObjectType& sink);
  33. //////////
  34. // Struct defining a counter.
  35. //////////
  36. struct PerfCounterDef
  37. {
  38. DWORD nameTitleOffset;
  39. DWORD counterType;
  40. DWORD defaultScale;
  41. DWORD detailLevel;
  42. };
  43. //////////
  44. // Struct defining an object type.
  45. //////////
  46. struct PerfObjectTypeDef
  47. {
  48. DWORD nameTitleOffset;
  49. DWORD numCounters;
  50. PerfCounterDef* counters;
  51. PerfDataSource dataSource; // May be NULL.
  52. BOOL multipleInstances; // TRUE if the type support multiple instances.
  53. DWORD defaultCounter;
  54. };
  55. //////////
  56. // Struct defining the data collector for an application.
  57. //////////
  58. struct PerfCollectorDef
  59. {
  60. PCWSTR name; // Registry key containing the counter offsets.
  61. DWORD numTypes;
  62. PerfObjectTypeDef* types;
  63. };
  64. ///////////////////////////////////////////////////////////////////////////////
  65. //
  66. // CLASS
  67. //
  68. // PerfCounterBlock
  69. //
  70. // DESCRIPTION
  71. //
  72. // Encapsulates a PERF_COUNTER_BLOCK struct.
  73. //
  74. ///////////////////////////////////////////////////////////////////////////////
  75. class PerfCounterBlock
  76. {
  77. public:
  78. // Returns a pointer to the internal counter array.
  79. PDWORD getCounters() throw ()
  80. { return counters; }
  81. // Zero out all the counters.
  82. void zeroCounters() throw ()
  83. { memset(counters, 0, pcb.ByteLength - sizeof(PERF_COUNTER_BLOCK)); }
  84. // Collects PerfMon data into the buffer bounded by 'first' and 'last'.
  85. // Returns a pointer to the end of the collected data.
  86. PBYTE collect(PBYTE first, PBYTE last);
  87. // Create a new PerfCounterBlock object.
  88. static PerfCounterBlock* create(DWORD numDWORDs);
  89. protected:
  90. // Constructor is protected since new objects may only be instantiated
  91. // through the 'create' method.
  92. PerfCounterBlock() throw () { }
  93. PERF_COUNTER_BLOCK pcb;
  94. DWORD counters[0];
  95. private:
  96. // Not implemented.
  97. PerfCounterBlock(const PerfCounterBlock&);
  98. PerfCounterBlock& operator=(const PerfCounterBlock&);
  99. };
  100. ///////////////////////////////////////////////////////////////////////////////
  101. //
  102. // CLASS
  103. //
  104. // PerfInstanceDefinition
  105. //
  106. // DESCRIPTION
  107. //
  108. // Encapsulates a PERF_INSTANCE_DEFINITION struct.
  109. //
  110. ///////////////////////////////////////////////////////////////////////////////
  111. class PerfInstanceDefinition
  112. {
  113. public:
  114. // Returns the name of the instance; may be null.
  115. PCWSTR getName() const throw ()
  116. { return pid.NameLength ? name : NULL; }
  117. // Returns the Unique ID of the instance.
  118. LONG getUniqueID() const throw ()
  119. { return pid.UniqueID; }
  120. PBYTE collect(PBYTE first, PBYTE last);
  121. // Create a new PerfInstanceDefinition object.
  122. static PerfInstanceDefinition* create(
  123. PCWSTR name, // May be NULL.
  124. LONG uniqueID
  125. );
  126. protected:
  127. // Constructor is protected since new objects may only be instantiated
  128. // through the 'create' method.
  129. PerfInstanceDefinition() throw () { }
  130. PERF_INSTANCE_DEFINITION pid;
  131. WCHAR name[0];
  132. private:
  133. // Not implemented.
  134. PerfInstanceDefinition(const PerfInstanceDefinition&);
  135. PerfInstanceDefinition& operator=(const PerfInstanceDefinition&);
  136. };
  137. ///////////////////////////////////////////////////////////////////////////////
  138. //
  139. // CLASS
  140. //
  141. // PerfInstance
  142. //
  143. // DESCRIPTION
  144. //
  145. // Represents an instance of a particular performance object type. Consists
  146. // of an optional PerfInstanceDefinition followed by a mandatory
  147. // PerfCounterBlock
  148. //
  149. ///////////////////////////////////////////////////////////////////////////////
  150. class PerfInstance
  151. {
  152. public:
  153. PerfInstance(DWORD numCounters)
  154. : pcb(PerfCounterBlock::create(numCounters))
  155. { }
  156. PerfInstance(PCWSTR name, LONG uniqueID, DWORD numCounters)
  157. : pid(PerfInstanceDefinition::create(name, uniqueID)),
  158. pcb(PerfCounterBlock::create(numCounters))
  159. { }
  160. // Returns a pointer to the internal counter array.
  161. PDWORD getCounters() throw ()
  162. { return pcb->getCounters(); }
  163. PBYTE collect(PBYTE first, PBYTE last);
  164. protected:
  165. std::auto_ptr<PerfInstanceDefinition> pid;
  166. std::auto_ptr<PerfCounterBlock> pcb;
  167. private:
  168. // Not implemented.
  169. PerfInstance(const PerfInstance&);
  170. PerfInstance& operator=(const PerfInstance&);
  171. };
  172. ///////////////////////////////////////////////////////////////////////////////
  173. //
  174. // CLASS
  175. //
  176. // PerfObjectType
  177. //
  178. // DESCRIPTION
  179. //
  180. // Encapsulates a performance object type containing zero or more
  181. // PerfInstance's.
  182. //
  183. ///////////////////////////////////////////////////////////////////////////////
  184. class PerfObjectType
  185. {
  186. public:
  187. typedef std::vector<PerfInstance*> MyVec;
  188. typedef MyVec::size_type size_type;
  189. ~PerfObjectType() throw ();
  190. // Access a given instance of this type.
  191. PerfInstance& operator[](size_type pos) throw ()
  192. { return *instances[pos]; }
  193. PerfInstance& at(size_type pos) throw ()
  194. { return *instances[pos]; }
  195. // Returns the index used by PerfMon to identify this object type.
  196. DWORD getIndex() const throw ()
  197. { return pot.ObjectNameTitleIndex; }
  198. // Clears all instances.
  199. void clear() throw ();
  200. // Reserves space for at least 'N' instances.
  201. void reserve(size_type N)
  202. { instances.reserve(N); }
  203. // Returns the number of instances.
  204. size_type size() const throw ()
  205. { return instances.size(); }
  206. // Add a new instance of this type.
  207. void addInstance(
  208. PCWSTR name = NULL,
  209. LONG uniqueID = PERF_NO_UNIQUE_ID
  210. );
  211. // Collect performance data for this object type.
  212. PBYTE collect(PBYTE first, PBYTE last);
  213. // Create a new PerfObjectType.
  214. static PerfObjectType* create(const PerfObjectTypeDef& def);
  215. protected:
  216. // Constructor is protected since new objects may only be instantiated
  217. // through the 'create' method.
  218. PerfObjectType() throw () { }
  219. PerfDataSource dataSource; // Callback for populating objects.
  220. MyVec instances; // Vector of existing instances.
  221. DWORD numDWORDs; // DWORD's of data for each instance.
  222. PERF_OBJECT_TYPE pot;
  223. PERF_COUNTER_DEFINITION pcd[0];
  224. private:
  225. // Not implemented.
  226. PerfObjectType(const PerfObjectType&);
  227. PerfObjectType& operator=(const PerfObjectType&);
  228. };
  229. ///////////////////////////////////////////////////////////////////////////////
  230. //
  231. // CLASS
  232. //
  233. // PerfCollector
  234. //
  235. // DESCRIPTION
  236. //
  237. // Maintains all the PerfObjectType's for a given application.
  238. //
  239. ///////////////////////////////////////////////////////////////////////////////
  240. class PerfCollector
  241. {
  242. public:
  243. typedef size_t size_type;
  244. PerfCollector() throw ()
  245. : types(NULL)
  246. { }
  247. ~PerfCollector() throw ();
  248. // Access a given object type.
  249. PerfObjectType& operator[](size_type pos) throw ()
  250. { return *(types[pos]); }
  251. // Clears all instances (but not all PerfObjectType's).
  252. void clear() throw ();
  253. // Initialize the collector for use.
  254. void open(const PerfCollectorDef& def);
  255. // Collect performance data for the indicated types.
  256. void collect(
  257. PCWSTR values,
  258. PVOID& data,
  259. DWORD& numBytes,
  260. DWORD& numTypes
  261. );
  262. // Shutdown the collector.
  263. void close() throw ();
  264. protected:
  265. PerfObjectType** types;
  266. private:
  267. // Not implemented.
  268. PerfCollector(PerfCollector&);
  269. PerfCollector& operator=(PerfCollector&);
  270. };
  271. #endif // _PERLIB_H_