| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_PLUGINS_CTF_FS_SINK_FS_SINK_STREAM_HPP |
| 8 | #define BABELTRACE_PLUGINS_CTF_FS_SINK_FS_SINK_STREAM_HPP |
| 9 | |
| 10 | #include <glib.h> |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #include <babeltrace2/babeltrace.h> |
| 14 | |
| 15 | #include "cpp-common/bt2c/logging.hpp" |
| 16 | #include "ctfser/ctfser.h" |
| 17 | |
| 18 | struct fs_sink_trace; |
| 19 | struct fs_sink_ctf_stream_class; |
| 20 | |
| 21 | struct fs_sink_stream |
| 22 | { |
| 23 | explicit fs_sink_stream(const bt2c::Logger& parentLogger) : |
| 24 | logger {parentLogger, "PLUGIN/SINK.CTF.FS/STREAM"} |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | bt2c::Logger logger; |
| 29 | fs_sink_trace *trace = nullptr; |
| 30 | bt_ctfser ctfser {}; |
| 31 | |
| 32 | /* Stream's file name */ |
| 33 | GString *file_name = nullptr; |
| 34 | |
| 35 | /* Weak */ |
| 36 | const bt_stream *ir_stream = nullptr; |
| 37 | |
| 38 | fs_sink_ctf_stream_class *sc = nullptr; |
| 39 | |
| 40 | /* Current packet's state */ |
| 41 | struct |
| 42 | { |
| 43 | /* |
| 44 | * True if we're, for this stream, within an opened |
| 45 | * packet (got a packet beginning message, but no |
| 46 | * packet end message yet). |
| 47 | */ |
| 48 | bool is_open = false; |
| 49 | |
| 50 | /* |
| 51 | * Current beginning default clock snapshot for the |
| 52 | * current packet (`UINT64_C(-1)` if not set). |
| 53 | */ |
| 54 | uint64_t beginning_cs = 0; |
| 55 | |
| 56 | /* |
| 57 | * Current end default clock snapshot for the current |
| 58 | * packet (`UINT64_C(-1)` if not set). |
| 59 | */ |
| 60 | uint64_t end_cs = 0; |
| 61 | |
| 62 | /* |
| 63 | * Current packet's content size (bits) for the current |
| 64 | * packet. |
| 65 | */ |
| 66 | uint64_t content_size = 0; |
| 67 | |
| 68 | /* |
| 69 | * Current packet's total size (bits) for the current |
| 70 | * packet. |
| 71 | */ |
| 72 | uint64_t total_size = 0; |
| 73 | |
| 74 | /* |
| 75 | * Discarded events (free running) counter for the |
| 76 | * current packet. |
| 77 | */ |
| 78 | uint64_t discarded_events_counter = 0; |
| 79 | |
| 80 | /* Sequence number (free running) of the current packet */ |
| 81 | uint64_t seq_num = 0; |
| 82 | |
| 83 | /* |
| 84 | * Offset of the packet context structure within the |
| 85 | * current packet (bits). |
| 86 | */ |
| 87 | uint64_t context_offset_bits = 0; |
| 88 | |
| 89 | /* |
| 90 | * Owned by this; `NULL` if the current packet is closed |
| 91 | * or if the trace IR stream does not support packets. |
| 92 | */ |
| 93 | const bt_packet *packet = nullptr; |
| 94 | } packet_state; |
| 95 | |
| 96 | /* Previous packet's state */ |
| 97 | struct |
| 98 | { |
| 99 | /* End default clock snapshot (`UINT64_C(-1)` if not set) */ |
| 100 | uint64_t end_cs = 0; |
| 101 | |
| 102 | /* Discarded events (free running) counter */ |
| 103 | uint64_t discarded_events_counter = 0; |
| 104 | |
| 105 | /* Sequence number (free running) */ |
| 106 | uint64_t seq_num = 0; |
| 107 | } prev_packet_state; |
| 108 | |
| 109 | /* State to handle discarded events */ |
| 110 | struct |
| 111 | { |
| 112 | /* |
| 113 | * True if we're in the time range given by a previously |
| 114 | * received discarded events message. In this case, |
| 115 | * `beginning_cs` and `end_cs` below contain the |
| 116 | * beginning and end clock snapshots for this range. |
| 117 | * |
| 118 | * This is used to validate that, when receiving a |
| 119 | * packet end message, the current discarded events time |
| 120 | * range matches what's expected for CTF 1.8, that is: |
| 121 | * |
| 122 | * * Its beginning time is the previous packet's end |
| 123 | * time (or the current packet's beginning time if |
| 124 | * this is the first packet). |
| 125 | * |
| 126 | * * Its end time is the current packet's end time. |
| 127 | */ |
| 128 | bool in_range = false; |
| 129 | |
| 130 | /* |
| 131 | * Beginning and end times of the time range given by a |
| 132 | * previously received discarded events message. |
| 133 | */ |
| 134 | uint64_t beginning_cs = 0; |
| 135 | uint64_t end_cs = 0; |
| 136 | } discarded_events_state; |
| 137 | |
| 138 | /* State to handle discarded packets */ |
| 139 | struct |
| 140 | { |
| 141 | /* |
| 142 | * True if we're in the time range given by a previously |
| 143 | * received discarded packets message. In this case, |
| 144 | * `beginning_cs` and `end_cs` below contain the |
| 145 | * beginning and end clock snapshots for this range. |
| 146 | * |
| 147 | * This is used to validate that, when receiving a |
| 148 | * packet beginning message, the current discarded |
| 149 | * packets time range matches what's expected for CTF |
| 150 | * 1.8, that is: |
| 151 | * |
| 152 | * * Its beginning time is the previous packet's end |
| 153 | * time. |
| 154 | * |
| 155 | * * Its end time is the current packet's beginning |
| 156 | * time. |
| 157 | */ |
| 158 | bool in_range = false; |
| 159 | |
| 160 | /* |
| 161 | * Beginning and end times of the time range given by a |
| 162 | * previously received discarded packets message. |
| 163 | */ |
| 164 | uint64_t beginning_cs = 0; |
| 165 | uint64_t end_cs = 0; |
| 166 | } discarded_packets_state; |
| 167 | }; |
| 168 | |
| 169 | struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace, |
| 170 | const bt_stream *ir_stream); |
| 171 | |
| 172 | void fs_sink_stream_destroy(struct fs_sink_stream *stream); |
| 173 | |
| 174 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, const bt_clock_snapshot *cs, |
| 175 | const bt_event *event, struct fs_sink_ctf_event_class *ec); |
| 176 | |
| 177 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, const bt_clock_snapshot *cs, |
| 178 | const bt_packet *packet); |
| 179 | |
| 180 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, const bt_clock_snapshot *cs); |
| 181 | |
| 182 | #endif /* BABELTRACE_PLUGINS_CTF_FS_SINK_FS_SINK_STREAM_HPP */ |