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

Calculating Cpu Percent Utilization (per process)

$
0
0
I'm trying to calculate cpu utilization for a list of processes returned via
a ProcessFinder, and I am hitting dead ends has anyone come across this yet,
or does anyone have any ideas?

Here is an example of what I have so far, (ala Java):
Sigar sigar = new Sigar();
ProcessFinder pf = new ProcessFinder(sigar);
long[] pids = pf.find("CredName.User.ew=root");
ProcTime ptime = new ProcTime();
long cpuTotal=0, cpuUser=0, cpuSys=0;

for(int i=0; i< pids.length(); i++){
ptime.gather(sigar, pids[i]);
cpuTotal += ptime.getTotal();
cpuUser += ptime.getUser();
cpuSys += ptime.getSys();
}
Cpu cpuTime = sigar.getCpu();

//note that this is just some inaccurate math as a place holder
double cpuTotalPercent = new Double( (((double) cpuTotal /
cpuTime.getTotal()) *100) );
double cpuUserPercent = new Double( (((double) cpuUser / cpuTime.getUser())
*100) );
double cpuSysPercent = new Double( (((double) cpuSys / cpuTime.getSys())
*100) );

In my research of this I decided to turn to some fun sections of the freebsd
source for ps, which has a friendly output from 'ps u' that does the same
thing per process on print (in C):
-------------------------->8----------------------
double getpcpu(k) KINFO *k; {
struct proc *p;
static int failure;

if (!nlistread)
failure = donlist();
if (failure)
return (0.0);

p = KI_PROC(k);
#define fxtofl(fixpt) ((double)(fixpt) / fscale)

/* XXX - I don't like this */
if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
return (0.0);
if (rawcpu)
return (10000.0 / sysconf(_SC_CLK_TCK) *
fxtofl(p->p_pctcpu));
return (10000.0 / sysconf(_SC_CLK_TCK) * fxtofl(p->p_pctcpu) /
(1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
}

void
pcpu(k, ve)
KINFO *k;
VARENT *ve;
{
VAR *v;

v = ve->var;
(void)printf("%*.1f", v->width, getpcpu(k));
}
-----------------------8<------------------------

So now that I have attacked everyone's senses with code, can anyone point me
to an algorithm to calculate this that does not require me to dig up the
number of ticks per second of the cpu, and can hopefully be encompassed
within the methods offered by SIGAR?
Thanks in advance for all your help, John

John Buren Southerland
Principal Consultant
Southerland Consulting
801.467.8090(office)
214.734.8099(cell)
john@southerland-consulting.com




Viewing all articles
Browse latest Browse all 52618

Trending Articles