4 * Babeltrace Trace Trimmer
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/compat/utc-internal.h>
30 #include <babeltrace/plugin/plugin-dev.h>
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>
37 #include <babeltrace/graph/private-component-filter.h>
38 #include <babeltrace/graph/notification-event.h>
39 #include <plugins-common.h>
45 void destroy_trimmer_data(struct trimmer
*trimmer
)
51 struct trimmer
*create_trimmer_data(void)
53 struct trimmer
*trimmer
;
55 trimmer
= g_new0(struct trimmer
, 1);
63 void finalize_trimmer(struct bt_private_component
*component
)
65 void *data
= bt_private_component_get_user_data(component
);
67 destroy_trimmer_data(data
);
71 * Parses a timestamp, figuring out its format.
73 * Returns a negative value if anything goes wrong.
77 * YYYY-MM-DD hh:mm:ss.ns
87 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
88 struct trimmer_bound
*result_bound
, bt_bool gmt
)
92 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
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
);
104 .tm_year
= year
- 1900,
110 result
= bt_timegm(&tm
);
115 result
= mktime(&tm
);
120 value
= (int64_t) result
;
121 value
*= NSEC_PER_SEC
;
123 if (!trimmer
->date
) {
124 trimmer
->year
= year
;
125 trimmer
->month
= month
;
127 trimmer
->date
= true;
132 ret
= sscanf(arg
, "%u:%u:%u.%u",
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
;
148 .tm_mday
= trimmer
->day
,
149 .tm_mon
= trimmer
->month
- 1,
150 .tm_year
= trimmer
->year
- 1900,
156 result
= bt_timegm(&tm
);
161 result
= mktime(&tm
);
166 value
= (int64_t) result
;
167 value
*= NSEC_PER_SEC
;
173 ret
= sscanf(arg
, "-%u.%u",
176 value
= -ss
* NSEC_PER_SEC
;
181 ret
= sscanf(arg
, "%u.%u",
184 value
= ss
* NSEC_PER_SEC
;
189 /* YYYY-MM-DD hh:mm:ss */
190 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
191 &year
, &month
, &day
, &hh
, &mm
, &ss
);
199 .tm_year
= year
- 1900,
204 value
= bt_timegm(&tm
);
214 value
*= NSEC_PER_SEC
;
215 if (!trimmer
->date
) {
216 trimmer
->year
= year
;
217 trimmer
->month
= month
;
219 trimmer
->date
= true;
224 ret
= sscanf(arg
, "%u:%u:%u",
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
;
240 .tm_mday
= trimmer
->day
,
241 .tm_mon
= trimmer
->month
- 1,
242 .tm_year
= trimmer
->year
- 1900,
248 result
= bt_timegm(&tm
);
253 result
= mktime(&tm
);
258 value
= (int64_t) result
;
259 value
*= NSEC_PER_SEC
;
264 ret
= sscanf(arg
, "-%u",
267 value
= -ss
* NSEC_PER_SEC
;
271 ret
= sscanf(arg
, "%u",
274 value
= ss
* NSEC_PER_SEC
;
282 result_bound
->value
= value
;
283 result_bound
->set
= true;
287 result_bound
->lazy
= true;
292 enum bt_component_status
init_from_params(struct trimmer
*trimmer
,
293 struct bt_value
*params
)
295 struct bt_value
*value
= NULL
;
296 bt_bool gmt
= BT_FALSE
;
297 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
301 value
= bt_value_map_get(params
, "clock-gmt");
303 enum bt_value_status value_ret
;
305 value_ret
= bt_value_bool_get(value
, &gmt
);
307 ret
= BT_COMPONENT_STATUS_INVALID
;
308 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
312 if (ret
!= BT_COMPONENT_STATUS_OK
) {
316 value
= bt_value_map_get(params
, "begin");
318 enum bt_value_status value_ret
;
321 value_ret
= bt_value_string_get(value
, &str
);
322 if (value_ret
|| timestamp_from_arg(str
,
323 trimmer
, &trimmer
->begin
, gmt
)) {
324 ret
= BT_COMPONENT_STATUS_INVALID
;
325 printf_error("Failed to retrieve begin value. Expecting a timestamp string");
329 if (ret
!= BT_COMPONENT_STATUS_OK
) {
333 value
= bt_value_map_get(params
, "end");
335 enum bt_value_status value_ret
;
338 value_ret
= bt_value_string_get(value
, &str
);
339 if (value_ret
|| timestamp_from_arg(str
,
340 trimmer
, &trimmer
->end
, gmt
)) {
341 ret
= BT_COMPONENT_STATUS_INVALID
;
342 printf_error("Failed to retrieve end value. Expecting a timestamp string");
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
;
356 enum bt_component_status
trimmer_component_init(
357 struct bt_private_component
*component
, struct bt_value
*params
,
358 UNUSED_VAR
void *init_method_data
)
360 enum bt_component_status ret
;
361 struct trimmer
*trimmer
= create_trimmer_data();
362 struct bt_private_port
*priv_port
= NULL
;
365 ret
= BT_COMPONENT_STATUS_NOMEM
;
369 /* Create input and output ports */
370 priv_port
= bt_private_component_filter_add_input_private_port(
371 component
, "in", NULL
);
377 priv_port
= bt_private_component_filter_add_output_private_port(
378 component
, "out", NULL
);
383 ret
= bt_private_component_set_user_data(component
, trimmer
);
384 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 ret
= init_from_params(trimmer
, params
);
393 destroy_trimmer_data(trimmer
);
394 ret
= BT_COMPONENT_STATUS_ERROR
;