2019-11-13 17:13:59

by Frank A. Cancio Bello

[permalink] [raw]
Subject: [RFC 0/2] docs: ftrace: Clarify the RAM impact of buffer_size_kb

Improves the documentation of buffer_size_kb by clarifying how is
calculated the number of allocated pages for the ring buffer.

** Do not apply the second patch **. It's just for illustration
purposes.

Frank A. Cancio Bello (2):
docs: ftrace: Clarify the RAM impact of buffer_size_kb
** do not apply this patch ** Just for illustration purposes

Documentation/trace/ftrace.rst | 13 +++++++++++--
kernel/trace/ring_buffer.c | 2 ++
2 files changed, 13 insertions(+), 2 deletions(-)

--
2.17.1


2019-11-13 19:01:54

by Frank A. Cancio Bello

[permalink] [raw]
Subject: [RFC 1/2] docs: ftrace: Clarify the RAM impact of buffer_size_kb

The current text could mislead the user into believing that the number
of pages allocated by each CPU ring buffer is calculated by the round
up of the division: buffer_size_kb / PAGE_SIZE.

Clarify that the number of pages allocated is the round up of the
division: buffer_size_kb / (PAGE_SIZE - BUF_PAGE_HDR_SIZE). Add an
example that shows how the number of pages allocated could be off by
5 pages more compared with how the current text suggests it should be.

Suggested-by: Joel Fernandes (Google) <[email protected]>
Signed-off-by: Frank A. Cancio Bello <[email protected]>
---
Documentation/trace/ftrace.rst | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst
index e3060eedb22d..ec2c4eff95a6 100644
--- a/Documentation/trace/ftrace.rst
+++ b/Documentation/trace/ftrace.rst
@@ -188,8 +188,17 @@ of ftrace. Here is a list of some of the key files:
If the last page allocated has room for more bytes
than requested, the rest of the page will be used,
making the actual allocation bigger than requested or shown.
- ( Note, the size may not be a multiple of the page size
- due to buffer management meta-data. )
+
+ The number of pages allocated for each CPU buffer may not
+ be the same than the round up of the division:
+ buffer_size_kb / PAGE_SIZE. This is because part of each page is
+ used to store a page header with metadata. E.g. with
+ buffer_size_kb=4096 (kilobytes), a PAGE_SIZE=4096 bytes and a
+ BUF_PAGE_HDR_SIZE=16 bytes (BUF_PAGE_HDR_SIZE is the size of the
+ page header with metadata) the number of pages allocated for each
+ CPU buffer is 1029, not 1024. The formula for calculating the
+ number of pages allocated for each CPU buffer is the round up of:
+ buffer_size_kb / (PAGE_SIZE - BUF_PAGE_HDR_SIZE).

Buffer sizes for individual CPUs may vary
(see "per_cpu/cpu0/buffer_size_kb" below), and if they do
--
2.17.1

2019-11-13 19:02:54

by Frank A. Cancio Bello

[permalink] [raw]
Subject: [RFC 2/2] ** do not apply this patch ** Just for illustration purposes

Prints a message that will allow us to evaluate the number of pages
allocated by each CPU buffer as well the main values that participate
in its calculation.

$ echo 0 > /sys/kernel/debug/tracing/tracing_on
$ echo 4096 > /sys/kernel/debug/tracing/buffer_size_kb
.... e.g. of output:
PAGE_SIZE: 4096, BUF_PAGE_HDR_SIZE: 16, size: 4194304, nr_pages: 1029

Signed-off-by: Frank A. Cancio Bello <[email protected]>
---
kernel/trace/ring_buffer.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 66358d66c933..c10b6bcb29b9 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -1730,6 +1730,8 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
return size;

nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
+ printk(KERN_ALERT "PAGE_SIZE: %lu, BUF_PAGE_HDR_SIZE: %lu, size: %lu, nr_pages: %ld",
+ PAGE_SIZE, BUF_PAGE_HDR_SIZE, size, nr_pages);

/* we need a minimum of two pages */
if (nr_pages < 2)
--
2.17.1