简体   繁体   中英

How to monitor a process in Linux CPU, Memory and time

How can I benchmark a process in Linux? I need something like "top" and "time" put together for a particular process name (it is a multiprocess program so many PIDs will be given)?

Moreover I would like to have a plot over time of memory and cpu usage for these processes and not just final numbers.

Any ideas?

I typically throw together a simple script for this type of work.

Take a look at the kernel documentation for the proc filesystem (Google 'linux proc.txt').

The first line of /proc/stat (Section 1.8 in proc.txt) will give you cumulative cpu usage stats (ie user, nice, system, idle, ...). For each process, the file /proc/$PID/stat (Table 1-4 in proc.txt) will provide you with both process-specific cpu usage stats and memory usage stats (see rss).

If you google a bit you'll find plenty of detailed info on these files, and pointers to libraries / apps / code snippets that can help you obtain / derive the values you need. With that in mind, I'll focus on the high-level strategy.

For CPU stats, use your favorite scripting language to create an executable that takes a set of process ids for monitoring. At a fixed interval (ex: 1 second) poll / calculate the cumulative totals for each process and the system as a whole. During each poll interval, write all results on a single line to stdout.

For memory stats, write a similar script, but simply log the per-process memory usage. Memory is a bit easier as we directly obtain the instantaneous values.

Run these script for the duration of your test, passing the set of processes ids that you'd like to monitor and redirecting its output to a log file.

./logcpu $(pidof foo) $(pidof bar) > cpustats
./logmem $(pidof foo) $(pidof bar) > memstats

Import the contents of these files into a spreadsheet (for certain applications this is as easy as copy / paste). For CPU, you are after instantaneous values but have cumulative values, so you'll need to do some minor spreadsheet work to derive these values (it's just the delta 't(x + 1) - t(x)'). Of course you could have your cpu logger write the delta, but you'll be spending a bit more time up front on the script.

Finally, use your spreadsheet to generate a nice plot.

Following are the tools for monitoring a linux system

  1. System commands like top , free -m , vmstat , iostat , iotop , sar , netstat , etc. Nothing comes near these linux utility when you are debugging a problem. These command give you a clear picture that is going inside your server
  2. SeaLion : Agent executes all the commands mentioned in #1 (also user defined) and outputs of these commands can be accessed in a beautiful web interface. This tool comes handy when you are debugging across hundreds of servers as installation is clear simple. And its FREE
  3. Nagios : It is the mother of all monitoring/alerting tools. It is very much customization but very much difficult to setup for beginners. There are sets of tools called nagios plugins that covers pretty much all important Linux metrics
  4. Munin
  5. Server Density : A cloudbased paid service that collects important Linux metrics and gives users ability to write own plugins.
  6. New Relic: Another well know hosted monitoring service.
  7. Zabbix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM