获取统计性能数据

在 WMI 中,可以基于派生自 Win32\_PerfFormattedData 的带格式性能类中的数据定义统计信息性能数据。 可用统计信息为平均值、最小值、最大值、范围和方差,如统计计数器类型中所定义。

以下列表包含特殊的统计计数器类型:

以下示例演示如何:

  • 创建一个 MOF 文件用于定义计算数据的类。
  • 编写一个脚本用于创建类的实例,并使用重新计算的统计值定期刷新实例中的数据。

MOF 文件

以下 MOF 代码示例创建一个名为 Win32_PerfFormattedData_AvailableMBytes 的新计算数据类。 此类包含原始类 Win32_PerfRawData_PerfOS_Memory 的 AvailableMBytes 属性中的数据。 Win32_PerfFormattedData_AvailableBytes 类定义属性 Average、Min、Max、Range 和 Variance。

MOF 文件使用带格式性能计数器类的属性限定符来定义属性数据源和计算公式。

  • Average 属性从 Win32_PerfRawData_PerfOS_Memory.AvailableMBytes 属性获取原始数据。
  • Average 属性的 Counter 限定符指定原始数据源。
  • CookingType 限定符指定用于计算数据的公式 COOKER_MIN
  • SampleWindow 限定符指定在执行计算之前要获取的样本数。
// Store the new Win32_PerfFormattedData_MemoryStatistics
//     class in the Root\Cimv2 namespace
#pragma autorecover
#pragma namespace("\\\\.\\Root\\CimV2")

qualifier vendor:ToInstance;
qualifier guid:ToInstance;
qualifier displayname:ToInstance;
qualifier perfindex:ToInstance;
qualifier helpindex:ToInstance;
qualifier perfdetail:ToInstance;
qualifier countertype:ToInstance;
qualifier perfdefault:ToInstance;
qualifier defaultscale:ToInstance;

qualifier dynamic:ToInstance;
qualifier hiperf:ToInstance;
qualifier AutoCook:ToInstance;
qualifier AutoCook_RawClass:ToInstance;
qualifier CookingType:ToInstance;
qualifier Counter:ToInstance;


// Define the Win32_PerFormattedData_MemoryStatistics
//     class, derived from Win32_PerfFormattedData
[
  dynamic,
  // Name of formatted data provider: "WMIPerfInst" for Vista 
  //   and later
  provider("HiPerfCooker_v1"), 
  // Text that will identify new counter in Perfmon
  displayname("My Calculated Counter"),                            
  // A high performance class     
  Hiperf,
  // Contains calculated data 
  Cooked, 
  // Value must be 1 
  AutoCook(1), 
  // Raw performance class to get data for calculations
  AutoCook_RawClass("Win32_PerfRawData_PerfOS_Memory"),
  // Value must be 1        
  AutoCook_RawDefault(1),
  // Name of raw class property to use for timestamp in formulas 
  PerfSysTimeStamp("Timestamp_PerfTime"), 
  // Name of raw class property to use for frequency in formulas
  PerfSysTimeFreq("Frequency_PerfTime"), 
  // Name of raw class property to use for timestamp in formulas
  Perf100NSTimeStamp("Timestamp_Sys100NS"),
  // Name of raw class property to use for frequency in formulas
  Perf100NSTimeFreq("Frequency_Sys100NS"),
  // Name of raw class property to use for timestamp in formulas
  PerfObjTimeStamp("Timestamp_Object"),
  // Name of raw class property to use for frequency in formulas 
  PerfObjTimeFreq("Frequency_Object"),
  // Only one instance of class allowed in namespace
  singleton                                                   
]

class Win32_PerfFormattedData_MemoryStatistics
          : Win32_PerfFormattedData
{

// Define the properties for the class. 
// All the properties perform different
//     statistical operations on the same
//     property, AvailableMBytes, in the raw class

// Define the Average property,
//     which uses the "COOKER_AVERAGE" counter type and 
//     gets its data from the AvailableMBytes 
//     property in the raw class

    [
     CookingType("COOKER_AVERAGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Average = 0;

// Define the Min property, which uses
//     the "COOKER_MIN" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_MIN"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Min = 0;

// Define the Max property, which uses
//     the "COOKER_MAX" counter type and 
//     gets its data from the
//     AvailableMBytes property in the raw class

    [
     CookingType("COOKER_MAX"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Max = 0;

// Define the Range property, which uses
//     the "COOKER_RANGE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_RANGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Range = 0;

// Define the Variance property, which uses
//     the "COOKER_VARIANCE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_VARIANCE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Variance = 0;
};

脚本

以下脚本代码示例使用之前创建的 MOF 获取有关可用内存(以 MB 为单位)的统计信息。 该脚本使用 SWbemRefresher 脚本对象创建一个刷新器,其中包含 MOF 文件创建的统计信息类的一个实例,即 Win32_PerfFormattedData_MemoryStatistics。 有关使用脚本的详细信息,请参阅在脚本中刷新 WMI 数据。 如果在 C++ 中操作,请参阅在 C++ 中访问性能数据

注意

通过调用 SWbemRefresher.Add 获取项后,必须调用 SWbemRefreshableItem.Object,否则脚本将失败。 必须在进入循环之前调用 SWbemRefresher.Refresh 以获取基线值,否则统计属性在第一次传递时为零 (0)。

 

' Connect to the Root\Cimv2 namespace
strComputer = "."
Set objService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

' Create a refresher
Set Refresher = CreateObject("WbemScripting.SWbemRefresher")
If Err <> 0 Then
WScript.Echo Err
WScript.Quit
End If

' Add the single instance of the statistics
'    class to the refresher
Set obMemoryStatistics = Refresher.Add(objService, _
    "Win32_PerfFormattedData_MemoryStatistics=@").Object

' Refresh once to obtain base values for cooking calculations
Refresher.Refresh

Const REFRESH_INTERVAL = 10

' Refresh every REFRESH_INTERVAL seconds 
For I=1 to 3
  WScript.Sleep REFRESH_INTERVAL * 1000
  Refresher.Refresh

  WScript.Echo "System memory statistics" _
      & "(Available Megabytes) " _
      & "for the past 100 seconds - pass " & i 
  WScript.Echo "Average = " _
     & obMemoryStatistics.Average & VBNewLine & "Max = " _
     & obMemoryStatistics.Max & VBNewLine & "Min = " _
     & obMemoryStatistics.Min & VBNewLine & "Range = " _ 
     & obMemoryStatistics.Range & VBNewLine & "Variance = " _
     & obMemoryStatistics.Variance 
Next

运行脚本

以下过程说明如何运行该示例。

运行示例脚本

  1. 将 MOF 代码和脚本复制到计算机上的文件中。
  2. 为 MOF 文件指定 .mof 扩展名,为脚本文件提供 .vbs 说明。
  3. 在命令行上切换到存储文件的目录,并针对 MOF 文件运行 Mofcomp。 例如,如果将文件命名为 CalculatedData.mof,则命令为 Mofcomp CalculatedData.mof
  4. 运行该脚本。

监视性能数据

访问 WMI 预安装的性能类

带格式性能计数器类的属性限定符

统计计数器类型