forked from uucidl/uu.spdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spdr.h
234 lines (192 loc) · 6.32 KB
/
spdr.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
#ifndef UU_SPDR_H
#define UU_SPDR_H
/**
* A tracing library
*/
#include "spdr-private.h"
#include "pstdint.h"
#include <stddef.h> /* for size_t */
/**
* Context for the library
*/
struct SPDR_Context;
/**
* Buffer capacity
*/
struct SPDR_Capacity {
size_t count;
size_t capacity;
};
/**
* Reporting type.
*/
enum SPDR_Report_Type { SPDR_PLAIN_REPORT, SPDR_CHROME_REPORT };
/**
* Initializes the library
*
* spdr will use the provided memory buffer for its memory
* allocations.
*
* the size of the memory buffer will limit the number of events that
* can be recorded at a time.
*
* @return 0 on success
*/
int spdr_init(struct SPDR_Context **context, void *buffer, size_t buffer_size);
/**
* Shutdowns the library
*/
void spdr_deinit(struct SPDR_Context **context);
/**
* Activates the recording of traces (off by default)
*/
void spdr_enable_trace(struct SPDR_Context *context, int traceon);
/**
* Clears the log buffer to start anew
*/
void spdr_reset(struct SPDR_Context *context);
/**
* Returns the current event count and total available capacity
*/
struct SPDR_Capacity spdr_capacity(struct SPDR_Context *context);
/**
* Provide your own clock function.
*
* It must return a strictly monotonic series of numbers
* representing elapsed microseconds.
*
* It must be thread-safe: it will be called concurrently
* from multiple threads
*/
void spdr_set_clock_microseconds_fn(
struct SPDR_Context *context,
uint64_t (*clock_microseconds_fn)(void *user_data),
void *user_data);
/**
* Provide your logging function if you want a trace stream to be produced.
*/
void spdr_set_log_fn(struct SPDR_Context *context,
void (*log_fn)(const char *line, void *user_data),
void *user_data);
/**
* Report the traces which have been recorded so far, using the
* provided log function.
*/
void spdr_report(struct SPDR_Context *context,
enum SPDR_Report_Type report_type,
void (*print_fn)(const char *string, void *user_data),
void *user_data);
/**
* Builds arguments of various types
*/
#define SPDR_INT(key, value) UU_SPDR_INT(key, value)
#define SPDR_FLOAT(key, value) UU_SPDR_FLOAT(key, value)
#define SPDR_STR(key, value) UU_SPDR_STR(key, value)
/* __ Instant events __ */
/**
* An instant event
*/
#define SPDR_EVENT(spdr, cat, name) UU_SPDR_TRACE(spdr, cat, name, SPDR_EVENT)
/**
* An instant event with one parameter
*/
#define SPDR_EVENT1(spdr, cat, name, arg0) \
UU_SPDR_TRACE1(spdr, cat, name, SPDR_EVENT, arg0)
/**
* An instant event with two parameters
*/
#define SPDR_EVENT2(spdr, cat, name, arg0, arg1) \
UU_SPDR_TRACE2(spdr, cat, name, SPDR_EVENT, arg0, arg1)
/**
* An instant event with two parameters
*/
#define SPDR_EVENT3(spdr, cat, name, arg0, arg1, arg2) \
UU_SPDR_TRACE3(spdr, cat, name, SPDR_EVENT, arg0, arg1, arg2)
/* __ Work slices __ */
/**
* Begin a slice of work
*/
#define SPDR_BEGIN(spdr, cat, name) UU_SPDR_TRACE(spdr, cat, name, SPDR_BEGIN)
/**
* Begin a slice of work, with one parameter.
*/
#define SPDR_BEGIN1(spdr, cat, name, arg0) \
UU_SPDR_TRACE1(spdr, cat, name, SPDR_BEGIN, arg0)
/**
* Begin a slice of work, with two parameters
*/
#define SPDR_BEGIN2(spdr, cat, name, arg0, arg1) \
UU_SPDR_TRACE2(spdr, cat, name, SPDR_BEGIN, arg0, arg1)
/**
* Begin a slice of work, with three parameters
*/
#define SPDR_BEGIN3(spdr, cat, name, arg0, arg1, arg2) \
UU_SPDR_TRACE3(spdr, cat, name, SPDR_BEGIN, arg0, arg1, arg2)
/**
* End a slice of work
*/
#define SPDR_END(spdr, cat, name) UU_SPDR_TRACE(spdr, cat, name, SPDR_END)
/**
* Mark the beginning and end of a scope
*
* Fully supported in C++. Only supported on select compilers for C.
*/
#define SPDR_SCOPE(spdr, cat, name) UU_SPDR_SCOPE_TRACE(spdr, cat, name)
#define SPDR_SCOPE1(spdr, cat, name, arg0) \
UU_SPDR_SCOPE_TRACE1(spdr, cat, name, arg0)
#define SPDR_SCOPE2(spdr, cat, name, arg0, arg1) \
UU_SPDR_SCOPE_TRACE2(spdr, cat, name, arg0, arg1)
#define SPDR_SCOPE3(spdr, cat, name, arg0, arg1, arg2) \
UU_SPDR_SCOPE_TRACE3(spdr, cat, name, arg0, arg1, arg2)
/* __ Counters __ */
/**
* Track values over time.
*
* @param arg must be SPDR_INT or SPDR_FLOAT
*/
#define SPDR_COUNTER1(spdr, cat, name, arg0) \
UU_SPDR_TRACE1(spdr, cat, name, SPDR_COUNTER, arg0)
#define SPDR_COUNTER2(spdr, cat, name, arg0, arg1) \
UU_SPDR_TRACE2(spdr, cat, name, SPDR_COUNTER, arg0, arg1)
#define SPDR_COUNTER3(spdr, cat, name, arg0, arg1, arg2) \
UU_SPDR_TRACE3(spdr, cat, name, SPDR_COUNTER, arg0, arg1, arg2)
/* __ Async events __ */
/**
* An async event
*/
#define SPDR_ASYNC_EVENT_BEGIN(spdr, cat, name, id) \
UU_SPDR_TRACE1(spdr, cat, name, SPDR_ASYNC_EVENT_BEGIN, \
SPDR_INT("id", id))
/**
* An async event with one parameter
*/
#define SPDR_ASYNC_EVENT_BEGIN1(spdr, cat, name, id, arg0) \
UU_SPDR_TRACE2(spdr, cat, name, SPDR_ASYNC_EVENT_BEGIN, \
SPDR_INT("id", id), arg0)
/**
* An async event with two parameters
*/
#define SPDR_ASYNC_EVENT_BEGIN2(spdr, cat, name, id, arg0, arg1) \
UU_SPDR_TRACE3(spdr, cat, name, SPDR_ASYNC_EVENT_BEGIN, \
SPDR_INT("id", id), arg0, arg1)
/**
* Ends an async event
*/
#define SPDR_ASYNC_EVENT_END(spdr, cat, name, id) \
UU_SPDR_TRACE1(spdr, cat, name, SPDR_ASYNC_EVENT_END, \
SPDR_INT("id", id))
/* __ Metadata __ */
/**
* Metadata.
*
* For instance to set the thread name:
* @code
* SPDR_METADATA1("thread_name", SPDR_STR("name", "My_Thread"))
* @code
*
* chrome://tracing will then display this name rather than the tid of
* the thread.
*/
#define SPDR_METADATA1(spdr, name, arg0) \
UU_SPDR_TRACE1(spdr, "__metadata", name, SPDR_METADATA, arg0)
#endif