libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NTPerformanceObjects.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #pragma once
20 
21 #include"../../RegistryKey.hpp"
22 #include"../SharedMemory.hpp"
23 #include"../../EventLog.hpp"
24 
25 #include<winperf.h>
26 
27 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace NTUtils {
28 
29  const unsigned long initial_shared_memory_size=16384;
30  const char string_shared_memory_name[]="{CA5A83A1-9925-11d4-B182-0001029FBE99}";
31  const char string_shared_mutex_name[]="{0883F431-99FC-11d4-B184-0001029FBE99}";
32  const char ptr_shared_memory_name[]="{0883F432-99FC-11d4-B184-0001029FBE99}";
33  const char ptr_shared_mutex_name[]="{0883F433-99FC-11d4-B184-0001029FBE99}";
34  const char list_shared_memory_name[]="{5538F380-A114-11d4-B188-0001029FBE99}";
35  const char list_shared_mutex_name[]="{5538F381-A114-11d4-B188-0001029FBE99}";
36  const char map_shared_memory_name[]="{88668F70-CF94-11d4-B19D-0001029FBE99}";
37  const char map_shared_mutex_name[]="{88668F71-CF94-11d4-B19D-0001029FBE99}";
38 
39  const std::pair< std::string, std::string > string_shared_memory_names(
42  );
43  const std::pair< std::string, std::string > ptr_shared_memory_names(
46  );
47  const std::pair< std::string, std::string > list_shared_memory_names(
50  );
51  const std::pair< std::string, std::string > map_shared_memory_names(
54  );
55 
57  public:
58  // All these classes are used to name the performance object, list the counters
59  // it contains and the counters' features.
61  public:
63  public:
64  std::string name;
65  std::string help;
66  inline PerfObjectStrings(void) : name( "" ), help( "" ) {}
67  inline PerfObjectStrings(const std::string &n, const std::string &h) : name( n ), help( h ) {}
68  inline PerfObjectStrings(const PerfObjectStrings &p) : name( p.name ), help( p.help ) {}
69  inline ~PerfObjectStrings(void) {}
70  inline PerfObjectStrings &operator=(const PerfObjectStrings &p) {name=p.name;help=p.help;return *this;}
71  inline bool operator<(const PerfObjectStrings &p) const {return name<p.name || (name==p.name && help<p.help);}
72  inline operator std::string (void) const {return "Name: '" + name + "', help: '" + help + "'";};
73  };
75  public:
77  unsigned long flags;
79  inline PerfCtrDetails(void) {}
80  inline PerfCtrDetails(const PerfObjectStrings &pos, const unsigned long f, const size_t s) : details( pos ), flags( f ), size( s ) {}
81  inline PerfCtrDetails(const PerfCtrDetails &pcd) : details( pcd.details ), flags( pcd.flags ), size( pcd.size ) {}
82  inline ~PerfCtrDetails(void) {}
83  inline PerfCtrDetails &operator=(const PerfCtrDetails &pcd) {details=pcd.details;flags=pcd.flags;size=pcd.size;return *this;}
84  inline bool operator<(const PerfCtrDetails &pcd) const {return details<pcd.details || (details==pcd.details && (flags<pcd.flags || (flags==pcd.flags && size<pcd.size)));}
85  inline operator std::string (void) const {std::stringstream ss;ss << (std::string)details << ", flags:" << flags << ", size:"<< size << " bytes"; return ss.str();};
86  };
87 
90  inline PerfObjectDetails(const PerfObjectStrings &pos, const std::set< PerfCtrDetails > &c) : object( pos ) {for (std::set< PerfCtrDetails >::const_iterator i(c.begin());i!=c.end();++i) {counters.insert( *i );}}
91  inline PerfObjectDetails(const PerfObjectDetails &pcd) : object( pcd.object ) {for (std::set< PerfCtrDetails >::const_iterator i(pcd.counters.begin());i!=pcd.counters.end();++i) {counters.insert( *i );}}
92  inline ~PerfObjectDetails(void) {}
93 
94  inline operator std::string (void) const {std::stringstream ss;ss << "Performance Object:" << std::endl << (std::string)object << std::endl;for (std::set< PerfCtrDetails >::const_iterator i(counters.begin());i!=counters.end();++i) {ss << "Counter details: " << (std::string)*i << std::endl;};return ss.str();};
95 
96  private:
97  inline PerfObjectDetails &operator=(const PerfObjectDetails &) noexcept(true);
98  };
99 
100  inline NTPerformanceObjects(const std::string &obj_prefix, const std::pair< std::string, std::string > &lang_details, const PerfObjectDetails &perf_obj_details, const bool q = true);
101  virtual inline ~NTPerformanceObjects(void);
102 
103 // typedef ManagedSharedMemorySTLIntf< CrapManagedSharedMemory< char >, 0 > sm_char_heap_type;
105  typedef std::basic_string
106  <
107  char,
108  std::char_traits< char >,
110  <
112  char
113  >
115 // typedef ManagedSharedMemorySTLIntf< CrapManagedSharedMemory< sm_string_type >, 2 > sm_string_heap_type;
117 // typedef ManagedSharedMemorySTLIntf< CrapManagedSharedMemory< NTPerformanceObjects * >, 1 > nt_po_heap_type;
119  typedef std::map
120  <
123  std::less< sm_string_type >,
125  <
128  >
130 // typedef ManagedSharedMemorySTLIntf< CrapManagedSharedMemory< cpp_performance_objects_type >, 3 > cpp_po_heap_type;
132  // Give everyone full control access to the shared memory heaps.
134  // Event logging stuff....
136  // Used to ensure serialised access to the map of performance counters.
138 
139  // This suite of three functions have to be static because the NT performance counter
140  // interface is "C"-style, so uses file-level (hence static) call-back functions.
141  // Used by processes to connect to this performance object. Increments the reference count.
142  // The addresses of these funtions are taken later, so they are certainly not "inline"!
143  static DWORD Open(NTPerformanceObjects * const pthis) {return pthis->Open();}
144  // Used by processes to get the current counter values.
145  static DWORD Collect(NTPerformanceObjects * const pthis, LPWSTR lpwszValue,LPVOID *lppData,LPDWORD lpcbBytes,LPDWORD lpcObjectTypes) {return pthis->Collect(lpwszValue, lppData, lpcbBytes, lpcObjectTypes);}
146  // Used by processes to notify disconnection from the performance objects.
147  static DWORD Close(void);
148 
149  protected:
150  virtual inline DWORD Open(void);
152 
153  private:
154  class TempPerfINIFile {
155  public:
156  inline TempPerfINIFile(const std::string &obj_prefix, const std::pair< std::string, std::string > &lang_details, const PerfObjectDetails &perf_obj_details);
157  inline ~TempPerfINIFile(void);
158 
159  inline const std::string &name(void) const noexcept(true);
160  static inline std::string make_driver_name(const std::string &);
161 
162  private:
163  class TempSymbolFile {
164  public:
165  inline TempSymbolFile(const std::string &obj_prefix, const std::vector< std::string >::size_type size);
166  inline ~TempSymbolFile(void);
167 
168  inline const std::string &name(void) const noexcept(true);
169  inline const std::vector< std::string > &names(void) const noexcept(true);
170 
171  private:
172  temp_file<std::fstream<char> > symbol;
173  std::vector< std::string > object_names;
174 
175  // I don't allow copying or assignment.
176  inline TempSymbolFile(const TempSymbolFile &) noexcept(true);
177  inline TempSymbolFile &operator=(const TempSymbolFile &) noexcept(true);
178  };
179 
180  temp_file<std::fstream<char> > ini;
181  TempSymbolFile symbol;
182 
183  // I don't allow assignment.
184  inline TempPerfINIFile &operator=(const TempPerfINIFile &) noexcept(true);
185  };
186 
187  const bool quiet;
188  const std::string driver_name;
189  const std::set< PerfObjectDetails::PerfCtrDetails >::size_type num_ctrs;
190  RegistryKey perf_key, perf_perf_key, perf_link_key;
191  // Shared memory heaps for sharing with the NT "Performance Monitor" process.
192  sm_char_heap_type sm_char_heap;
193  nt_po_heap_type ptr_heap;
194  sm_string_heap_type sm_string_heap;
195  cpp_po_heap_type cpp_po_heap;
196  cpp_performance_objects_type *cpp_performance_objects;
197 
198  typedef struct {
199  PERF_OBJECT_TYPE object_type;
200  PERF_COUNTER_DEFINITION *ctr_defs;
201  } perf_objects_details_type;
202  perf_objects_details_type *perf_objects_details;
203 
204  typedef struct {
205  PERF_COUNTER_BLOCK ctr_block;
206  DWORD *values;
207  } perf_ctrs_details_type;
208  perf_ctrs_details_type *perf_ctrs_details;
209 
210  // I don't allow assignment.
211  inline NTPerformanceObjects &operator=(const NTPerformanceObjects &) noexcept(true);
212  };
213 
214 } } }