This tool is implemented with shell scripts to collect and monitor CPU performance metrics on Linux, including CPU usage, load averages, context switches, interrupts, and CPU-intensive processes.
- Collect CPU basic information and static parameters
- Monitor CPU usage (user, system, nice, idle, IO wait, etc.)
- Track system load changes
- Distinguish voluntary and involuntary context switches
- Count hardware and software interrupts (with detailed type breakdown)
- Identify CPU-intensive processes
- Generate a formatted report
./cpu_performance_monitor.sh [options]| Option | Description |
|---|---|
-i, --interval |
Sampling interval, default 5 seconds |
-d, --duration |
Total monitoring duration, default 60 seconds |
-o, --output |
Output report filename, default cpu_performance_report_TIMESTAMP.txt |
-s, --static |
Static info filename, default cpu_static_info_TIMESTAMP.txt |
-h, --help |
Show help |
# Run with defaults
./cpu_performance_monitor.sh
# Use 10-second interval for 5 minutes
./cpu_performance_monitor.sh -i 10 -d 300
# Specify output files
./cpu_performance_monitor.sh -o my_cpu_report.txt -s my_cpu_static_info.txtCollected once and stored in a separate file, including:
| Metric | Source | Description |
|---|---|---|
| CPU model | /proc/cpuinfo |
Specific processor model |
| CPU cores | nproc --all |
Number of physical CPU cores |
| CPU threads | /proc/cpuinfo |
Number of logical processors including hyperthreads |
| Cache info | lscpu |
L1, L2, L3 cache sizes |
| CPU frequency | /proc/cpuinfo, lscpu |
Current, max and min CPU frequency |
| Architecture | lscpu |
CPU architecture, operation modes, byte order |
| CPU features | /proc/cpuinfo |
Supported instruction sets and features |
| Virtualization | lscpu |
Whether virtualization is supported and the type |
Collected each sampling interval from top and broken down as:
| Metric | Description |
|---|---|
| user (us) | Percentage in user space |
| system (sy) | Percentage in kernel space |
| nice (ni) | Percentage for niced processes |
| idle (id) | Idle percentage |
| iowait (wa) | Percentage waiting for I/O |
| hardirq (hi) | Percentage handling hardware interrupts |
| softirq (si) | Percentage handling software interrupts |
| steal (st) | CPU time stolen by other VMs in virtualization |
From uptime, recording:
- 1-minute average load
- 5-minute average load
- 15-minute average load
Total context switches from vmstat, broken into:
- Voluntary context switches: process yields (e.g., waiting for I/O)
- Involuntary context switches: preemption (e.g., time slice expired)
By traversing voluntary_ctxt_switches and nonvoluntary_ctxt_switches fields in /proc/<pid>/status.
- Hardware interrupts: parsed from
/proc/interrupts - Software interrupts: parsed from
/proc/softirqswith per-type counts (e.g., TIMER, NET_TX, NET_RX)
From ps -eo pid,pcpu,time,comm --sort=-pcpu, top 5 by CPU usage including:
- Process ID (PID)
- CPU usage (%CPU)
- Accumulated CPU time (TIME)
- Command (COMMAND)
The script produces two files:
- CPU static info report: CPU hardware and configuration
- CPU performance monitoring report: Periodic samples and summary statistics
Performance report includes:
- CPU usage statistics
- System load averages
- Context switch statistics (voluntary vs. involuntary)
- Interrupt statistics (hardware vs. software)
- Detailed softirq type counts
- CPU-intensive processes per sample (timestamped)
- Summary statistics (averages/peaks)
- Root privileges may be required to read some system files
- Prefer running under relatively stable system load for representative data
- Temporary files are cleaned on Ctrl+C or normal exit
- Remaining time is displayed during monitoring
bc: floating-point arithmetictop: collect CPU usageps: process informationvmstat: context switch information- Basic text tools:
awk,grep,cut, etc.