4

I have been using the Linux perf tool in the user space. I want to write code that reads performance counters for a thread every time it does a context switch.

The steps required are:

1) Get a mechanism to read the performance counter registers.

2) Call step(1) from the scheduler after every context switch.

I am stuck at step(1) as I could not figure out which functions to call for reading the performance registers and how to describe an event while doing it. I tried going through the docs and also this question How do I use performance counters inside of the kernel?.

  • `perf` is a tool for using the performance counters subsystem in Linux. How are you using `perf` ? Which performance counters are you looking to read from ? Do you have an example where you have tried to use `perf` from userspace ? – Arnabjyoti Kalita Feb 21 '19 at 01:40
  • Yes, I have been using perf for some time. The counters I wish to measure are all there in perf list events, basically all the Hardware cache events. I have only used it from the user space till now with the -I flag which gave me counters after a fixed intervel. – Nikhilesh Singh Feb 21 '19 at 13:35

1 Answers1

4

You can actually do this entirely with perf by using tracepoint events and group leader sampling.

The sched:sched_switch is a tracepoint event triggering on every context switch. Putting that event with other events into a group with enabled group leader sampling will allow you to read the non-leader counters whenever a leader sample happens. The syntax looks like this:

perf record -e "{sched:sched_switch,cycles,instructions}:S" -a

This will record cycles values and instructions on every CPU whenever there is a context switch. You can check the output with perf script, which also allows you to read it with python programs.

If you want to monitor in your own program, you can use perf_event_open with PERF_FORMAT_GROUP and PERF_SAMPLE_READ.

The perf tools and it's underlying perf_event_open interface are very powerful, but the documentation can sometimes be lacking. If you need even more flexibility, you can use BPF and bcc.

Zulan
  • 21,896
  • 6
  • 49
  • 109
  • 1
    I think we can use a similar command for https://stackoverflow.com/questions/54674710/using-pebs-and-linux-perf-to-count-the-number-of-cpu-cycles-passed-to-execute-x. – Hadi Brais Feb 21 '19 at 15:45
  • Thanks @Zulan, How can I access the counter values in run time? I am planning to this in the kernel itself, but not quite sure, how to. Also, can you comment on the overhead involved? – Nikhilesh Singh Feb 21 '19 at 16:38
  • 2
    *Why* do you want to implement stuff in the kernel when the kernel is capable of doing this for you already? It even allows you to run your own code using BPF. Modifying the kernel code itself is so much more complicated and rather fragile. – Zulan Feb 21 '19 at 20:13
  • The overhead depends... on context switch rate, number and type of counters to read, .... You will have to **measure** to figure out the overhead in your particular case. – Zulan Feb 21 '19 at 20:14