+enum target_event {
+ FIRST_EVENT,
+ LAST_EVENT,
+};
+
+static
+int decode_clock_snapshot_after_event(struct ctf_fs_trace *ctf_fs_trace,
+ struct ctf_clock_class *default_cc,
+ struct ctf_fs_ds_index_entry *index_entry,
+ enum target_event target_event, uint64_t *cs, int64_t *ts_ns)
+{
+ enum bt_msg_iter_status iter_status = BT_MSG_ITER_STATUS_OK;
+ struct ctf_fs_ds_file *ds_file = NULL;
+ struct bt_msg_iter *msg_iter = NULL;
+ bt_logging_level log_level = ctf_fs_trace->log_level;
+ bt_self_component *self_comp = ctf_fs_trace->self_comp;
+ int ret = 0;
+
+ BT_ASSERT(ctf_fs_trace);
+ BT_ASSERT(ctf_fs_trace->metadata);
+ BT_ASSERT(ctf_fs_trace->metadata->tc);
+
+ msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc,
+ bt_common_get_page_size(log_level) * 8, ctf_fs_ds_file_medops,
+ NULL, log_level, self_comp);
+ if (!msg_iter) {
+ /* bt_msg_iter_create() logs errors. */
+ ret = -1;
+ goto end;
+ }
+
+ BT_ASSERT(index_entry);
+ BT_ASSERT(index_entry->path);
+
+ ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter,
+ NULL, index_entry->path, log_level);
+ if (!ds_file) {
+ BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create a ctf_fs_ds_file");
+ ret = -1;
+ goto end;
+ }
+
+ /*
+ * Turn on dry run mode to prevent the creation and usage of Babeltrace
+ * library objects (bt_field, bt_message_*, etc.).
+ */
+ bt_msg_iter_set_dry_run(msg_iter, true);
+
+ /* Seek to the beginning of the target packet. */
+ iter_status = bt_msg_iter_seek(ds_file->msg_iter, index_entry->offset);
+ if (iter_status) {
+ /* bt_msg_iter_seek() logs errors. */
+ ret = -1;
+ goto end;
+ }
+
+ switch (target_event) {
+ case FIRST_EVENT:
+ /*
+ * Start to decode the packet until we reach the end of
+ * the first event. To extract the first event's clock
+ * snapshot.
+ */
+ iter_status = bt_msg_iter_curr_packet_first_event_clock_snapshot(
+ ds_file->msg_iter, cs);
+ break;
+ case LAST_EVENT:
+ /* Decode the packet to extract the last event's clock snapshot. */
+ iter_status = bt_msg_iter_curr_packet_last_event_clock_snapshot(
+ ds_file->msg_iter, cs);
+ break;
+ default:
+ abort();
+ }
+ if (iter_status) {
+ ret = -1;
+ goto end;
+ }
+
+ /* Convert clock snapshot to timestamp. */
+ ret = bt_util_clock_cycles_to_ns_from_origin(*cs,
+ default_cc->frequency, default_cc->offset_seconds,
+ default_cc->offset_cycles, ts_ns);
+ if (ret) {
+ BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+ "Failed to convert clock snapshot to timestamp");
+ goto end;
+ }
+
+end:
+ if (ds_file) {
+ ctf_fs_ds_file_destroy(ds_file);
+ }
+ if (msg_iter) {
+ bt_msg_iter_destroy(msg_iter);
+ }
+
+ return ret;
+}
+
+static
+int decode_packet_first_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
+ struct ctf_clock_class *default_cc,
+ struct ctf_fs_ds_index_entry *index_entry, uint64_t *cs, int64_t *ts_ns)
+{
+ return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc,
+ index_entry, FIRST_EVENT, cs, ts_ns);
+}
+
+static
+int decode_packet_last_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
+ struct ctf_clock_class *default_cc,
+ struct ctf_fs_ds_index_entry *index_entry, uint64_t *cs, int64_t *ts_ns)
+{
+ return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc,
+ index_entry, LAST_EVENT, cs, ts_ns);
+}
+