where does top gets real-time data
Asked Answered
A

2

11

Where does top application gets it's data on Linux? I would be interested in real-time CPU load/pid data.(I read allmost all documentation in /proc/pid man page, but the info isn't there).

The pid is a jboss. I need the data lightweight (to be exported easily).

Acidforming answered 28/1, 2011 at 10:17 Comment(4)
Load is a system wide metric, not a process one. Did you mean process CPU usage?Tradeswoman
are you sure the info isn't there? 'find /proc/1' for example, there's a LOT of stuff, did you go through it all?Well
@Xander, apparently not, because that is, in fact, where top gets its data ... :)Saire
@Saire there is no /proc/pid/stat field for cpu usage. the data must come from some calculation/aggregation of that file's data. but, what's the algorithm?Acidforming
S
6

As documented in proc(5), in the file /proc/(pid)/stat you have the fields:

utime %lu

Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.

stime %lu

Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).

To get CPU usage for a specific process, use those fields. The toplevel process will aggregate CPU usage over all threads; for a per-thread breakdown, you can find the other threads in /proc/(pid)/task.

If you would prefer to be notified when CPU time exceeds some threshold, you can use clock_getcpuclockid to get a handle to its cpu time clock, then timer_create or timerfd to be notified when it hits a specified level. However, note that cross-process cputime timers are an optional feature in the POSIX specification and may not be supported (I've not tested).

Saire answered 28/1, 2011 at 17:22 Comment(2)
actually I belive "ps -Lp(pid) opcpu" does aggregate all that threads, isn't it? Unfortunately ps gives just snapshots of that cpu/thread load (it can't be used in real-time monitoring), and aggregating in the bash script all 300 threads in that jboss, by "cat"-ing all the file specific for that child pids it looks kind of unreliable.Acidforming
@Xander, after checking, it turns out the toplevel process aggregates over all threads. So you can just use the stat file for monitoring. I've also added some notes on clock_getcpuclockid, which can be helpful as well.Saire
C
12

If in doubt, use strace(1)!

open("/proc/2/stat", O_RDONLY)    = 4
open("/proc/2/statm", O_RDONLY)   = 4
open("/proc/3/stat", O_RDONLY)    = 4
open("/proc/3/statm", O_RDONLY)   = 4
Cohin answered 28/1, 2011 at 15:39 Comment(1)
hi, thanks for strace hint! it's great. the only issue is that following a strace dump I can't get where it actually write to the stdout the cpu load (because analyzing the files you recommended I could't find what I was searching...)Acidforming
S
6

As documented in proc(5), in the file /proc/(pid)/stat you have the fields:

utime %lu

Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.

stime %lu

Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).

To get CPU usage for a specific process, use those fields. The toplevel process will aggregate CPU usage over all threads; for a per-thread breakdown, you can find the other threads in /proc/(pid)/task.

If you would prefer to be notified when CPU time exceeds some threshold, you can use clock_getcpuclockid to get a handle to its cpu time clock, then timer_create or timerfd to be notified when it hits a specified level. However, note that cross-process cputime timers are an optional feature in the POSIX specification and may not be supported (I've not tested).

Saire answered 28/1, 2011 at 17:22 Comment(2)
actually I belive "ps -Lp(pid) opcpu" does aggregate all that threads, isn't it? Unfortunately ps gives just snapshots of that cpu/thread load (it can't be used in real-time monitoring), and aggregating in the bash script all 300 threads in that jboss, by "cat"-ing all the file specific for that child pids it looks kind of unreliable.Acidforming
@Xander, after checking, it turns out the toplevel process aggregates over all threads. So you can just use the stat file for monitoring. I've also added some notes on clock_getcpuclockid, which can be helpful as well.Saire

© 2022 - 2024 — McMap. All rights reserved.