forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrf_profiler.h
318 lines (280 loc) · 9.11 KB
/
nrf_profiler.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
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef _NRF_PROFILER_H_
#define _NRF_PROFILER_H_
/**
* @defgroup nrf_profiler Profiler
* @brief Profiler
*
* @{
*/
#include <zephyr/types.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/__assert.h>
#ifndef CONFIG_NRF_PROFILER_MAX_NUMBER_OF_APP_EVENTS
/** Maximum number of events. */
#define CONFIG_NRF_PROFILER_MAX_NUMBER_OF_APP_EVENTS 0
#endif
#ifndef CONFIG_NRF_PROFILER_NUMBER_OF_INTERNAL_EVENTS
/** Number of internal events. */
#define CONFIG_NRF_PROFILER_NUMBER_OF_INTERNAL_EVENTS 0
#endif
/** Maximum number of events including user application events and internal events. */
#define NRF_PROFILER_MAX_NUMBER_OF_APPLICATION_AND_INTERNAL_EVENTS \
((CONFIG_NRF_PROFILER_MAX_NUMBER_OF_APP_EVENTS) + \
(CONFIG_NRF_PROFILER_NUMBER_OF_INTERNAL_EVENTS))
/** @brief Bitmask indicating event is enabled.
* This structure is private to nrf_profiler and should not be referred from outside.
*/
struct nrf_profiler_event_enabled_bm {
ATOMIC_DEFINE(flags, NRF_PROFILER_MAX_NUMBER_OF_APPLICATION_AND_INTERNAL_EVENTS);
};
/** @brief Set of flags for enabling/disabling profiling for given event types.
*/
extern struct nrf_profiler_event_enabled_bm _nrf_profiler_event_enabled_bm;
/** @brief Number of event types registered in the Profiler.
*/
extern uint8_t nrf_profiler_num_events;
/** @brief Data types for profiling.
*/
enum nrf_profiler_arg {
NRF_PROFILER_ARG_U8,
NRF_PROFILER_ARG_S8,
NRF_PROFILER_ARG_U16,
NRF_PROFILER_ARG_S16,
NRF_PROFILER_ARG_U32,
NRF_PROFILER_ARG_S32,
NRF_PROFILER_ARG_STRING,
NRF_PROFILER_ARG_TIMESTAMP
};
/** @brief Buffer required for data that is sent with the event.
*/
struct log_event_buf {
#ifdef CONFIG_NRF_PROFILER
/** Pointer to the end of the payload. */
uint8_t *payload;
/** Array where the payload is located before it is sent. */
uint8_t payload_start[CONFIG_NRF_PROFILER_CUSTOM_EVENT_BUF_LEN];
#endif
};
/** @brief Initialize the Profiler.
*
* @note This function is thread-safe, but not safe to use in
* interrupts.
*
* @retval 0 If the operation was successful.
*/
#ifdef CONFIG_NRF_PROFILER
int nrf_profiler_init(void);
#else
static inline int nrf_profiler_init(void) {return 0; }
#endif
/** @brief Terminate the Profiler.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_term(void);
#else
static inline void nrf_profiler_term(void) {}
#endif
/** @brief Retrieve the description of an event type.
*
* @param nrf_profiler_event_id Event ID.
*
* @return Event description.
*/
#ifdef CONFIG_NRF_PROFILER
const char *nrf_profiler_get_event_descr(size_t nrf_profiler_event_id);
#else
static inline const char *nrf_profiler_get_event_descr(size_t nrf_profiler_event_id)
{
return NULL;
}
#endif
/** @brief Check if profiling is enabled for a given event type.
*
* @param nrf_profiler_event_id Event ID.
*
* @return Logical value indicating if the event type is currently profiled.
*/
static inline bool is_profiling_enabled(size_t nrf_profiler_event_id)
{
if (IS_ENABLED(CONFIG_NRF_PROFILER)) {
__ASSERT_NO_MSG(nrf_profiler_event_id <
NRF_PROFILER_MAX_NUMBER_OF_APPLICATION_AND_INTERNAL_EVENTS);
return atomic_test_bit(_nrf_profiler_event_enabled_bm.flags, nrf_profiler_event_id);
}
return false;
}
/** @brief Register an event type.
*
* @note This function is thread-safe, but not safe to use in
* interrupts.
*
* @param name Name of the event type.
* @param args Names of data values sent with the event.
* @param arg_types Types of data values sent with the event.
* @param arg_cnt Number of data values sent with the event.
*
* @return ID assigned to the event type.
*/
#ifdef CONFIG_NRF_PROFILER
uint16_t nrf_profiler_register_event_type(const char *name, const char * const *args,
const enum nrf_profiler_arg *arg_types,
uint8_t arg_cnt);
#else
static inline uint16_t nrf_profiler_register_event_type(const char *name,
const char * const *args, const enum nrf_profiler_arg *arg_types,
uint8_t arg_cnt) {return 0; }
#endif
/** @brief Initialize a buffer for the data of an event.
*
* @param buf Pointer to the data buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_start(struct log_event_buf *buf);
#else
static inline void nrf_profiler_log_start(struct log_event_buf *buf) {}
#endif
/** @brief Encode and add uint32_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_uint32(struct log_event_buf *buf, uint32_t data);
#else
static inline void nrf_profiler_log_encode_uint32(struct log_event_buf *buf,
uint32_t data) {}
#endif
/** @brief Encode and add int32_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_int32(struct log_event_buf *buf, int32_t data);
#else
static inline void nrf_profiler_log_encode_int32(struct log_event_buf *buf,
int32_t data) {}
#endif
/** @brief Encode and add uint16_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_uint16(struct log_event_buf *buf, uint16_t data);
#else
static inline void nrf_profiler_log_encode_uint16(struct log_event_buf *buf,
uint16_t data) {}
#endif
/** @brief Encode and add int16_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_int16(struct log_event_buf *buf, int16_t data);
#else
static inline void nrf_profiler_log_encode_int16(struct log_event_buf *buf,
int16_t data) {}
#endif
/** @brief Encode and add uint8_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_uint8(struct log_event_buf *buf, uint8_t data);
#else
static inline void nrf_profiler_log_encode_uint8(struct log_event_buf *buf,
uint8_t data) {}
#endif
/** @brief Encode and add int8_t data type to a buffer.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param data Data to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_int8(struct log_event_buf *buf, int8_t data);
#else
static inline void nrf_profiler_log_encode_int8(struct log_event_buf *buf,
int8_t data) {}
#endif
/** @brief Encode and add string to a buffer.
*
* Maximum 255 characters can be sent (the rest is omitted).
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param string String to add to the buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_encode_string(struct log_event_buf *buf, const char *string);
#else
static inline void nrf_profiler_log_encode_string(struct log_event_buf *buf,
const char *string) {}
#endif
/** @brief Encode and add the event's address in memory to the buffer.
*
* This information is used for event identification.
*
* @note The buffer must be initialized with @ref nrf_profiler_log_start
* before calling this function.
*
* @param buf Pointer to the data buffer.
* @param mem_address Memory address to encode.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_add_mem_address(struct log_event_buf *buf,
const void *mem_address);
#else
static inline void nrf_profiler_log_add_mem_address(struct log_event_buf *buf,
const void *mem_address) {}
#endif
/** @brief Send data from the buffer to the host.
*
* This function only sends data that is already stored in the buffer.
* Use @ref nrf_profiler_log_encode_uint32, @ref nrf_profiler_log_encode_int32,
* @ref nrf_profiler_log_encode_uint16, @ref nrf_profiler_log_encode_int16,
* @ref nrf_profiler_log_encode_uint8, @ref nrf_profiler_log_encode_int8,
* @ref nrf_profiler_log_encode_string or @ref nrf_profiler_log_add_mem_address
* to add data to the buffer.
*
* @param event_type_id Event type ID as assigned to the event type
* when it is registered.
* @param buf Pointer to the data buffer.
*/
#ifdef CONFIG_NRF_PROFILER
void nrf_profiler_log_send(struct log_event_buf *buf, uint16_t event_type_id);
#else
static inline void nrf_profiler_log_send(struct log_event_buf *buf,
uint16_t event_type_id) {}
#endif
/**
* @}
*/
#endif /* _NRF_PROFILER_H_ */