d4b4ee0ed40971d89aa43195845d9a5781a6c2f6
[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 vctx)
70 {
71 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->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 uint32_t {{ prefix }}discarded_event_records_count(const void * const vctx)
94 {
95 return {{ prefix }}packet_events_discarded(vctx);
96 }
97
98 uint8_t *{{ prefix }}packet_buf(const void * const vctx)
99 {
100 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->buf;
101 }
102
103 uint8_t *{{ prefix }}packet_buf_addr(const void * const vctx)
104 {
105 return {{ prefix }}packet_buf(vctx);
106 }
107
108 uint32_t {{ prefix }}packet_buf_size(const void * const vctx)
109 {
110 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
111
112 return _BITS_TO_BYTES(ctx->packet_size);
113 }
114
115 void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf,
116 const uint32_t buf_size)
117 {
118 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
119
120 ctx->buf = buf;
121 ctx->packet_size = _BYTES_TO_BITS(buf_size);
122 }
123
124 int {{ prefix }}packet_is_open(const void * const vctx)
125 {
126 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_is_open;
127 }
128
129 int {{ prefix }}is_in_tracing_section(const void * const vctx)
130 {
131 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
132 }
133
134 volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void * const vctx)
135 {
136 return &_FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
137 }
138
139 int {{ prefix }}is_tracing_enabled(const void * const vctx)
140 {
141 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled;
142 }
143
144 void {{ prefix }}enable_tracing(void * const vctx, const int enable)
145 {
146 _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled = enable;
147 }
148
149 static
150 void _write_c_str(struct {{ ctx_struct_name }} * const ctx, const char * const src)
151 {
152 const uint32_t sz = strlen(src) + 1;
153
154 memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz);
155 ctx->at += _BYTES_TO_BITS(sz);
156 }
157
158 static
159 int _reserve_er_space(void * const vctx, const uint32_t er_size)
160 {
161 int ret;
162 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
163
164 /* Event _cannot_ fit? */
165 if (er_size > (ctx->packet_size - ctx->off_content)) {
166 goto no_space;
167 }
168
169 /* Packet is full? */
170 if ({{ prefix }}packet_is_full(ctx)) {
171 /* Yes: is the back end full? */
172 if (ctx->cbs.is_backend_full(ctx->data)) {
173 /* Yes: discard event record */
174 goto no_space;
175 }
176
177 /* Back-end is _not_ full: open new packet */
178 ctx->use_cur_last_event_ts = 1;
179 ctx->cbs.open_packet(ctx->data);
180 ctx->use_cur_last_event_ts = 0;
181 }
182
183 /* Event fits the current packet? */
184 if (er_size > (ctx->packet_size - ctx->at)) {
185 /* No: close packet now */
186 ctx->use_cur_last_event_ts = 1;
187 ctx->cbs.close_packet(ctx->data);
188 ctx->use_cur_last_event_ts = 0;
189
190 /* Is the back end full? */
191 if (ctx->cbs.is_backend_full(ctx->data)) {
192 /* Yes: discard event record */
193 goto no_space;
194 }
195
196 /* Back-end is _not_ full: open new packet */
197 ctx->use_cur_last_event_ts = 1;
198 ctx->cbs.open_packet(ctx->data);
199 ctx->use_cur_last_event_ts = 0;
200 assert(er_size <= (ctx->packet_size - ctx->at));
201 }
202
203 ret = 1;
204 goto end;
205
206 no_space:
207 ctx->events_discarded++;
208 ret = 0;
209
210 end:
211 return ret;
212 }
213
214 static
215 void _commit_er(void * const vctx)
216 {
217 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
218
219 /* Is the packet full? */
220 if ({{ prefix }}packet_is_full(ctx)) {
221 /* Yes: close it now */
222 ctx->cbs.close_packet(ctx->data);
223 }
224 }
225
226 {% include 'c/ctx-init-func-proto.j2' %}
227
228 {
229 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
230 ctx->cbs = cbs;
231 ctx->data = data;
232 ctx->buf = buf;
233 ctx->packet_size = _BYTES_TO_BITS(buf_size);
234 ctx->at = 0;
235 ctx->events_discarded = 0;
236 ctx->packet_is_open = 0;
237 ctx->in_tracing_section = 0;
238 ctx->is_tracing_enabled = 1;
239 ctx->use_cur_last_event_ts = 0;
240 }
241
242 {% for dst in cfg.trace.type.data_stream_types | sort %}
243 {% set def_clk_type = dst.default_clock_type %}
244 {% set sctx_name %}{{ prefix }}{{ dst.name }}{% endset %}
245 {% set this_ds_ops = ds_ops[dst] %}
246 {% include 'c/open-func-proto.j2' %}
247
248 {
249 {{ macros.open_close_func_preamble(dst, dst.features.packet_features.beginning_timestamp_field_type) | indent_tab }}
250
251 /*
252 * This function is either called by a tracing function, or
253 * directly by the platform.
254 *
255 * If it's called by a tracing function, then
256 * `ctx->in_tracing_section` is 1, so it's safe to open
257 * the packet here (alter the packet), even if tracing was
258 * disabled in the meantime because we're already in a tracing
259 * section (which finishes at the end of the tracing function
260 * call).
261 *
262 * If it's called directly by the platform, then if tracing is
263 * disabled, we don't want to alter the packet, and return
264 * immediately.
265 */
266 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
267 ctx->in_tracing_section = 0;
268 goto end;
269 }
270
271 /* We can alter the packet */
272 ctx->in_tracing_section = 1;
273
274 /* Do not open a packet that is already open */
275 if (ctx->packet_is_open) {
276 ctx->in_tracing_section = saved_in_tracing_section;
277 goto end;
278 }
279
280 ctx->at = 0;
281 {% set pkt_header_op = this_ds_ops.pkt_header_op %}
282 {% if pkt_header_op %}
283
284 {{ pkt_header_op.serialize_str(dst=dst) | indent_tab }}
285 {% endif %}
286
287 {{ this_ds_ops.pkt_ctx_op.serialize_str(dst=dst) | indent_tab }}
288
289 /* Save content beginning's offset */
290 ctx->off_content = ctx->at;
291
292 /* Mark current packet as open */
293 ctx->packet_is_open = 1;
294
295 /* Not tracing anymore */
296 ctx->in_tracing_section = saved_in_tracing_section;
297
298 end:
299 return;
300 }
301
302 {% include 'c/close-func-proto.j2' %}
303
304 {
305 {{ macros.open_close_func_preamble(dst, dst.features.packet_features.end_timestamp_field_type) | indent_tab }}
306
307 /*
308 * This function is either called by a tracing function, or
309 * directly by the platform.
310 *
311 * If it's called by a tracing function, then
312 * `ctx->in_tracing_section` is 1, so it's safe to close
313 * the packet here (alter the packet), even if tracing was
314 * disabled in the meantime, because we're already in a tracing
315 * section (which finishes at the end of the tracing function
316 * call).
317 *
318 * If it's called directly by the platform, then if tracing is
319 * disabled, we don't want to alter the packet, and return
320 * immediately.
321 */
322 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
323 ctx->in_tracing_section = 0;
324 goto end;
325 }
326
327 /* We can alter the packet */
328 ctx->in_tracing_section = 1;
329
330 /* Do not close a packet that is not open */
331 if (!ctx->packet_is_open) {
332 ctx->in_tracing_section = saved_in_tracing_section;
333 goto end;
334 }
335
336 /* Save content size */
337 ctx->content_size = ctx->at;
338 {% set name = 'timestamp_end' %}
339 {% if name in dst._pkt_ctx_ft.members %}
340 {% set op = ds_op_pkt_ctx_op(dst, name) %}
341
342 /* Go back to `timestamp_end` field offset */
343 ctx->at = sctx->off_{{ op | op_src_var_name }};
344
345 {% set src = 'ts' %}
346 {% filter indent_tab(indent_first=true) %}
347 {% include 'c/serialize-write-saved-int-statements.j2' %}
348
349 {% endfilter %}
350 {% endif %}
351 {% set name = 'content_size' %}
352 {% if name in dst._pkt_ctx_ft.members %}
353 {% set op = ds_op_pkt_ctx_op(dst, name) %}
354
355 /* Go back to `content_size` field offset */
356 ctx->at = sctx->off_{{ op | op_src_var_name }};
357
358 {% set src %}ctx->{{ name }}{% endset %}
359 {% filter indent_tab(indent_first=true) %}
360 {% include 'c/serialize-write-saved-int-statements.j2' %}
361
362 {% endfilter %}
363 {% endif %}
364 {% set name = 'events_discarded' %}
365 {% if name in dst._pkt_ctx_ft.members %}
366 {% set op = ds_op_pkt_ctx_op(dst, name) %}
367
368 /* Go back to `events_discarded` field offset */
369 ctx->at = sctx->off_{{ op | op_src_var_name }};
370
371 {% set src %}ctx->{{ name }}{% endset %}
372 {% filter indent_tab(indent_first=true) %}
373 {% include 'c/serialize-write-saved-int-statements.j2' %}
374
375 {% endfilter %}
376 {% endif %}
377
378 /* Go back to end of packet */
379 ctx->at = ctx->packet_size;
380
381 /* Mark packet as closed */
382 ctx->packet_is_open = 0;
383
384 /* Not tracing anymore */
385 ctx->in_tracing_section = saved_in_tracing_section;
386
387 end:
388 return;
389 }
390
391 {% if dst._er_header_ft %}
392 static void _serialize_er_header_{{ dst.name }}(void * const vctx,
393 const uint32_t ert_id)
394 {
395 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
396 {% if def_clk_type and dst.features.event_record_features.timestamp_field_type %}
397 struct {{ sctx_name }}_ctx * const sctx = _FROM_VOID_PTR(struct {{ sctx_name }}_ctx, vctx);
398 const {{ cg_opts.clock_type_c_types[def_clk_type] }} ts = sctx->cur_last_event_ts;
399 {% endif %}
400
401 {{ this_ds_ops.er_header_op.serialize_str(dst=dst) | indent_tab }}
402 }
403
404 {% endif %}
405 {% if dst.event_record_common_context_field_type %}
406 static void _serialize_er_common_ctx_{{ dst.name }}(void * const vctx{{ dst | serialize_er_common_ctx_func_params_str(const_params) }})
407 {
408 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
409
410 {{ this_ds_ops.er_common_ctx_op.serialize_str(dst=dst) | indent_tab }}
411 }
412
413 {% endif %}
414 {# internal serialization functions #}
415 {% for ert in dst.event_record_types | sort %}
416 static void _serialize_er_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params) }})
417 {
418 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
419 {% if dst._er_header_ft %}
420
421 /* Serialize header */
422 _serialize_er_header_{{ dst.name }}(ctx, {{ ert.id }});
423 {% endif %}
424 {% if dst.event_record_common_context_field_type %}
425
426 /* Serialize common context */
427 {% set params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type) %}
428 _serialize_er_common_ctx_{{ dst.name }}(ctx{{ params }});
429 {% endif %}
430 {% set this_er_ops = this_ds_ops.er_ops[ert] %}
431 {% if this_er_ops.spec_ctx_op %}
432
433 {{ this_er_ops.spec_ctx_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
434 {% endif %}
435 {% if this_er_ops.payload_op %}
436
437 {{ this_er_ops.payload_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
438 {% endif %}
439 }
440
441 {% endfor %}
442 {# internal size functions #}
443 {% for ert in dst.event_record_types | sort %}
444 {% set this_er_ops = this_ds_ops.er_ops[ert] %}
445 static uint32_t _er_size_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params, only_dyn=true) }})
446 {
447 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
448 uint32_t at = ctx->at;
449 {% if this_ds_ops.er_header_op %}
450
451 {{ this_ds_ops.er_header_op.size_str(dst=dst) | indent_tab }}
452 {% endif %}
453 {% if this_ds_ops.er_common_ctx_op %}
454
455 {{ this_ds_ops.er_common_ctx_op.size_str(dst=dst) | indent_tab }}
456 {% endif %}
457 {% if this_er_ops.spec_ctx_op %}
458
459 {{ this_er_ops.spec_ctx_op.size_str(dst=dst, ert=ert) | indent_tab }}
460 {% endif %}
461 {% if this_er_ops.payload_op %}
462
463 {{ this_er_ops.payload_op.size_str(dst=dst, ert=ert) | indent_tab }}
464 {% endif %}
465
466 return at - ctx->at;
467 }
468
469 {% endfor %}
470 {# public tracing functions #}
471 {% for ert in dst.event_record_types | sort %}
472 {% include 'c/trace-func-proto.j2' %}
473
474 {
475 struct {{ ctx_struct_name }} * const ctx = &sctx->parent;
476 uint32_t er_size;
477
478 {% if def_clk_type %}
479 /* Save timestamp */
480 sctx->cur_last_event_ts = ctx->cbs.{{ def_clk_type.name }}_clock_get_value(ctx->data);
481
482 {% endif %}
483 if (!ctx->is_tracing_enabled) {
484 goto end;
485 }
486
487 /* We can alter the packet */
488 ctx->in_tracing_section = 1;
489
490 /* Compute event record size */
491 {% set er_common_ctx_params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type, true) %}
492 {% set spec_ctx_params = macros.ft_call_params(root_ft_prefixes.ERSC, ert.specific_context_field_type, true) %}
493 {% set payload_params = macros.ft_call_params(root_ft_prefixes.ERP, ert.payload_field_type, true) %}
494 {% set params %}{{ er_common_ctx_params }}{{ spec_ctx_params }}{{ payload_params }}{% endset %}
495 er_size = _er_size_{{ dst.name }}_{{ ert.name }}(_TO_VOID_PTR(ctx){{ params }});
496
497 /* Is there enough space to serialize? */
498 if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
499 /* no: forget this */
500 ctx->in_tracing_section = 0;
501 goto end;
502 }
503
504 /* Serialize event record */
505 {% set er_common_ctx_params = macros.ft_call_params(root_ft_prefixes.ERCC, dst.event_record_common_context_field_type) %}
506 {% set spec_ctx_params = macros.ft_call_params(root_ft_prefixes.ERSC, ert.specific_context_field_type) %}
507 {% set payload_params = macros.ft_call_params(root_ft_prefixes.ERP, ert.payload_field_type) %}
508 {% set params %}{{ er_common_ctx_params }}{{ spec_ctx_params }}{{ payload_params }}{% endset %}
509 _serialize_er_{{ dst.name }}_{{ ert.name }}(_TO_VOID_PTR(ctx){{ params }});
510
511 /* Commit event record */
512 _commit_er(_TO_VOID_PTR(ctx));
513
514 /* Not tracing anymore */
515 ctx->in_tracing_section = 0;
516
517 end:
518 return;
519 }
520 {% if not loop.last %}{{ '\n' }}{% endif %}
521 {% endfor %}
522 {% endfor %}
This page took 0.040074 seconds and 3 git commands to generate.