Re: division and cpu usage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello again.

I have the following code:
========================================
clock_t c_sum, j, p;
cputime_t j_tmp;
...
c_sum = cputime64_to_clock_t(task->utime) +
cputime64_to_clock_t(task->stime);
cur_j = jiffies;
j_tmp =  jiffies64_to_cputime64(cur_j);
j = cputime64_to_clock_t(j_tmp);
p = (c_sum * 100) / j;
========================================

And if I check the p value of a certain process it gives wrong results.
For example for a process using 99% of the CPU it shows 20. What am I
doing wrong?

Greets,
Luka

Erik Mouw pravi:
> On Fri, Aug 24, 2007 at 03:18:32AM +0200, Luka Napotnik wrote:
>> I'm new to kernel development and have some questions.
> 
>> 1. Apperently kernel doesn't support operations with floating-point
>> types. How can I then divide a number 265 with the number 7 if I can
>> divide only by 2 with right-shifting?
> 
> Correct, floating point arithmetic is not allowed in the kernel. Use
> fixed point math if you really need more precision. Or export the raw
> values to userland and do the floating point math over there.
> 
>> 2. I'm trying to get the percentage of CPU used for a certain
>> task_struct and figured the following formula:
> 
>> (task->utime + task->stime) / jiffies
> 
>> Before calculating I convert all the variables to jiffies. Is this correct?
> 
> That should be ok. Couple of things:
> 
> - jiffies is continuously updated, so your percentage formula doesn't
>   work. you need to use a jiffies difference
> - jiffies can and will wrap around
> 
> But why would you calculate this in kernel? All the information is
> already exported to userland so do the calculations over there. Get the
> source for "top" and see how it is done.
> 
> 
> Erik
> 

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux