取得統計效能資料

在 WMI 中,您可以根據衍生自 Win32_PerfFormattedData之格式化效能類別中的資料來定義統計效能資料。 可用的統計資料是統計 計數器類型中所定義的平均值、最小值、最大值、範圍和變異數。

下列清單包含特殊的統計計數器類型:

下列範例示範如何:

  • 建立 MOF 檔案,以定義匯出資料的類別。
  • 撰寫腳本以建立 類別的實例,並使用重新計算的統計值定期重新整理 實例中的資料。

MOF 檔案

下列 MOF 程式碼範例會建立名為 Win32_PerfFormattedData_AvailableMBytes的新匯出資料類別。 這個類別包含原始類別 之 AvailableMBytes 屬性的資料 Win32_PerfRawData_PerfOS_MemoryWin32_PerfFormattedData_AvailableBytes類別會定義AverageMinMaxRangeVariance屬性。

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,則命令為 MofcompCalculatedData.mof
  4. 執行指令碼。

監視效能資料

存取 WMI 預先安裝效能類別

格式化效能計數器類別的屬性限定詞

統計計數器類型