Trace IR and notification APIs: split into private and public APIs
[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_class *fc,
30 struct bt_private_clock_class **clock_class)
31 {
32 int ret = 0;
33 uint64_t i;
34
35 if (!fc) {
36 goto end;
37 }
38
39 switch (fc->type) {
40 case CTF_FIELD_CLASS_TYPE_INT:
41 case CTF_FIELD_CLASS_TYPE_ENUM:
42 {
43 struct ctf_field_class_int *int_fc = (void *) fc;
44
45 if (int_fc->mapped_clock_class) {
46 if (*clock_class && *clock_class !=
47 int_fc->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(
52 bt_clock_class_borrow_from_private(
53 *clock_class)),
54 bt_clock_class_get_name(
55 bt_clock_class_borrow_from_private(
56 int_fc->mapped_clock_class)));
57 ret = -1;
58 goto end;
59 }
60
61 *clock_class = int_fc->mapped_clock_class;
62 }
63
64 break;
65 }
66 case CTF_FIELD_CLASS_TYPE_STRUCT:
67 {
68 struct ctf_field_class_struct *struct_fc = (void *) fc;
69
70 for (i = 0; i < struct_fc->members->len; i++) {
71 struct ctf_named_field_class *named_fc =
72 ctf_field_class_struct_borrow_member_by_index(
73 struct_fc, i);
74
75 ret = find_mapped_clock_class(named_fc->fc,
76 clock_class);
77 if (ret) {
78 goto end;
79 }
80 }
81
82 break;
83 }
84 case CTF_FIELD_CLASS_TYPE_VARIANT:
85 {
86 struct ctf_field_class_variant *var_fc = (void *) fc;
87
88 for (i = 0; i < var_fc->options->len; i++) {
89 struct ctf_named_field_class *named_fc =
90 ctf_field_class_variant_borrow_option_by_index(
91 var_fc, i);
92
93 ret = find_mapped_clock_class(named_fc->fc,
94 clock_class);
95 if (ret) {
96 goto end;
97 }
98 }
99
100 break;
101 }
102 case CTF_FIELD_CLASS_TYPE_ARRAY:
103 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
104 {
105 struct ctf_field_class_array_base *array_fc = (void *) fc;
106
107 ret = find_mapped_clock_class(array_fc->elem_fc, clock_class);
108 if (ret) {
109 goto end;
110 }
111
112 break;
113 }
114 default:
115 break;
116 }
117
118 end:
119 return ret;
120 }
121
122 static inline
123 int update_stream_class_default_clock_class(
124 struct ctf_stream_class *stream_class)
125 {
126 int ret = 0;
127 struct bt_private_clock_class *clock_class =
128 stream_class->default_clock_class;
129 uint64_t i;
130
131 ret = find_mapped_clock_class(stream_class->packet_context_fc,
132 &clock_class);
133 if (ret) {
134 goto end;
135 }
136
137 ret = find_mapped_clock_class(stream_class->event_header_fc,
138 &clock_class);
139 if (ret) {
140 goto end;
141 }
142
143 ret = find_mapped_clock_class(stream_class->event_common_context_fc,
144 &clock_class);
145 if (ret) {
146 goto end;
147 }
148
149 for (i = 0; i < stream_class->event_classes->len; i++) {
150 struct ctf_event_class *event_class =
151 stream_class->event_classes->pdata[i];
152
153 ret = find_mapped_clock_class(event_class->spec_context_fc,
154 &clock_class);
155 if (ret) {
156 goto end;
157 }
158
159 ret = find_mapped_clock_class(event_class->payload_fc,
160 &clock_class);
161 if (ret) {
162 goto end;
163 }
164 }
165
166 if (!stream_class->default_clock_class) {
167 stream_class->default_clock_class = bt_object_get_ref(clock_class);
168 }
169
170 end:
171 return ret;
172 }
173
174 BT_HIDDEN
175 int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc)
176 {
177 uint64_t i;
178 int ret = 0;
179 struct bt_private_clock_class *clock_class = NULL;
180
181 ret = find_mapped_clock_class(ctf_tc->packet_header_fc,
182 &clock_class);
183 if (ret) {
184 goto end;
185 }
186
187 if (clock_class) {
188 ret = -1;
189 goto end;
190 }
191
192 for (i = 0; i < ctf_tc->stream_classes->len; i++) {
193 struct ctf_stream_class *sc =
194 ctf_tc->stream_classes->pdata[i];
195
196 ret = update_stream_class_default_clock_class(
197 ctf_tc->stream_classes->pdata[i]);
198 if (ret) {
199 BT_LOGE("Stream class contains more than one "
200 "clock class: stream-class-id=%" PRIu64,
201 sc->id);
202 goto end;
203 }
204 }
205
206 end:
207 return ret;
208 }
This page took 0.035404 seconds and 4 git commands to generate.