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, |
9e550e5f | 30 | struct bt_private_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\"", | |
9e550e5f | 51 | bt_clock_class_get_name( |
bb61965b | 52 | bt_private_clock_class_as_clock_class( |
9e550e5f PP |
53 | *clock_class)), |
54 | bt_clock_class_get_name( | |
bb61965b | 55 | bt_private_clock_class_as_clock_class( |
9e550e5f | 56 | int_fc->mapped_clock_class))); |
7b33a0e0 PP |
57 | ret = -1; |
58 | goto end; | |
59 | } | |
60 | ||
939190b3 | 61 | *clock_class = int_fc->mapped_clock_class; |
7b33a0e0 PP |
62 | } |
63 | ||
64 | break; | |
65 | } | |
af0c18e3 | 66 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
7b33a0e0 | 67 | { |
939190b3 | 68 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
7b33a0e0 | 69 | |
939190b3 PP |
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); | |
7b33a0e0 | 74 | |
939190b3 | 75 | ret = find_mapped_clock_class(named_fc->fc, |
7b33a0e0 PP |
76 | clock_class); |
77 | if (ret) { | |
78 | goto end; | |
79 | } | |
80 | } | |
81 | ||
82 | break; | |
83 | } | |
af0c18e3 | 84 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
7b33a0e0 | 85 | { |
939190b3 | 86 | struct ctf_field_class_variant *var_fc = (void *) fc; |
7b33a0e0 | 87 | |
939190b3 PP |
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); | |
7b33a0e0 | 92 | |
939190b3 | 93 | ret = find_mapped_clock_class(named_fc->fc, |
7b33a0e0 PP |
94 | clock_class); |
95 | if (ret) { | |
96 | goto end; | |
97 | } | |
98 | } | |
99 | ||
100 | break; | |
101 | } | |
af0c18e3 PP |
102 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
103 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
7b33a0e0 | 104 | { |
939190b3 | 105 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
7b33a0e0 | 106 | |
939190b3 | 107 | ret = find_mapped_clock_class(array_fc->elem_fc, clock_class); |
7b33a0e0 PP |
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; | |
9e550e5f PP |
127 | struct bt_private_clock_class *clock_class = |
128 | stream_class->default_clock_class; | |
7b33a0e0 PP |
129 | uint64_t i; |
130 | ||
939190b3 | 131 | ret = find_mapped_clock_class(stream_class->packet_context_fc, |
7b33a0e0 PP |
132 | &clock_class); |
133 | if (ret) { | |
134 | goto end; | |
135 | } | |
136 | ||
939190b3 | 137 | ret = find_mapped_clock_class(stream_class->event_header_fc, |
7b33a0e0 PP |
138 | &clock_class); |
139 | if (ret) { | |
140 | goto end; | |
141 | } | |
142 | ||
939190b3 | 143 | ret = find_mapped_clock_class(stream_class->event_common_context_fc, |
7b33a0e0 PP |
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 | ||
939190b3 | 153 | ret = find_mapped_clock_class(event_class->spec_context_fc, |
7b33a0e0 PP |
154 | &clock_class); |
155 | if (ret) { | |
156 | goto end; | |
157 | } | |
158 | ||
939190b3 | 159 | ret = find_mapped_clock_class(event_class->payload_fc, |
7b33a0e0 PP |
160 | &clock_class); |
161 | if (ret) { | |
162 | goto end; | |
163 | } | |
164 | } | |
165 | ||
166 | if (!stream_class->default_clock_class) { | |
8138bfe1 | 167 | stream_class->default_clock_class = bt_object_get_ref(clock_class); |
7b33a0e0 PP |
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; | |
9e550e5f | 179 | struct bt_private_clock_class *clock_class = NULL; |
7b33a0e0 | 180 | |
939190b3 | 181 | ret = find_mapped_clock_class(ctf_tc->packet_header_fc, |
7b33a0e0 PP |
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 | } |