繁体   English   中英

如何阅读windows perfmon counter?

[英]how to read windows perfmon counter?

我可以获得一个C ++代码来读取windows perfmon计数器(类别,计数器名称和实例名称)吗?

这在c#中很容易,但我需要c ++代码。

谢谢

正如Doug T.先前指出的那样,我前一段时间发布了一个辅助类来查询性能计数器值。 该类的用法非常简单,您所要做的就是为性能计数器提供字符串。 http://askldjd.wordpress.com/2011/01/05/a-pdh-helper-class-cpdhquery/

但是,我在博客上发布的代码在实践中已被修改。 从您的评论中,您似乎对查询单个字段感兴趣。

在这种情况下,请尝试将以下函数添加到我的CPdhQuery类中。

double CPdhQuery::CollectSingleData()
{
    double data = 0;
    while(true)
    {
        status = PdhCollectQueryData(hQuery);

        if (ERROR_SUCCESS != status)
        {
            throw CException(GetErrorString(status));
        }

        PDH_FMT_COUNTERVALUE cv;
        // Format the performance data record.
        status = PdhGetFormattedCounterValue(hCounter,
            PDH_FMT_DOUBLE,
            (LPDWORD)NULL,
            &cv);

        if (ERROR_SUCCESS != status)
        {
            continue;
        }

        data = cv.doubleValue;

        break;

    }

    return data;
}

例如,获得处理器时间

counter = boost::make_shared<CPdhQuery>(std::tstring(_T("\\Processor Information(_Total)\% Processor Time")));

要获取文件读取字节数/秒:

counter = boost::make_shared<CPdhQuery>(std::tstring(_T("\\System\\File Read Bytes/sec")));

获取%Committed Bytes:

counter = boost::make_shared<CPdhQuery>(std::tstring(_T("\\Memory\\% Committed Bytes In Use")));

要获取数据,请执行此操作。

double data = counter->CollectSingleData();

我希望这有帮助。

艾伦

一些常用的性能值具有API调用以直接获取它们。 例如,总处理器时间可以从GetSystemTimes获得,您可以自己计算百分比。

如果这不是一个选项,那么Performance Data Helper库为性能数据提供了一个中等简单的界面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM