7

こんにちは、perfmon で使用するカスタム パフォーマンス カウンターを作成しようとしています。次のコードはかなりうまく機能しますが、1 つ問題があります。

このソリューションでは、パフォーマンス カウンターの値を更新するタイマーがありますが、必要なデータを取得するためにこの実行可能ファイルを実行する必要はありません。つまり、カウンターを一度だけインストールしてから、データに対して perfmon クエリを実行できるようにしたいと考えています (事前にインストールされているすべてのカウンターと同様)。

どうすればこれを達成できますか?

using System;
using System.Diagnostics;
using System.Net.NetworkInformation;

namespace PerfCounter
{
class PerfCounter
{
    private const String categoryName = "Custom category";
    private const String counterName = "Total bytes received";
    private const String categoryHelp = "A category for custom performance counters";
    private const String counterHelp = "Total bytes received on network interface";
    private const String lanName = "Local Area Connection"; // change this to match your network connection
    private const int sampleRateInMillis = 1000;
    private const int numberofSamples = 100;

    private static NetworkInterface lan = null;
    private static PerformanceCounter perfCounter;

    static void Main(string[] args)
    {
        setupLAN();
        setupCategory();
        createCounters();
        updatePerfCounters();
    }

    private static void setupCategory()
    {
        if (!PerformanceCounterCategory.Exists(categoryName))
        {
            CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
            CounterCreationData totalBytesReceived = new CounterCreationData();
            totalBytesReceived.CounterType = PerformanceCounterType.NumberOfItems64;
            totalBytesReceived.CounterName = counterName;
            counterCreationDataCollection.Add(totalBytesReceived);
            PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection);
        }
        else
            Console.WriteLine("Category {0} exists", categoryName);
    }

    private static void createCounters() {
        perfCounter = new PerformanceCounter(categoryName, counterName, false);
        perfCounter.RawValue = getTotalBytesReceived();
    }

    private static long getTotalBytesReceived()
    {
        return lan.GetIPv4Statistics().BytesReceived;
    }

    private static void setupLAN()
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface networkInterface in interfaces)
        {
            if (networkInterface.Name.Equals(lanName))
                lan = networkInterface;
        }
    }
    private static void updatePerfCounters()
    {
        for (int i = 0; i < numberofSamples; i++)
        {
            perfCounter.RawValue = getTotalBytesReceived();
            Console.WriteLine("perfCounter.RawValue = {0}", perfCounter.RawValue);
            System.Threading.Thread.Sleep(sampleRateInMillis);
        }
    }
}

}

4

1 に答える 1

6

Win32 では、パフォーマンス カウンターは、カウンター値を提供する DLL を PerfMon に読み込ませることで機能します。

.NET では、この DLL は共有メモリを使用して実行中の .NET プロセスと通信するスタブです。プロセスは定期的に新しい値を共有メモリ ブロックにプッシュし、DLL はそれらをパフォーマンス カウンターとして使用できるようにします。

したがって、.NET パフォーマンス カウンターはプロセスが実行されていることを前提としているため、基本的にはパフォーマンス カウンター DLL をネイティブ コードで実装する必要があります。

于 2011-10-03T10:52:18.767 に答える