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>
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(struct bt_private_component
*component
)
53 void *data
= bt_private_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_arg(const char *arg
, struct trimmer
*trimmer
,
76 struct trimmer_bound
*result_bound
, bt_bool gmt
)
80 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
82 /* YYYY-MM-DD hh:mm:ss.ns */
83 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
84 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
92 .tm_year
= year
- 1900,
98 result
= bt_timegm(&tm
);
103 result
= mktime(&tm
);
108 value
= (int64_t) result
;
109 value
*= NSEC_PER_SEC
;
111 if (!trimmer
->date
) {
112 trimmer
->year
= year
;
113 trimmer
->month
= month
;
115 trimmer
->date
= true;
120 ret
= sscanf(arg
, "%u:%u:%u.%u",
123 if (!trimmer
->date
) {
124 /* We don't know which day until we get an event. */
125 result_bound
->lazy_values
.hh
= hh
;
126 result_bound
->lazy_values
.mm
= mm
;
127 result_bound
->lazy_values
.ss
= ss
;
128 result_bound
->lazy_values
.ns
= ns
;
129 result_bound
->lazy_values
.gmt
= gmt
;
136 .tm_mday
= trimmer
->day
,
137 .tm_mon
= trimmer
->month
- 1,
138 .tm_year
= trimmer
->year
- 1900,
144 result
= bt_timegm(&tm
);
149 result
= mktime(&tm
);
154 value
= (int64_t) result
;
155 value
*= NSEC_PER_SEC
;
161 ret
= sscanf(arg
, "-%u.%u",
164 value
= -ss
* NSEC_PER_SEC
;
169 ret
= sscanf(arg
, "%u.%u",
172 value
= ss
* NSEC_PER_SEC
;
177 /* YYYY-MM-DD hh:mm:ss */
178 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
179 &year
, &month
, &day
, &hh
, &mm
, &ss
);
187 .tm_year
= year
- 1900,
192 value
= bt_timegm(&tm
);
202 value
*= NSEC_PER_SEC
;
203 if (!trimmer
->date
) {
204 trimmer
->year
= year
;
205 trimmer
->month
= month
;
207 trimmer
->date
= true;
212 ret
= sscanf(arg
, "%u:%u:%u",
215 if (!trimmer
->date
) {
216 /* We don't know which day until we get an event. */
217 result_bound
->lazy_values
.hh
= hh
;
218 result_bound
->lazy_values
.mm
= mm
;
219 result_bound
->lazy_values
.ss
= ss
;
220 result_bound
->lazy_values
.ns
= 0;
221 result_bound
->lazy_values
.gmt
= gmt
;
228 .tm_mday
= trimmer
->day
,
229 .tm_mon
= trimmer
->month
- 1,
230 .tm_year
= trimmer
->year
- 1900,
236 result
= bt_timegm(&tm
);
241 result
= mktime(&tm
);
246 value
= (int64_t) result
;
247 value
*= NSEC_PER_SEC
;
252 ret
= sscanf(arg
, "-%u",
255 value
= -ss
* NSEC_PER_SEC
;
259 ret
= sscanf(arg
, "%u",
262 value
= ss
* NSEC_PER_SEC
;
270 result_bound
->value
= value
;
271 result_bound
->set
= true;
275 result_bound
->lazy
= true;
280 enum bt_component_status
init_from_params(struct trimmer
*trimmer
,
281 struct bt_value
*params
)
283 struct bt_value
*value
= NULL
;
284 bt_bool gmt
= BT_FALSE
;
285 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
289 value
= bt_value_map_get(params
, "clock-gmt");
291 enum bt_value_status value_ret
;
293 value_ret
= bt_value_bool_get(value
, &gmt
);
295 ret
= BT_COMPONENT_STATUS_INVALID
;
296 BT_LOGE_STR("Failed to retrieve clock-gmt value. Expecting a boolean");
300 if (ret
!= BT_COMPONENT_STATUS_OK
) {
304 value
= bt_value_map_get(params
, "begin");
306 enum bt_value_status value_ret
;
309 value_ret
= bt_value_string_get(value
, &str
);
310 if (value_ret
|| timestamp_from_arg(str
,
311 trimmer
, &trimmer
->begin
, gmt
)) {
312 ret
= BT_COMPONENT_STATUS_INVALID
;
313 BT_LOGE_STR("Failed to retrieve begin value. Expecting a timestamp string");
317 if (ret
!= BT_COMPONENT_STATUS_OK
) {
321 value
= bt_value_map_get(params
, "end");
323 enum bt_value_status value_ret
;
326 value_ret
= bt_value_string_get(value
, &str
);
327 if (value_ret
|| timestamp_from_arg(str
,
328 trimmer
, &trimmer
->end
, gmt
)) {
329 ret
= BT_COMPONENT_STATUS_INVALID
;
330 BT_LOGE_STR("Failed to retrieve end value. Expecting a timestamp string");
335 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
336 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
337 BT_LOGE_STR("Unexpected: time range begin value is above end value");
338 ret
= BT_COMPONENT_STATUS_INVALID
;
344 enum bt_component_status
trimmer_component_init(
345 struct bt_private_component
*component
, struct bt_value
*params
,
346 UNUSED_VAR
void *init_method_data
)
348 enum bt_component_status ret
;
349 struct trimmer
*trimmer
= create_trimmer_data();
352 ret
= BT_COMPONENT_STATUS_NOMEM
;
356 /* Create input and output ports */
357 ret
= bt_private_component_filter_add_input_private_port(
358 component
, "in", NULL
, NULL
);
359 if (ret
!= BT_COMPONENT_STATUS_OK
) {
363 ret
= bt_private_component_filter_add_output_private_port(
364 component
, "out", NULL
, NULL
);
365 if (ret
!= BT_COMPONENT_STATUS_OK
) {
369 ret
= bt_private_component_set_user_data(component
, trimmer
);
370 if (ret
!= BT_COMPONENT_STATUS_OK
) {
374 ret
= init_from_params(trimmer
, params
);
378 destroy_trimmer_data(trimmer
);
379 ret
= BT_COMPONENT_STATUS_ERROR
;