My repository of tests of Perf tool in UNIX (Linux) to measure time and cycles of executed code.
results_all/ ├── decreased_system_run │ ├── no_stress │ │ ├── sort_decreasing │ │ ├── sort_random │ │ └── test_delay │ └── stress │ ├── sort_decreasing │ ├── sort_random │ └── test_delay └── normal_run ├── no_stress │ ├── sort_decreasing │ ├── sort_random │ └── test_delay └── stress ├── sort_decreasing ├── sort_random └── test_delay
Install Perf using:
sudo apt install linux-tools-`uname -r`
edit sysctf.conf file and add line kernel... to enable system profiling
nano /etc/sysctl.conf
kernel.perf_event_paranoid = -1
Create .c file and compile it using gcc:
gcc -g -o <output-file-name> <file-name>
"-g" flag used to produce debugging information in the operating system's native format
now as root:
I. create software event for entering function perf probe --exec compiled-file 'function_name'
Perf probe tracepoint naming convention prefix "probe_", executable name plus ":" and function name
i.e.: probe_test:functionB
II. create software event for exiting function perf probe --exec compiled-file 'function_name%return'
Perf probe tracepoint naming convention prefix "probe_", executable name plus ":" and function name and suffix "__return"
i.e.: probe_test:functionB__return
III. Now you are able to measure tracepoints.
Use:
1. sudo perf record --event your_event --event your_event_ret executable 2. sudo perf record --event your_event --event "{your_event_ret,cycles:u}:S" executable
1. Use if you want to measure only time difference between tracepoints
2. Use if you want to measure time difference between tracepoints and also amount of cycles between first and second tracepoint (i.e. second event causes to read cycles counter)
IV. Now you can read Perf measurements
sudo perf script --ns --deltatime > file_name
V. In the final step, you can run pps.py script to analyze and plot data.
./pps.py --ignore="ignored_tracepoints" --tracepoints="wanted_tracepoints" --file file_name_fro_perf_script --stress true_or_false --date date_time
Example run:
sudo perf probe --exec sort_decreasing 'funcB' sudo perf probe --exec sort_decreasing 'funcB%return' sudo perf record --event probe_sort_decreasing:funcB --event "{probe_sort_decreasing:funcB__return,cycles:U}:S" sort_decreasing sudo perf script --ns --deltatime > perf_test source venv/bin/activate ./pps.py --ignore="probe_sort_decreasing:funcB" --tracepoints="probe_sort_decreasing:funcB__return, cycles:U" --file perf_test --stress false --date 121212_010101 sudo perf probe --del probe_sort_decreasing:funcB --quiet sudo perf probe --del probe_sort_decreasing:funcB__return --quiet
Also you should install stress tool (tool to impose load on and stress test a computer system)
sudo apt install stress
Show potential probe-able functions.
perf probe --exec executable_name --funcs
Show source code lines.
perf probe --exec executable_name --line function_name
If command perf probe was used with "--force" switch then each new probe (of the same name) will have suffix of "_x", where x is number.
Run all tests by using:
sudo ./run_all --system [true|false]Run command below, to get help:
./run_all -h
Flags description: -s, --system (bool) : Required, takes boolean value. True: Run with decreased system stats in BIOS: > SpeedStep disabled - CPU frequency multiplier change disabled > HyperThreading disabled - CPU virtual cores disabled, only phisical cores > TurboBoost disabled - CPU turbo (Max) frequency disabled Also for better performance (kernel configuration): > Change CPU governor to performance, to lock CPU frequency in constant (high) value, using: sudo cpupower frequency-set --governor performance > Force tests to run on single core only (no context switching) using flag: -f or --force-single-core False: Run with normal system stats (No options disabled in BIOS) -q, --quiet : Optional, default all output will be displayed. Disable output from test scripts. -c, --comment (text) : Optional, default no comment.txt file generated. Create comment.txt file in results dir, with informations about how script been called, and comment itself -f, --force-single-core : Optional, default permit context switching. Force tests to run on single core, which disables context switching between other CPU which positively affects measurements (less noise)