barectf/templates/c: rename "back-end" -> "back end"
[barectf.git] / barectf / templates / c / barectf.c.j2
1 {#
2 # The MIT License (MIT)
3 #
4 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #}
25 {% import 'common.j2' as common %}
26 {% import 'c/common.j2' as c_common %}
27 {% import 'c/barectf.c-macros.j2' as macros %}
28 {% set prefix = common.prefix %}
29 {% set ucprefix = common.ucprefix %}
30 {% set ctx_struct_name = c_common.ctx_struct_name %}
31 {% set cg_opts = cfg.options.code_generation_options %}
32 {% set const_params = true %}
33 {% include 'license-header.j2' %}
34
35
36 #include <stdint.h>
37 #include <string.h>
38 #include <assert.h>
39
40 #include "{{ header_file_name }}"
41 #include "{{ bitfield_header_file_name }}"
42
43 #define _ALIGN(_at_var, _align) \
44 do { \
45 (_at_var) = ((_at_var) + ((_align) - 1)) & -(_align); \
46 } while (0)
47
48 #ifdef __cplusplus
49 # define _TO_VOID_PTR(_value) static_cast<void *>(_value)
50 # define _FROM_VOID_PTR(_type, _value) static_cast<_type *>(_value)
51 #else
52 # define _TO_VOID_PTR(_value) ((void *) (_value))
53 # define _FROM_VOID_PTR(_type, _value) ((_type *) (_value))
54 #endif
55
56 #define _BITS_TO_BYTES(_x) ((_x) >> 3)
57 #define _BYTES_TO_BITS(_x) ((_x) << 3)
58
59 union _f2u {
60 float f;
61 uint32_t u;
62 };
63
64 union _d2u {
65 double f;
66 uint64_t u;
67 };
68
69 uint32_t {{ prefix }}packet_size(const void * const ctx)
70 {
71 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, ctx)->packet_size;
72 }
73
74 int {{ prefix }}packet_is_full(const void * const vctx)
75 {
76 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
77
78 return ctx->at == ctx->packet_size;
79 }
80
81 int {{ prefix }}packet_is_empty(const void * const vctx)
82 {
83 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
84
85 return ctx->at <= ctx->off_content;
86 }
87
88 uint32_t {{ prefix }}packet_events_discarded(const void * const vctx)
89 {
90 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->events_discarded;
91 }
92
93 uint8_t *{{ prefix }}packet_buf(const void * const vctx)
94 {
95 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->buf;
96 }
97
98 uint32_t {{ prefix }}packet_buf_size(const void * const vctx)
99 {
100 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
101
102 return _BITS_TO_BYTES(ctx->packet_size);
103 }
104
105 void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf,
106 const uint32_t buf_size)
107 {
108 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
109
110 ctx->buf = buf;
111 ctx->packet_size = _BYTES_TO_BITS(buf_size);
112 }
113
114 int {{ prefix }}packet_is_open(const void * const vctx)
115 {
116 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_is_open;
117 }
118
119 int {{ prefix }}is_in_tracing_section(const void * const vctx)
120 {
121 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
122 }
123
124 volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void * const vctx)
125 {
126 return &_FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
127 }
128
129 int {{ prefix }}is_tracing_enabled(const void * const vctx)
130 {
131 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled;
132 }
133
134 void {{ prefix }}enable_tracing(void * const vctx, const int enable)
135 {
136 _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled = enable;
137 }
138
139 static
140 void _write_c_str(struct {{ ctx_struct_name }} * const ctx, const char * const src)
141 {
142 const uint32_t sz = strlen(src) + 1;
143
144 memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz);
145 ctx->at += _BYTES_TO_BITS(sz);
146 }
147
148 static
149 int _reserve_er_space(void * const vctx, const uint32_t er_size)
150 {
151 int ret;
152 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
153
154 /* Event _cannot_ fit? */
155 if (er_size > (ctx->packet_size - ctx->off_content)) {
156 goto no_space;
157 }
158
159 /* Packet is full? */
160 if ({{ prefix }}packet_is_full(ctx)) {
161 /* Yes: is the back end full? */
162 if (ctx->cbs.is_backend_full(ctx->data)) {
163 /* Yes: discard event record */
164 goto no_space;
165 }
166
167 /* Back-end is _not_ full: open new packet */
168 ctx->use_cur_last_event_ts = 1;
169 ctx->cbs.open_packet(ctx->data);
170 ctx->use_cur_last_event_ts = 0;
171 }
172
173 /* Event fits the current packet? */
174 if (er_size > (ctx->packet_size - ctx->at)) {
175 /* No: close packet now */
176 ctx->use_cur_last_event_ts = 1;
177 ctx->cbs.close_packet(ctx->data);
178 ctx->use_cur_last_event_ts = 0;
179
180 /* Is the back end full? */
181 if (ctx->cbs.is_backend_full(ctx->data)) {
182 /* Yes: discard event record */
183 goto no_space;
184 }
185
186 /* Back-end is _not_ full: open new packet */
187 ctx->use_cur_last_event_ts = 1;
188 ctx->cbs.open_packet(ctx->data);
189 ctx->use_cur_last_event_ts = 0;
190 assert(er_size <= (ctx->packet_size - ctx->at));
191 }
192
193 ret = 1;
194 goto end;
195
196 no_space:
197 ctx->events_discarded++;
198 ret = 0;
199
200 end:
201 return ret;
202 }
203
204 static
205 void _commit_er(void * const vctx)
206 {
207 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
208
209 /* Is the packet full? */
210 if ({{ prefix }}packet_is_full(ctx)) {
211 /* Yes: close it now */
212 ctx->cbs.close_packet(ctx->data);
213 }
214 }
215
216 {% include 'c/ctx-init-func-proto.j2' %}
217
218 {
219 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
220 ctx->cbs = cbs;
221 ctx->data = data;
222 ctx->buf = buf;
223 ctx->packet_size = _BYTES_TO_BITS(buf_size);
224 ctx->at = 0;
225 ctx->events_discarded = 0;
226 ctx->packet_is_open = 0;
227 ctx->in_tracing_section = 0;
228 ctx->is_tracing_enabled = 1;
229 ctx->use_cur_last_event_ts = 0;
230 }
231
232 {% for dst in cfg.trace.type.data_stream_types | sort %}
233 {% set def_clk_type = dst.default_clock_type %}
234 {% set sctx_name %}{{ prefix }}{{ dst.name }}{% endset %}
235 {% set this_ds_ops = ds_ops[dst] %}
236 {% include 'c/open-func-proto.j2' %}
237
238 {
239 {{ macros.open_close_func_preamble(dst) | indent_tab }}
240
241 /*
242 * This function is either called by a tracing function, or
243 * directly by the platform.
244 *
245 * If it's called by a tracing function, then
246 * `ctx->in_tracing_section` is 1, so it's safe to open
247 * the packet here (alter the packet), even if tracing was
248 * disabled in the meantime because we're already in a tracing
249 * section (which finishes at the end of the tracing function
250 * call).
251 *
252 * If it's called directly by the platform, then if tracing is
253 * disabled, we don't want to alter the packet, and return
254 * immediately.
255 */
256 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
257 ctx->in_tracing_section = 0;
258 goto end;
259 }
260
261 /* We can alter the packet */
262 ctx->in_tracing_section = 1;
263
264 /* Do not open a packet that is already open */
265 if (ctx->packet_is_open) {
266 ctx->in_tracing_section = saved_in_tracing_section;
267 goto end;
268 }
269
270 ctx->at = 0;
271 {% set pkt_header_op = this_ds_ops.pkt_header_op %}
272 {% if pkt_header_op %}
273
274 {{ pkt_header_op.serialize_str(dst=dst) | indent_tab }}
275 {% endif %}
276
277 {{ this_ds_ops.pkt_ctx_op.serialize_str(dst=dst) | indent_tab }}
278
279 /* Save content beginning's offset */
280 ctx->off_content = ctx->at;
281
282 /* Mark current packet as open */
283 ctx->packet_is_open = 1;
284
285 /* Not tracing anymore */
286 ctx->in_tracing_section = saved_in_tracing_section;
287
288 end:
289 return;
290 }
291
292 {% include 'c/close-func-proto.j2' %}
293
294 {
295 {{ macros.open_close_func_preamble(dst) | indent_tab }}
296
297 /*
298 * This function is either called by a tracing function, or
299 * directly by the platform.
300 *
301 * If it's called by a tracing function, then
302 * `ctx->in_tracing_section` is 1, so it's safe to close
303 * the packet here (alter the packet), even if tracing was
304 * disabled in the meantime, because we're already in a tracing
305 * section (which finishes at the end of the tracing function
306 * call).
307 *
308 * If it's called directly by the platform, then if tracing is
309 * disabled, we don't want to alter the packet, and return
310 * immediately.
311 */
312 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
313 ctx->in_tracing_section = 0;
314 goto end;
315 }
316
317 /* We can alter the packet */
318 ctx->in_tracing_section = 1;
319
320 /* Do not close a packet that is not open */
321 if (!ctx->packet_is_open) {
322 ctx->in_tracing_section = saved_in_tracing_section;
323 goto end;
324 }
325
326 /* Save content size */
327 ctx->content_size = ctx->at;
328 {% set name = 'timestamp_end' %}
329 {% if name in dst._pkt_ctx_ft.members %}
330 {% set op = ds_op_pkt_ctx_op(dst, name) %}
331
332 /* Go back to `timestamp_end` field offset */
333 ctx->at = sctx->off_{{ op | op_src_var_name }};
334
335 {% set src = 'ts' %}
336 {% filter indent_tab(indent_first=true) %}
337 {% include 'c/serialize-write-saved-int-statements.j2' %}
338
339 {% endfilter %}
340 {% endif %}
341 {% set name = 'content_size' %}
342 {% if name in dst._pkt_ctx_ft.members %}
343 {% set op = ds_op_pkt_ctx_op(dst, name) %}
344
345 /* Go back to `content_size` field offset */
346 ctx->at = sctx->off_{{ op | op_src_var_name }};
347
348 {% set src %}ctx->{{ name }}{% endset %}
349 {% filter indent_tab(indent_first=true) %}
350 {% include 'c/serialize-write-saved-int-statements.j2' %}
351
352 {% endfilter %}
353 {% endif %}
354 {% set name = 'events_discarded' %}
355 {% if name in dst._pkt_ctx_ft.members %}
356 {% set op = ds_op_pkt_ctx_op(dst, name) %}
357
358 /* Go back to `events_discarded` field offset */
359 ctx->at = sctx->off_{{ op | op_src_var_name }};
360
361 {% set src %}ctx->{{ name }}{% endset %}
362 {% filter indent_tab(indent_first=true) %}
363 {% include 'c/serialize-write-saved-int-statements.j2' %}
364
365 {% endfilter %}
366 {% endif %}
367
368 /* Go back to end of packet */
369 ctx->at = ctx->packet_size;
370
371 /* Mark packet as closed */
372 ctx->packet_is_open = 0;
373
374 /* Not tracing anymore */
375 ctx->in_tracing_section = saved_in_tracing_section;
376
377 end:
378 return;
379 }
380 {% if dst._er_header_ft %}
381
382 static void _serialize_er_header_{{ dst.name }}(void * const vctx,
383 const uint32_t ert_id)
384 {
385 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
386 {% if def_clk_type %}
387 struct {{ sctx_name }}_ctx * const sctx = _FROM_VOID_PTR(struct {{ sctx_name }}_ctx, vctx);
388 const {{ cg_opts.clock_type_c_types[def_clk_type] }} ts = sctx->cur_last_event_ts;
389 {% endif %}
390
391 {{ this_ds_ops.er_header_op.serialize_str(dst=dst) | indent_tab }}
392 }
393 {% endif %}
394 {% if dst.event_record_common_context_field_type %}
395
396 static void _serialize_er_common_ctx_{{ dst.name }}(void * const vctx{{ dst | serialize_er_common_ctx_func_params_str(const_params) }})
397 {
398 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
399
400 {{ this_ds_ops.er_common_ctx_op.serialize_str(dst=dst) | indent_tab }}
401 }
402 {% endif %}
403 {# internal serialization functions #}
404 {% for ert in dst.event_record_types | sort %}
405
406 static void _serialize_er_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params) }})
407 {
408 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
409 {% if dst._er_header_ft %}
410
411 /* Serialize header */
412 _serialize_er_header_{{ dst.name }}(ctx, {{ ert.id }});
413 {% endif %}
414 {% if dst.event_record_common_context_field_type %}
415
416 /* Serialize common context */
417 {% set params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type) %}
418 _serialize_er_common_ctx_{{ dst.name }}(ctx{{ params }});
419 {% endif %}
420 {% set this_er_ops = this_ds_ops.er_ops[ert] %}
421 {% if this_er_ops.spec_ctx_op %}
422
423 {{ this_er_ops.spec_ctx_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
424 {% endif %}
425 {% if this_er_ops.payload_op %}
426
427 {{ this_er_ops.payload_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
428 {% endif %}
429 }
430 {% endfor %}
431 {# internal size functions #}
432 {% for ert in dst.event_record_types | sort %}
433 {% set this_er_ops = this_ds_ops.er_ops[ert] %}
434
435 static uint32_t _er_size_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params, only_dyn=true) }})
436 {
437 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
438 uint32_t at = ctx->at;
439 {% if this_ds_ops.er_header_op %}
440
441 {{ this_ds_ops.er_header_op.size_str(dst=dst) | indent_tab }}
442 {% endif %}
443 {% if this_ds_ops.er_common_ctx_op %}
444
445 {{ this_ds_ops.er_common_ctx_op.size_str(dst=dst) | indent_tab }}
446 {% endif %}
447 {% if this_er_ops.spec_ctx_op %}
448
449 {{ this_er_ops.spec_ctx_op.size_str(dst=dst, ert=ert) | indent_tab }}
450 {% endif %}
451 {% if this_er_ops.payload_op %}
452
453 {{ this_er_ops.payload_op.size_str(dst=dst, ert=ert) | indent_tab }}
454 {% endif %}
455
456 return at - ctx->at;
457 }
458 {% endfor %}
459 {# public tracing functions #}
460 {% for ert in dst.event_record_types | sort %}
461
462 {% include 'c/trace-func-proto.j2' %}
463
464 {
465 struct {{ ctx_struct_name }} * const ctx = &sctx->parent;
466 uint32_t er_size;
467
468 {% if def_clk_type %}
469 /* Save timestamp */
470 sctx->cur_last_event_ts = ctx->cbs.{{ def_clk_type.name }}_clock_get_value(ctx->data);
471
472 {% endif %}
473 if (!ctx->is_tracing_enabled) {
474 goto end;
475 }
476
477 /* We can alter the packet */
478 ctx->in_tracing_section = 1;
479
480 /* Compute event record size */
481 {% set er_common_ctx_params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type, true) %}
482 {% set spec_ctx_params = macros.ft_call_params(root_ft_prefixes.ERSC, ert.specific_context_field_type, true) %}
483 {% set payload_params = macros.ft_call_params(root_ft_prefixes.ERP, ert.payload_field_type, true) %}
484 {% set params %}{{ er_common_ctx_params }}{{ spec_ctx_params }}{{ payload_params }}{% endset %}
485 er_size = _er_size_{{ dst.name }}_{{ ert.name }}(_TO_VOID_PTR(ctx){{ params }});
486
487 /* Is there enough space to serialize? */
488 if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
489 /* no: forget this */
490 ctx->in_tracing_section = 0;
491 goto end;
492 }
493
494 /* Serialize event record */
495 {% set er_common_ctx_params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type) %}
496 {% set spec_ctx_params = macros.ft_call_params(root_ft_prefixes.ERSC, ert.specific_context_field_type) %}
497 {% set payload_params = macros.ft_call_params(root_ft_prefixes.ERP, ert.payload_field_type) %}
498 {% set params %}{{ er_common_ctx_params }}{{ spec_ctx_params }}{{ payload_params }}{% endset %}
499 _serialize_er_{{ dst.name }}_{{ ert.name }}(_TO_VOID_PTR(ctx){{ params }});
500
501 /* Commit event record */
502 _commit_er(_TO_VOID_PTR(ctx));
503
504 /* Not tracing anymore */
505 ctx->in_tracing_section = 0;
506
507 end:
508 return;
509 }
510 {% endfor %}
511 {% endfor %}
This page took 0.046566 seconds and 4 git commands to generate.