Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux perf record: difference between count (-c) and frequency (-F) options

I'm trying to understand what the -c and -F options of perf record really do but I cannot explain what I'm seeing. I'm running these commands:

perf record -a -F <frequency> sleep 1

and

perf record -a -c <count> sleep 1

trying different values of frequency and count. The results I get are the following

In the first table I set the frequency and in the second one the count. How do frequency and count affect the number of events? I thought the number of events was independent on the frequency and count, but clearly it's not the case. What does perf actually do?

like image 826
S. Rinco Avatar asked Nov 23 '18 15:11

S. Rinco


People also ask

What does perf record do?

A perf record command was used to trace the block:block_rq_issue probe, which fires when a block device I/O request is issued (disk I/O). Options included -a to trace all CPUs, and -g to capture call graphs (stack traces). Trace data is written to a perf. data file, and tracing ended when Ctrl-C was hit.

How do I read a perf data file?

There is builtin perf. data parser and printer in perf tool of linux tools with subcommand "script". perf-script - Read perf. data (created by perf record) and display trace output This command reads the input file and displays the trace recorded.

How does perf work in Linux?

Perf works on the Model Specific Registers of your CPU for measurements like cycles or branch-misses or so. A special Part called PMU(Performance Measurement Unit) is counting all kinds of events.

How do I cancel a perf record?

The perf tool records events until you terminate it by pressing Control and C (Ctrl+C).


1 Answers

Count and frequency are two fundamental switches that tune the rate of sampling when using perf record (which does sampling internally).

Count

When you run perf record -c <number>, you are specifying the sample period (where "number" is the sample period). That is, for every "number"th occurrence of the event a sample will be recorded. The sample will be recorded when the performance counter that keeps track of the number of events has overflowed.

I am guessing you are obtaining the number of events with the help of perf report. Note that perf report will never report the actual number of events, but only an approximate. The number of events will keep changing as you keep tweaking the sample period. perf report will only read the perf.data file that perf record generates, and based on the size of the file generated, it makes an assumption of the number of samples recorded (by knowing the size of a sample recorded in memory). The actual number of events recorded is obtained by -

Number of events = Fixed Sample Period * Number of samples collected

where Fixed Sample Period is what you specified with perf record -c.

Frequency

This is the other way around to express the sampling period, that is to specify the average rate of samples per second (frequency) - which you can do using perf record -F. So perf record -F 1000 will record around 1000 samples per second and these samples will be generated when the hardware/PMU counter corresponding to the event overflows. This means that the kernel will dynamically adjust the sampling period to make sure that the sampling process adheres to the sampling frequency.

This is how the sample period gets updated dynamically.

Higher the sampling frequency, higher the number of samples collected (almost proportionately).

The variation in the sampling period can be seen by running the command -

sudo perf report -D -i perf.data | fgrep RECORD_SAMPLE

Whenever the sampling period keeps varying, the total number of events will keep incrementing with the variation in the sampling period. And when the sampling period remains fixed, the total number of events remain fixed and is obtained by the formula showed above. The total number of events will be approximate in both the cases.

like image 138
Arnabjyoti Kalita Avatar answered Sep 28 '22 01:09

Arnabjyoti Kalita