public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Luka Napotnik <luka.napotnik@gmail.com>
To: Jan Engelhardt <jengelh@computergmbh.de>
Cc: "linux-os (Dick Johnson)" <linux-os@analogic.com>,
	linux-kernel@vger.kernel.org
Subject: Re: division and cpu usage
Date: Sun, 26 Aug 2007 01:25:55 +0200	[thread overview]
Message-ID: <46D0BA83.3040405@gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0708241442590.28540@fbirervta.pbzchgretzou.qr>

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

Jan Engelhardt pravi:
> On Aug 24 2007 07:34, linux-os (Dick Johnson) wrote:
>>> I'm new to kernel development and have some questions.
>>>
>>> 1. Why can't I divide with regular casting to double ((double)a /
>>> (double)b)? It gives me strange errors when compiling:
>>>
>>> WARNING: "__divdf3" [/root....] undefined!
>>> WARNING: "__addf3" [/root/...] undefined!
>>> WARNING: "__floatsidf" [/root/...] undefined!
>>>
>>> And if I compile with normal integers, I get zero as the result.
>>>
>>> 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?
> 
> * So use integer math: (task->utime + task->stime) * 100 / jiffies
>   and you get the 'common' percentage. In integer, that is.
> 
> * I am not sure about the use of jiffies when it comes to CONFIG_NO_HZ=y.
> 
>> Floating point operations are not allowed in the kernel. Often,
> 
> IIRC they are allowed since ... recently (2.6.16, .17? can't remember). When
> the kernel tries to execute an FP instruction (and traps as a result), more
> kernel code will enable that the FP stack gets properly switched when a process
> changes between userspace-kernelspace.
> 
>> You can use "long long" for high precision math if necessary.
> 
> That will give link failure for __udivdi3. Use do_div().
> 
> 
> 	Jan


  reply	other threads:[~2007-08-25 23:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-23 23:43 division and cpu usage Luka Napotnik
2007-08-24  1:04 ` David Schwartz
2007-08-24 11:34 ` linux-os (Dick Johnson)
2007-08-24 12:46   ` Jan Engelhardt
2007-08-25 23:25     ` Luka Napotnik [this message]
2007-08-27 22:07     ` Luka Napotnik
2007-08-28  6:45       ` Jan Engelhardt
2007-08-28 10:41         ` Luka Napotnik
2007-08-28 12:11           ` Jan Engelhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46D0BA83.3040405@gmail.com \
    --to=luka.napotnik@gmail.com \
    --cc=jengelh@computergmbh.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-os@analogic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox