Make API CTF-agnostic
[babeltrace.git] / plugins / ctf / common / metadata / ctf-meta-update-default-clock-classes.c
1 /*
2 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 */
14
15 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-UPDATE-DEF-CC"
16 #include "logging.h"
17
18 #include <babeltrace/babeltrace.h>
19 #include <babeltrace/babeltrace-internal.h>
20 #include <babeltrace/assert-internal.h>
21 #include <glib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <inttypes.h>
25
26 #include "ctf-meta-visitors.h"
27
28 static inline
29 int find_mapped_clock_class(struct ctf_field_type *ft,
30 struct bt_clock_class **clock_class)
31 {
32 int ret = 0;
33 uint64_t i;
34
35 if (!ft) {
36 goto end;
37 }
38
39 switch (ft->id) {
40 case CTF_FIELD_TYPE_ID_INT:
41 case CTF_FIELD_TYPE_ID_ENUM:
42 {
43 struct ctf_field_type_int *int_ft = (void *) ft;
44
45 if (int_ft->mapped_clock_class) {
46 if (*clock_class && *clock_class !=
47 int_ft->mapped_clock_class) {
48 BT_LOGE("Stream class contains more than one "
49 "clock class: expected-cc-name=\"%s\", "
50 "other-cc-name=\"%s\"",
51 bt_clock_class_get_name(*clock_class),
52 bt_clock_class_get_name(int_ft->mapped_clock_class));
53 ret = -1;
54 goto end;
55 }
56
57 *clock_class = int_ft->mapped_clock_class;
58 }
59
60 break;
61 }
62 case CTF_FIELD_TYPE_ID_STRUCT:
63 {
64 struct ctf_field_type_struct *struct_ft = (void *) ft;
65
66 for (i = 0; i < struct_ft->members->len; i++) {
67 struct ctf_named_field_type *named_ft =
68 ctf_field_type_struct_borrow_member_by_index(
69 struct_ft, i);
70
71 ret = find_mapped_clock_class(named_ft->ft,
72 clock_class);
73 if (ret) {
74 goto end;
75 }
76 }
77
78 break;
79 }
80 case CTF_FIELD_TYPE_ID_VARIANT:
81 {
82 struct ctf_field_type_variant *var_ft = (void *) ft;
83
84 for (i = 0; i < var_ft->options->len; i++) {
85 struct ctf_named_field_type *named_ft =
86 ctf_field_type_variant_borrow_option_by_index(
87 var_ft, i);
88
89 ret = find_mapped_clock_class(named_ft->ft,
90 clock_class);
91 if (ret) {
92 goto end;
93 }
94 }
95
96 break;
97 }
98 case CTF_FIELD_TYPE_ID_ARRAY:
99 case CTF_FIELD_TYPE_ID_SEQUENCE:
100 {
101 struct ctf_field_type_array_base *array_ft = (void *) ft;
102
103 ret = find_mapped_clock_class(array_ft->elem_ft, clock_class);
104 if (ret) {
105 goto end;
106 }
107
108 break;
109 }
110 default:
111 break;
112 }
113
114 end:
115 return ret;
116 }
117
118 static inline
119 int update_stream_class_default_clock_class(
120 struct ctf_stream_class *stream_class)
121 {
122 int ret = 0;
123 struct bt_clock_class *clock_class = stream_class->default_clock_class;
124 uint64_t i;
125
126 ret = find_mapped_clock_class(stream_class->packet_context_ft,
127 &clock_class);
128 if (ret) {
129 goto end;
130 }
131
132 ret = find_mapped_clock_class(stream_class->event_header_ft,
133 &clock_class);
134 if (ret) {
135 goto end;
136 }
137
138 ret = find_mapped_clock_class(stream_class->event_common_context_ft,
139 &clock_class);
140 if (ret) {
141 goto end;
142 }
143
144 for (i = 0; i < stream_class->event_classes->len; i++) {
145 struct ctf_event_class *event_class =
146 stream_class->event_classes->pdata[i];
147
148 ret = find_mapped_clock_class(event_class->spec_context_ft,
149 &clock_class);
150 if (ret) {
151 goto end;
152 }
153
154 ret = find_mapped_clock_class(event_class->payload_ft,
155 &clock_class);
156 if (ret) {
157 goto end;
158 }
159 }
160
161 if (!stream_class->default_clock_class) {
162 stream_class->default_clock_class = bt_get(clock_class);
163 }
164
165 end:
166 return ret;
167 }
168
169 BT_HIDDEN
170 int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc)
171 {
172 uint64_t i;
173 int ret = 0;
174 struct bt_clock_class *clock_class = NULL;
175
176 ret = find_mapped_clock_class(ctf_tc->packet_header_ft,
177 &clock_class);
178 if (ret) {
179 goto end;
180 }
181
182 if (clock_class) {
183 ret = -1;
184 goto end;
185 }
186
187 for (i = 0; i < ctf_tc->stream_classes->len; i++) {
188 struct ctf_stream_class *sc =
189 ctf_tc->stream_classes->pdata[i];
190
191 ret = update_stream_class_default_clock_class(
192 ctf_tc->stream_classes->pdata[i]);
193 if (ret) {
194 BT_LOGE("Stream class contains more than one "
195 "clock class: stream-class-id=%" PRIu64,
196 sc->id);
197 goto end;
198 }
199 }
200
201 end:
202 return ret;
203 }
This page took 0.033194 seconds and 4 git commands to generate.