18

I want to use the /proc to find the resource usage of a particular process every second. The resources include cputime, disk usage and network usage. I looked at /proc/pid/stat , but I am not sure whether I am getting the required details. I want all 3 resource usage and I want to monitor them every second.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
sethu
  • 1,691
  • 4
  • 22
  • 34
  • 1
    I want the information for all 3 resources (cpu,disk and network) – sethu Jul 08 '11 at 04:54
  • The /proc/pid/stat gives me the CPUtime but I am able to collect only the total network and disk statistics of the whole machine and not that particular process – sethu Jul 08 '11 at 14:27
  • Don't think there is a way to collect disk and network stats on a per process basis -- mostly down to how the disk and network traffic is done as kernel work. For example, the reading ans writing to disk is through kernel buffers, and hence multiple processes may access the same buffer before it is actually flushed to disk. Same for read, multiple processes may read the same file, only the first one will cause an actual disk access with the second just getting a pointer to the kernel buffer. – Soren Jul 09 '11 at 00:01
  • there are two tools called iostat and nethog which provides me that functionality. I am not too sure about the internal working of linux. I will read about them and try to understand it , so that i can put a hook somewhere . – sethu Jul 21 '11 at 15:06
  • In order to monitor every second you may use `watch` – etuardu Oct 06 '11 at 23:41
  • This is a very good question and I'm surprised there isn't an answer. There's htop and iotop and speedometer but there's no one tool that pulls it all together, which is too bad, it would be a great boon to diagnosing errant programs. – stu Dec 07 '15 at 21:53

8 Answers8

8

Some newer kernels have /proc/<pid_of_process>/io file. This is where IO stats are. It is not documented in man proc, but you can try to figure out the numbers yourself.

Hope it helps. Alex.

EverJay Dihenkar
  • 143
  • 1
  • 1
  • 10
Alexander Sandler
  • 2,078
  • 2
  • 19
  • 21
2

checkout glances.

It's got cpu disk and network all on one screen. It's not per process but it's better than looking at 3 separate tools.

amirouche
  • 7,682
  • 6
  • 40
  • 94
stu
  • 8,461
  • 18
  • 74
  • 112
1

getrusage() accomplishes cpu, memory and disk etc.

man 2 getrusage

I don't know about network.

Tomas
  • 57,621
  • 49
  • 238
  • 373
0

The best way to approach problems like this is to look up the source code of tools that perform similar monitoring and reporting. Although there is no guarantee that they are using /proc directly, they will lead you to an efficient way to tackle the problem. For your case, top(1), iotop(8) and nethogs(8) come to mind.

0

You can use SAR

-x report statistics for a given process.

See this for more details: http://www.linuxcommand.org/man_pages/sar1.html

Example: sar -u -r -b 1 -X PID | grep -v Average | grep -v Linux

  • This technique requires a reboot of the system and root must enable system-wide collection of statistics. – Valerio Schiavoni Apr 03 '14 at 09:00
  • With `-x` or `-X` option, I am only seeing stats for `minflt/s majflt/s %user %system nswap/s CPU`, not disk or network stats. E.g., if I also specify `-n DEV` to see network stats, sar gives separate lines with separate headers. I tested it on RHEL 5.7. – haridsv Apr 07 '14 at 05:50
0

Don't think there is a way to get the disk and network information on a per process basis.

The best you can have is the global disk and network, and the per process CPU time.

All documented in man proc

Soren
  • 14,402
  • 4
  • 41
  • 67
  • You can track some IO info (see the `iotop` utility for an example), but it's not available on all kernels. –  Jul 09 '11 at 01:13
  • iotop needs a newer version of kernel .I saw iotop for io and nethog for network statistics but I need to recompile a newer version of kernel for that. I was having that as last resort . – sethu Jul 21 '11 at 15:04
0

You can use top

SYNOPSIS
    top -hv|-bcHiOSs -d delay -n limit -u|U user -p PID -o champ -w [columns]

This is a screen capture of top in a terminal

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459