Commit | Line | Data |
---|---|---|
cab3f160 JG |
1 | /* |
2 | * trimmer.c | |
3 | * | |
4 | * Babeltrace Trace Trimmer | |
5 | * | |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
3d2f08e7 | 29 | #include <babeltrace/compat/utc-internal.h> |
33b34c43 | 30 | #include <babeltrace/plugin/plugin-dev.h> |
b2e0c907 PP |
31 | #include <babeltrace/graph/component.h> |
32 | #include <babeltrace/graph/private-component.h> | |
33 | #include <babeltrace/graph/component-filter.h> | |
34 | #include <babeltrace/graph/notification.h> | |
35 | #include <babeltrace/graph/notification-iterator.h> | |
36 | #include <babeltrace/graph/private-notification-iterator.h> | |
b9d103be | 37 | #include <babeltrace/graph/private-component-filter.h> |
b2e0c907 | 38 | #include <babeltrace/graph/notification-event.h> |
7d61fa8e | 39 | #include <plugins-common.h> |
cab3f160 JG |
40 | #include "trimmer.h" |
41 | #include "iterator.h" | |
44d3cbf0 | 42 | #include <assert.h> |
cab3f160 JG |
43 | |
44 | static | |
45 | void destroy_trimmer_data(struct trimmer *trimmer) | |
46 | { | |
47 | g_free(trimmer); | |
48 | } | |
49 | ||
50 | static | |
51 | struct trimmer *create_trimmer_data(void) | |
52 | { | |
53 | struct trimmer *trimmer; | |
54 | ||
55 | trimmer = g_new0(struct trimmer, 1); | |
56 | if (!trimmer) { | |
57 | goto end; | |
58 | } | |
59 | end: | |
60 | return trimmer; | |
61 | } | |
62 | ||
64cadc66 | 63 | void finalize_trimmer(struct bt_private_component *component) |
cab3f160 | 64 | { |
890882ef | 65 | void *data = bt_private_component_get_user_data(component); |
cab3f160 JG |
66 | |
67 | destroy_trimmer_data(data); | |
68 | } | |
69 | ||
528debdf MD |
70 | /* |
71 | * Parses a timestamp, figuring out its format. | |
72 | * | |
73 | * Returns a negative value if anything goes wrong. | |
74 | * | |
75 | * Expected formats: | |
76 | * | |
77 | * YYYY-MM-DD hh:mm:ss.ns | |
78 | * hh:mm:ss.ns | |
79 | * -ss.ns | |
80 | * ss.ns | |
81 | * YYYY-MM-DD hh:mm:ss | |
82 | * hh:mm:ss | |
83 | * -ss | |
84 | * ss | |
85 | */ | |
44d3cbf0 | 86 | static |
c6dedfd4 | 87 | int timestamp_from_arg(const char *arg, struct trimmer *trimmer, |
c55a9f58 | 88 | struct trimmer_bound *result_bound, bt_bool gmt) |
528debdf MD |
89 | { |
90 | int ret; | |
91 | int64_t value; | |
92 | unsigned int year, month, day, hh, mm, ss, ns; | |
93 | ||
94 | /* YYYY-MM-DD hh:mm:ss.ns */ | |
95 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u.%u", | |
96 | &year, &month, &day, &hh, &mm, &ss, &ns); | |
97 | if (ret == 7) { | |
98 | struct tm tm = { | |
99 | .tm_sec = ss, | |
100 | .tm_min = mm, | |
101 | .tm_hour = hh, | |
102 | .tm_mday = day, | |
103 | .tm_mon = month - 1, | |
104 | .tm_year = year - 1900, | |
105 | .tm_isdst = -1, | |
106 | }; | |
107 | time_t result; | |
108 | ||
109 | if (gmt) { | |
3d2f08e7 | 110 | result = bt_timegm(&tm); |
528debdf MD |
111 | if (result < 0) { |
112 | return -1; | |
113 | } | |
114 | } else { | |
115 | result = mktime(&tm); | |
116 | if (result < 0) { | |
117 | return -1; | |
118 | } | |
119 | } | |
120 | value = (int64_t) result; | |
121 | value *= NSEC_PER_SEC; | |
122 | value += ns; | |
c6dedfd4 MD |
123 | if (!trimmer->date) { |
124 | trimmer->year = year; | |
125 | trimmer->month = month; | |
126 | trimmer->day = day; | |
127 | trimmer->date = true; | |
128 | } | |
528debdf MD |
129 | goto set; |
130 | } | |
131 | /* hh:mm:ss.ns */ | |
132 | ret = sscanf(arg, "%u:%u:%u.%u", | |
133 | &hh, &mm, &ss, &ns); | |
134 | if (ret == 4) { | |
c6dedfd4 MD |
135 | if (!trimmer->date) { |
136 | /* We don't know which day until we get an event. */ | |
137 | result_bound->lazy_values.hh = hh; | |
138 | result_bound->lazy_values.mm = mm; | |
139 | result_bound->lazy_values.ss = ss; | |
140 | result_bound->lazy_values.ns = ns; | |
141 | result_bound->lazy_values.gmt = gmt; | |
142 | goto lazy; | |
143 | } else { | |
144 | struct tm tm = { | |
145 | .tm_sec = ss, | |
146 | .tm_min = mm, | |
147 | .tm_hour = hh, | |
148 | .tm_mday = trimmer->day, | |
149 | .tm_mon = trimmer->month - 1, | |
150 | .tm_year = trimmer->year - 1900, | |
151 | .tm_isdst = -1, | |
152 | }; | |
153 | time_t result; | |
154 | ||
155 | if (gmt) { | |
3d2f08e7 | 156 | result = bt_timegm(&tm); |
c6dedfd4 MD |
157 | if (result < 0) { |
158 | return -1; | |
159 | } | |
160 | } else { | |
161 | result = mktime(&tm); | |
162 | if (result < 0) { | |
163 | return -1; | |
164 | } | |
165 | } | |
166 | value = (int64_t) result; | |
167 | value *= NSEC_PER_SEC; | |
168 | value += ns; | |
169 | goto set; | |
170 | } | |
528debdf MD |
171 | } |
172 | /* -ss.ns */ | |
173 | ret = sscanf(arg, "-%u.%u", | |
174 | &ss, &ns); | |
175 | if (ret == 2) { | |
176 | value = -ss * NSEC_PER_SEC; | |
177 | value -= ns; | |
178 | goto set; | |
179 | } | |
180 | /* ss.ns */ | |
181 | ret = sscanf(arg, "%u.%u", | |
182 | &ss, &ns); | |
183 | if (ret == 2) { | |
184 | value = ss * NSEC_PER_SEC; | |
185 | value += ns; | |
186 | goto set; | |
187 | } | |
188 | ||
189 | /* YYYY-MM-DD hh:mm:ss */ | |
190 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u", | |
191 | &year, &month, &day, &hh, &mm, &ss); | |
192 | if (ret == 6) { | |
193 | struct tm tm = { | |
194 | .tm_sec = ss, | |
195 | .tm_min = mm, | |
196 | .tm_hour = hh, | |
197 | .tm_mday = day, | |
198 | .tm_mon = month - 1, | |
199 | .tm_year = year - 1900, | |
200 | .tm_isdst = -1, | |
201 | }; | |
202 | ||
203 | if (gmt) { | |
3d2f08e7 | 204 | value = bt_timegm(&tm); |
528debdf MD |
205 | if (value < 0) { |
206 | return -1; | |
207 | } | |
208 | } else { | |
209 | value = mktime(&tm); | |
210 | if (value < 0) { | |
211 | return -1; | |
212 | } | |
213 | } | |
214 | value *= NSEC_PER_SEC; | |
c6dedfd4 MD |
215 | if (!trimmer->date) { |
216 | trimmer->year = year; | |
217 | trimmer->month = month; | |
218 | trimmer->day = day; | |
219 | trimmer->date = true; | |
220 | } | |
528debdf MD |
221 | goto set; |
222 | } | |
223 | /* hh:mm:ss */ | |
224 | ret = sscanf(arg, "%u:%u:%u", | |
225 | &hh, &mm, &ss); | |
226 | if (ret == 3) { | |
c6dedfd4 MD |
227 | if (!trimmer->date) { |
228 | /* We don't know which day until we get an event. */ | |
229 | result_bound->lazy_values.hh = hh; | |
230 | result_bound->lazy_values.mm = mm; | |
231 | result_bound->lazy_values.ss = ss; | |
232 | result_bound->lazy_values.ns = 0; | |
233 | result_bound->lazy_values.gmt = gmt; | |
234 | goto lazy; | |
235 | } else { | |
236 | struct tm tm = { | |
237 | .tm_sec = ss, | |
238 | .tm_min = mm, | |
239 | .tm_hour = hh, | |
240 | .tm_mday = trimmer->day, | |
241 | .tm_mon = trimmer->month - 1, | |
242 | .tm_year = trimmer->year - 1900, | |
243 | .tm_isdst = -1, | |
244 | }; | |
245 | time_t result; | |
246 | ||
247 | if (gmt) { | |
3d2f08e7 | 248 | result = bt_timegm(&tm); |
c6dedfd4 MD |
249 | if (result < 0) { |
250 | return -1; | |
251 | } | |
252 | } else { | |
253 | result = mktime(&tm); | |
254 | if (result < 0) { | |
255 | return -1; | |
256 | } | |
257 | } | |
258 | value = (int64_t) result; | |
259 | value *= NSEC_PER_SEC; | |
260 | goto set; | |
261 | } | |
528debdf MD |
262 | } |
263 | /* -ss */ | |
264 | ret = sscanf(arg, "-%u", | |
265 | &ss); | |
266 | if (ret == 1) { | |
267 | value = -ss * NSEC_PER_SEC; | |
268 | goto set; | |
269 | } | |
270 | /* ss */ | |
271 | ret = sscanf(arg, "%u", | |
272 | &ss); | |
273 | if (ret == 1) { | |
274 | value = ss * NSEC_PER_SEC; | |
275 | goto set; | |
276 | } | |
277 | ||
278 | /* Not found. */ | |
279 | return -1; | |
280 | ||
281 | set: | |
282 | result_bound->value = value; | |
283 | result_bound->set = true; | |
284 | return 0; | |
285 | ||
286 | lazy: | |
287 | result_bound->lazy = true; | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static | |
890882ef PP |
292 | enum bt_component_status init_from_params(struct trimmer *trimmer, |
293 | struct bt_value *params) | |
44d3cbf0 JG |
294 | { |
295 | struct bt_value *value = NULL; | |
c55a9f58 | 296 | bt_bool gmt = BT_FALSE; |
528debdf | 297 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
44d3cbf0 JG |
298 | |
299 | assert(params); | |
300 | ||
528debdf MD |
301 | value = bt_value_map_get(params, "clock-gmt"); |
302 | if (value) { | |
44d3cbf0 JG |
303 | enum bt_value_status value_ret; |
304 | ||
528debdf MD |
305 | value_ret = bt_value_bool_get(value, &gmt); |
306 | if (value_ret) { | |
307 | ret = BT_COMPONENT_STATUS_INVALID; | |
55595636 | 308 | printf_error("Failed to retrieve clock-gmt value. Expecting a boolean"); |
44d3cbf0 JG |
309 | } |
310 | } | |
311 | bt_put(value); | |
528debdf MD |
312 | if (ret != BT_COMPONENT_STATUS_OK) { |
313 | goto end; | |
314 | } | |
315 | ||
316 | value = bt_value_map_get(params, "begin"); | |
317 | if (value) { | |
44d3cbf0 | 318 | enum bt_value_status value_ret; |
528debdf | 319 | const char *str; |
44d3cbf0 | 320 | |
528debdf MD |
321 | value_ret = bt_value_string_get(value, &str); |
322 | if (value_ret || timestamp_from_arg(str, | |
c6dedfd4 | 323 | trimmer, &trimmer->begin, gmt)) { |
528debdf | 324 | ret = BT_COMPONENT_STATUS_INVALID; |
55595636 | 325 | printf_error("Failed to retrieve begin value. Expecting a timestamp string"); |
44d3cbf0 JG |
326 | } |
327 | } | |
328 | bt_put(value); | |
528debdf MD |
329 | if (ret != BT_COMPONENT_STATUS_OK) { |
330 | goto end; | |
331 | } | |
332 | ||
333 | value = bt_value_map_get(params, "end"); | |
334 | if (value) { | |
335 | enum bt_value_status value_ret; | |
336 | const char *str; | |
337 | ||
338 | value_ret = bt_value_string_get(value, &str); | |
339 | if (value_ret || timestamp_from_arg(str, | |
c6dedfd4 | 340 | trimmer, &trimmer->end, gmt)) { |
528debdf | 341 | ret = BT_COMPONENT_STATUS_INVALID; |
55595636 | 342 | printf_error("Failed to retrieve end value. Expecting a timestamp string"); |
528debdf MD |
343 | } |
344 | } | |
345 | bt_put(value); | |
346 | end: | |
55595636 MD |
347 | if (trimmer->begin.set && trimmer->end.set) { |
348 | if (trimmer->begin.value > trimmer->end.value) { | |
349 | printf_error("Unexpected: time range begin value is above end value"); | |
350 | ret = BT_COMPONENT_STATUS_INVALID; | |
351 | } | |
352 | } | |
528debdf | 353 | return ret; |
44d3cbf0 JG |
354 | } |
355 | ||
cab3f160 | 356 | enum bt_component_status trimmer_component_init( |
890882ef | 357 | struct bt_private_component *component, struct bt_value *params, |
7d61fa8e | 358 | UNUSED_VAR void *init_method_data) |
cab3f160 JG |
359 | { |
360 | enum bt_component_status ret; | |
361 | struct trimmer *trimmer = create_trimmer_data(); | |
362 | ||
363 | if (!trimmer) { | |
364 | ret = BT_COMPONENT_STATUS_NOMEM; | |
365 | goto end; | |
366 | } | |
367 | ||
b9d103be | 368 | /* Create input and output ports */ |
147337a3 PP |
369 | ret = bt_private_component_filter_add_input_private_port( |
370 | component, "in", NULL, NULL); | |
371 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
372 | goto error; |
373 | } | |
374 | ||
147337a3 PP |
375 | ret = bt_private_component_filter_add_output_private_port( |
376 | component, "out", NULL, NULL); | |
377 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
378 | goto error; |
379 | } | |
380 | ||
890882ef | 381 | ret = bt_private_component_set_user_data(component, trimmer); |
cab3f160 JG |
382 | if (ret != BT_COMPONENT_STATUS_OK) { |
383 | goto error; | |
384 | } | |
385 | ||
528debdf | 386 | ret = init_from_params(trimmer, params); |
cab3f160 JG |
387 | end: |
388 | return ret; | |
389 | error: | |
390 | destroy_trimmer_data(trimmer); | |
b9d103be | 391 | ret = BT_COMPONENT_STATUS_ERROR; |
cab3f160 JG |
392 | return ret; |
393 | } |