This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Get the percentage of CPU using of a process

Tags: None
(comma "," separated)
User avatar
JanGerrit
Moderator
Posts
647
Karma
3
OS
I use some KSysguard classes to get a list of processes and get some information about them. But now I want to get the percentage of their CPU using (e.g. the processes "dolphin" consumes 10% of the CPU load). I didn't find something which helps me. Only two things:
Code: Select all
process->sysTime;
process->sysUsage;

Could this be the thing I search for? Or do you have any other clues for me?
If someone wants to have a look at the code where this should get in, he/she can find it in playground: playground/base/plasma/runners/kill

Thanks in advance.


Image
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
From the Model KSysGuard uses:

Code: Select all
{
    double total;
    if(d->mShowChildTotals && !d->mSimple) total = process->totalUserUsage + process->totalSysUsage;
    else total = process->userUsage + process->sysUsage;
    if(d->mNormalizeCPUUsage)
         total = total / d->mNumProcessorCores;

    if(total < 1 && process->status != KSysGuard::Process::Sleeping && process->status != KSysGuard::Process::Running)
        return process->translatedStatus();  //tell the user when the process is a zombie or stopped
    if(total < 0.5)
        return "";

    return QString::number((int)(total+0.5)) + '%';
}


Therefore the answer is process->userUsage + process->sysUsage = Total CPU Usage. If you wish to include their children in their usage as well, you would use process->totalUserUsage + process->totalSysUsage instead. You may wish to "normalize" it also, by dividing usage by the number of cores.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
JanGerrit
Moderator
Posts
647
Karma
3
OS
Thanks for the help, bcooksley. That's what I wanted.


Image


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]