共用方式為


取得統計效能數據

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

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

下列範例示範如何:

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

MOF 檔案

下列MOF程式代碼範例會建立名為 Win32_PerfFormattedData_AvailableMBytes的新匯出數據類別。 這個類別包含來自原始類別之 AvailableMBytes 屬性的數據Win32_PerfRawData_PerfOS_MemoryWin32_PerfFormattedData_AvailableBytes類別會定義 AverageMin、MaxRangeVariance 屬性

MOF 檔案會使用 格式化性能計數器類別 的屬性限定符來定義屬性數據源和計算公式。

  • Average 屬性會從Win32_PerfRawData_PerfOS_Memory取得原始數據。AvailableMBytes 屬性。
  • Average 屬性的計數器限定符會指定原始數據源。
  • 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 預安裝效能類別

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

統計計數器類型