2

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 ?

Community
  • 1
  • 1
crackerplace
  • 5,305
  • 8
  • 34
  • 42
  • can someone say whats the reason to close this.I wanted to summarize all the ways and see if there are any better ways.Also few things like using top has a hidden issue when you don't keenly observe and which I wanted to also include.Sometimes its better to be open on why you want to close something.Also dont judge by what you know as there will be guyz who would be looking for info which might look as basics. – crackerplace Dec 06 '15 at 09:35

1 Answers1

0

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).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • ok.But getting all the PID's and then for each PID getting its cpu usage at 2 instants from proc/stats,wouldn't this take more time and if I want to push these metrics at a granularity of second. – crackerplace Dec 05 '15 at 13:10
  • If I want to do this in linux,which language would be better shell or python or any other such as C. – crackerplace Dec 05 '15 at 13:11
  • 1
    I think Python would be a great choice, at least for a first version. If it happens that you need more speed later you can always rewrite a part of it in C. – John Zwinck Dec 05 '15 at 16:36