統計パフォーマンス データの取得

WMI では、Win32\_PerfFormattedData から派生した書式設定されたパフォーマンス クラスのデータに基づいて統計パフォーマンス データを定義できます。 使用可能な統計情報は、統計カウンター型で定義されている平均、最小、最大、範囲、差異です。

次の一覧には、特殊な統計カウンター型が含まれています。

次の例で説明するのは、以下の方法についてです。

  • 計算データのクラスを定義する MOF ファイルを作成する。
  • クラスのインスタンスを作成するスクリプトを記述し、再計算された統計値を使用してインスタンス内のデータを定期的に更新する。

MOF ファイル

次の MOF コード例では、Win32_PerfFormattedData_AvailableMBytes という名前の新しい計算データ クラス を作成します。 このクラスには、生のクラス Win32_PerfRawData_PerfOS_MemoryAvailableMBytes プロパティのデータが含まれます。 Win32_PerfFormattedData_AvailableBytes クラスでは、AverageMinMaxRangeVariance の各プロパティが定義されます。

MOF ファイルでは、書式設定されたパフォーマンス カウンター クラスのプロパティ修飾子を使用して、プロパティ データ ソースと計算式を定義します。

  • Average プロパティは、Win32_PerfRawData_PerfOS_MemoryAvailableMBytes プロパティから生データを取得します。
  • 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 ファイルで作成される統計クラスの 1 つのインスタンス (Win32_PerfFormattedData_MemoryStatistics) を含むリフレッシャーを作成します。 スクリプトの使用について詳しくは、「スクリプトでの WMI データのリフレッシュ」を参照してください。 C++ で作業している場合は、「C++ でのパフォーマンス データへのアクセス」を参照してください。

注意

SWbemRefreshableItem.Object は、SWbemRefresher.Add の呼び出しから項目を取得した後に呼び出さなければスクリプトは失敗します。 ベースライン値を取得するには、ループに入る前に 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 プレインストールされたパフォーマンス クラスへのアクセス

書式設定されたパフォーマンス カウンター クラスのプロパティ修飾子

統計カウンター型