| @@ -318,6 +318,9 @@ EXPORT_SYMBOL_GPL(ring_buffer_event_data); · 317 unmodified lines |
| 318 | 318 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 319 | 319 | #define TS_DELTA_TEST (~TS_MASK) |
| 320 | 320 | |
| 321 | +/* Flag when events were overwritten */ |
| 322 | +#define RB_MISSED_EVENTS (1 << 31) |
| 323 | + |
| 321 | 324 | struct buffer_data_page { |
| 322 | 325 | u64 time_stamp; /* page time stamp */ |
| 323 | 326 | local_t commit; /* write committed index */ |
| @@ -416,6 +419,12 @@ int ring_buffer_print_page_header(struct trace_seq *s) · 92 unmodified lines |
| 416 | 419 | (unsigned int)sizeof(field.commit), |
| 417 | 420 | (unsigned int)is_signed_type(long)); |
| 418 | 421 | |
| 422 | + ret = trace_seq_printf(s, "\tfield: int overwrite;\t" |
| 423 | + "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 424 | + (unsigned int)offsetof(typeof(field), commit), |
| 425 | + 1, |
| 426 | + (unsigned int)is_signed_type(long)); |
| 427 | + |
| 419 | 428 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
| 420 | 429 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 421 | 430 | (unsigned int)offsetof(typeof(field), data), |
| @@ -439,6 +448,8 @@ struct ring_buffer_per_cpu { · 17 unmodified lines |
| 439 | 448 | struct buffer_page *tail_page; /* write to tail */ |
| 440 | 449 | struct buffer_page *commit_page; /* committed pages */ |
| 441 | 450 | struct buffer_page *reader_page; |
| 451 | + unsigned long lost_events; |
| 452 | + unsigned long last_overrun; |
| 442 | 453 | local_t commit_overrun; |
| 443 | 454 | local_t overrun; |
| 444 | 455 | local_t entries; |
| @@ -2835,6 +2846,7 @@ static struct buffer_page * · 2390 unmodified lines |
| 2835 | 2846 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
| 2836 | 2847 | { |
| 2837 | 2848 | struct buffer_page *reader = NULL; |
| 2849 | + unsigned long overwrite; |
| 2838 | 2850 | unsigned long flags; |
| 2839 | 2851 | int nr_loops = 0; |
| 2840 | 2852 | int ret; |
| @@ -2895,6 +2907,18 @@ rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) · 54 unmodified lines |
| 2895 | 2907 | /* The reader page will be pointing to the new head */ |
| 2896 | 2908 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); |
| 2897 | 2909 | |
| 2910 | + /* |
| 2911 | + * We want to make sure we read the overruns after we set up our |
| 2912 | + * pointers to the next object. The writer side does a |
| 2913 | + * cmpxchg to cross pages which acts as the mb on the writer |
| 2914 | + * side. Note, the reader will constantly fail the swap |
| 2915 | + * while the writer is updating the pointers, so this |
| 2916 | + * guarantees that the overwrite recorded here is the one we |
| 2917 | + * want to compare with the last_overrun. |
| 2918 | + */ |
| 2919 | + smp_mb(); |
| 2920 | + overwrite = local_read(&(cpu_buffer->overrun)); |
| 2921 | + |
| 2898 | 2922 | /* |
| 2899 | 2923 | * Here's the tricky part. |
| 2900 | 2924 | * |
| @@ -2926,6 +2950,11 @@ rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) · 25 unmodified lines |
| 2926 | 2950 | cpu_buffer->reader_page = reader; |
| 2927 | 2951 | rb_reset_reader_page(cpu_buffer); |
| 2928 | 2952 | |
| 2953 | + if (overwrite != cpu_buffer->last_overrun) { |
| 2954 | + cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; |
| 2955 | + cpu_buffer->last_overrun = overwrite; |
| 2956 | + } |
| 2957 | + |
| 2929 | 2958 | goto again; |
| 2930 | 2959 | |
| 2931 | 2960 | out: |
| @@ -3002,8 +3031,14 @@ static void rb_advance_iter(struct ring_buffer_iter *iter) · 70 unmodified lines |
| 3002 | 3031 | rb_advance_iter(iter); |
| 3003 | 3032 | } |
| 3004 | 3033 | |
| 3034 | +static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
| 3035 | +{ |
| 3036 | + return cpu_buffer->lost_events; |
| 3037 | +} |
| 3038 | + |
| 3005 | 3039 | static struct ring_buffer_event * |
| 3006 | | −rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts) |
| 3040 | +rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
| 3041 | + unsigned long *lost_events) |
| 3007 | 3042 | { |
| 3008 | 3043 | struct ring_buffer_event *event; |
| 3009 | 3044 | struct buffer_page *reader; |
| @@ -3055,6 +3090,8 @@ rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts) · 45 unmodified lines |
| 3055 | 3090 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
| 3056 | 3091 | cpu_buffer->cpu, ts); |
| 3057 | 3092 | } |
| 3093 | + if (lost_events) |
| 3094 | + *lost_events = rb_lost_events(cpu_buffer); |
| 3058 | 3095 | return event; |
| 3059 | 3096 | |
| 3060 | 3097 | default: |
| @@ -3165,12 +3202,14 @@ static inline int rb_ok_to_lock(void) · 104 unmodified lines |
| 3165 | 3202 | * @buffer: The ring buffer to read |
| 3166 | 3203 | * @cpu: The cpu to peak at |
| 3167 | 3204 | * @ts: The timestamp counter of this event. |
| 3205 | + * @lost_events: a variable to store if events were lost (may be NULL) |
| 3168 | 3206 | * |
| 3169 | 3207 | * This will return the event that will be read next, but does |
| 3170 | 3208 | * not consume the data. |
| 3171 | 3209 | */ |
| 3172 | 3210 | struct ring_buffer_event * |
| 3173 | | −ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 3211 | +ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 3212 | + unsigned long *lost_events) |
| 3174 | 3213 | { |
| 3175 | 3214 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 3176 | 3215 | struct ring_buffer_event *event; |
| @@ -3185,7 +3224,7 @@ ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) · 8 unmodified lines |
| 3185 | 3224 | local_irq_save(flags); |
| 3186 | 3225 | if (dolock) |
| 3187 | 3226 | spin_lock(&cpu_buffer->reader_lock); |
| 3188 | | − event = rb_buffer_peek(cpu_buffer, ts); |
| 3227 | + event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
| 3189 | 3228 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
| 3190 | 3229 | rb_advance_reader(cpu_buffer); |
| 3191 | 3230 | if (dolock) |
| @@ -3227,13 +3266,17 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) · 35 unmodified lines |
| 3227 | 3266 | /** |
| 3228 | 3267 | * ring_buffer_consume - return an event and consume it |
| 3229 | 3268 | * @buffer: The ring buffer to get the next event from |
| 3269 | + * @cpu: the cpu to read the buffer from |
| 3270 | + * @ts: a variable to store the timestamp (may be NULL) |
| 3271 | + * @lost_events: a variable to store if events were lost (may be NULL) |
| 3230 | 3272 | * |
| 3231 | 3273 | * Returns the next event in the ring buffer, and that event is consumed. |
| 3232 | 3274 | * Meaning, that sequential reads will keep returning a different event, |
| 3233 | 3275 | * and eventually empty the ring buffer if the producer is slower. |
| 3234 | 3276 | */ |
| 3235 | 3277 | struct ring_buffer_event * |
| 3236 | | −ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 3278 | +ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 3279 | + unsigned long *lost_events) |
| 3237 | 3280 | { |
| 3238 | 3281 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3239 | 3282 | struct ring_buffer_event *event = NULL; |
| @@ -3254,9 +3297,11 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts) · 14 unmodified lines |
| 3254 | 3297 | if (dolock) |
| 3255 | 3298 | spin_lock(&cpu_buffer->reader_lock); |
| 3256 | 3299 | |
| 3257 | | − event = rb_buffer_peek(cpu_buffer, ts); |
| 3258 | | − if (event) |
| 3300 | + event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
| 3301 | + if (event) { |
| 3302 | + cpu_buffer->lost_events = 0; |
| 3259 | 3303 | rb_advance_reader(cpu_buffer); |
| 3304 | + } |
| 3260 | 3305 | |
| 3261 | 3306 | if (dolock) |
| 3262 | 3307 | spin_unlock(&cpu_buffer->reader_lock); |
| @@ -3405,6 +3450,9 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) · 142 unmodified lines |
| 3405 | 3450 | cpu_buffer->write_stamp = 0; |
| 3406 | 3451 | cpu_buffer->read_stamp = 0; |
| 3407 | 3452 | |
| 3453 | + cpu_buffer->lost_events = 0; |
| 3454 | + cpu_buffer->last_overrun = 0; |
| 3455 | + |
| 3408 | 3456 | rb_head_page_activate(cpu_buffer); |
| 3409 | 3457 | } |
| 3410 | 3458 | |
| @@ -3684,6 +3732,7 @@ int ring_buffer_read_page(struct ring_buffer *buffer, · 273 unmodified lines |
| 3684 | 3732 | unsigned int commit; |
| 3685 | 3733 | unsigned int read; |
| 3686 | 3734 | u64 save_timestamp; |
| 3735 | + int missed_events = 0; |
| 3687 | 3736 | int ret = -1; |
| 3688 | 3737 | |
| 3689 | 3738 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| @@ -3716,6 +3765,10 @@ int ring_buffer_read_page(struct ring_buffer *buffer, · 26 unmodified lines |
| 3716 | 3765 | read = reader->read; |
| 3717 | 3766 | commit = rb_page_commit(reader); |
| 3718 | 3767 | |
| 3768 | + /* Check if any events were dropped */ |
| 3769 | + if (cpu_buffer->lost_events) |
| 3770 | + missed_events = 1; |
| 3771 | + |
| 3719 | 3772 | /* |
| 3720 | 3773 | * If this page has been partially read or |
| 3721 | 3774 | * if len is not big enough to read the rest of the page or |
| @@ -3779,6 +3832,13 @@ int ring_buffer_read_page(struct ring_buffer *buffer, · 57 unmodified lines |
| 3779 | 3832 | } |
| 3780 | 3833 | ret = read; |
| 3781 | 3834 | |
| 3835 | + cpu_buffer->lost_events = 0; |
| 3836 | + /* |
| 3837 | + * Set a flag in the commit field if we lost events |
| 3838 | + */ |
| 3839 | + if (missed_events) |
| 3840 | + local_add(RB_MISSED_EVENTS, &bpage->commit); |
| 3841 | + |
| 3782 | 3842 | out_unlock: |
| 3783 | 3843 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 3784 | 3844 | |