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 'licence-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 ctx)
71 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, ctx)->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 uint8_t *{{ prefix }}packet_buf(const void * const vctx)
95 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->buf;
98 uint32_t {{ prefix }}packet_buf_size(const void * const vctx)
100 const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx);
102 return _BITS_TO_BYTES(ctx->packet_size);
105 void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf,
106 const uint32_t buf_size)
108 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
111 ctx->packet_size = _BYTES_TO_BITS(buf_size);
114 int {{ prefix }}packet_is_open(const void * const vctx)
116 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_is_open;
119 int {{ prefix }}is_in_tracing_section(const void * const vctx)
121 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
124 volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void * const vctx)
126 return &_FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section;
129 int {{ prefix }}is_tracing_enabled(const void * const vctx)
131 return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled;
134 void {{ prefix }}enable_tracing(void * const vctx, const int enable)
136 _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled = enable;
140 void _write_c_str(struct {{ ctx_struct_name }} * const ctx, const char * const src)
142 const uint32_t sz = strlen(src) + 1;
144 memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz);
145 ctx->at += _BYTES_TO_BITS(sz);
149 int _reserve_ev_space(void * const vctx, const uint32_t ev_size)
151 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
153 /* Event _cannot_ fit? */
154 if (ev_size > (ctx->packet_size - ctx->off_content)) {
155 ctx->events_discarded++;
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 */
164 ctx->events_discarded++;
168 /* Back-end is _not_ full: open new packet */
169 ctx->use_cur_last_event_ts = 1;
170 ctx->cbs.open_packet(ctx->data);
171 ctx->use_cur_last_event_ts = 0;
174 /* Event fits the current packet? */
175 if (ev_size > (ctx->packet_size - ctx->at)) {
176 /* No: close packet now */
177 ctx->use_cur_last_event_ts = 1;
178 ctx->cbs.close_packet(ctx->data);
179 ctx->use_cur_last_event_ts = 0;
181 /* Is the back-end full? */
182 if (ctx->cbs.is_backend_full(ctx->data)) {
183 /* Yes: discard event */
184 ctx->events_discarded++;
188 /* Back-end is _not_ full: open new packet */
189 ctx->use_cur_last_event_ts = 1;
190 ctx->cbs.open_packet(ctx->data);
191 ctx->use_cur_last_event_ts = 0;
192 assert(ev_size <= (ctx->packet_size - ctx->at));
199 void _commit_ev(void * const vctx)
201 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
203 /* Is the packet full? */
204 if ({{ prefix }}packet_is_full(ctx)) {
205 /* Yes: close it now */
206 ctx->cbs.close_packet(ctx->data);
210 {% include 'c/ctx-init-func-proto.j2' %}
213 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
217 ctx->packet_size = _BYTES_TO_BITS(buf_size);
219 ctx->events_discarded = 0;
220 ctx->packet_is_open = 0;
221 ctx->in_tracing_section = 0;
222 ctx->is_tracing_enabled = 1;
223 ctx->use_cur_last_event_ts = 0;
226 {% for stream_type in cfg.trace.type.stream_types | sort %}
227 {% set def_clk_type = stream_type.default_clock_type %}
228 {% set sctx_name %}{{ prefix }}{{ stream_type.name }}{% endset %}
229 {% set this_stream_ops = stream_ops[stream_type] %}
230 {% include 'c/open-func-proto.j2' %}
233 {{ macros.open_close_func_preamble(stream_type) | indent_tab }}
236 * This function is either called by a tracing function, or
237 * directly by the platform.
239 * If it's called by a tracing function, then
240 * `ctx->in_tracing_section` is 1, so it's safe to open
241 * the packet here (alter the packet), even if tracing was
242 * disabled in the meantime because we're already in a tracing
243 * section (which finishes at the end of the tracing function
246 * If it's called directly by the platform, then if tracing is
247 * disabled, we don't want to alter the packet, and return
250 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
251 ctx->in_tracing_section = 0;
255 /* We can alter the packet */
256 ctx->in_tracing_section = 1;
258 /* Do not open a packet that is already open */
259 if (ctx->packet_is_open) {
260 ctx->in_tracing_section = saved_in_tracing_section;
265 {% set pkt_header_ops = this_stream_ops.pkt_header_ops %}
266 {% if pkt_header_ops %}
268 /* Serialize packet header */
270 {% for op in pkt_header_ops %}
271 {{ op.serialize_str(stream_type=stream_type) | indent_tab(2) }}
277 /* Serialize packet context */
279 {% for op in this_stream_ops.pkt_ctx_ops %}
280 {{ op.serialize_str(stream_type=stream_type) | indent_tab(2) }}
285 /* Save content beginning's offset */
286 ctx->off_content = ctx->at;
288 /* Mark current packet as open */
289 ctx->packet_is_open = 1;
291 /* Not tracing anymore */
292 ctx->in_tracing_section = saved_in_tracing_section;
295 {% include 'c/close-func-proto.j2' %}
298 {{ macros.open_close_func_preamble(stream_type) | indent_tab }}
301 * This function is either called by a tracing function, or
302 * directly by the platform.
304 * If it's called by a tracing function, then
305 * `ctx->in_tracing_section` is 1, so it's safe to close
306 * the packet here (alter the packet), even if tracing was
307 * disabled in the meantime, because we're already in a tracing
308 * section (which finishes at the end of the tracing function
311 * If it's called directly by the platform, then if tracing is
312 * disabled, we don't want to alter the packet, and return
315 if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
316 ctx->in_tracing_section = 0;
320 /* We can alter the packet */
321 ctx->in_tracing_section = 1;
323 /* Do not close a packet that is not open */
324 if (!ctx->packet_is_open) {
325 ctx->in_tracing_section = saved_in_tracing_section;
329 /* Save content size */
330 ctx->content_size = ctx->at;
331 {% set name = 'timestamp_end' %}
332 {% if name in stream_type._pkt_ctx_ft.members %}
333 {% set op = stream_op_pkt_ctx_op(stream_type, name) %}
335 /* Go back to `timestamp_end` field offset */
336 ctx->at = sctx->off_{{ c_common.op_src(op) }};
339 {% filter indent_tab(indent_first=true) %}
340 {% include 'c/serialize-write-saved-int-statements.j2' %}
344 {% set name = 'content_size' %}
345 {% if name in stream_type._pkt_ctx_ft.members %}
346 {% set op = stream_op_pkt_ctx_op(stream_type, name) %}
348 /* Go back to `content_size` field offset */
349 ctx->at = sctx->off_{{ c_common.op_src(op) }};
351 {% set src %}ctx->{{ name }}{% endset %}
352 {% filter indent_tab(indent_first=true) %}
353 {% include 'c/serialize-write-saved-int-statements.j2' %}
357 {% set name = 'events_discarded' %}
358 {% if name in stream_type._pkt_ctx_ft.members %}
359 {% set op = stream_op_pkt_ctx_op(stream_type, name) %}
361 /* Go back to `events_discarded` field offset */
362 ctx->at = sctx->off_{{ c_common.op_src(op) }};
364 {% set src %}ctx->{{ name }}{% endset %}
365 {% filter indent_tab(indent_first=true) %}
366 {% include 'c/serialize-write-saved-int-statements.j2' %}
371 /* Go back to end of packet */
372 ctx->at = ctx->packet_size;
374 /* Mark packet as closed */
375 ctx->packet_is_open = 0;
377 /* Not tracing anymore */
378 ctx->in_tracing_section = saved_in_tracing_section;
380 {% if stream_type._ev_header_ft %}
382 static void _serialize_ev_header_{{ stream_type.name }}(void * const vctx,
383 const uint32_t ev_type_id)
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;
391 /* Serialize event header */
393 {% for op in this_stream_ops.ev_header_ops %}
394 {{ op.serialize_str(stream_type=stream_type) | indent_tab(2) }}
400 {% if stream_type.event_common_context_field_type %}
402 static void _serialize_ev_common_ctx_{{ stream_type.name }}(void * const vctx{{ stream_type | serialize_ev_common_ctx_func_params_str(const_params) }})
404 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
406 /* Serialize event common context */
408 {% for op in this_stream_ops.ev_common_ctx_ops %}
409 {{ op.serialize_str(stream_type=stream_type) | indent_tab(2) }}
415 {# internal serialization functions #}
416 {% for ev_type in stream_type.event_types | sort %}
418 static void _serialize_ev_{{ stream_type.name }}_{{ ev_type.name }}(void * const vctx{{ (stream_type, ev_type) | trace_func_params_str(const_params) }})
420 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
421 {% if stream_type._ev_header_ft %}
423 /* Serialize header */
424 _serialize_ev_header_{{ stream_type.name }}(ctx, {{ ev_type.id }});
426 {% if stream_type.event_common_context_field_type %}
428 /* Serialize common context */
429 {% set params = macros.ft_call_params(root_ft_prefixes.ECC, stream_type.event_common_context_field_type) %}
430 _serialize_ev_common_ctx_{{ stream_type.name }}(ctx{{ params }});
432 {% set this_ev_ops = this_stream_ops.ev_ops[ev_type] %}
433 {% if this_ev_ops.spec_ctx_ops %}
435 /* Serialize specific context */
437 {% for op in this_ev_ops.spec_ctx_ops %}
438 {{ op.serialize_str(stream_type=stream_type, ev_type=ev_type) | indent_tab(2) }}
443 {% if this_ev_ops.payload_ops %}
445 /* Serialize payload */
447 {% for op in this_ev_ops.payload_ops %}
448 {{ op.serialize_str(stream_type=stream_type, ev_type=ev_type) | indent_tab(2) }}
455 {# internal size functions #}
456 {% for ev_type in stream_type.event_types | sort %}
457 {% set this_ev_ops = this_stream_ops.ev_ops[ev_type] %}
459 static uint32_t _ev_size_{{ stream_type.name }}_{{ ev_type.name }}(void * const vctx{{ (stream_type, ev_type) | trace_func_params_str(const_params) }})
461 struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
462 uint32_t at = ctx->at;
463 {% if this_stream_ops.ev_header_ops %}
465 /* Add header size */
467 {% for op in this_stream_ops.ev_header_ops %}
468 {{ op.size_str(stream_type=stream_type) | indent_tab(2) }}
473 {% if this_stream_ops.ev_common_ctx_ops %}
475 /* Add common context size */
477 {% for op in this_stream_ops.ev_common_ctx_ops %}
478 {{ op.size_str(stream_type=stream_type) | indent_tab(2) }}
483 {% if this_ev_ops.spec_ctx_ops %}
485 /* Add specific context size */
487 {% for op in this_ev_ops.spec_ctx_ops %}
488 {{ op.size_str(stream_type=stream_type, ev_type=ev_type) | indent_tab(2) }}
493 {% if this_ev_ops.payload_ops %}
495 /* Add payload size */
497 {% for op in this_ev_ops.payload_ops %}
498 {{ op.size_str(stream_type=stream_type, ev_type=ev_type) | indent_tab(2) }}
507 {# public tracing functions #}
508 {% for ev_type in stream_type.event_types | sort %}
510 {% include 'c/trace-func-proto.j2' %}
513 struct {{ ctx_struct_name }} * const ctx = &sctx->parent;
516 {% if def_clk_type %}
518 sctx->cur_last_event_ts = ctx->cbs.{{ def_clk_type.name }}_clock_get_value(ctx->data);
522 if (!ctx->is_tracing_enabled) {
526 /* We can alter the packet */
527 ctx->in_tracing_section = 1;
529 /* Compute event size */
530 {% set ev_common_ctx_params = macros.ft_call_params(root_ft_prefixes.ECC, stream_type.event_common_context_field_type) %}
531 {% set spec_ctx_params = macros.ft_call_params(root_ft_prefixes.SC, ev_type.specific_context_field_type) %}
532 {% set payload_params = macros.ft_call_params(root_ft_prefixes.P, ev_type.payload_field_type) %}
533 {% set params %}{{ ev_common_ctx_params }}{{ spec_ctx_params }}{{ payload_params }}{% endset %}
534 ev_size = _ev_size_{{ stream_type.name }}_{{ ev_type.name }}(_TO_VOID_PTR(ctx){{ params }});
536 /* Is there enough space to serialize? */
537 if (!_reserve_ev_space(_TO_VOID_PTR(ctx), ev_size)) {
538 /* no: forget this */
539 ctx->in_tracing_section = 0;
543 /* Serialize event */
544 _serialize_ev_{{ stream_type.name }}_{{ ev_type.name }}(_TO_VOID_PTR(ctx){{ params }});
547 _commit_ev(_TO_VOID_PTR(ctx));
549 /* Not tracing anymore */
550 ctx->in_tracing_section = 0;