1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "kbuffer.h"
26
27#define MISSING_EVENTS (1UL << 31)
28#define MISSING_STORED (1UL << 30)
29
30#define COMMIT_MASK ((1 << 27) - 1)
31
32enum {
33 KBUFFER_FL_HOST_BIG_ENDIAN = (1<<0),
34 KBUFFER_FL_BIG_ENDIAN = (1<<1),
35 KBUFFER_FL_LONG_8 = (1<<2),
36 KBUFFER_FL_OLD_FORMAT = (1<<3),
37};
38
39#define ENDIAN_MASK (KBUFFER_FL_HOST_BIG_ENDIAN | KBUFFER_FL_BIG_ENDIAN)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58struct kbuffer {
59 unsigned long long timestamp;
60 long long lost_events;
61 unsigned long flags;
62 void *subbuffer;
63 void *data;
64 unsigned int index;
65 unsigned int curr;
66 unsigned int next;
67 unsigned int size;
68 unsigned int start;
69
70 unsigned int (*read_4)(void *ptr);
71 unsigned long long (*read_8)(void *ptr);
72 unsigned long long (*read_long)(struct kbuffer *kbuf, void *ptr);
73 int (*next_event)(struct kbuffer *kbuf);
74};
75
76static void *zmalloc(size_t size)
77{
78 return calloc(1, size);
79}
80
81static int host_is_bigendian(void)
82{
83 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 };
84 unsigned int *ptr;
85
86 ptr = (unsigned int *)str;
87 return *ptr == 0x01020304;
88}
89
90static int do_swap(struct kbuffer *kbuf)
91{
92 return ((kbuf->flags & KBUFFER_FL_HOST_BIG_ENDIAN) + kbuf->flags) &
93 ENDIAN_MASK;
94}
95
96static unsigned long long __read_8(void *ptr)
97{
98 unsigned long long data = *(unsigned long long *)ptr;
99
100 return data;
101}
102
103static unsigned long long __read_8_sw(void *ptr)
104{
105 unsigned long long data = *(unsigned long long *)ptr;
106 unsigned long long swap;
107
108 swap = ((data & 0xffULL) << 56) |
109 ((data & (0xffULL << 8)) << 40) |
110 ((data & (0xffULL << 16)) << 24) |
111 ((data & (0xffULL << 24)) << 8) |
112 ((data & (0xffULL << 32)) >> 8) |
113 ((data & (0xffULL << 40)) >> 24) |
114 ((data & (0xffULL << 48)) >> 40) |
115 ((data & (0xffULL << 56)) >> 56);
116
117 return swap;
118}
119
120static unsigned int __read_4(void *ptr)
121{
122 unsigned int data = *(unsigned int *)ptr;
123
124 return data;
125}
126
127static unsigned int __read_4_sw(void *ptr)
128{
129 unsigned int data = *(unsigned int *)ptr;
130 unsigned int swap;
131
132 swap = ((data & 0xffULL) << 24) |
133 ((data & (0xffULL << 8)) << 8) |
134 ((data & (0xffULL << 16)) >> 8) |
135 ((data & (0xffULL << 24)) >> 24);
136
137 return swap;
138}
139
140static unsigned long long read_8(struct kbuffer *kbuf, void *ptr)
141{
142 return kbuf->read_8(ptr);
143}
144
145static unsigned int read_4(struct kbuffer *kbuf, void *ptr)
146{
147 return kbuf->read_4(ptr);
148}
149
150static unsigned long long __read_long_8(struct kbuffer *kbuf, void *ptr)
151{
152 return kbuf->read_8(ptr);
153}
154
155static unsigned long long __read_long_4(struct kbuffer *kbuf, void *ptr)
156{
157 return kbuf->read_4(ptr);
158}
159
160static unsigned long long read_long(struct kbuffer *kbuf, void *ptr)
161{
162 return kbuf->read_long(kbuf, ptr);
163}
164
165static int calc_index(struct kbuffer *kbuf, void *ptr)
166{
167 return (unsigned long)ptr - (unsigned long)kbuf->data;
168}
169
170static int __next_event(struct kbuffer *kbuf);
171
172
173
174
175
176
177
178
179struct kbuffer *
180kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
181{
182 struct kbuffer *kbuf;
183 int flags = 0;
184
185 switch (size) {
186 case KBUFFER_LSIZE_4:
187 break;
188 case KBUFFER_LSIZE_8:
189 flags |= KBUFFER_FL_LONG_8;
190 break;
191 default:
192 return NULL;
193 }
194
195 switch (endian) {
196 case KBUFFER_ENDIAN_LITTLE:
197 break;
198 case KBUFFER_ENDIAN_BIG:
199 flags |= KBUFFER_FL_BIG_ENDIAN;
200 break;
201 default:
202 return NULL;
203 }
204
205 kbuf = zmalloc(sizeof(*kbuf));
206 if (!kbuf)
207 return NULL;
208
209 kbuf->flags = flags;
210
211 if (host_is_bigendian())
212 kbuf->flags |= KBUFFER_FL_HOST_BIG_ENDIAN;
213
214 if (do_swap(kbuf)) {
215 kbuf->read_8 = __read_8_sw;
216 kbuf->read_4 = __read_4_sw;
217 } else {
218 kbuf->read_8 = __read_8;
219 kbuf->read_4 = __read_4;
220 }
221
222 if (kbuf->flags & KBUFFER_FL_LONG_8)
223 kbuf->read_long = __read_long_8;
224 else
225 kbuf->read_long = __read_long_4;
226
227
228 kbuf->next_event = __next_event;
229
230 return kbuf;
231}
232
233
234
235
236
237
238void kbuffer_free(struct kbuffer *kbuf)
239{
240 free(kbuf);
241}
242
243static unsigned int type4host(struct kbuffer *kbuf,
244 unsigned int type_len_ts)
245{
246 if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
247 return (type_len_ts >> 29) & 3;
248 else
249 return type_len_ts & 3;
250}
251
252static unsigned int len4host(struct kbuffer *kbuf,
253 unsigned int type_len_ts)
254{
255 if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
256 return (type_len_ts >> 27) & 7;
257 else
258 return (type_len_ts >> 2) & 7;
259}
260
261static unsigned int type_len4host(struct kbuffer *kbuf,
262 unsigned int type_len_ts)
263{
264 if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
265 return (type_len_ts >> 27) & ((1 << 5) - 1);
266 else
267 return type_len_ts & ((1 << 5) - 1);
268}
269
270static unsigned int ts4host(struct kbuffer *kbuf,
271 unsigned int type_len_ts)
272{
273 if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
274 return type_len_ts & ((1 << 27) - 1);
275 else
276 return type_len_ts >> 5;
277}
278
279
280
281
282
283enum old_ring_buffer_type {
284 OLD_RINGBUF_TYPE_PADDING,
285 OLD_RINGBUF_TYPE_TIME_EXTEND,
286 OLD_RINGBUF_TYPE_TIME_STAMP,
287 OLD_RINGBUF_TYPE_DATA,
288};
289
290static unsigned int old_update_pointers(struct kbuffer *kbuf)
291{
292 unsigned long long extend;
293 unsigned int type_len_ts;
294 unsigned int type;
295 unsigned int len;
296 unsigned int delta;
297 unsigned int length;
298 void *ptr = kbuf->data + kbuf->curr;
299
300 type_len_ts = read_4(kbuf, ptr);
301 ptr += 4;
302
303 type = type4host(kbuf, type_len_ts);
304 len = len4host(kbuf, type_len_ts);
305 delta = ts4host(kbuf, type_len_ts);
306
307 switch (type) {
308 case OLD_RINGBUF_TYPE_PADDING:
309 kbuf->next = kbuf->size;
310 return 0;
311
312 case OLD_RINGBUF_TYPE_TIME_EXTEND:
313 extend = read_4(kbuf, ptr);
314 extend <<= TS_SHIFT;
315 extend += delta;
316 delta = extend;
317 ptr += 4;
318 length = 0;
319 break;
320
321 case OLD_RINGBUF_TYPE_TIME_STAMP:
322
323 kbuf->curr = kbuf->size;
324 kbuf->next = kbuf->size;
325 kbuf->index = kbuf->size;
326 return -1;
327 default:
328 if (len)
329 length = len * 4;
330 else {
331 length = read_4(kbuf, ptr);
332 length -= 4;
333 ptr += 4;
334 }
335 break;
336 }
337
338 kbuf->timestamp += delta;
339 kbuf->index = calc_index(kbuf, ptr);
340 kbuf->next = kbuf->index + length;
341
342 return type;
343}
344
345static int __old_next_event(struct kbuffer *kbuf)
346{
347 int type;
348
349 do {
350 kbuf->curr = kbuf->next;
351 if (kbuf->next >= kbuf->size)
352 return -1;
353 type = old_update_pointers(kbuf);
354 } while (type == OLD_RINGBUF_TYPE_TIME_EXTEND || type == OLD_RINGBUF_TYPE_PADDING);
355
356 return 0;
357}
358
359static unsigned int
360translate_data(struct kbuffer *kbuf, void *data, void **rptr,
361 unsigned long long *delta, int *length)
362{
363 unsigned long long extend;
364 unsigned int type_len_ts;
365 unsigned int type_len;
366
367 type_len_ts = read_4(kbuf, data);
368 data += 4;
369
370 type_len = type_len4host(kbuf, type_len_ts);
371 *delta = ts4host(kbuf, type_len_ts);
372
373 switch (type_len) {
374 case KBUFFER_TYPE_PADDING:
375 *length = read_4(kbuf, data);
376 break;
377
378 case KBUFFER_TYPE_TIME_EXTEND:
379 extend = read_4(kbuf, data);
380 data += 4;
381 extend <<= TS_SHIFT;
382 extend += *delta;
383 *delta = extend;
384 *length = 0;
385 break;
386
387 case KBUFFER_TYPE_TIME_STAMP:
388 data += 12;
389 *length = 0;
390 break;
391 case 0:
392 *length = read_4(kbuf, data) - 4;
393 *length = (*length + 3) & ~3;
394 data += 4;
395 break;
396 default:
397 *length = type_len * 4;
398 break;
399 }
400
401 *rptr = data;
402
403 return type_len;
404}
405
406static unsigned int update_pointers(struct kbuffer *kbuf)
407{
408 unsigned long long delta;
409 unsigned int type_len;
410 int length;
411 void *ptr = kbuf->data + kbuf->curr;
412
413 type_len = translate_data(kbuf, ptr, &ptr, &delta, &length);
414
415 kbuf->timestamp += delta;
416 kbuf->index = calc_index(kbuf, ptr);
417 kbuf->next = kbuf->index + length;
418
419 return type_len;
420}
421
422
423
424
425
426
427
428
429
430
431
432void *kbuffer_translate_data(int swap, void *data, unsigned int *size)
433{
434 unsigned long long delta;
435 struct kbuffer kbuf;
436 int type_len;
437 int length;
438 void *ptr;
439
440 if (swap) {
441 kbuf.read_8 = __read_8_sw;
442 kbuf.read_4 = __read_4_sw;
443 kbuf.flags = host_is_bigendian() ? 0 : KBUFFER_FL_BIG_ENDIAN;
444 } else {
445 kbuf.read_8 = __read_8;
446 kbuf.read_4 = __read_4;
447 kbuf.flags = host_is_bigendian() ? KBUFFER_FL_BIG_ENDIAN: 0;
448 }
449
450 type_len = translate_data(&kbuf, data, &ptr, &delta, &length);
451 switch (type_len) {
452 case KBUFFER_TYPE_PADDING:
453 case KBUFFER_TYPE_TIME_EXTEND:
454 case KBUFFER_TYPE_TIME_STAMP:
455 return NULL;
456 };
457
458 *size = length;
459
460 return ptr;
461}
462
463static int __next_event(struct kbuffer *kbuf)
464{
465 int type;
466
467 do {
468 kbuf->curr = kbuf->next;
469 if (kbuf->next >= kbuf->size)
470 return -1;
471 type = update_pointers(kbuf);
472 } while (type == KBUFFER_TYPE_TIME_EXTEND || type == KBUFFER_TYPE_PADDING);
473
474 return 0;
475}
476
477static int next_event(struct kbuffer *kbuf)
478{
479 return kbuf->next_event(kbuf);
480}
481
482
483
484
485
486
487
488
489
490
491
492
493
494void *kbuffer_next_event(struct kbuffer *kbuf, unsigned long long *ts)
495{
496 int ret;
497
498 if (!kbuf || !kbuf->subbuffer)
499 return NULL;
500
501 ret = next_event(kbuf);
502 if (ret < 0)
503 return NULL;
504
505 if (ts)
506 *ts = kbuf->timestamp;
507
508 return kbuf->data + kbuf->index;
509}
510
511
512
513
514
515
516
517
518
519
520
521
522int kbuffer_load_subbuffer(struct kbuffer *kbuf, void *subbuffer)
523{
524 unsigned long long flags;
525 void *ptr = subbuffer;
526
527 if (!kbuf || !subbuffer)
528 return -1;
529
530 kbuf->subbuffer = subbuffer;
531
532 kbuf->timestamp = read_8(kbuf, ptr);
533 ptr += 8;
534
535 kbuf->curr = 0;
536
537 if (kbuf->flags & KBUFFER_FL_LONG_8)
538 kbuf->start = 16;
539 else
540 kbuf->start = 12;
541
542 kbuf->data = subbuffer + kbuf->start;
543
544 flags = read_long(kbuf, ptr);
545 kbuf->size = (unsigned int)flags & COMMIT_MASK;
546
547 if (flags & MISSING_EVENTS) {
548 if (flags & MISSING_STORED) {
549 ptr = kbuf->data + kbuf->size;
550 kbuf->lost_events = read_long(kbuf, ptr);
551 } else
552 kbuf->lost_events = -1;
553 } else
554 kbuf->lost_events = 0;
555
556 kbuf->index = 0;
557 kbuf->next = 0;
558
559 next_event(kbuf);
560
561 return 0;
562}
563
564
565
566
567
568
569
570
571
572void *kbuffer_read_event(struct kbuffer *kbuf, unsigned long long *ts)
573{
574 if (!kbuf || !kbuf->subbuffer)
575 return NULL;
576
577 if (kbuf->curr >= kbuf->size)
578 return NULL;
579
580 if (ts)
581 *ts = kbuf->timestamp;
582 return kbuf->data + kbuf->index;
583}
584
585
586
587
588
589
590
591unsigned long long kbuffer_timestamp(struct kbuffer *kbuf)
592{
593 return kbuf->timestamp;
594}
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614void *kbuffer_read_at_offset(struct kbuffer *kbuf, int offset,
615 unsigned long long *ts)
616{
617 void *data;
618
619 if (offset < kbuf->start)
620 offset = 0;
621 else
622 offset -= kbuf->start;
623
624
625 kbuffer_load_subbuffer(kbuf, kbuf->subbuffer);
626 data = kbuffer_read_event(kbuf, ts);
627
628 while (kbuf->curr < offset) {
629 data = kbuffer_next_event(kbuf, ts);
630 if (!data)
631 break;
632 }
633
634 return data;
635}
636
637
638
639
640
641
642
643
644
645int kbuffer_subbuffer_size(struct kbuffer *kbuf)
646{
647 return kbuf->size;
648}
649
650
651
652
653
654
655
656
657
658
659
660int kbuffer_curr_index(struct kbuffer *kbuf)
661{
662 return kbuf->curr;
663}
664
665
666
667
668
669
670
671
672int kbuffer_curr_offset(struct kbuffer *kbuf)
673{
674 return kbuf->curr + kbuf->start;
675}
676
677
678
679
680
681
682
683
684int kbuffer_event_size(struct kbuffer *kbuf)
685{
686 return kbuf->next - kbuf->index;
687}
688
689
690
691
692
693
694
695
696int kbuffer_curr_size(struct kbuffer *kbuf)
697{
698 return kbuf->next - kbuf->curr;
699}
700
701
702
703
704
705
706
707
708
709
710int kbuffer_missed_events(struct kbuffer *kbuf)
711{
712
713 if (kbuf->curr)
714 return 0;
715
716 return kbuf->lost_events;
717}
718
719
720
721
722
723
724
725
726
727
728void kbuffer_set_old_format(struct kbuffer *kbuf)
729{
730 kbuf->flags |= KBUFFER_FL_OLD_FORMAT;
731
732 kbuf->next_event = __old_next_event;
733}
734
735
736
737
738
739
740
741int kbuffer_start_of_data(struct kbuffer *kbuf)
742{
743 return kbuf->start;
744}
745