Skip to content

Commit 2f84b39

Browse files
committed
tracing: Update subbuffer with kilobytes not page order
Using page order for deciding what the size of the ring buffer sub buffers are is exposing a bit too much of the implementation. Although the sub buffers are only allocated in orders of pages, allow the user to specify the minimum size of each sub-buffer via kilobytes like they can with the buffer size itself. If the user specifies 3 via: echo 3 > buffer_subbuf_size_kb Then the sub-buffer size will round up to 4kb (on a 4kb page size system). If they specify: echo 6 > buffer_subbuf_size_kb The sub-buffer size will become 8kb. and so on. Link: https://lore.kernel.org/linux-trace-kernel/20231219185631.809766769@goodmis.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Tzvetomir Stoyanov <tz.stoyanov@gmail.com> Cc: Vincent Donnefort <vdonnefort@google.com> Cc: Kent Overstreet <kent.overstreet@gmail.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 1acce70 commit 2f84b39

3 files changed

Lines changed: 54 additions & 48 deletions

File tree

Documentation/trace/ftrace.rst

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,26 @@ of ftrace. Here is a list of some of the key files:
203203

204204
This displays the total combined size of all the trace buffers.
205205

206-
buffer_subbuf_order:
207-
208-
This sets or displays the sub buffer page size order. The ring buffer
209-
is broken up into several same size "sub buffers". An event can not be
210-
bigger than the size of the sub buffer. Normally, the sub buffer is
211-
the size of the architecture's page (4K on x86). The sub buffer also
212-
contains meta data at the start which also limits the size of an event.
213-
That means when the sub buffer is a page size, no event can be larger
214-
than the page size minus the sub buffer meta data.
215-
216-
The buffer_subbuf_order allows the user to change the size of the sub
217-
buffer. As the sub buffer is a set of pages by the power of 2, thus
218-
the sub buffer total size is defined by the order:
219-
220-
order size
221-
---- ----
222-
0 PAGE_SIZE
223-
1 PAGE_SIZE * 2
224-
2 PAGE_SIZE * 4
225-
3 PAGE_SIZE * 8
226-
227-
Changing the order will change the sub buffer size allowing for events
228-
to be larger than the page size.
229-
230-
Note: When changing the order, tracing is stopped and any data in the
231-
ring buffer and the snapshot buffer will be discarded.
206+
buffer_subbuf_size_kb:
207+
208+
This sets or displays the sub buffer size. The ring buffer is broken up
209+
into several same size "sub buffers". An event can not be bigger than
210+
the size of the sub buffer. Normally, the sub buffer is the size of the
211+
architecture's page (4K on x86). The sub buffer also contains meta data
212+
at the start which also limits the size of an event. That means when
213+
the sub buffer is a page size, no event can be larger than the page
214+
size minus the sub buffer meta data.
215+
216+
Note, the buffer_subbuf_size_kb is a way for the user to specify the
217+
minimum size of the subbuffer. The kernel may make it bigger due to the
218+
implementation details, or simply fail the operation if the kernel can
219+
not handle the request.
220+
221+
Changing the sub buffer size allows for events to be larger than the
222+
page size.
223+
224+
Note: When changing the sub-buffer size, tracing is stopped and any
225+
data in the ring buffer and the snapshot buffer will be discarded.
232226

233227
free_buffer:
234228

kernel/trace/trace.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9384,42 +9384,54 @@ static const struct file_operations buffer_percent_fops = {
93849384
};
93859385

93869386
static ssize_t
9387-
buffer_order_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
9387+
buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
93889388
{
93899389
struct trace_array *tr = filp->private_data;
9390+
size_t size;
93909391
char buf[64];
9392+
int order;
93919393
int r;
93929394

9393-
r = sprintf(buf, "%d\n", ring_buffer_subbuf_order_get(tr->array_buffer.buffer));
9395+
order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9396+
size = (PAGE_SIZE << order) / 1024;
9397+
9398+
r = sprintf(buf, "%zd\n", size);
93949399

93959400
return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
93969401
}
93979402

