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