Skip to content

Commit ef710e1

Browse files
kosakirostedt
authored andcommitted
tracing: Shrink max latency ringbuffer if unnecessary
Documentation/trace/ftrace.txt says buffer_size_kb: This sets or displays the number of kilobytes each CPU buffer can hold. The tracer buffers are the same size for each CPU. The displayed number is the size of the CPU buffer and not total size of all buffers. The trace buffers are allocated in pages (blocks of memory that the kernel uses for allocation, usually 4 KB in size). 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. ( Note, the size may not be a multiple of the page size due to buffer management overhead. ) This can only be updated when the current_tracer is set to "nop". But it's incorrect. currently total memory consumption is 'buffer_size_kb x CPUs x 2'. Why two times difference is there? because ftrace implicitly allocate the buffer for max latency too. That makes sad result when admin want to use large buffer. (If admin want full logging and makes detail analysis). example, If admin have 24 CPUs machine and write 200MB to buffer_size_kb, the system consume ~10GB memory (200MB x 24 x 2). umm.. 5GB memory waste is usually unacceptable. Fortunatelly, almost all users don't use max latency feature. The max latency buffer can be disabled easily. This patch shrink buffer size of the max latency buffer if unnecessary. Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> LKML-Reference: <20100701104554.DA2D.A69D9226@jp.fujitsu.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent bc289ae commit ef710e1

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
660660
return;
661661

662662
WARN_ON_ONCE(!irqs_disabled());
663+
if (!current_trace->use_max_tr) {
664+
WARN_ON_ONCE(1);
665+
return;
666+
}
663667
arch_spin_lock(&ftrace_max_lock);
664668

665669
tr->buffer = max_tr.buffer;
@@ -686,6 +690,11 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
686690
return;
687691

688692
WARN_ON_ONCE(!irqs_disabled());
693+
if (!current_trace->use_max_tr) {
694+
WARN_ON_ONCE(1);
695+
return;
696+
}
697+
689698
arch_spin_lock(&ftrace_max_lock);
690699

691700
ftrace_disable_cpu();
@@ -2801,6 +2810,9 @@ static int tracing_resize_ring_buffer(unsigned long size)
28012810
if (ret < 0)
28022811
return ret;
28032812

2813+
if (!current_trace->use_max_tr)
2814+
goto out;
2815+
28042816
ret = ring_buffer_resize(max_tr.buffer, size);
28052817
if (ret < 0) {
28062818
int r;
@@ -2828,11 +2840,14 @@ static int tracing_resize_ring_buffer(unsigned long size)
28282840
return ret;
28292841
}
28302842

2843+
max_tr.entries = size;
2844+
out:
28312845
global_trace.entries = size;
28322846

28332847
return ret;
28342848
}
28352849

2850+
28362851
/**
28372852
* tracing_update_buffers - used by tracing facility to expand ring buffers
28382853
*
@@ -2893,12 +2908,26 @@ static int tracing_set_tracer(const char *buf)
28932908
trace_branch_disable();
28942909
if (current_trace && current_trace->reset)
28952910
current_trace->reset(tr);
2896-
2911+
if (current_trace && current_trace->use_max_tr) {
2912+
/*
2913+
* We don't free the ring buffer. instead, resize it because
2914+
* The max_tr ring buffer has some state (e.g. ring->clock) and
2915+
* we want preserve it.
2916+
*/
2917+
ring_buffer_resize(max_tr.buffer, 1);
2918+
max_tr.entries = 1;
2919+
}
28972920
destroy_trace_option_files(topts);
28982921

28992922
current_trace = t;
29002923

29012924
topts = create_trace_option_files(current_trace);
2925+
if (current_trace->use_max_tr) {
2926+
ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2927+
if (ret < 0)
2928+
goto out;
2929+
max_tr.entries = global_trace.entries;
2930+
}
29022931

29032932
if (t->init) {
29042933
ret = tracer_init(t, tr);
@@ -3480,7 +3509,6 @@ tracing_entries_write(struct file *filp, const char __user *ubuf,
34803509
}
34813510

34823511
tracing_start();
3483-
max_tr.entries = global_trace.entries;
34843512
mutex_unlock(&trace_types_lock);
34853513

34863514
return cnt;
@@ -4578,16 +4606,14 @@ __init static int tracer_alloc_buffers(void)
45784606

45794607

45804608
#ifdef CONFIG_TRACER_MAX_TRACE
4581-
max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4582-
TRACE_BUFFER_FLAGS);
4609+
max_tr.buffer = ring_buffer_alloc(1, TRACE_BUFFER_FLAGS);
45834610
if (!max_tr.buffer) {
45844611
printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
45854612
WARN_ON(1);
45864613
ring_buffer_free(global_trace.buffer);
45874614
goto out_free_cpumask;
45884615
}
4589-
max_tr.entries = ring_buffer_size(max_tr.buffer);
4590-
WARN_ON(max_tr.entries != global_trace.entries);
4616+
max_tr.entries = 1;
45914617
#endif
45924618

45934619
/* Allocate the first page for all buffers */

‎kernel/trace/trace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ struct tracer {
276276
struct tracer *next;
277277
int print_max;
278278
struct tracer_flags *flags;
279+
int use_max_tr;
279280
};
280281

281282

‎kernel/trace/trace_irqsoff.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ static struct tracer irqsoff_tracer __read_mostly =
649649
#endif
650650
.open = irqsoff_trace_open,
651651
.close = irqsoff_trace_close,
652+
.use_max_tr = 1,
652653
};
653654
# define register_irqsoff(trace) register_tracer(&trace)
654655
#else
@@ -681,6 +682,7 @@ static struct tracer preemptoff_tracer __read_mostly =
681682
#endif
682683
.open = irqsoff_trace_open,
683684
.close = irqsoff_trace_close,
685+
.use_max_tr = 1,
684686
};
685687
# define register_preemptoff(trace) register_tracer(&trace)
686688
#else
@@ -715,6 +717,7 @@ static struct tracer preemptirqsoff_tracer __read_mostly =
715717
#endif
716718
.open = irqsoff_trace_open,
717719
.close = irqsoff_trace_close,
720+
.use_max_tr = 1,
718721
};
719722

720723
# define register_preemptirqsoff(trace) register_tracer(&trace)

‎kernel/trace/trace_sched_wakeup.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ static struct tracer wakeup_tracer __read_mostly =
382382
#ifdef CONFIG_FTRACE_SELFTEST
383383
.selftest = trace_selftest_startup_wakeup,
384384
#endif
385+
.use_max_tr = 1,
385386
};
386387

387388
static struct tracer wakeup_rt_tracer __read_mostly =
@@ -396,6 +397,7 @@ static struct tracer wakeup_rt_tracer __read_mostly =
396397
#ifdef CONFIG_FTRACE_SELFTEST
397398
.selftest = trace_selftest_startup_wakeup,
398399
#endif
400+
.use_max_tr = 1,
399401
};
400402

401403
__init static int init_wakeup_tracer(void)

0 commit comments

Comments
 (0)