Skip to content

LucasHes/linux_perf_cpu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux CPU Performance Monitoring Tool

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.

Features

  • 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

Usage

./cpu_performance_monitor.sh [options]

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

Examples

# 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.txt

Metrics

CPU static information

Collected 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

CPU usage

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

Load average

From uptime, recording:

  • 1-minute average load
  • 5-minute average load
  • 15-minute average load

Context switches

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.

Interrupt statistics

  • Hardware interrupts: parsed from /proc/interrupts
  • Software interrupts: parsed from /proc/softirqs with per-type counts (e.g., TIMER, NET_TX, NET_RX)

CPU-intensive processes

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)

Outputs

The script produces two files:

  1. CPU static info report: CPU hardware and configuration
  2. 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)

Notes

  1. Root privileges may be required to read some system files
  2. Prefer running under relatively stable system load for representative data
  3. Temporary files are cleaned on Ctrl+C or normal exit
  4. Remaining time is displayed during monitoring

Dependencies

  • bc: floating-point arithmetic
  • top: collect CPU usage
  • ps: process information
  • vmstat: context switch information
  • Basic text tools: awk, grep, cut, etc.

About

Linux CPU performance check

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages