xref: /linux-tools/perf/profiler/README.md (revision 9858fde7666acb09b4fa6d3f2932b8f6bed00e9b)
1# A simple profiler
2Fit for lightwight usage, minimum dependency
3
4## Build
5```
6g++ -o profiler profiler.cpp
7```
8The code needs c++11 features, if an old g++ compiler is used, `-std=c++11` is needed.
9
10
11## How it works
12Profiler open  perf-event and collect sampled (pid,callchain) pair, whether userspace call-chain is collected depends on kernel version, a new kernel which can unwind user space stack is recommended, (I only tested the profiler on limited number of kernel version, and they all can unwind user stack, the lowest version of kernel I have tested is 3.10.0 from centos/redhat distributions.)
13For each (pid, callchain) pair, if this pid has not been symbol-collected, profiler would  parse elf information based on `/proc/[pid]/maps` and `/proc/[pid]/map_files/*`; (If the program has its symbol stripped, e.g. via `ld -s`, user space call chain will be dropped.) Symbols are stored in an ordered structure, C++ map,  after symbols collected, each callchain address is binary searched for its  function name, and then full chain is inserted into a tree.
14
15
16## Run
17The profiler would open perf event with the cgroup which controls the specified pid
18```
19./profiler <pid>
20```
21
22To create a perf-event cgroup
23
24cgroup v1
25```
26mkdir /sys/fs/cgroup/perf_event/<somename>
27echo $$ > /sys/fs/cgroup/perf_event/<somename>/cgroup.procs
28# run the target progrom
29# run profiler with any pid within the cgroup
30```
31For cgroup v2, just use /sys/fs/cgroup/<somename>
32
33
34## Example
35When profiler terminated, a report is generated, following is an example showing the performance impact from seccomp when running a high-IO program within a docker container.
36![example](./example1.png "report")
37