Browse DevX
Sign up for e-mail newsletters from DevX


Access Raw Data with Performance Counters in Visual C++ : Page 3

Though Windows allows you direct access to performance data, using the native C API is unwieldy. James Curran explains a few C++ techniques to help make it more manageable.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Shared_ptr is a simple "drop-in" class to handle reference counting. Just say "boost::shared_ptr<HCOUNTER>" and that's it. Well, that's almost it. Shared_ptr will destroy the object it's holding using delete, so you create it using new. But the standard new allocates memory in your local memory space, and you need it in shared global memory. Fortunately, C++ provides the tools for that as well, and you don't even have to leave the current Standard. Simply define class-specific new and delete functions.

struct GlobalCounter { void * operator new(size_t ) { return GlobalAlloc(GPTR,sizeof(HCOUNTER)); } void operator delete(void * ptr) { GlobalFree(ptr); } HCOUNTER* handle() { return (HCOUNTER*) this; } };

When new is called, it allocates enough space from global memory, and frees it on delete. I also added a member function called handle() to get a pointer to the raw memory itself. The version shown here is just for presentation; the version given in the complete source listing is templatized to allow reuse (see Listing 2).

Now that all the pieces are in place, you can finally create a wrapper for a HCOUNTER struct which will allow you to treat it just like any other object.

#include "boost/smart_ptr.hpp" struct Counter : public boost::shared_ptr< GlobalCounter > { Counter () : boost::shared_ptr< GlobalCounter > (new GlobalCounter ) {} operator HCOUNTER *() { return get()->handle(); } };

As with GlobalCounter, the full version of the inline code is in the complete source (see Listing 3).

Finally, you have to deal with the essence of performance counters. You have one or more Counter objects, which are created and managed by a Query object. So, request a new counter by adding it to the Query object, which provides it with a Counter object. To determine what values the counter totals, ask the Query object to Collect the data. The values will appear in the Counter objects.

Query hQuery; Counter counter = hQuery.AddCounter(_T("\\TCP\\Connections Established")); hQuery.Collect();

The string passed to AddCounter describes the value you want to monitor. They follow the basic format "\\Machine\PerfObject(ParentInstance/ObjectInstance#InstanceIndex)\Counter." Some of those pieces are optional. For example, in the above code, you have \TCP\Connections Established", which has just the PerfObject and Counter parts. Similarly, you could have \\Mach02\Process(Explorer)\% Processor Time to find the processor time used by Explorer.exe on a remote PC called "Mach02."

A full list of the monitored values isn't possible since the system is extensible to user-defined counters. However, you can use other functions in the PDH to get a partial list. These functions can be found by looking at the options in PerfMon.

Now that you have the value you want in the Counter, you need to get it out. This poses a new problem, as the format of the count varies depending on what's being counted. Sometimes it's a long and sometimes it a double, or LONGLONG, or a string. And to make things even more complicated, you have to ask the OS to convert the counter into any of those forms. I've added a few more member functions to Counter, to simplify this. Each comes in two variations: One to return the value as the native type (Counter::asDouble for example), and another to return the value converted to a std::string (Counter:: asLongString). If an error occurs, say, when you ask for the value in an incompatible type, the string functions return "(error)" while the native type functions throw an exception.

Despite 20 years experience, membership in the ISO C++ Standardization Committee, and a publication history spanning more than half his life, James M. Curran just spent 5 months on unemployment, before finally landing a position is a Senior Programmer/Analyst for Aurora Software, Inc in NYC (two blocks away from a large hole which had a lot to do with not having a job in the first place). He can be reached at JamesCurran@mvps.org.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date