Search code examples
clinux-kernelcpuperf

What is the exactly Perf buffer size?


after I read the manual of perf_event_open(), I have a few question about buffer's size in capture sampling event.

In the description section, it says that

A sampling event periodically writes measurements to a buffer that can then be accessed via mmap(2).

And when the buffer overflow, PMU will trigger the PMI, we can access the content of buffer via mmap() system call. (is my understanding correct?)

But the question is, how big is the Perf buffer? Can I set a new value (buffer length) to overwrite it?

Variable __u64 data_size in the struct perf_event_mmap_page only indicates the records size in this time.


Solution

  • Your understanding is correct, when you say that you can use mmap to collect perf data periodically.

    Before I begin, perf uses two buffers to record different kinds of events. An auxiliary buffer is used, under the main ring buffer( the ring buffer contains this auxiliary buffer ), to store different kinds of event information. Since you are asking me about the main perf buffer I will try answering that.

    You can modify the perf buffer size. As per the documentation, the perf buffer size is defined in terms of pages. It is defined to be 1 + 2^n pages, where one page is required to store metadata about the ring buffer that perf uses.

    When you try recording events, using perf record , (read the manpage for perf record), you will have a switch -m which will allow you to specify the number of pages that will increase/decrease the perf ring buffer. The number of (mmap) pages that you specify must be a power of 2.(However, you can also directly specify the size) Increasing the number of pages by 1, will increase the ring buffer size by 4kB (which is the page-size, specified in code by 1 << 12).

    However, as far as I know, this number of pages will only go so far as 2^31. Moreover, using such a huge buffer will risk not recording any events at all or recording arbitrary events.