text plugin: add color support
[babeltrace.git] / plugins / trimmer / iterator.c
CommitLineData
cab3f160
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Trace Trimmer Iterator
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29#include "trimmer.h"
30#include "iterator.h"
33b34c43
PP
31#include <babeltrace/component/notification/iterator.h>
32#include <babeltrace/component/notification/notification.h>
33#include <babeltrace/component/notification/event.h>
34#include <babeltrace/component/notification/stream.h>
35#include <babeltrace/component/notification/packet.h>
d71dcf2c 36#include <babeltrace/component/component-filter.h>
44d3cbf0
JG
37#include <babeltrace/ctf-ir/event.h>
38#include <babeltrace/ctf-ir/stream.h>
39#include <babeltrace/ctf-ir/stream-class.h>
ac0c6bdd 40#include <babeltrace/ctf-ir/clock-class.h>
44d3cbf0
JG
41#include <babeltrace/ctf-ir/packet.h>
42#include <babeltrace/ctf-ir/trace.h>
72208f03 43#include <babeltrace/ctf-ir/fields.h>
44d3cbf0 44#include <assert.h>
8b0ce102 45#include <plugins-common.h>
44d3cbf0 46
d3eb6e8f 47BT_HIDDEN
44d3cbf0
JG
48void trimmer_iterator_destroy(struct bt_notification_iterator *it)
49{
50 struct trimmer_iterator *it_data;
51
52 it_data = bt_notification_iterator_get_private_data(it);
53 assert(it_data);
54
55 if (it_data->input_iterator_group) {
56 g_ptr_array_free(it_data->input_iterator_group, TRUE);
57 }
58 bt_put(it_data->current_notification);
44d3cbf0
JG
59 g_free(it_data);
60}
cab3f160
JG
61
62BT_HIDDEN
d3eb6e8f
PP
63enum bt_notification_iterator_status trimmer_iterator_init(
64 struct bt_component *component,
8b0ce102
PP
65 struct bt_notification_iterator *iterator,
66 UNUSED_VAR void *init_method_data)
cab3f160 67{
d3eb6e8f
PP
68 enum bt_notification_iterator_status ret =
69 BT_NOTIFICATION_ITERATOR_STATUS_OK;
cab3f160 70 enum bt_notification_iterator_status it_ret;
44d3cbf0
JG
71 struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1);
72
73 if (!it_data) {
d3eb6e8f 74 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
44d3cbf0
JG
75 goto end;
76 }
77
78 /* FIXME init trimmer_iterator */
79 it_ret = bt_notification_iterator_set_private_data(iterator, it_data);
80 if (it_ret) {
81 goto end;
82 }
cab3f160
JG
83end:
84 return ret;
85}
86
87BT_HIDDEN
88struct bt_notification *trimmer_iterator_get(
89 struct bt_notification_iterator *iterator)
90{
44d3cbf0
JG
91 struct trimmer_iterator *trim_it;
92
93 trim_it = bt_notification_iterator_get_private_data(iterator);
94 assert(trim_it);
95
96 if (!trim_it->current_notification) {
97 enum bt_notification_iterator_status it_ret;
98
99 it_ret = trimmer_iterator_next(iterator);
100 if (it_ret) {
101 goto end;
102 }
103 }
104end:
105 return bt_get(trim_it->current_notification);
106}
107
528debdf
MD
108static
109int update_lazy_bound(struct trimmer_bound *bound, const char *name,
55595636 110 int64_t ts, bool *lazy_update)
528debdf
MD
111{
112 struct tm tm;
113 int64_t value;
114 time_t timeval;
115
55595636
MD
116 *lazy_update = false;
117
528debdf
MD
118 if (!bound->lazy) {
119 return 0;
120 }
121 tm.tm_isdst = -1;
122 timeval = ts / NSEC_PER_SEC;
123
124 if (bound->lazy_values.gmt) {
125 /* Get day, month, year. */
126 if (!gmtime_r(&timeval, &tm)) {
55595636 127 printf_error("Failure in gmtime_r()");
528debdf
MD
128 goto error;
129 }
130 tm.tm_sec = bound->lazy_values.ss;
131 tm.tm_min = bound->lazy_values.mm;
132 tm.tm_hour = bound->lazy_values.hh;
133 timeval = timegm(&tm);
134 if (timeval < 0) {
55595636
MD
135 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
136 name);
528debdf
MD
137 goto error;
138 }
139 } else {
140 /* Get day, month, year. */
141 if (!localtime_r(&timeval, &tm)) {
55595636 142 printf_error("Failure in localtime_r()");
528debdf
MD
143 goto error;
144 }
145 tm.tm_sec = bound->lazy_values.ss;
146 tm.tm_min = bound->lazy_values.mm;
147 tm.tm_hour = bound->lazy_values.hh;
148 timeval = mktime(&tm);
149 if (timeval < 0) {
55595636 150 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
528debdf
MD
151 name);
152 goto error;
153 }
154 }
155 value = (int64_t) timeval;
156 value *= NSEC_PER_SEC;
157 value += bound->lazy_values.ns;
158 bound->value = value;
159 bound->set = true;
160 bound->lazy = false;
55595636 161 *lazy_update = true;
528debdf
MD
162 return 0;
163
164error:
528debdf
MD
165 return -1;
166}
167
44d3cbf0 168static
55595636
MD
169enum bt_notification_iterator_status
170evaluate_event_notification(struct bt_notification *notification,
171 struct trimmer_bound *begin, struct trimmer_bound *end,
172 bool *_event_in_range)
44d3cbf0 173{
72208f03
JG
174 int64_t ts;
175 int clock_ret;
176 struct bt_ctf_event *event = NULL;
177 bool in_range = true;
ac0c6bdd 178 struct bt_ctf_clock_class *clock_class = NULL;
72208f03 179 struct bt_ctf_trace *trace = NULL;
44d3cbf0 180 struct bt_ctf_stream *stream = NULL;
72208f03
JG
181 struct bt_ctf_stream_class *stream_class = NULL;
182 struct bt_ctf_clock_value *clock_value = NULL;
55595636
MD
183 enum bt_notification_iterator_status ret =
184 BT_NOTIFICATION_ITERATOR_STATUS_OK;
185 bool lazy_update = false;
44d3cbf0 186
72208f03
JG
187 event = bt_notification_event_get_event(notification);
188 assert(event);
44d3cbf0 189
72208f03
JG
190 stream = bt_ctf_event_get_stream(event);
191 assert(stream);
44d3cbf0 192
72208f03
JG
193 stream_class = bt_ctf_stream_get_class(stream);
194 assert(stream_class);
195
196 trace = bt_ctf_stream_class_get_trace(stream_class);
197 assert(trace);
198
199 /* FIXME multi-clock? */
ac0c6bdd
PP
200 clock_class = bt_ctf_trace_get_clock_class(trace, 0);
201 if (!clock_class) {
72208f03 202 goto end;
44d3cbf0 203 }
44d3cbf0 204
ac0c6bdd 205 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
72208f03 206 if (!clock_value) {
55595636
MD
207 printf_error("Failed to retrieve clock value");
208 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
72208f03 209 goto end;
44d3cbf0 210 }
72208f03
JG
211
212 clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch(
213 clock_value, &ts);
214 if (clock_ret) {
55595636
MD
215 printf_error("Failed to retrieve clock value timestamp");
216 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
44d3cbf0
JG
217 goto end;
218 }
55595636
MD
219 if (update_lazy_bound(begin, "begin", ts, &lazy_update)) {
220 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
221 goto end;
222 }
55595636
MD
223 if (update_lazy_bound(end, "end", ts, &lazy_update)) {
224 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
225 goto end;
226 }
55595636
MD
227 if (lazy_update && begin->set && end->set) {
228 if (begin->value > end->value) {
229 printf_error("Unexpected: time range begin value is above end value");
230 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
231 goto end;
232 }
233 }
72208f03
JG
234 if (begin->set && ts < begin->value) {
235 in_range = false;
236 }
237 if (end->set && ts > end->value) {
238 in_range = false;
239 }
44d3cbf0 240end:
72208f03 241 bt_put(event);
ac0c6bdd 242 bt_put(clock_class);
72208f03
JG
243 bt_put(trace);
244 bt_put(stream);
245 bt_put(stream_class);
246 bt_put(clock_value);
55595636
MD
247 *_event_in_range = in_range;
248 return ret;
44d3cbf0
JG
249}
250
44d3cbf0 251static
72208f03 252int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns)
44d3cbf0 253{
72208f03
JG
254 int ret = 0;
255 int is_signed;
256 uint64_t raw_clock_value;
257 struct bt_ctf_field_type *integer_type = NULL;
ac0c6bdd 258 struct bt_ctf_clock_class *clock_class = NULL;
44d3cbf0
JG
259 struct bt_ctf_clock_value *clock_value = NULL;
260
72208f03
JG
261 integer_type = bt_ctf_field_get_type(integer);
262 assert(integer_type);
ac0c6bdd
PP
263 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
264 integer_type);
265 if (!clock_class) {
72208f03 266 ret = -1;
44d3cbf0
JG
267 goto end;
268 }
269
72208f03
JG
270 is_signed = bt_ctf_field_type_integer_get_signed(integer_type);
271 if (!is_signed) {
272 ret = bt_ctf_field_unsigned_integer_get_value(integer,
273 &raw_clock_value);
274 if (ret) {
44d3cbf0
JG
275 goto end;
276 }
72208f03
JG
277 } else {
278 /* Signed clock values are unsupported. */
279 goto end;
280 }
44d3cbf0 281
ac0c6bdd 282 clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value);
72208f03
JG
283 if (!clock_value) {
284 goto end;
285 }
44d3cbf0 286
72208f03
JG
287 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
288end:
289 bt_put(integer_type);
ac0c6bdd 290 bt_put(clock_class);
72208f03
JG
291 bt_put(clock_value);
292 return ret;
293}
44d3cbf0 294
72208f03 295static
55595636
MD
296enum bt_notification_iterator_status evaluate_packet_notification(
297 struct bt_notification *notification,
298 struct trimmer_bound *begin, struct trimmer_bound *end,
299 bool *_packet_in_range)
72208f03 300{
55595636
MD
301 enum bt_notification_iterator_status ret =
302 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03
JG
303 int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns;
304 bool in_range = true;
305 struct bt_ctf_packet *packet = NULL;
306 struct bt_ctf_field *packet_context = NULL,
307 *timestamp_begin = NULL,
308 *timestamp_end = NULL;
309
310 switch (bt_notification_get_type(notification)) {
311 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
312 packet = bt_notification_packet_begin_get_packet(notification);
44d3cbf0 313 break;
72208f03
JG
314 case BT_NOTIFICATION_TYPE_PACKET_END:
315 packet = bt_notification_packet_end_get_packet(notification);
316 break;
317 default:
55595636 318 break;
44d3cbf0 319 }
72208f03
JG
320 assert(packet);
321
322 packet_context = bt_ctf_packet_get_context(packet);
323 if (!packet_context) {
324 goto end;
325 }
326
327 if (!bt_ctf_field_is_structure(packet_context)) {
328 goto end;
329 }
330
331 timestamp_begin = bt_ctf_field_structure_get_field(
332 packet_context, "timestamp_begin");
333 if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) {
334 goto end;
335 }
336 timestamp_end = bt_ctf_field_structure_get_field(
337 packet_context, "timestamp_end");
338 if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) {
339 goto end;
340 }
341
55595636 342 if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) {
72208f03
JG
343 goto end;
344 }
55595636 345 if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) {
72208f03
JG
346 goto end;
347 }
348
349 begin_ns = begin->set ? begin->value : INT64_MIN;
350 end_ns = end->set ? end->value : INT64_MAX;
351
352 /*
353 * Accept if there is any overlap between the selected region and the
354 * packet.
355 */
356 in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns);
357end:
55595636 358 *_packet_in_range = in_range;
72208f03
JG
359 bt_put(packet);
360 bt_put(packet_context);
361 bt_put(timestamp_begin);
362 bt_put(timestamp_end);
55595636 363 return ret;
72208f03
JG
364}
365
366/* Return true if the notification should be forwarded. */
367static
55595636
MD
368enum bt_notification_iterator_status evaluate_notification(
369 struct bt_notification *notification,
370 struct trimmer_bound *begin, struct trimmer_bound *end,
371 bool *in_range)
72208f03 372{
72208f03 373 enum bt_notification_type type;
55595636
MD
374 enum bt_notification_iterator_status ret =
375 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03 376
b61397c4 377 *in_range = true;
72208f03
JG
378 type = bt_notification_get_type(notification);
379 switch (type) {
380 case BT_NOTIFICATION_TYPE_EVENT:
55595636
MD
381 ret = evaluate_event_notification(notification, begin,
382 end, in_range);
72208f03
JG
383 break;
384 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
385 case BT_NOTIFICATION_TYPE_PACKET_END:
55595636
MD
386 ret = evaluate_packet_notification(notification, begin,
387 end, in_range);
72208f03 388 break;
44d3cbf0 389 default:
72208f03 390 /* Accept all other notifications. */
44d3cbf0
JG
391 break;
392 }
55595636 393 return ret;
cab3f160
JG
394}
395
396BT_HIDDEN
397enum bt_notification_iterator_status trimmer_iterator_next(
398 struct bt_notification_iterator *iterator)
399{
44d3cbf0
JG
400 struct trimmer_iterator *trim_it = NULL;
401 struct bt_component *component = NULL;
402 struct trimmer *trimmer = NULL;
403 struct bt_notification_iterator *source_it = NULL;
404 enum bt_notification_iterator_status ret =
55595636 405 BT_NOTIFICATION_ITERATOR_STATUS_OK;
44d3cbf0 406 enum bt_component_status component_ret;
72208f03 407 bool notification_in_range = false;
cab3f160 408
44d3cbf0
JG
409 trim_it = bt_notification_iterator_get_private_data(iterator);
410 assert(trim_it);
411
412 component = bt_notification_iterator_get_component(iterator);
413 assert(component);
414 trimmer = bt_component_get_private_data(component);
415 assert(trimmer);
416
417 /* FIXME, should handle input iterator groups. */
418 component_ret = bt_component_filter_get_input_iterator(component, 0,
419 &source_it);
420 assert((component_ret == BT_COMPONENT_STATUS_OK) && source_it);
421
72208f03 422 while (!notification_in_range) {
44d3cbf0
JG
423 struct bt_notification *notification;
424
425 ret = bt_notification_iterator_next(source_it);
426 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
427 goto end;
428 }
429
430 notification = bt_notification_iterator_get_notification(
431 source_it);
432 if (!notification) {
433 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
434 goto end;
435 }
436
55595636
MD
437 ret = evaluate_notification(notification,
438 &trimmer->begin, &trimmer->end,
439 &notification_in_range);
72208f03 440 if (notification_in_range) {
44d3cbf0
JG
441 BT_MOVE(trim_it->current_notification, notification);
442 } else {
443 bt_put(notification);
444 }
6d5f6792
JG
445
446 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
447 break;
448 }
44d3cbf0 449 }
cab3f160 450end:
44d3cbf0
JG
451 bt_put(source_it);
452 bt_put(component);
cab3f160
JG
453 return ret;
454}
455
456BT_HIDDEN
457enum bt_notification_iterator_status trimmer_iterator_seek_time(
458 struct bt_notification_iterator *iterator, int64_t time)
459{
460 enum bt_notification_iterator_status ret;
461
462 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
463end:
464 return ret;
465}
This page took 0.04736 seconds and 4 git commands to generate.