relayd: replace lttng_index_file with relay_index_file
[deliverable/lttng-tools.git] / src / bin / lttng-relayd / stream.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <common/common.h>
22 #include <common/utils.h>
23 #include <common/defaults.h>
24 #include <urcu/rculist.h>
25 #include <sys/stat.h>
26
27 #include "lttng-relayd.h"
28 #include "index.h"
29 #include "stream.h"
30 #include "viewer-stream.h"
31
32 /* Should be called with RCU read-side lock held. */
33 bool stream_get(struct relay_stream *stream)
34 {
35 return urcu_ref_get_unless_zero(&stream->ref);
36 }
37
38 /*
39 * Get stream from stream id from the streams hash table. Return stream
40 * if found else NULL. A stream reference is taken when a stream is
41 * returned. stream_put() must be called on that stream.
42 */
43 struct relay_stream *stream_get_by_id(uint64_t stream_id)
44 {
45 struct lttng_ht_node_u64 *node;
46 struct lttng_ht_iter iter;
47 struct relay_stream *stream = NULL;
48
49 rcu_read_lock();
50 lttng_ht_lookup(relay_streams_ht, &stream_id, &iter);
51 node = lttng_ht_iter_get_node_u64(&iter);
52 if (!node) {
53 DBG("Relay stream %" PRIu64 " not found", stream_id);
54 goto end;
55 }
56 stream = caa_container_of(node, struct relay_stream, node);
57 if (!stream_get(stream)) {
58 stream = NULL;
59 }
60 end:
61 rcu_read_unlock();
62 return stream;
63 }
64
65 /*
66 * We keep ownership of path_name and channel_name.
67 */
68 struct relay_stream *stream_create(struct ctf_trace *trace,
69 uint64_t stream_handle, char *path_name,
70 char *channel_name, uint64_t tracefile_size,
71 uint64_t tracefile_count,
72 const struct relay_stream_chunk_id *chunk_id)
73 {
74 int ret;
75 struct relay_stream *stream = NULL;
76 struct relay_session *session = trace->session;
77
78 stream = zmalloc(sizeof(struct relay_stream));
79 if (stream == NULL) {
80 PERROR("relay stream zmalloc");
81 goto error_no_alloc;
82 }
83
84 stream->stream_handle = stream_handle;
85 stream->prev_seq = -1ULL;
86 stream->last_net_seq_num = -1ULL;
87 stream->ctf_stream_id = -1ULL;
88 stream->tracefile_size = tracefile_size;
89 stream->tracefile_count = tracefile_count;
90 stream->path_name = path_name;
91 stream->channel_name = channel_name;
92 stream->rotate_at_seq_num = -1ULL;
93 lttng_ht_node_init_u64(&stream->node, stream->stream_handle);
94 pthread_mutex_init(&stream->lock, NULL);
95 urcu_ref_init(&stream->ref);
96 ctf_trace_get(trace);
97 stream->trace = trace;
98 stream->current_chunk_id = *chunk_id;
99
100 stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
101 if (!stream->indexes_ht) {
102 ERR("Cannot created indexes_ht");
103 ret = -1;
104 goto end;
105 }
106
107 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
108 -1, -1);
109 if (ret < 0) {
110 ERR("relay creating output directory");
111 goto end;
112 }
113
114 /*
115 * No need to use run_as API here because whatever we receive,
116 * the relayd uses its own credentials for the stream files.
117 */
118 stream->stream_fd = stream_fd_create(stream->path_name,
119 stream->channel_name, stream->tracefile_size, 0, NULL);
120 if (!stream->stream_fd) {
121 if (close(ret)) {
122 PERROR("Error closing file %d", ret);
123 }
124 ret = -1;
125 goto end;
126 }
127 stream->tfa = tracefile_array_create(stream->tracefile_count);
128 if (!stream->tfa) {
129 ret = -1;
130 goto end;
131 }
132 if (stream->tracefile_size) {
133 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
134 } else {
135 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
136 }
137
138 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) {
139 stream->is_metadata = 1;
140 }
141
142 stream->in_recv_list = true;
143
144 /*
145 * Add the stream in the recv list of the session. Once the end stream
146 * message is received, all session streams are published.
147 */
148 pthread_mutex_lock(&session->recv_list_lock);
149 cds_list_add_rcu(&stream->recv_node, &session->recv_list);
150 session->stream_count++;
151 pthread_mutex_unlock(&session->recv_list_lock);
152
153 /*
154 * Both in the ctf_trace object and the global stream ht since the data
155 * side of the relayd does not have the concept of session.
156 */
157 lttng_ht_add_unique_u64(relay_streams_ht, &stream->node);
158 stream->in_stream_ht = true;
159
160 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
161 stream->stream_handle);
162 ret = 0;
163
164 end:
165 if (ret) {
166 if (stream->stream_fd) {
167 stream_fd_put(stream->stream_fd);
168 stream->stream_fd = NULL;
169 }
170 stream_put(stream);
171 stream = NULL;
172 }
173 return stream;
174
175 error_no_alloc:
176 /*
177 * path_name and channel_name need to be freed explicitly here
178 * because we cannot rely on stream_put().
179 */
180 free(path_name);
181 free(channel_name);
182 return NULL;
183 }
184
185 /*
186 * Called with the session lock held.
187 */
188 void stream_publish(struct relay_stream *stream)
189 {
190 struct relay_session *session;
191
192 pthread_mutex_lock(&stream->lock);
193 if (stream->published) {
194 goto unlock;
195 }
196
197 session = stream->trace->session;
198
199 pthread_mutex_lock(&session->recv_list_lock);
200 if (stream->in_recv_list) {
201 cds_list_del_rcu(&stream->recv_node);
202 stream->in_recv_list = false;
203 }
204 pthread_mutex_unlock(&session->recv_list_lock);
205
206 pthread_mutex_lock(&stream->trace->stream_list_lock);
207 cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list);
208 pthread_mutex_unlock(&stream->trace->stream_list_lock);
209
210 stream->published = true;
211 unlock:
212 pthread_mutex_unlock(&stream->lock);
213 }
214
215 /*
216 * Stream must be protected by holding the stream lock or by virtue of being
217 * called from stream_destroy.
218 */
219 static void stream_unpublish(struct relay_stream *stream)
220 {
221 if (stream->in_stream_ht) {
222 struct lttng_ht_iter iter;
223 int ret;
224
225 iter.iter.node = &stream->node.node;
226 ret = lttng_ht_del(relay_streams_ht, &iter);
227 assert(!ret);
228 stream->in_stream_ht = false;
229 }
230 if (stream->published) {
231 pthread_mutex_lock(&stream->trace->stream_list_lock);
232 cds_list_del_rcu(&stream->stream_node);
233 pthread_mutex_unlock(&stream->trace->stream_list_lock);
234 stream->published = false;
235 }
236 }
237
238 static void stream_destroy(struct relay_stream *stream)
239 {
240 if (stream->indexes_ht) {
241 /*
242 * Calling lttng_ht_destroy in call_rcu worker thread so
243 * we don't hold the RCU read-side lock while calling
244 * it.
245 */
246 lttng_ht_destroy(stream->indexes_ht);
247 }
248 if (stream->tfa) {
249 tracefile_array_destroy(stream->tfa);
250 }
251 free(stream->path_name);
252 free(stream->channel_name);
253 free(stream);
254 }
255
256 static void stream_destroy_rcu(struct rcu_head *rcu_head)
257 {
258 struct relay_stream *stream =
259 caa_container_of(rcu_head, struct relay_stream, rcu_node);
260
261 stream_destroy(stream);
262 }
263
264 /*
265 * No need to take stream->lock since this is only called on the final
266 * stream_put which ensures that a single thread may act on the stream.
267 */
268 static void stream_release(struct urcu_ref *ref)
269 {
270 struct relay_stream *stream =
271 caa_container_of(ref, struct relay_stream, ref);
272 struct relay_session *session;
273
274 session = stream->trace->session;
275
276 DBG("Releasing stream id %" PRIu64, stream->stream_handle);
277
278 pthread_mutex_lock(&session->recv_list_lock);
279 session->stream_count--;
280 if (stream->in_recv_list) {
281 cds_list_del_rcu(&stream->recv_node);
282 stream->in_recv_list = false;
283 }
284 pthread_mutex_unlock(&session->recv_list_lock);
285
286 stream_unpublish(stream);
287
288 if (stream->stream_fd) {
289 stream_fd_put(stream->stream_fd);
290 stream->stream_fd = NULL;
291 }
292 if (stream->index_file) {
293 relay_index_file_put(stream->index_file);
294 stream->index_file = NULL;
295 }
296 if (stream->trace) {
297 ctf_trace_put(stream->trace);
298 stream->trace = NULL;
299 }
300
301 call_rcu(&stream->rcu_node, stream_destroy_rcu);
302 }
303
304 void stream_put(struct relay_stream *stream)
305 {
306 DBG("stream put for stream id %" PRIu64, stream->stream_handle);
307 rcu_read_lock();
308 assert(stream->ref.refcount != 0);
309 /*
310 * Wait until we have processed all the stream packets before
311 * actually putting our last stream reference.
312 */
313 DBG("stream put stream id %" PRIu64 " refcount %d",
314 stream->stream_handle,
315 (int) stream->ref.refcount);
316 urcu_ref_put(&stream->ref, stream_release);
317 rcu_read_unlock();
318 }
319
320 void try_stream_close(struct relay_stream *stream)
321 {
322 bool session_aborted;
323 struct relay_session *session = stream->trace->session;
324
325 DBG("Trying to close stream %" PRIu64, stream->stream_handle);
326
327 pthread_mutex_lock(&session->lock);
328 session_aborted = session->aborted;
329 pthread_mutex_unlock(&session->lock);
330
331 pthread_mutex_lock(&stream->lock);
332 /*
333 * Can be called concurently by connection close and reception of last
334 * pending data.
335 */
336 if (stream->closed) {
337 pthread_mutex_unlock(&stream->lock);
338 DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle);
339 return;
340 }
341
342 stream->close_requested = true;
343
344 if (stream->last_net_seq_num == -1ULL) {
345 /*
346 * Handle connection close without explicit stream close
347 * command.
348 *
349 * We can be clever about indexes partially received in
350 * cases where we received the data socket part, but not
351 * the control socket part: since we're currently closing
352 * the stream on behalf of the control socket, we *know*
353 * there won't be any more control information for this
354 * socket. Therefore, we can destroy all indexes for
355 * which we have received only the file descriptor (from
356 * data socket). This takes care of consumerd crashes
357 * between sending the data and control information for
358 * a packet. Since those are sent in that order, we take
359 * care of consumerd crashes.
360 */
361 DBG("relay_index_close_partial_fd");
362 relay_index_close_partial_fd(stream);
363 /*
364 * Use the highest net_seq_num we currently have pending
365 * As end of stream indicator. Leave last_net_seq_num
366 * at -1ULL if we cannot find any index.
367 */
368 stream->last_net_seq_num = relay_index_find_last(stream);
369 DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num);
370 /* Fall-through into the next check. */
371 }
372
373 if (stream->last_net_seq_num != -1ULL &&
374 ((int64_t) (stream->prev_seq - stream->last_net_seq_num)) < 0
375 && !session_aborted) {
376 /*
377 * Don't close since we still have data pending. This
378 * handles cases where an explicit close command has
379 * been received for this stream, and cases where the
380 * connection has been closed, and we are awaiting for
381 * index information from the data socket. It is
382 * therefore expected that all the index fd information
383 * we need has already been received on the control
384 * socket. Matching index information from data socket
385 * should be Expected Soon(TM).
386 *
387 * TODO: We should implement a timer to garbage collect
388 * streams after a timeout to be resilient against a
389 * consumerd implementation that would not match this
390 * expected behavior.
391 */
392 pthread_mutex_unlock(&stream->lock);
393 DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle);
394 return;
395 }
396 /*
397 * We received all the indexes we can expect.
398 */
399 stream_unpublish(stream);
400 stream->closed = true;
401 /* Relay indexes are only used by the "consumer/sessiond" end. */
402 relay_index_close_all(stream);
403 pthread_mutex_unlock(&stream->lock);
404 DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle);
405 stream_put(stream);
406 }
407
408 static void print_stream_indexes(struct relay_stream *stream)
409 {
410 struct lttng_ht_iter iter;
411 struct relay_index *index;
412
413 rcu_read_lock();
414 cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index,
415 index_n.node) {
416 DBG("index %p net_seq_num %" PRIu64 " refcount %ld"
417 " stream %" PRIu64 " trace %" PRIu64
418 " session %" PRIu64,
419 index,
420 index->index_n.key,
421 stream->ref.refcount,
422 index->stream->stream_handle,
423 index->stream->trace->id,
424 index->stream->trace->session->id);
425 }
426 rcu_read_unlock();
427 }
428
429 void print_relay_streams(void)
430 {
431 struct lttng_ht_iter iter;
432 struct relay_stream *stream;
433
434 if (!relay_streams_ht) {
435 return;
436 }
437
438 rcu_read_lock();
439 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
440 node.node) {
441 if (!stream_get(stream)) {
442 continue;
443 }
444 DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
445 " session %" PRIu64,
446 stream,
447 stream->ref.refcount,
448 stream->stream_handle,
449 stream->trace->id,
450 stream->trace->session->id);
451 print_stream_indexes(stream);
452 stream_put(stream);
453 }
454 rcu_read_unlock();
455 }
This page took 0.04129 seconds and 5 git commands to generate.