How to get thread processor utilization in Redhat Linux

I need to get processor utilization metrics for all threads in a process.

  • Operating System = Redhat linux
  • programming language = C ++ using POSIX
  • requirements = you need to take samples every few seconds indefinitely, and not just for one snapshot in time.
  • constraints = not allowed to write additional code in the stream

    I know you can use the "top" command, but what other methods exist? Is there a flag for "ps"?

Thank you in advance for your help.

+6
performance multithreading linux cpu
source share
4 answers

You can read the contents of /proc/[your PID]/stat to get information for the whole process, and if you have a 2.6 kernel, there is also /proc/[your PID]/task/[thread ID]/stat with information for individual threads. (see here )

In particular, you will find these two fields:

The number of jiffies that this process has been scheduled in user mode.

stime% lu

The number of jiffies that this process has been scheduled in kernel mode.

cutime% ld

The problem part here is the unit in which the values ​​are indicated. Jiffy is 1 / HZ seconds, where HZ is the core clock frequency, and determining this clock frequency is a difficult part.

If you need it only for one specific system, you can simply run some tests or look at the kernel headers and write this value to your program. If you want to learn how to define it in a more general way, you can see how the tool does it, for example, top by looking at its source code (see the old_Hertz_hack() function and related comments)

+6
source share

Perhaps an easier way to do this is to use getrusage with the linux extension for RUSAGE_THREAD . Once you have this time, you can simply subtract the time the last time you tried and divide by the real time elapsed since the last sample. This means that you are using the processor as a percentage.

For specific Linux documentation, see the rusage liunx man page .

+1
source share

You may be able to do this with gnu gprof , available on Linux. I believe that they claim to support threads, but this is more complicated than profiling at the process level, so you cannot get the results you are looking for.

Here's howto for your situation.

0
source share

you can use the "top" command to find the process id first.

After that, you can use the following command to display CPU / memory usage for the process

 top -p {pid} 

After that you can press "Shift" + "h" to show the streams

0
source share

All Articles