How to get cpu utilization through code with least overhead?
Hi, I need to write code to get and record the cpu utilization in certain interval with the least overhead. Could anybody please advise me on how to proceed ? I tried looking at the code for "top" command to see how it works but couldn't figure out much. I don't want to write script that uses commands and pulls out the results as I want the overhead to be as small as possible. Thanks Vaibhav Jain
On Fri, Jul 29, 2011 at 00:38, Vaibhav Jain <vjoss197@gmail.com> wrote:
Hi,
I need to write code to get and record the cpu utilization in certain interval with the least overhead.
try to derive them from /proc/loadavg or /proc/stat... -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Thanks! Just curious to know if this is how "top" does it. Thanks Vaibhav Jain On Thu, Jul 28, 2011 at 10:45 AM, Mulyadi Santosa <mulyadi.santosa@gmail.com
wrote:
On Fri, Jul 29, 2011 at 00:38, Vaibhav Jain <vjoss197@gmail.com> wrote:
Hi,
I need to write code to get and record the cpu utilization in certain interval with the least overhead.
try to derive them from /proc/loadavg or /proc/stat...
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Hi, Is there any other way to do this? If I need to write a kernel module that needs to get cpu utilization should I still use proc/stat ? Is there a kernel function that can be used in a module ? Thanks Vaibhav Jain On Thu, Jul 28, 2011 at 10:51 AM, Vaibhav Jain <vjoss197@gmail.com> wrote:
Thanks! Just curious to know if this is how "top" does it.
Thanks Vaibhav Jain
On Thu, Jul 28, 2011 at 10:45 AM, Mulyadi Santosa < mulyadi.santosa@gmail.com> wrote:
On Fri, Jul 29, 2011 at 00:38, Vaibhav Jain <vjoss197@gmail.com> wrote:
Hi,
I need to write code to get and record the cpu utilization in certain interval with the least overhead.
try to derive them from /proc/loadavg or /proc/stat...
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
On Fri, Jul 29, 2011 at 5:26 AM, Vaibhav Jain <vjoss197@gmail.com> wrote:
Hi,
Is there any other way to do this? If I need to write a kernel module that needs to get cpu utilization should I still use proc/stat ? Is there a kernel function that can be used in a module ?
Thanks Vaibhav Jain
The kernel writes all cpu metrics and more to /proc/stat. I/O data goes to /proc/diskstat and memory to /proc/vmstat. Top and other tools interpret the counters written to these files. You can also look at hardware performance counters, as used by the perf utility. -- Raghu S Udiyar
On Thu, Jul 28, 2011 at 04:56:16PM -0700, Vaibhav Jain wrote:
Hi,
Is there any other way to do this? If I need to write a kernel module that needs to get cpu utilization should I still use proc/stat ? Is there a kernel function that can be used in a module ?
To get the information that's shown in /proc/stat, I'd suggest having a look at fs/proc/stat.c in the kernel tree. HTH, Jonathan Neuschäfer
Thanks !! I looked at the code for top command again and I could see /proc/stat being used. So I guess that should be the best way to do it. On Fri, Jul 29, 2011 at 12:15 PM, Jonathan Neuschäfer <j.neuschaefer@gmx.net
wrote:
On Thu, Jul 28, 2011 at 04:56:16PM -0700, Vaibhav Jain wrote:
Hi,
Is there any other way to do this? If I need to write a kernel module that needs to get cpu utilization should I still use proc/stat ? Is there a kernel function that can be used in a module ?
To get the information that's shown in /proc/stat, I'd suggest having a look at fs/proc/stat.c in the kernel tree.
HTH, Jonathan Neuschäfer
participants (4)
-
Jonathan Neuschäfer -
Mulyadi Santosa -
Raghu Udiyar -
Vaibhav Jain