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.

314 lines
8.4 KiB

  1. /*
  2. - perfapp.hpp
  3. -
  4. * Purpose:
  5. * Declare Perfmon Classes
  6. *
  7. * Notes:
  8. * User must define two zero-based enums representing the PerfMon counters:
  9. * GLOBAL_CNTR Global Perfmon Counters
  10. * INST_CNTR Instance Perfmon Counters
  11. * They must be defined before including this header.
  12. *
  13. * Copyright:
  14. *
  15. *
  16. */
  17. #if !defined(_PERFAPP_HPP_)
  18. #define _PERFAPP_HPP_
  19. #pragma once
  20. #include <perfcommon.h>
  21. // Forward Reference for Lib
  22. enum GLOBAL_CNTR;
  23. enum INST_CNTR;
  24. /*
  25. - class GLOBCNTR
  26. -
  27. * Purpose:
  28. * Object that encapsulates the global perfmon counter for DMI.
  29. *
  30. */
  31. class GLOBCNTR
  32. {
  33. private:
  34. //
  35. // Data Members
  36. HANDLE m_hsm; // Shared Memory
  37. DWORD m_cCounters; // # of counters
  38. DWORD * m_rgdwPerfData; // Counters
  39. BOOL m_fInit; // Init test flag
  40. // For Shared Memory
  41. SECURITY_ATTRIBUTES m_sa;
  42. public:
  43. // Constructor
  44. //
  45. // Declared private to ensure that arbitrary instances
  46. // of this class cannot be created. The Singleton
  47. // template (declared as a friend above) controls
  48. // the sole instance of this class.
  49. //
  50. GLOBCNTR() :
  51. m_hsm(NULL),
  52. m_cCounters(0),
  53. m_rgdwPerfData(NULL),
  54. m_fInit(FALSE)
  55. {
  56. m_sa.lpSecurityDescriptor=NULL;
  57. };
  58. ~GLOBCNTR()
  59. {
  60. if(m_sa.lpSecurityDescriptor)
  61. LocalFree(m_sa.lpSecurityDescriptor);
  62. };
  63. // Parameters:
  64. // cCounters Total number of global counters. ("special" last element in GLOB_CNTR)
  65. // wszGlobalSMName Name of the shared memory block (shared with DLL)
  66. // wszSvcName Service Name (for event logging)
  67. HRESULT HrInit(GLOBAL_CNTR cCounters,
  68. LPWSTR szGlobalSMName,
  69. LPWSTR szSvcName);
  70. void Shutdown(void);
  71. void IncPerfCntr(GLOBAL_CNTR cntr);
  72. void DecPerfCntr(GLOBAL_CNTR cntr);
  73. void SetPerfCntr(GLOBAL_CNTR cntr, DWORD dw);
  74. void AddPerfCntr(GLOBAL_CNTR cntr, DWORD dw);
  75. void SubPerfCntr(GLOBAL_CNTR cntr, DWORD dw);
  76. LONG LGetPerfCntr(GLOBAL_CNTR cntr);
  77. private:
  78. // Not Implemented to prevent compiler from auto-generating
  79. //
  80. GLOBCNTR(const GLOBCNTR& x);
  81. GLOBCNTR& operator=(const GLOBCNTR& x);
  82. };
  83. /*
  84. - class INSTCNTR
  85. -
  86. * Purpose:
  87. * Class used to manipulate the per instance PerfMon counters for DMI.
  88. *
  89. * Notes:
  90. * This manages two shared memory blocks: The first manages the per instance
  91. * info, and whether or not that instance record is in use. The second is an array
  92. * of counter blocks; each counter block is an array of however many Instance Counters
  93. * the user says there are (passed in to HrInit()). These blocks correspond to
  94. * m_hsmAdm and m_hsmCntr, respectively.
  95. *
  96. */
  97. class INSTCNTR
  98. {
  99. // Constructor
  100. //
  101. // Declared private to ensure that arbitrary instances
  102. // of this class cannot be created. The Singleton
  103. // template (declared as a friend above) controls
  104. // the sole instance of this class.
  105. INSTCNTR() :
  106. m_hsmAdm(NULL),
  107. m_hsmCntr(NULL),
  108. m_hmtx(NULL),
  109. m_rgInstRec(NULL),
  110. m_rgdwCntr(NULL),
  111. m_cCounters(0),
  112. m_fInit(FALSE)
  113. {
  114. m_sa.lpSecurityDescriptor=NULL;
  115. };
  116. ~INSTCNTR();
  117. // Private Data Members
  118. HANDLE m_hsmAdm; // Admin Shared Memory
  119. HANDLE m_hsmCntr; // Counters Shared Memory
  120. HANDLE m_hmtx; // Mutex controlling Shared Memory
  121. INSTCNTR_DATA * m_picd; // Perf Counter Data
  122. INSTREC * m_rgInstRec; // Array of Instance Records
  123. DWORD * m_rgdwCntr; // Array of Instance Counter Blocks
  124. DWORD m_cCounters; // # Counters
  125. BOOL m_fInit; // Init test flag
  126. // For Shared Memory
  127. SECURITY_ATTRIBUTES m_sa;
  128. public:
  129. // NOTE: Use the Singleton mechanisms for Creating, Destroying and
  130. // obtaining an instance of this object
  131. // Parameters:
  132. // cCounters Total number of global counters. ("special" last element in INST_CNTR)
  133. // wszInstSMName Name of the shared memory block (shared with DLL)
  134. // wszInstMutexName Name of the Mutex controlling the instance memory blocks
  135. // wszSvcName Service Name (for event logging)
  136. HRESULT HrInit(INST_CNTR cCounters,
  137. LPWSTR szInstSMName,
  138. LPWSTR szInstMutexName,
  139. LPWSTR szSvcName);
  140. void Shutdown(BOOL fWipeOut=TRUE);
  141. HRESULT HrCreateOrGetInstance(IN LPCWSTR wszInstName,
  142. OUT INSTCNTR_ID *picid);
  143. HRESULT HrDestroyInstance(INSTCNTR_ID icid);
  144. void IncPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr);
  145. void DecPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr);
  146. void SetPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw);
  147. void AddPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw);
  148. void SubPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw);
  149. LONG LGetPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr);
  150. private:
  151. // Not Implemented to prevent compiler from auto-generating
  152. //
  153. INSTCNTR(const INSTCNTR& x);
  154. INSTCNTR& operator=(const INSTCNTR& x);
  155. };
  156. //#endif // INSTANCE_DATA_DEFINED
  157. //-----------------------------------------------------------------------------
  158. // GLOBCNTR inline functions
  159. //-----------------------------------------------------------------------------
  160. inline
  161. void
  162. GLOBCNTR::IncPerfCntr(GLOBAL_CNTR cntr)
  163. {
  164. if (m_fInit)
  165. InterlockedIncrement((LONG *)&m_rgdwPerfData[cntr]);
  166. }
  167. inline
  168. void
  169. GLOBCNTR::DecPerfCntr(GLOBAL_CNTR cntr)
  170. {
  171. if (m_fInit)
  172. InterlockedDecrement((LONG *)&m_rgdwPerfData[cntr]);
  173. }
  174. inline
  175. void
  176. GLOBCNTR::SetPerfCntr(GLOBAL_CNTR cntr, DWORD dw)
  177. {
  178. if (m_fInit)
  179. InterlockedExchange((LONG *)&m_rgdwPerfData[cntr], (LONG)dw);
  180. }
  181. inline
  182. void
  183. GLOBCNTR::AddPerfCntr(GLOBAL_CNTR cntr, DWORD dw)
  184. {
  185. if (m_fInit)
  186. InterlockedExchangeAdd((LONG *)&m_rgdwPerfData[cntr], (LONG)dw);
  187. }
  188. inline
  189. void
  190. GLOBCNTR::SubPerfCntr(GLOBAL_CNTR cntr, DWORD dw)
  191. {
  192. if (m_fInit)
  193. InterlockedExchangeAdd((LONG *)&m_rgdwPerfData[cntr], -((LONG)dw));
  194. }
  195. inline
  196. LONG
  197. GLOBCNTR::LGetPerfCntr(GLOBAL_CNTR cntr)
  198. {
  199. return m_fInit ? m_rgdwPerfData[cntr] : 0;
  200. }
  201. // #ifdef INSTANCE_DATA_DEFINED
  202. //-----------------------------------------------------------------------------
  203. // INSTCNTR inline functions
  204. //-----------------------------------------------------------------------------
  205. inline
  206. void
  207. INSTCNTR::IncPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr)
  208. {
  209. if (m_fInit)
  210. {
  211. if ((icid != INVALID_INST_ID) && m_rgInstRec[icid].fInUse)
  212. InterlockedIncrement((LONG *)&m_rgdwCntr[((icid * m_cCounters)+ cntr)]);
  213. }
  214. }
  215. inline
  216. void
  217. INSTCNTR::DecPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr)
  218. {
  219. if (m_fInit)
  220. {
  221. if ((icid != INVALID_INST_ID) && m_rgInstRec[icid].fInUse)
  222. InterlockedDecrement((LONG *)&m_rgdwCntr[((icid * m_cCounters) + cntr)]);
  223. }
  224. }
  225. inline
  226. void
  227. INSTCNTR::SetPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw)
  228. {
  229. if (m_fInit)
  230. {
  231. if ((icid != INVALID_INST_ID) && m_rgInstRec[icid].fInUse)
  232. InterlockedExchange((LONG *)&m_rgdwCntr[((icid * m_cCounters) + cntr)], (LONG)dw);
  233. }
  234. }
  235. inline
  236. void
  237. INSTCNTR::AddPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw)
  238. {
  239. if (m_fInit)
  240. {
  241. if ((icid != INVALID_INST_ID) && m_rgInstRec[icid].fInUse)
  242. InterlockedExchangeAdd((LONG *)&m_rgdwCntr[((icid * m_cCounters) + cntr)], (LONG)dw);
  243. }
  244. }
  245. inline
  246. void
  247. INSTCNTR::SubPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr, DWORD dw)
  248. {
  249. if (m_fInit)
  250. {
  251. if ((icid != INVALID_INST_ID) && m_rgInstRec[icid].fInUse)
  252. InterlockedExchangeAdd((LONG *)&m_rgdwCntr[((icid * m_cCounters) + cntr)], -((LONG)dw));
  253. }
  254. }
  255. inline
  256. LONG
  257. INSTCNTR::LGetPerfCntr(INSTCNTR_ID icid, INST_CNTR cntr)
  258. {
  259. return m_fInit ? m_rgdwCntr[((icid * m_cCounters) + cntr)] : 0;
  260. }
  261. // #endif // INSTANCE_DATA_DEFINED
  262. #endif //!defined(_PERFAPP_HPP_)