Commit | Line | Data |
---|---|---|
cab3f160 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
cab3f160 | 4 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7de0e49a | 5 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
cab3f160 JG |
6 | */ |
7 | ||
0d9a3d3e | 8 | #define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level) |
350ad6c1 | 9 | #define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER" |
d9c39b0a | 10 | #include "logging/comp-logging.h" |
b4565e8b | 11 | |
578e048b MJ |
12 | #include "compat/utc.h" |
13 | #include "compat/time.h" | |
3fadfbc0 | 14 | #include <babeltrace2/babeltrace.h> |
578e048b | 15 | #include "common/common.h" |
578e048b | 16 | #include "common/assert.h" |
c4f23e30 | 17 | #include <stdbool.h> |
7de0e49a PP |
18 | #include <stdint.h> |
19 | #include <inttypes.h> | |
20 | #include <glib.h> | |
188edac1 | 21 | #include "compat/glib.h" |
90a55a4e | 22 | #include "plugins/common/param-validation/param-validation.h" |
7de0e49a PP |
23 | |
24 | #include "trimmer.h" | |
25 | ||
26 | #define NS_PER_S INT64_C(1000000000) | |
27 | ||
28 | static const char * const in_port_name = "in"; | |
29 | ||
30 | struct trimmer_time { | |
31 | unsigned int hour, minute, second, ns; | |
32 | }; | |
33 | ||
34 | struct trimmer_bound { | |
35 | /* | |
36 | * Nanoseconds from origin, valid if `is_set` is set and | |
37 | * `is_infinite` is false. | |
38 | */ | |
39 | int64_t ns_from_origin; | |
40 | ||
41 | /* True if this bound's full time (`ns_from_origin`) is set */ | |
42 | bool is_set; | |
43 | ||
44 | /* | |
45 | * True if this bound represents the infinity (negative or | |
46 | * positive depending on which bound it is). If this is true, | |
47 | * then we don't care about `ns_from_origin` above. | |
48 | */ | |
49 | bool is_infinite; | |
50 | ||
51 | /* | |
52 | * This bound's time without the date; this time is used to set | |
53 | * `ns_from_origin` once we know the date. | |
54 | */ | |
55 | struct trimmer_time time; | |
56 | }; | |
57 | ||
58 | struct trimmer_comp { | |
59 | struct trimmer_bound begin, end; | |
60 | bool is_gmt; | |
0d9a3d3e | 61 | bt_logging_level log_level; |
ec4ae660 | 62 | bt_self_component *self_comp; |
a3f0c7db | 63 | bt_self_component_filter *self_comp_filter; |
7de0e49a PP |
64 | }; |
65 | ||
66 | enum trimmer_iterator_state { | |
67 | /* | |
68 | * Find the first message's date and set the bounds's times | |
69 | * accordingly. | |
70 | */ | |
71 | TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN, | |
72 | ||
73 | /* | |
74 | * Initially seek to the trimming range's beginning time. | |
75 | */ | |
76 | TRIMMER_ITERATOR_STATE_SEEK_INITIALLY, | |
77 | ||
78 | /* | |
79 | * Fill the output message queue for as long as received input | |
80 | * messages are within the trimming time range. | |
81 | */ | |
82 | TRIMMER_ITERATOR_STATE_TRIM, | |
83 | ||
84 | /* Flush the remaining messages in the output message queue */ | |
85 | TRIMMER_ITERATOR_STATE_ENDING, | |
86 | ||
87 | /* Trimming operation and message iterator is ended */ | |
88 | TRIMMER_ITERATOR_STATE_ENDED, | |
89 | }; | |
90 | ||
91 | struct trimmer_iterator { | |
92 | /* Weak */ | |
93 | struct trimmer_comp *trimmer_comp; | |
94 | ||
95 | /* Weak */ | |
96 | bt_self_message_iterator *self_msg_iter; | |
97 | ||
98 | enum trimmer_iterator_state state; | |
99 | ||
100 | /* Owned by this */ | |
9a2c8b8e | 101 | bt_message_iterator *upstream_iter; |
7de0e49a PP |
102 | struct trimmer_bound begin, end; |
103 | ||
104 | /* | |
105 | * Queue of `const bt_message *` (owned by the queue). | |
106 | * | |
107 | * This is where the trimming operation pushes the messages to | |
108 | * output by this message iterator. | |
109 | */ | |
110 | GQueue *output_messages; | |
111 | ||
112 | /* | |
113 | * Hash table of `bt_stream *` (weak) to | |
114 | * `struct trimmer_iterator_stream_state *` (owned by the HT). | |
115 | */ | |
116 | GHashTable *stream_states; | |
117 | }; | |
118 | ||
119 | struct trimmer_iterator_stream_state { | |
7de0e49a PP |
120 | /* Weak */ |
121 | const bt_stream *stream; | |
122 | ||
188edac1 SM |
123 | /* Have we seen a message with clock_snapshot going through this stream? */ |
124 | bool seen_clock_snapshot; | |
125 | ||
7de0e49a PP |
126 | /* Owned by this (`NULL` initially and between packets) */ |
127 | const bt_packet *cur_packet; | |
7de0e49a | 128 | }; |
cab3f160 JG |
129 | |
130 | static | |
7de0e49a | 131 | void destroy_trimmer_comp(struct trimmer_comp *trimmer_comp) |
cab3f160 | 132 | { |
7de0e49a PP |
133 | BT_ASSERT(trimmer_comp); |
134 | g_free(trimmer_comp); | |
cab3f160 JG |
135 | } |
136 | ||
137 | static | |
7de0e49a PP |
138 | struct trimmer_comp *create_trimmer_comp(void) |
139 | { | |
140 | return g_new0(struct trimmer_comp, 1); | |
141 | } | |
142 | ||
143 | BT_HIDDEN | |
144 | void trimmer_finalize(bt_self_component_filter *self_comp) | |
cab3f160 | 145 | { |
7de0e49a PP |
146 | struct trimmer_comp *trimmer_comp = |
147 | bt_self_component_get_data( | |
148 | bt_self_component_filter_as_self_component(self_comp)); | |
149 | ||
150 | if (trimmer_comp) { | |
151 | destroy_trimmer_comp(trimmer_comp); | |
152 | } | |
cab3f160 JG |
153 | } |
154 | ||
d4d7ffc9 SM |
155 | /* |
156 | * Compile regex in `pattern`, and try to match `string`. If there's a match, | |
157 | * return true and set `*match_info` to the list of matches. The list of | |
158 | * matches must be freed by the caller. If there's no match, return false and | |
159 | * set `*match_info` to NULL; | |
160 | */ | |
161 | static | |
162 | bool compile_and_match(const char *pattern, const char *string, GMatchInfo **match_info) { | |
163 | bool matches = false; | |
164 | GError *regex_error = NULL; | |
165 | GRegex *regex; | |
166 | ||
167 | regex = g_regex_new(pattern, 0, 0, ®ex_error); | |
168 | if (!regex) { | |
169 | goto end; | |
170 | } | |
171 | ||
172 | matches = g_regex_match(regex, string, 0, match_info); | |
173 | if (!matches) { | |
174 | /* | |
175 | * g_regex_match allocates `*match_info` even if it returns | |
176 | * FALSE. If there's no match, we have no use for it, so free | |
177 | * it immediatly and don't return it to the caller. | |
178 | */ | |
a42eab88 | 179 | g_match_info_free(*match_info); |
d4d7ffc9 SM |
180 | *match_info = NULL; |
181 | } | |
182 | ||
183 | g_regex_unref(regex); | |
184 | ||
185 | end: | |
186 | ||
187 | if (regex_error) { | |
188 | g_error_free(regex_error); | |
189 | } | |
190 | ||
191 | return matches; | |
192 | } | |
193 | ||
194 | /* | |
195 | * Convert the captured text in match number `match_num` in `match_info` | |
196 | * to an unsigned integer. | |
197 | */ | |
198 | static | |
199 | guint64 match_to_uint(const GMatchInfo *match_info, gint match_num) { | |
200 | gchar *text, *endptr; | |
201 | guint64 result; | |
202 | ||
203 | text = g_match_info_fetch(match_info, match_num); | |
204 | BT_ASSERT(text); | |
205 | ||
206 | /* | |
207 | * Because the input is carefully sanitized with regexes by the caller, | |
208 | * we assume that g_ascii_strtoull cannot fail. | |
209 | */ | |
210 | errno = 0; | |
211 | result = g_ascii_strtoull(text, &endptr, 10); | |
212 | BT_ASSERT(endptr > text); | |
213 | BT_ASSERT(errno == 0); | |
214 | ||
215 | g_free(text); | |
216 | ||
217 | return result; | |
218 | } | |
219 | ||
220 | /* | |
221 | * When parsing the nanoseconds part, .512 means .512000000, not .000000512. | |
222 | * This function is like match_to_uint, but multiplies the parsed number to get | |
223 | * the expected result. | |
224 | */ | |
225 | static | |
226 | guint64 match_to_uint_ns(const GMatchInfo *match_info, gint match_num) { | |
227 | guint64 nanoseconds; | |
228 | gboolean ret; | |
229 | gint start_pos, end_pos, power; | |
230 | static int pow10[] = { | |
231 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, | |
232 | }; | |
233 | ||
234 | nanoseconds = match_to_uint(match_info, match_num); | |
235 | ||
236 | /* Multiply by 10 as many times as there are omitted digits. */ | |
237 | ret = g_match_info_fetch_pos(match_info, match_num, &start_pos, &end_pos); | |
238 | BT_ASSERT(ret); | |
239 | ||
240 | power = 9 - (end_pos - start_pos); | |
241 | BT_ASSERT(power >= 0 && power <= 8); | |
242 | ||
243 | nanoseconds *= pow10[power]; | |
244 | ||
245 | return nanoseconds; | |
246 | } | |
247 | ||
7de0e49a PP |
248 | /* |
249 | * Sets the time (in ns from origin) of a trimmer bound from date and | |
250 | * time components. | |
251 | * | |
252 | * Returns a negative value if anything goes wrong. | |
253 | */ | |
254 | static | |
255 | int set_bound_ns_from_origin(struct trimmer_bound *bound, | |
256 | unsigned int year, unsigned int month, unsigned int day, | |
257 | unsigned int hour, unsigned int minute, unsigned int second, | |
258 | unsigned int ns, bool is_gmt) | |
cab3f160 | 259 | { |
7de0e49a PP |
260 | int ret = 0; |
261 | time_t result; | |
262 | struct tm tm = { | |
263 | .tm_sec = second, | |
264 | .tm_min = minute, | |
265 | .tm_hour = hour, | |
266 | .tm_mday = day, | |
267 | .tm_mon = month - 1, | |
268 | .tm_year = year - 1900, | |
269 | .tm_isdst = -1, | |
270 | }; | |
271 | ||
272 | if (is_gmt) { | |
273 | result = bt_timegm(&tm); | |
274 | } else { | |
275 | result = mktime(&tm); | |
276 | } | |
277 | ||
278 | if (result < 0) { | |
279 | ret = -1; | |
280 | goto end; | |
281 | } | |
cab3f160 | 282 | |
7de0e49a PP |
283 | BT_ASSERT(bound); |
284 | bound->ns_from_origin = (int64_t) result; | |
285 | bound->ns_from_origin *= NS_PER_S; | |
286 | bound->ns_from_origin += ns; | |
287 | bound->is_set = true; | |
288 | ||
289 | end: | |
290 | return ret; | |
cab3f160 JG |
291 | } |
292 | ||
528debdf MD |
293 | /* |
294 | * Parses a timestamp, figuring out its format. | |
295 | * | |
296 | * Returns a negative value if anything goes wrong. | |
297 | * | |
298 | * Expected formats: | |
299 | * | |
7de0e49a PP |
300 | * YYYY-MM-DD hh:mm[:ss[.ns]] |
301 | * [hh:mm:]ss[.ns] | |
302 | * [-]s[.ns] | |
303 | * | |
304 | * TODO: Check overflows. | |
305 | */ | |
306 | static | |
0d9a3d3e PP |
307 | int set_bound_from_str(struct trimmer_comp *trimmer_comp, |
308 | const char *str, struct trimmer_bound *bound, bool is_gmt) | |
7de0e49a | 309 | { |
d4d7ffc9 SM |
310 | /* Matches YYYY-MM-DD */ |
311 | #define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})" | |
312 | ||
313 | /* Matches HH:MM[:SS[.NS]] */ | |
314 | #define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?" | |
315 | ||
316 | /* Matches [-]SS[.NS] */ | |
317 | #define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$" | |
318 | ||
319 | GMatchInfo *match_info; | |
7de0e49a | 320 | int ret = 0; |
7de0e49a | 321 | |
d4d7ffc9 SM |
322 | /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */ |
323 | if (compile_and_match("^" DATE_RE " " TIME_RE "$", str, &match_info)) { | |
324 | unsigned int year = 0, month = 0, day = 0, hours = 0, minutes = 0, seconds = 0, nanoseconds = 0; | |
325 | gint match_count = g_match_info_get_match_count(match_info); | |
7de0e49a | 326 | |
d4d7ffc9 | 327 | BT_ASSERT(match_count >= 6 && match_count <= 8); |
7de0e49a | 328 | |
d4d7ffc9 SM |
329 | year = match_to_uint(match_info, 1); |
330 | month = match_to_uint(match_info, 2); | |
331 | day = match_to_uint(match_info, 3); | |
332 | hours = match_to_uint(match_info, 4); | |
333 | minutes = match_to_uint(match_info, 5); | |
7de0e49a | 334 | |
d4d7ffc9 SM |
335 | if (match_count >= 7) { |
336 | seconds = match_to_uint(match_info, 6); | |
337 | } | |
7de0e49a | 338 | |
d4d7ffc9 SM |
339 | if (match_count >= 8) { |
340 | nanoseconds = match_to_uint_ns(match_info, 7); | |
341 | } | |
342 | ||
343 | set_bound_ns_from_origin(bound, year, month, day, hours, minutes, seconds, nanoseconds, is_gmt); | |
7de0e49a | 344 | |
7de0e49a PP |
345 | goto end; |
346 | } | |
347 | ||
d4d7ffc9 SM |
348 | if (compile_and_match("^" DATE_RE "$", str, &match_info)) { |
349 | unsigned int year = 0, month = 0, day = 0; | |
350 | ||
351 | BT_ASSERT(g_match_info_get_match_count(match_info) == 4); | |
352 | ||
353 | year = match_to_uint(match_info, 1); | |
354 | month = match_to_uint(match_info, 2); | |
355 | day = match_to_uint(match_info, 3); | |
356 | ||
357 | set_bound_ns_from_origin(bound, year, month, day, 0, 0, 0, 0, is_gmt); | |
358 | ||
7de0e49a PP |
359 | goto end; |
360 | } | |
361 | ||
d4d7ffc9 SM |
362 | /* Try `hh:mm[:ss[.ns]]` format */ |
363 | if (compile_and_match("^" TIME_RE "$", str, &match_info)) { | |
364 | gint match_count = g_match_info_get_match_count(match_info); | |
365 | BT_ASSERT(match_count >= 3 && match_count <= 5); | |
366 | bound->time.hour = match_to_uint(match_info, 1); | |
367 | bound->time.minute = match_to_uint(match_info, 2); | |
368 | ||
369 | if (match_count >= 4) { | |
370 | bound->time.second = match_to_uint(match_info, 3); | |
371 | } | |
372 | ||
373 | if (match_count >= 5) { | |
374 | bound->time.ns = match_to_uint_ns(match_info, 4); | |
375 | } | |
376 | ||
7de0e49a PP |
377 | goto end; |
378 | } | |
379 | ||
d4d7ffc9 SM |
380 | /* Try `[-]s[.ns]` format */ |
381 | if (compile_and_match("^" S_NS_RE "$", str, &match_info)) { | |
382 | gboolean is_neg, fetch_pos_ret; | |
383 | gint start_pos, end_pos, match_count; | |
384 | guint64 seconds, nanoseconds = 0; | |
385 | ||
386 | match_count = g_match_info_get_match_count(match_info); | |
387 | BT_ASSERT(match_count >= 3 && match_count <= 4); | |
388 | ||
389 | /* Check for presence of negation sign. */ | |
390 | fetch_pos_ret = g_match_info_fetch_pos(match_info, 1, &start_pos, &end_pos); | |
391 | BT_ASSERT(fetch_pos_ret); | |
392 | is_neg = (end_pos - start_pos) > 0; | |
393 | ||
394 | seconds = match_to_uint(match_info, 2); | |
395 | ||
396 | if (match_count >= 4) { | |
397 | nanoseconds = match_to_uint_ns(match_info, 3); | |
398 | } | |
399 | ||
400 | bound->ns_from_origin = seconds * NS_PER_S + nanoseconds; | |
401 | ||
402 | if (is_neg) { | |
403 | bound->ns_from_origin = -bound->ns_from_origin; | |
404 | } | |
405 | ||
7de0e49a | 406 | bound->is_set = true; |
d4d7ffc9 | 407 | |
7de0e49a PP |
408 | goto end; |
409 | } | |
410 | ||
ab90ee94 SM |
411 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
412 | "Invalid date/time format: param=\"%s\"", str); | |
7de0e49a PP |
413 | ret = -1; |
414 | ||
415 | end: | |
f1083338 SM |
416 | g_match_info_free(match_info); |
417 | ||
7de0e49a PP |
418 | return ret; |
419 | } | |
420 | ||
421 | /* | |
422 | * Sets a trimmer bound's properties from a parameter string/integer | |
423 | * value. | |
424 | * | |
425 | * Returns a negative value if anything goes wrong. | |
528debdf | 426 | */ |
44d3cbf0 | 427 | static |
0d9a3d3e PP |
428 | int set_bound_from_param(struct trimmer_comp *trimmer_comp, |
429 | const char *param_name, const bt_value *param, | |
7de0e49a | 430 | struct trimmer_bound *bound, bool is_gmt) |
528debdf MD |
431 | { |
432 | int ret; | |
268fae9a | 433 | const char *arg; |
7de0e49a | 434 | char tmp_arg[64]; |
268fae9a | 435 | |
fdd3a2da | 436 | if (bt_value_is_signed_integer(param)) { |
9c08c816 | 437 | int64_t value = bt_value_integer_signed_get(param); |
7de0e49a PP |
438 | |
439 | /* | |
440 | * Just convert it to a temporary string to handle | |
441 | * everything the same way. | |
442 | */ | |
443 | sprintf(tmp_arg, "%" PRId64, value); | |
444 | arg = tmp_arg; | |
7de0e49a | 445 | } else { |
90a55a4e SM |
446 | BT_ASSERT(bt_value_is_string(param)); |
447 | arg = bt_value_string_get(param); | |
268fae9a PP |
448 | } |
449 | ||
0d9a3d3e | 450 | ret = set_bound_from_str(trimmer_comp, arg, bound, is_gmt); |
7de0e49a | 451 | |
7de0e49a PP |
452 | return ret; |
453 | } | |
454 | ||
455 | static | |
0d9a3d3e PP |
456 | int validate_trimmer_bounds(struct trimmer_comp *trimmer_comp, |
457 | struct trimmer_bound *begin, struct trimmer_bound *end) | |
7de0e49a PP |
458 | { |
459 | int ret = 0; | |
460 | ||
461 | BT_ASSERT(begin->is_set); | |
462 | BT_ASSERT(end->is_set); | |
463 | ||
464 | if (!begin->is_infinite && !end->is_infinite && | |
465 | begin->ns_from_origin > end->ns_from_origin) { | |
ab90ee94 SM |
466 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
467 | "Trimming time range's beginning time is greater than end time: " | |
7de0e49a PP |
468 | "begin-ns-from-origin=%" PRId64 ", " |
469 | "end-ns-from-origin=%" PRId64, | |
470 | begin->ns_from_origin, | |
471 | end->ns_from_origin); | |
472 | ret = -1; | |
473 | goto end; | |
528debdf MD |
474 | } |
475 | ||
7de0e49a | 476 | if (!begin->is_infinite && begin->ns_from_origin == INT64_MIN) { |
ab90ee94 SM |
477 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
478 | "Invalid trimming time range's beginning time: " | |
7de0e49a PP |
479 | "ns-from-origin=%" PRId64, |
480 | begin->ns_from_origin); | |
481 | ret = -1; | |
482 | goto end; | |
483 | } | |
528debdf | 484 | |
7de0e49a | 485 | if (!end->is_infinite && end->ns_from_origin == INT64_MIN) { |
ab90ee94 SM |
486 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
487 | "Invalid trimming time range's end time: " | |
7de0e49a PP |
488 | "ns-from-origin=%" PRId64, |
489 | end->ns_from_origin); | |
490 | ret = -1; | |
491 | goto end; | |
492 | } | |
528debdf | 493 | |
7de0e49a PP |
494 | end: |
495 | return ret; | |
528debdf MD |
496 | } |
497 | ||
498 | static | |
90a55a4e SM |
499 | enum bt_param_validation_status validate_bound_type( |
500 | const bt_value *value, | |
501 | struct bt_param_validation_context *context) | |
502 | { | |
503 | enum bt_param_validation_status status = BT_PARAM_VALIDATION_STATUS_OK; | |
504 | ||
505 | if (!bt_value_is_signed_integer(value) && | |
506 | !bt_value_is_string(value)) { | |
507 | status = bt_param_validation_error(context, | |
508 | "unexpected type: expected-types=[%s, %s], actual-type=%s", | |
509 | bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER), | |
510 | bt_common_value_type_string(BT_VALUE_TYPE_STRING), | |
511 | bt_common_value_type_string(bt_value_get_type(value))); | |
512 | } | |
513 | ||
514 | return status; | |
515 | } | |
516 | ||
d9120ccb | 517 | static |
90a55a4e SM |
518 | struct bt_param_validation_map_value_entry_descr trimmer_params[] = { |
519 | { "gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
520 | { "begin", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .validation_func = validate_bound_type } }, | |
521 | { "end", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .validation_func = validate_bound_type } }, | |
522 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
523 | }; | |
524 | ||
525 | static | |
526 | bt_component_class_initialize_method_status init_trimmer_comp_from_params( | |
527 | struct trimmer_comp *trimmer_comp, | |
7de0e49a | 528 | const bt_value *params) |
44d3cbf0 | 529 | { |
7de0e49a | 530 | const bt_value *value; |
90a55a4e SM |
531 | bt_component_class_initialize_method_status status; |
532 | enum bt_param_validation_status validation_status; | |
533 | gchar *validate_error = NULL; | |
534 | ||
535 | validation_status = bt_param_validation_validate(params, | |
536 | trimmer_params, &validate_error); | |
537 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
538 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
539 | goto end; | |
540 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
541 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
542 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, "%s", | |
543 | validate_error); | |
544 | goto end; | |
545 | } | |
44d3cbf0 | 546 | |
f6ccaed9 | 547 | BT_ASSERT(params); |
7de0e49a | 548 | value = bt_value_map_borrow_entry_value_const(params, "gmt"); |
528debdf | 549 | if (value) { |
7de0e49a | 550 | trimmer_comp->is_gmt = (bool) bt_value_bool_get(value); |
528debdf MD |
551 | } |
552 | ||
7de0e49a | 553 | value = bt_value_map_borrow_entry_value_const(params, "begin"); |
528debdf | 554 | if (value) { |
0d9a3d3e | 555 | if (set_bound_from_param(trimmer_comp, "begin", value, |
7de0e49a PP |
556 | &trimmer_comp->begin, trimmer_comp->is_gmt)) { |
557 | /* set_bound_from_param() logs errors */ | |
90a55a4e | 558 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
268fae9a | 559 | goto end; |
44d3cbf0 | 560 | } |
7de0e49a PP |
561 | } else { |
562 | trimmer_comp->begin.is_infinite = true; | |
563 | trimmer_comp->begin.is_set = true; | |
44d3cbf0 | 564 | } |
528debdf | 565 | |
7de0e49a | 566 | value = bt_value_map_borrow_entry_value_const(params, "end"); |
528debdf | 567 | if (value) { |
0d9a3d3e | 568 | if (set_bound_from_param(trimmer_comp, "end", value, |
7de0e49a PP |
569 | &trimmer_comp->end, trimmer_comp->is_gmt)) { |
570 | /* set_bound_from_param() logs errors */ | |
90a55a4e | 571 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
268fae9a | 572 | goto end; |
528debdf | 573 | } |
7de0e49a PP |
574 | } else { |
575 | trimmer_comp->end.is_infinite = true; | |
576 | trimmer_comp->end.is_set = true; | |
528debdf | 577 | } |
268fae9a | 578 | |
7de0e49a PP |
579 | if (trimmer_comp->begin.is_set && trimmer_comp->end.is_set) { |
580 | /* validate_trimmer_bounds() logs errors */ | |
90a55a4e SM |
581 | if (validate_trimmer_bounds(trimmer_comp, |
582 | &trimmer_comp->begin, &trimmer_comp->end)) { | |
583 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
584 | goto end; | |
585 | } | |
55595636 | 586 | } |
7de0e49a | 587 | |
90a55a4e SM |
588 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
589 | ||
590 | end: | |
591 | g_free(validate_error); | |
592 | ||
593 | return status; | |
44d3cbf0 JG |
594 | } |
595 | ||
21a9f056 | 596 | bt_component_class_initialize_method_status trimmer_init( |
d24d5663 | 597 | bt_self_component_filter *self_comp_flt, |
59225a3e | 598 | bt_self_component_filter_configuration *config, |
7de0e49a | 599 | const bt_value *params, void *init_data) |
cab3f160 | 600 | { |
90a55a4e | 601 | bt_component_class_initialize_method_status status; |
d24d5663 | 602 | bt_self_component_add_port_status add_port_status; |
7de0e49a | 603 | struct trimmer_comp *trimmer_comp = create_trimmer_comp(); |
ec4ae660 PP |
604 | bt_self_component *self_comp = |
605 | bt_self_component_filter_as_self_component(self_comp_flt); | |
90a55a4e | 606 | |
7de0e49a | 607 | if (!trimmer_comp) { |
21a9f056 | 608 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
7de0e49a | 609 | goto error; |
cab3f160 JG |
610 | } |
611 | ||
0d9a3d3e | 612 | trimmer_comp->log_level = bt_component_get_logging_level( |
ec4ae660 PP |
613 | bt_self_component_as_component(self_comp)); |
614 | trimmer_comp->self_comp = self_comp; | |
a3f0c7db | 615 | trimmer_comp->self_comp_filter = self_comp_flt; |
90a55a4e | 616 | |
d24d5663 | 617 | add_port_status = bt_self_component_filter_add_input_port( |
ec4ae660 | 618 | self_comp_flt, in_port_name, NULL, NULL); |
90a55a4e SM |
619 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
620 | status = (int) add_port_status; | |
d24d5663 | 621 | goto error; |
b9d103be PP |
622 | } |
623 | ||
d24d5663 | 624 | add_port_status = bt_self_component_filter_add_output_port( |
ec4ae660 | 625 | self_comp_flt, "out", NULL, NULL); |
90a55a4e SM |
626 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
627 | status = (int) add_port_status; | |
d24d5663 | 628 | goto error; |
b9d103be PP |
629 | } |
630 | ||
90a55a4e SM |
631 | status = init_trimmer_comp_from_params(trimmer_comp, params); |
632 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { | |
cab3f160 JG |
633 | goto error; |
634 | } | |
635 | ||
ec4ae660 | 636 | bt_self_component_set_data(self_comp, trimmer_comp); |
90a55a4e SM |
637 | |
638 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
7de0e49a PP |
639 | goto end; |
640 | ||
641 | error: | |
7de0e49a PP |
642 | if (trimmer_comp) { |
643 | destroy_trimmer_comp(trimmer_comp); | |
644 | } | |
645 | ||
cab3f160 | 646 | end: |
8dad9b32 | 647 | return status; |
7de0e49a PP |
648 | } |
649 | ||
650 | static | |
651 | void destroy_trimmer_iterator(struct trimmer_iterator *trimmer_it) | |
652 | { | |
e803df70 SM |
653 | if (!trimmer_it) { |
654 | goto end; | |
655 | } | |
656 | ||
9a2c8b8e | 657 | bt_message_iterator_put_ref( |
7de0e49a PP |
658 | trimmer_it->upstream_iter); |
659 | ||
660 | if (trimmer_it->output_messages) { | |
661 | g_queue_free(trimmer_it->output_messages); | |
662 | } | |
663 | ||
664 | if (trimmer_it->stream_states) { | |
665 | g_hash_table_destroy(trimmer_it->stream_states); | |
666 | } | |
667 | ||
668 | g_free(trimmer_it); | |
e803df70 SM |
669 | end: |
670 | return; | |
7de0e49a PP |
671 | } |
672 | ||
673 | static | |
674 | void destroy_trimmer_iterator_stream_state( | |
675 | struct trimmer_iterator_stream_state *sstate) | |
676 | { | |
677 | BT_ASSERT(sstate); | |
678 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); | |
7de0e49a PP |
679 | g_free(sstate); |
680 | } | |
681 | ||
682 | BT_HIDDEN | |
a3f0c7db | 683 | bt_message_iterator_class_initialize_method_status trimmer_msg_iter_init( |
7de0e49a | 684 | bt_self_message_iterator *self_msg_iter, |
8d8b141d | 685 | bt_self_message_iterator_configuration *config, |
7de0e49a PP |
686 | bt_self_component_port_output *port) |
687 | { | |
a3f0c7db | 688 | bt_message_iterator_class_initialize_method_status status; |
9a2c8b8e | 689 | bt_message_iterator_create_from_message_iterator_status |
e803df70 | 690 | msg_iter_status; |
7de0e49a | 691 | struct trimmer_iterator *trimmer_it; |
f615b250 PP |
692 | bt_self_component *self_comp = |
693 | bt_self_message_iterator_borrow_component(self_msg_iter); | |
7de0e49a PP |
694 | |
695 | trimmer_it = g_new0(struct trimmer_iterator, 1); | |
696 | if (!trimmer_it) { | |
a3f0c7db | 697 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
e803df70 | 698 | goto error; |
7de0e49a PP |
699 | } |
700 | ||
a3f0c7db | 701 | trimmer_it->trimmer_comp = bt_self_component_get_data(self_comp); |
7de0e49a PP |
702 | BT_ASSERT(trimmer_it->trimmer_comp); |
703 | ||
704 | if (trimmer_it->trimmer_comp->begin.is_set && | |
705 | trimmer_it->trimmer_comp->end.is_set) { | |
706 | /* | |
707 | * Both trimming time range's bounds are set, so skip | |
708 | * the | |
709 | * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN` | |
710 | * phase. | |
711 | */ | |
712 | trimmer_it->state = TRIMMER_ITERATOR_STATE_SEEK_INITIALLY; | |
713 | } | |
714 | ||
715 | trimmer_it->begin = trimmer_it->trimmer_comp->begin; | |
716 | trimmer_it->end = trimmer_it->trimmer_comp->end; | |
e803df70 | 717 | msg_iter_status = |
9a2c8b8e | 718 | bt_message_iterator_create_from_message_iterator( |
ca02df0a | 719 | self_msg_iter, |
7de0e49a | 720 | bt_self_component_filter_borrow_input_port_by_name( |
a3f0c7db SM |
721 | trimmer_it->trimmer_comp->self_comp_filter, in_port_name), |
722 | &trimmer_it->upstream_iter); | |
9a2c8b8e | 723 | if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { |
e803df70 SM |
724 | status = (int) msg_iter_status; |
725 | goto error; | |
7de0e49a PP |
726 | } |
727 | ||
728 | trimmer_it->output_messages = g_queue_new(); | |
729 | if (!trimmer_it->output_messages) { | |
a3f0c7db | 730 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
e803df70 | 731 | goto error; |
7de0e49a PP |
732 | } |
733 | ||
734 | trimmer_it->stream_states = g_hash_table_new_full(g_direct_hash, | |
735 | g_direct_equal, NULL, | |
736 | (GDestroyNotify) destroy_trimmer_iterator_stream_state); | |
737 | if (!trimmer_it->stream_states) { | |
a3f0c7db | 738 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
e803df70 | 739 | goto error; |
7de0e49a PP |
740 | } |
741 | ||
c0e46a7c SM |
742 | /* |
743 | * The trimmer requires upstream messages to have times, so it can | |
744 | * always seek forward. | |
745 | */ | |
746 | bt_self_message_iterator_configuration_set_can_seek_forward( | |
747 | config, BT_TRUE); | |
748 | ||
7de0e49a PP |
749 | trimmer_it->self_msg_iter = self_msg_iter; |
750 | bt_self_message_iterator_set_data(self_msg_iter, trimmer_it); | |
751 | ||
a3f0c7db | 752 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; |
e803df70 SM |
753 | goto end; |
754 | ||
755 | error: | |
756 | destroy_trimmer_iterator(trimmer_it); | |
7de0e49a | 757 | |
e803df70 | 758 | end: |
7de0e49a PP |
759 | return status; |
760 | } | |
761 | ||
762 | static inline | |
763 | int get_msg_ns_from_origin(const bt_message *msg, int64_t *ns_from_origin, | |
188edac1 | 764 | bool *has_clock_snapshot) |
7de0e49a PP |
765 | { |
766 | const bt_clock_class *clock_class = NULL; | |
767 | const bt_clock_snapshot *clock_snapshot = NULL; | |
7de0e49a PP |
768 | int ret = 0; |
769 | ||
98b15851 PP |
770 | BT_ASSERT_DBG(msg); |
771 | BT_ASSERT_DBG(ns_from_origin); | |
772 | BT_ASSERT_DBG(has_clock_snapshot); | |
7de0e49a PP |
773 | |
774 | switch (bt_message_get_type(msg)) { | |
775 | case BT_MESSAGE_TYPE_EVENT: | |
776 | clock_class = | |
777 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
778 | msg); | |
91d81473 | 779 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
780 | goto error; |
781 | } | |
782 | ||
0cbc2c33 PP |
783 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
784 | msg); | |
7de0e49a PP |
785 | break; |
786 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
787 | clock_class = | |
788 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( | |
789 | msg); | |
91d81473 | 790 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
791 | goto error; |
792 | } | |
793 | ||
0cbc2c33 PP |
794 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
795 | msg); | |
7de0e49a PP |
796 | break; |
797 | case BT_MESSAGE_TYPE_PACKET_END: | |
798 | clock_class = | |
799 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( | |
800 | msg); | |
91d81473 | 801 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
802 | goto error; |
803 | } | |
804 | ||
0cbc2c33 PP |
805 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const( |
806 | msg); | |
7de0e49a | 807 | break; |
188edac1 SM |
808 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
809 | { | |
810 | enum bt_message_stream_clock_snapshot_state cs_state; | |
811 | ||
7de0e49a | 812 | clock_class = |
188edac1 | 813 | bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg); |
91d81473 | 814 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
815 | goto error; |
816 | } | |
817 | ||
188edac1 SM |
818 | cs_state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &clock_snapshot); |
819 | if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
820 | goto no_clock_snapshot; | |
7de0e49a PP |
821 | } |
822 | ||
7de0e49a | 823 | break; |
188edac1 SM |
824 | } |
825 | case BT_MESSAGE_TYPE_STREAM_END: | |
826 | { | |
827 | enum bt_message_stream_clock_snapshot_state cs_state; | |
828 | ||
7de0e49a | 829 | clock_class = |
188edac1 | 830 | bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg); |
91d81473 | 831 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
832 | goto error; |
833 | } | |
834 | ||
188edac1 SM |
835 | cs_state = bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &clock_snapshot); |
836 | if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
7de0e49a PP |
837 | goto no_clock_snapshot; |
838 | } | |
839 | ||
840 | break; | |
188edac1 SM |
841 | } |
842 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
7de0e49a | 843 | clock_class = |
188edac1 | 844 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
7de0e49a | 845 | msg); |
91d81473 | 846 | if (G_UNLIKELY(!clock_class)) { |
7de0e49a PP |
847 | goto error; |
848 | } | |
849 | ||
188edac1 SM |
850 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
851 | msg); | |
852 | break; | |
853 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
854 | clock_class = | |
855 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( | |
856 | msg); | |
857 | if (G_UNLIKELY(!clock_class)) { | |
858 | goto error; | |
7de0e49a PP |
859 | } |
860 | ||
188edac1 SM |
861 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
862 | msg); | |
7de0e49a PP |
863 | break; |
864 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
0cbc2c33 | 865 | clock_snapshot = |
60d02328 | 866 | bt_message_message_iterator_inactivity_borrow_clock_snapshot_const( |
0cbc2c33 | 867 | msg); |
7de0e49a PP |
868 | break; |
869 | default: | |
870 | goto no_clock_snapshot; | |
871 | } | |
872 | ||
7de0e49a PP |
873 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, |
874 | ns_from_origin); | |
91d81473 | 875 | if (G_UNLIKELY(ret)) { |
7de0e49a PP |
876 | goto error; |
877 | } | |
878 | ||
188edac1 | 879 | *has_clock_snapshot = true; |
7de0e49a PP |
880 | goto end; |
881 | ||
882 | no_clock_snapshot: | |
188edac1 | 883 | *has_clock_snapshot = false; |
7de0e49a PP |
884 | goto end; |
885 | ||
cab3f160 | 886 | error: |
7de0e49a PP |
887 | ret = -1; |
888 | ||
889 | end: | |
890 | return ret; | |
891 | } | |
892 | ||
893 | static inline | |
894 | void put_messages(bt_message_array_const msgs, uint64_t count) | |
895 | { | |
896 | uint64_t i; | |
897 | ||
898 | for (i = 0; i < count; i++) { | |
899 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
900 | } | |
901 | } | |
902 | ||
903 | static inline | |
0d9a3d3e PP |
904 | int set_trimmer_iterator_bound(struct trimmer_iterator *trimmer_it, |
905 | struct trimmer_bound *bound, int64_t ns_from_origin, | |
906 | bool is_gmt) | |
7de0e49a | 907 | { |
0d9a3d3e | 908 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
7de0e49a | 909 | struct tm tm; |
53a42a43 | 910 | struct tm *res; |
7de0e49a PP |
911 | time_t time_seconds = (time_t) (ns_from_origin / NS_PER_S); |
912 | int ret = 0; | |
913 | ||
914 | BT_ASSERT(!bound->is_set); | |
915 | errno = 0; | |
916 | ||
917 | /* We only need to extract the date from this time */ | |
918 | if (is_gmt) { | |
53a42a43 | 919 | res = bt_gmtime_r(&time_seconds, &tm); |
7de0e49a | 920 | } else { |
53a42a43 | 921 | res = bt_localtime_r(&time_seconds, &tm); |
7de0e49a PP |
922 | } |
923 | ||
53a42a43 | 924 | if (!res) { |
ab90ee94 SM |
925 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(trimmer_comp->self_comp, |
926 | "Cannot convert timestamp to date and time", | |
f42489db | 927 | ": ts=%" PRId64, (int64_t) time_seconds); |
7de0e49a PP |
928 | ret = -1; |
929 | goto end; | |
930 | } | |
931 | ||
932 | ret = set_bound_ns_from_origin(bound, tm.tm_year + 1900, tm.tm_mon + 1, | |
933 | tm.tm_mday, bound->time.hour, bound->time.minute, | |
934 | bound->time.second, bound->time.ns, is_gmt); | |
935 | ||
936 | end: | |
cab3f160 JG |
937 | return ret; |
938 | } | |
7de0e49a PP |
939 | |
940 | static | |
a3f0c7db | 941 | bt_message_iterator_class_next_method_status |
d24d5663 | 942 | state_set_trimmer_iterator_bounds( |
7de0e49a PP |
943 | struct trimmer_iterator *trimmer_it) |
944 | { | |
d24d5663 | 945 | bt_message_iterator_next_status upstream_iter_status = |
9275bef4 | 946 | BT_MESSAGE_ITERATOR_NEXT_STATUS_OK; |
7de0e49a PP |
947 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
948 | bt_message_array_const msgs; | |
949 | uint64_t count = 0; | |
950 | int64_t ns_from_origin = INT64_MIN; | |
951 | uint64_t i; | |
952 | int ret; | |
953 | ||
954 | BT_ASSERT(!trimmer_it->begin.is_set || | |
955 | !trimmer_it->end.is_set); | |
956 | ||
957 | while (true) { | |
958 | upstream_iter_status = | |
9a2c8b8e | 959 | bt_message_iterator_next( |
7de0e49a | 960 | trimmer_it->upstream_iter, &msgs, &count); |
d24d5663 | 961 | if (upstream_iter_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) { |
7de0e49a PP |
962 | goto end; |
963 | } | |
964 | ||
965 | for (i = 0; i < count; i++) { | |
966 | const bt_message *msg = msgs[i]; | |
188edac1 | 967 | bool has_ns_from_origin; |
7de0e49a | 968 | ret = get_msg_ns_from_origin(msg, &ns_from_origin, |
188edac1 | 969 | &has_ns_from_origin); |
7de0e49a PP |
970 | if (ret) { |
971 | goto error; | |
972 | } | |
973 | ||
188edac1 | 974 | if (!has_ns_from_origin) { |
7de0e49a PP |
975 | continue; |
976 | } | |
977 | ||
98b15851 | 978 | BT_ASSERT_DBG(ns_from_origin != INT64_MIN && |
7de0e49a PP |
979 | ns_from_origin != INT64_MAX); |
980 | put_messages(msgs, count); | |
981 | goto found; | |
982 | } | |
983 | ||
984 | put_messages(msgs, count); | |
985 | } | |
986 | ||
987 | found: | |
988 | if (!trimmer_it->begin.is_set) { | |
989 | BT_ASSERT(!trimmer_it->begin.is_infinite); | |
0d9a3d3e | 990 | ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->begin, |
7de0e49a PP |
991 | ns_from_origin, trimmer_comp->is_gmt); |
992 | if (ret) { | |
993 | goto error; | |
994 | } | |
995 | } | |
996 | ||
997 | if (!trimmer_it->end.is_set) { | |
998 | BT_ASSERT(!trimmer_it->end.is_infinite); | |
0d9a3d3e | 999 | ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->end, |
7de0e49a PP |
1000 | ns_from_origin, trimmer_comp->is_gmt); |
1001 | if (ret) { | |
1002 | goto error; | |
1003 | } | |
1004 | } | |
1005 | ||
0d9a3d3e PP |
1006 | ret = validate_trimmer_bounds(trimmer_it->trimmer_comp, |
1007 | &trimmer_it->begin, &trimmer_it->end); | |
7de0e49a PP |
1008 | if (ret) { |
1009 | goto error; | |
1010 | } | |
1011 | ||
1012 | goto end; | |
1013 | ||
1014 | error: | |
1015 | put_messages(msgs, count); | |
9275bef4 | 1016 | upstream_iter_status = BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR; |
7de0e49a PP |
1017 | |
1018 | end: | |
1019 | return (int) upstream_iter_status; | |
1020 | } | |
1021 | ||
1022 | static | |
a3f0c7db | 1023 | bt_message_iterator_class_next_method_status state_seek_initially( |
7de0e49a PP |
1024 | struct trimmer_iterator *trimmer_it) |
1025 | { | |
0d9a3d3e | 1026 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
a3f0c7db | 1027 | bt_message_iterator_class_next_method_status status; |
7de0e49a PP |
1028 | |
1029 | BT_ASSERT(trimmer_it->begin.is_set); | |
1030 | ||
1031 | if (trimmer_it->begin.is_infinite) { | |
f2fb1b32 SM |
1032 | bt_bool can_seek; |
1033 | ||
9a2c8b8e | 1034 | status = (int) bt_message_iterator_can_seek_beginning( |
f2fb1b32 | 1035 | trimmer_it->upstream_iter, &can_seek); |
a3f0c7db | 1036 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
f2fb1b32 SM |
1037 | if (status < 0) { |
1038 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1039 | "Cannot make upstream message iterator initially seek its beginning."); | |
1040 | } | |
1041 | ||
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | if (!can_seek) { | |
ab90ee94 SM |
1046 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1047 | "Cannot make upstream message iterator initially seek its beginning."); | |
a3f0c7db | 1048 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1049 | goto end; |
1050 | } | |
1051 | ||
9a2c8b8e | 1052 | status = (int) bt_message_iterator_seek_beginning( |
7de0e49a PP |
1053 | trimmer_it->upstream_iter); |
1054 | } else { | |
f2fb1b32 SM |
1055 | bt_bool can_seek; |
1056 | ||
9a2c8b8e | 1057 | status = (int) bt_message_iterator_can_seek_ns_from_origin( |
f2fb1b32 SM |
1058 | trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin, |
1059 | &can_seek); | |
1060 | ||
a3f0c7db | 1061 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
f2fb1b32 SM |
1062 | if (status < 0) { |
1063 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1064 | "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64, | |
1065 | trimmer_it->begin.ns_from_origin); | |
1066 | } | |
1067 | ||
1068 | goto end; | |
1069 | } | |
1070 | ||
1071 | if (!can_seek) { | |
ab90ee94 SM |
1072 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1073 | "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64, | |
7de0e49a | 1074 | trimmer_it->begin.ns_from_origin); |
a3f0c7db | 1075 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1076 | goto end; |
1077 | } | |
1078 | ||
9a2c8b8e | 1079 | status = (int) bt_message_iterator_seek_ns_from_origin( |
7de0e49a PP |
1080 | trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin); |
1081 | } | |
1082 | ||
a3f0c7db | 1083 | if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1084 | trimmer_it->state = TRIMMER_ITERATOR_STATE_TRIM; |
1085 | } | |
1086 | ||
1087 | end: | |
1088 | return status; | |
1089 | } | |
1090 | ||
1091 | static inline | |
1092 | void push_message(struct trimmer_iterator *trimmer_it, const bt_message *msg) | |
1093 | { | |
1094 | g_queue_push_head(trimmer_it->output_messages, (void *) msg); | |
1095 | } | |
1096 | ||
1097 | static inline | |
1098 | const bt_message *pop_message(struct trimmer_iterator *trimmer_it) | |
1099 | { | |
1100 | return g_queue_pop_tail(trimmer_it->output_messages); | |
1101 | } | |
1102 | ||
1103 | static inline | |
1104 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, | |
1105 | int64_t ns_from_origin, uint64_t *raw_value) | |
1106 | { | |
1107 | ||
1108 | int64_t cc_offset_s; | |
1109 | uint64_t cc_offset_cycles; | |
1110 | uint64_t cc_freq; | |
1111 | ||
1112 | bt_clock_class_get_offset(clock_class, &cc_offset_s, &cc_offset_cycles); | |
1113 | cc_freq = bt_clock_class_get_frequency(clock_class); | |
1114 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, | |
1115 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); | |
1116 | } | |
1117 | ||
1118 | static inline | |
a3f0c7db | 1119 | bt_message_iterator_class_next_method_status |
d24d5663 | 1120 | end_stream(struct trimmer_iterator *trimmer_it, |
7de0e49a PP |
1121 | struct trimmer_iterator_stream_state *sstate) |
1122 | { | |
a3f0c7db SM |
1123 | bt_message_iterator_class_next_method_status status = |
1124 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
188edac1 SM |
1125 | /* Initialize to silence maybe-uninitialized warning. */ |
1126 | uint64_t raw_value = 0; | |
7de0e49a PP |
1127 | bt_message *msg = NULL; |
1128 | ||
1129 | BT_ASSERT(!trimmer_it->end.is_infinite); | |
188edac1 | 1130 | BT_ASSERT(sstate->stream); |
7de0e49a | 1131 | |
188edac1 SM |
1132 | /* |
1133 | * If we haven't seen a message with a clock snapshot, we don't know if the trimmer's end bound is within | |
1134 | * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value. | |
1135 | * | |
1136 | * Also, it would be a bit of a lie to generate a stream end message with the end bound as its | |
1137 | * clock snapshot, because we don't really know if the stream existed at that time. If we have | |
1138 | * seen a message with a clock snapshot and the stream is cut short by another message with a | |
1139 | * clock snapshot, then we are sure that the the end bound time is not below the clock range, | |
1140 | * and we know the stream was active at that time (and that we cut it short). | |
1141 | */ | |
1142 | if (sstate->seen_clock_snapshot) { | |
1143 | const bt_clock_class *clock_class; | |
1144 | int ret; | |
7de0e49a | 1145 | |
7de0e49a PP |
1146 | clock_class = bt_stream_class_borrow_default_clock_class_const( |
1147 | bt_stream_borrow_class_const(sstate->stream)); | |
1148 | BT_ASSERT(clock_class); | |
1149 | ret = clock_raw_value_from_ns_from_origin(clock_class, | |
1150 | trimmer_it->end.ns_from_origin, &raw_value); | |
1151 | if (ret) { | |
a3f0c7db | 1152 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1153 | goto end; |
1154 | } | |
188edac1 SM |
1155 | } |
1156 | ||
1157 | if (sstate->cur_packet) { | |
1158 | /* | |
1159 | * Create and push a packet end message, making its time | |
1160 | * the trimming range's end time. | |
1161 | * | |
1162 | * We know that we must have seen a clock snapshot, the one in | |
1163 | * the packet beginning message, since trimmer currently | |
1164 | * requires packet messages to have clock snapshots (see comment | |
1165 | * in create_stream_state_entry). | |
1166 | */ | |
1167 | BT_ASSERT(sstate->seen_clock_snapshot); | |
7de0e49a PP |
1168 | |
1169 | msg = bt_message_packet_end_create_with_default_clock_snapshot( | |
1170 | trimmer_it->self_msg_iter, sstate->cur_packet, | |
1171 | raw_value); | |
1172 | if (!msg) { | |
a3f0c7db | 1173 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
7de0e49a PP |
1174 | goto end; |
1175 | } | |
1176 | ||
1177 | push_message(trimmer_it, msg); | |
1178 | msg = NULL; | |
1179 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); | |
7de0e49a PP |
1180 | } |
1181 | ||
188edac1 | 1182 | /* Create and push a stream end message. */ |
7de0e49a PP |
1183 | msg = bt_message_stream_end_create(trimmer_it->self_msg_iter, |
1184 | sstate->stream); | |
1185 | if (!msg) { | |
a3f0c7db | 1186 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
7de0e49a PP |
1187 | goto end; |
1188 | } | |
1189 | ||
188edac1 SM |
1190 | if (sstate->seen_clock_snapshot) { |
1191 | bt_message_stream_end_set_default_clock_snapshot(msg, raw_value); | |
1192 | } | |
1193 | ||
7de0e49a PP |
1194 | push_message(trimmer_it, msg); |
1195 | msg = NULL; | |
1196 | ||
1197 | /* | |
1198 | * Just to make sure that we don't use this stream state again | |
1199 | * in the future without an obvious error. | |
1200 | */ | |
1201 | sstate->stream = NULL; | |
1202 | ||
1203 | end: | |
1204 | bt_message_put_ref(msg); | |
1205 | return status; | |
1206 | } | |
1207 | ||
1208 | static inline | |
a3f0c7db | 1209 | bt_message_iterator_class_next_method_status end_iterator_streams( |
7de0e49a PP |
1210 | struct trimmer_iterator *trimmer_it) |
1211 | { | |
a3f0c7db SM |
1212 | bt_message_iterator_class_next_method_status status = |
1213 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
7de0e49a PP |
1214 | GHashTableIter iter; |
1215 | gpointer key, sstate; | |
1216 | ||
1217 | if (trimmer_it->end.is_infinite) { | |
1218 | /* | |
1219 | * An infinite trimming range's end time guarantees that | |
1220 | * we received (and pushed) all the appropriate end | |
1221 | * messages. | |
1222 | */ | |
1223 | goto remove_all; | |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * End each stream and then remove them from the hash table of | |
1228 | * stream states to release unneeded references. | |
1229 | */ | |
1230 | g_hash_table_iter_init(&iter, trimmer_it->stream_states); | |
1231 | ||
1232 | while (g_hash_table_iter_next(&iter, &key, &sstate)) { | |
1233 | status = end_stream(trimmer_it, sstate); | |
1234 | if (status) { | |
1235 | goto end; | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | remove_all: | |
1240 | g_hash_table_remove_all(trimmer_it->stream_states); | |
1241 | ||
1242 | end: | |
1243 | return status; | |
1244 | } | |
1245 | ||
188edac1 | 1246 | static |
a3f0c7db | 1247 | bt_message_iterator_class_next_method_status |
188edac1 SM |
1248 | create_stream_state_entry( |
1249 | struct trimmer_iterator *trimmer_it, | |
1250 | const struct bt_stream *stream, | |
1251 | struct trimmer_iterator_stream_state **stream_state) | |
1252 | { | |
1253 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; | |
a3f0c7db | 1254 | bt_message_iterator_class_next_method_status status; |
188edac1 SM |
1255 | struct trimmer_iterator_stream_state *sstate; |
1256 | const bt_stream_class *sc; | |
1257 | ||
1258 | BT_ASSERT(!bt_g_hash_table_contains(trimmer_it->stream_states, stream)); | |
1259 | ||
1260 | /* | |
1261 | * Validate right now that the stream's class | |
1262 | * has a registered default clock class so that | |
1263 | * an existing stream state guarantees existing | |
1264 | * default clock snapshots for its associated | |
1265 | * messages. | |
1266 | * | |
1267 | * Also check that clock snapshots are always | |
1268 | * known. | |
1269 | */ | |
1270 | sc = bt_stream_borrow_class_const(stream); | |
1271 | if (!bt_stream_class_borrow_default_clock_class_const(sc)) { | |
ab90ee94 SM |
1272 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1273 | "Unsupported stream: stream class does " | |
188edac1 SM |
1274 | "not have a default clock class: " |
1275 | "stream-addr=%p, " | |
1276 | "stream-id=%" PRIu64 ", " | |
1277 | "stream-name=\"%s\"", | |
1278 | stream, bt_stream_get_id(stream), | |
1279 | bt_stream_get_name(stream)); | |
a3f0c7db | 1280 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
188edac1 SM |
1281 | goto end; |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * Temporary: make sure packet beginning, packet | |
1286 | * end, discarded events, and discarded packets | |
1287 | * messages have default clock snapshots until | |
1288 | * the support for not having them is | |
1289 | * implemented. | |
1290 | */ | |
21f6a44a SM |
1291 | if (bt_stream_class_supports_packets(sc)) { |
1292 | if (!bt_stream_class_packets_have_beginning_default_clock_snapshot( | |
1293 | sc)) { | |
1294 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1295 | "Unsupported stream: packets have no beginning clock snapshot: " | |
1296 | "stream-addr=%p, " | |
1297 | "stream-id=%" PRIu64 ", " | |
1298 | "stream-name=\"%s\"", | |
1299 | stream, bt_stream_get_id(stream), | |
1300 | bt_stream_get_name(stream)); | |
1301 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1302 | goto end; | |
1303 | } | |
188edac1 | 1304 | |
21f6a44a SM |
1305 | if (!bt_stream_class_packets_have_end_default_clock_snapshot( |
1306 | sc)) { | |
1307 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1308 | "Unsupported stream: packets have no end clock snapshot: " | |
1309 | "stream-addr=%p, " | |
1310 | "stream-id=%" PRIu64 ", " | |
1311 | "stream-name=\"%s\"", | |
1312 | stream, bt_stream_get_id(stream), | |
1313 | bt_stream_get_name(stream)); | |
1314 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1315 | goto end; | |
1316 | } | |
1317 | ||
1318 | if (bt_stream_class_supports_discarded_packets(sc) && | |
1319 | !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) { | |
1320 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1321 | "Unsupported stream: discarded packets " | |
1322 | "have no clock snapshots: " | |
1323 | "stream-addr=%p, " | |
1324 | "stream-id=%" PRIu64 ", " | |
1325 | "stream-name=\"%s\"", | |
1326 | stream, bt_stream_get_id(stream), | |
1327 | bt_stream_get_name(stream)); | |
1328 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1329 | goto end; | |
1330 | } | |
188edac1 SM |
1331 | } |
1332 | ||
1333 | if (bt_stream_class_supports_discarded_events(sc) && | |
1334 | !bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) { | |
ab90ee94 SM |
1335 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1336 | "Unsupported stream: discarded events have no clock snapshots: " | |
188edac1 SM |
1337 | "stream-addr=%p, " |
1338 | "stream-id=%" PRIu64 ", " | |
1339 | "stream-name=\"%s\"", | |
1340 | stream, bt_stream_get_id(stream), | |
1341 | bt_stream_get_name(stream)); | |
a3f0c7db | 1342 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
188edac1 SM |
1343 | goto end; |
1344 | } | |
1345 | ||
188edac1 SM |
1346 | sstate = g_new0(struct trimmer_iterator_stream_state, 1); |
1347 | if (!sstate) { | |
a3f0c7db | 1348 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
188edac1 SM |
1349 | goto end; |
1350 | } | |
1351 | ||
1352 | sstate->stream = stream; | |
1353 | ||
1354 | g_hash_table_insert(trimmer_it->stream_states, (void *) stream, sstate); | |
1355 | ||
1356 | *stream_state = sstate; | |
1357 | ||
a3f0c7db | 1358 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
188edac1 SM |
1359 | |
1360 | end: | |
1361 | return status; | |
1362 | } | |
1363 | ||
1364 | static | |
1365 | struct trimmer_iterator_stream_state *get_stream_state_entry( | |
1366 | struct trimmer_iterator *trimmer_it, | |
1367 | const struct bt_stream *stream) | |
1368 | { | |
1369 | struct trimmer_iterator_stream_state *sstate; | |
1370 | ||
98b15851 | 1371 | BT_ASSERT_DBG(stream); |
188edac1 | 1372 | sstate = g_hash_table_lookup(trimmer_it->stream_states, stream); |
98b15851 | 1373 | BT_ASSERT_DBG(sstate); |
188edac1 SM |
1374 | |
1375 | return sstate; | |
1376 | } | |
1377 | ||
7de0e49a PP |
1378 | /* |
1379 | * Handles a message which is associated to a given stream state. This | |
1380 | * _could_ make the iterator's output message queue grow; this could | |
1381 | * also consume the message without pushing anything to this queue, only | |
1382 | * modifying the stream state. | |
1383 | * | |
1384 | * This function consumes the `msg` reference, _whatever the outcome_. | |
1385 | * | |
188edac1 SM |
1386 | * If non-NULL, `ns_from_origin` is the message's time, as given by |
1387 | * get_msg_ns_from_origin(). If NULL, the message doesn't have a time. | |
7de0e49a PP |
1388 | * |
1389 | * This function sets `reached_end` if handling this message made the | |
1390 | * iterator reach the end of the trimming range. Note that the output | |
1391 | * message queue could contain messages even if this function sets | |
1392 | * `reached_end`. | |
1393 | */ | |
188edac1 | 1394 | static |
a3f0c7db | 1395 | bt_message_iterator_class_next_method_status |
188edac1 | 1396 | handle_message_with_stream( |
7de0e49a | 1397 | struct trimmer_iterator *trimmer_it, const bt_message *msg, |
188edac1 SM |
1398 | const struct bt_stream *stream, const int64_t *ns_from_origin, |
1399 | bool *reached_end) | |
7de0e49a | 1400 | { |
a3f0c7db SM |
1401 | bt_message_iterator_class_next_method_status status = |
1402 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
7de0e49a PP |
1403 | bt_message_type msg_type = bt_message_get_type(msg); |
1404 | int ret; | |
188edac1 SM |
1405 | struct trimmer_iterator_stream_state *sstate = NULL; |
1406 | ||
1407 | /* | |
1408 | * Retrieve the stream's state - except if the message is stream | |
1409 | * beginning, in which case we don't know about about this stream yet. | |
1410 | */ | |
1411 | if (msg_type != BT_MESSAGE_TYPE_STREAM_BEGINNING) { | |
1412 | sstate = get_stream_state_entry(trimmer_it, stream); | |
1413 | } | |
7de0e49a PP |
1414 | |
1415 | switch (msg_type) { | |
1416 | case BT_MESSAGE_TYPE_EVENT: | |
188edac1 SM |
1417 | /* |
1418 | * Event messages always have a clock snapshot if the stream | |
1419 | * class has a clock class. And we know it has, otherwise we | |
1420 | * couldn't be using the trimmer component. | |
1421 | */ | |
98b15851 | 1422 | BT_ASSERT_DBG(ns_from_origin); |
188edac1 | 1423 | |
91d81473 | 1424 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
188edac1 | 1425 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
7de0e49a PP |
1426 | status = end_iterator_streams(trimmer_it); |
1427 | *reached_end = true; | |
1428 | break; | |
1429 | } | |
1430 | ||
188edac1 SM |
1431 | sstate->seen_clock_snapshot = true; |
1432 | ||
7de0e49a PP |
1433 | push_message(trimmer_it, msg); |
1434 | msg = NULL; | |
1435 | break; | |
188edac1 | 1436 | |
7de0e49a | 1437 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
188edac1 SM |
1438 | /* |
1439 | * Packet beginning messages won't have a clock snapshot if | |
1440 | * stream_class->packets_have_beginning_default_clock_snapshot | |
1441 | * is false. But for now, assume they always do. | |
1442 | */ | |
1443 | BT_ASSERT(ns_from_origin); | |
1444 | BT_ASSERT(!sstate->cur_packet); | |
1445 | ||
91d81473 | 1446 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
188edac1 | 1447 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
7de0e49a PP |
1448 | status = end_iterator_streams(trimmer_it); |
1449 | *reached_end = true; | |
1450 | break; | |
1451 | } | |
1452 | ||
7de0e49a PP |
1453 | sstate->cur_packet = |
1454 | bt_message_packet_beginning_borrow_packet_const(msg); | |
1455 | bt_packet_get_ref(sstate->cur_packet); | |
188edac1 SM |
1456 | |
1457 | sstate->seen_clock_snapshot = true; | |
1458 | ||
7de0e49a PP |
1459 | push_message(trimmer_it, msg); |
1460 | msg = NULL; | |
1461 | break; | |
188edac1 | 1462 | |
7de0e49a | 1463 | case BT_MESSAGE_TYPE_PACKET_END: |
188edac1 SM |
1464 | /* |
1465 | * Packet end messages won't have a clock snapshot if | |
1466 | * stream_class->packets_have_end_default_clock_snapshot | |
1467 | * is false. But for now, assume they always do. | |
1468 | */ | |
1469 | BT_ASSERT(ns_from_origin); | |
1470 | BT_ASSERT(sstate->cur_packet); | |
7de0e49a | 1471 | |
91d81473 | 1472 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
188edac1 | 1473 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
7de0e49a PP |
1474 | status = end_iterator_streams(trimmer_it); |
1475 | *reached_end = true; | |
1476 | break; | |
1477 | } | |
1478 | ||
7de0e49a | 1479 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); |
188edac1 SM |
1480 | |
1481 | sstate->seen_clock_snapshot = true; | |
1482 | ||
7de0e49a PP |
1483 | push_message(trimmer_it, msg); |
1484 | msg = NULL; | |
1485 | break; | |
188edac1 | 1486 | |
7de0e49a PP |
1487 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1488 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1489 | { | |
1490 | /* | |
1491 | * `ns_from_origin` is the message's time range's | |
1492 | * beginning time here. | |
1493 | */ | |
1494 | int64_t end_ns_from_origin; | |
1495 | const bt_clock_snapshot *end_cs; | |
1496 | ||
188edac1 SM |
1497 | BT_ASSERT(ns_from_origin); |
1498 | ||
1499 | sstate->seen_clock_snapshot = true; | |
1500 | ||
7de0e49a PP |
1501 | if (bt_message_get_type(msg) == |
1502 | BT_MESSAGE_TYPE_DISCARDED_EVENTS) { | |
1503 | /* | |
1504 | * Safe to ignore the return value because we | |
1505 | * know there's a default clock and it's always | |
1506 | * known. | |
1507 | */ | |
9b24b6aa | 1508 | end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
0cbc2c33 | 1509 | msg); |
7de0e49a PP |
1510 | } else { |
1511 | /* | |
1512 | * Safe to ignore the return value because we | |
1513 | * know there's a default clock and it's always | |
1514 | * known. | |
1515 | */ | |
9b24b6aa | 1516 | end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
0cbc2c33 | 1517 | msg); |
7de0e49a PP |
1518 | } |
1519 | ||
1520 | if (bt_clock_snapshot_get_ns_from_origin(end_cs, | |
1521 | &end_ns_from_origin)) { | |
a3f0c7db | 1522 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1523 | goto end; |
1524 | } | |
1525 | ||
7de0e49a | 1526 | if (!trimmer_it->end.is_infinite && |
188edac1 | 1527 | *ns_from_origin > trimmer_it->end.ns_from_origin) { |
7de0e49a PP |
1528 | status = end_iterator_streams(trimmer_it); |
1529 | *reached_end = true; | |
1530 | break; | |
1531 | } | |
1532 | ||
1533 | if (!trimmer_it->end.is_infinite && | |
1534 | end_ns_from_origin > trimmer_it->end.ns_from_origin) { | |
1535 | /* | |
1536 | * This message's end time is outside the | |
1537 | * trimming time range: replace it with a new | |
1538 | * message having an end time equal to the | |
1539 | * trimming time range's end and without a | |
1540 | * count. | |
1541 | */ | |
1542 | const bt_clock_class *clock_class = | |
1543 | bt_clock_snapshot_borrow_clock_class_const( | |
1544 | end_cs); | |
1545 | const bt_clock_snapshot *begin_cs; | |
1546 | bt_message *new_msg; | |
1547 | uint64_t end_raw_value; | |
1548 | ||
1549 | ret = clock_raw_value_from_ns_from_origin(clock_class, | |
1550 | trimmer_it->end.ns_from_origin, &end_raw_value); | |
1551 | if (ret) { | |
a3f0c7db | 1552 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1553 | goto end; |
1554 | } | |
1555 | ||
1556 | if (msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) { | |
9b24b6aa | 1557 | begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 1558 | msg); |
7de0e49a PP |
1559 | new_msg = bt_message_discarded_events_create_with_default_clock_snapshots( |
1560 | trimmer_it->self_msg_iter, | |
1561 | sstate->stream, | |
1562 | bt_clock_snapshot_get_value(begin_cs), | |
1563 | end_raw_value); | |
1564 | } else { | |
9b24b6aa | 1565 | begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 1566 | msg); |
7de0e49a PP |
1567 | new_msg = bt_message_discarded_packets_create_with_default_clock_snapshots( |
1568 | trimmer_it->self_msg_iter, | |
1569 | sstate->stream, | |
1570 | bt_clock_snapshot_get_value(begin_cs), | |
1571 | end_raw_value); | |
1572 | } | |
1573 | ||
1574 | if (!new_msg) { | |
a3f0c7db | 1575 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
7de0e49a PP |
1576 | goto end; |
1577 | } | |
1578 | ||
1579 | /* Replace the original message */ | |
1580 | BT_MESSAGE_MOVE_REF(msg, new_msg); | |
1581 | } | |
1582 | ||
7de0e49a PP |
1583 | push_message(trimmer_it, msg); |
1584 | msg = NULL; | |
1585 | break; | |
1586 | } | |
188edac1 SM |
1587 | |
1588 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1589 | /* | |
1590 | * If this message has a time and this time is greater than the | |
1591 | * trimmer's end bound, it triggers the end of the trim window. | |
1592 | */ | |
1593 | if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite && | |
1594 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { | |
7de0e49a PP |
1595 | status = end_iterator_streams(trimmer_it); |
1596 | *reached_end = true; | |
1597 | break; | |
1598 | } | |
1599 | ||
188edac1 SM |
1600 | /* Learn about this stream. */ |
1601 | status = create_stream_state_entry(trimmer_it, stream, &sstate); | |
a3f0c7db | 1602 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
188edac1 | 1603 | goto end; |
7de0e49a PP |
1604 | } |
1605 | ||
188edac1 SM |
1606 | if (ns_from_origin) { |
1607 | sstate->seen_clock_snapshot = true; | |
7de0e49a PP |
1608 | } |
1609 | ||
5b7b55be SM |
1610 | push_message(trimmer_it, msg); |
1611 | msg = NULL; | |
7de0e49a PP |
1612 | break; |
1613 | case BT_MESSAGE_TYPE_STREAM_END: | |
188edac1 SM |
1614 | { |
1615 | gboolean removed; | |
1616 | ||
5b7b55be | 1617 | /* |
188edac1 SM |
1618 | * If this message has a time and this time is greater than the |
1619 | * trimmer's end bound, it triggers the end of the trim window. | |
5b7b55be | 1620 | */ |
188edac1 SM |
1621 | if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite && |
1622 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { | |
1623 | status = end_iterator_streams(trimmer_it); | |
1624 | *reached_end = true; | |
1625 | break; | |
7de0e49a | 1626 | } |
188edac1 SM |
1627 | |
1628 | /* | |
1629 | * Either the stream end message's time is within the trimmer's | |
1630 | * bounds, or it doesn't have a time. In both cases, pass | |
1631 | * the message unmodified. | |
1632 | */ | |
1633 | push_message(trimmer_it, msg); | |
1634 | msg = NULL; | |
1635 | ||
1636 | /* Forget about this stream. */ | |
1637 | removed = g_hash_table_remove(trimmer_it->stream_states, sstate->stream); | |
1638 | BT_ASSERT(removed); | |
7de0e49a | 1639 | break; |
188edac1 | 1640 | } |
7de0e49a PP |
1641 | default: |
1642 | break; | |
1643 | } | |
1644 | ||
1645 | end: | |
1646 | /* We release the message's reference whatever the outcome */ | |
1647 | bt_message_put_ref(msg); | |
188edac1 | 1648 | return status; |
7de0e49a PP |
1649 | } |
1650 | ||
1651 | /* | |
1652 | * Handles an input message. This _could_ make the iterator's output | |
1653 | * message queue grow; this could also consume the message without | |
1654 | * pushing anything to this queue, only modifying the stream state. | |
1655 | * | |
1656 | * This function consumes the `msg` reference, _whatever the outcome_. | |
1657 | * | |
1658 | * This function sets `reached_end` if handling this message made the | |
1659 | * iterator reach the end of the trimming range. Note that the output | |
1660 | * message queue could contain messages even if this function sets | |
1661 | * `reached_end`. | |
1662 | */ | |
1663 | static inline | |
a3f0c7db | 1664 | bt_message_iterator_class_next_method_status handle_message( |
7de0e49a PP |
1665 | struct trimmer_iterator *trimmer_it, const bt_message *msg, |
1666 | bool *reached_end) | |
1667 | { | |
a3f0c7db | 1668 | bt_message_iterator_class_next_method_status status; |
7de0e49a PP |
1669 | const bt_stream *stream = NULL; |
1670 | int64_t ns_from_origin = INT64_MIN; | |
4af85094 | 1671 | bool has_ns_from_origin = false; |
7de0e49a | 1672 | int ret; |
7de0e49a PP |
1673 | |
1674 | /* Find message's associated stream */ | |
1675 | switch (bt_message_get_type(msg)) { | |
1676 | case BT_MESSAGE_TYPE_EVENT: | |
1677 | stream = bt_event_borrow_stream_const( | |
1678 | bt_message_event_borrow_event_const(msg)); | |
1679 | break; | |
1680 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
1681 | stream = bt_packet_borrow_stream_const( | |
1682 | bt_message_packet_beginning_borrow_packet_const(msg)); | |
1683 | break; | |
1684 | case BT_MESSAGE_TYPE_PACKET_END: | |
1685 | stream = bt_packet_borrow_stream_const( | |
1686 | bt_message_packet_end_borrow_packet_const(msg)); | |
1687 | break; | |
1688 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1689 | stream = bt_message_discarded_events_borrow_stream_const(msg); | |
1690 | break; | |
1691 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1692 | stream = bt_message_discarded_packets_borrow_stream_const(msg); | |
1693 | break; | |
7de0e49a PP |
1694 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
1695 | stream = bt_message_stream_beginning_borrow_stream_const(msg); | |
1696 | break; | |
1697 | case BT_MESSAGE_TYPE_STREAM_END: | |
1698 | stream = bt_message_stream_end_borrow_stream_const(msg); | |
1699 | break; | |
1700 | default: | |
1701 | break; | |
1702 | } | |
1703 | ||
7de0e49a | 1704 | /* Retrieve the message's time */ |
188edac1 | 1705 | ret = get_msg_ns_from_origin(msg, &ns_from_origin, &has_ns_from_origin); |
91d81473 | 1706 | if (G_UNLIKELY(ret)) { |
a3f0c7db | 1707 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
7de0e49a PP |
1708 | goto end; |
1709 | } | |
1710 | ||
188edac1 | 1711 | if (G_LIKELY(stream)) { |
7de0e49a | 1712 | /* Message associated to a stream */ |
188edac1 SM |
1713 | status = handle_message_with_stream(trimmer_it, msg, |
1714 | stream, has_ns_from_origin ? &ns_from_origin : NULL, reached_end); | |
7de0e49a PP |
1715 | |
1716 | /* | |
1717 | * handle_message_with_stream_state() unconditionally | |
1718 | * consumes `msg`. | |
1719 | */ | |
1720 | msg = NULL; | |
1721 | } else { | |
1722 | /* | |
1723 | * Message not associated to a stream (message iterator | |
1724 | * inactivity). | |
1725 | */ | |
91d81473 | 1726 | if (G_UNLIKELY(ns_from_origin > trimmer_it->end.ns_from_origin)) { |
7de0e49a PP |
1727 | BT_MESSAGE_PUT_REF_AND_RESET(msg); |
1728 | status = end_iterator_streams(trimmer_it); | |
1729 | *reached_end = true; | |
1730 | } else { | |
1731 | push_message(trimmer_it, msg); | |
a3f0c7db | 1732 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
7de0e49a PP |
1733 | msg = NULL; |
1734 | } | |
1735 | } | |
1736 | ||
1737 | end: | |
1738 | /* We release the message's reference whatever the outcome */ | |
1739 | bt_message_put_ref(msg); | |
1740 | return status; | |
1741 | } | |
1742 | ||
1743 | static inline | |
1744 | void fill_message_array_from_output_messages( | |
1745 | struct trimmer_iterator *trimmer_it, | |
1746 | bt_message_array_const msgs, uint64_t capacity, uint64_t *count) | |
1747 | { | |
1748 | *count = 0; | |
1749 | ||
1750 | /* | |
1751 | * Move auto-seek messages to the output array (which is this | |
1752 | * iterator's base message array). | |
1753 | */ | |
1754 | while (capacity > 0 && !g_queue_is_empty(trimmer_it->output_messages)) { | |
1755 | msgs[*count] = pop_message(trimmer_it); | |
1756 | capacity--; | |
1757 | (*count)++; | |
1758 | } | |
1759 | ||
98b15851 | 1760 | BT_ASSERT_DBG(*count > 0); |
7de0e49a PP |
1761 | } |
1762 | ||
1763 | static inline | |
a3f0c7db | 1764 | bt_message_iterator_class_next_method_status state_ending( |
7de0e49a PP |
1765 | struct trimmer_iterator *trimmer_it, |
1766 | bt_message_array_const msgs, uint64_t capacity, | |
1767 | uint64_t *count) | |
1768 | { | |
a3f0c7db SM |
1769 | bt_message_iterator_class_next_method_status status = |
1770 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
7de0e49a PP |
1771 | |
1772 | if (g_queue_is_empty(trimmer_it->output_messages)) { | |
1773 | trimmer_it->state = TRIMMER_ITERATOR_STATE_ENDED; | |
a3f0c7db | 1774 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
7de0e49a PP |
1775 | goto end; |
1776 | } | |
1777 | ||
1778 | fill_message_array_from_output_messages(trimmer_it, msgs, | |
1779 | capacity, count); | |
1780 | ||
1781 | end: | |
1782 | return status; | |
1783 | } | |
1784 | ||
1785 | static inline | |
a3f0c7db | 1786 | bt_message_iterator_class_next_method_status |
d24d5663 | 1787 | state_trim(struct trimmer_iterator *trimmer_it, |
7de0e49a PP |
1788 | bt_message_array_const msgs, uint64_t capacity, |
1789 | uint64_t *count) | |
1790 | { | |
a3f0c7db SM |
1791 | bt_message_iterator_class_next_method_status status = |
1792 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
7de0e49a PP |
1793 | bt_message_array_const my_msgs; |
1794 | uint64_t my_count; | |
1795 | uint64_t i; | |
1796 | bool reached_end = false; | |
1797 | ||
1798 | while (g_queue_is_empty(trimmer_it->output_messages)) { | |
9a2c8b8e | 1799 | status = (int) bt_message_iterator_next( |
7de0e49a | 1800 | trimmer_it->upstream_iter, &my_msgs, &my_count); |
a3f0c7db SM |
1801 | if (G_UNLIKELY(status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) { |
1802 | if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) { | |
7de0e49a | 1803 | status = end_iterator_streams(trimmer_it); |
a3f0c7db | 1804 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1805 | goto end; |
1806 | } | |
1807 | ||
1808 | trimmer_it->state = | |
1809 | TRIMMER_ITERATOR_STATE_ENDING; | |
1810 | status = state_ending(trimmer_it, msgs, | |
1811 | capacity, count); | |
1812 | } | |
1813 | ||
1814 | goto end; | |
1815 | } | |
1816 | ||
98b15851 | 1817 | BT_ASSERT_DBG(my_count > 0); |
7de0e49a PP |
1818 | |
1819 | for (i = 0; i < my_count; i++) { | |
1820 | status = handle_message(trimmer_it, my_msgs[i], | |
1821 | &reached_end); | |
1822 | ||
1823 | /* | |
1824 | * handle_message() unconditionally consumes the | |
1825 | * message reference. | |
1826 | */ | |
1827 | my_msgs[i] = NULL; | |
1828 | ||
91d81473 | 1829 | if (G_UNLIKELY(status != |
a3f0c7db | 1830 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) { |
7de0e49a PP |
1831 | put_messages(my_msgs, my_count); |
1832 | goto end; | |
1833 | } | |
1834 | ||
91d81473 | 1835 | if (G_UNLIKELY(reached_end)) { |
7de0e49a PP |
1836 | /* |
1837 | * This message's time was passed the | |
1838 | * trimming time range's end time: we | |
1839 | * are done. Their might still be | |
1840 | * messages in the output message queue, | |
1841 | * so move to the "ending" state and | |
1842 | * apply it immediately since | |
1843 | * state_trim() is called within the | |
1844 | * "next" method. | |
1845 | */ | |
1846 | put_messages(my_msgs, my_count); | |
1847 | trimmer_it->state = | |
1848 | TRIMMER_ITERATOR_STATE_ENDING; | |
1849 | status = state_ending(trimmer_it, msgs, | |
1850 | capacity, count); | |
1851 | goto end; | |
1852 | } | |
1853 | } | |
1854 | } | |
1855 | ||
1856 | /* | |
1857 | * There's at least one message in the output message queue: | |
1858 | * move the messages to the output message array. | |
1859 | */ | |
98b15851 | 1860 | BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it->output_messages)); |
7de0e49a PP |
1861 | fill_message_array_from_output_messages(trimmer_it, msgs, |
1862 | capacity, count); | |
1863 | ||
1864 | end: | |
1865 | return status; | |
1866 | } | |
1867 | ||
1868 | BT_HIDDEN | |
a3f0c7db | 1869 | bt_message_iterator_class_next_method_status trimmer_msg_iter_next( |
7de0e49a PP |
1870 | bt_self_message_iterator *self_msg_iter, |
1871 | bt_message_array_const msgs, uint64_t capacity, | |
1872 | uint64_t *count) | |
1873 | { | |
1874 | struct trimmer_iterator *trimmer_it = | |
1875 | bt_self_message_iterator_get_data(self_msg_iter); | |
a3f0c7db SM |
1876 | bt_message_iterator_class_next_method_status status = |
1877 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
7de0e49a | 1878 | |
98b15851 | 1879 | BT_ASSERT_DBG(trimmer_it); |
7de0e49a | 1880 | |
91d81473 | 1881 | if (G_LIKELY(trimmer_it->state == TRIMMER_ITERATOR_STATE_TRIM)) { |
7de0e49a | 1882 | status = state_trim(trimmer_it, msgs, capacity, count); |
a3f0c7db | 1883 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1884 | goto end; |
1885 | } | |
1886 | } else { | |
1887 | switch (trimmer_it->state) { | |
1888 | case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN: | |
1889 | status = state_set_trimmer_iterator_bounds(trimmer_it); | |
a3f0c7db | 1890 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1891 | goto end; |
1892 | } | |
1893 | ||
1894 | status = state_seek_initially(trimmer_it); | |
a3f0c7db | 1895 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1896 | goto end; |
1897 | } | |
1898 | ||
1899 | status = state_trim(trimmer_it, msgs, capacity, count); | |
a3f0c7db | 1900 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1901 | goto end; |
1902 | } | |
1903 | ||
1904 | break; | |
1905 | case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY: | |
1906 | status = state_seek_initially(trimmer_it); | |
a3f0c7db | 1907 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1908 | goto end; |
1909 | } | |
1910 | ||
1911 | status = state_trim(trimmer_it, msgs, capacity, count); | |
a3f0c7db | 1912 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1913 | goto end; |
1914 | } | |
1915 | ||
1916 | break; | |
1917 | case TRIMMER_ITERATOR_STATE_ENDING: | |
1918 | status = state_ending(trimmer_it, msgs, capacity, | |
1919 | count); | |
a3f0c7db | 1920 | if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) { |
7de0e49a PP |
1921 | goto end; |
1922 | } | |
1923 | ||
1924 | break; | |
1925 | case TRIMMER_ITERATOR_STATE_ENDED: | |
a3f0c7db | 1926 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
7de0e49a PP |
1927 | break; |
1928 | default: | |
498e7994 | 1929 | bt_common_abort(); |
7de0e49a PP |
1930 | } |
1931 | } | |
1932 | ||
1933 | end: | |
1934 | return status; | |
1935 | } | |
1936 | ||
1937 | BT_HIDDEN | |
1938 | void trimmer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) | |
1939 | { | |
1940 | struct trimmer_iterator *trimmer_it = | |
1941 | bt_self_message_iterator_get_data(self_msg_iter); | |
1942 | ||
1943 | BT_ASSERT(trimmer_it); | |
1944 | destroy_trimmer_iterator(trimmer_it); | |
1945 | } |