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 | ||
b4565e8b PP |
29 | #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT" |
30 | #include "logging.h" | |
31 | ||
3d2f08e7 | 32 | #include <babeltrace/compat/utc-internal.h> |
9d408fca | 33 | #include <babeltrace/babeltrace.h> |
7d61fa8e | 34 | #include <plugins-common.h> |
cab3f160 JG |
35 | #include "trimmer.h" |
36 | #include "iterator.h" | |
f6ccaed9 | 37 | #include <babeltrace/assert-internal.h> |
cab3f160 JG |
38 | |
39 | static | |
40 | void destroy_trimmer_data(struct trimmer *trimmer) | |
41 | { | |
42 | g_free(trimmer); | |
43 | } | |
44 | ||
45 | static | |
46 | struct trimmer *create_trimmer_data(void) | |
47 | { | |
4fde0f03 | 48 | return g_new0(struct trimmer, 1); |
cab3f160 JG |
49 | } |
50 | ||
b19ff26f | 51 | void finalize_trimmer(bt_self_component *component) |
cab3f160 | 52 | { |
d94d92ac | 53 | void *data = bt_self_component_get_user_data(component); |
cab3f160 JG |
54 | |
55 | destroy_trimmer_data(data); | |
56 | } | |
57 | ||
528debdf MD |
58 | /* |
59 | * Parses a timestamp, figuring out its format. | |
60 | * | |
61 | * Returns a negative value if anything goes wrong. | |
62 | * | |
63 | * Expected formats: | |
64 | * | |
65 | * YYYY-MM-DD hh:mm:ss.ns | |
66 | * hh:mm:ss.ns | |
67 | * -ss.ns | |
68 | * ss.ns | |
69 | * YYYY-MM-DD hh:mm:ss | |
70 | * hh:mm:ss | |
71 | * -ss | |
72 | * ss | |
73 | */ | |
44d3cbf0 | 74 | static |
b19ff26f | 75 | int timestamp_from_param(const char *param_name, bt_value *param, |
268fae9a PP |
76 | struct trimmer *trimmer, struct trimmer_bound *result_bound, |
77 | bt_bool gmt) | |
528debdf MD |
78 | { |
79 | int ret; | |
80 | int64_t value; | |
81 | unsigned int year, month, day, hh, mm, ss, ns; | |
268fae9a PP |
82 | const char *arg; |
83 | ||
84 | if (bt_value_is_integer(param)) { | |
85 | (void) bt_value_integer_get(param, &value); | |
86 | goto set; | |
87 | } else if (!bt_value_is_string(param)) { | |
88 | BT_LOGE("`%s` parameter must be an integer or a string value.", | |
89 | param_name); | |
90 | goto error; | |
91 | } | |
92 | ||
93 | (void) bt_value_string_get(param, &arg); | |
528debdf MD |
94 | |
95 | /* YYYY-MM-DD hh:mm:ss.ns */ | |
96 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u.%u", | |
97 | &year, &month, &day, &hh, &mm, &ss, &ns); | |
98 | if (ret == 7) { | |
99 | struct tm tm = { | |
100 | .tm_sec = ss, | |
101 | .tm_min = mm, | |
102 | .tm_hour = hh, | |
103 | .tm_mday = day, | |
104 | .tm_mon = month - 1, | |
105 | .tm_year = year - 1900, | |
106 | .tm_isdst = -1, | |
107 | }; | |
108 | time_t result; | |
109 | ||
110 | if (gmt) { | |
3d2f08e7 | 111 | result = bt_timegm(&tm); |
528debdf MD |
112 | if (result < 0) { |
113 | return -1; | |
114 | } | |
115 | } else { | |
116 | result = mktime(&tm); | |
117 | if (result < 0) { | |
118 | return -1; | |
119 | } | |
120 | } | |
121 | value = (int64_t) result; | |
122 | value *= NSEC_PER_SEC; | |
123 | value += ns; | |
c6dedfd4 MD |
124 | if (!trimmer->date) { |
125 | trimmer->year = year; | |
126 | trimmer->month = month; | |
127 | trimmer->day = day; | |
128 | trimmer->date = true; | |
129 | } | |
528debdf MD |
130 | goto set; |
131 | } | |
132 | /* hh:mm:ss.ns */ | |
133 | ret = sscanf(arg, "%u:%u:%u.%u", | |
134 | &hh, &mm, &ss, &ns); | |
135 | if (ret == 4) { | |
c6dedfd4 MD |
136 | if (!trimmer->date) { |
137 | /* We don't know which day until we get an event. */ | |
c6bd8523 | 138 | result_bound->lazy_value.hh = hh; |
c6dedfd4 MD |
139 | result_bound->lazy_values.mm = mm; |
140 | result_bound->lazy_values.ss = ss; | |
141 | result_bound->lazy_values.ns = ns; | |
142 | result_bound->lazy_values.gmt = gmt; | |
143 | goto lazy; | |
144 | } else { | |
145 | struct tm tm = { | |
146 | .tm_sec = ss, | |
147 | .tm_min = mm, | |
148 | .tm_hour = hh, | |
149 | .tm_mday = trimmer->day, | |
150 | .tm_mon = trimmer->month - 1, | |
151 | .tm_year = trimmer->year - 1900, | |
152 | .tm_isdst = -1, | |
153 | }; | |
154 | time_t result; | |
155 | ||
156 | if (gmt) { | |
3d2f08e7 | 157 | result = bt_timegm(&tm); |
c6dedfd4 MD |
158 | if (result < 0) { |
159 | return -1; | |
160 | } | |
161 | } else { | |
162 | result = mktime(&tm); | |
163 | if (result < 0) { | |
164 | return -1; | |
165 | } | |
166 | } | |
167 | value = (int64_t) result; | |
168 | value *= NSEC_PER_SEC; | |
169 | value += ns; | |
170 | goto set; | |
171 | } | |
528debdf MD |
172 | } |
173 | /* -ss.ns */ | |
174 | ret = sscanf(arg, "-%u.%u", | |
175 | &ss, &ns); | |
176 | if (ret == 2) { | |
177 | value = -ss * NSEC_PER_SEC; | |
178 | value -= ns; | |
179 | goto set; | |
180 | } | |
181 | /* ss.ns */ | |
182 | ret = sscanf(arg, "%u.%u", | |
183 | &ss, &ns); | |
184 | if (ret == 2) { | |
185 | value = ss * NSEC_PER_SEC; | |
186 | value += ns; | |
187 | goto set; | |
188 | } | |
189 | ||
190 | /* YYYY-MM-DD hh:mm:ss */ | |
191 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u", | |
192 | &year, &month, &day, &hh, &mm, &ss); | |
193 | if (ret == 6) { | |
194 | struct tm tm = { | |
195 | .tm_sec = ss, | |
196 | .tm_min = mm, | |
197 | .tm_hour = hh, | |
198 | .tm_mday = day, | |
199 | .tm_mon = month - 1, | |
200 | .tm_year = year - 1900, | |
201 | .tm_isdst = -1, | |
202 | }; | |
203 | ||
204 | if (gmt) { | |
3d2f08e7 | 205 | value = bt_timegm(&tm); |
528debdf MD |
206 | if (value < 0) { |
207 | return -1; | |
208 | } | |
209 | } else { | |
210 | value = mktime(&tm); | |
211 | if (value < 0) { | |
212 | return -1; | |
213 | } | |
214 | } | |
215 | value *= NSEC_PER_SEC; | |
c6dedfd4 MD |
216 | if (!trimmer->date) { |
217 | trimmer->year = year; | |
218 | trimmer->month = month; | |
219 | trimmer->day = day; | |
220 | trimmer->date = true; | |
221 | } | |
528debdf MD |
222 | goto set; |
223 | } | |
224 | /* hh:mm:ss */ | |
225 | ret = sscanf(arg, "%u:%u:%u", | |
226 | &hh, &mm, &ss); | |
227 | if (ret == 3) { | |
c6dedfd4 MD |
228 | if (!trimmer->date) { |
229 | /* We don't know which day until we get an event. */ | |
c6bd8523 | 230 | result_bound->lazy_value.hh = hh; |
c6dedfd4 MD |
231 | result_bound->lazy_values.mm = mm; |
232 | result_bound->lazy_values.ss = ss; | |
233 | result_bound->lazy_values.ns = 0; | |
234 | result_bound->lazy_values.gmt = gmt; | |
235 | goto lazy; | |
236 | } else { | |
237 | struct tm tm = { | |
238 | .tm_sec = ss, | |
239 | .tm_min = mm, | |
240 | .tm_hour = hh, | |
241 | .tm_mday = trimmer->day, | |
242 | .tm_mon = trimmer->month - 1, | |
243 | .tm_year = trimmer->year - 1900, | |
244 | .tm_isdst = -1, | |
245 | }; | |
246 | time_t result; | |
247 | ||
248 | if (gmt) { | |
3d2f08e7 | 249 | result = bt_timegm(&tm); |
c6dedfd4 MD |
250 | if (result < 0) { |
251 | return -1; | |
252 | } | |
253 | } else { | |
254 | result = mktime(&tm); | |
255 | if (result < 0) { | |
256 | return -1; | |
257 | } | |
258 | } | |
259 | value = (int64_t) result; | |
260 | value *= NSEC_PER_SEC; | |
261 | goto set; | |
262 | } | |
528debdf MD |
263 | } |
264 | /* -ss */ | |
265 | ret = sscanf(arg, "-%u", | |
266 | &ss); | |
267 | if (ret == 1) { | |
268 | value = -ss * NSEC_PER_SEC; | |
269 | goto set; | |
270 | } | |
271 | /* ss */ | |
272 | ret = sscanf(arg, "%u", | |
273 | &ss); | |
274 | if (ret == 1) { | |
275 | value = ss * NSEC_PER_SEC; | |
276 | goto set; | |
277 | } | |
278 | ||
268fae9a | 279 | error: |
528debdf MD |
280 | /* Not found. */ |
281 | return -1; | |
282 | ||
283 | set: | |
284 | result_bound->value = value; | |
285 | result_bound->set = true; | |
286 | return 0; | |
287 | ||
288 | lazy: | |
289 | result_bound->lazy = true; | |
290 | return 0; | |
291 | } | |
292 | ||
293 | static | |
890882ef | 294 | enum bt_component_status init_from_params(struct trimmer *trimmer, |
b19ff26f | 295 | bt_value *params) |
44d3cbf0 | 296 | { |
b19ff26f | 297 | bt_value *value = NULL; |
c55a9f58 | 298 | bt_bool gmt = BT_FALSE; |
528debdf | 299 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
44d3cbf0 | 300 | |
f6ccaed9 | 301 | BT_ASSERT(params); |
44d3cbf0 | 302 | |
528debdf MD |
303 | value = bt_value_map_get(params, "clock-gmt"); |
304 | if (value) { | |
44d3cbf0 JG |
305 | enum bt_value_status value_ret; |
306 | ||
601b0d3c | 307 | gmt = bt_value_bool_get(value); |
44d3cbf0 | 308 | } |
528debdf MD |
309 | if (ret != BT_COMPONENT_STATUS_OK) { |
310 | goto end; | |
311 | } | |
312 | ||
c5b9b441 | 313 | BT_VALUE_PUT_REF_AND_RESET(value); |
528debdf MD |
314 | value = bt_value_map_get(params, "begin"); |
315 | if (value) { | |
268fae9a | 316 | if (timestamp_from_param("begin", value, |
c6dedfd4 | 317 | trimmer, &trimmer->begin, gmt)) { |
268fae9a | 318 | BT_LOGE_STR("Failed to convert `begin` parameter to a timestamp."); |
528debdf | 319 | ret = BT_COMPONENT_STATUS_INVALID; |
268fae9a | 320 | goto end; |
44d3cbf0 JG |
321 | } |
322 | } | |
528debdf | 323 | |
c5b9b441 | 324 | BT_VALUE_PUT_REF_AND_RESET(value); |
528debdf MD |
325 | value = bt_value_map_get(params, "end"); |
326 | if (value) { | |
268fae9a | 327 | if (timestamp_from_param("end", value, |
c6dedfd4 | 328 | trimmer, &trimmer->end, gmt)) { |
268fae9a | 329 | BT_LOGE_STR("Failed to convert `end` parameter to a timestamp."); |
528debdf | 330 | ret = BT_COMPONENT_STATUS_INVALID; |
268fae9a | 331 | goto end; |
528debdf MD |
332 | } |
333 | } | |
268fae9a | 334 | |
528debdf | 335 | end: |
c5b9b441 | 336 | bt_value_put_ref(value); |
268fae9a | 337 | |
55595636 MD |
338 | if (trimmer->begin.set && trimmer->end.set) { |
339 | if (trimmer->begin.value > trimmer->end.value) { | |
b4565e8b | 340 | BT_LOGE_STR("Unexpected: time range begin value is above end value"); |
55595636 MD |
341 | ret = BT_COMPONENT_STATUS_INVALID; |
342 | } | |
343 | } | |
528debdf | 344 | return ret; |
44d3cbf0 JG |
345 | } |
346 | ||
cab3f160 | 347 | enum bt_component_status trimmer_component_init( |
b19ff26f | 348 | bt_self_component *component, bt_value *params, |
7d61fa8e | 349 | UNUSED_VAR void *init_method_data) |
cab3f160 JG |
350 | { |
351 | enum bt_component_status ret; | |
352 | struct trimmer *trimmer = create_trimmer_data(); | |
353 | ||
354 | if (!trimmer) { | |
355 | ret = BT_COMPONENT_STATUS_NOMEM; | |
356 | goto end; | |
357 | } | |
358 | ||
b9d103be | 359 | /* Create input and output ports */ |
d94d92ac | 360 | ret = bt_self_component_filter_add_input_port( |
147337a3 PP |
361 | component, "in", NULL, NULL); |
362 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
363 | goto error; |
364 | } | |
365 | ||
d94d92ac | 366 | ret = bt_self_component_filter_add_output_port( |
147337a3 PP |
367 | component, "out", NULL, NULL); |
368 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
369 | goto error; |
370 | } | |
371 | ||
d94d92ac | 372 | ret = bt_self_component_set_user_data(component, trimmer); |
cab3f160 JG |
373 | if (ret != BT_COMPONENT_STATUS_OK) { |
374 | goto error; | |
375 | } | |
376 | ||
528debdf | 377 | ret = init_from_params(trimmer, params); |
cab3f160 JG |
378 | end: |
379 | return ret; | |
380 | error: | |
381 | destroy_trimmer_data(trimmer); | |
b9d103be | 382 | ret = BT_COMPONENT_STATUS_ERROR; |
cab3f160 JG |
383 | return ret; |
384 | } |