63c98350a4772435b50a84ae4b0ac25f4e4f3455
[lttng-tools.git] / src / common / consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as 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
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <assert.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <inttypes.h>
31
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
41
42 #include "consumer.h"
43
44 struct lttng_consumer_global_data consumer_data = {
45 .stream_count = 0,
46 .need_update = 1,
47 .type = LTTNG_CONSUMER_UNKNOWN,
48 };
49
50 /*
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 * polling threads.
55 */
56 volatile int consumer_quit;
57
58 /*
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
62 */
63 static struct lttng_ht *metadata_ht;
64 static struct lttng_ht *data_ht;
65
66 /*
67 * This hash table contains the mapping between the session id of the sessiond
68 * and the relayd session id. Element of the ht are indexed by sessiond session
69 * id.
70 *
71 * Node can be added when a relayd communication is opened in the sessiond
72 * thread.
73 *
74 * Note that a session id of the session daemon is unique to a tracing session
75 * and not to a domain session. However, a domain session has one consumer
76 * which forces the 1-1 mapping between a consumer and a domain session (ex:
77 * UST). This means that we can't have duplicate in this ht.
78 */
79 static struct lttng_ht *relayd_session_id_ht;
80
81 /*
82 * Notify a thread pipe to poll back again. This usually means that some global
83 * state has changed so we just send back the thread in a poll wait call.
84 */
85 static void notify_thread_pipe(int wpipe)
86 {
87 int ret;
88
89 do {
90 struct lttng_consumer_stream *null_stream = NULL;
91
92 ret = write(wpipe, &null_stream, sizeof(null_stream));
93 } while (ret < 0 && errno == EINTR);
94 }
95
96 /*
97 * Find a stream. The consumer_data.lock must be locked during this
98 * call.
99 */
100 static struct lttng_consumer_stream *consumer_find_stream(int key,
101 struct lttng_ht *ht)
102 {
103 struct lttng_ht_iter iter;
104 struct lttng_ht_node_ulong *node;
105 struct lttng_consumer_stream *stream = NULL;
106
107 assert(ht);
108
109 /* Negative keys are lookup failures */
110 if (key < 0) {
111 return NULL;
112 }
113
114 rcu_read_lock();
115
116 lttng_ht_lookup(ht, (void *)((unsigned long) key), &iter);
117 node = lttng_ht_iter_get_node_ulong(&iter);
118 if (node != NULL) {
119 stream = caa_container_of(node, struct lttng_consumer_stream, node);
120 }
121
122 rcu_read_unlock();
123
124 return stream;
125 }
126
127 void consumer_steal_stream_key(int key, struct lttng_ht *ht)
128 {
129 struct lttng_consumer_stream *stream;
130
131 rcu_read_lock();
132 stream = consumer_find_stream(key, ht);
133 if (stream) {
134 stream->key = -1;
135 /*
136 * We don't want the lookup to match, but we still need
137 * to iterate on this stream when iterating over the hash table. Just
138 * change the node key.
139 */
140 stream->node.key = -1;
141 }
142 rcu_read_unlock();
143 }
144
145 /*
146 * Return a channel object for the given key.
147 *
148 * RCU read side lock MUST be acquired before calling this function and
149 * protects the channel ptr.
150 */
151 static struct lttng_consumer_channel *consumer_find_channel(int key)
152 {
153 struct lttng_ht_iter iter;
154 struct lttng_ht_node_ulong *node;
155 struct lttng_consumer_channel *channel = NULL;
156
157 /* Negative keys are lookup failures */
158 if (key < 0) {
159 return NULL;
160 }
161
162 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
163 &iter);
164 node = lttng_ht_iter_get_node_ulong(&iter);
165 if (node != NULL) {
166 channel = caa_container_of(node, struct lttng_consumer_channel, node);
167 }
168
169 return channel;
170 }
171
172 static void consumer_steal_channel_key(int key)
173 {
174 struct lttng_consumer_channel *channel;
175
176 rcu_read_lock();
177 channel = consumer_find_channel(key);
178 if (channel) {
179 channel->key = -1;
180 /*
181 * We don't want the lookup to match, but we still need
182 * to iterate on this channel when iterating over the hash table. Just
183 * change the node key.
184 */
185 channel->node.key = -1;
186 }
187 rcu_read_unlock();
188 }
189
190 static
191 void consumer_free_stream(struct rcu_head *head)
192 {
193 struct lttng_ht_node_ulong *node =
194 caa_container_of(head, struct lttng_ht_node_ulong, head);
195 struct lttng_consumer_stream *stream =
196 caa_container_of(node, struct lttng_consumer_stream, node);
197
198 free(stream);
199 }
200
201 /*
202 * RCU protected relayd socket pair free.
203 */
204 static void consumer_rcu_free_relayd(struct rcu_head *head)
205 {
206 struct lttng_ht_node_ulong *node =
207 caa_container_of(head, struct lttng_ht_node_ulong, head);
208 struct consumer_relayd_sock_pair *relayd =
209 caa_container_of(node, struct consumer_relayd_sock_pair, node);
210
211 /*
212 * Close all sockets. This is done in the call RCU since we don't want the
213 * socket fds to be reassigned thus potentially creating bad state of the
214 * relayd object.
215 *
216 * We do not have to lock the control socket mutex here since at this stage
217 * there is no one referencing to this relayd object.
218 */
219 (void) relayd_close(&relayd->control_sock);
220 (void) relayd_close(&relayd->data_sock);
221
222 free(relayd);
223 }
224
225 /*
226 * Destroy and free relayd socket pair object.
227 *
228 * This function MUST be called with the consumer_data lock acquired.
229 */
230 static void destroy_relayd(struct consumer_relayd_sock_pair *relayd)
231 {
232 int ret;
233 struct lttng_ht_iter iter;
234 struct lttng_ht_node_ulong *node;
235
236 if (relayd == NULL) {
237 return;
238 }
239
240 DBG("Consumer destroy and close relayd socket pair");
241
242 /* Loockup for a relayd node in the session id map hash table. */
243 lttng_ht_lookup(relayd_session_id_ht,
244 (void *)((unsigned long) relayd->sessiond_session_id), &iter);
245 node = lttng_ht_iter_get_node_ulong(&iter);
246 if (node == NULL) {
247 /* We assume the relayd is being or is destroyed */
248 return;
249 }
250
251 /*
252 * Try to delete it from the relayd session id ht. The return value is of
253 * no importance since either way we are going to try to delete the relayd
254 * from the global relayd_ht.
255 */
256 lttng_ht_del(relayd_session_id_ht, &iter);
257
258 iter.iter.node = &relayd->node.node;
259 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
260 if (ret != 0) {
261 /* We assume the relayd is being or is destroyed */
262 return;
263 }
264
265 /* RCU free() call */
266 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
267 }
268
269 /*
270 * Iterate over the relayd hash table and destroy each element. Finally,
271 * destroy the whole hash table.
272 */
273 static void cleanup_relayd_ht(void)
274 {
275 struct lttng_ht_iter iter;
276 struct consumer_relayd_sock_pair *relayd;
277
278 rcu_read_lock();
279
280 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
281 node.node) {
282 destroy_relayd(relayd);
283 }
284
285 lttng_ht_destroy(consumer_data.relayd_ht);
286 /* The destroy_relayd call makes sure that this ht is empty here. */
287 lttng_ht_destroy(relayd_session_id_ht);
288
289 rcu_read_unlock();
290 }
291
292 /*
293 * Update the end point status of all streams having the given network sequence
294 * index (relayd index).
295 *
296 * It's atomically set without having the stream mutex locked which is fine
297 * because we handle the write/read race with a pipe wakeup for each thread.
298 */
299 static void update_endpoint_status_by_netidx(int net_seq_idx,
300 enum consumer_endpoint_status status)
301 {
302 struct lttng_ht_iter iter;
303 struct lttng_consumer_stream *stream;
304
305 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx);
306
307 rcu_read_lock();
308
309 /* Let's begin with metadata */
310 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
311 if (stream->net_seq_idx == net_seq_idx) {
312 uatomic_set(&stream->endpoint_status, status);
313 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
314 }
315 }
316
317 /* Follow up by the data streams */
318 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
319 if (stream->net_seq_idx == net_seq_idx) {
320 uatomic_set(&stream->endpoint_status, status);
321 DBG("Delete flag set to data stream %d", stream->wait_fd);
322 }
323 }
324 rcu_read_unlock();
325 }
326
327 /*
328 * Cleanup a relayd object by flagging every associated streams for deletion,
329 * destroying the object meaning removing it from the relayd hash table,
330 * closing the sockets and freeing the memory in a RCU call.
331 *
332 * If a local data context is available, notify the threads that the streams'
333 * state have changed.
334 */
335 static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd,
336 struct lttng_consumer_local_data *ctx)
337 {
338 int netidx;
339
340 assert(relayd);
341
342 DBG("Cleaning up relayd sockets");
343
344 /* Save the net sequence index before destroying the object */
345 netidx = relayd->net_seq_idx;
346
347 /*
348 * Delete the relayd from the relayd hash table, close the sockets and free
349 * the object in a RCU call.
350 */
351 destroy_relayd(relayd);
352
353 /* Set inactive endpoint to all streams */
354 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
355
356 /*
357 * With a local data context, notify the threads that the streams' state
358 * have changed. The write() action on the pipe acts as an "implicit"
359 * memory barrier ordering the updates of the end point status from the
360 * read of this status which happens AFTER receiving this notify.
361 */
362 if (ctx) {
363 notify_thread_pipe(ctx->consumer_data_pipe[1]);
364 notify_thread_pipe(ctx->consumer_metadata_pipe[1]);
365 }
366 }
367
368 /*
369 * Flag a relayd socket pair for destruction. Destroy it if the refcount
370 * reaches zero.
371 *
372 * RCU read side lock MUST be aquired before calling this function.
373 */
374 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
375 {
376 assert(relayd);
377
378 /* Set destroy flag for this object */
379 uatomic_set(&relayd->destroy_flag, 1);
380
381 /* Destroy the relayd if refcount is 0 */
382 if (uatomic_read(&relayd->refcount) == 0) {
383 destroy_relayd(relayd);
384 }
385 }
386
387 /*
388 * Remove a stream from the global list protected by a mutex. This
389 * function is also responsible for freeing its data structures.
390 */
391 void consumer_del_stream(struct lttng_consumer_stream *stream,
392 struct lttng_ht *ht)
393 {
394 int ret;
395 struct lttng_ht_iter iter;
396 struct lttng_consumer_channel *free_chan = NULL;
397 struct consumer_relayd_sock_pair *relayd;
398
399 assert(stream);
400
401 DBG("Consumer del stream %d", stream->wait_fd);
402
403 if (ht == NULL) {
404 /* Means the stream was allocated but not successfully added */
405 goto free_stream;
406 }
407
408 pthread_mutex_lock(&consumer_data.lock);
409 pthread_mutex_lock(&stream->lock);
410
411 switch (consumer_data.type) {
412 case LTTNG_CONSUMER_KERNEL:
413 if (stream->mmap_base != NULL) {
414 ret = munmap(stream->mmap_base, stream->mmap_len);
415 if (ret != 0) {
416 PERROR("munmap");
417 }
418 }
419 break;
420 case LTTNG_CONSUMER32_UST:
421 case LTTNG_CONSUMER64_UST:
422 lttng_ustconsumer_del_stream(stream);
423 break;
424 default:
425 ERR("Unknown consumer_data type");
426 assert(0);
427 goto end;
428 }
429
430 rcu_read_lock();
431 iter.iter.node = &stream->node.node;
432 ret = lttng_ht_del(ht, &iter);
433 assert(!ret);
434
435 /* Remove node session id from the consumer_data stream ht */
436 iter.iter.node = &stream->node_session_id.node;
437 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
438 assert(!ret);
439 rcu_read_unlock();
440
441 assert(consumer_data.stream_count > 0);
442 consumer_data.stream_count--;
443
444 if (stream->out_fd >= 0) {
445 ret = close(stream->out_fd);
446 if (ret) {
447 PERROR("close");
448 }
449 }
450 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
451 ret = close(stream->wait_fd);
452 if (ret) {
453 PERROR("close");
454 }
455 }
456 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
457 ret = close(stream->shm_fd);
458 if (ret) {
459 PERROR("close");
460 }
461 }
462
463 /* Check and cleanup relayd */
464 rcu_read_lock();
465 relayd = consumer_find_relayd(stream->net_seq_idx);
466 if (relayd != NULL) {
467 uatomic_dec(&relayd->refcount);
468 assert(uatomic_read(&relayd->refcount) >= 0);
469
470 /* Closing streams requires to lock the control socket. */
471 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
472 ret = relayd_send_close_stream(&relayd->control_sock,
473 stream->relayd_stream_id,
474 stream->next_net_seq_num - 1);
475 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
476 if (ret < 0) {
477 DBG("Unable to close stream on the relayd. Continuing");
478 /*
479 * Continue here. There is nothing we can do for the relayd.
480 * Chances are that the relayd has closed the socket so we just
481 * continue cleaning up.
482 */
483 }
484
485 /* Both conditions are met, we destroy the relayd. */
486 if (uatomic_read(&relayd->refcount) == 0 &&
487 uatomic_read(&relayd->destroy_flag)) {
488 destroy_relayd(relayd);
489 }
490 }
491 rcu_read_unlock();
492
493 uatomic_dec(&stream->chan->refcount);
494 if (!uatomic_read(&stream->chan->refcount)
495 && !uatomic_read(&stream->chan->nb_init_streams)) {
496 free_chan = stream->chan;
497 }
498
499 end:
500 consumer_data.need_update = 1;
501 pthread_mutex_unlock(&stream->lock);
502 pthread_mutex_unlock(&consumer_data.lock);
503
504 if (free_chan) {
505 consumer_del_channel(free_chan);
506 }
507
508 free_stream:
509 call_rcu(&stream->node.head, consumer_free_stream);
510 }
511
512 struct lttng_consumer_stream *consumer_allocate_stream(
513 int channel_key, int stream_key,
514 int shm_fd, int wait_fd,
515 enum lttng_consumer_stream_state state,
516 uint64_t mmap_len,
517 enum lttng_event_output output,
518 const char *path_name,
519 uid_t uid,
520 gid_t gid,
521 int net_index,
522 int metadata_flag,
523 uint64_t session_id,
524 int *alloc_ret)
525 {
526 struct lttng_consumer_stream *stream;
527
528 stream = zmalloc(sizeof(*stream));
529 if (stream == NULL) {
530 PERROR("malloc struct lttng_consumer_stream");
531 *alloc_ret = -ENOMEM;
532 goto end;
533 }
534
535 rcu_read_lock();
536
537 /*
538 * Get stream's channel reference. Needed when adding the stream to the
539 * global hash table.
540 */
541 stream->chan = consumer_find_channel(channel_key);
542 if (!stream->chan) {
543 *alloc_ret = -ENOENT;
544 ERR("Unable to find channel for stream %d", stream_key);
545 goto error;
546 }
547
548 stream->key = stream_key;
549 stream->shm_fd = shm_fd;
550 stream->wait_fd = wait_fd;
551 stream->out_fd = -1;
552 stream->out_fd_offset = 0;
553 stream->state = state;
554 stream->mmap_len = mmap_len;
555 stream->mmap_base = NULL;
556 stream->output = output;
557 stream->uid = uid;
558 stream->gid = gid;
559 stream->net_seq_idx = net_index;
560 stream->metadata_flag = metadata_flag;
561 stream->session_id = session_id;
562 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
563 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
564 pthread_mutex_init(&stream->lock, NULL);
565
566 /*
567 * Index differently the metadata node because the thread is using an
568 * internal hash table to match streams in the metadata_ht to the epoll set
569 * file descriptor.
570 */
571 if (metadata_flag) {
572 lttng_ht_node_init_ulong(&stream->node, stream->wait_fd);
573 } else {
574 lttng_ht_node_init_ulong(&stream->node, stream->key);
575 }
576
577 /* Init session id node with the stream session id */
578 lttng_ht_node_init_ulong(&stream->node_session_id, stream->session_id);
579
580 /*
581 * The cpu number is needed before using any ustctl_* actions. Ignored for
582 * the kernel so the value does not matter.
583 */
584 pthread_mutex_lock(&consumer_data.lock);
585 stream->cpu = stream->chan->cpucount++;
586 pthread_mutex_unlock(&consumer_data.lock);
587
588 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
589 " out_fd %d, net_seq_idx %d, session_id %" PRIu64,
590 stream->path_name, stream->key, stream->shm_fd, stream->wait_fd,
591 (unsigned long long) stream->mmap_len, stream->out_fd,
592 stream->net_seq_idx, stream->session_id);
593
594 rcu_read_unlock();
595 return stream;
596
597 error:
598 rcu_read_unlock();
599 free(stream);
600 end:
601 return NULL;
602 }
603
604 /*
605 * Add a stream to the global list protected by a mutex.
606 */
607 static int consumer_add_stream(struct lttng_consumer_stream *stream,
608 struct lttng_ht *ht)
609 {
610 int ret = 0;
611 struct consumer_relayd_sock_pair *relayd;
612
613 assert(stream);
614 assert(ht);
615
616 DBG3("Adding consumer stream %d", stream->key);
617
618 pthread_mutex_lock(&consumer_data.lock);
619 pthread_mutex_lock(&stream->lock);
620 rcu_read_lock();
621
622 /* Steal stream identifier to avoid having streams with the same key */
623 consumer_steal_stream_key(stream->key, ht);
624
625 lttng_ht_add_unique_ulong(ht, &stream->node);
626
627 /*
628 * Add stream to the stream_list_ht of the consumer data. No need to steal
629 * the key since the HT does not use it and we allow to add redundant keys
630 * into this table.
631 */
632 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
633
634 /* Check and cleanup relayd */
635 relayd = consumer_find_relayd(stream->net_seq_idx);
636 if (relayd != NULL) {
637 uatomic_inc(&relayd->refcount);
638 }
639
640 /* Update channel refcount once added without error(s). */
641 uatomic_inc(&stream->chan->refcount);
642
643 /*
644 * When nb_init_streams reaches 0, we don't need to trigger any action in
645 * terms of destroying the associated channel, because the action that
646 * causes the count to become 0 also causes a stream to be added. The
647 * channel deletion will thus be triggered by the following removal of this
648 * stream.
649 */
650 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
651 uatomic_dec(&stream->chan->nb_init_streams);
652 }
653
654 /* Update consumer data once the node is inserted. */
655 consumer_data.stream_count++;
656 consumer_data.need_update = 1;
657
658 rcu_read_unlock();
659 pthread_mutex_unlock(&stream->lock);
660 pthread_mutex_unlock(&consumer_data.lock);
661
662 return ret;
663 }
664
665 /*
666 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
667 * be acquired before calling this.
668 */
669 static int add_relayd(struct consumer_relayd_sock_pair *relayd)
670 {
671 int ret = 0;
672 struct lttng_ht_node_ulong *node;
673 struct lttng_ht_iter iter;
674
675 if (relayd == NULL) {
676 ret = -1;
677 goto end;
678 }
679
680 lttng_ht_lookup(consumer_data.relayd_ht,
681 (void *)((unsigned long) relayd->net_seq_idx), &iter);
682 node = lttng_ht_iter_get_node_ulong(&iter);
683 if (node != NULL) {
684 /* Relayd already exist. Ignore the insertion */
685 goto end;
686 }
687 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
688
689 end:
690 return ret;
691 }
692
693 /*
694 * Allocate and return a consumer relayd socket.
695 */
696 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
697 int net_seq_idx)
698 {
699 struct consumer_relayd_sock_pair *obj = NULL;
700
701 /* Negative net sequence index is a failure */
702 if (net_seq_idx < 0) {
703 goto error;
704 }
705
706 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
707 if (obj == NULL) {
708 PERROR("zmalloc relayd sock");
709 goto error;
710 }
711
712 obj->net_seq_idx = net_seq_idx;
713 obj->refcount = 0;
714 obj->destroy_flag = 0;
715 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
716 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
717
718 error:
719 return obj;
720 }
721
722 /*
723 * Find a relayd socket pair in the global consumer data.
724 *
725 * Return the object if found else NULL.
726 * RCU read-side lock must be held across this call and while using the
727 * returned object.
728 */
729 struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
730 {
731 struct lttng_ht_iter iter;
732 struct lttng_ht_node_ulong *node;
733 struct consumer_relayd_sock_pair *relayd = NULL;
734
735 /* Negative keys are lookup failures */
736 if (key < 0) {
737 goto error;
738 }
739
740 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
741 &iter);
742 node = lttng_ht_iter_get_node_ulong(&iter);
743 if (node != NULL) {
744 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
745 }
746
747 error:
748 return relayd;
749 }
750
751 /*
752 * Handle stream for relayd transmission if the stream applies for network
753 * streaming where the net sequence index is set.
754 *
755 * Return destination file descriptor or negative value on error.
756 */
757 static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
758 size_t data_size, unsigned long padding,
759 struct consumer_relayd_sock_pair *relayd)
760 {
761 int outfd = -1, ret;
762 struct lttcomm_relayd_data_hdr data_hdr;
763
764 /* Safety net */
765 assert(stream);
766 assert(relayd);
767
768 /* Reset data header */
769 memset(&data_hdr, 0, sizeof(data_hdr));
770
771 if (stream->metadata_flag) {
772 /* Caller MUST acquire the relayd control socket lock */
773 ret = relayd_send_metadata(&relayd->control_sock, data_size);
774 if (ret < 0) {
775 goto error;
776 }
777
778 /* Metadata are always sent on the control socket. */
779 outfd = relayd->control_sock.fd;
780 } else {
781 /* Set header with stream information */
782 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
783 data_hdr.data_size = htobe32(data_size);
784 data_hdr.padding_size = htobe32(padding);
785 /*
786 * Note that net_seq_num below is assigned with the *current* value of
787 * next_net_seq_num and only after that the next_net_seq_num will be
788 * increment. This is why when issuing a command on the relayd using
789 * this next value, 1 should always be substracted in order to compare
790 * the last seen sequence number on the relayd side to the last sent.
791 */
792 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
793 /* Other fields are zeroed previously */
794
795 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
796 sizeof(data_hdr));
797 if (ret < 0) {
798 goto error;
799 }
800
801 /* Set to go on data socket */
802 outfd = relayd->data_sock.fd;
803 }
804
805 error:
806 return outfd;
807 }
808
809 static
810 void consumer_free_channel(struct rcu_head *head)
811 {
812 struct lttng_ht_node_ulong *node =
813 caa_container_of(head, struct lttng_ht_node_ulong, head);
814 struct lttng_consumer_channel *channel =
815 caa_container_of(node, struct lttng_consumer_channel, node);
816
817 free(channel);
818 }
819
820 /*
821 * Remove a channel from the global list protected by a mutex. This
822 * function is also responsible for freeing its data structures.
823 */
824 void consumer_del_channel(struct lttng_consumer_channel *channel)
825 {
826 int ret;
827 struct lttng_ht_iter iter;
828
829 DBG("Consumer delete channel key %d", channel->key);
830
831 pthread_mutex_lock(&consumer_data.lock);
832
833 switch (consumer_data.type) {
834 case LTTNG_CONSUMER_KERNEL:
835 break;
836 case LTTNG_CONSUMER32_UST:
837 case LTTNG_CONSUMER64_UST:
838 lttng_ustconsumer_del_channel(channel);
839 break;
840 default:
841 ERR("Unknown consumer_data type");
842 assert(0);
843 goto end;
844 }
845
846 rcu_read_lock();
847 iter.iter.node = &channel->node.node;
848 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
849 assert(!ret);
850 rcu_read_unlock();
851
852 if (channel->mmap_base != NULL) {
853 ret = munmap(channel->mmap_base, channel->mmap_len);
854 if (ret != 0) {
855 PERROR("munmap");
856 }
857 }
858 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
859 ret = close(channel->wait_fd);
860 if (ret) {
861 PERROR("close");
862 }
863 }
864 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
865 ret = close(channel->shm_fd);
866 if (ret) {
867 PERROR("close");
868 }
869 }
870
871 call_rcu(&channel->node.head, consumer_free_channel);
872 end:
873 pthread_mutex_unlock(&consumer_data.lock);
874 }
875
876 struct lttng_consumer_channel *consumer_allocate_channel(
877 int channel_key,
878 int shm_fd, int wait_fd,
879 uint64_t mmap_len,
880 uint64_t max_sb_size,
881 unsigned int nb_init_streams)
882 {
883 struct lttng_consumer_channel *channel;
884 int ret;
885
886 channel = zmalloc(sizeof(*channel));
887 if (channel == NULL) {
888 PERROR("malloc struct lttng_consumer_channel");
889 goto end;
890 }
891 channel->key = channel_key;
892 channel->shm_fd = shm_fd;
893 channel->wait_fd = wait_fd;
894 channel->mmap_len = mmap_len;
895 channel->max_sb_size = max_sb_size;
896 channel->refcount = 0;
897 channel->nb_init_streams = nb_init_streams;
898 lttng_ht_node_init_ulong(&channel->node, channel->key);
899
900 switch (consumer_data.type) {
901 case LTTNG_CONSUMER_KERNEL:
902 channel->mmap_base = NULL;
903 channel->mmap_len = 0;
904 break;
905 case LTTNG_CONSUMER32_UST:
906 case LTTNG_CONSUMER64_UST:
907 ret = lttng_ustconsumer_allocate_channel(channel);
908 if (ret) {
909 free(channel);
910 return NULL;
911 }
912 break;
913 default:
914 ERR("Unknown consumer_data type");
915 assert(0);
916 goto end;
917 }
918 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
919 channel->key, channel->shm_fd, channel->wait_fd,
920 (unsigned long long) channel->mmap_len,
921 (unsigned long long) channel->max_sb_size);
922 end:
923 return channel;
924 }
925
926 /*
927 * Add a channel to the global list protected by a mutex.
928 */
929 int consumer_add_channel(struct lttng_consumer_channel *channel)
930 {
931 struct lttng_ht_node_ulong *node;
932 struct lttng_ht_iter iter;
933
934 pthread_mutex_lock(&consumer_data.lock);
935 /* Steal channel identifier, for UST */
936 consumer_steal_channel_key(channel->key);
937 rcu_read_lock();
938
939 lttng_ht_lookup(consumer_data.channel_ht,
940 (void *)((unsigned long) channel->key), &iter);
941 node = lttng_ht_iter_get_node_ulong(&iter);
942 if (node != NULL) {
943 /* Channel already exist. Ignore the insertion */
944 goto end;
945 }
946
947 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
948
949 end:
950 rcu_read_unlock();
951 pthread_mutex_unlock(&consumer_data.lock);
952
953 return 0;
954 }
955
956 /*
957 * Allocate the pollfd structure and the local view of the out fds to avoid
958 * doing a lookup in the linked list and concurrency issues when writing is
959 * needed. Called with consumer_data.lock held.
960 *
961 * Returns the number of fds in the structures.
962 */
963 static int consumer_update_poll_array(
964 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
965 struct lttng_consumer_stream **local_stream, struct lttng_ht *ht)
966 {
967 int i = 0;
968 struct lttng_ht_iter iter;
969 struct lttng_consumer_stream *stream;
970
971 DBG("Updating poll fd array");
972 rcu_read_lock();
973 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
974 /*
975 * Only active streams with an active end point can be added to the
976 * poll set and local stream storage of the thread.
977 *
978 * There is a potential race here for endpoint_status to be updated
979 * just after the check. However, this is OK since the stream(s) will
980 * be deleted once the thread is notified that the end point state has
981 * changed where this function will be called back again.
982 */
983 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
984 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
985 continue;
986 }
987 DBG("Active FD %d", stream->wait_fd);
988 (*pollfd)[i].fd = stream->wait_fd;
989 (*pollfd)[i].events = POLLIN | POLLPRI;
990 local_stream[i] = stream;
991 i++;
992 }
993 rcu_read_unlock();
994
995 /*
996 * Insert the consumer_data_pipe at the end of the array and don't
997 * increment i so nb_fd is the number of real FD.
998 */
999 (*pollfd)[i].fd = ctx->consumer_data_pipe[0];
1000 (*pollfd)[i].events = POLLIN | POLLPRI;
1001 return i;
1002 }
1003
1004 /*
1005 * Poll on the should_quit pipe and the command socket return -1 on error and
1006 * should exit, 0 if data is available on the command socket
1007 */
1008 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1009 {
1010 int num_rdy;
1011
1012 restart:
1013 num_rdy = poll(consumer_sockpoll, 2, -1);
1014 if (num_rdy == -1) {
1015 /*
1016 * Restart interrupted system call.
1017 */
1018 if (errno == EINTR) {
1019 goto restart;
1020 }
1021 PERROR("Poll error");
1022 goto exit;
1023 }
1024 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
1025 DBG("consumer_should_quit wake up");
1026 goto exit;
1027 }
1028 return 0;
1029
1030 exit:
1031 return -1;
1032 }
1033
1034 /*
1035 * Set the error socket.
1036 */
1037 void lttng_consumer_set_error_sock(
1038 struct lttng_consumer_local_data *ctx, int sock)
1039 {
1040 ctx->consumer_error_socket = sock;
1041 }
1042
1043 /*
1044 * Set the command socket path.
1045 */
1046 void lttng_consumer_set_command_sock_path(
1047 struct lttng_consumer_local_data *ctx, char *sock)
1048 {
1049 ctx->consumer_command_sock_path = sock;
1050 }
1051
1052 /*
1053 * Send return code to the session daemon.
1054 * If the socket is not defined, we return 0, it is not a fatal error
1055 */
1056 int lttng_consumer_send_error(
1057 struct lttng_consumer_local_data *ctx, int cmd)
1058 {
1059 if (ctx->consumer_error_socket > 0) {
1060 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1061 sizeof(enum lttcomm_sessiond_command));
1062 }
1063
1064 return 0;
1065 }
1066
1067 /*
1068 * Close all the tracefiles and stream fds and MUST be called when all
1069 * instances are destroyed i.e. when all threads were joined and are ended.
1070 */
1071 void lttng_consumer_cleanup(void)
1072 {
1073 struct lttng_ht_iter iter;
1074 struct lttng_ht_node_ulong *node;
1075
1076 rcu_read_lock();
1077
1078 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
1079 node) {
1080 struct lttng_consumer_channel *channel =
1081 caa_container_of(node, struct lttng_consumer_channel, node);
1082 consumer_del_channel(channel);
1083 }
1084
1085 rcu_read_unlock();
1086
1087 lttng_ht_destroy(consumer_data.channel_ht);
1088
1089 cleanup_relayd_ht();
1090
1091 /*
1092 * This HT contains streams that are freed by either the metadata thread or
1093 * the data thread so we do *nothing* on the hash table and simply destroy
1094 * it.
1095 */
1096 lttng_ht_destroy(consumer_data.stream_list_ht);
1097 }
1098
1099 /*
1100 * Called from signal handler.
1101 */
1102 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1103 {
1104 int ret;
1105 consumer_quit = 1;
1106 do {
1107 ret = write(ctx->consumer_should_quit[1], "4", 1);
1108 } while (ret < 0 && errno == EINTR);
1109 if (ret < 0 || ret != 1) {
1110 PERROR("write consumer quit");
1111 }
1112
1113 DBG("Consumer flag that it should quit");
1114 }
1115
1116 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1117 off_t orig_offset)
1118 {
1119 int outfd = stream->out_fd;
1120
1121 /*
1122 * This does a blocking write-and-wait on any page that belongs to the
1123 * subbuffer prior to the one we just wrote.
1124 * Don't care about error values, as these are just hints and ways to
1125 * limit the amount of page cache used.
1126 */
1127 if (orig_offset < stream->chan->max_sb_size) {
1128 return;
1129 }
1130 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
1131 stream->chan->max_sb_size,
1132 SYNC_FILE_RANGE_WAIT_BEFORE
1133 | SYNC_FILE_RANGE_WRITE
1134 | SYNC_FILE_RANGE_WAIT_AFTER);
1135 /*
1136 * Give hints to the kernel about how we access the file:
1137 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1138 * we write it.
1139 *
1140 * We need to call fadvise again after the file grows because the
1141 * kernel does not seem to apply fadvise to non-existing parts of the
1142 * file.
1143 *
1144 * Call fadvise _after_ having waited for the page writeback to
1145 * complete because the dirty page writeback semantic is not well
1146 * defined. So it can be expected to lead to lower throughput in
1147 * streaming.
1148 */
1149 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
1150 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
1151 }
1152
1153 /*
1154 * Initialise the necessary environnement :
1155 * - create a new context
1156 * - create the poll_pipe
1157 * - create the should_quit pipe (for signal handler)
1158 * - create the thread pipe (for splice)
1159 *
1160 * Takes a function pointer as argument, this function is called when data is
1161 * available on a buffer. This function is responsible to do the
1162 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1163 * buffer configuration and then kernctl_put_next_subbuf at the end.
1164 *
1165 * Returns a pointer to the new context or NULL on error.
1166 */
1167 struct lttng_consumer_local_data *lttng_consumer_create(
1168 enum lttng_consumer_type type,
1169 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
1170 struct lttng_consumer_local_data *ctx),
1171 int (*recv_channel)(struct lttng_consumer_channel *channel),
1172 int (*recv_stream)(struct lttng_consumer_stream *stream),
1173 int (*update_stream)(int stream_key, uint32_t state))
1174 {
1175 int ret, i;
1176 struct lttng_consumer_local_data *ctx;
1177
1178 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1179 consumer_data.type == type);
1180 consumer_data.type = type;
1181
1182 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
1183 if (ctx == NULL) {
1184 PERROR("allocating context");
1185 goto error;
1186 }
1187
1188 ctx->consumer_error_socket = -1;
1189 /* assign the callbacks */
1190 ctx->on_buffer_ready = buffer_ready;
1191 ctx->on_recv_channel = recv_channel;
1192 ctx->on_recv_stream = recv_stream;
1193 ctx->on_update_stream = update_stream;
1194
1195 ret = pipe(ctx->consumer_data_pipe);
1196 if (ret < 0) {
1197 PERROR("Error creating poll pipe");
1198 goto error_poll_pipe;
1199 }
1200
1201 /* set read end of the pipe to non-blocking */
1202 ret = fcntl(ctx->consumer_data_pipe[0], F_SETFL, O_NONBLOCK);
1203 if (ret < 0) {
1204 PERROR("fcntl O_NONBLOCK");
1205 goto error_poll_fcntl;
1206 }
1207
1208 /* set write end of the pipe to non-blocking */
1209 ret = fcntl(ctx->consumer_data_pipe[1], F_SETFL, O_NONBLOCK);
1210 if (ret < 0) {
1211 PERROR("fcntl O_NONBLOCK");
1212 goto error_poll_fcntl;
1213 }
1214
1215 ret = pipe(ctx->consumer_should_quit);
1216 if (ret < 0) {
1217 PERROR("Error creating recv pipe");
1218 goto error_quit_pipe;
1219 }
1220
1221 ret = pipe(ctx->consumer_thread_pipe);
1222 if (ret < 0) {
1223 PERROR("Error creating thread pipe");
1224 goto error_thread_pipe;
1225 }
1226
1227 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1228 if (ret < 0) {
1229 goto error_metadata_pipe;
1230 }
1231
1232 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1233 if (ret < 0) {
1234 goto error_splice_pipe;
1235 }
1236
1237 return ctx;
1238
1239 error_splice_pipe:
1240 utils_close_pipe(ctx->consumer_metadata_pipe);
1241 error_metadata_pipe:
1242 utils_close_pipe(ctx->consumer_thread_pipe);
1243 error_thread_pipe:
1244 for (i = 0; i < 2; i++) {
1245 int err;
1246
1247 err = close(ctx->consumer_should_quit[i]);
1248 if (err) {
1249 PERROR("close");
1250 }
1251 }
1252 error_poll_fcntl:
1253 error_quit_pipe:
1254 for (i = 0; i < 2; i++) {
1255 int err;
1256
1257 err = close(ctx->consumer_data_pipe[i]);
1258 if (err) {
1259 PERROR("close");
1260 }
1261 }
1262 error_poll_pipe:
1263 free(ctx);
1264 error:
1265 return NULL;
1266 }
1267
1268 /*
1269 * Close all fds associated with the instance and free the context.
1270 */
1271 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1272 {
1273 int ret;
1274
1275 DBG("Consumer destroying it. Closing everything.");
1276
1277 ret = close(ctx->consumer_error_socket);
1278 if (ret) {
1279 PERROR("close");
1280 }
1281 ret = close(ctx->consumer_thread_pipe[0]);
1282 if (ret) {
1283 PERROR("close");
1284 }
1285 ret = close(ctx->consumer_thread_pipe[1]);
1286 if (ret) {
1287 PERROR("close");
1288 }
1289 ret = close(ctx->consumer_data_pipe[0]);
1290 if (ret) {
1291 PERROR("close");
1292 }
1293 ret = close(ctx->consumer_data_pipe[1]);
1294 if (ret) {
1295 PERROR("close");
1296 }
1297 ret = close(ctx->consumer_should_quit[0]);
1298 if (ret) {
1299 PERROR("close");
1300 }
1301 ret = close(ctx->consumer_should_quit[1]);
1302 if (ret) {
1303 PERROR("close");
1304 }
1305 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1306
1307 unlink(ctx->consumer_command_sock_path);
1308 free(ctx);
1309 }
1310
1311 /*
1312 * Write the metadata stream id on the specified file descriptor.
1313 */
1314 static int write_relayd_metadata_id(int fd,
1315 struct lttng_consumer_stream *stream,
1316 struct consumer_relayd_sock_pair *relayd,
1317 unsigned long padding)
1318 {
1319 int ret;
1320 struct lttcomm_relayd_metadata_payload hdr;
1321
1322 hdr.stream_id = htobe64(stream->relayd_stream_id);
1323 hdr.padding_size = htobe32(padding);
1324 do {
1325 ret = write(fd, (void *) &hdr, sizeof(hdr));
1326 } while (ret < 0 && errno == EINTR);
1327 if (ret < 0 || ret != sizeof(hdr)) {
1328 /*
1329 * This error means that the fd's end is closed so ignore the perror
1330 * not to clubber the error output since this can happen in a normal
1331 * code path.
1332 */
1333 if (errno != EPIPE) {
1334 PERROR("write metadata stream id");
1335 }
1336 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
1337 goto end;
1338 }
1339 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1340 stream->relayd_stream_id, padding);
1341
1342 end:
1343 return ret;
1344 }
1345
1346 /*
1347 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1348 * core function for writing trace buffers to either the local filesystem or
1349 * the network.
1350 *
1351 * It must be called with the stream lock held.
1352 *
1353 * Careful review MUST be put if any changes occur!
1354 *
1355 * Returns the number of bytes written
1356 */
1357 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1358 struct lttng_consumer_local_data *ctx,
1359 struct lttng_consumer_stream *stream, unsigned long len,
1360 unsigned long padding)
1361 {
1362 unsigned long mmap_offset;
1363 ssize_t ret = 0, written = 0;
1364 off_t orig_offset = stream->out_fd_offset;
1365 /* Default is on the disk */
1366 int outfd = stream->out_fd;
1367 struct consumer_relayd_sock_pair *relayd = NULL;
1368 unsigned int relayd_hang_up = 0;
1369
1370 /* RCU lock for the relayd pointer */
1371 rcu_read_lock();
1372
1373 /* Flag that the current stream if set for network streaming. */
1374 if (stream->net_seq_idx != -1) {
1375 relayd = consumer_find_relayd(stream->net_seq_idx);
1376 if (relayd == NULL) {
1377 goto end;
1378 }
1379 }
1380
1381 /* get the offset inside the fd to mmap */
1382 switch (consumer_data.type) {
1383 case LTTNG_CONSUMER_KERNEL:
1384 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1385 break;
1386 case LTTNG_CONSUMER32_UST:
1387 case LTTNG_CONSUMER64_UST:
1388 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1389 stream->buf, &mmap_offset);
1390 break;
1391 default:
1392 ERR("Unknown consumer_data type");
1393 assert(0);
1394 }
1395 if (ret != 0) {
1396 errno = -ret;
1397 PERROR("tracer ctl get_mmap_read_offset");
1398 written = ret;
1399 goto end;
1400 }
1401
1402 /* Handle stream on the relayd if the output is on the network */
1403 if (relayd) {
1404 unsigned long netlen = len;
1405
1406 /*
1407 * Lock the control socket for the complete duration of the function
1408 * since from this point on we will use the socket.
1409 */
1410 if (stream->metadata_flag) {
1411 /* Metadata requires the control socket. */
1412 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1413 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
1414 }
1415
1416 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
1417 if (ret >= 0) {
1418 /* Use the returned socket. */
1419 outfd = ret;
1420
1421 /* Write metadata stream id before payload */
1422 if (stream->metadata_flag) {
1423 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
1424 if (ret < 0) {
1425 written = ret;
1426 /* Socket operation failed. We consider the relayd dead */
1427 if (ret == -EPIPE || ret == -EINVAL) {
1428 relayd_hang_up = 1;
1429 goto write_error;
1430 }
1431 goto end;
1432 }
1433 }
1434 } else {
1435 /* Socket operation failed. We consider the relayd dead */
1436 if (ret == -EPIPE || ret == -EINVAL) {
1437 relayd_hang_up = 1;
1438 goto write_error;
1439 }
1440 /* Else, use the default set before which is the filesystem. */
1441 }
1442 } else {
1443 /* No streaming, we have to set the len with the full padding */
1444 len += padding;
1445 }
1446
1447 while (len > 0) {
1448 do {
1449 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1450 } while (ret < 0 && errno == EINTR);
1451 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
1452 if (ret < 0) {
1453 /*
1454 * This is possible if the fd is closed on the other side (outfd)
1455 * or any write problem. It can be verbose a bit for a normal
1456 * execution if for instance the relayd is stopped abruptly. This
1457 * can happen so set this to a DBG statement.
1458 */
1459 DBG("Error in file write mmap");
1460 if (written == 0) {
1461 written = ret;
1462 }
1463 /* Socket operation failed. We consider the relayd dead */
1464 if (errno == EPIPE || errno == EINVAL) {
1465 relayd_hang_up = 1;
1466 goto write_error;
1467 }
1468 goto end;
1469 } else if (ret > len) {
1470 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
1471 written += ret;
1472 goto end;
1473 } else {
1474 len -= ret;
1475 mmap_offset += ret;
1476 }
1477
1478 /* This call is useless on a socket so better save a syscall. */
1479 if (!relayd) {
1480 /* This won't block, but will start writeout asynchronously */
1481 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1482 SYNC_FILE_RANGE_WRITE);
1483 stream->out_fd_offset += ret;
1484 }
1485 written += ret;
1486 }
1487 lttng_consumer_sync_trace_file(stream, orig_offset);
1488
1489 write_error:
1490 /*
1491 * This is a special case that the relayd has closed its socket. Let's
1492 * cleanup the relayd object and all associated streams.
1493 */
1494 if (relayd && relayd_hang_up) {
1495 cleanup_relayd(relayd, ctx);
1496 }
1497
1498 end:
1499 /* Unlock only if ctrl socket used */
1500 if (relayd && stream->metadata_flag) {
1501 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1502 }
1503
1504 rcu_read_unlock();
1505 return written;
1506 }
1507
1508 /*
1509 * Splice the data from the ring buffer to the tracefile.
1510 *
1511 * It must be called with the stream lock held.
1512 *
1513 * Returns the number of bytes spliced.
1514 */
1515 ssize_t lttng_consumer_on_read_subbuffer_splice(
1516 struct lttng_consumer_local_data *ctx,
1517 struct lttng_consumer_stream *stream, unsigned long len,
1518 unsigned long padding)
1519 {
1520 ssize_t ret = 0, written = 0, ret_splice = 0;
1521 loff_t offset = 0;
1522 off_t orig_offset = stream->out_fd_offset;
1523 int fd = stream->wait_fd;
1524 /* Default is on the disk */
1525 int outfd = stream->out_fd;
1526 struct consumer_relayd_sock_pair *relayd = NULL;
1527 int *splice_pipe;
1528 unsigned int relayd_hang_up = 0;
1529
1530 switch (consumer_data.type) {
1531 case LTTNG_CONSUMER_KERNEL:
1532 break;
1533 case LTTNG_CONSUMER32_UST:
1534 case LTTNG_CONSUMER64_UST:
1535 /* Not supported for user space tracing */
1536 return -ENOSYS;
1537 default:
1538 ERR("Unknown consumer_data type");
1539 assert(0);
1540 }
1541
1542 /* RCU lock for the relayd pointer */
1543 rcu_read_lock();
1544
1545 /* Flag that the current stream if set for network streaming. */
1546 if (stream->net_seq_idx != -1) {
1547 relayd = consumer_find_relayd(stream->net_seq_idx);
1548 if (relayd == NULL) {
1549 goto end;
1550 }
1551 }
1552
1553 /*
1554 * Choose right pipe for splice. Metadata and trace data are handled by
1555 * different threads hence the use of two pipes in order not to race or
1556 * corrupt the written data.
1557 */
1558 if (stream->metadata_flag) {
1559 splice_pipe = ctx->consumer_splice_metadata_pipe;
1560 } else {
1561 splice_pipe = ctx->consumer_thread_pipe;
1562 }
1563
1564 /* Write metadata stream id before payload */
1565 if (relayd) {
1566 int total_len = len;
1567
1568 if (stream->metadata_flag) {
1569 /*
1570 * Lock the control socket for the complete duration of the function
1571 * since from this point on we will use the socket.
1572 */
1573 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1574
1575 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1576 padding);
1577 if (ret < 0) {
1578 written = ret;
1579 /* Socket operation failed. We consider the relayd dead */
1580 if (ret == -EBADF) {
1581 WARN("Remote relayd disconnected. Stopping");
1582 relayd_hang_up = 1;
1583 goto write_error;
1584 }
1585 goto end;
1586 }
1587
1588 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1589 }
1590
1591 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1592 if (ret >= 0) {
1593 /* Use the returned socket. */
1594 outfd = ret;
1595 } else {
1596 /* Socket operation failed. We consider the relayd dead */
1597 if (ret == -EBADF) {
1598 WARN("Remote relayd disconnected. Stopping");
1599 relayd_hang_up = 1;
1600 goto write_error;
1601 }
1602 goto end;
1603 }
1604 } else {
1605 /* No streaming, we have to set the len with the full padding */
1606 len += padding;
1607 }
1608
1609 while (len > 0) {
1610 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1611 (unsigned long)offset, len, fd, splice_pipe[1]);
1612 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
1613 SPLICE_F_MOVE | SPLICE_F_MORE);
1614 DBG("splice chan to pipe, ret %zd", ret_splice);
1615 if (ret_splice < 0) {
1616 PERROR("Error in relay splice");
1617 if (written == 0) {
1618 written = ret_splice;
1619 }
1620 ret = errno;
1621 goto splice_error;
1622 }
1623
1624 /* Handle stream on the relayd if the output is on the network */
1625 if (relayd) {
1626 if (stream->metadata_flag) {
1627 size_t metadata_payload_size =
1628 sizeof(struct lttcomm_relayd_metadata_payload);
1629
1630 /* Update counter to fit the spliced data */
1631 ret_splice += metadata_payload_size;
1632 len += metadata_payload_size;
1633 /*
1634 * We do this so the return value can match the len passed as
1635 * argument to this function.
1636 */
1637 written -= metadata_payload_size;
1638 }
1639 }
1640
1641 /* Splice data out */
1642 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
1643 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1644 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
1645 if (ret_splice < 0) {
1646 PERROR("Error in file splice");
1647 if (written == 0) {
1648 written = ret_splice;
1649 }
1650 /* Socket operation failed. We consider the relayd dead */
1651 if (errno == EBADF || errno == EPIPE) {
1652 WARN("Remote relayd disconnected. Stopping");
1653 relayd_hang_up = 1;
1654 goto write_error;
1655 }
1656 ret = errno;
1657 goto splice_error;
1658 } else if (ret_splice > len) {
1659 errno = EINVAL;
1660 PERROR("Wrote more data than requested %zd (len: %lu)",
1661 ret_splice, len);
1662 written += ret_splice;
1663 ret = errno;
1664 goto splice_error;
1665 }
1666 len -= ret_splice;
1667
1668 /* This call is useless on a socket so better save a syscall. */
1669 if (!relayd) {
1670 /* This won't block, but will start writeout asynchronously */
1671 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1672 SYNC_FILE_RANGE_WRITE);
1673 stream->out_fd_offset += ret_splice;
1674 }
1675 written += ret_splice;
1676 }
1677 lttng_consumer_sync_trace_file(stream, orig_offset);
1678
1679 ret = ret_splice;
1680
1681 goto end;
1682
1683 write_error:
1684 /*
1685 * This is a special case that the relayd has closed its socket. Let's
1686 * cleanup the relayd object and all associated streams.
1687 */
1688 if (relayd && relayd_hang_up) {
1689 cleanup_relayd(relayd, ctx);
1690 /* Skip splice error so the consumer does not fail */
1691 goto end;
1692 }
1693
1694 splice_error:
1695 /* send the appropriate error description to sessiond */
1696 switch (ret) {
1697 case EINVAL:
1698 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
1699 break;
1700 case ENOMEM:
1701 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
1702 break;
1703 case ESPIPE:
1704 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
1705 break;
1706 }
1707
1708 end:
1709 if (relayd && stream->metadata_flag) {
1710 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1711 }
1712
1713 rcu_read_unlock();
1714 return written;
1715 }
1716
1717 /*
1718 * Take a snapshot for a specific fd
1719 *
1720 * Returns 0 on success, < 0 on error
1721 */
1722 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1723 struct lttng_consumer_stream *stream)
1724 {
1725 switch (consumer_data.type) {
1726 case LTTNG_CONSUMER_KERNEL:
1727 return lttng_kconsumer_take_snapshot(ctx, stream);
1728 case LTTNG_CONSUMER32_UST:
1729 case LTTNG_CONSUMER64_UST:
1730 return lttng_ustconsumer_take_snapshot(ctx, stream);
1731 default:
1732 ERR("Unknown consumer_data type");
1733 assert(0);
1734 return -ENOSYS;
1735 }
1736
1737 }
1738
1739 /*
1740 * Get the produced position
1741 *
1742 * Returns 0 on success, < 0 on error
1743 */
1744 int lttng_consumer_get_produced_snapshot(
1745 struct lttng_consumer_local_data *ctx,
1746 struct lttng_consumer_stream *stream,
1747 unsigned long *pos)
1748 {
1749 switch (consumer_data.type) {
1750 case LTTNG_CONSUMER_KERNEL:
1751 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
1752 case LTTNG_CONSUMER32_UST:
1753 case LTTNG_CONSUMER64_UST:
1754 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1755 default:
1756 ERR("Unknown consumer_data type");
1757 assert(0);
1758 return -ENOSYS;
1759 }
1760 }
1761
1762 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1763 int sock, struct pollfd *consumer_sockpoll)
1764 {
1765 switch (consumer_data.type) {
1766 case LTTNG_CONSUMER_KERNEL:
1767 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1768 case LTTNG_CONSUMER32_UST:
1769 case LTTNG_CONSUMER64_UST:
1770 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1771 default:
1772 ERR("Unknown consumer_data type");
1773 assert(0);
1774 return -ENOSYS;
1775 }
1776 }
1777
1778 /*
1779 * Iterate over all streams of the hashtable and free them properly.
1780 *
1781 * WARNING: *MUST* be used with data stream only.
1782 */
1783 static void destroy_data_stream_ht(struct lttng_ht *ht)
1784 {
1785 struct lttng_ht_iter iter;
1786 struct lttng_consumer_stream *stream;
1787
1788 if (ht == NULL) {
1789 return;
1790 }
1791
1792 rcu_read_lock();
1793 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1794 /*
1795 * Ignore return value since we are currently cleaning up so any error
1796 * can't be handled.
1797 */
1798 (void) consumer_del_stream(stream, ht);
1799 }
1800 rcu_read_unlock();
1801
1802 lttng_ht_destroy(ht);
1803 }
1804
1805 /*
1806 * Iterate over all streams of the hashtable and free them properly.
1807 *
1808 * XXX: Should not be only for metadata stream or else use an other name.
1809 */
1810 static void destroy_stream_ht(struct lttng_ht *ht)
1811 {
1812 struct lttng_ht_iter iter;
1813 struct lttng_consumer_stream *stream;
1814
1815 if (ht == NULL) {
1816 return;
1817 }
1818
1819 rcu_read_lock();
1820 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1821 /*
1822 * Ignore return value since we are currently cleaning up so any error
1823 * can't be handled.
1824 */
1825 (void) consumer_del_metadata_stream(stream, ht);
1826 }
1827 rcu_read_unlock();
1828
1829 lttng_ht_destroy(ht);
1830 }
1831
1832 /*
1833 * Clean up a metadata stream and free its memory.
1834 */
1835 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1836 struct lttng_ht *ht)
1837 {
1838 int ret;
1839 struct lttng_ht_iter iter;
1840 struct lttng_consumer_channel *free_chan = NULL;
1841 struct consumer_relayd_sock_pair *relayd;
1842
1843 assert(stream);
1844 /*
1845 * This call should NEVER receive regular stream. It must always be
1846 * metadata stream and this is crucial for data structure synchronization.
1847 */
1848 assert(stream->metadata_flag);
1849
1850 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1851
1852 if (ht == NULL) {
1853 /* Means the stream was allocated but not successfully added */
1854 goto free_stream;
1855 }
1856
1857 pthread_mutex_lock(&consumer_data.lock);
1858 pthread_mutex_lock(&stream->lock);
1859
1860 switch (consumer_data.type) {
1861 case LTTNG_CONSUMER_KERNEL:
1862 if (stream->mmap_base != NULL) {
1863 ret = munmap(stream->mmap_base, stream->mmap_len);
1864 if (ret != 0) {
1865 PERROR("munmap metadata stream");
1866 }
1867 }
1868 break;
1869 case LTTNG_CONSUMER32_UST:
1870 case LTTNG_CONSUMER64_UST:
1871 lttng_ustconsumer_del_stream(stream);
1872 break;
1873 default:
1874 ERR("Unknown consumer_data type");
1875 assert(0);
1876 goto end;
1877 }
1878
1879 rcu_read_lock();
1880 iter.iter.node = &stream->node.node;
1881 ret = lttng_ht_del(ht, &iter);
1882 assert(!ret);
1883
1884 /* Remove node session id from the consumer_data stream ht */
1885 iter.iter.node = &stream->node_session_id.node;
1886 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1887 assert(!ret);
1888 rcu_read_unlock();
1889
1890 if (stream->out_fd >= 0) {
1891 ret = close(stream->out_fd);
1892 if (ret) {
1893 PERROR("close");
1894 }
1895 }
1896
1897 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1898 ret = close(stream->wait_fd);
1899 if (ret) {
1900 PERROR("close");
1901 }
1902 }
1903
1904 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1905 ret = close(stream->shm_fd);
1906 if (ret) {
1907 PERROR("close");
1908 }
1909 }
1910
1911 /* Check and cleanup relayd */
1912 rcu_read_lock();
1913 relayd = consumer_find_relayd(stream->net_seq_idx);
1914 if (relayd != NULL) {
1915 uatomic_dec(&relayd->refcount);
1916 assert(uatomic_read(&relayd->refcount) >= 0);
1917
1918 /* Closing streams requires to lock the control socket. */
1919 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1920 ret = relayd_send_close_stream(&relayd->control_sock,
1921 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1922 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1923 if (ret < 0) {
1924 DBG("Unable to close stream on the relayd. Continuing");
1925 /*
1926 * Continue here. There is nothing we can do for the relayd.
1927 * Chances are that the relayd has closed the socket so we just
1928 * continue cleaning up.
1929 */
1930 }
1931
1932 /* Both conditions are met, we destroy the relayd. */
1933 if (uatomic_read(&relayd->refcount) == 0 &&
1934 uatomic_read(&relayd->destroy_flag)) {
1935 destroy_relayd(relayd);
1936 }
1937 }
1938 rcu_read_unlock();
1939
1940 /* Atomically decrement channel refcount since other threads can use it. */
1941 uatomic_dec(&stream->chan->refcount);
1942 if (!uatomic_read(&stream->chan->refcount)
1943 && !uatomic_read(&stream->chan->nb_init_streams)) {
1944 /* Go for channel deletion! */
1945 free_chan = stream->chan;
1946 }
1947
1948 end:
1949 pthread_mutex_unlock(&stream->lock);
1950 pthread_mutex_unlock(&consumer_data.lock);
1951
1952 if (free_chan) {
1953 consumer_del_channel(free_chan);
1954 }
1955
1956 free_stream:
1957 call_rcu(&stream->node.head, consumer_free_stream);
1958 }
1959
1960 /*
1961 * Action done with the metadata stream when adding it to the consumer internal
1962 * data structures to handle it.
1963 */
1964 static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1965 struct lttng_ht *ht)
1966 {
1967 int ret = 0;
1968 struct consumer_relayd_sock_pair *relayd;
1969 struct lttng_ht_iter iter;
1970 struct lttng_ht_node_ulong *node;
1971
1972 assert(stream);
1973 assert(ht);
1974
1975 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1976
1977 pthread_mutex_lock(&consumer_data.lock);
1978 pthread_mutex_lock(&stream->lock);
1979
1980 /*
1981 * From here, refcounts are updated so be _careful_ when returning an error
1982 * after this point.
1983 */
1984
1985 rcu_read_lock();
1986
1987 /*
1988 * Lookup the stream just to make sure it does not exist in our internal
1989 * state. This should NEVER happen.
1990 */
1991 lttng_ht_lookup(ht, (void *)((unsigned long) stream->wait_fd), &iter);
1992 node = lttng_ht_iter_get_node_ulong(&iter);
1993 assert(!node);
1994
1995 /* Find relayd and, if one is found, increment refcount. */
1996 relayd = consumer_find_relayd(stream->net_seq_idx);
1997 if (relayd != NULL) {
1998 uatomic_inc(&relayd->refcount);
1999 }
2000
2001 /* Update channel refcount once added without error(s). */
2002 uatomic_inc(&stream->chan->refcount);
2003
2004 /*
2005 * When nb_init_streams reaches 0, we don't need to trigger any action in
2006 * terms of destroying the associated channel, because the action that
2007 * causes the count to become 0 also causes a stream to be added. The
2008 * channel deletion will thus be triggered by the following removal of this
2009 * stream.
2010 */
2011 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
2012 uatomic_dec(&stream->chan->nb_init_streams);
2013 }
2014
2015 lttng_ht_add_unique_ulong(ht, &stream->node);
2016
2017 /*
2018 * Add stream to the stream_list_ht of the consumer data. No need to steal
2019 * the key since the HT does not use it and we allow to add redundant keys
2020 * into this table.
2021 */
2022 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
2023
2024 rcu_read_unlock();
2025
2026 pthread_mutex_unlock(&stream->lock);
2027 pthread_mutex_unlock(&consumer_data.lock);
2028 return ret;
2029 }
2030
2031 /*
2032 * Delete data stream that are flagged for deletion (endpoint_status).
2033 */
2034 static void validate_endpoint_status_data_stream(void)
2035 {
2036 struct lttng_ht_iter iter;
2037 struct lttng_consumer_stream *stream;
2038
2039 DBG("Consumer delete flagged data stream");
2040
2041 rcu_read_lock();
2042 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2043 /* Validate delete flag of the stream */
2044 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2045 continue;
2046 }
2047 /* Delete it right now */
2048 consumer_del_stream(stream, data_ht);
2049 }
2050 rcu_read_unlock();
2051 }
2052
2053 /*
2054 * Delete metadata stream that are flagged for deletion (endpoint_status).
2055 */
2056 static void validate_endpoint_status_metadata_stream(
2057 struct lttng_poll_event *pollset)
2058 {
2059 struct lttng_ht_iter iter;
2060 struct lttng_consumer_stream *stream;
2061
2062 DBG("Consumer delete flagged metadata stream");
2063
2064 assert(pollset);
2065
2066 rcu_read_lock();
2067 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2068 /* Validate delete flag of the stream */
2069 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2070 continue;
2071 }
2072 /*
2073 * Remove from pollset so the metadata thread can continue without
2074 * blocking on a deleted stream.
2075 */
2076 lttng_poll_del(pollset, stream->wait_fd);
2077
2078 /* Delete it right now */
2079 consumer_del_metadata_stream(stream, metadata_ht);
2080 }
2081 rcu_read_unlock();
2082 }
2083
2084 /*
2085 * Thread polls on metadata file descriptor and write them on disk or on the
2086 * network.
2087 */
2088 void *consumer_thread_metadata_poll(void *data)
2089 {
2090 int ret, i, pollfd;
2091 uint32_t revents, nb_fd;
2092 struct lttng_consumer_stream *stream = NULL;
2093 struct lttng_ht_iter iter;
2094 struct lttng_ht_node_ulong *node;
2095 struct lttng_poll_event events;
2096 struct lttng_consumer_local_data *ctx = data;
2097 ssize_t len;
2098
2099 rcu_register_thread();
2100
2101 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2102 if (!metadata_ht) {
2103 /* ENOMEM at this point. Better to bail out. */
2104 goto error;
2105 }
2106
2107 DBG("Thread metadata poll started");
2108
2109 /* Size is set to 1 for the consumer_metadata pipe */
2110 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2111 if (ret < 0) {
2112 ERR("Poll set creation failed");
2113 goto end;
2114 }
2115
2116 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
2117 if (ret < 0) {
2118 goto end;
2119 }
2120
2121 /* Main loop */
2122 DBG("Metadata main loop started");
2123
2124 while (1) {
2125 /* Only the metadata pipe is set */
2126 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2127 goto end;
2128 }
2129
2130 restart:
2131 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2132 ret = lttng_poll_wait(&events, -1);
2133 DBG("Metadata event catched in thread");
2134 if (ret < 0) {
2135 if (errno == EINTR) {
2136 ERR("Poll EINTR catched");
2137 goto restart;
2138 }
2139 goto error;
2140 }
2141
2142 nb_fd = ret;
2143
2144 /* From here, the event is a metadata wait fd */
2145 for (i = 0; i < nb_fd; i++) {
2146 revents = LTTNG_POLL_GETEV(&events, i);
2147 pollfd = LTTNG_POLL_GETFD(&events, i);
2148
2149 /* Just don't waste time if no returned events for the fd */
2150 if (!revents) {
2151 continue;
2152 }
2153
2154 if (pollfd == ctx->consumer_metadata_pipe[0]) {
2155 if (revents & (LPOLLERR | LPOLLHUP )) {
2156 DBG("Metadata thread pipe hung up");
2157 /*
2158 * Remove the pipe from the poll set and continue the loop
2159 * since their might be data to consume.
2160 */
2161 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
2162 ret = close(ctx->consumer_metadata_pipe[0]);
2163 if (ret < 0) {
2164 PERROR("close metadata pipe");
2165 }
2166 continue;
2167 } else if (revents & LPOLLIN) {
2168 do {
2169 /* Get the stream pointer received */
2170 ret = read(pollfd, &stream, sizeof(stream));
2171 } while (ret < 0 && errno == EINTR);
2172 if (ret < 0 ||
2173 ret < sizeof(struct lttng_consumer_stream *)) {
2174 PERROR("read metadata stream");
2175 /*
2176 * Let's continue here and hope we can still work
2177 * without stopping the consumer. XXX: Should we?
2178 */
2179 continue;
2180 }
2181
2182 /* A NULL stream means that the state has changed. */
2183 if (stream == NULL) {
2184 /* Check for deleted streams. */
2185 validate_endpoint_status_metadata_stream(&events);
2186 continue;
2187 }
2188
2189 DBG("Adding metadata stream %d to poll set",
2190 stream->wait_fd);
2191
2192 ret = consumer_add_metadata_stream(stream, metadata_ht);
2193 if (ret) {
2194 ERR("Unable to add metadata stream");
2195 /* Stream was not setup properly. Continuing. */
2196 consumer_del_metadata_stream(stream, NULL);
2197 continue;
2198 }
2199
2200 /* Add metadata stream to the global poll events list */
2201 lttng_poll_add(&events, stream->wait_fd,
2202 LPOLLIN | LPOLLPRI);
2203 }
2204
2205 /* Handle other stream */
2206 continue;
2207 }
2208
2209 rcu_read_lock();
2210 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
2211 &iter);
2212 node = lttng_ht_iter_get_node_ulong(&iter);
2213 assert(node);
2214
2215 stream = caa_container_of(node, struct lttng_consumer_stream,
2216 node);
2217
2218 /* Check for error event */
2219 if (revents & (LPOLLERR | LPOLLHUP)) {
2220 DBG("Metadata fd %d is hup|err.", pollfd);
2221 if (!stream->hangup_flush_done
2222 && (consumer_data.type == LTTNG_CONSUMER32_UST
2223 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2224 DBG("Attempting to flush and consume the UST buffers");
2225 lttng_ustconsumer_on_stream_hangup(stream);
2226
2227 /* We just flushed the stream now read it. */
2228 do {
2229 len = ctx->on_buffer_ready(stream, ctx);
2230 /*
2231 * We don't check the return value here since if we get
2232 * a negative len, it means an error occured thus we
2233 * simply remove it from the poll set and free the
2234 * stream.
2235 */
2236 } while (len > 0);
2237 }
2238
2239 lttng_poll_del(&events, stream->wait_fd);
2240 /*
2241 * This call update the channel states, closes file descriptors
2242 * and securely free the stream.
2243 */
2244 consumer_del_metadata_stream(stream, metadata_ht);
2245 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2246 /* Get the data out of the metadata file descriptor */
2247 DBG("Metadata available on fd %d", pollfd);
2248 assert(stream->wait_fd == pollfd);
2249
2250 len = ctx->on_buffer_ready(stream, ctx);
2251 /* It's ok to have an unavailable sub-buffer */
2252 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2253 /* Clean up stream from consumer and free it. */
2254 lttng_poll_del(&events, stream->wait_fd);
2255 consumer_del_metadata_stream(stream, metadata_ht);
2256 } else if (len > 0) {
2257 stream->data_read = 1;
2258 }
2259 }
2260
2261 /* Release RCU lock for the stream looked up */
2262 rcu_read_unlock();
2263 }
2264 }
2265
2266 error:
2267 end:
2268 DBG("Metadata poll thread exiting");
2269 lttng_poll_clean(&events);
2270
2271 destroy_stream_ht(metadata_ht);
2272
2273 rcu_unregister_thread();
2274 return NULL;
2275 }
2276
2277 /*
2278 * This thread polls the fds in the set to consume the data and write
2279 * it to tracefile if necessary.
2280 */
2281 void *consumer_thread_data_poll(void *data)
2282 {
2283 int num_rdy, num_hup, high_prio, ret, i;
2284 struct pollfd *pollfd = NULL;
2285 /* local view of the streams */
2286 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2287 /* local view of consumer_data.fds_count */
2288 int nb_fd = 0;
2289 struct lttng_consumer_local_data *ctx = data;
2290 ssize_t len;
2291
2292 rcu_register_thread();
2293
2294 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2295 if (data_ht == NULL) {
2296 /* ENOMEM at this point. Better to bail out. */
2297 goto end;
2298 }
2299
2300 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
2301
2302 while (1) {
2303 high_prio = 0;
2304 num_hup = 0;
2305
2306 /*
2307 * the fds set has been updated, we need to update our
2308 * local array as well
2309 */
2310 pthread_mutex_lock(&consumer_data.lock);
2311 if (consumer_data.need_update) {
2312 if (pollfd != NULL) {
2313 free(pollfd);
2314 pollfd = NULL;
2315 }
2316 if (local_stream != NULL) {
2317 free(local_stream);
2318 local_stream = NULL;
2319 }
2320
2321 /* allocate for all fds + 1 for the consumer_data_pipe */
2322 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
2323 if (pollfd == NULL) {
2324 PERROR("pollfd malloc");
2325 pthread_mutex_unlock(&consumer_data.lock);
2326 goto end;
2327 }
2328
2329 /* allocate for all fds + 1 for the consumer_data_pipe */
2330 local_stream = zmalloc((consumer_data.stream_count + 1) *
2331 sizeof(struct lttng_consumer_stream));
2332 if (local_stream == NULL) {
2333 PERROR("local_stream malloc");
2334 pthread_mutex_unlock(&consumer_data.lock);
2335 goto end;
2336 }
2337 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
2338 data_ht);
2339 if (ret < 0) {
2340 ERR("Error in allocating pollfd or local_outfds");
2341 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2342 pthread_mutex_unlock(&consumer_data.lock);
2343 goto end;
2344 }
2345 nb_fd = ret;
2346 consumer_data.need_update = 0;
2347 }
2348 pthread_mutex_unlock(&consumer_data.lock);
2349
2350 /* No FDs and consumer_quit, consumer_cleanup the thread */
2351 if (nb_fd == 0 && consumer_quit == 1) {
2352 goto end;
2353 }
2354 /* poll on the array of fds */
2355 restart:
2356 DBG("polling on %d fd", nb_fd + 1);
2357 num_rdy = poll(pollfd, nb_fd + 1, -1);
2358 DBG("poll num_rdy : %d", num_rdy);
2359 if (num_rdy == -1) {
2360 /*
2361 * Restart interrupted system call.
2362 */
2363 if (errno == EINTR) {
2364 goto restart;
2365 }
2366 PERROR("Poll error");
2367 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2368 goto end;
2369 } else if (num_rdy == 0) {
2370 DBG("Polling thread timed out");
2371 goto end;
2372 }
2373
2374 /*
2375 * If the consumer_data_pipe triggered poll go directly to the
2376 * beginning of the loop to update the array. We want to prioritize
2377 * array update over low-priority reads.
2378 */
2379 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2380 size_t pipe_readlen;
2381
2382 DBG("consumer_data_pipe wake up");
2383 /* Consume 1 byte of pipe data */
2384 do {
2385 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
2386 sizeof(new_stream));
2387 } while (pipe_readlen == -1 && errno == EINTR);
2388 if (pipe_readlen < 0) {
2389 PERROR("read consumer data pipe");
2390 /* Continue so we can at least handle the current stream(s). */
2391 continue;
2392 }
2393
2394 /*
2395 * If the stream is NULL, just ignore it. It's also possible that
2396 * the sessiond poll thread changed the consumer_quit state and is
2397 * waking us up to test it.
2398 */
2399 if (new_stream == NULL) {
2400 validate_endpoint_status_data_stream();
2401 continue;
2402 }
2403
2404 ret = consumer_add_stream(new_stream, data_ht);
2405 if (ret) {
2406 ERR("Consumer add stream %d failed. Continuing",
2407 new_stream->key);
2408 /*
2409 * At this point, if the add_stream fails, it is not in the
2410 * hash table thus passing the NULL value here.
2411 */
2412 consumer_del_stream(new_stream, NULL);
2413 }
2414
2415 /* Continue to update the local streams and handle prio ones */
2416 continue;
2417 }
2418
2419 /* Take care of high priority channels first. */
2420 for (i = 0; i < nb_fd; i++) {
2421 if (local_stream[i] == NULL) {
2422 continue;
2423 }
2424 if (pollfd[i].revents & POLLPRI) {
2425 DBG("Urgent read on fd %d", pollfd[i].fd);
2426 high_prio = 1;
2427 len = ctx->on_buffer_ready(local_stream[i], ctx);
2428 /* it's ok to have an unavailable sub-buffer */
2429 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2430 /* Clean the stream and free it. */
2431 consumer_del_stream(local_stream[i], data_ht);
2432 local_stream[i] = NULL;
2433 } else if (len > 0) {
2434 local_stream[i]->data_read = 1;
2435 }
2436 }
2437 }
2438
2439 /*
2440 * If we read high prio channel in this loop, try again
2441 * for more high prio data.
2442 */
2443 if (high_prio) {
2444 continue;
2445 }
2446
2447 /* Take care of low priority channels. */
2448 for (i = 0; i < nb_fd; i++) {
2449 if (local_stream[i] == NULL) {
2450 continue;
2451 }
2452 if ((pollfd[i].revents & POLLIN) ||
2453 local_stream[i]->hangup_flush_done) {
2454 DBG("Normal read on fd %d", pollfd[i].fd);
2455 len = ctx->on_buffer_ready(local_stream[i], ctx);
2456 /* it's ok to have an unavailable sub-buffer */
2457 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2458 /* Clean the stream and free it. */
2459 consumer_del_stream(local_stream[i], data_ht);
2460 local_stream[i] = NULL;
2461 } else if (len > 0) {
2462 local_stream[i]->data_read = 1;
2463 }
2464 }
2465 }
2466
2467 /* Handle hangup and errors */
2468 for (i = 0; i < nb_fd; i++) {
2469 if (local_stream[i] == NULL) {
2470 continue;
2471 }
2472 if (!local_stream[i]->hangup_flush_done
2473 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2474 && (consumer_data.type == LTTNG_CONSUMER32_UST
2475 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2476 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2477 pollfd[i].fd);
2478 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2479 /* Attempt read again, for the data we just flushed. */
2480 local_stream[i]->data_read = 1;
2481 }
2482 /*
2483 * If the poll flag is HUP/ERR/NVAL and we have
2484 * read no data in this pass, we can remove the
2485 * stream from its hash table.
2486 */
2487 if ((pollfd[i].revents & POLLHUP)) {
2488 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2489 if (!local_stream[i]->data_read) {
2490 consumer_del_stream(local_stream[i], data_ht);
2491 local_stream[i] = NULL;
2492 num_hup++;
2493 }
2494 } else if (pollfd[i].revents & POLLERR) {
2495 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2496 if (!local_stream[i]->data_read) {
2497 consumer_del_stream(local_stream[i], data_ht);
2498 local_stream[i] = NULL;
2499 num_hup++;
2500 }
2501 } else if (pollfd[i].revents & POLLNVAL) {
2502 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2503 if (!local_stream[i]->data_read) {
2504 consumer_del_stream(local_stream[i], data_ht);
2505 local_stream[i] = NULL;
2506 num_hup++;
2507 }
2508 }
2509 if (local_stream[i] != NULL) {
2510 local_stream[i]->data_read = 0;
2511 }
2512 }
2513 }
2514 end:
2515 DBG("polling thread exiting");
2516 if (pollfd != NULL) {
2517 free(pollfd);
2518 pollfd = NULL;
2519 }
2520 if (local_stream != NULL) {
2521 free(local_stream);
2522 local_stream = NULL;
2523 }
2524
2525 /*
2526 * Close the write side of the pipe so epoll_wait() in
2527 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2528 * read side of the pipe. If we close them both, epoll_wait strangely does
2529 * not return and could create a endless wait period if the pipe is the
2530 * only tracked fd in the poll set. The thread will take care of closing
2531 * the read side.
2532 */
2533 ret = close(ctx->consumer_metadata_pipe[1]);
2534 if (ret < 0) {
2535 PERROR("close data pipe");
2536 }
2537
2538 destroy_data_stream_ht(data_ht);
2539
2540 rcu_unregister_thread();
2541 return NULL;
2542 }
2543
2544 /*
2545 * This thread listens on the consumerd socket and receives the file
2546 * descriptors from the session daemon.
2547 */
2548 void *consumer_thread_sessiond_poll(void *data)
2549 {
2550 int sock = -1, client_socket, ret;
2551 /*
2552 * structure to poll for incoming data on communication socket avoids
2553 * making blocking sockets.
2554 */
2555 struct pollfd consumer_sockpoll[2];
2556 struct lttng_consumer_local_data *ctx = data;
2557
2558 rcu_register_thread();
2559
2560 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2561 unlink(ctx->consumer_command_sock_path);
2562 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2563 if (client_socket < 0) {
2564 ERR("Cannot create command socket");
2565 goto end;
2566 }
2567
2568 ret = lttcomm_listen_unix_sock(client_socket);
2569 if (ret < 0) {
2570 goto end;
2571 }
2572
2573 DBG("Sending ready command to lttng-sessiond");
2574 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
2575 /* return < 0 on error, but == 0 is not fatal */
2576 if (ret < 0) {
2577 ERR("Error sending ready command to lttng-sessiond");
2578 goto end;
2579 }
2580
2581 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2582 if (ret < 0) {
2583 PERROR("fcntl O_NONBLOCK");
2584 goto end;
2585 }
2586
2587 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2588 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2589 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2590 consumer_sockpoll[1].fd = client_socket;
2591 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2592
2593 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2594 goto end;
2595 }
2596 DBG("Connection on client_socket");
2597
2598 /* Blocking call, waiting for transmission */
2599 sock = lttcomm_accept_unix_sock(client_socket);
2600 if (sock <= 0) {
2601 WARN("On accept");
2602 goto end;
2603 }
2604 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2605 if (ret < 0) {
2606 PERROR("fcntl O_NONBLOCK");
2607 goto end;
2608 }
2609
2610 /* This socket is not useful anymore. */
2611 ret = close(client_socket);
2612 if (ret < 0) {
2613 PERROR("close client_socket");
2614 }
2615 client_socket = -1;
2616
2617 /* update the polling structure to poll on the established socket */
2618 consumer_sockpoll[1].fd = sock;
2619 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2620
2621 while (1) {
2622 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2623 goto end;
2624 }
2625 DBG("Incoming command on sock");
2626 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2627 if (ret == -ENOENT) {
2628 DBG("Received STOP command");
2629 goto end;
2630 }
2631 if (ret <= 0) {
2632 /*
2633 * This could simply be a session daemon quitting. Don't output
2634 * ERR() here.
2635 */
2636 DBG("Communication interrupted on command socket");
2637 goto end;
2638 }
2639 if (consumer_quit) {
2640 DBG("consumer_thread_receive_fds received quit from signal");
2641 goto end;
2642 }
2643 DBG("received fds on sock");
2644 }
2645 end:
2646 DBG("consumer_thread_receive_fds exiting");
2647
2648 /*
2649 * when all fds have hung up, the polling thread
2650 * can exit cleanly
2651 */
2652 consumer_quit = 1;
2653
2654 /*
2655 * Notify the data poll thread to poll back again and test the
2656 * consumer_quit state that we just set so to quit gracefully.
2657 */
2658 notify_thread_pipe(ctx->consumer_data_pipe[1]);
2659
2660 /* Cleaning up possibly open sockets. */
2661 if (sock >= 0) {
2662 ret = close(sock);
2663 if (ret < 0) {
2664 PERROR("close sock sessiond poll");
2665 }
2666 }
2667 if (client_socket >= 0) {
2668 ret = close(sock);
2669 if (ret < 0) {
2670 PERROR("close client_socket sessiond poll");
2671 }
2672 }
2673
2674 rcu_unregister_thread();
2675 return NULL;
2676 }
2677
2678 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
2679 struct lttng_consumer_local_data *ctx)
2680 {
2681 ssize_t ret;
2682
2683 pthread_mutex_lock(&stream->lock);
2684
2685 switch (consumer_data.type) {
2686 case LTTNG_CONSUMER_KERNEL:
2687 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
2688 break;
2689 case LTTNG_CONSUMER32_UST:
2690 case LTTNG_CONSUMER64_UST:
2691 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
2692 break;
2693 default:
2694 ERR("Unknown consumer_data type");
2695 assert(0);
2696 ret = -ENOSYS;
2697 break;
2698 }
2699
2700 pthread_mutex_unlock(&stream->lock);
2701 return ret;
2702 }
2703
2704 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2705 {
2706 switch (consumer_data.type) {
2707 case LTTNG_CONSUMER_KERNEL:
2708 return lttng_kconsumer_on_recv_stream(stream);
2709 case LTTNG_CONSUMER32_UST:
2710 case LTTNG_CONSUMER64_UST:
2711 return lttng_ustconsumer_on_recv_stream(stream);
2712 default:
2713 ERR("Unknown consumer_data type");
2714 assert(0);
2715 return -ENOSYS;
2716 }
2717 }
2718
2719 /*
2720 * Allocate and set consumer data hash tables.
2721 */
2722 void lttng_consumer_init(void)
2723 {
2724 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2725 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2726 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2727 relayd_session_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2728 }
2729
2730 /*
2731 * Process the ADD_RELAYD command receive by a consumer.
2732 *
2733 * This will create a relayd socket pair and add it to the relayd hash table.
2734 * The caller MUST acquire a RCU read side lock before calling it.
2735 */
2736 int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2737 struct lttng_consumer_local_data *ctx, int sock,
2738 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock,
2739 unsigned int sessiond_id)
2740 {
2741 int fd = -1, ret = -1, relayd_created = 0;
2742 enum lttng_error_code ret_code = LTTNG_OK;
2743 struct consumer_relayd_sock_pair *relayd;
2744 struct consumer_relayd_session_id *relayd_id_node;
2745
2746 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2747
2748 /* First send a status message before receiving the fds. */
2749 ret = consumer_send_status_msg(sock, ret_code);
2750 if (ret < 0) {
2751 /* Somehow, the session daemon is not responding anymore. */
2752 goto error;
2753 }
2754
2755 /* Get relayd reference if exists. */
2756 relayd = consumer_find_relayd(net_seq_idx);
2757 if (relayd == NULL) {
2758 /* Not found. Allocate one. */
2759 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2760 if (relayd == NULL) {
2761 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
2762 ret = -1;
2763 goto error;
2764 }
2765 relayd->sessiond_session_id = (uint64_t) sessiond_id;
2766 relayd_created = 1;
2767 }
2768
2769 /* Poll on consumer socket. */
2770 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2771 ret = -EINTR;
2772 goto error;
2773 }
2774
2775 /* Get relayd socket from session daemon */
2776 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2777 if (ret != sizeof(fd)) {
2778 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
2779 ret = -1;
2780 fd = -1; /* Just in case it gets set with an invalid value. */
2781 goto error;
2782 }
2783
2784 /* We have the fds without error. Send status back. */
2785 ret = consumer_send_status_msg(sock, ret_code);
2786 if (ret < 0) {
2787 /* Somehow, the session daemon is not responding anymore. */
2788 goto error;
2789 }
2790
2791 /* Copy socket information and received FD */
2792 switch (sock_type) {
2793 case LTTNG_STREAM_CONTROL:
2794 /* Copy received lttcomm socket */
2795 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2796 ret = lttcomm_create_sock(&relayd->control_sock);
2797 /* Immediately try to close the created socket if valid. */
2798 if (relayd->control_sock.fd >= 0) {
2799 if (close(relayd->control_sock.fd)) {
2800 PERROR("close relayd control socket");
2801 }
2802 }
2803 /* Handle create_sock error. */
2804 if (ret < 0) {
2805 goto error;
2806 }
2807
2808 /* Assign new file descriptor */
2809 relayd->control_sock.fd = fd;
2810
2811 /*
2812 * Create a session on the relayd and store the returned id. Lock the
2813 * control socket mutex if the relayd was NOT created before.
2814 */
2815 if (!relayd_created) {
2816 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2817 }
2818 ret = relayd_create_session(&relayd->control_sock,
2819 &relayd->relayd_session_id);
2820 if (!relayd_created) {
2821 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2822 }
2823 if (ret < 0) {
2824 goto error;
2825 }
2826
2827 /* Set up a relayd session id node. */
2828 relayd_id_node = zmalloc(sizeof(struct consumer_relayd_session_id));
2829 if (!relayd_id_node) {
2830 PERROR("zmalloc relayd id node");
2831 ret = -1;
2832 goto error;
2833 }
2834
2835 relayd_id_node->relayd_id = relayd->relayd_session_id;
2836 relayd_id_node->sessiond_id = (uint64_t) sessiond_id;
2837
2838 /* Indexed by session id of the sessiond. */
2839 lttng_ht_node_init_ulong(&relayd_id_node->node,
2840 relayd_id_node->sessiond_id);
2841 rcu_read_lock();
2842 lttng_ht_add_unique_ulong(relayd_session_id_ht, &relayd_id_node->node);
2843 rcu_read_unlock();
2844
2845 break;
2846 case LTTNG_STREAM_DATA:
2847 /* Copy received lttcomm socket */
2848 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2849 ret = lttcomm_create_sock(&relayd->data_sock);
2850 /* Immediately try to close the created socket if valid. */
2851 if (relayd->data_sock.fd >= 0) {
2852 if (close(relayd->data_sock.fd)) {
2853 PERROR("close relayd data socket");
2854 }
2855 }
2856 /* Handle create_sock error. */
2857 if (ret < 0) {
2858 goto error;
2859 }
2860
2861 /* Assign new file descriptor */
2862 relayd->data_sock.fd = fd;
2863 break;
2864 default:
2865 ERR("Unknown relayd socket type (%d)", sock_type);
2866 ret = -1;
2867 goto error;
2868 }
2869
2870 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2871 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2872 relayd->net_seq_idx, fd);
2873
2874 /*
2875 * Add relayd socket pair to consumer data hashtable. If object already
2876 * exists or on error, the function gracefully returns.
2877 */
2878 add_relayd(relayd);
2879
2880 /* All good! */
2881 return 0;
2882
2883 error:
2884 /* Close received socket if valid. */
2885 if (fd >= 0) {
2886 if (close(fd)) {
2887 PERROR("close received socket");
2888 }
2889 }
2890
2891 if (relayd_created) {
2892 /* We just want to cleanup. Ignore ret value. */
2893 (void) relayd_close(&relayd->control_sock);
2894 (void) relayd_close(&relayd->data_sock);
2895 free(relayd);
2896 }
2897
2898 return ret;
2899 }
2900
2901 /*
2902 * Try to lock the stream mutex.
2903 *
2904 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2905 */
2906 static int stream_try_lock(struct lttng_consumer_stream *stream)
2907 {
2908 int ret;
2909
2910 assert(stream);
2911
2912 /*
2913 * Try to lock the stream mutex. On failure, we know that the stream is
2914 * being used else where hence there is data still being extracted.
2915 */
2916 ret = pthread_mutex_trylock(&stream->lock);
2917 if (ret) {
2918 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2919 ret = 0;
2920 goto end;
2921 }
2922
2923 ret = 1;
2924
2925 end:
2926 return ret;
2927 }
2928
2929 /*
2930 * Search for a relayd associated to the session id and return the reference.
2931 *
2932 * A rcu read side lock MUST be acquire before calling this function and locked
2933 * until the relayd object is no longer necessary.
2934 */
2935 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
2936 {
2937 struct lttng_ht_iter iter;
2938 struct lttng_ht_node_ulong *node;
2939 struct consumer_relayd_sock_pair *relayd = NULL;
2940 struct consumer_relayd_session_id *session_id_map;
2941
2942 /* Get the session id map. */
2943 lttng_ht_lookup(relayd_session_id_ht, (void *)((unsigned long) id), &iter);
2944 node = lttng_ht_iter_get_node_ulong(&iter);
2945 if (node == NULL) {
2946 goto end;
2947 }
2948
2949 session_id_map = caa_container_of(node, struct consumer_relayd_session_id,
2950 node);
2951
2952 /* Iterate over all relayd since they are indexed by net_seq_idx. */
2953 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
2954 node.node) {
2955 if (relayd->relayd_session_id == session_id_map->relayd_id) {
2956 /* Found the relayd. There can be only one per id. */
2957 break;
2958 }
2959 }
2960
2961 end:
2962 return relayd;
2963 }
2964
2965 /*
2966 * Check if for a given session id there is still data needed to be extract
2967 * from the buffers.
2968 *
2969 * Return 1 if data is pending or else 0 meaning ready to be read.
2970 */
2971 int consumer_data_pending(uint64_t id)
2972 {
2973 int ret;
2974 struct lttng_ht_iter iter;
2975 struct lttng_ht *ht;
2976 struct lttng_consumer_stream *stream;
2977 struct consumer_relayd_sock_pair *relayd = NULL;
2978 int (*data_pending)(struct lttng_consumer_stream *);
2979
2980 DBG("Consumer data pending command on session id %" PRIu64, id);
2981
2982 rcu_read_lock();
2983 pthread_mutex_lock(&consumer_data.lock);
2984
2985 switch (consumer_data.type) {
2986 case LTTNG_CONSUMER_KERNEL:
2987 data_pending = lttng_kconsumer_data_pending;
2988 break;
2989 case LTTNG_CONSUMER32_UST:
2990 case LTTNG_CONSUMER64_UST:
2991 data_pending = lttng_ustconsumer_data_pending;
2992 break;
2993 default:
2994 ERR("Unknown consumer data type");
2995 assert(0);
2996 }
2997
2998 /* Ease our life a bit */
2999 ht = consumer_data.stream_list_ht;
3000
3001 relayd = find_relayd_by_session_id(id);
3002 if (relayd) {
3003 /* Send init command for data pending. */
3004 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3005 ret = relayd_begin_data_pending(&relayd->control_sock,
3006 relayd->relayd_session_id);
3007 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3008 if (ret < 0) {
3009 /* Communication error thus the relayd so no data pending. */
3010 goto data_not_pending;
3011 }
3012 }
3013
3014 cds_lfht_for_each_entry_duplicate(ht->ht,
3015 ht->hash_fct((void *)((unsigned long) id), lttng_ht_seed),
3016 ht->match_fct, (void *)((unsigned long) id),
3017 &iter.iter, stream, node_session_id.node) {
3018 /* If this call fails, the stream is being used hence data pending. */
3019 ret = stream_try_lock(stream);
3020 if (!ret) {
3021 goto data_pending;
3022 }
3023
3024 /*
3025 * A removed node from the hash table indicates that the stream has
3026 * been deleted thus having a guarantee that the buffers are closed
3027 * on the consumer side. However, data can still be transmitted
3028 * over the network so don't skip the relayd check.
3029 */
3030 ret = cds_lfht_is_node_deleted(&stream->node.node);
3031 if (!ret) {
3032 /* Check the stream if there is data in the buffers. */
3033 ret = data_pending(stream);
3034 if (ret == 1) {
3035 pthread_mutex_unlock(&stream->lock);
3036 goto data_pending;
3037 }
3038 }
3039
3040 /* Relayd check */
3041 if (relayd) {
3042 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3043 if (stream->metadata_flag) {
3044 ret = relayd_quiescent_control(&relayd->control_sock,
3045 stream->relayd_stream_id);
3046 } else {
3047 ret = relayd_data_pending(&relayd->control_sock,
3048 stream->relayd_stream_id,
3049 stream->next_net_seq_num - 1);
3050 }
3051 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3052 if (ret == 1) {
3053 pthread_mutex_unlock(&stream->lock);
3054 goto data_pending;
3055 }
3056 }
3057 pthread_mutex_unlock(&stream->lock);
3058 }
3059
3060 if (relayd) {
3061 unsigned int is_data_inflight = 0;
3062
3063 /* Send init command for data pending. */
3064 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3065 ret = relayd_end_data_pending(&relayd->control_sock,
3066 relayd->relayd_session_id, &is_data_inflight);
3067 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3068 if (ret < 0) {
3069 goto data_not_pending;
3070 }
3071 if (is_data_inflight) {
3072 goto data_pending;
3073 }
3074 }
3075
3076 /*
3077 * Finding _no_ node in the hash table and no inflight data means that the
3078 * stream(s) have been removed thus data is guaranteed to be available for
3079 * analysis from the trace files.
3080 */
3081
3082 data_not_pending:
3083 /* Data is available to be read by a viewer. */
3084 pthread_mutex_unlock(&consumer_data.lock);
3085 rcu_read_unlock();
3086 return 0;
3087
3088 data_pending:
3089 /* Data is still being extracted from buffers. */
3090 pthread_mutex_unlock(&consumer_data.lock);
3091 rcu_read_unlock();
3092 return 1;
3093 }
3094
3095 /*
3096 * Send a ret code status message to the sessiond daemon.
3097 *
3098 * Return the sendmsg() return value.
3099 */
3100 int consumer_send_status_msg(int sock, int ret_code)
3101 {
3102 struct lttcomm_consumer_status_msg msg;
3103
3104 msg.ret_code = ret_code;
3105
3106 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3107 }
This page took 0.160102 seconds and 3 git commands to generate.