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 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT"
32 #include <babeltrace/compat/utc-internal.h>
33 #include <babeltrace/plugin/plugin-dev.h>
34 #include <babeltrace/graph/component.h>
35 #include <babeltrace/graph/private-component.h>
36 #include <babeltrace/graph/component-filter.h>
37 #include <babeltrace/graph/notification.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
40 #include <babeltrace/graph/private-component-filter.h>
41 #include <babeltrace/graph/notification-event.h>
42 #include <plugins-common.h>
48 void destroy_trimmer_data(struct trimmer
*trimmer
)
54 struct trimmer
*create_trimmer_data(void)
56 return g_new0(struct trimmer
, 1);
59 void finalize_trimmer(struct bt_private_component
*component
)
61 void *data
= bt_private_component_get_user_data(component
);
63 destroy_trimmer_data(data
);
67 * Parses a timestamp, figuring out its format.
69 * Returns a negative value if anything goes wrong.
73 * YYYY-MM-DD hh:mm:ss.ns
83 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
84 struct trimmer_bound
*result_bound
, bt_bool gmt
)
88 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
90 /* YYYY-MM-DD hh:mm:ss.ns */
91 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
92 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
100 .tm_year
= year
- 1900,
106 result
= bt_timegm(&tm
);
111 result
= mktime(&tm
);
116 value
= (int64_t) result
;
117 value
*= NSEC_PER_SEC
;
119 if (!trimmer
->date
) {
120 trimmer
->year
= year
;
121 trimmer
->month
= month
;
123 trimmer
->date
= true;
128 ret
= sscanf(arg
, "%u:%u:%u.%u",
131 if (!trimmer
->date
) {
132 /* We don't know which day until we get an event. */
133 result_bound
->lazy_values
.hh
= hh
;
134 result_bound
->lazy_values
.mm
= mm
;
135 result_bound
->lazy_values
.ss
= ss
;
136 result_bound
->lazy_values
.ns
= ns
;
137 result_bound
->lazy_values
.gmt
= gmt
;
144 .tm_mday
= trimmer
->day
,
145 .tm_mon
= trimmer
->month
- 1,
146 .tm_year
= trimmer
->year
- 1900,
152 result
= bt_timegm(&tm
);
157 result
= mktime(&tm
);
162 value
= (int64_t) result
;
163 value
*= NSEC_PER_SEC
;
169 ret
= sscanf(arg
, "-%u.%u",
172 value
= -ss
* NSEC_PER_SEC
;
177 ret
= sscanf(arg
, "%u.%u",
180 value
= ss
* NSEC_PER_SEC
;
185 /* YYYY-MM-DD hh:mm:ss */
186 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
187 &year
, &month
, &day
, &hh
, &mm
, &ss
);
195 .tm_year
= year
- 1900,
200 value
= bt_timegm(&tm
);
210 value
*= NSEC_PER_SEC
;
211 if (!trimmer
->date
) {
212 trimmer
->year
= year
;
213 trimmer
->month
= month
;
215 trimmer
->date
= true;
220 ret
= sscanf(arg
, "%u:%u:%u",
223 if (!trimmer
->date
) {
224 /* We don't know which day until we get an event. */
225 result_bound
->lazy_values
.hh
= hh
;
226 result_bound
->lazy_values
.mm
= mm
;
227 result_bound
->lazy_values
.ss
= ss
;
228 result_bound
->lazy_values
.ns
= 0;
229 result_bound
->lazy_values
.gmt
= gmt
;
236 .tm_mday
= trimmer
->day
,
237 .tm_mon
= trimmer
->month
- 1,
238 .tm_year
= trimmer
->year
- 1900,
244 result
= bt_timegm(&tm
);
249 result
= mktime(&tm
);
254 value
= (int64_t) result
;
255 value
*= NSEC_PER_SEC
;
260 ret
= sscanf(arg
, "-%u",
263 value
= -ss
* NSEC_PER_SEC
;
267 ret
= sscanf(arg
, "%u",
270 value
= ss
* NSEC_PER_SEC
;
278 result_bound
->value
= value
;
279 result_bound
->set
= true;
283 result_bound
->lazy
= true;
288 enum bt_component_status
init_from_params(struct trimmer
*trimmer
,
289 struct bt_value
*params
)
291 struct bt_value
*value
= NULL
;
292 bt_bool gmt
= BT_FALSE
;
293 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
297 value
= bt_value_map_get(params
, "clock-gmt");
299 enum bt_value_status value_ret
;
301 value_ret
= bt_value_bool_get(value
, &gmt
);
303 ret
= BT_COMPONENT_STATUS_INVALID
;
304 BT_LOGE_STR("Failed to retrieve clock-gmt value. Expecting a boolean");
308 if (ret
!= BT_COMPONENT_STATUS_OK
) {
312 value
= bt_value_map_get(params
, "begin");
314 enum bt_value_status value_ret
;
317 value_ret
= bt_value_string_get(value
, &str
);
318 if (value_ret
|| timestamp_from_arg(str
,
319 trimmer
, &trimmer
->begin
, gmt
)) {
320 ret
= BT_COMPONENT_STATUS_INVALID
;
321 BT_LOGE_STR("Failed to retrieve begin value. Expecting a timestamp string");
325 if (ret
!= BT_COMPONENT_STATUS_OK
) {
329 value
= bt_value_map_get(params
, "end");
331 enum bt_value_status value_ret
;
334 value_ret
= bt_value_string_get(value
, &str
);
335 if (value_ret
|| timestamp_from_arg(str
,
336 trimmer
, &trimmer
->end
, gmt
)) {
337 ret
= BT_COMPONENT_STATUS_INVALID
;
338 BT_LOGE_STR("Failed to retrieve end value. Expecting a timestamp string");
343 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
344 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
345 BT_LOGE_STR("Unexpected: time range begin value is above end value");
346 ret
= BT_COMPONENT_STATUS_INVALID
;
352 enum bt_component_status
trimmer_component_init(
353 struct bt_private_component
*component
, struct bt_value
*params
,
354 UNUSED_VAR
void *init_method_data
)
356 enum bt_component_status ret
;
357 struct trimmer
*trimmer
= create_trimmer_data();
360 ret
= BT_COMPONENT_STATUS_NOMEM
;
364 /* Create input and output ports */
365 ret
= bt_private_component_filter_add_input_private_port(
366 component
, "in", NULL
, NULL
);
367 if (ret
!= BT_COMPONENT_STATUS_OK
) {
371 ret
= bt_private_component_filter_add_output_private_port(
372 component
, "out", NULL
, NULL
);
373 if (ret
!= BT_COMPONENT_STATUS_OK
) {
377 ret
= bt_private_component_set_user_data(component
, trimmer
);
378 if (ret
!= BT_COMPONENT_STATUS_OK
) {
382 ret
= init_from_params(trimmer
, params
);
386 destroy_trimmer_data(trimmer
);
387 ret
= BT_COMPONENT_STATUS_ERROR
;