Linux Comm Perf
[ Linux Command Encyclopaedia](#)\\n\\n* * *\\n\\nperf is a Linux system performance analysis toolkit, full name is Performance Event Counters. It is based on the Linux kernel's perf_events subsystem and provides hardware and software-level performance analysis capabilities.\\n\\nThe main functions of perf include:\\n\\n* CPU performance analysis\\n* Function call tracing\\n* Hardware event statistics\\n* Software event monitoring\\n* System call tracing\\n\\n* * *\\n\\n## perf Basic Syntax\\n\\nThe basic syntax format of perf command is:\\n\\nperf COMMAND \\nCommon subcommands include:\\n\\n* `stat`: Performance counter statistics\\n* `record`: Record performance data\\n* `report`: Analyze recorded data\\n* `top`: Real-time performance monitoring\\n* `list`: List available events\\n* `annotate`: Source code level analysis\\n\\n* * *\\n\\n## perf Common Subcommands Detailed Explanation\\n\\n### perf stat\\n\\nStatistics of various hardware and software events during command execution.\\n\\nperf stat command \\nCommon options:\\n\\n* `-e`: Specify events to monitor\\n* `-p`: Monitor specified process ID\\n* `-a`: Monitor all CPUs\\n* `-r`: Repeat run and show average\\n* `-d`: Show more detailed events\\n\\nExample:\\n\\n## Example\\n\\n# Statistics of ls command execution\\n\\n perf stat ls\\n\\n# Monitor specified process\\n\\n perf stat-p 1234\\n\\n# Monitor specific events\\n\\n perf stat-e cycles,instructions,cache-misses ls\\n\\n### perf record\\n\\nRecord performance data to file (default perf.data).\\n\\nperf record command \\nCommon options:\\n\\n* `-g`: Record call graph\\n* `-F`: Sampling frequency (Hz)\\n* `-p`: Record specified process\\n* `-o`: Specify output file\\n* `-e`: Specify events to record\\n\\nExample:\\n\\n## Example\\n\\n# Record ls command execution\\n\\n perf record ls\\n\\n# Record process 1234 at 99Hz frequency\\n\\n perf record -F 99-p 1234-g\\n\\n### perf report\\n\\nAnalyze data recorded by perf record.\\n\\nperf report \\nCommon options:\\n\\n* `-i`: Specify input file\\n* `-n`: Show sample count\\n* `--stdio`: Text mode output\\n* `-g`: Show call graph\\n* `-s`: Sort by specified field\\n\\nExample:\\n\\n## Example\\n\\n# Analyze default perf.data file\\n\\n perf report\\n\\n# Analyze specified file and output in text mode\\n\\n perf report -i perf.data.old --stdio\\n\\n### perf top\\n\\nReal-time display of functions that consume the most resources in the system.\\n\\nperf top \\nCommon options:\\n\\n* `-e`: Specify monitoring event\\n* `-p`: Monitor specified process\\n* `-K`: Hide kernel symbols\\n* `-U`: Hide user space symbols\\n* `-g`: Show call graph\\n\\nExample:\\n\\n## Example\\n\\n# Real-time system performance monitoring\\n\\n perf top\\n\\n# Monitor specific events\\n\\n perf top -e cache-misses\\n\\n### perf list\\n\\nList all monitorable events.\\n\\nperf list [hw|sw|cache|tracepoint|pmu|event_glob]\\nExample:\\n\\n## Example\\n\\n# List all events\\n\\n perf list\\n\\n# List hardware cache events\\n\\n perf list cache\\n\\n* * *\\n\\n## perf Event Types\\n\\nperf can monitor multiple types of events:\\n\\n| Event Type | Description | Example |\\n| --- | --- | --- |\\n| Hardware | CPU hardware events | cycles, instructions |\\n| Software | Kernel software events | context-switches, page-faults |\\n| Cache | Cache related events | cache-references, cache-misses |\\n| Tracepoints | Kernel static tracepoints | syscalls, block, sched |\\n| PMU | Processor specific events | (vendor specific) |\\n| Breakpoints | Breakpoint events | mem:[:access] |\\n\\n* * *\\n\\n## perf Practical Application Examples\\n\\n### 1. Analyze Program Performance Bottlenecks\\n\\n## Example\\n\\n# Record program execution\\n\\n perf record -g ./my_program\\n\\n# Analyze results\\n\\n perf report -g\\n\\n### 2. Find CPU Hotspot Functions\\n\\nperf top -p $(pidof my_program)\\n### 3. Compare Performance Differences Between Two Runs\\n\\n## Example\\n\\n# First run\\n\\n perf record -o perf.data.1 ./my_program input1\\n\\n# Second run\\n\\n perf record -o perf.data.2 ./my_program input2\\n\\n# Compare differences\\n\\n perf diff perf.data.1 perf.data.2\\n\\n### 4. Analyze System Calls\\n\\n## Example\\n\\n# List available system call tracepoints\\n\\n perf list 'syscalls:*'\\n\\n# Trace open system call\\n\\n perf stat-e'syscalls:sys_enter_open'-a sleep 10\\n\\n* * *\\n\\n## perf Usage Tips\\n\\n1. **Reduce overhead**: For long-running performance analysis, appropriately reduce sampling frequency (e.g., -F 99)\\n\\n2. **Symbol resolution**: Ensure debug symbols are available, can be installed via debug packages or compile program with -g\\n\\n3. **Flame Graph generation**: Combine with FlameGraph tool to generate intuitive performance analysis charts\\n\\n4. **Multi-core analysis**: Use -a option to monitor all CPUs, or use -C to specify specific CPU\\n\\n5. **User/Kernel space separation**: Use -k and -u options to analyze kernel and user space separately\\n\\n* * *\\n\\n## Common Problem Solutions\\n\\n1. **Permission issues**:\\n\\necho -1 > /proc/sys/kernel/perf_event_paranoid\\nor use sudo to run perf\\n\\n2. **Missing symbol information**:\\n\\n * Ensure program is compiled with -g option\\n * Install debug symbol packages\\n\\n3. **Sampling data too large**:\\n\\n * Reduce sampling frequency\\n * Shorten sampling time\\n * Use --no-call-graph to reduce call graph information\\n\\n4. **Unable to parse high-level languages like Java/Python**:\\n\\n * Requires perf tool support for specific languages\\n * Consider using language-specific analysis tools\\n\\n* * *\\n\\n## Advanced Learning Resources\\n\\n1. **Official documentation**: tools/perf/Documentation directory in Linux kernel source code\\n\\n2. **Book recommendations**:\\n\\n * γSystems Performance: Enterprise and the Cloudγ\\n * γSystems Performance: Enterprise and the Cloud\\n\\n3. **Online resources**:\\n\\n * Brendan Gregg's blog and toolset\\n * perf-tools toolset\\n\\n4. **Related tools**:\\n\\n * FlameGraph: Visualize performance data\\n * bpftrace: More flexible dynamic tracing\\n\\nBy mastering the perf command, you can deeply analyze Linux system performance characteristics, quickly locate performance bottlenecks, and optimize application and system configuration.\\n\\n* * Linux Command Encyclopaedia](#)
YouTip