c# / perfmon中的自定义性能计数器

本文关键字:自定义 性能计数器 perfmon | 更新日期: 2023-09-27 18:10:28

你好,我试图创建一个自定义性能计数器用于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);
        }
    }
}

}

c# / perfmon中的自定义性能计数器

在Win32中,性能计数器通过让PerfMon加载提供计数器值的DLL来工作。

在。net中,这个DLL是一个存根,它使用共享内存与正在运行的。net进程通信。进程定期将新值推送到共享内存块,DLL将它们作为性能计数器使用。

基本上,你可能需要用本机代码实现你的性能计数器DLL,因为。net性能计数器假设有一个进程在运行