Fix: relayd: notify parent of readiness when all threads ready
[lttng-tools.git] / src / bin / lttng-relayd / lttng-relayd.h
1 /*
2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef LTTNG_RELAYD_H
20 #define LTTNG_RELAYD_H
21
22 #define _LGPL_SOURCE
23 #include <limits.h>
24 #include <urcu.h>
25 #include <urcu/wfqueue.h>
26 #include <urcu/list.h>
27
28 #include <common/hashtable/hashtable.h>
29 #include <common/index/ctf-index.h>
30
31 #include "ctf-trace.h"
32
33 /*
34 * Queue used to enqueue relay requests
35 */
36 struct relay_cmd_queue {
37 struct cds_wfq_queue queue;
38 int32_t futex;
39 };
40
41 enum connection_type {
42 RELAY_DATA = 1,
43 RELAY_CONTROL = 2,
44 RELAY_VIEWER_COMMAND = 3,
45 RELAY_VIEWER_NOTIFICATION = 4,
46 };
47
48 /*
49 * When we receive a stream, it gets stored in a list (on a per connection
50 * basis) until we have all the streams of the same channel and the metadata
51 * associated with it, then it gets flagged with viewer_ready.
52 */
53 struct relay_stream_recv_handle {
54 uint64_t id; /* stream handle */
55 struct cds_list_head node;
56 };
57
58 /*
59 * Represents a session for the relay point of view
60 */
61 struct relay_session {
62 /*
63 * This session id is used to identify a set of stream to a tracing session
64 * but also make sure we have a unique session id associated with a session
65 * daemon which can provide multiple data source.
66 */
67 uint64_t id;
68 struct lttcomm_sock *sock;
69 char session_name[NAME_MAX];
70 char hostname[HOST_NAME_MAX];
71 uint32_t live_timer;
72 struct lttng_ht_node_ulong session_n;
73 struct rcu_head rcu_node;
74 uint32_t viewer_attached;
75 uint32_t stream_count;
76 /* Tell if this session is for a snapshot or not. */
77 unsigned int snapshot:1;
78
79 /*
80 * Indicate version protocol for this session. This is especially useful
81 * for the data thread that has no idea which version it operates on since
82 * linking control/data sockets is non trivial.
83 */
84 uint64_t minor;
85 uint64_t major;
86 };
87
88 /*
89 * Represents a stream in the relay
90 */
91 struct relay_stream {
92 uint64_t stream_handle;
93 uint64_t prev_seq; /* previous data sequence number encountered */
94 struct lttng_ht_node_ulong stream_n;
95 struct relay_session *session;
96 struct rcu_head rcu_node;
97 int fd;
98 /* FD on which to write the index data. */
99 int index_fd;
100 /* FD on which to read the index data for the viewer. */
101 int read_index_fd;
102
103 char *path_name;
104 char *channel_name;
105 /* on-disk circular buffer of tracefiles */
106 uint64_t tracefile_size;
107 uint64_t tracefile_size_current;
108 uint64_t tracefile_count;
109 uint64_t tracefile_count_current;
110 /* To inform the viewer up to where it can go back in time. */
111 uint64_t oldest_tracefile_id;
112
113 uint64_t total_index_received;
114 struct relay_viewer_stream *viewer_stream;
115 uint64_t last_net_seq_num;
116
117 /*
118 * This node is added to the *control* connection hash table and the
119 * pointer is copied in here so we can access it when deleting this object.
120 * When deleting this, the ctf trace ht MUST NOT be destroyed. This happens
121 * at connection deletion.
122 */
123 struct lttng_ht_node_str ctf_trace_node;
124 struct lttng_ht *ctf_traces_ht;
125
126 /*
127 * To protect from concurrent read/update between the
128 * streaming-side and the viewer-side.
129 * This lock must be held, we reading/updating the
130 * ctf_trace pointer.
131 */
132 pthread_mutex_t lock;
133
134 struct ctf_trace *ctf_trace;
135 /*
136 * If the stream is inactive, this field is updated with the live beacon
137 * timestamp end, when it is active, this field == -1ULL.
138 */
139 uint64_t beacon_ts_end;
140 /*
141 * To protect the update of the close_write_flag and the checks of
142 * the tracefile_count_current.
143 * It is taken before checking whenever we need to know if the
144 * writer and reader are working in the same tracefile.
145 */
146 pthread_mutex_t viewer_stream_rotation_lock;
147
148 /* Information telling us when to close the stream */
149 unsigned int close_flag:1;
150 /* Indicate if the stream was initialized for a data pending command. */
151 unsigned int data_pending_check_done:1;
152 unsigned int metadata_flag:1;
153 /*
154 * To detect when we start overwriting old data, it is used to
155 * update the oldest_tracefile_id.
156 */
157 unsigned int tracefile_overwrite:1;
158 /*
159 * Can this stream be used by a viewer or are we waiting for additional
160 * information.
161 */
162 unsigned int viewer_ready:1;
163 };
164
165 /*
166 * Shadow copy of the relay_stream structure for the viewer side. The only
167 * fields updated by the writer (streaming side) after allocation are :
168 * total_index_received and close_flag. Everything else is updated by the
169 * reader (viewer side).
170 */
171 struct relay_viewer_stream {
172 uint64_t stream_handle;
173 uint64_t session_id;
174 int read_fd;
175 int index_read_fd;
176 char *path_name;
177 char *channel_name;
178 uint64_t last_sent_index;
179 uint64_t total_index_received;
180 uint64_t tracefile_count;
181 uint64_t tracefile_count_current;
182 /* Stop after reading this tracefile. */
183 uint64_t tracefile_count_last;
184 struct lttng_ht_node_u64 stream_n;
185 struct rcu_head rcu_node;
186 struct ctf_trace *ctf_trace;
187 /*
188 * This lock blocks only when the writer is about to start overwriting
189 * a file currently read by the reader.
190 *
191 * This is nested INSIDE the viewer_stream_rotation_lock.
192 */
193 pthread_mutex_t overwrite_lock;
194 /* Information telling us if the stream is a metadata stream. */
195 unsigned int metadata_flag:1;
196 /*
197 * Information telling us that the stream is closed in write, so
198 * we don't expect new indexes and we can read up to EOF.
199 */
200 unsigned int close_write_flag:1;
201 /*
202 * If the streaming side closes a FD in use in the viewer side,
203 * it sets this flag to inform that it is a normal error.
204 */
205 unsigned int abort_flag:1;
206 };
207
208 /*
209 * Internal structure to map a socket with the corresponding session.
210 * A hashtable indexed on the socket FD is used for the lookups.
211 */
212 struct relay_command {
213 struct lttcomm_sock *sock;
214 struct relay_session *session;
215 struct cds_wfq_node node;
216 struct lttng_ht_node_ulong sock_n;
217 struct rcu_head rcu_node;
218 enum connection_type type;
219 /* protocol version to use for this session */
220 uint32_t major;
221 uint32_t minor;
222 struct lttng_ht *ctf_traces_ht; /* indexed by path name */
223 uint64_t session_id;
224 struct cds_list_head recv_head;
225 unsigned int version_check_done:1;
226 };
227
228 struct relay_local_data {
229 struct lttng_ht *sessions_ht;
230 };
231
232 extern char *opt_output_path;
233
234 extern struct lttng_ht *relay_streams_ht;
235 extern struct lttng_ht *viewer_streams_ht;
236 extern struct lttng_ht *indexes_ht;
237
238 extern const char *tracing_group_name;
239
240 extern const char * const config_section_name;
241
242 struct relay_stream *relay_stream_find_by_id(uint64_t stream_id);
243 void lttng_relay_notify_ready(void);
244
245 #endif /* LTTNG_RELAYD_H */
This page took 0.035374 seconds and 5 git commands to generate.