2 # The MIT License (MIT)
4 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
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:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
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.
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' %}
40 #include "{{ header_file_name }}"
41 #include "{{ bitfield_header_file_name }}"
43 #define _ALIGN(_at_var, _align) \
45 (_at_var) = ((_at_var) + ((_align) - 1)) & -(_align); \
49 # define _TO_VOID_PTR(_value) static_cast<void *>(_value)
50 # define _FROM_VOID_PTR(_type, _value) static_cast<_type *>(_value)
52 # define _TO_VOID_PTR(_value) ((void *) (_value))
53 # define _FROM_VOID_PTR(_type, _value) ((_type *) (_value))
56 #define _BITS_TO_BYTES(_x) ((_x) >> 3)
57 #define _BYTES_TO_BITS(_x) ((_x) << 3)
69 uint32_t {{ prefix }}packet_size(const void * const vctx)
71 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_size;
74 int {{ prefix }}packet_is_full(const void * const vctx)
76 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
78 return ctx->at == ctx->packet_size;
81 int {{ prefix }}packet_is_empty(const void * const vctx)
83 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
85 return ctx->at <= ctx->off_content;
88 uint32_t {{ prefix }}packet_events_discarded(const void * const vctx)
90 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->events_discarded;
93 uint32_t {{ prefix }}discarded_event_records_count(const void * const vctx)
95 return {{ prefix }}packet_events_discarded(vctx);
98 uint8_t *{{ prefix }}packet_buf(const void * const vctx)
100 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->buf;
103 uint8_t *{{ prefix }}packet_buf_addr(const void * const vctx)
105 return {{ prefix }}packet_buf(vctx);
108 uint32_t {{ prefix }}packet_buf_size(const void * const vctx)
110 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
112 return _BITS_TO_BYTES(ctx->packet_size);
115 void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf,
116 const uint32_t buf_size)
118 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
121 ctx->packet_size = _BYTES_TO_BITS(buf_size);
124 int {{ prefix }}packet_is_open(const void * const vctx)
126 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_is_open;
129 int {{ prefix }}is_in_tracing_section(const void * const vctx)
131 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
134 volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void * const vctx)
136 return &_FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
139 int {{ prefix }}is_tracing_enabled(const void * const vctx)
141 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled;
144 void {{ prefix }}enable_tracing(void * const vctx, const int enable)
146 _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled = enable;
150 void _write_c_str(struct {{ ctx_struct_name }} * const ctx, const char * const src)
152 const uint32_t sz = strlen(src) + 1;
154 memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz);
155 ctx->at += _BYTES_TO_BITS(sz);
159 int _reserve_er_space(void * const vctx, const uint32_t er_size)
162 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
164 /* Event _cannot_ fit? */
165 if (er_size > (ctx->packet_size - ctx->off_content)) {
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 */
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;
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;
190 /* Is the back end full? */
191 if (ctx->cbs.is_backend_full(ctx->data)) {
192 /* Yes: discard event record */
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));
207 ctx->events_discarded++;
215 void _commit_er(void * const vctx)
217 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
219 /* Is the packet full? */
220 if ({{ prefix }}packet_is_full(ctx)) {
221 /* Yes: close it now */
222 ctx->cbs.close_packet(ctx->data);
226 {% include 'c/ctx-init-func-proto.j2' %}
229 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
233 ctx->packet_size = _BYTES_TO_BITS(buf_size);
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;
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' %}
249 {{ macros.open_close_func_preamble(dst, dst.features.packet_features.beginning_timestamp_field_type) | indent_tab }}
252 * This function is either called by a tracing function, or
253 * directly by the platform.
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
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
266 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
267 ctx->in_tracing_section = 0;
271 /* We can alter the packet */
272 ctx->in_tracing_section = 1;
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;
281 {% set pkt_header_op = this_ds_ops.pkt_header_op %}
282 {% if pkt_header_op %}
284 {{ pkt_header_op.serialize_str(dst=dst) | indent_tab }}
287 {{ this_ds_ops.pkt_ctx_op.serialize_str(dst=dst) | indent_tab }}
289 /* Save content beginning's offset */
290 ctx->off_content = ctx->at;
292 /* Mark current packet as open */
293 ctx->packet_is_open = 1;
295 /* Not tracing anymore */
296 ctx->in_tracing_section = saved_in_tracing_section;
302 {% include 'c/close-func-proto.j2' %}
305 {{ macros.open_close_func_preamble(dst, dst.features.packet_features.end_timestamp_field_type) | indent_tab }}
308 * This function is either called by a tracing function, or
309 * directly by the platform.
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
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
322 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
323 ctx->in_tracing_section = 0;
327 /* We can alter the packet */
328 ctx->in_tracing_section = 1;
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;
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) %}
342 /* Go back to `timestamp_end` field offset */
343 ctx->at = sctx->off_{{ op | op_src_var_name }};
346 {% filter indent_tab(indent_first=true) %}
347 {% include 'c/serialize-write-saved-int-statements.j2' %}
351 {% set name = 'content_size' %}
352 {% if name in dst._pkt_ctx_ft.members %}
353 {% set op = ds_op_pkt_ctx_op(dst, name) %}
355 /* Go back to `content_size` field offset */
356 ctx->at = sctx->off_{{ op | op_src_var_name }};
358 {% set src %}ctx->{{ name }}{% endset %}
359 {% filter indent_tab(indent_first=true) %}
360 {% include 'c/serialize-write-saved-int-statements.j2' %}
364 {% set name = 'events_discarded' %}
365 {% if name in dst._pkt_ctx_ft.members %}
366 {% set op = ds_op_pkt_ctx_op(dst, name) %}
368 /* Go back to `events_discarded` field offset */
369 ctx->at = sctx->off_{{ op | op_src_var_name }};
371 {% set src %}ctx->{{ name }}{% endset %}
372 {% filter indent_tab(indent_first=true) %}
373 {% include 'c/serialize-write-saved-int-statements.j2' %}
378 /* Go back to end of packet */
379 ctx->at = ctx->packet_size;
381 /* Mark packet as closed */
382 ctx->packet_is_open = 0;
384 /* Not tracing anymore */
385 ctx->in_tracing_section = saved_in_tracing_section;
391 {% if dst._er_header_ft %}
392 static void _serialize_er_header_{{ dst.name }}(void * const vctx,
393 const uint32_t ert_id)
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;
401 {{ this_ds_ops.er_header_op.serialize_str(dst=dst) | indent_tab }}
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) }})
408 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
410 {{ this_ds_ops.er_common_ctx_op.serialize_str(dst=dst) | indent_tab }}
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) }})
418 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
419 {% if dst._er_header_ft %}
421 /* Serialize header */
422 _serialize_er_header_{{ dst.name }}(ctx, {{ ert.id }});
424 {% if dst.event_record_common_context_field_type %}
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 }});
430 {% set this_er_ops = this_ds_ops.er_ops[ert] %}
431 {% if this_er_ops.spec_ctx_op %}
433 {{ this_er_ops.spec_ctx_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
435 {% if this_er_ops.payload_op %}
437 {{ this_er_ops.payload_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
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) }})
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 %}
451 {{ this_ds_ops.er_header_op.size_str(dst=dst) | indent_tab }}
453 {% if this_ds_ops.er_common_ctx_op %}
455 {{ this_ds_ops.er_common_ctx_op.size_str(dst=dst) | indent_tab }}
457 {% if this_er_ops.spec_ctx_op %}
459 {{ this_er_ops.spec_ctx_op.size_str(dst=dst, ert=ert) | indent_tab }}
461 {% if this_er_ops.payload_op %}
463 {{ this_er_ops.payload_op.size_str(dst=dst, ert=ert) | indent_tab }}
470 {# public tracing functions #}
471 {% for ert in dst.event_record_types | sort %}
472 {% include 'c/trace-func-proto.j2' %}
475 struct {{ ctx_struct_name }} * const ctx = &sctx->parent;
478 {% if def_clk_type %}
480 sctx->cur_last_event_ts = ctx->cbs.{{ def_clk_type.name }}_clock_get_value(ctx->data);
483 if (!ctx->is_tracing_enabled) {
487 /* We can alter the packet */
488 ctx->in_tracing_section = 1;
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 }});
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;
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 }});
511 /* Commit event record */
512 _commit_er(_TO_VOID_PTR(ctx));
514 /* Not tracing anymore */
515 ctx->in_tracing_section = 0;
520 {% if not loop.last %}{{ '\n' }}{% endif %}