-
Notifications
You must be signed in to change notification settings - Fork 6
/
recorder.h
1070 lines (947 loc) · 50.4 KB
/
recorder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef RECORDER_H
#define RECORDER_H
// *****************************************************************************
// recorder.h Recorder project
// *****************************************************************************
//
// File description:
//
// Record information about what's going on in the application
// (Lifted from ELFE / XL / Tao with specific improvements)
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU Lesser General Public License v2+
// (C) 2017-2020, Christophe de Dinechin <[email protected]>
// *****************************************************************************
// This file is part of Recorder
//
// Recorder is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Recorder is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Recorder, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
#include "recorder_ring.h"
#include <stdarg.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// ============================================================================
//
// Information about the API
//
// ============================================================================
#define RECORDER_CURRENT_VERSION RECORDER_VERSION(1,2,2)
#define RECORDER_VERSION(majr,minr,ptch) ((majr)<<16|((minr)<<8|(ptch)))
#define RECORDER_VERSION_MAJOR(version) (((version) >> 16) & 0xFF)
#define RECORDER_VERSION_MINOR(version) (((version) >> 8)& 0xFF)
#define RECORDER_VERSION_PATCH(version) (((version) >> 0)& 0xFF)
#define RECORDER_64BIT (INTPTR_MAX > 0x7fffffff)
// ============================================================================
//
// Recorder dump, to use in fault handlers or within a debugger
//
// ============================================================================
// Within gdb, you can use: 'p recorder_dump()' to get a dump of what
// happened in your program until this point
//
// Ownership of `const char *` strings:
// In general, when a 'const char *' argument is given, it is expected
// that its lifetime will be sufficient for the duration of all its uses.
// The application is responsible for enforcing this rule. In particular,
// recorder_traces_set(getenv("RECORDER_TRACES"));
// is considered a correct usage. Overwriting the environment
// afterwards is considered an evil and bogus mis-use of the library.
// Dump all recorder entries for all recorders, sorted between recorders
extern unsigned recorder_dump(void);
// Dump all recorder entries with a name matching regular expression 'what'
extern unsigned recorder_dump_for(const char *what);
#ifndef RECORDER_STANDALONE
// Dump recorder entries on signal
extern void recorder_dump_on_signal(int signal);
// Dump recorder entries on common signals (exact signals depend on platform)
// By default, when called with (0,0), this dumps for:
// SIGQUIT, SIGILL, SIGABRT, SIGBUS, SIGSEGV, SIGSYS,
// SIGXCPU, SIGXFSZ, SIGINFO, SIGUSR1, SIGUSR2, SIGSTKFLT, SIGPWR
// You can add add or remove signals by setting the bitmasks 'add' and 'remove'
extern void recorder_dump_on_common_signals(unsigned add, unsigned remove);
#endif // RECORDER_STANDALONE
// Configuration of the function used to dump the recorder
typedef unsigned (*recorder_show_fn) (const char *text,size_t len,void *output);
typedef void (*recorder_format_fn)(recorder_show_fn show,
void *output,
const char *label,
const char *location,
uintptr_t order,
uintptr_t timestamp,
const char *message);
typedef size_t (*recorder_type_fn)(intptr_t trace,
const char *format,
char *buffer, size_t length,
uintptr_t data);
// Configure function used to print and format entries
extern void * recorder_configure_output(void *output);
extern recorder_show_fn recorder_configure_show(recorder_show_fn show);
extern recorder_format_fn recorder_configure_format(recorder_format_fn format);
extern recorder_type_fn recorder_configure_type(uint8_t id,
recorder_type_fn type);
// Sort recorder entries with specific format and output functions
extern unsigned recorder_sort(const char *what,
recorder_format_fn format,
recorder_show_fn show, void *show_arg);
// Return the current indent for the recorder
extern unsigned recorder_indent(void);
#ifndef RECORDER_STANDALONE
// Background recorder dump thread
extern void recorder_background_dump(const char *what);
extern void recorder_background_dump_stop(void);
#endif //RECORDER_STANDALONE
// ============================================================================
//
// Recorder data structures
//
// ============================================================================
typedef struct recorder_entry
/// ---------------------------------------------------------------------------
/// Entry in the flight recorder.
///----------------------------------------------------------------------------
/// Notice that the arguments are stored as "intptr_t" because that type
/// is guaranteed to be the same size as a pointer. This allows us to
/// properly align recorder entries to powers of 2 for efficiency.
/// Also read explanations of \ref _recorder_double and \ref _recorder_float
/// below regarding how to use floating-point with the recorder.
{
const char *format; ///< Printf-style format for record + file/line
uintptr_t order; ///< Global order of events (across recorders)
uintptr_t timestamp; ///< Time at which record took place
const char *where; ///< Source code function
uintptr_t args[4]; ///< Four arguments, for a total of 8 fields
} recorder_entry;
/// A global counter indicating the order of entries across recorders.
/// this is incremented atomically for each record() call.
/// It must be exposed because all XYZ_record() implementations need to
/// touch the same shared variable in order to provide a global order.
extern uintptr_t recorder_order;
typedef struct recorder_info
///----------------------------------------------------------------------------
/// A linked list of the activated recorders
///----------------------------------------------------------------------------
// We allow up to 12 exported values because that's what RECORD supports
{
intptr_t trace; ///< Trace this recorder
const char * name; ///< Name of this parameter / recorder
const char * description;///< Description of what is recorded
struct recorder_info * next; ///< Pointer to next in list
struct recorder_chan * exported[12];///< Shared-memory ring export
recorder_ring_t ring; ///< Pointer to ring for this recorder
recorder_entry data[0]; ///< Data for this recorder
} recorder_info;
typedef struct recorder_tweak
///----------------------------------------------------------------------------
/// A linked list of recorder tweaks
///----------------------------------------------------------------------------
{
intptr_t trace; ///< Tweak for this recorder
const char * name; ///< Name of this parameter / recorder
const char * description;///< Description of what is recorded
struct recorder_tweak * next; ///< Pointer to next in list
} recorder_tweak;
// List all the activated recorders
extern recorder_info *recorder_list(void);
// ============================================================================
//
// Adding data to a recorder
//
// ============================================================================
extern ringidx_t recorder_append(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3);
extern ringidx_t recorder_append2(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7);
extern ringidx_t recorder_append3(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7,
uintptr_t a8,
uintptr_t a9,
uintptr_t a10,
uintptr_t a11);
extern ringidx_t recorder_append_fast(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3);
extern ringidx_t recorder_append_fast2(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7);
extern ringidx_t recorder_append_fast3(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7,
uintptr_t a8,
uintptr_t a9,
uintptr_t a10,
uintptr_t a11);
/// Activate a recorder (during construction time)
extern void recorder_activate(recorder_info *recorder);
/// Activate a tweak
extern void recorder_tweak_activate(recorder_tweak *tweak);
/// Show one recorder entry when a trace is enabled
extern void recorder_trace_entry(recorder_info *info, recorder_entry *entry);
/// Activate or deactivate traces (e.g. from command line or env variable)
extern int recorder_trace_set(const char *set);
enum { RECORDER_TRACE_OK,
RECORDER_TRACE_INVALID_NAME,
RECORDER_TRACE_INVALID_VALUE };
// ============================================================================
//
// Declaration of recorders and tweaks
//
// ============================================================================
#define RECORDER_DECLARE(Name) \
/* ----------------------------------------------------------------*/ \
/* Declare a recorder with the given name (for use in headers) */ \
/* ----------------------------------------------------------------*/ \
extern recorder_info * const recorder_info_ptr_for_##Name; \
extern struct recorder_info_for_##Name recorder_info_for_##Name
#define RECORDER_TWEAK_DECLARE(Name) \
/* ----------------------------------------------------------------*/ \
/* Declare a tweak with the given name (for use in headers) */ \
/* ----------------------------------------------------------------*/ \
extern recorder_tweak * const recorder_info_ptr_for_##Name; \
extern struct recorder_tweak_for_##Name recorder_info_for_##Name
// ============================================================================
//
// Definition of recorders and tweaks
//
// ============================================================================
#define RECORDER(Name, Size, Info) RECORDER_DEFINE(Name,Size,Info)
#define RECORDER_DEFINE(Name, Size, Info) \
/*!----------------------------------------------------------------*/ \
/*! Define a recorder type with Size elements */ \
/*!----------------------------------------------------------------*/ \
/*! \param Name is the C name fo the recorder. \
*! \param Size is the number of entries in the circular buffer. \
*! \param Info is a description of the recorder for help. */ \
\
/* The entry in linked list for this type */ \
struct recorder_info_for_##Name \
{ \
recorder_info info; \
recorder_entry data[Size]; \
} \
recorder_info_for_##Name = \
{ \
{ \
0, #Name, Info, NULL, \
{ NULL, NULL, NULL, NULL }, \
{ Size, sizeof(recorder_entry), 0, 0, 0, 0 }, \
{} \
}, \
{} \
}; \
extern recorder_info * const recorder_info_ptr_for_##Name; \
recorder_info * const recorder_info_ptr_for_##Name = \
&recorder_info_for_##Name.info; \
\
RECORDER_CONSTRUCTOR \
static void recorder_activate_##Name(void) \
/* ----------------------------------------------------------------*/ \
/* Activate recorder before entering main() */ \
/* ----------------------------------------------------------------*/ \
{ \
recorder_activate(RECORDER_INFO(Name)); \
} \
\
/* Purposefully generate compile error if macro not followed by ; */ \
extern void recorder_activate(recorder_info *recorder)
#define RECORDER_TWEAK_DEFINE(Name, Value, Info) \
struct recorder_tweak_for_##Name \
{ \
recorder_tweak info; \
} \
recorder_info_for_##Name = \
{ \
{ Value, #Name, Info, NULL } \
}; \
recorder_tweak * const recorder_info_ptr_for_##Name = \
&recorder_info_for_##Name.info; \
\
RECORDER_CONSTRUCTOR \
static void recorder_tweak_activate_##Name(void) \
/* ----------------------------------------------------------------*/ \
/* Activate a tweak before entering main() */ \
/* ----------------------------------------------------------------*/ \
{ \
recorder_tweak_activate(&recorder_info_for_##Name.info); \
}
// ============================================================================
//
// Access to recorder and tweak info
//
// ============================================================================
#define RECORDER_INFO(Name) (recorder_info_ptr_for_##Name)
#define RECORDER_TRACE(Name) (RECORDER_INFO(Name)->trace)
#define RECORDER_TWEAK(Name) RECORDER_TRACE(Name)
#define RECORDER_ENABLED(Name) (RECORDER_TRACE(Name) != -1)
// ============================================================================
//
// Recording stuff
//
// ============================================================================
#define record(Name, ...) RECORD_MACRO(Name, __VA_ARGS__)
#define RECORD(Name, ...) RECORD_MACRO(Name, __VA_ARGS__)
#define RECORD_MACRO(Name, ...) \
(RECORDER_ENABLED(Name) \
? RECORD_ALWAYS(Name, __VA_ARGS__) \
: (ringidx_t) -1)
#define RECORD_ALWAYS(Name, ...) \
RECORD_(RECORD, RECORD_COUNT_(Name,__VA_ARGS__),Name,##__VA_ARGS__)
#define RECORD_(RECORD,RCOUNT,Name,...) \
RECORD__(RECORD,RCOUNT,Name,## __VA_ARGS__)
#define RECORD__(RECORD,RCOUNT,Name,...) \
RECORD##RCOUNT(Name,##__VA_ARGS__)
#define RECORD_COUNT_(...) RECORD_COUNT__(Name,##__VA_ARGS__,_12,_11,_10,_9,_8,_7,_6,_5,_4,_3,_2,_1,_0,_X)
#define RECORD_COUNT__(Name,_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_N,...) _N
#define RECORD_0(Name, Format) \
recorder_append(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, 0, 0, 0, 0)
#define RECORD_1(Name, Format, a) \
recorder_append(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), 0, 0, 0)
#define RECORD_2(Name, Format, a,b) \
recorder_append(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), 0, 0)
#define RECORD_3(Name, Format, a,b,c) \
recorder_append(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), 0)
#define RECORD_4(Name, Format, a,b,c,d) \
recorder_append(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d))
#define RECORD_5(Name, Format, a,b,c,d,e) \
recorder_append2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), 0, 0, 0)
#define RECORD_6(Name, Format, a,b,c,d,e,f) \
recorder_append2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), 0, 0)
#define RECORD_7(Name, Format, a,b,c,d,e,f,g) \
recorder_append2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), 0)
#define RECORD_8(Name, Format, a,b,c,d,e,f,g,h) \
recorder_append2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h))
#define RECORD_9(Name, Format, a,b,c,d,e,f,g,h,i) \
recorder_append3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), 0,0,0)
#define RECORD_10(Name, Format, a,b,c,d,e,f,g,h,i,j) \
recorder_append3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), 0,0)
#define RECORD_11(Name, Format, a,b,c,d,e,f,g,h,i,j,k) \
recorder_append3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), \
RECORDER_ARG(k),0)
#define RECORD_12(Name,Format,a,b,c,d,e,f,g,h,i,j,k,l) \
recorder_append3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), \
RECORDER_ARG(k), \
RECORDER_ARG(l))
#define RECORD_X(Name, Format, ...) RECORD_TOO_MANY_ARGS(printf(Format, __VA_ARGS__))
// Faster version that does not record time, about 2x faster on x86
#define record_fast(Name, ...) RECORD_FAST(Name, __VA_ARGS__)
#define RECORD_FAST(Name, ...) \
(RECORDER_ENABLED(Name) \
? RECORD_FAST_ALWAYS(Name, __VA_ARGS__) \
: (ringidx_t) -1)
#define RECORD_FAST_ALWAYS(Name,...) \
RECORD_(RECORD_FAST,RECORD_COUNT_(Name,__VA_ARGS__),Name,##__VA_ARGS__)
#define RECORD_FAST_0(Name, Format) \
recorder_append_fast(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, 0, 0, 0, 0)
#define RECORD_FAST_1(Name, Format, a) \
recorder_append_fast(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), 0, 0, 0)
#define RECORD_FAST_2(Name, Format, a,b) \
recorder_append_fast(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), 0, 0)
#define RECORD_FAST_3(Name, Format, a,b,c) \
recorder_append_fast(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), 0)
#define RECORD_FAST_4(Name, Format, a,b,c,d) \
recorder_append_fast(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d))
#define RECORD_FAST_5(Name, Format, a,b,c,d,e) \
recorder_append_fast2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), 0, 0, 0)
#define RECORD_FAST_6(Name, Format, a,b,c,d,e,f) \
recorder_append_fast2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), 0, 0)
#define RECORD_FAST_7(Name, Format, a,b,c,d,e,f,g) \
recorder_append_fast2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), 0)
#define RECORD_FAST_8(Name, Format, a,b,c,d,e,f,g,h) \
recorder_append_fast2(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h))
#define RECORD_FAST_9(Name, Format, a,b,c,d,e,f,g,h,i) \
recorder_append_fast3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), 0,0,0)
#define RECORD_FAST_10(Name, Format, a,b,c,d,e,f,g,h,i,j) \
recorder_append_fast3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), 0,0)
#define RECORD_FAST_11(Name, Format, a,b,c,d,e,f,g,h,i,j,k) \
recorder_append_fast3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), \
RECORDER_ARG(k),0)
#define RECORD_FAST_12(Name, Format, a,b,c,d,e,f,g,h,i,j,k,l) \
recorder_append_fast3(RECORDER_INFO(Name), \
RECORDER_SOURCE_FUNCTION, \
RECORDER_SOURCE_LOCATION \
Format, \
RECORDER_ARG(a), \
RECORDER_ARG(b), \
RECORDER_ARG(c), \
RECORDER_ARG(d), \
RECORDER_ARG(e), \
RECORDER_ARG(f), \
RECORDER_ARG(g), \
RECORDER_ARG(h), \
RECORDER_ARG(i), \
RECORDER_ARG(j), \
RECORDER_ARG(k), \
RECORDER_ARG(l))
#define RECORD_FAST_X(Name, Format, ...) RECORD_TOO_MANY_ARGS(printf(Format, __VA_ARGS__))
// Some ugly macro drudgery to make things easy to use. Adjust type.
#ifdef __cplusplus
#define RECORDER_ARG(arg) _recorder_arg(arg)
#else // !__cplusplus
#if defined(__GNUC__) && !defined(__clang__)
# if __GNUC__ <= 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)
# define RECORDER_WITHOUT_GENERIC
# endif
#endif // __GNUC__
#ifdef RECORDER_WITHOUT_GENERIC
#define RECORDER_ARG(arg) ((uintptr_t) (arg))
#else // !RECORDER_WITHOUT_GENERIC
#define RECORDER_ARG(arg) \
_Generic(arg, \
unsigned char: _recorder_unsigned, \
unsigned short: _recorder_unsigned, \
unsigned: _recorder_unsigned, \
unsigned long: _recorder_unsigned, \
unsigned long long:_recorder_unsigned, \
char: _recorder_char, \
signed char: _recorder_signed, \
signed short: _recorder_signed, \
signed: _recorder_signed, \
signed long: _recorder_signed, \
signed long long: _recorder_signed, \
_Bool: _recorder_signed, \
float: _recorder_float, \
double: _recorder_double, \
default: _recorder_pointer)(arg)
#endif // RECORDER_WITHOUT_GENERIC
#endif // __cplusplus
// ============================================================================
//
// Default recorders declared in recorder.c
//
// ============================================================================
RECORDER_DECLARE(recorders);
RECORDER_DECLARE(recorder_traces);
RECORDER_DECLARE(recorder_signals);
// ============================================================================
//
// Timing information
//
// ============================================================================
#define RECORD_TIMING_BEGIN(Recorder) \
do { \
static uintptr_t _last_second = 0; \
static uintptr_t _total = 0; \
static uintptr_t _total_last_second = 0; \
static uintptr_t _duration_last_second = 0; \
static uintptr_t _iterations_last_second = 0; \
uintptr_t _start_time = recorder_tick()
#define RECORD_TIMING_END(Recorder, Operation, Name, Value) \
uintptr_t _end_time = recorder_tick(); \
uintptr_t _duration = _end_time - _start_time; \
uintptr_t _value = (Value); \
recorder_ring_fetch_add(_total, _value); \
recorder_ring_fetch_add(_total_last_second, _value); \
recorder_ring_fetch_add(_duration_last_second, _duration); \
recorder_ring_fetch_add(_iterations_last_second, 1); \
uintptr_t _print_interval = \
((RECORDER_INFO(Recorder)->trace == RECORDER_CHAN_MAGIC \
? 100 \
: RECORDER_INFO(Recorder)->trace) \
* (RECORDER_HZ / 1000)); \
uintptr_t _known = _last_second; \
uintptr_t _interval = _end_time - _known; \
double _scale = (double) RECORDER_HZ / _interval; \
if (_interval >= _print_interval && \
recorder_ring_compare_exchange(_last_second,_known,_end_time)) \
{ \
record(Recorder, \
Operation " %.2f " Name "/s, total %lu, " \
"%.2f loops/s, avg duration %.2f us, ", \
_total_last_second * _scale, \
_total, \
_iterations_last_second * _scale, \
_duration_last_second * _scale); \
_total_last_second = 0; \
_duration_last_second = 0; \
_iterations_last_second = 0; \
} \
} while (0)
// ============================================================================
//
// Data export from recorders
//
// ============================================================================
typedef enum recorder_type
// ----------------------------------------------------------------------------
// Type of data exported
// ----------------------------------------------------------------------------
{
RECORDER_NONE, // Nothing exported (pending format)
RECORDER_INVALID, // Invalid data
RECORDER_SIGNED, // Signed value
RECORDER_UNSIGNED, // Unsigned value
RECORDER_REAL // Real number
} recorder_type;
typedef union recorder_data
// ----------------------------------------------------------------------------
// Data exported
// ----------------------------------------------------------------------------
{
intptr_t signed_value;
uintptr_t unsigned_value;
#if RECORDER_64BIT
double real_value;
#else // Small intptr_t, so we save 32-bit float values
float real_value;
#endif // INTPTR_MAX
} recorder_data;
// A collection of data recorder_shmem in memory
typedef struct recorder_chans *recorder_chans_p;
typedef struct recorder_chan *recorder_chan_p;
// Magic number in export channel is different for 32-bit and 64-bit values
#define RECORDER_CHAN_MAGIC (0xC0DABABE ^ RECORDER_64BIT)
// The recorder channel version (update only when channel format changes)
#define RECORDER_CHAN_VERSION RECORDER_VERSION(1,2,0)
#define RECORDER_EXPORT_SIZE 2048
extern const char *recorder_export_file(void);
// ============================================================================
//
// Interface for the local process
//
// ============================================================================
// Creating recorder_chans for the local process to write into
extern recorder_chans_p recorder_chans_new(const char *file);
extern void recorder_chans_delete(recorder_chans_p);
extern recorder_chan_p recorder_chan_new(recorder_chans_p chans,
recorder_type type,
size_t size,
const char * name,
const char * description,
const char * unit,
recorder_data min,
recorder_data max);
extern void recorder_chan_delete(recorder_chan_p chan);
// ============================================================================
//
// Subscribing to recorder_chans in a remote process
//
// ============================================================================
// Subscribing to recorder_chans from another process
extern recorder_chans_p recorder_chans_open(const char *file);
extern void recorder_chans_close(recorder_chans_p chans);
extern bool recorder_chans_configure(recorder_chans_p chans,
const char *message);
extern bool recorder_chans_valid(recorder_chans_p chans);
extern recorder_chan_p recorder_chan_find(recorder_chans_p chans,
const char *pattern,
recorder_chan_p after);
extern const char * recorder_chan_name(recorder_chan_p chan);
extern const char * recorder_chan_description(recorder_chan_p chan);
extern const char * recorder_chan_unit(recorder_chan_p chan);
extern recorder_data recorder_chan_min(recorder_chan_p chan);
extern recorder_data recorder_chan_max(recorder_chan_p chan);
extern recorder_type recorder_chan_type(recorder_chan_p chan);
extern size_t recorder_chan_size(recorder_chan_p chan);
extern size_t recorder_chan_readable(recorder_chan_p chan,
ringidx_t *readerID);
extern size_t recorder_chan_read(recorder_chan_p chan,
recorder_data *ptr, size_t count,
ringidx_t *readerID);
// ============================================================================
//
// Support macros
//
// ============================================================================
#define RECORDER_SOURCE_FUNCTION __func__ /* Works in C99 and C++11 */
#define RECORDER_SOURCE_LOCATION __FILE__ ":" RECORDER_STRING(__LINE__) ":"
#define RECORDER_STRING(LINE) RECORDER_STRING_(LINE)
#define RECORDER_STRING_(LINE) #LINE
#ifdef __GNUC__
#define RECORDER_CONSTRUCTOR __attribute__((constructor))
#else
#define RECORDER_CONSTRUCTOR
#endif
// ============================================================================
//
// Portability helpers
//
// ============================================================================
#ifndef recorder_tick
// Return ticks (some kind of time unit) since first called
extern uintptr_t recorder_tick(void);
#endif
#ifndef RECORDER_HZ
#if RECORDER_64BIT
#define RECORDER_HZ 1000000
#else // Small time stamp, do not generate huge values
#define RECORDER_HZ 1000
#endif // INTPTR_MAX
#endif // RECORDER_HZ
#ifdef __cplusplus
}
#endif // __cplusplus
// ============================================================================
//
// Utility: Convert floating point values for vararg format
//
// ============================================================================
//
// The recorder stores only uintptr_t in recorder entries. Integer types
// are promoted, pointer types are converted. Floating point values
// are converted a floating point type of the same size as uintptr_t,
// i.e. float are converted to double on 64-bit platforms, and conversely.
#ifdef __cplusplus
#include <string>
// In C++, we don't use _Generic but actual overloading
template <class inttype>
static inline uintptr_t _recorder_arg(inttype i)
{
return (uintptr_t) i;
}
static inline uintptr_t _recorder_arg(const std::string &arg)
{
return (uintptr_t) arg.c_str();
}
#define _recorder_float _recorder_arg
#define _recorder_double _recorder_arg
#else // !__cplusplus
static inline uintptr_t _recorder_char(char c)
// ----------------------------------------------------------------------------
// Necessary because of the way generic selections work
// ----------------------------------------------------------------------------
{
return c;
}
static inline uintptr_t _recorder_unsigned(uintptr_t i)
// ----------------------------------------------------------------------------
// Necessary because of the way generic selections work
// ----------------------------------------------------------------------------
{
return i;