Monitoring CPU/RAM Use For A Single Process

If you need to monitor your CPU and Ram use... well, you should use ServerSuit! That's why we're here! If you wanted a more granular look on your own system though, we don't want to leave you hanging. Here's our article for the tools you can use for monitoring individual processes.

Procpath

2020 update (Linux/procfs-only). Returning to the problem of process analysis frequently enough and not being satisfied with the solutions I described below originally, I decided to write my own. It's a pure-Python CLI package including its couple of dependencies (no heavy Matplotlib), can potentially plot many metrics from procfs, JSONPath queries to the process tree, has basic decimation/aggregation (Ramer-Douglas-Peucker and moving average), filtering by time ranges and PIDs, and a couple of other things.

pip3 install --user procpath

Here's an example with Firefox. This records all processes with "firefox" in their cmdline (query by a PID would look like '$..children[?(@.stat.pid == 42)]') 120 times one time per second.

procpath record -i 1 -r 120 -d ff.sqlite '$..children[?("firefox" in @.cmdline)]'

Plotting RSS and CPU usage of a single process (or several) out of all recorded would look like:

procpath plot -d ff.sqlite -q cpu -p 123 -f cpu.svg
procpath plot -d ff.sqlite -q rss -p 123 -f rss.svg

Charts look like this (they are actually interactive Pygal SVGs):

RSS CPU

psrecord

The following addresses history graph of some sort. Python psrecord package does exactly this.

pip install psrecord                             # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip

For single process it's the following (stopped by Ctrl+C):

psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png

For several processes the following script is helpful to synchronise the charts:

#!/bin/bash    
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'

Charts look like: psrecord example

memory_profiler

The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help).

pip install memory_profiler
mprof run /path/to/executable
mprof plot

By default this pops up a Tkinter-based (python-tk may be needed) chart explorer which can be exported:

mprof

graphite-stack & statsd

It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack (from Grafana's authors) image and psutil and statsd client. procmon.py provides an implementation.

$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack

Then in another terminal, after starting target process:

$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'

Then opening Grafana at http://localhost:8080, authentication as admin:admin, setting up datasource https://localhost, you can plot a chart like:

grafana chart

graphite-stack & telegraf

Instead of Python script sending the metrics to Statsd, telegraf (and procstat input plugin) can be used to send the metrics to Graphite directly.

Minimal telegraf configuration looks like:

[agent]
  interval = "1s"

[[outputs.graphite]]
  servers = ["localhost:2003"]
  prefix = "testprfx"

[[inputs.procstat]]
  pid_file = "/path/to/file/with.pid"

Then run line telegraf --config minconf.conf. Grafana part is the same, except metrics names.

pidstat

pidstat (part of sysstat package) can produce output that can be easily parsed. It's useful in case when you need extra metrics from the process(es), e.g. most useful 3 groups (CPU, memory and disk) contain: %usr, %system, %guest, %CPU, minflt/s, majflt/s, VSZ, RSS, %MEM, kB_rd/s, kB_wr/s, kB_ccwr/s.

If you have any comments, feel free to post them below or find us on Twitter and Facebook!

-Until next time!

July 13 2021

Add or review comments

Please leave your comment

Existing comments

Comments 1


Peace Lee
What do you think about Guider(https://github.com/iipeace/guider)? # pip3 install guider # guider top -o /tmp/guider.out # guider draw /tmp/guider.out # guider draw /tmp/guider.out -g [PID|COMM]