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