Commit | Line | Data |
---|---|---|
7b33a0e0 PP |
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 | |
939190b3 | 29 | int find_mapped_clock_class(struct ctf_field_class *fc, |
78cf9df6 | 30 | struct bt_clock_class **clock_class) |
7b33a0e0 PP |
31 | { |
32 | int ret = 0; | |
33 | uint64_t i; | |
34 | ||
939190b3 | 35 | if (!fc) { |
7b33a0e0 PP |
36 | goto end; |
37 | } | |
38 | ||
af0c18e3 PP |
39 | switch (fc->type) { |
40 | case CTF_FIELD_CLASS_TYPE_INT: | |
41 | case CTF_FIELD_CLASS_TYPE_ENUM: | |
7b33a0e0 | 42 | { |
939190b3 | 43 | struct ctf_field_class_int *int_fc = (void *) fc; |
7b33a0e0 | 44 | |
939190b3 | 45 | if (int_fc->mapped_clock_class) { |
7b33a0e0 | 46 | if (*clock_class && *clock_class != |
939190b3 | 47 | int_fc->mapped_clock_class) { |
7b33a0e0 PP |
48 | BT_LOGE("Stream class contains more than one " |
49 | "clock class: expected-cc-name=\"%s\", " | |
50 | "other-cc-name=\"%s\"", | |
78cf9df6 PP |
51 | bt_clock_class_get_name(*clock_class), |
52 | bt_clock_class_get_name(int_fc->mapped_clock_class)); | |
7b33a0e0 PP |
53 | ret = -1; |
54 | goto end; | |
55 | } | |
56 | ||
939190b3 | 57 | *clock_class = int_fc->mapped_clock_class; |
7b33a0e0 PP |
58 | } |
59 | ||
60 | break; | |
61 | } | |
af0c18e3 | 62 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
7b33a0e0 | 63 | { |
939190b3 | 64 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
7b33a0e0 | 65 | |
939190b3 PP |
66 | for (i = 0; i < struct_fc->members->len; i++) { |
67 | struct ctf_named_field_class *named_fc = | |
68 | ctf_field_class_struct_borrow_member_by_index( | |
69 | struct_fc, i); | |
7b33a0e0 | 70 | |
939190b3 | 71 | ret = find_mapped_clock_class(named_fc->fc, |
7b33a0e0 PP |
72 | clock_class); |
73 | if (ret) { | |
74 | goto end; | |
75 | } | |
76 | } | |
77 | ||
78 | break; | |
79 | } | |
af0c18e3 | 80 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
7b33a0e0 | 81 | { |
939190b3 | 82 | struct ctf_field_class_variant *var_fc = (void *) fc; |
7b33a0e0 | 83 | |
939190b3 PP |
84 | for (i = 0; i < var_fc->options->len; i++) { |
85 | struct ctf_named_field_class *named_fc = | |
86 | ctf_field_class_variant_borrow_option_by_index( | |
87 | var_fc, i); | |
7b33a0e0 | 88 | |
939190b3 | 89 | ret = find_mapped_clock_class(named_fc->fc, |
7b33a0e0 PP |
90 | clock_class); |
91 | if (ret) { | |
92 | goto end; | |
93 | } | |
94 | } | |
95 | ||
96 | break; | |
97 | } | |
af0c18e3 PP |
98 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
99 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
7b33a0e0 | 100 | { |
939190b3 | 101 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
7b33a0e0 | 102 | |
939190b3 | 103 | ret = find_mapped_clock_class(array_fc->elem_fc, clock_class); |
7b33a0e0 PP |
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; | |
78cf9df6 | 123 | struct bt_clock_class *clock_class = |
9e550e5f | 124 | stream_class->default_clock_class; |
7b33a0e0 PP |
125 | uint64_t i; |
126 | ||
939190b3 | 127 | ret = find_mapped_clock_class(stream_class->packet_context_fc, |
7b33a0e0 PP |
128 | &clock_class); |
129 | if (ret) { | |
130 | goto end; | |
131 | } | |
132 | ||
939190b3 | 133 | ret = find_mapped_clock_class(stream_class->event_header_fc, |
7b33a0e0 PP |
134 | &clock_class); |
135 | if (ret) { | |
136 | goto end; | |
137 | } | |
138 | ||
939190b3 | 139 | ret = find_mapped_clock_class(stream_class->event_common_context_fc, |
7b33a0e0 PP |
140 | &clock_class); |
141 | if (ret) { | |
142 | goto end; | |
143 | } | |
144 | ||
145 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
146 | struct ctf_event_class *event_class = | |
147 | stream_class->event_classes->pdata[i]; | |
148 | ||
939190b3 | 149 | ret = find_mapped_clock_class(event_class->spec_context_fc, |
7b33a0e0 PP |
150 | &clock_class); |
151 | if (ret) { | |
152 | goto end; | |
153 | } | |
154 | ||
939190b3 | 155 | ret = find_mapped_clock_class(event_class->payload_fc, |
7b33a0e0 PP |
156 | &clock_class); |
157 | if (ret) { | |
158 | goto end; | |
159 | } | |
160 | } | |
161 | ||
162 | if (!stream_class->default_clock_class) { | |
4b70020d | 163 | stream_class->default_clock_class = clock_class; |
8c6884d9 | 164 | bt_clock_class_get_ref(stream_class->default_clock_class); |
7b33a0e0 PP |
165 | } |
166 | ||
167 | end: | |
168 | return ret; | |
169 | } | |
170 | ||
171 | BT_HIDDEN | |
172 | int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc) | |
173 | { | |
174 | uint64_t i; | |
175 | int ret = 0; | |
78cf9df6 | 176 | struct bt_clock_class *clock_class = NULL; |
7b33a0e0 | 177 | |
939190b3 | 178 | ret = find_mapped_clock_class(ctf_tc->packet_header_fc, |
7b33a0e0 PP |
179 | &clock_class); |
180 | if (ret) { | |
181 | goto end; | |
182 | } | |
183 | ||
184 | if (clock_class) { | |
185 | ret = -1; | |
186 | goto end; | |
187 | } | |
188 | ||
189 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
190 | struct ctf_stream_class *sc = | |
191 | ctf_tc->stream_classes->pdata[i]; | |
192 | ||
193 | ret = update_stream_class_default_clock_class( | |
194 | ctf_tc->stream_classes->pdata[i]); | |
195 | if (ret) { | |
196 | BT_LOGE("Stream class contains more than one " | |
197 | "clock class: stream-class-id=%" PRIu64, | |
198 | sc->id); | |
199 | goto end; | |
200 | } | |
201 | } | |
202 | ||
203 | end: | |
204 | return ret; | |
205 | } |