Quantcast
Channel: VMware Communities : Popular Discussions - Hyperic User Forums
Viewing all articles
Browse latest Browse all 52618

How to get correctly the percent of used CPU per process

$
0
0

I'm trying to get the percent of used CPU per process on windows with Qt/C++. Firstly i get a list of running processes and after, for each process i try to get the used CPU, for most process the result looks valid (they match with task manager in windows), but with the AIDA64 process (that is running a CPU stress test in background), i got strange values like 312% what is wrong with my c++ code?

 

    sigar_t *sigarproclist;
    sigar_proc_list_t proclist;
    sigar_open(&sigarproclist);
    sigar_proc_list_get(sigarproclist, &proclist);
    for (size_t i = 0; i < proclist.number; i++)
    {
        sigar_proc_cpu_t cpu;
        int status1 = sigar_proc_cpu_get(sigarproclist, proclist.data[i], &cpu);
        if (status1 == SIGAR_OK)
        {
            Sleep(50);
            int status2 = sigar_proc_cpu_get(sigarproclist, proclist.data[i], &cpu);
            if (status2 == SIGAR_OK)
            {
                sigar_proc_state_t procstate;
                sigar_proc_state_get(sigarproclist, proclist.data[i], &procstate);
                qDebug() << procstate.name << cpu.percent * 100 << "%";
            }
        }
    }
    sigar_close(sigarproclist);

Viewing all articles
Browse latest Browse all 52618

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>