Search code examples
linuxperformanceprocesscpu-usagetop-command

Efficient way to calculate cpu usage of multiple processes in linux


I need to calculate the cpu usage of multiple processes on a given server. Few of the options are as below

a)Though we can use ps to find the cpu usage of processes,ps gives the cpu usage of the process over its lifetime and not for instant.Of course when I mean instant I meant some small specific period like for 1 second or so because at any given moment the cpu is utilized by a process or not.

b)vmstat doesn't display process wise cpu usage.

c)We can use top to find the cpu usage of a process.

top -b -n 2 -d 1 | grep 'mysqld' | tail -n 1

For n=1,the top command behaves more like ps where in it gives the cpu usage of process since boot and hence using n=2 and ignoring the first line by tail would give cpu usage at that instant.

https://superuser.com/questions/609949/what-are-the-methods-available-to-get-the-cpu-usage-in-linux-command-line

But ignoring the first few lines would not work with multiple grep strings(process names) and where in the process's might not always occur in top as they keep on changing their states.

top -b -n 2 -d 1 | grep 'mysqld\|apache' | tail -n 1

So one way to do this for multiple process's is to maintain a map kind of structure and then only take the cpu usage of those processes which appear only the second time.But not sure if this is efficient.Doing a top each time for a specific process is not efficient enough obviously I suppose or is it same as a grep with multiple processes on a single top.

4)As top internally uses the /proc/stats info ,we can explicitly implement this logic ourselves.

How to calculate the CPU usage of a process by PID in Linux from C?

Which one could be a better option ?


Solution

  • You should do it using /proc as you have already discovered. Or perhaps using some higher-level library if your language of choice offers one. Parsing the output of ps and top is not winning you anything. The data you need are all in /proc, you just need to be careful to check the identity of each process over time, in case PIDs get recycled while you are collecting data (on most systems PID recycling won't happen in the span of mere seconds, so it's workable).