1

Is it possible to get the peak usage for a process?

Tools such as 'top' and 'ps' seem to output the instantaneous CPU usage. And 'pidstat' seems to do the average...

But do any make a note of the peak?

1

I think there is not a file that contain this info by default.

You have to record it by third party tool, then do the calculation.

For example use a script like:

while true; do uptime >> uptime.log; sleep 1; done

Then do some processing using awk for example and get the data that you want.

This will give you the load in every minute:

awk  '{print $1, substr($8, 1, length($8)-1)}' uptime.log

now you can get your peak usage.

1

A possible solution could be using ps as you suggested, a shell script, and schedule a cron.

The shell script should get the current cpu usage of the process, compare it with the previous max value, and if it is greater update the file.

#!/bin/bash

CURRENT_VALUE=`ps -p 3445 -o %cpu | sed -n '2p'`
PREVIOUS_VALUE=`sed -n '2p' max_cpu_usage_of_pid.sh.out`

if (( $(echo "$PREVIOUS_VALUE $CURRENT_VALUE" | awk '{print ($1 > $2)}') )); then
        ps -p 3445 -o %cpu > max_cpu_usage_of_pid.sh.out
fi
  • Note 1: max_cpu_usage_of_pid.sh.out file must exist
  • Note 2: I had some difficulties on compare two decimal numbers in bash script, this solved

PS: I'm not really an expert of bash scripting, any improve or suggestion is welcomed : )

  • I don't know if cron is the best place for this sort of thing. It's probably a one-off thing and once a minute is probably too long to wait for the next sample. You should probably wrap the if statement in an infinite while loop with a subsecond wait at the end. – Bratchley Dec 12 '16 at 16:01
1

The short answer would be there is not or probably not what you are looking for.

Process CPU stats are for the most part cumulative. Every jiffy the OS decides what processes to run using the scheduler so at any given point if you narrow it down to the smallest time value, a process uses 100% of the CPU. With multiple cores its 1/Nth of the available processors.

The only way you might get it is using a longer sample size and then keep on looking at the cumulative values. This is how in effect top works. If the user jiffy counter was 100 and is now 150 we now know 50 jiffies of CPU were "consumed" in that time period. Looking at the system counters we can then determine what that is as a percentage.

So you could do something like run top with a low refresh time and in batch mode. That might give you what you need but I do wonder why you need this and will it answer whatever question you have of your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.