Commands related to retrieving performance counters
This package provides procedures related to retrieving Windows performance counters.
To get a list of performance objects on a system, call pdh_enumerate_objects. Commands pdh_enumerate_object_counters, pdh_enumerate_object_instances and pdh_enumerate_object_items return the counters and instances for a particular performance object.
Similar to files in a file system, performance counters are identified by a counter path. A counter path is of the form
\\Computer\PerfObject(ParentInstance/ObjectInstance#InstanceIndex)\Counter
Not all components in a counter path need to be specified. See Windows SDK documentation for a detailed description and examples. Note that \ and / are not interchangeable in a counter path
To construct a counter path, call pdh_counter_path. The break down a counter path into its components, call pdh_parse_counter_path.
To retrieve values for a counter, use the following sequence of commands
pdh_query_open | Opens a query object to which counters can be added. |
pdh_counter_path | Constructs a counter path for the performance object and instance of interest. Call for each counter of interest. |
pdh_add_counter | Adds a counter (specified by its counter path as returned in the previous step) to a query and returns a handle to it. Call for each counter of interest. |
pdh_query_refresh | Updates all counters in the query to their current values. For rate based counters, this must be called at least twice before retrieving the value of a counter. |
pdh_get_scalar | Returns the scalar value of a counter as updated in the last call to pdh_query_refresh. |
pdh_get_array | Returns a counter that is an array of values (for example, one for each process) as updated in the last call call to pdh_query_refresh. |
pdh_query_get | Returns values for multiple counters from a query. Internally does the equivalent of pdh_query_refresh. |
pdh_query_close | Closes a query. This should be called to release resources associated with a query once the contained counters are no longer required. |
The pdh_system_performance_query is a wrapper around pdh_query_open that includes some commonly used system counters.
set queryh [pdh_system_performance_query processor_utilization memory_free_kb] proc print_counters {queryh} { pdh_query_refresh $queryh puts [pdh_query_get $queryh] after 1000 [info level 0] } print_counters $queryh vwait forever
-array BOOLEAN | If true ,the counter is treated as an array of values. If false (default), it will be returned as a scalar. |
-format FORMAT | Specifies how the counter value should be formatted. FORMAT may be on of long, large or double for a 32-bit integer, a 64-bit integer or a floating point double. |
-name NAME | Specifies the mnemonic name by which the counter will be identified when multiple counter values are retrieved with pdh_query_get. If unspecified, defaults to the full counter path CTRPATH. |
-scale SCALE | By default, each counter has a default scaling factor. If SCALE is none, the counter value is returned without the default scaling. If SCALE is x1000, the returned value is scaled up by a factor of 1000. If SCALE is nocap100, the counter value is not capped at 100 as is the scale for some counters like processor utilization. |
-instance INSTANCE | When there are many instances of a performance object, as is true for the Process object for example, this option allows specification of a particular instance by specifying its name as INSTANCE. Many performance objects allow INSTANCE to be specified as * to indicate counters for all instances are to be returned. |
-instanceindex INSTANCEINDEX | When there is more than one instance of a performance object with the same name, this option allows selection of a particular one by specifying its numeric instance index. |
-localized BOOLEAN | Performance object and counter names are localized based on the system languages. Using Process as the object name on a French localized system will result in a failure. By default the command assumes the passed names are in English and localizes them for the local system. If -localized is passed as true, the command assumes that the names being passed are already appropriately localized. |
-parent PARENTINSTANCE | Some performance objects, like Thread, are contained within another parent object, Process in the case of Thread. In such cases PARENTINSTANCE specifies the context for the target object. |
-detail DETAILLEVEL | Controls which detail levels are to be returned. DETAILLEVEL may be one of novice, advanced, expert and wizard (default) which return performance objects at an increasing level of detail. |
-refresh | The list of performance objects is returned from a cache. Performance objects may be added by any application at any time. Specifying -refresh causes the cache to be refreshed before being returned. |
-detail DETAILLEVEL | Controls which detail levels are to be returned. DETAILLEVEL may be one of novice, advanced, expert and wizard (default) which return performance objects at an increasing level of detail. |
-refresh | The list of performance objects is returned from a cache. Performance objects may be added by any application at any time. The command will fail if PERFOBJ is not in the cache. Specifying -refresh causes the cache to be refreshed before attempting to access the information for PERFOBJ. |
-format FORMAT | Specifies how the counter value should be formatted. FORMAT may be on of long, large or double for a 32-bit integer, a 64-bit integer or a floating point double. |
-scale SCALE | By default, each counter has a default scaling factor. If SCALE is none, the counter value is returned without the default scaling. If SCALE is x1000, the returned value is scaled up by a factor of 1000. If SCALE is nocap100, the counter value is not capped at 100 as is the scale for some counters like processor utilization. |
-var VARNAME | Specifies VARNAME as the name of a variable in the caller's context where the counter value is stored instead of being returned as the result of the command. If specified, the return value of the command is 1 on success and 0 if the counter does not exist. Without this option, the command will raise an error if the counter does not exist. |
Copyright © 2014 Ashok P. Nadkarni