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/babeltrace.h>
34 #include <plugins-common.h>
37 #include <babeltrace/assert-internal.h>
40 void destroy_trimmer_data(struct trimmer
*trimmer
)
46 struct trimmer
*create_trimmer_data(void)
48 return g_new0(struct trimmer
, 1);
51 void finalize_trimmer(bt_self_component
*component
)
53 void *data
= bt_self_component_get_user_data(component
);
55 destroy_trimmer_data(data
);
59 * Parses a timestamp, figuring out its format.
61 * Returns a negative value if anything goes wrong.
65 * YYYY-MM-DD hh:mm:ss.ns
75 int timestamp_from_param(const char *param_name
, bt_value
*param
,
76 struct trimmer
*trimmer
, struct trimmer_bound
*result_bound
,
81 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
84 if (bt_value_is_integer(param
)) {
85 (void) bt_value_integer_get(param
, &value
);
87 } else if (!bt_value_is_string(param
)) {
88 BT_LOGE("`%s` parameter must be an integer or a string value.",
93 (void) bt_value_string_get(param
, &arg
);
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
);
105 .tm_year
= year
- 1900,
111 result
= bt_timegm(&tm
);
116 result
= mktime(&tm
);
121 value
= (int64_t) result
;
122 value
*= NSEC_PER_SEC
;
124 if (!trimmer
->date
) {
125 trimmer
->year
= year
;
126 trimmer
->month
= month
;
128 trimmer
->date
= true;
133 ret
= sscanf(arg
, "%u:%u:%u.%u",
136 if (!trimmer
->date
) {
137 /* We don't know which day until we get an event. */
138 result_bound
->lazy_value
.hh
= hh
;
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
;
149 .tm_mday
= trimmer
->day
,
150 .tm_mon
= trimmer
->month
- 1,
151 .tm_year
= trimmer
->year
- 1900,
157 result
= bt_timegm(&tm
);
162 result
= mktime(&tm
);
167 value
= (int64_t) result
;
168 value
*= NSEC_PER_SEC
;
174 ret
= sscanf(arg
, "-%u.%u",
177 value
= -ss
* NSEC_PER_SEC
;
182 ret
= sscanf(arg
, "%u.%u",
185 value
= ss
* NSEC_PER_SEC
;
190 /* YYYY-MM-DD hh:mm:ss */
191 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
192 &year
, &month
, &day
, &hh
, &mm
, &ss
);
200 .tm_year
= year
- 1900,
205 value
= bt_timegm(&tm
);
215 value
*= NSEC_PER_SEC
;
216 if (!trimmer
->date
) {
217 trimmer
->year
= year
;
218 trimmer
->month
= month
;
220 trimmer
->date
= true;
225 ret
= sscanf(arg
, "%u:%u:%u",
228 if (!trimmer
->date
) {
229 /* We don't know which day until we get an event. */
230 result_bound
->lazy_value
.hh
= hh
;
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
;
241 .tm_mday
= trimmer
->day
,
242 .tm_mon
= trimmer
->month
- 1,
243 .tm_year
= trimmer
->year
- 1900,
249 result
= bt_timegm(&tm
);
254 result
= mktime(&tm
);
259 value
= (int64_t) result
;
260 value
*= NSEC_PER_SEC
;
265 ret
= sscanf(arg
, "-%u",
268 value
= -ss
* NSEC_PER_SEC
;
272 ret
= sscanf(arg
, "%u",
275 value
= ss
* NSEC_PER_SEC
;
284 result_bound
->value
= value
;
285 result_bound
->set
= true;
289 result_bound
->lazy
= true;
294 bt_component_status
init_from_params(struct trimmer
*trimmer
,
297 bt_value
*value
= NULL
;
298 bt_bool gmt
= BT_FALSE
;
299 bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
303 value
= bt_value_map_get(params
, "clock-gmt");
305 bt_value_status value_ret
;
307 gmt
= bt_value_bool_get(value
);
309 if (ret
!= BT_COMPONENT_STATUS_OK
) {
313 BT_VALUE_PUT_REF_AND_RESET(value
);
314 value
= bt_value_map_get(params
, "begin");
316 if (timestamp_from_param("begin", value
,
317 trimmer
, &trimmer
->begin
, gmt
)) {
318 BT_LOGE_STR("Failed to convert `begin` parameter to a timestamp.");
319 ret
= BT_COMPONENT_STATUS_INVALID
;
324 BT_VALUE_PUT_REF_AND_RESET(value
);
325 value
= bt_value_map_get(params
, "end");
327 if (timestamp_from_param("end", value
,
328 trimmer
, &trimmer
->end
, gmt
)) {
329 BT_LOGE_STR("Failed to convert `end` parameter to a timestamp.");
330 ret
= BT_COMPONENT_STATUS_INVALID
;
336 bt_value_put_ref(value
);
338 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
339 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
340 BT_LOGE_STR("Unexpected: time range begin value is above end value");
341 ret
= BT_COMPONENT_STATUS_INVALID
;
347 bt_component_status
trimmer_component_init(
348 bt_self_component
*component
, bt_value
*params
,
349 UNUSED_VAR
void *init_method_data
)
351 bt_component_status ret
;
352 struct trimmer
*trimmer
= create_trimmer_data();
355 ret
= BT_COMPONENT_STATUS_NOMEM
;
359 /* Create input and output ports */
360 ret
= bt_self_component_filter_add_input_port(
361 component
, "in", NULL
, NULL
);
362 if (ret
!= BT_COMPONENT_STATUS_OK
) {
366 ret
= bt_self_component_filter_add_output_port(
367 component
, "out", NULL
, NULL
);
368 if (ret
!= BT_COMPONENT_STATUS_OK
) {
372 ret
= bt_self_component_set_user_data(component
, trimmer
);
373 if (ret
!= BT_COMPONENT_STATUS_OK
) {
377 ret
= init_from_params(trimmer
, params
);
381 destroy_trimmer_data(trimmer
);
382 ret
= BT_COMPONENT_STATUS_ERROR
;