Using kstat to calculate each cpu load



Hi, I'm trying to write a module to calculate the load of every single
cpu on the system, the calculation is done by using the kstat array
(as show_stat() use it):


void foo(void)
{
cputime64_t user, nice, system, idle, iowait, irq, softirq, steal,
all, old_all;
unsigned long long old_jif[NR_CPUS + 1];
unsigned long long old_load[NR_CPUS + 1];
unsigned long dif;
int j = 0;
int i = get_cpu();

for (j = 0; j < 10 ; ++j )
{
for_each_online_cpu(i)
{
printk( KERN_ALERT "j :%d, i : %d\n", j, i);
all = user = nice = system = idle = iowait =
irq = softirq = steal = cputime64_zero;

user = cputime64_add(user, kstat_cpu(i).cpustat.user);
nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
system = cputime64_add(system, kstat_cpu(i).cpustat.system);
idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);

all = user + nice + system + idle + iowait + irq + softirq + steal;
if (j != 0)
{
dif = (unsigned long)((all - idle - old_load[i]) * 100) / (unsigned
long)(all - old_jif[i]);
printk( KERN_ALERT "cpu %d, load : %lu", i, dif);
}

old_jif[i] = all;
old_load[i] = all - idle;
}
{
DECLARE_WAIT_QUEUE_HEAD(wq);
wait_event_timeout(wq, 0, 20);
}
//set_current_state(TASK_INTERRUPTIBLE);
//schedule_timeout( 20 );
}
}

but the results I'm getting are quite different from what top shows me
- How can I solve it?

Thanks in advance,
Guy.
.



Relevant Pages