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/plugin/plugin-dev.h>
30 #include <babeltrace/graph/component.h>
31 #include <babeltrace/graph/private-component.h>
32 #include <babeltrace/graph/component-filter.h>
33 #include <babeltrace/graph/notification.h>
34 #include <babeltrace/graph/notification-iterator.h>
35 #include <babeltrace/graph/private-notification-iterator.h>
36 #include <babeltrace/graph/notification-event.h>
37 #include <plugins-common.h>
43 void destroy_trimmer_data(struct trimmer
*trimmer
)
49 struct trimmer
*create_trimmer_data(void)
51 struct trimmer
*trimmer
;
53 trimmer
= g_new0(struct trimmer
, 1);
61 void finalize_trimmer(struct bt_private_component
*component
)
63 void *data
= bt_private_component_get_user_data(component
);
65 destroy_trimmer_data(data
);
69 * Parses a timestamp, figuring out its format.
71 * Returns a negative value if anything goes wrong.
75 * YYYY-MM-DD hh:mm:ss.ns
85 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
86 struct trimmer_bound
*result_bound
, bool gmt
)
90 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
92 /* YYYY-MM-DD hh:mm:ss.ns */
93 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
94 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
102 .tm_year
= year
- 1900,
108 result
= timegm(&tm
);
113 result
= mktime(&tm
);
118 value
= (int64_t) result
;
119 value
*= NSEC_PER_SEC
;
121 if (!trimmer
->date
) {
122 trimmer
->year
= year
;
123 trimmer
->month
= month
;
125 trimmer
->date
= true;
130 ret
= sscanf(arg
, "%u:%u:%u.%u",
133 if (!trimmer
->date
) {
134 /* We don't know which day until we get an event. */
135 result_bound
->lazy_values
.hh
= hh
;
136 result_bound
->lazy_values
.mm
= mm
;
137 result_bound
->lazy_values
.ss
= ss
;
138 result_bound
->lazy_values
.ns
= ns
;
139 result_bound
->lazy_values
.gmt
= gmt
;
146 .tm_mday
= trimmer
->day
,
147 .tm_mon
= trimmer
->month
- 1,
148 .tm_year
= trimmer
->year
- 1900,
154 result
= timegm(&tm
);
159 result
= mktime(&tm
);
164 value
= (int64_t) result
;
165 value
*= NSEC_PER_SEC
;
171 ret
= sscanf(arg
, "-%u.%u",
174 value
= -ss
* NSEC_PER_SEC
;
179 ret
= sscanf(arg
, "%u.%u",
182 value
= ss
* NSEC_PER_SEC
;
187 /* YYYY-MM-DD hh:mm:ss */
188 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
189 &year
, &month
, &day
, &hh
, &mm
, &ss
);
197 .tm_year
= year
- 1900,
212 value
*= NSEC_PER_SEC
;
213 if (!trimmer
->date
) {
214 trimmer
->year
= year
;
215 trimmer
->month
= month
;
217 trimmer
->date
= true;
222 ret
= sscanf(arg
, "%u:%u:%u",
225 if (!trimmer
->date
) {
226 /* We don't know which day until we get an event. */
227 result_bound
->lazy_values
.hh
= hh
;
228 result_bound
->lazy_values
.mm
= mm
;
229 result_bound
->lazy_values
.ss
= ss
;
230 result_bound
->lazy_values
.ns
= 0;
231 result_bound
->lazy_values
.gmt
= gmt
;
238 .tm_mday
= trimmer
->day
,
239 .tm_mon
= trimmer
->month
- 1,
240 .tm_year
= trimmer
->year
- 1900,
246 result
= timegm(&tm
);
251 result
= mktime(&tm
);
256 value
= (int64_t) result
;
257 value
*= NSEC_PER_SEC
;
262 ret
= sscanf(arg
, "-%u",
265 value
= -ss
* NSEC_PER_SEC
;
269 ret
= sscanf(arg
, "%u",
272 value
= ss
* NSEC_PER_SEC
;
280 result_bound
->value
= value
;
281 result_bound
->set
= true;
285 result_bound
->lazy
= true;
290 enum bt_component_status
init_from_params(struct trimmer
*trimmer
,
291 struct bt_value
*params
)
293 struct bt_value
*value
= NULL
;
295 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
299 value
= bt_value_map_get(params
, "clock-gmt");
301 enum bt_value_status value_ret
;
303 value_ret
= bt_value_bool_get(value
, &gmt
);
305 ret
= BT_COMPONENT_STATUS_INVALID
;
306 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
310 if (ret
!= BT_COMPONENT_STATUS_OK
) {
314 value
= bt_value_map_get(params
, "begin");
316 enum bt_value_status value_ret
;
319 value_ret
= bt_value_string_get(value
, &str
);
320 if (value_ret
|| timestamp_from_arg(str
,
321 trimmer
, &trimmer
->begin
, gmt
)) {
322 ret
= BT_COMPONENT_STATUS_INVALID
;
323 printf_error("Failed to retrieve begin value. Expecting a timestamp string");
327 if (ret
!= BT_COMPONENT_STATUS_OK
) {
331 value
= bt_value_map_get(params
, "end");
333 enum bt_value_status value_ret
;
336 value_ret
= bt_value_string_get(value
, &str
);
337 if (value_ret
|| timestamp_from_arg(str
,
338 trimmer
, &trimmer
->end
, gmt
)) {
339 ret
= BT_COMPONENT_STATUS_INVALID
;
340 printf_error("Failed to retrieve end value. Expecting a timestamp string");
345 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
346 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
347 printf_error("Unexpected: time range begin value is above end value");
348 ret
= BT_COMPONENT_STATUS_INVALID
;
354 enum bt_component_status
trimmer_component_init(
355 struct bt_private_component
*component
, struct bt_value
*params
,
356 UNUSED_VAR
void *init_method_data
)
358 enum bt_component_status ret
;
359 struct trimmer
*trimmer
= create_trimmer_data();
362 ret
= BT_COMPONENT_STATUS_NOMEM
;
366 ret
= bt_private_component_set_user_data(component
, trimmer
);
367 if (ret
!= BT_COMPONENT_STATUS_OK
) {
371 ret
= init_from_params(trimmer
, params
);
375 destroy_trimmer_data(trimmer
);