I've got a problem with the kswapd0 process (pid 52), which from time to time would start to use 25% of the cpu (one full core). I usually stop it with the command sudo sh -c "echo 1 > /proc/sys/vm/drop_caches" (see IBM's page about the problem, here).

I'd like to set a watchdog, which would execute that command whenever kswapd0 keeps using 22%+ CPU for more than three seconds. Therefore I'd like to get kswapd0 cpu usage periodically, in an efficient manner.

Here are the commands I currently use to get the instant CPU usage:

I get the information from top:

top -bn1 -p $(pgrep kswapd0)

and I pipe the output to sed to extract the %CPU value from the last line:

... | tail -n1 | sed -r 's/^ *([^ ]+ +){8}([^ ]+)( +[^ ]+){3}/\2/'

I know that in interactive mode, the cpu usages displayed by top are the average cpu usages between two refreshes, but I don't know what it is when top is invoked a single time.

Note that ps -p $PID -o %CPU doesn't fit my problem since the average cpu usage it prints is done on the whole process lifetime (rather than the very last seconds).

How do I get the cpu usage with a lighter technique ? (a more appropriate one)



You must log in to answer this question.

Browse other questions tagged .