简体   繁体   中英

Using perf to get events counts depending on the occurrence of other events

Is there any possible way on which I can get the value of event counters depending on the occurrence of other events? For example: if I want to know the value of performance counters each time a specific counter reach a specific value.

You can do that with perf_event_open , but AFAIK not directly with the current version of perf record .

I want to know the value of performance counters each time a specific counter reach a specific value.

Use a group of events, the "specific counter" is the group leader. For this event you set:

struct perf_event_attr leader;
leader.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_READ;
leader.sample_period = specific_value;
// set type/config accordingly
leader.read_format = PERF_FORMAT_GROUP;
group_fd = syscall(__NR_perf_event_open, &leader, tid, cpu, -1, 0);
...

struct perf_event_attr other;
other.sample_period = 0; // doesn't trigger overflows
// set type/config accordingly
syscall(__NR_perf_event_open, &other, tid, cpu, group_fd, 0);

// do the mmap dance, ioctl etc. with the fd you get for the leader
// read values from both leader and other counters in your mmap buffer.

This isn't a great or complete answer, but it's too big for a comment.


IDK if that's possible with the perf utility itself, but in theory yes you could get that for legacy events that trigger an interrupt every time their counter overflows (at a programmable overflow count; this is how event sampling granularity works). You can then read the values from the counters for other events. Probably using the same API that perf does, you could write code that does this from user-space.

But on x86 for PEBS (precise events) , you probably can't, because counter overflows put an event in a buffer instead of triggering an interrupt right then where you could do arbitrary other things. So if the event you want to use is only available as a precise event, you will need a different solution to your ultimate problem.

(Low level bonus reading about interrupts / exceptions in general, including performance events vs. PEBS: When an interrupt occurs, what happens to instructions in the pipeline? )


You probably want to know something about how events are correlated with each other. Wanting to sample other events when one overflows may be an XY problem, if you can't implement it easily.

perf record --timestamp will put a timestamp on each event. This may give you the raw data you need to learn what you want to know. Collecting the data for a partiulcar process from PMU for every 1 milli second is related, and suggests using perf script to do something with the results of perf record --all-cpus --timestamp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM