Skip to content

Commit 58fbc3c

Browse files
committed
ring-buffer: Consolidate add_timestamp to remove some branches
Reorganize a little the logic to handle adding the absolute time stamp, extended and forced time stamps, in such a way to remove a branch or two. This is just a micro optimization. Also add before and after time stamps to the rb_event_info structure to display those values in the rb_check_timestamps() code, if something were to go wrong. Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 75b21c6 commit 58fbc3c

1 file changed

Lines changed: 73 additions & 66 deletions

File tree

‎kernel/trace/ring_buffer.c‎

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ struct rb_irq_work {
416416
struct rb_event_info {
417417
u64 ts;
418418
u64 delta;
419+
u64 before;
420+
u64 after;
419421
unsigned long length;
420422
struct buffer_page *tail_page;
421423
int add_timestamp;
@@ -2619,6 +2621,33 @@ rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
26192621
static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
26202622
struct ring_buffer_event *event);
26212623

2624+
#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2625+
static inline bool sched_clock_stable(void)
2626+
{
2627+
return true;
2628+
}
2629+
#endif
2630+
2631+
static noinline void
2632+
rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2633+
struct rb_event_info *info)
2634+
{
2635+
u64 write_stamp;
2636+
2637+
WARN_ONCE(info->delta > (1ULL << 59),
2638+
KERN_WARNING "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
2639+
(unsigned long long)info->delta,
2640+
(unsigned long long)info->ts,
2641+
(unsigned long long)info->before,
2642+
(unsigned long long)info->after,
2643+
(unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2644+
sched_clock_stable() ? "" :
2645+
"If you just came from a suspend/resume,\n"
2646+
"please switch to the trace global clock:\n"
2647+
" echo global > /sys/kernel/debug/tracing/trace_clock\n"
2648+
"or add trace_clock=global to the kernel command line\n");
2649+
}
2650+
26222651
/**
26232652
* rb_update_event - update event type and data
26242653
* @cpu_buffer: The per cpu buffer of the @event
@@ -2646,6 +2675,7 @@ rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
26462675
bool abs = info->add_timestamp &
26472676
(RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
26482677

2678+
rb_check_timestamp(cpu_buffer, info);
26492679
event = rb_add_time_stamp(event, abs ? info->delta : delta, abs);
26502680
length -= RB_LEN_TIME_EXTEND;
26512681
delta = 0;
@@ -2692,13 +2722,6 @@ static unsigned rb_calculate_event_length(unsigned length)
26922722
return length;
26932723
}
26942724

2695-
#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2696-
static inline bool sched_clock_stable(void)
2697-
{
2698-
return true;
2699-
}
2700-
#endif
2701-
27022725
static __always_inline bool
27032726
rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
27042727
struct ring_buffer_event *event)
@@ -3092,32 +3115,13 @@ int ring_buffer_unlock_commit(struct trace_buffer *buffer,
30923115
}
30933116
EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
30943117

3095-
static noinline void
3096-
rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
3097-
struct rb_event_info *info)
3098-
{
3099-
u64 write_stamp;
3100-
3101-
WARN_ONCE(info->delta > (1ULL << 59),
3102-
KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
3103-
(unsigned long long)info->delta,
3104-
(unsigned long long)info->ts,
3105-
(unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
3106-
sched_clock_stable() ? "" :
3107-
"If you just came from a suspend/resume,\n"
3108-
"please switch to the trace global clock:\n"
3109-
" echo global > /sys/kernel/debug/tracing/trace_clock\n"
3110-
"or add trace_clock=global to the kernel command line\n");
3111-
}
3112-
31133118
static struct ring_buffer_event *
31143119
__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
31153120
struct rb_event_info *info)
31163121
{
31173122
struct ring_buffer_event *event;
31183123
struct buffer_page *tail_page;
31193124
unsigned long tail, write, w;
3120-
u64 before, after;
31213125
bool a_ok;
31223126
bool b_ok;
31233127

@@ -3126,40 +3130,31 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
31263130

31273131
/*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
31283132
barrier();
3129-
b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3130-
a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3133+
b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3134+
a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
31313135
barrier();
31323136
info->ts = rb_time_stamp(cpu_buffer->buffer);
31333137

3134-
if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3138+
if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
31353139
info->delta = info->ts;
3136-
info->add_timestamp = RB_ADD_STAMP_ABSOLUTE;
31373140
} else {
3138-
info->delta = info->ts - after;
3139-
}
3140-
3141-
if (likely(a_ok && b_ok)) {
3142-
if (unlikely(test_time_stamp(info->delta))) {
3143-
rb_check_timestamp(cpu_buffer, info);
3144-
info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3141+
/*
3142+
* If interrupting an event time update, we may need an
3143+
* absolute timestamp.
3144+
* Don't bother if this is the start of a new page (w == 0).
3145+
*/
3146+
if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3147+
info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3148+
info->length += RB_LEN_TIME_EXTEND;
3149+
} else {
3150+
info->delta = info->ts - info->after;
3151+
if (unlikely(test_time_stamp(info->delta))) {
3152+
info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3153+
info->length += RB_LEN_TIME_EXTEND;
3154+
}
31453155
}
31463156
}
31473157

3148-
/*
3149-
* If interrupting an event time update, we may need an absolute timestamp.
3150-
* Don't bother if this is the start of a new page (w == 0).
3151-
*/
3152-
if (unlikely(!a_ok || !b_ok || (before != after && w)))
3153-
info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3154-
3155-
/*
3156-
* If the time delta since the last event is too big to
3157-
* hold in the time field of the event, then we append a
3158-
* TIME EXTEND event ahead of the data event.
3159-
*/
3160-
if (unlikely(info->add_timestamp))
3161-
info->length += RB_LEN_TIME_EXTEND;
3162-
31633158
/*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
31643159

31653160
/*C*/ write = local_add_return(info->length, &tail_page->write);
@@ -3173,10 +3168,11 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
31733168
if (unlikely(write > BUF_PAGE_SIZE)) {
31743169
if (tail != w) {
31753170
/* before and after may now different, fix it up*/
3176-
b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3177-
a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3178-
if (a_ok && b_ok && before != after)
3179-
(void)rb_time_cmpxchg(&cpu_buffer->before_stamp, before, after);
3171+
b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3172+
a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3173+
if (a_ok && b_ok && info->before != info->after)
3174+
(void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3175+
info->before, info->after);
31803176
}
31813177
return rb_move_tail(cpu_buffer, tail, info);
31823178
}
@@ -3193,39 +3189,41 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
31933189
if (likely(!(info->add_timestamp &
31943190
(RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
31953191
/* This did not interrupt any time update */
3196-
info->delta = info->ts - after;
3192+
info->delta = info->ts - info->after;
31973193
else
31983194
/* Just use full timestamp for inerrupting event */
31993195
info->delta = info->ts;
32003196
barrier();
32013197
if (unlikely(info->ts != save_before)) {
32023198
/* SLOW PATH - Interrupted between C and E */
32033199

3204-
a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3200+
a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
32053201
RB_WARN_ON(cpu_buffer, !a_ok);
32063202

32073203
/* Write stamp must only go forward */
3208-
if (save_before > after) {
3204+
if (save_before > info->after) {
32093205
/*
32103206
* We do not care about the result, only that
32113207
* it gets updated atomically.
32123208
*/
3213-
(void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, save_before);
3209+
(void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3210+
info->after, save_before);
32143211
}
32153212
}
32163213
} else {
32173214
u64 ts;
32183215
/* SLOW PATH - Interrupted between A and C */
3219-
a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3216+
a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
32203217
/* Was interrupted before here, write_stamp must be valid */
32213218
RB_WARN_ON(cpu_buffer, !a_ok);
32223219
ts = rb_time_stamp(cpu_buffer->buffer);
32233220
barrier();
32243221
/*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3225-
after < ts) {
3222+
info->after < ts) {
32263223
/* Nothing came after this event between C and E */
3227-
info->delta = ts - after;
3228-
(void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, info->ts);
3224+
info->delta = ts - info->after;
3225+
(void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3226+
info->after, info->ts);
32293227
info->ts = ts;
32303228
} else {
32313229
/*
@@ -3277,6 +3275,7 @@ rb_reserve_next_event(struct trace_buffer *buffer,
32773275
struct ring_buffer_event *event;
32783276
struct rb_event_info info;
32793277
int nr_loops = 0;
3278+
int add_ts_default;
32803279

32813280
rb_start_commit(cpu_buffer);
32823281
/* The commit page can not change after this */
@@ -3297,8 +3296,16 @@ rb_reserve_next_event(struct trace_buffer *buffer,
32973296
#endif
32983297

32993298
info.length = rb_calculate_event_length(length);
3299+
3300+
if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3301+
add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3302+
info.length += RB_LEN_TIME_EXTEND;
3303+
} else {
3304+
add_ts_default = RB_ADD_STAMP_NONE;
3305+
}
3306+
33003307
again:
3301-
info.add_timestamp = RB_ADD_STAMP_NONE;
3308+
info.add_timestamp = add_ts_default;
33023309
info.delta = 0;
33033310

33043311
/*
@@ -3316,7 +3323,7 @@ rb_reserve_next_event(struct trace_buffer *buffer,
33163323
event = __rb_reserve_next(cpu_buffer, &info);
33173324

33183325
if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3319-
if (info.add_timestamp)
3326+
if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
33203327
info.length -= RB_LEN_TIME_EXTEND;
33213328
goto again;
33223329
}

0 commit comments

Comments
 (0)