pidstat

On this page 7

pidstat is a command-line tool from the sysstat package that reports resource usage per process and per thread on Linux, sampling CPU time, memory faults, disk I/O, context switches and stack usage at a fixed interval. In Linux monitoring, it fills the gap between system-wide counters such as vmstat or mpstat and interactive viewers such as top: it answers which task consumed the resource, in a plain, scriptable text format.

Because pidstat reads the same kernel accounting data that the kernel exposes under procfs, it adds almost no overhead and needs no tracing infrastructure. Administrators use it to attribute load, to compare threads of one service and to record repeatable time series during an incident or a benchmark.

How pidstat collects per-process statistics

pidstat works by reading counters from /proc twice and printing the difference. For every task it parses files such as /proc/[pid]/stat, /proc/[pid]/io and /proc/[pid]/status, stores the cumulative values, waits for the sampling interval, reads them again and divides the delta by the elapsed time. The result is a rate per second or a percentage of one CPU, not a lifetime total.

The basic syntax is pidstat [options] [interval [count]]. Without an interval, pidstat prints averages since boot or since process start, which are rarely useful for diagnosis. With pidstat 1 10, it prints ten one-second reports followed by an Average line per task, which is the form most practitioners rely on.

By default, pidstat shows only tasks that were active during the interval. Idle processes disappear from the output, which keeps reports short on busy servers but can surprise newcomers. The option -p ALL includes every task, and -p followed by a list of PIDs restricts the report to selected processes.

The following table maps the main report types to their switches and the kernel sources they read.

OptionReportKernel sourceKey columns
-u (default)CPU utilization/proc/[pid]/stat%usr, %system, %guest, %wait, %CPU
-rPage faults and memory/proc/[pid]/stat, statusminflt/s, majflt/s, VSZ, RSS, %MEM
-dDisk I/O/proc/[pid]/iokB_rd/s, kB_wr/s, kB_ccwr/s, iodelay
-wContext switches/proc/[pid]/statuscswch/s, nvcswch/s
-sStack usage/proc/[pid]/statusStkSize, StkRef
-vKernel tables/proc/[pid]/stat, fdthreads, fd-nr
-RScheduling policy/proc/[pid]/statprio, policy
Main pidstat report types and their data sources

Reading the pidstat output columns

Each pidstat column measures one precise counter, and several of them are easy to misread. The CPU report expresses usage as a percentage of a single CPU, so a multithreaded process on an eight-core host can legitimately show up to 800 percent. The option -I divides the value by the number of processors on SMP systems.

The table below explains the columns that matter most in daily diagnosis and what an abnormal value usually indicates.

ColumnMeaningWhat a high value suggests
%usrTime in user spaceComputation in application code
%systemTime in kernel codeHeavy system calls, locking or networking
%waitRunnable but waiting for a CPUCPU saturation or cgroup throttling
majflt/sFaults that required disk readsMemory pressure or swapping
kB_ccwr/sWrites canceled before flushTemporary files deleted quickly
iodelayClock ticks blocked on block I/OSlow storage affecting this task
nvcswch/sInvoluntary context switchesCompetition for CPU time
cswch/sVoluntary context switchesFrequent blocking on I/O or locks
Important pidstat columns and how to interpret them

The %wait column deserves particular attention. It reflects scheduler run-queue delay, not I/O wait, and therefore reveals CPU contention that the %CPU figure hides. The iodelay field comes from delay accounting, which on recent kernels must be enabled with the delayacct boot parameter or the kernel.task_delayacct sysctl; otherwise it stays at zero.

Per-thread and per-command filtering

pidstat can descend from processes to individual threads, which is its main advantage over many other tools. The option -t prints a TGID line for each process followed by one TID line per thread, so an administrator can see that one worker thread of a database consumes most of the CPU while the others sleep.

Several options narrow the scope. -C filters by a command name pattern, -G by process name, and -T CHILD accumulates statistics of a process together with its terminated children, which suits build jobs and shell scripts that spawn short-lived helpers. The option -l prints the full command line, and -h puts every metric on a single line for machine parsing.

  • pidstat -t -p 1234 1 shows every thread of process 1234 each second.
  • pidstat -u -C nginx 5 12 samples all nginx processes for one minute.
  • pidstat -d -l 2 lists disk activity with full command lines.
  • pidstat -T CHILD -e make -j8 measures a build including its children.

pidstat belongs to a family of sampling tools that each answer a different question. It is not interactive, it does not keep history by itself and it does not trace individual events. Its strength is per-task attribution in reproducible text, which complements the system-wide view of other utilities.

ToolScopeOutput styleBest used for
pidstatPer process and threadInterval text reportsAttributing load to tasks
top and htopPer processInteractive screenQuick live inspection
iotopPer process I/OInteractive screenSpotting live disk writers
mpstatPer CPU coreInterval text reportsUneven core utilization
sarSystem-wideStored historyTrends over days
strace and perfEvents and samplesTraces and profilesExplaining why a task is slow
pidstat compared with neighboring tools

A practical rule follows from this division. Use system-wide tools to detect a problem, pidstat to identify the responsible task, and tracing tools such as strace, perf or bpftrace to explain its behavior.

Limitations and common mistakes

pidstat is accurate within its design, but its sampling model imposes real limits. A process that starts and exits between two samples is never reported, so short-lived tasks require -T CHILD on a parent, the -e option or an event-based tool. Values are also rounded to clock ticks, which makes very small percentages noisy.

MistakeConsequenceFix
Running without an intervalLifetime averages hide spikesAlways pass an interval such as 1 or 5
Reading %CPU as a host shareOverestimating loadUse -I or divide by core count
Confusing %wait with I/O waitWrong diagnosisTreat it as CPU run-queue delay
Ignoring permissionsMissing I/O data for other usersRun as root for -d on foreign tasks
Expecting iodelay by defaultColumn stays at zeroEnable task delay accounting
Typical pidstat mistakes and their fixes

Another frequent misunderstanding concerns disk statistics. The kB_wr/s value counts data that the task caused to be written to storage, which can happen later through writeback, so page cache behavior can shift writes in time. Container users should also remember that pidstat sees host PIDs unless it runs inside the same PID namespace.

The short version

pidstat is a tool that tells you which program is using the computer, and how much. Other tools can say that the processor is busy or that the disk is working hard. pidstat names the specific program, or even the specific part of a program, that causes it.

Think of an electricity bill for a whole house compared with a meter on every appliance. The house bill shows that something uses too much power. pidstat is the meter on each appliance, read again every second so you can watch the numbers change.

Where it makes a difference

An operations engineer receives an alert that an API server with eight cores shows rising response times, although overall CPU usage sits at a moderate 55 percent. She runs pidstat -u -t -p 4120 1 30 against the Java application process and notices that one thread shows %CPU near 100 while also reporting a %wait of about 30 percent for the request workers.

A second run with pidstat -w -t -p 4120 1 reveals thousands of involuntary context switches per second among the worker threads. The busy thread turns out to be a garbage collection thread competing with request handlers inside a container limited to four CPUs. After the team raises the CPU quota and adjusts the collector settings, the %wait values fall below 5 percent and median latency returns to its normal level within the hour.