151ae25c1ad2be5f3243bc22094c186556a61b29
[lttng-tools.git] / src / bin / lttng-relayd / stream.h
1 #ifndef _STREAM_H
2 #define _STREAM_H
3
4 /*
5 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
6 * David Goulet <dgoulet@efficios.com>
7 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
23 #include <limits.h>
24 #include <inttypes.h>
25 #include <pthread.h>
26 #include <urcu/list.h>
27
28 #include <common/hashtable/hashtable.h>
29
30 #include "session.h"
31 #include "stream-fd.h"
32 #include "tracefile-array.h"
33
34 struct relay_stream_chunk_id {
35 bool is_set;
36 uint64_t value;
37 };
38
39 /*
40 * Represents a stream in the relay
41 */
42 struct relay_stream {
43 uint64_t stream_handle;
44
45 struct urcu_ref ref;
46 /* Back reference to trace. Protected by refcount on trace object. */
47 struct ctf_trace *trace;
48
49 /*
50 * To protect from concurrent read/update. The viewer stream
51 * lock nests inside the stream lock. The stream lock nests
52 * inside the ctf_trace lock.
53 */
54 pthread_mutex_t lock;
55 uint64_t prev_seq; /* previous data sequence number encountered. */
56 /* previous index sequence number written to disk. */
57 uint64_t prev_index_seq;
58 uint64_t last_net_seq_num; /* seq num to encounter before closing. */
59
60 /* FD on which to write the stream data. */
61 struct stream_fd *stream_fd;
62 /* index file on which to write the index data. */
63 struct lttng_index_file *index_file;
64
65 char *path_name;
66 char *channel_name;
67
68 /* On-disk circular buffer of tracefiles. */
69 uint64_t tracefile_size;
70 uint64_t tracefile_size_current;
71 uint64_t tracefile_count;
72
73 /*
74 * Position in the tracefile where we have the full index also on disk.
75 */
76 uint64_t pos_after_last_complete_data_index;
77
78 /*
79 * Counts the number of received indexes. The "tag" associated
80 * with an index is taken before incrementing this seqcount.
81 * Therefore, the sequence tag associated with the last index
82 * received is always index_received_seqcount - 1.
83 */
84 uint64_t index_received_seqcount;
85
86 /*
87 * Tracefile array is an index of the stream trace files,
88 * indexed by position. It allows keeping track of the oldest
89 * available indexes when overwriting trace files in tracefile
90 * rotation.
91 */
92 struct tracefile_array *tfa;
93
94 bool closed; /* Stream is closed. */
95 bool close_requested; /* Close command has been received. */
96
97 /*
98 * Counts number of indexes in indexes_ht. Redundant info.
99 * Protected by stream lock.
100 */
101 int indexes_in_flight;
102 struct lttng_ht *indexes_ht;
103
104 /*
105 * If the stream is inactive, this field is updated with the
106 * live beacon timestamp end, when it is active, this
107 * field == -1ULL.
108 */
109 uint64_t beacon_ts_end;
110
111 /* CTF stream ID, -1ULL when unset (first packet not received yet). */
112 uint64_t ctf_stream_id;
113
114 /* Indicate if the stream was initialized for a data pending command. */
115 bool data_pending_check_done;
116
117 /* Is this stream a metadata stream ? */
118 int32_t is_metadata;
119 /* Amount of metadata received (bytes). */
120 uint64_t metadata_received;
121
122 /*
123 * Member of the stream list in struct ctf_trace.
124 * Updates are protected by the stream_list_lock.
125 * Traversals are protected by RCU.
126 */
127 struct cds_list_head stream_node;
128 /*
129 * Temporary list belonging to the connection until all streams
130 * are received for that connection.
131 * Member of the stream recv list in the connection.
132 * Updates are protected by the stream_recv_list_lock.
133 * Traversals are protected by RCU.
134 */
135 bool in_recv_list;
136 struct cds_list_head recv_node;
137 bool published; /* Protected by session lock. */
138 /*
139 * Node of stream within global stream hash table.
140 */
141 struct lttng_ht_node_u64 node;
142 bool in_stream_ht; /* is stream in stream hash table. */
143 struct rcu_head rcu_node; /* For call_rcu teardown. */
144 /*
145 * When we have written the data and index corresponding to this
146 * seq_num, rotate the tracefile (session rotation). The path_name is
147 * already up-to-date.
148 * This is set to -1ULL when no rotation is pending.
149 *
150 * Always access with stream lock held.
151 */
152 uint64_t rotate_at_seq_num;
153 /*
154 * This is the id of the chunk where we are writing to if no rotation is
155 * pending (rotate_at_seq_num == -1ULL). If a rotation is pending, this
156 * is the chunk_id we will have after the rotation. It must be updated
157 * atomically with rotate_at_seq_num.
158 *
159 * Always access with stream lock held.
160 *
161 * This attribute is not set if the stream is created by a pre-2.11
162 * consumer.
163 */
164 struct relay_stream_chunk_id current_chunk_id;
165 };
166
167 struct relay_stream *stream_create(struct ctf_trace *trace,
168 uint64_t stream_handle, char *path_name,
169 char *channel_name, uint64_t tracefile_size,
170 uint64_t tracefile_count, const struct relay_stream_chunk_id *chunk_id);
171
172 struct relay_stream *stream_get_by_id(uint64_t stream_id);
173 bool stream_get(struct relay_stream *stream);
174 void stream_put(struct relay_stream *stream);
175 void try_stream_close(struct relay_stream *stream);
176 void stream_publish(struct relay_stream *stream);
177 void print_relay_streams(void);
178
179 #endif /* _STREAM_H */
This page took 0.033744 seconds and 5 git commands to generate.