TrumanWong

pidstat

Monitor the system resource usage of processes

illustrate

pidstat is a command of the sysstat tool, which is used to monitor the usage of system resources such as CPU, memory, threads, and device IO for all or specified processes. When pidstat is run for the first time, various statistical information will be displayed since the system startup. Running pidstat thereafter will display statistical information since the last time the command was run. Users can obtain the required statistical information by specifying the number and time of statistics.

grammar

pidstat [options] [<time interval>] [<number of times>]

Options

-u: Default parameter, displays cpu usage statistics of each process -r: Display memory usage statistics of each process -d: Display the IO usage of each process -w: Display the context switching status of each process -t: Displays statistics about threads -p: Specify the process number

  • T { TASK | CHILD | ALL } This option specifies what pidstat monitors. TASK means reporting an independent task, and the CHILD keyword means reporting statistical information of all threads under the process. ALL means reporting on an independent task and all threads under the task. Note: Global statistics for tasks and child threads have nothing to do with the pidstat option. These statistics will not correspond to the current statistical interval. These statistics will only be collected when the child thread is killed or completed. -V: version number -h: Shows all activities on one line so other programs can easily parse. -I: In SMP environment, indicates the CPU usage/number of cores of the task -l: Display the command name and all parameters

Example

View the CPU usage of all processes

# pidstat -u -p ALL
11:04:06 AM UID PID %usr %system %guest %CPU CPU Command
11:04:06 AM 0 1 0.03 0.05 0.00 0.08 20 systemd
11:04:06 AM 0 2 0.00 0.00 0.00 0.00 28 kthreadd
11:04:06 AM 0 3 0.00 0.00 0.00 0.00 0 rcu_gp
11:04:06 AM 0 4 0.00 0.00 0.00 0.00 0 rcu_par_gp
11:04:06 AM 0 6 0.00 0.00 0.00 0.00 0 kworker/0:0H-kblockd
11:04:06 AM 0 9 0.00 0.00 0.00 0.00 38 mm_percpu_wq
11:04:06 AM 0 10 0.00 0.01 0.00 0.01 0 ksoftirqd/0
11:04:06 AM 0 11 0.00 0.23 0.00 0.23 15 rcu_sched
11:04:06 AM 0 12 0.00 0.00 0.00 0.00 0 migration/0
11:04:06 AM 0 14 0.00 0.00 0.00 0.00 0 cpuhp/0
11:04:06 AM 0 15 0.00 0.00 0.00 0.00 1 cpuhp/1
...

Header field description

  • PID: Process ID
  • %usr: The percentage of CPU occupied by the process in user space
  • %system: The percentage of CPU occupied by the process in the kernel space
  • %guest: the percentage of CPU occupied by the process in the virtual machine
  • %CPU: The percentage of CPU occupied by the process
  • CPU: cpu number of the processing process
  • Command: the command corresponding to the current process

View the memory usage of all processes

# pidstat -r
11:10:35 AM UID PID minflt/s majflt/s VSZ RSS %MEM Command
11:10:35 AM 0 1 7.24 0.05 191312 4208 0.01 systemd
11:10:35 AM 0 1407 2.02 0.09 39316 9016 0.03 systemd-journal
11:10:35 AM 0 1442 0.03 0.00 190380 1080 0.00 lvmetad
11:10:35 AM 0 1457 0.46 0.00 46252 1560 0.00 systemd-udevd
11:10:35 AM 0 2429 0.11 0.00 163620 9472 0.03 sshd
11:10:35 AM 992 2446 0.02 0.00 8588 996 0.00 lsmd
11:10:35 AM 0 2447 0.05 0.00 90572 1468 0.00 rngd
11:10:35 AM 0 2448 0.15 0.02 451272 3520 0.01 udisksd
11:10:35 AM 0 2449 0.18 0.02 396488 1948 0.01 accounts-daemon
11:10:35 AM 0 2454 0.02 0.02 201336 1464 0.00 gssproxy
...

Header field description

  • PID: process identifier
  • Minflt/s: Minor errors per second for tasks that do not require loading memory pages from disk
  • Majflt/s: Major errors per second for tasks that require loading memory pages from disk
  • VSZ: virtual address size, virtual memory usage in KB
  • RSS: resident set size, non-swap area physical memory usage in KB
  • Command: task command name

View the IO usage of all processes

# pidstat -d
11:12:30 AM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
11:12:30 AM 0 1 250.05 11.57 2.13 systemd
11:12:30 AM 0 224 1.21 0.00 0.00 khugepaged
11:12:30 AM 0 290 0.03 0.00 0.00 kswapd0
11:12:30 AM 0 291 0.02 0.00 0.00 kswapd1
11:12:30 AM 0 1312 1.03 0.00 0.00 xfsaild/dm-0
11:12:30 AM 0 1407 1.31 0.00 0.00 systemd-journal
11:12:30 AM 0 1442 0.01 0.00 0.00 lvmetad
11:12:30 AM 0 1457 1.77 0.00 0.00 systemd-udevd
11:12:30 AM 992 2446 0.79 0.00 0.00 lsmd
11:12:30 AM 0 2447 0.07 0.00 0.00 rngd
11:12:30 AM 0 2448 0.46 0.00 0.00 udisksd
...

Header field description -PID: process id

  • kB_rd/s: KB read from disk per second
  • kB_wr/s: KB written to disk per second
  • kB_ccwr/s: KB written to disk when the task was canceled. Occurs when a task truncates a dirty pagecache.
  • COMMAND: command name of task

Check the context switching of the process

# pidstat -w
11:15:52 AM UID PID cswch/s nvcswch/s Command
11:15:52 AM 0 1 3.15 0.03 systemd
11:15:52 AM 0 2 0.13 0.00 kthreadd
11:15:52 AM 0 3 0.00 0.00 rcu_gp
11:15:52 AM 0 4 0.00 0.00 rcu_par_gp
11:15:52 AM 0 6 0.00 0.00 kworker/0:0H-kblockd
11:15:52 AM 0 9 0.00 0.00 mm_percpu_wq
11:15:52 AM 0 10 1.99 0.00 ksoftirqd/0
...

Header field meaning

  • PID: process id
  • Cswch/s: number of active task context switches per second
  • Nvcswch/s: Number of passive task context switches per second
  • Command: command name