| 1 | #ifndef _STREAM_H |
| 2 | #define _STREAM_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 6 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 7 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0-only |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <limits.h> |
| 14 | #include <inttypes.h> |
| 15 | #include <pthread.h> |
| 16 | #include <urcu/list.h> |
| 17 | |
| 18 | #include <common/hashtable/hashtable.h> |
| 19 | #include <common/trace-chunk.h> |
| 20 | #include <common/optional.h> |
| 21 | #include <common/buffer-view.h> |
| 22 | |
| 23 | #include "session.h" |
| 24 | #include "tracefile-array.h" |
| 25 | |
| 26 | struct lttcomm_relayd_index; |
| 27 | |
| 28 | struct relay_stream_rotation { |
| 29 | /* |
| 30 | * Indicates if the stream's data and index have been rotated. A |
| 31 | * rotation is considered completed when both rotations have occurred. |
| 32 | */ |
| 33 | bool data_rotated; |
| 34 | bool index_rotated; |
| 35 | /* |
| 36 | * Packet sequence number of the first packet of the new trace chunk to |
| 37 | * which the stream is rotating. |
| 38 | */ |
| 39 | uint64_t packet_seq_num; |
| 40 | /* |
| 41 | * Monotonically increasing previous network sequence number of first |
| 42 | * data packet of the new trace chunk to which the stream is rotating. |
| 43 | */ |
| 44 | uint64_t prev_data_net_seq; |
| 45 | struct lttng_trace_chunk *next_trace_chunk; |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * Represents a stream in the relay |
| 50 | */ |
| 51 | struct relay_stream { |
| 52 | uint64_t stream_handle; |
| 53 | |
| 54 | struct urcu_ref ref; |
| 55 | /* Back reference to trace. Protected by refcount on trace object. */ |
| 56 | struct ctf_trace *trace; |
| 57 | |
| 58 | /* |
| 59 | * To protect from concurrent read/update. The viewer stream |
| 60 | * lock nests inside the stream lock. The stream lock nests |
| 61 | * inside the ctf_trace lock. |
| 62 | */ |
| 63 | pthread_mutex_t lock; |
| 64 | /* previous data sequence number written to disk. */ |
| 65 | uint64_t prev_data_seq; |
| 66 | /* previous index sequence number written to disk. */ |
| 67 | uint64_t prev_index_seq; |
| 68 | /* seq num to encounter before closing. */ |
| 69 | uint64_t last_net_seq_num; |
| 70 | |
| 71 | struct fs_handle *file; |
| 72 | /* index file on which to write the index data. */ |
| 73 | struct lttng_index_file *index_file; |
| 74 | |
| 75 | char *path_name; |
| 76 | char *channel_name; |
| 77 | |
| 78 | /* On-disk circular buffer of tracefiles. */ |
| 79 | uint64_t tracefile_size; |
| 80 | uint64_t tracefile_size_current; |
| 81 | /* Max number of trace files for this stream. */ |
| 82 | uint64_t tracefile_count; |
| 83 | /* |
| 84 | * Index of the currently active file for this stream's on-disk |
| 85 | * ring buffer. |
| 86 | */ |
| 87 | uint64_t tracefile_current_index; |
| 88 | /* |
| 89 | * Indicates that the on-disk buffer has wrapped around. Stream |
| 90 | * files shall be unlinked before being opened after this has occurred. |
| 91 | */ |
| 92 | bool tracefile_wrapped_around; |
| 93 | |
| 94 | /* |
| 95 | * Position in the tracefile where we have the full index also on disk. |
| 96 | */ |
| 97 | uint64_t pos_after_last_complete_data_index; |
| 98 | |
| 99 | /* |
| 100 | * Counts the number of received indexes. The "tag" associated |
| 101 | * with an index is taken before incrementing this seqcount. |
| 102 | * Therefore, the sequence tag associated with the last index |
| 103 | * received is always index_received_seqcount - 1. |
| 104 | */ |
| 105 | uint64_t index_received_seqcount; |
| 106 | |
| 107 | /* |
| 108 | * Packet sequence number of the last received packet index. |
| 109 | * Only populated when interacting with CTF_INDEX 1.1+. |
| 110 | */ |
| 111 | LTTNG_OPTIONAL(uint64_t) received_packet_seq_num; |
| 112 | |
| 113 | /* |
| 114 | * Tracefile array is an index of the stream trace files, |
| 115 | * indexed by position. It allows keeping track of the oldest |
| 116 | * available indexes when overwriting trace files in tracefile |
| 117 | * rotation. |
| 118 | */ |
| 119 | struct tracefile_array *tfa; |
| 120 | |
| 121 | bool closed; /* Stream is closed. */ |
| 122 | bool close_requested; /* Close command has been received. */ |
| 123 | |
| 124 | /* |
| 125 | * Counts number of indexes in indexes_ht. Redundant info. |
| 126 | * Protected by stream lock. |
| 127 | */ |
| 128 | int indexes_in_flight; |
| 129 | struct lttng_ht *indexes_ht; |
| 130 | |
| 131 | /* |
| 132 | * If the stream is inactive, this field is updated with the |
| 133 | * live beacon timestamp end, when it is active, this |
| 134 | * field == -1ULL. |
| 135 | */ |
| 136 | uint64_t beacon_ts_end; |
| 137 | |
| 138 | /* CTF stream ID, -1ULL when unset (first packet not received yet). */ |
| 139 | uint64_t ctf_stream_id; |
| 140 | |
| 141 | /* Indicate if the stream was initialized for a data pending command. */ |
| 142 | bool data_pending_check_done; |
| 143 | |
| 144 | /* Is this stream a metadata stream ? */ |
| 145 | bool is_metadata; |
| 146 | /* Amount of metadata received (bytes). */ |
| 147 | uint64_t metadata_received; |
| 148 | |
| 149 | /* |
| 150 | * Member of the stream list in struct ctf_trace. |
| 151 | * Updates are protected by the stream_list_lock. |
| 152 | * Traversals are protected by RCU. |
| 153 | */ |
| 154 | struct cds_list_head stream_node; |
| 155 | /* |
| 156 | * Temporary list belonging to the connection until all streams |
| 157 | * are received for that connection. |
| 158 | * Member of the stream recv list in the connection. |
| 159 | * Updates are protected by the stream_recv_list_lock. |
| 160 | * Traversals are protected by RCU. |
| 161 | */ |
| 162 | bool in_recv_list; |
| 163 | struct cds_list_head recv_node; |
| 164 | /* Protected by session lock. */ |
| 165 | bool published; |
| 166 | /* Notified viewer that no new metadata is available. */ |
| 167 | bool no_new_metadata_notified; |
| 168 | /* |
| 169 | * Node of stream within global stream hash table. |
| 170 | */ |
| 171 | struct lttng_ht_node_u64 node; |
| 172 | bool in_stream_ht; /* is stream in stream hash table. */ |
| 173 | struct rcu_head rcu_node; /* For call_rcu teardown. */ |
| 174 | /* |
| 175 | * The trace chunk to which the file currently being produced (if any) |
| 176 | * belongs. |
| 177 | * |
| 178 | * Note that a relay stream can have no output trace chunk. For |
| 179 | * instance, after a session stop followed by a session clear, |
| 180 | * streams will not have an output trace chunk until the session |
| 181 | * is resumed. |
| 182 | */ |
| 183 | struct lttng_trace_chunk *trace_chunk; |
| 184 | LTTNG_OPTIONAL(struct relay_stream_rotation) ongoing_rotation; |
| 185 | uint64_t completed_rotation_count; |
| 186 | }; |
| 187 | |
| 188 | struct relay_stream *stream_create(struct ctf_trace *trace, |
| 189 | uint64_t stream_handle, char *path_name, |
| 190 | char *channel_name, uint64_t tracefile_size, |
| 191 | uint64_t tracefile_count); |
| 192 | |
| 193 | struct relay_stream *stream_get_by_id(uint64_t stream_id); |
| 194 | bool stream_get(struct relay_stream *stream); |
| 195 | void stream_put(struct relay_stream *stream); |
| 196 | int stream_rotate_output_files(struct relay_session *session, |
| 197 | struct relay_stream *stream); |
| 198 | int stream_set_pending_rotation(struct relay_stream *stream, |
| 199 | struct lttng_trace_chunk *next_trace_chunk, |
| 200 | uint64_t rotation_sequence_number); |
| 201 | void try_stream_close(struct relay_stream *stream); |
| 202 | void stream_publish(struct relay_stream *stream); |
| 203 | int stream_init_packet(struct relay_stream *stream, size_t packet_size, |
| 204 | bool *file_rotated); |
| 205 | int stream_write(struct relay_stream *stream, |
| 206 | const struct lttng_buffer_view *packet, size_t padding_len); |
| 207 | /* Called after the reception of a complete data packet. */ |
| 208 | int stream_update_index(struct relay_stream *stream, uint64_t net_seq_num, |
| 209 | bool rotate_index, bool *flushed, uint64_t total_size); |
| 210 | int stream_complete_packet(struct relay_stream *stream, |
| 211 | size_t packet_total_size, uint64_t sequence_number, |
| 212 | bool index_flushed); |
| 213 | /* Index info is in host endianness. */ |
| 214 | int stream_add_index(struct relay_stream *stream, |
| 215 | const struct lttcomm_relayd_index *index_info); |
| 216 | int stream_reset_file(struct relay_stream *stream); |
| 217 | |
| 218 | void print_relay_streams(void); |
| 219 | |
| 220 | #endif /* _STREAM_H */ |