93989403
static ssize_t
9399-
buffer_order_write(struct file *filp, const char __user *ubuf,
9400-
size_t cnt, loff_t *ppos)
9404+
buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
9405+
size_t cnt, loff_t *ppos)
94019406
{
94029407
struct trace_array *tr = filp->private_data;
94039408
unsigned long val;
94049409
int old_order;
9410+
int order;
9411+
int pages;
94059412
int ret;
94069413

94079414
ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
94089415
if (ret)
94099416
return ret;
94109417

9418+
val *= 1024; /* value passed in is in KB */
9419+
9420+
pages = DIV_ROUND_UP(val, PAGE_SIZE);
9421+
order = fls(pages - 1);
9422+
94119423
/* limit between 1 and 128 system pages */
9412-
if (val < 0 || val > 7)
9424+
if (order < 0 || order > 7)
94139425
return -EINVAL;
94149426

94159427
/* Do not allow tracing while changing the order of the ring buffer */
94169428
tracing_stop_tr(tr);
94179429

94189430
old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9419-
if (old_order == val)
9431+
if (old_order == order)
94209432
goto out;
94219433

9422-
ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, val);
9434+
ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order);
94239435
if (ret)
94249436
goto out;
94259437

@@ -9428,7 +9440,7 @@ buffer_order_write(struct file *filp, const char __user *ubuf,
94289440
if (!tr->allocated_snapshot)
94299441
goto out_max;
94309442

9431-
ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, val);
9443+
ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
94329444
if (ret) {
94339445
/* Put back the old order */
94349446
cnt = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, old_order);
@@ -9460,10 +9472,10 @@ buffer_order_write(struct file *filp, const char __user *ubuf,
94609472
return cnt;
94619473
}
94629474

9463-
static const struct file_operations buffer_order_fops = {
9475+
static const struct file_operations buffer_subbuf_size_fops = {
94649476
.open = tracing_open_generic_tr,
9465-
.read = buffer_order_read,
9466-
.write = buffer_order_write,
9477+
.read = buffer_subbuf_size_read,
9478+
.write = buffer_subbuf_size_write,
94679479
.release = tracing_release_generic_tr,
94689480
.llseek = default_llseek,
94699481
};
@@ -9934,8 +9946,8 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
99349946
trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
99359947
tr, &buffer_percent_fops);
99369948

9937-
trace_create_file("buffer_subbuf_order", TRACE_MODE_WRITE, d_tracer,
9938-
tr, &buffer_order_fops);
9949+
trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer,
9950+
tr, &buffer_subbuf_size_fops);
99399951

99409952
create_trace_options_dir(tr);
99419953

tools/testing/selftests/ftrace/test.d/00basic/ringbuffer_order.tc renamed to tools/testing/selftests/ftrace/test.d/00basic/ringbuffer_subbuf_size.tc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
3-
# description: Change the ringbuffer sub-buffer order
4-
# requires: buffer_subbuf_order
3+
# description: Change the ringbuffer sub-buffer size
4+
# requires: buffer_subbuf_size_kb
55
# flags: instance
66

77
get_buffer_data_size() {
@@ -52,8 +52,8 @@ write_buffer() {
5252
}
5353

5454
test_buffer() {
55-
orde=$1
56-
page_size=$((4096<<order))
55+
size_kb=$1
56+
page_size=$((size_kb*1024))
5757

5858
size=`get_buffer_data_size`
5959

@@ -82,14 +82,14 @@ test_buffer() {
8282
fi
8383
}
8484

85-
ORIG=`cat buffer_subbuf_order`
85+
ORIG=`cat buffer_subbuf_size_kb`
8686

87-
# Could test bigger orders than 3, but then creating the string
87+
# Could test bigger sizes than 32K, but then creating the string
8888
# to write into the ring buffer takes too long
89-
for a in 0 1 2 3 ; do
90-
echo $a > buffer_subbuf_order
89+
for a in 4 8 16 32 ; do
90+
echo $a > buffer_subbuf_size_kb
9191
test_buffer $a
9292
done
9393

94-
echo $ORIG > buffer_subbuf_order
94+
echo $ORIG > buffer_subbuf_size_kb
9595

0 commit comments

Comments
 (0)