Skip to contentSkip to conversation

Use of PMU counters inside kernel?

DISCUSSION Jesper Dangaard Brouer 2 participants 3 messages Jun 19, 2014 – Jun 20, 2014

lore
Share & tools
lore
#0 lore raw

Hi

I want to use/read the PMU counters for 'instructions' and 'cycles', from within the kernel, to measure the overhead of different in-kernel functions (and calc the pipeline efficiency ala perf-stat insns per cycle).

What is the prefered way/API to read these counters, without conflicting with the perf tool?

I've already looked at Andi Kleens "simple-pmu" kernel module
https://github.com/andikleen/simple-pmu/
Thus, I have figured out how I can hack this together open-coded. I'm looking for advice on howto use the existing perf APIs ?

I basically just need a in-kernel API to turn in these counters, and then I can just read them via the rdpcm instruction.

Info I already learned (correct me if I'm wrong):

The 'instructions' counter is the "Instruction Retired" counter
* Event num: 0xC0, Umask: 0x00
* Read via rdpmc 0x40000000

The 'cycles' counter is the "Unhalted core cycles" counter
* Event num: 0x3C, Umask: 0x00
* Read via rdpmc 0x40000001

I'm uncertain about the rdpmc hex values (taken from simple-pmu).

Signature · 5 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Sr. Network Kernel Developer at Red Hat
  Author of http://www.iptv-analyzer.org
  LinkedIn: http://www.linkedin.com/in/brouer
#1 lore raw
Quoted 7 lines · Jesper Dangaard Brouer

I want to use/read the PMU counters for 'instructions' and 'cycles', from within the kernel, to measure the overhead of different in-kernel functions (and calc the pipeline efficiency ala perf-stat insns per cycle).

What is the prefered way/API to read these counters, without conflicting with the perf tool?

perf_event_create_counter()

Quoted 2 lines

I basically just need a in-kernel API to turn in these counters, and then I can just read them via the rdpcm instruction.

For RDPMC you would need extra support actually, as there is no guarantee perf uses the fixed counter. In user space this information is in the perf mmap page.

Also please be aware that RDPMC is not synchronizing.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#2 lore raw
Quoted 11 lines · On Thu, 19 Jun 2014 14:11:59 -0700 Andi Kleen <[email protected]> wrote:
Quoted 7 lines · Jesper Dangaard Brouer

I want to use/read the PMU counters for 'instructions' and 'cycles', from within the kernel, to measure the overhead of different in-kernel functions (and calc the pipeline efficiency ala perf-stat insns per cycle).

What is the prefered way/API to read these counters, without conflicting with the perf tool?

perf_event_create_counter()

Thanks, guess you mean:
perf_event_create_kernel_counter()

Quoted 6 lines
Quoted 2 lines

I basically just need a in-kernel API to turn in these counters, and then I can just read them via the rdpcm instruction.

For RDPMC you would need extra support actually, as there is no guarantee perf uses the fixed counter. In user space this information is in the perf mmap page.

Hmm, so you are saying that I cannot rely on just reading the counters with the "rdpmc" inctruction. What should I then use?

I'm a little confused about the fixed counters, and the (to me) magic number that is used to read them:

// From: https://github.com/andikleen/simple-pmu
enum {
FIXED_SELECT = (1U << 30), /* == 0x40000000 */ FIXED_INST_RETIRED_ANY = 0, FIXED_CPU_CLK_UNHALTED_CORE = 1, FIXED_CPU_CLK_UNHALTED_REF = 2,
};

Quoted 1 line

Also please be aware that RDPMC is not synchronizing.

What do you mean by "not synchronizing"?

I've read some where, that I can use the "rdmsr" instruction in the kernel, and read the PCM register directly, to give me higher accuracy. Is that a bad idea?

Thanks for your feedback,

Signature · 5 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Sr. Network Kernel Developer at Red Hat
  Author of http://www.iptv-analyzer.org
  LinkedIn: http://www.linkedin.com/in/brouer