单个程序的监视CPU和磁盘利用率 [英] Monitoring CPU and disk utilization of a single program

查看:275
本文介绍了单个程序的监视CPU和磁盘利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能计算另一个并发程序的CPU和磁盘利用率?即一个程序正在运行,并且其他计算第一的资源利用。

How can I calculate CPU and disk utilization of another concurrent program? I.e. one program is running and other calculates the first's resource use.

我使用C和C ++和Windows XP下运行。

I'm using C and C++ and running under Windows XP.

推荐答案

至于CPU利用率也不难在这个环节的 Windows的C ++获取CPU和内存Utilisation随着性能计数器的。据我理解(但还没有测试),也可以找出磁盘利用率。

As for CPU utilization it is not difficult to do after taking a look at this link Windows C++ Get CPU and Memory Utilisation With Performance Counters. As far as I understand (but have not tested) it is also possible to find out disk utilization.

我们的想法是使用性能计数器的。在您的情况,您需要使用性能计数器 L\\\\流程(program_you_are_interested_in_name)\\\\%处理器时间 CPU利用率,并可能 L \\\\流程(program_you_are_interested_in_name)\\\\数据字节数/秒磁盘操作。因为我不知道你所需要的参数来了解磁盘操作正是你可以采取所有可用的参数列表来看一下吧:的过程对象

The idea is to use Performance Counters. In your situation you need to use the performance counter L"\\Process(program_you_are_interested_in_name)\\% Processor Time" for CPU Utilization and possibly L"\\Process(program_you_are_interested_in_name)\\Data Bytes/sec" for disk operations. Since I am not sure what parameters you need to know about disk operations exactly you can take a look yourself at the list of all available parameters: Process Object

例如,如果你有一个并发程序名为 a_program_name.exe 你可以找到它的CPU利用率测量至少两倍的性能计数器→ \\\\流程(a_program_name)\\\\%处理器时间。在这个例子中它是在一个循环中完成。顺便与该测试测量在多核处理器上运行的多线程应用程序可能给CPU使用率是100%以上。

If for example you have a concurrent program with the name a_program_name.exe you can find its CPU utilization measuring at least twice the performance counter L"\\Process(a_program_name)\\% Processor Time". In the example it is done in a loop. By the way measuring with this test a multithreaded application running on a multicore processor might give CPU utilization which is more than 100%.

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <string.h>
#include <string>
#include <iostream>

// Put name of your process here!!!!
CONST PWSTR COUNTER_PATH    = L"\\Process(a_program_name)\\% Processor Time";

void main(int argc, char *argv[]){

    PDH_HQUERY hquery;
    PDH_HCOUNTER hcountercpu;
    PDH_STATUS status;
    LPSTR pMessage;
    PDH_FMT_COUNTERVALUE countervalcpu;

    if((status=PdhOpenQuery(NULL, 0, &hquery))!=ERROR_SUCCESS){
        printf("PdhOpenQuery %lx\n", status);    
        goto END;
    }

    if((status=PdhAddCounter(hquery,COUNTER_PATH,0, &hcountercpu))!=ERROR_SUCCESS){
            printf("PdhAddCounter (cpu) %lx\n", status);    
            goto END;
    }

    /*Start outside the loop as CPU requires difference 
    between two PdhCollectQueryData s*/
    if((status=PdhCollectQueryData(hquery))!=ERROR_SUCCESS){
        printf("PdhCollectQueryData %lx\n", status);    
        goto END;
    }

    while(true){
        if((status=PdhCollectQueryData(hquery))!=ERROR_SUCCESS){
            printf("PdhCollectQueryData %lx\n", status);    
            goto END;
        }

        if((status=PdhGetFormattedCounterValue(hcountercpu, PDH_FMT_LONG | PDH_FMT_NOCAP100, 0, &countervalcpu))!=ERROR_SUCCESS){
                printf("PdhGetFormattedCounterValue(cpu) %lx\n", status);    
                goto END;
        }

        printf("cpu %3d%%\n", countervalcpu.longValue);

        Sleep(1000);

    }
END:
    ;
}

有一件事提。 PdhExpandWildCardPath 让你展开一个这样的升的字符串\\\\流程(*)\\\\%处理器时间在计算机上运行的进程列表中。然后你就可以查询性能计数器为每个进程。

There is one more thing to mention. PdhExpandWildCardPath lets you expand a string like this L"\\Process(*)\\% Processor Time" in a list of processes running on a computer. And then you can query performance counters for each process.

这篇关于单个程序的监视CPU和磁盘利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