consumerd: tag metadata channel as being part of a live session
[lttng-tools.git] / src / common / consumer / 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 _LGPL_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 #include <signal.h>
32
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/utils.h>
36 #include <common/compat/poll.h>
37 #include <common/compat/endian.h>
38 #include <common/index/index.h>
39 #include <common/kernel-ctl/kernel-ctl.h>
40 #include <common/sessiond-comm/relayd.h>
41 #include <common/sessiond-comm/sessiond-comm.h>
42 #include <common/kernel-consumer/kernel-consumer.h>
43 #include <common/relayd/relayd.h>
44 #include <common/ust-consumer/ust-consumer.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/consumer/consumer.h>
47 #include <common/consumer/consumer-stream.h>
48 #include <common/consumer/consumer-testpoint.h>
49 #include <common/align.h>
50 #include <common/consumer/consumer-metadata-cache.h>
51
52 struct lttng_consumer_global_data consumer_data = {
53 .stream_count = 0,
54 .need_update = 1,
55 .type = LTTNG_CONSUMER_UNKNOWN,
56 };
57
58 enum consumer_channel_action {
59 CONSUMER_CHANNEL_ADD,
60 CONSUMER_CHANNEL_DEL,
61 CONSUMER_CHANNEL_QUIT,
62 };
63
64 struct consumer_channel_msg {
65 enum consumer_channel_action action;
66 struct lttng_consumer_channel *chan; /* add */
67 uint64_t key; /* del */
68 };
69
70 /*
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the consumer_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (consumer_should_exit()). Read by the
74 * polling threads.
75 */
76 volatile int consumer_quit;
77
78 /*
79 * Global hash table containing respectively metadata and data streams. The
80 * stream element in this ht should only be updated by the metadata poll thread
81 * for the metadata and the data poll thread for the data.
82 */
83 static struct lttng_ht *metadata_ht;
84 static struct lttng_ht *data_ht;
85
86 /*
87 * Notify a thread lttng pipe to poll back again. This usually means that some
88 * global state has changed so we just send back the thread in a poll wait
89 * call.
90 */
91 static void notify_thread_lttng_pipe(struct lttng_pipe *pipe)
92 {
93 struct lttng_consumer_stream *null_stream = NULL;
94
95 assert(pipe);
96
97 (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream));
98 }
99
100 static void notify_health_quit_pipe(int *pipe)
101 {
102 ssize_t ret;
103
104 ret = lttng_write(pipe[1], "4", 1);
105 if (ret < 1) {
106 PERROR("write consumer health quit");
107 }
108 }
109
110 static void notify_channel_pipe(struct lttng_consumer_local_data *ctx,
111 struct lttng_consumer_channel *chan,
112 uint64_t key,
113 enum consumer_channel_action action)
114 {
115 struct consumer_channel_msg msg;
116 ssize_t ret;
117
118 memset(&msg, 0, sizeof(msg));
119
120 msg.action = action;
121 msg.chan = chan;
122 msg.key = key;
123 ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
124 if (ret < sizeof(msg)) {
125 PERROR("notify_channel_pipe write error");
126 }
127 }
128
129 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
130 uint64_t key)
131 {
132 notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL);
133 }
134
135 static int read_channel_pipe(struct lttng_consumer_local_data *ctx,
136 struct lttng_consumer_channel **chan,
137 uint64_t *key,
138 enum consumer_channel_action *action)
139 {
140 struct consumer_channel_msg msg;
141 ssize_t ret;
142
143 ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
144 if (ret < sizeof(msg)) {
145 ret = -1;
146 goto error;
147 }
148 *action = msg.action;
149 *chan = msg.chan;
150 *key = msg.key;
151 error:
152 return (int) ret;
153 }
154
155 /*
156 * Cleanup the stream list of a channel. Those streams are not yet globally
157 * visible
158 */
159 static void clean_channel_stream_list(struct lttng_consumer_channel *channel)
160 {
161 struct lttng_consumer_stream *stream, *stmp;
162
163 assert(channel);
164
165 /* Delete streams that might have been left in the stream list. */
166 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
167 send_node) {
168 cds_list_del(&stream->send_node);
169 /*
170 * Once a stream is added to this list, the buffers were created so we
171 * have a guarantee that this call will succeed. Setting the monitor
172 * mode to 0 so we don't lock nor try to delete the stream from the
173 * global hash table.
174 */
175 stream->monitor = 0;
176 consumer_stream_destroy(stream, NULL);
177 }
178 }
179
180 /*
181 * Find a stream. The consumer_data.lock must be locked during this
182 * call.
183 */
184 static struct lttng_consumer_stream *find_stream(uint64_t key,
185 struct lttng_ht *ht)
186 {
187 struct lttng_ht_iter iter;
188 struct lttng_ht_node_u64 *node;
189 struct lttng_consumer_stream *stream = NULL;
190
191 assert(ht);
192
193 /* -1ULL keys are lookup failures */
194 if (key == (uint64_t) -1ULL) {
195 return NULL;
196 }
197
198 rcu_read_lock();
199
200 lttng_ht_lookup(ht, &key, &iter);
201 node = lttng_ht_iter_get_node_u64(&iter);
202 if (node != NULL) {
203 stream = caa_container_of(node, struct lttng_consumer_stream, node);
204 }
205
206 rcu_read_unlock();
207
208 return stream;
209 }
210
211 static void steal_stream_key(uint64_t key, struct lttng_ht *ht)
212 {
213 struct lttng_consumer_stream *stream;
214
215 rcu_read_lock();
216 stream = find_stream(key, ht);
217 if (stream) {
218 stream->key = (uint64_t) -1ULL;
219 /*
220 * We don't want the lookup to match, but we still need
221 * to iterate on this stream when iterating over the hash table. Just
222 * change the node key.
223 */
224 stream->node.key = (uint64_t) -1ULL;
225 }
226 rcu_read_unlock();
227 }
228
229 /*
230 * Return a channel object for the given key.
231 *
232 * RCU read side lock MUST be acquired before calling this function and
233 * protects the channel ptr.
234 */
235 struct lttng_consumer_channel *consumer_find_channel(uint64_t key)
236 {
237 struct lttng_ht_iter iter;
238 struct lttng_ht_node_u64 *node;
239 struct lttng_consumer_channel *channel = NULL;
240
241 /* -1ULL keys are lookup failures */
242 if (key == (uint64_t) -1ULL) {
243 return NULL;
244 }
245
246 lttng_ht_lookup(consumer_data.channel_ht, &key, &iter);
247 node = lttng_ht_iter_get_node_u64(&iter);
248 if (node != NULL) {
249 channel = caa_container_of(node, struct lttng_consumer_channel, node);
250 }
251
252 return channel;
253 }
254
255 /*
256 * There is a possibility that the consumer does not have enough time between
257 * the close of the channel on the session daemon and the cleanup in here thus
258 * once we have a channel add with an existing key, we know for sure that this
259 * channel will eventually get cleaned up by all streams being closed.
260 *
261 * This function just nullifies the already existing channel key.
262 */
263 static void steal_channel_key(uint64_t key)
264 {
265 struct lttng_consumer_channel *channel;
266
267 rcu_read_lock();
268 channel = consumer_find_channel(key);
269 if (channel) {
270 channel->key = (uint64_t) -1ULL;
271 /*
272 * We don't want the lookup to match, but we still need to iterate on
273 * this channel when iterating over the hash table. Just change the
274 * node key.
275 */
276 channel->node.key = (uint64_t) -1ULL;
277 }
278 rcu_read_unlock();
279 }
280
281 static void free_channel_rcu(struct rcu_head *head)
282 {
283 struct lttng_ht_node_u64 *node =
284 caa_container_of(head, struct lttng_ht_node_u64, head);
285 struct lttng_consumer_channel *channel =
286 caa_container_of(node, struct lttng_consumer_channel, node);
287
288 switch (consumer_data.type) {
289 case LTTNG_CONSUMER_KERNEL:
290 break;
291 case LTTNG_CONSUMER32_UST:
292 case LTTNG_CONSUMER64_UST:
293 lttng_ustconsumer_free_channel(channel);
294 break;
295 default:
296 ERR("Unknown consumer_data type");
297 abort();
298 }
299 free(channel);
300 }
301
302 /*
303 * RCU protected relayd socket pair free.
304 */
305 static void free_relayd_rcu(struct rcu_head *head)
306 {
307 struct lttng_ht_node_u64 *node =
308 caa_container_of(head, struct lttng_ht_node_u64, head);
309 struct consumer_relayd_sock_pair *relayd =
310 caa_container_of(node, struct consumer_relayd_sock_pair, node);
311
312 /*
313 * Close all sockets. This is done in the call RCU since we don't want the
314 * socket fds to be reassigned thus potentially creating bad state of the
315 * relayd object.
316 *
317 * We do not have to lock the control socket mutex here since at this stage
318 * there is no one referencing to this relayd object.
319 */
320 (void) relayd_close(&relayd->control_sock);
321 (void) relayd_close(&relayd->data_sock);
322
323 pthread_mutex_destroy(&relayd->ctrl_sock_mutex);
324 free(relayd);
325 }
326
327 /*
328 * Destroy and free relayd socket pair object.
329 */
330 void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
331 {
332 int ret;
333 struct lttng_ht_iter iter;
334
335 if (relayd == NULL) {
336 return;
337 }
338
339 DBG("Consumer destroy and close relayd socket pair");
340
341 iter.iter.node = &relayd->node.node;
342 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
343 if (ret != 0) {
344 /* We assume the relayd is being or is destroyed */
345 return;
346 }
347
348 /* RCU free() call */
349 call_rcu(&relayd->node.head, free_relayd_rcu);
350 }
351
352 /*
353 * Remove a channel from the global list protected by a mutex. This function is
354 * also responsible for freeing its data structures.
355 */
356 void consumer_del_channel(struct lttng_consumer_channel *channel)
357 {
358 int ret;
359 struct lttng_ht_iter iter;
360
361 DBG("Consumer delete channel key %" PRIu64, channel->key);
362
363 pthread_mutex_lock(&consumer_data.lock);
364 pthread_mutex_lock(&channel->lock);
365
366 /* Destroy streams that might have been left in the stream list. */
367 clean_channel_stream_list(channel);
368
369 if (channel->live_timer_enabled == 1) {
370 consumer_timer_live_stop(channel);
371 }
372
373 switch (consumer_data.type) {
374 case LTTNG_CONSUMER_KERNEL:
375 break;
376 case LTTNG_CONSUMER32_UST:
377 case LTTNG_CONSUMER64_UST:
378 lttng_ustconsumer_del_channel(channel);
379 break;
380 default:
381 ERR("Unknown consumer_data type");
382 assert(0);
383 goto end;
384 }
385
386 rcu_read_lock();
387 iter.iter.node = &channel->node.node;
388 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
389 assert(!ret);
390 rcu_read_unlock();
391
392 call_rcu(&channel->node.head, free_channel_rcu);
393 end:
394 pthread_mutex_unlock(&channel->lock);
395 pthread_mutex_unlock(&consumer_data.lock);
396 }
397
398 /*
399 * Iterate over the relayd hash table and destroy each element. Finally,
400 * destroy the whole hash table.
401 */
402 static void cleanup_relayd_ht(void)
403 {
404 struct lttng_ht_iter iter;
405 struct consumer_relayd_sock_pair *relayd;
406
407 rcu_read_lock();
408
409 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
410 node.node) {
411 consumer_destroy_relayd(relayd);
412 }
413
414 rcu_read_unlock();
415
416 lttng_ht_destroy(consumer_data.relayd_ht);
417 }
418
419 /*
420 * Update the end point status of all streams having the given relayd id.
421 *
422 * It's atomically set without having the stream mutex locked which is fine
423 * because we handle the write/read race with a pipe wakeup for each thread.
424 */
425 static void update_endpoint_status_by_netidx(uint64_t relayd_id,
426 enum consumer_endpoint_status status)
427 {
428 struct lttng_ht_iter iter;
429 struct lttng_consumer_stream *stream;
430
431 DBG("Consumer set delete flag on stream by idx %" PRIu64, relayd_id);
432
433 rcu_read_lock();
434
435 /* Let's begin with metadata */
436 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
437 if (stream->relayd_id == relayd_id) {
438 uatomic_set(&stream->endpoint_status, status);
439 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
440 }
441 }
442
443 /* Follow up by the data streams */
444 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
445 if (stream->relayd_id == relayd_id) {
446 uatomic_set(&stream->endpoint_status, status);
447 DBG("Delete flag set to data stream %d", stream->wait_fd);
448 }
449 }
450 rcu_read_unlock();
451 }
452
453 /*
454 * Cleanup a relayd object by flagging every associated streams for deletion,
455 * destroying the object meaning removing it from the relayd hash table,
456 * closing the sockets and freeing the memory in a RCU call.
457 *
458 * If a local data context is available, notify the threads that the streams'
459 * state have changed.
460 */
461 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd)
462 {
463 uint64_t netidx;
464
465 assert(relayd);
466
467 DBG("Cleaning up relayd object ID %"PRIu64, relayd->id);
468
469 /* Save the net sequence index before destroying the object */
470 netidx = relayd->id;
471
472 /*
473 * Delete the relayd from the relayd hash table, close the sockets and free
474 * the object in a RCU call.
475 */
476 consumer_destroy_relayd(relayd);
477
478 /* Set inactive endpoint to all streams */
479 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
480
481 /*
482 * With a local data context, notify the threads that the streams' state
483 * have changed. The write() action on the pipe acts as an "implicit"
484 * memory barrier ordering the updates of the end point status from the
485 * read of this status which happens AFTER receiving this notify.
486 */
487 notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe);
488 notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe);
489 }
490
491 /*
492 * Flag a relayd socket pair for destruction. Destroy it if the refcount
493 * reaches zero.
494 *
495 * RCU read side lock MUST be aquired before calling this function.
496 */
497 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
498 {
499 assert(relayd);
500
501 /* Set destroy flag for this object */
502 uatomic_set(&relayd->destroy_flag, 1);
503
504 /* Destroy the relayd if refcount is 0 */
505 if (uatomic_read(&relayd->refcount) == 0) {
506 consumer_destroy_relayd(relayd);
507 }
508 }
509
510 /*
511 * Completly destroy stream from every visiable data structure and the given
512 * hash table if one.
513 *
514 * One this call returns, the stream object is not longer usable nor visible.
515 */
516 void consumer_del_stream(struct lttng_consumer_stream *stream,
517 struct lttng_ht *ht)
518 {
519 consumer_stream_destroy(stream, ht);
520 }
521
522 /*
523 * XXX naming of del vs destroy is all mixed up.
524 */
525 void consumer_del_stream_for_data(struct lttng_consumer_stream *stream)
526 {
527 consumer_stream_destroy(stream, data_ht);
528 }
529
530 void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream)
531 {
532 consumer_stream_destroy(stream, metadata_ht);
533 }
534
535 struct lttng_consumer_stream *consumer_allocate_stream(
536 struct lttng_consumer_channel *channel,
537 uint64_t channel_key,
538 uint64_t stream_key,
539 enum lttng_consumer_stream_state state,
540 const char *channel_name,
541 uid_t uid,
542 gid_t gid,
543 uint64_t relayd_id,
544 uint64_t session_id,
545 int cpu,
546 int *alloc_ret,
547 enum consumer_channel_type type,
548 unsigned int monitor)
549 {
550 int ret;
551 struct lttng_consumer_stream *stream;
552
553 stream = zmalloc(sizeof(*stream));
554 if (stream == NULL) {
555 PERROR("malloc struct lttng_consumer_stream");
556 ret = -ENOMEM;
557 goto end;
558 }
559
560 rcu_read_lock();
561 stream->chan = channel;
562 stream->key = stream_key;
563 stream->out_fd = -1;
564 stream->out_fd_offset = 0;
565 stream->output_written = 0;
566 stream->state = state;
567 stream->uid = uid;
568 stream->gid = gid;
569 stream->relayd_id = relayd_id;
570 stream->session_id = session_id;
571 stream->monitor = monitor;
572 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
573 stream->index_file = NULL;
574 stream->last_sequence_number = -1ULL;
575 pthread_mutex_init(&stream->lock, NULL);
576 pthread_mutex_init(&stream->metadata_timer_lock, NULL);
577
578 /* If channel is the metadata, flag this stream as metadata. */
579 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
580 stream->metadata_flag = 1;
581 /* Metadata is flat out. */
582 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
583 /* Live rendez-vous point. */
584 pthread_cond_init(&stream->metadata_rdv, NULL);
585 pthread_mutex_init(&stream->metadata_rdv_lock, NULL);
586 } else {
587 /* Format stream name to <channel_name>_<cpu_number> */
588 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
589 channel_name, cpu);
590 if (ret < 0) {
591 PERROR("snprintf stream name");
592 goto error;
593 }
594 }
595
596 /* Key is always the wait_fd for streams. */
597 lttng_ht_node_init_u64(&stream->node, stream->key);
598
599 /* Init node per channel id key */
600 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
601
602 /* Init session id node with the stream session id */
603 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
604
605 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
606 " relayd_id %" PRIu64 ", session_id %" PRIu64,
607 stream->name, stream->key, channel_key,
608 stream->relayd_id, stream->session_id);
609
610 rcu_read_unlock();
611 return stream;
612
613 error:
614 rcu_read_unlock();
615 free(stream);
616 end:
617 if (alloc_ret) {
618 *alloc_ret = ret;
619 }
620 return NULL;
621 }
622
623 /*
624 * Add a stream to the global list protected by a mutex.
625 */
626 int consumer_add_data_stream(struct lttng_consumer_stream *stream)
627 {
628 struct lttng_ht *ht = data_ht;
629 int ret = 0;
630
631 assert(stream);
632 assert(ht);
633
634 DBG3("Adding consumer stream %" PRIu64, stream->key);
635
636 pthread_mutex_lock(&consumer_data.lock);
637 pthread_mutex_lock(&stream->chan->lock);
638 pthread_mutex_lock(&stream->chan->timer_lock);
639 pthread_mutex_lock(&stream->lock);
640 rcu_read_lock();
641
642 /* Steal stream identifier to avoid having streams with the same key */
643 steal_stream_key(stream->key, ht);
644
645 lttng_ht_add_unique_u64(ht, &stream->node);
646
647 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
648 &stream->node_channel_id);
649
650 /*
651 * Add stream to the stream_list_ht of the consumer data. No need to steal
652 * the key since the HT does not use it and we allow to add redundant keys
653 * into this table.
654 */
655 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
656
657 /*
658 * When nb_init_stream_left reaches 0, we don't need to trigger any action
659 * in terms of destroying the associated channel, because the action that
660 * causes the count to become 0 also causes a stream to be added. The
661 * channel deletion will thus be triggered by the following removal of this
662 * stream.
663 */
664 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
665 /* Increment refcount before decrementing nb_init_stream_left */
666 cmm_smp_wmb();
667 uatomic_dec(&stream->chan->nb_init_stream_left);
668 }
669
670 /* Update consumer data once the node is inserted. */
671 consumer_data.stream_count++;
672 consumer_data.need_update = 1;
673
674 rcu_read_unlock();
675 pthread_mutex_unlock(&stream->lock);
676 pthread_mutex_unlock(&stream->chan->timer_lock);
677 pthread_mutex_unlock(&stream->chan->lock);
678 pthread_mutex_unlock(&consumer_data.lock);
679
680 return ret;
681 }
682
683 void consumer_del_data_stream(struct lttng_consumer_stream *stream)
684 {
685 consumer_del_stream(stream, data_ht);
686 }
687
688 /*
689 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
690 * be acquired before calling this.
691 */
692 static int add_relayd(struct consumer_relayd_sock_pair *relayd)
693 {
694 int ret = 0;
695 struct lttng_ht_node_u64 *node;
696 struct lttng_ht_iter iter;
697
698 assert(relayd);
699
700 lttng_ht_lookup(consumer_data.relayd_ht,
701 &relayd->id, &iter);
702 node = lttng_ht_iter_get_node_u64(&iter);
703 if (node != NULL) {
704 goto end;
705 }
706 lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node);
707
708 end:
709 return ret;
710 }
711
712 /*
713 * Allocate and return a consumer relayd socket.
714 */
715 static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
716 uint64_t relayd_id)
717 {
718 struct consumer_relayd_sock_pair *obj = NULL;
719
720 /* net sequence index of -1 is a failure */
721 if (relayd_id == (uint64_t) -1ULL) {
722 goto error;
723 }
724
725 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
726 if (obj == NULL) {
727 PERROR("zmalloc relayd sock");
728 goto error;
729 }
730
731 obj->id = relayd_id;
732 obj->refcount = 0;
733 obj->destroy_flag = 0;
734 obj->control_sock.sock.fd = -1;
735 obj->data_sock.sock.fd = -1;
736 lttng_ht_node_init_u64(&obj->node, obj->id);
737 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
738
739 error:
740 return obj;
741 }
742
743 /*
744 * Find a relayd socket pair in the global consumer data.
745 *
746 * Return the object if found else NULL.
747 * RCU read-side lock must be held across this call and while using the
748 * returned object.
749 */
750 struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key)
751 {
752 struct lttng_ht_iter iter;
753 struct lttng_ht_node_u64 *node;
754 struct consumer_relayd_sock_pair *relayd = NULL;
755
756 /* Negative keys are lookup failures */
757 if (key == (uint64_t) -1ULL) {
758 goto error;
759 }
760
761 lttng_ht_lookup(consumer_data.relayd_ht, &key,
762 &iter);
763 node = lttng_ht_iter_get_node_u64(&iter);
764 if (node != NULL) {
765 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
766 }
767
768 error:
769 return relayd;
770 }
771
772 /*
773 * Find a relayd and send the stream
774 *
775 * Returns 0 on success, < 0 on error
776 */
777 int consumer_send_relayd_stream(struct lttng_consumer_stream *stream,
778 char *path)
779 {
780 int ret = 0;
781 struct consumer_relayd_sock_pair *relayd;
782
783 assert(stream);
784 assert(stream->relayd_id != -1ULL);
785 assert(path);
786
787 /* The stream is not metadata. Get relayd reference if exists. */
788 rcu_read_lock();
789 relayd = consumer_find_relayd(stream->relayd_id);
790 if (relayd != NULL) {
791 /* Add stream on the relayd */
792 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
793 ret = relayd_add_stream(&relayd->control_sock, stream->name,
794 path, &stream->relayd_stream_id,
795 stream->chan->tracefile_size, stream->chan->tracefile_count);
796 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
797 if (ret < 0) {
798 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->id);
799 lttng_consumer_cleanup_relayd(relayd);
800 goto end;
801 }
802
803 uatomic_inc(&relayd->refcount);
804 stream->sent_to_relayd = 1;
805 } else {
806 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.",
807 stream->key, stream->relayd_id);
808 ret = -1;
809 goto end;
810 }
811
812 DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64,
813 stream->name, stream->key, stream->relayd_id);
814
815 end:
816 rcu_read_unlock();
817 return ret;
818 }
819
820 /*
821 * Find a relayd and send the streams sent message
822 *
823 * Returns 0 on success, < 0 on error
824 */
825 int consumer_send_relayd_streams_sent(uint64_t relayd_id)
826 {
827 int ret = 0;
828 struct consumer_relayd_sock_pair *relayd;
829
830 assert(relayd_id != -1ULL);
831
832 /* The stream is not metadata. Get relayd reference if exists. */
833 rcu_read_lock();
834 relayd = consumer_find_relayd(relayd_id);
835 if (relayd != NULL) {
836 /* Add stream on the relayd */
837 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
838 ret = relayd_streams_sent(&relayd->control_sock);
839 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
840 if (ret < 0) {
841 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->id);
842 lttng_consumer_cleanup_relayd(relayd);
843 goto end;
844 }
845 } else {
846 ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.",
847 relayd_id);
848 ret = -1;
849 goto end;
850 }
851
852 ret = 0;
853 DBG("All streams sent relayd id %" PRIu64, relayd_id);
854
855 end:
856 rcu_read_unlock();
857 return ret;
858 }
859
860 /*
861 * Find a relayd and close the stream
862 */
863 void close_relayd_stream(struct lttng_consumer_stream *stream)
864 {
865 struct consumer_relayd_sock_pair *relayd;
866
867 /* The stream is not metadata. Get relayd reference if exists. */
868 rcu_read_lock();
869 relayd = consumer_find_relayd(stream->relayd_id);
870 if (relayd) {
871 consumer_stream_relayd_close(stream, relayd);
872 }
873 rcu_read_unlock();
874 }
875
876 /*
877 * Handle stream for relayd transmission if the stream applies for network
878 * streaming where the net sequence index is set.
879 *
880 * Return destination file descriptor or negative value on error.
881 */
882 static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
883 size_t data_size, unsigned long padding,
884 struct consumer_relayd_sock_pair *relayd)
885 {
886 int outfd = -1, ret;
887 struct lttcomm_relayd_data_hdr data_hdr;
888
889 /* Safety net */
890 assert(stream);
891 assert(relayd);
892
893 /* Reset data header */
894 memset(&data_hdr, 0, sizeof(data_hdr));
895
896 if (stream->metadata_flag) {
897 /* Caller MUST acquire the relayd control socket lock */
898 ret = relayd_send_metadata(&relayd->control_sock, data_size);
899 if (ret < 0) {
900 goto error;
901 }
902
903 /* Metadata are always sent on the control socket. */
904 outfd = relayd->control_sock.sock.fd;
905 } else {
906 /* Set header with stream information */
907 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
908 data_hdr.data_size = htobe32(data_size);
909 data_hdr.padding_size = htobe32(padding);
910 /*
911 * Note that net_seq_num below is assigned with the *current* value of
912 * next_net_seq_num and only after that the next_net_seq_num will be
913 * increment. This is why when issuing a command on the relayd using
914 * this next value, 1 should always be substracted in order to compare
915 * the last seen sequence number on the relayd side to the last sent.
916 */
917 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num);
918 /* Other fields are zeroed previously */
919
920 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
921 sizeof(data_hdr));
922 if (ret < 0) {
923 goto error;
924 }
925
926 ++stream->next_net_seq_num;
927
928 /* Set to go on data socket */
929 outfd = relayd->data_sock.sock.fd;
930 }
931
932 error:
933 return outfd;
934 }
935
936 /*
937 * Allocate and return a new lttng_consumer_channel object using the given key
938 * to initialize the hash table node.
939 *
940 * On error, return NULL.
941 */
942 struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
943 uint64_t session_id,
944 const char *pathname,
945 const char *name,
946 uid_t uid,
947 gid_t gid,
948 uint64_t relayd_id,
949 enum lttng_event_output output,
950 uint64_t tracefile_size,
951 uint64_t tracefile_count,
952 uint64_t session_id_per_pid,
953 unsigned int monitor,
954 unsigned int live_timer_interval,
955 bool is_in_live_session,
956 const char *root_shm_path,
957 const char *shm_path)
958 {
959 struct lttng_consumer_channel *channel;
960
961 channel = zmalloc(sizeof(*channel));
962 if (channel == NULL) {
963 PERROR("malloc struct lttng_consumer_channel");
964 goto end;
965 }
966
967 channel->key = key;
968 channel->refcount = 0;
969 channel->session_id = session_id;
970 channel->session_id_per_pid = session_id_per_pid;
971 channel->uid = uid;
972 channel->gid = gid;
973 channel->relayd_id = relayd_id;
974 channel->tracefile_size = tracefile_size;
975 channel->tracefile_count = tracefile_count;
976 channel->monitor = monitor;
977 channel->live_timer_interval = live_timer_interval;
978 channel->is_live = is_in_live_session;
979 pthread_mutex_init(&channel->lock, NULL);
980 pthread_mutex_init(&channel->timer_lock, NULL);
981
982 switch (output) {
983 case LTTNG_EVENT_SPLICE:
984 channel->output = CONSUMER_CHANNEL_SPLICE;
985 break;
986 case LTTNG_EVENT_MMAP:
987 channel->output = CONSUMER_CHANNEL_MMAP;
988 break;
989 default:
990 assert(0);
991 free(channel);
992 channel = NULL;
993 goto end;
994 }
995
996 /*
997 * In monitor mode, the streams associated with the channel will be put in
998 * a special list ONLY owned by this channel. So, the refcount is set to 1
999 * here meaning that the channel itself has streams that are referenced.
1000 *
1001 * On a channel deletion, once the channel is no longer visible, the
1002 * refcount is decremented and checked for a zero value to delete it. With
1003 * streams in no monitor mode, it will now be safe to destroy the channel.
1004 */
1005 if (!channel->monitor) {
1006 channel->refcount = 1;
1007 }
1008
1009 strncpy(channel->pathname, pathname, sizeof(channel->pathname));
1010 channel->pathname[sizeof(channel->pathname) - 1] = '\0';
1011
1012 strncpy(channel->name, name, sizeof(channel->name));
1013 channel->name[sizeof(channel->name) - 1] = '\0';
1014
1015 if (root_shm_path) {
1016 strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path));
1017 channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0';
1018 }
1019 if (shm_path) {
1020 strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path));
1021 channel->shm_path[sizeof(channel->shm_path) - 1] = '\0';
1022 }
1023
1024 lttng_ht_node_init_u64(&channel->node, channel->key);
1025
1026 channel->wait_fd = -1;
1027
1028 CDS_INIT_LIST_HEAD(&channel->streams.head);
1029
1030 DBG("Allocated channel (key %" PRIu64 ")", channel->key);
1031
1032 end:
1033 return channel;
1034 }
1035
1036 /*
1037 * Add a channel to the global list protected by a mutex.
1038 *
1039 * Always return 0 indicating success.
1040 */
1041 int consumer_add_channel(struct lttng_consumer_channel *channel,
1042 struct lttng_consumer_local_data *ctx)
1043 {
1044 pthread_mutex_lock(&consumer_data.lock);
1045 pthread_mutex_lock(&channel->lock);
1046 pthread_mutex_lock(&channel->timer_lock);
1047
1048 /*
1049 * This gives us a guarantee that the channel we are about to add to the
1050 * channel hash table will be unique. See this function comment on the why
1051 * we need to steel the channel key at this stage.
1052 */
1053 steal_channel_key(channel->key);
1054
1055 rcu_read_lock();
1056 lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node);
1057 rcu_read_unlock();
1058
1059 pthread_mutex_unlock(&channel->timer_lock);
1060 pthread_mutex_unlock(&channel->lock);
1061 pthread_mutex_unlock(&consumer_data.lock);
1062
1063 if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) {
1064 notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD);
1065 }
1066
1067 return 0;
1068 }
1069
1070 /*
1071 * Allocate the pollfd structure and the local view of the out fds to avoid
1072 * doing a lookup in the linked list and concurrency issues when writing is
1073 * needed. Called with consumer_data.lock held.
1074 *
1075 * Returns the number of fds in the structures.
1076 */
1077 static int update_poll_array(struct lttng_consumer_local_data *ctx,
1078 struct pollfd **pollfd, struct lttng_consumer_stream **local_stream,
1079 struct lttng_ht *ht, int *nb_inactive_fd)
1080 {
1081 int i = 0;
1082 struct lttng_ht_iter iter;
1083 struct lttng_consumer_stream *stream;
1084
1085 assert(ctx);
1086 assert(ht);
1087 assert(pollfd);
1088 assert(local_stream);
1089
1090 DBG("Updating poll fd array");
1091 *nb_inactive_fd = 0;
1092 rcu_read_lock();
1093 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1094 /*
1095 * Only active streams with an active end point can be added to the
1096 * poll set and local stream storage of the thread.
1097 *
1098 * There is a potential race here for endpoint_status to be updated
1099 * just after the check. However, this is OK since the stream(s) will
1100 * be deleted once the thread is notified that the end point state has
1101 * changed where this function will be called back again.
1102 *
1103 * We track the number of inactive FDs because they still need to be
1104 * closed by the polling thread after a wakeup on the data_pipe or
1105 * metadata_pipe.
1106 */
1107 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
1108 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
1109 (*nb_inactive_fd)++;
1110 continue;
1111 }
1112 /*
1113 * This clobbers way too much the debug output. Uncomment that if you
1114 * need it for debugging purposes.
1115 *
1116 * DBG("Active FD %d", stream->wait_fd);
1117 */
1118 (*pollfd)[i].fd = stream->wait_fd;
1119 (*pollfd)[i].events = POLLIN | POLLPRI;
1120 local_stream[i] = stream;
1121 i++;
1122 }
1123 rcu_read_unlock();
1124
1125 /*
1126 * Insert the consumer_data_pipe at the end of the array and don't
1127 * increment i so nb_fd is the number of real FD.
1128 */
1129 (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe);
1130 (*pollfd)[i].events = POLLIN | POLLPRI;
1131
1132 (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe);
1133 (*pollfd)[i + 1].events = POLLIN | POLLPRI;
1134 return i;
1135 }
1136
1137 /*
1138 * Poll on the should_quit pipe and the command socket return -1 on
1139 * error, 1 if should exit, 0 if data is available on the command socket
1140 */
1141 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1142 {
1143 int num_rdy;
1144
1145 restart:
1146 num_rdy = poll(consumer_sockpoll, 2, -1);
1147 if (num_rdy == -1) {
1148 /*
1149 * Restart interrupted system call.
1150 */
1151 if (errno == EINTR) {
1152 goto restart;
1153 }
1154 PERROR("Poll error");
1155 return -1;
1156 }
1157 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
1158 DBG("consumer_should_quit wake up");
1159 return 1;
1160 }
1161 return 0;
1162 }
1163
1164 /*
1165 * Set the error socket.
1166 */
1167 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
1168 int sock)
1169 {
1170 ctx->consumer_error_socket = sock;
1171 }
1172
1173 /*
1174 * Set the command socket path.
1175 */
1176 void lttng_consumer_set_command_sock_path(
1177 struct lttng_consumer_local_data *ctx, char *sock)
1178 {
1179 ctx->consumer_command_sock_path = sock;
1180 }
1181
1182 /*
1183 * Send return code to the session daemon.
1184 * If the socket is not defined, we return 0, it is not a fatal error
1185 */
1186 int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd)
1187 {
1188 if (ctx->consumer_error_socket > 0) {
1189 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1190 sizeof(enum lttcomm_sessiond_command));
1191 }
1192
1193 return 0;
1194 }
1195
1196 /*
1197 * Close all the tracefiles and stream fds and MUST be called when all
1198 * instances are destroyed i.e. when all threads were joined and are ended.
1199 */
1200 void lttng_consumer_cleanup(void)
1201 {
1202 struct lttng_ht_iter iter;
1203 struct lttng_consumer_channel *channel;
1204
1205 rcu_read_lock();
1206
1207 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel,
1208 node.node) {
1209 consumer_del_channel(channel);
1210 }
1211
1212 rcu_read_unlock();
1213
1214 lttng_ht_destroy(consumer_data.channel_ht);
1215
1216 cleanup_relayd_ht();
1217
1218 lttng_ht_destroy(consumer_data.stream_per_chan_id_ht);
1219
1220 /*
1221 * This HT contains streams that are freed by either the metadata thread or
1222 * the data thread so we do *nothing* on the hash table and simply destroy
1223 * it.
1224 */
1225 lttng_ht_destroy(consumer_data.stream_list_ht);
1226 }
1227
1228 /*
1229 * Called from signal handler.
1230 */
1231 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1232 {
1233 ssize_t ret;
1234
1235 consumer_quit = 1;
1236 ret = lttng_write(ctx->consumer_should_quit[1], "4", 1);
1237 if (ret < 1) {
1238 PERROR("write consumer quit");
1239 }
1240
1241 DBG("Consumer flag that it should quit");
1242 }
1243
1244
1245 /*
1246 * Flush pending writes to trace output disk file.
1247 */
1248 static
1249 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1250 off_t orig_offset)
1251 {
1252 int ret;
1253 int outfd = stream->out_fd;
1254
1255 /*
1256 * This does a blocking write-and-wait on any page that belongs to the
1257 * subbuffer prior to the one we just wrote.
1258 * Don't care about error values, as these are just hints and ways to
1259 * limit the amount of page cache used.
1260 */
1261 if (orig_offset < stream->max_sb_size) {
1262 return;
1263 }
1264 lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size,
1265 stream->max_sb_size,
1266 SYNC_FILE_RANGE_WAIT_BEFORE
1267 | SYNC_FILE_RANGE_WRITE
1268 | SYNC_FILE_RANGE_WAIT_AFTER);
1269 /*
1270 * Give hints to the kernel about how we access the file:
1271 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1272 * we write it.
1273 *
1274 * We need to call fadvise again after the file grows because the
1275 * kernel does not seem to apply fadvise to non-existing parts of the
1276 * file.
1277 *
1278 * Call fadvise _after_ having waited for the page writeback to
1279 * complete because the dirty page writeback semantic is not well
1280 * defined. So it can be expected to lead to lower throughput in
1281 * streaming.
1282 */
1283 ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size,
1284 stream->max_sb_size, POSIX_FADV_DONTNEED);
1285 if (ret && ret != -ENOSYS) {
1286 errno = ret;
1287 PERROR("posix_fadvise on fd %i", outfd);
1288 }
1289 }
1290
1291 /*
1292 * Initialise the necessary environnement :
1293 * - create a new context
1294 * - create the poll_pipe
1295 * - create the should_quit pipe (for signal handler)
1296 * - create the thread pipe (for splice)
1297 *
1298 * Takes a function pointer as argument, this function is called when data is
1299 * available on a buffer. This function is responsible to do the
1300 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1301 * buffer configuration and then kernctl_put_next_subbuf at the end.
1302 *
1303 * Returns a pointer to the new context or NULL on error.
1304 */
1305 struct lttng_consumer_local_data *lttng_consumer_create(
1306 enum lttng_consumer_type type,
1307 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
1308 struct lttng_consumer_local_data *ctx),
1309 int (*recv_channel)(struct lttng_consumer_channel *channel),
1310 int (*recv_stream)(struct lttng_consumer_stream *stream),
1311 int (*update_stream)(uint64_t stream_key, uint32_t state))
1312 {
1313 int ret;
1314 struct lttng_consumer_local_data *ctx;
1315
1316 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1317 consumer_data.type == type);
1318 consumer_data.type = type;
1319
1320 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
1321 if (ctx == NULL) {
1322 PERROR("allocating context");
1323 goto error;
1324 }
1325
1326 ctx->consumer_error_socket = -1;
1327 ctx->consumer_metadata_socket = -1;
1328 pthread_mutex_init(&ctx->metadata_socket_lock, NULL);
1329 /* assign the callbacks */
1330 ctx->on_buffer_ready = buffer_ready;
1331 ctx->on_recv_channel = recv_channel;
1332 ctx->on_recv_stream = recv_stream;
1333 ctx->on_update_stream = update_stream;
1334
1335 ctx->consumer_data_pipe = lttng_pipe_open(0);
1336 if (!ctx->consumer_data_pipe) {
1337 goto error_poll_pipe;
1338 }
1339
1340 ctx->consumer_wakeup_pipe = lttng_pipe_open(0);
1341 if (!ctx->consumer_wakeup_pipe) {
1342 goto error_wakeup_pipe;
1343 }
1344
1345 ret = pipe(ctx->consumer_should_quit);
1346 if (ret < 0) {
1347 PERROR("Error creating recv pipe");
1348 goto error_quit_pipe;
1349 }
1350
1351 ret = pipe(ctx->consumer_channel_pipe);
1352 if (ret < 0) {
1353 PERROR("Error creating channel pipe");
1354 goto error_channel_pipe;
1355 }
1356
1357 ctx->consumer_metadata_pipe = lttng_pipe_open(0);
1358 if (!ctx->consumer_metadata_pipe) {
1359 goto error_metadata_pipe;
1360 }
1361
1362 return ctx;
1363
1364 error_metadata_pipe:
1365 utils_close_pipe(ctx->consumer_channel_pipe);
1366 error_channel_pipe:
1367 utils_close_pipe(ctx->consumer_should_quit);
1368 error_quit_pipe:
1369 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
1370 error_wakeup_pipe:
1371 lttng_pipe_destroy(ctx->consumer_data_pipe);
1372 error_poll_pipe:
1373 free(ctx);
1374 error:
1375 return NULL;
1376 }
1377
1378 /*
1379 * Iterate over all streams of the hashtable and free them properly.
1380 */
1381 static void destroy_data_stream_ht(struct lttng_ht *ht)
1382 {
1383 struct lttng_ht_iter iter;
1384 struct lttng_consumer_stream *stream;
1385
1386 if (ht == NULL) {
1387 return;
1388 }
1389
1390 rcu_read_lock();
1391 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1392 /*
1393 * Ignore return value since we are currently cleaning up so any error
1394 * can't be handled.
1395 */
1396 (void) consumer_del_stream(stream, ht);
1397 }
1398 rcu_read_unlock();
1399
1400 lttng_ht_destroy(ht);
1401 }
1402
1403 /*
1404 * Iterate over all streams of the metadata hashtable and free them
1405 * properly.
1406 */
1407 static void destroy_metadata_stream_ht(struct lttng_ht *ht)
1408 {
1409 struct lttng_ht_iter iter;
1410 struct lttng_consumer_stream *stream;
1411
1412 if (ht == NULL) {
1413 return;
1414 }
1415
1416 rcu_read_lock();
1417 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1418 /*
1419 * Ignore return value since we are currently cleaning up so any error
1420 * can't be handled.
1421 */
1422 (void) consumer_del_metadata_stream(stream, ht);
1423 }
1424 rcu_read_unlock();
1425
1426 lttng_ht_destroy(ht);
1427 }
1428
1429 /*
1430 * Close all fds associated with the instance and free the context.
1431 */
1432 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1433 {
1434 int ret;
1435
1436 DBG("Consumer destroying it. Closing everything.");
1437
1438 if (!ctx) {
1439 return;
1440 }
1441
1442 destroy_data_stream_ht(data_ht);
1443 destroy_metadata_stream_ht(metadata_ht);
1444
1445 ret = close(ctx->consumer_error_socket);
1446 if (ret) {
1447 PERROR("close");
1448 }
1449 ret = close(ctx->consumer_metadata_socket);
1450 if (ret) {
1451 PERROR("close");
1452 }
1453 utils_close_pipe(ctx->consumer_channel_pipe);
1454 lttng_pipe_destroy(ctx->consumer_data_pipe);
1455 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1456 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
1457 utils_close_pipe(ctx->consumer_should_quit);
1458
1459 unlink(ctx->consumer_command_sock_path);
1460 free(ctx);
1461 }
1462
1463 /*
1464 * Write the metadata stream id on the specified file descriptor.
1465 */
1466 static int write_relayd_metadata_id(int fd,
1467 struct lttng_consumer_stream *stream,
1468 struct consumer_relayd_sock_pair *relayd, unsigned long padding)
1469 {
1470 ssize_t ret;
1471 struct lttcomm_relayd_metadata_payload hdr;
1472
1473 hdr.stream_id = htobe64(stream->relayd_stream_id);
1474 hdr.padding_size = htobe32(padding);
1475 ret = lttng_write(fd, (void *) &hdr, sizeof(hdr));
1476 if (ret < sizeof(hdr)) {
1477 /*
1478 * This error means that the fd's end is closed so ignore the PERROR
1479 * not to clubber the error output since this can happen in a normal
1480 * code path.
1481 */
1482 if (errno != EPIPE) {
1483 PERROR("write metadata stream id");
1484 }
1485 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
1486 /*
1487 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1488 * handle writting the missing part so report that as an error and
1489 * don't lie to the caller.
1490 */
1491 ret = -1;
1492 goto end;
1493 }
1494 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1495 stream->relayd_stream_id, padding);
1496
1497 end:
1498 return (int) ret;
1499 }
1500
1501 /*
1502 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1503 * core function for writing trace buffers to either the local filesystem or
1504 * the network.
1505 *
1506 * It must be called with the stream lock held.
1507 *
1508 * Careful review MUST be put if any changes occur!
1509 *
1510 * Returns the number of bytes written
1511 */
1512 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1513 struct lttng_consumer_local_data *ctx,
1514 struct lttng_consumer_stream *stream,
1515 const struct lttng_buffer_view *buffer,
1516 unsigned long padding,
1517 struct ctf_packet_index *index)
1518 {
1519 ssize_t ret = 0;
1520 off_t orig_offset = stream->out_fd_offset;
1521 /* Default is on the disk */
1522 int outfd = stream->out_fd;
1523 struct consumer_relayd_sock_pair *relayd = NULL;
1524 unsigned int relayd_hang_up = 0;
1525 const size_t subbuf_content_size = buffer->size - padding;
1526 size_t write_len;
1527
1528 /* RCU lock for the relayd pointer */
1529 rcu_read_lock();
1530
1531 /* Flag that the current stream if set for network streaming. */
1532 if (stream->relayd_id != (uint64_t) -1ULL) {
1533 relayd = consumer_find_relayd(stream->relayd_id);
1534 if (relayd == NULL) {
1535 ret = -EPIPE;
1536 goto end;
1537 }
1538 }
1539
1540 /* Handle stream on the relayd if the output is on the network */
1541 if (relayd) {
1542 unsigned long netlen = subbuf_content_size;
1543
1544 /*
1545 * Lock the control socket for the complete duration of the function
1546 * since from this point on we will use the socket.
1547 */
1548 if (stream->metadata_flag) {
1549 /* Metadata requires the control socket. */
1550 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1551 if (stream->reset_metadata_flag) {
1552 ret = relayd_reset_metadata(&relayd->control_sock,
1553 stream->relayd_stream_id,
1554 stream->metadata_version);
1555 if (ret < 0) {
1556 relayd_hang_up = 1;
1557 goto write_error;
1558 }
1559 stream->reset_metadata_flag = 0;
1560 }
1561 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
1562 }
1563
1564 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
1565 if (ret < 0) {
1566 relayd_hang_up = 1;
1567 goto write_error;
1568 }
1569 /* Use the returned socket. */
1570 outfd = ret;
1571
1572 /* Write metadata stream id before payload */
1573 if (stream->metadata_flag) {
1574 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
1575 if (ret < 0) {
1576 relayd_hang_up = 1;
1577 goto write_error;
1578 }
1579 }
1580
1581 write_len = subbuf_content_size;
1582 } else {
1583 /* No streaming; we have to write the full padding. */
1584 if (stream->metadata_flag && stream->reset_metadata_flag) {
1585 ret = utils_truncate_stream_file(stream->out_fd, 0);
1586 if (ret < 0) {
1587 ERR("Reset metadata file");
1588 goto end;
1589 }
1590 stream->reset_metadata_flag = 0;
1591 }
1592
1593 /*
1594 * Check if we need to change the tracefile before writing the packet.
1595 */
1596 if (stream->chan->tracefile_size > 0 &&
1597 (stream->tracefile_size_current + buffer->size) >
1598 stream->chan->tracefile_size) {
1599 ret = utils_rotate_stream_file(stream->chan->pathname,
1600 stream->name, stream->chan->tracefile_size,
1601 stream->chan->tracefile_count, stream->uid, stream->gid,
1602 stream->out_fd, &(stream->tracefile_count_current),
1603 &stream->out_fd);
1604 if (ret < 0) {
1605 ERR("Rotating output file");
1606 goto end;
1607 }
1608 outfd = stream->out_fd;
1609
1610 if (stream->index_file) {
1611 lttng_index_file_put(stream->index_file);
1612 stream->index_file = lttng_index_file_create(stream->chan->pathname,
1613 stream->name, stream->uid, stream->gid,
1614 stream->chan->tracefile_size,
1615 stream->tracefile_count_current,
1616 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1617 if (!stream->index_file) {
1618 goto end;
1619 }
1620 }
1621
1622 /* Reset current size because we just perform a rotation. */
1623 stream->tracefile_size_current = 0;
1624 stream->out_fd_offset = 0;
1625 orig_offset = 0;
1626 }
1627 stream->tracefile_size_current += buffer->size;
1628 if (index) {
1629 index->offset = htobe64(stream->out_fd_offset);
1630 }
1631
1632 write_len = buffer->size;
1633 }
1634
1635 /*
1636 * This call guarantee that len or less is returned. It's impossible to
1637 * receive a ret value that is bigger than len.
1638 */
1639 ret = lttng_write(outfd, buffer->data, write_len);
1640 DBG("Consumer mmap write() ret %zd (len %lu)", ret, write_len);
1641 if (ret < 0 || ((size_t) ret != write_len)) {
1642 /*
1643 * Report error to caller if nothing was written else at least send the
1644 * amount written.
1645 */
1646 if (ret < 0) {
1647 ret = -errno;
1648 }
1649 relayd_hang_up = 1;
1650
1651 /* Socket operation failed. We consider the relayd dead */
1652 if (errno == EPIPE || errno == EINVAL || errno == EBADF) {
1653 /*
1654 * This is possible if the fd is closed on the other side
1655 * (outfd) or any write problem. It can be verbose a bit for a
1656 * normal execution if for instance the relayd is stopped
1657 * abruptly. This can happen so set this to a DBG statement.
1658 */
1659 DBG("Consumer mmap write detected relayd hang up");
1660 } else {
1661 /* Unhandled error, print it and stop function right now. */
1662 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret,
1663 write_len);
1664 }
1665 goto write_error;
1666 }
1667 stream->output_written += ret;
1668
1669 /* This call is useless on a socket so better save a syscall. */
1670 if (!relayd) {
1671 /* This won't block, but will start writeout asynchronously */
1672 lttng_sync_file_range(outfd, stream->out_fd_offset, write_len,
1673 SYNC_FILE_RANGE_WRITE);
1674 stream->out_fd_offset += write_len;
1675 lttng_consumer_sync_trace_file(stream, orig_offset);
1676 }
1677
1678 write_error:
1679 /*
1680 * This is a special case that the relayd has closed its socket. Let's
1681 * cleanup the relayd object and all associated streams.
1682 */
1683 if (relayd && relayd_hang_up) {
1684 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->id);
1685 lttng_consumer_cleanup_relayd(relayd);
1686 }
1687
1688 end:
1689 /* Unlock only if ctrl socket used */
1690 if (relayd && stream->metadata_flag) {
1691 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1692 }
1693
1694 rcu_read_unlock();
1695 return ret;
1696 }
1697
1698 /*
1699 * Splice the data from the ring buffer to the tracefile.
1700 *
1701 * It must be called with the stream lock held.
1702 *
1703 * Returns the number of bytes spliced.
1704 */
1705 ssize_t lttng_consumer_on_read_subbuffer_splice(
1706 struct lttng_consumer_local_data *ctx,
1707 struct lttng_consumer_stream *stream, unsigned long len,
1708 unsigned long padding,
1709 struct ctf_packet_index *index)
1710 {
1711 ssize_t ret = 0, written = 0, ret_splice = 0;
1712 loff_t offset = 0;
1713 off_t orig_offset = stream->out_fd_offset;
1714 int fd = stream->wait_fd;
1715 /* Default is on the disk */
1716 int outfd = stream->out_fd;
1717 struct consumer_relayd_sock_pair *relayd = NULL;
1718 int *splice_pipe;
1719 unsigned int relayd_hang_up = 0;
1720
1721 switch (consumer_data.type) {
1722 case LTTNG_CONSUMER_KERNEL:
1723 break;
1724 case LTTNG_CONSUMER32_UST:
1725 case LTTNG_CONSUMER64_UST:
1726 /* Not supported for user space tracing */
1727 return -ENOSYS;
1728 default:
1729 ERR("Unknown consumer_data type");
1730 assert(0);
1731 }
1732
1733 /* RCU lock for the relayd pointer */
1734 rcu_read_lock();
1735
1736 /* Flag that the current stream if set for network streaming. */
1737 if (stream->relayd_id != (uint64_t) -1ULL) {
1738 relayd = consumer_find_relayd(stream->relayd_id);
1739 if (relayd == NULL) {
1740 written = -ret;
1741 goto end;
1742 }
1743 }
1744 splice_pipe = stream->splice_pipe;
1745
1746 /* Write metadata stream id before payload */
1747 if (relayd) {
1748 unsigned long total_len = len;
1749
1750 if (stream->metadata_flag) {
1751 /*
1752 * Lock the control socket for the complete duration of the function
1753 * since from this point on we will use the socket.
1754 */
1755 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1756
1757 if (stream->reset_metadata_flag) {
1758 ret = relayd_reset_metadata(&relayd->control_sock,
1759 stream->relayd_stream_id,
1760 stream->metadata_version);
1761 if (ret < 0) {
1762 relayd_hang_up = 1;
1763 goto write_error;
1764 }
1765 stream->reset_metadata_flag = 0;
1766 }
1767 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1768 padding);
1769 if (ret < 0) {
1770 written = ret;
1771 relayd_hang_up = 1;
1772 goto write_error;
1773 }
1774
1775 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1776 }
1777
1778 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1779 if (ret < 0) {
1780 written = ret;
1781 relayd_hang_up = 1;
1782 goto write_error;
1783 }
1784 /* Use the returned socket. */
1785 outfd = ret;
1786 } else {
1787 /* No streaming, we have to set the len with the full padding */
1788 len += padding;
1789
1790 if (stream->metadata_flag && stream->reset_metadata_flag) {
1791 ret = utils_truncate_stream_file(stream->out_fd, 0);
1792 if (ret < 0) {
1793 ERR("Reset metadata file");
1794 goto end;
1795 }
1796 stream->reset_metadata_flag = 0;
1797 }
1798 /*
1799 * Check if we need to change the tracefile before writing the packet.
1800 */
1801 if (stream->chan->tracefile_size > 0 &&
1802 (stream->tracefile_size_current + len) >
1803 stream->chan->tracefile_size) {
1804 ret = utils_rotate_stream_file(stream->chan->pathname,
1805 stream->name, stream->chan->tracefile_size,
1806 stream->chan->tracefile_count, stream->uid, stream->gid,
1807 stream->out_fd, &(stream->tracefile_count_current),
1808 &stream->out_fd);
1809 if (ret < 0) {
1810 written = ret;
1811 ERR("Rotating output file");
1812 goto end;
1813 }
1814 outfd = stream->out_fd;
1815
1816 if (stream->index_file) {
1817 lttng_index_file_put(stream->index_file);
1818 stream->index_file = lttng_index_file_create(stream->chan->pathname,
1819 stream->name, stream->uid, stream->gid,
1820 stream->chan->tracefile_size,
1821 stream->tracefile_count_current,
1822 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1823 if (!stream->index_file) {
1824 goto end;
1825 }
1826 }
1827
1828 /* Reset current size because we just perform a rotation. */
1829 stream->tracefile_size_current = 0;
1830 stream->out_fd_offset = 0;
1831 orig_offset = 0;
1832 }
1833 stream->tracefile_size_current += len;
1834 index->offset = htobe64(stream->out_fd_offset);
1835 }
1836
1837 while (len > 0) {
1838 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1839 (unsigned long)offset, len, fd, splice_pipe[1]);
1840 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
1841 SPLICE_F_MOVE | SPLICE_F_MORE);
1842 DBG("splice chan to pipe, ret %zd", ret_splice);
1843 if (ret_splice < 0) {
1844 ret = errno;
1845 written = -ret;
1846 PERROR("Error in relay splice");
1847 goto splice_error;
1848 }
1849
1850 /* Handle stream on the relayd if the output is on the network */
1851 if (relayd && stream->metadata_flag) {
1852 size_t metadata_payload_size =
1853 sizeof(struct lttcomm_relayd_metadata_payload);
1854
1855 /* Update counter to fit the spliced data */
1856 ret_splice += metadata_payload_size;
1857 len += metadata_payload_size;
1858 /*
1859 * We do this so the return value can match the len passed as
1860 * argument to this function.
1861 */
1862 written -= metadata_payload_size;
1863 }
1864
1865 /* Splice data out */
1866 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
1867 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1868 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1869 outfd, ret_splice);
1870 if (ret_splice < 0) {
1871 ret = errno;
1872 written = -ret;
1873 relayd_hang_up = 1;
1874 goto write_error;
1875 } else if (ret_splice > len) {
1876 /*
1877 * We don't expect this code path to be executed but you never know
1878 * so this is an extra protection agains a buggy splice().
1879 */
1880 ret = errno;
1881 written += ret_splice;
1882 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice,
1883 len);
1884 goto splice_error;
1885 } else {
1886 /* All good, update current len and continue. */
1887 len -= ret_splice;
1888 }
1889
1890 /* This call is useless on a socket so better save a syscall. */
1891 if (!relayd) {
1892 /* This won't block, but will start writeout asynchronously */
1893 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1894 SYNC_FILE_RANGE_WRITE);
1895 stream->out_fd_offset += ret_splice;
1896 }
1897 stream->output_written += ret_splice;
1898 written += ret_splice;
1899 }
1900 if (!relayd) {
1901 lttng_consumer_sync_trace_file(stream, orig_offset);
1902 }
1903 goto end;
1904
1905 write_error:
1906 /*
1907 * This is a special case that the relayd has closed its socket. Let's
1908 * cleanup the relayd object and all associated streams.
1909 */
1910 if (relayd && relayd_hang_up) {
1911 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->id);
1912 lttng_consumer_cleanup_relayd(relayd);
1913 /* Skip splice error so the consumer does not fail */
1914 goto end;
1915 }
1916
1917 splice_error:
1918 /* send the appropriate error description to sessiond */
1919 switch (ret) {
1920 case EINVAL:
1921 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
1922 break;
1923 case ENOMEM:
1924 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
1925 break;
1926 case ESPIPE:
1927 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
1928 break;
1929 }
1930
1931 end:
1932 if (relayd && stream->metadata_flag) {
1933 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1934 }
1935
1936 rcu_read_unlock();
1937 return written;
1938 }
1939
1940 /*
1941 * Take a snapshot for a specific fd
1942 *
1943 * Returns 0 on success, < 0 on error
1944 */
1945 int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
1946 {
1947 switch (consumer_data.type) {
1948 case LTTNG_CONSUMER_KERNEL:
1949 return lttng_kconsumer_take_snapshot(stream);
1950 case LTTNG_CONSUMER32_UST:
1951 case LTTNG_CONSUMER64_UST:
1952 return lttng_ustconsumer_take_snapshot(stream);
1953 default:
1954 ERR("Unknown consumer_data type");
1955 assert(0);
1956 return -ENOSYS;
1957 }
1958 }
1959
1960 /*
1961 * Get the produced position
1962 *
1963 * Returns 0 on success, < 0 on error
1964 */
1965 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
1966 unsigned long *pos)
1967 {
1968 switch (consumer_data.type) {
1969 case LTTNG_CONSUMER_KERNEL:
1970 return lttng_kconsumer_get_produced_snapshot(stream, pos);
1971 case LTTNG_CONSUMER32_UST:
1972 case LTTNG_CONSUMER64_UST:
1973 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
1974 default:
1975 ERR("Unknown consumer_data type");
1976 assert(0);
1977 return -ENOSYS;
1978 }
1979 }
1980
1981 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1982 int sock, struct pollfd *consumer_sockpoll)
1983 {
1984 switch (consumer_data.type) {
1985 case LTTNG_CONSUMER_KERNEL:
1986 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1987 case LTTNG_CONSUMER32_UST:
1988 case LTTNG_CONSUMER64_UST:
1989 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1990 default:
1991 ERR("Unknown consumer_data type");
1992 assert(0);
1993 return -ENOSYS;
1994 }
1995 }
1996
1997 void lttng_consumer_close_all_metadata(void)
1998 {
1999 switch (consumer_data.type) {
2000 case LTTNG_CONSUMER_KERNEL:
2001 /*
2002 * The Kernel consumer has a different metadata scheme so we don't
2003 * close anything because the stream will be closed by the session
2004 * daemon.
2005 */
2006 break;
2007 case LTTNG_CONSUMER32_UST:
2008 case LTTNG_CONSUMER64_UST:
2009 /*
2010 * Close all metadata streams. The metadata hash table is passed and
2011 * this call iterates over it by closing all wakeup fd. This is safe
2012 * because at this point we are sure that the metadata producer is
2013 * either dead or blocked.
2014 */
2015 lttng_ustconsumer_close_all_metadata(metadata_ht);
2016 break;
2017 default:
2018 ERR("Unknown consumer_data type");
2019 assert(0);
2020 }
2021 }
2022
2023 /*
2024 * Clean up a metadata stream and free its memory.
2025 */
2026 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
2027 struct lttng_ht *ht)
2028 {
2029 struct lttng_consumer_channel *free_chan = NULL;
2030
2031 assert(stream);
2032 /*
2033 * This call should NEVER receive regular stream. It must always be
2034 * metadata stream and this is crucial for data structure synchronization.
2035 */
2036 assert(stream->metadata_flag);
2037
2038 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
2039
2040 pthread_mutex_lock(&consumer_data.lock);
2041 pthread_mutex_lock(&stream->chan->lock);
2042 pthread_mutex_lock(&stream->lock);
2043 if (stream->chan->metadata_cache) {
2044 /* Only applicable to userspace consumers. */
2045 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2046 }
2047
2048 /* Remove any reference to that stream. */
2049 consumer_stream_delete(stream, ht);
2050
2051 /* Close down everything including the relayd if one. */
2052 consumer_stream_close(stream);
2053 /* Destroy tracer buffers of the stream. */
2054 consumer_stream_destroy_buffers(stream);
2055
2056 /* Atomically decrement channel refcount since other threads can use it. */
2057 if (!uatomic_sub_return(&stream->chan->refcount, 1)
2058 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
2059 /* Go for channel deletion! */
2060 free_chan = stream->chan;
2061 }
2062
2063 /*
2064 * Nullify the stream reference so it is not used after deletion. The
2065 * channel lock MUST be acquired before being able to check for a NULL
2066 * pointer value.
2067 */
2068 stream->chan->metadata_stream = NULL;
2069
2070 if (stream->chan->metadata_cache) {
2071 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2072 }
2073 pthread_mutex_unlock(&stream->lock);
2074 pthread_mutex_unlock(&stream->chan->lock);
2075 pthread_mutex_unlock(&consumer_data.lock);
2076
2077 if (free_chan) {
2078 consumer_del_channel(free_chan);
2079 }
2080
2081 consumer_stream_free(stream);
2082 }
2083
2084 /*
2085 * Action done with the metadata stream when adding it to the consumer internal
2086 * data structures to handle it.
2087 */
2088 int consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
2089 {
2090 struct lttng_ht *ht = metadata_ht;
2091 int ret = 0;
2092 struct lttng_ht_iter iter;
2093 struct lttng_ht_node_u64 *node;
2094
2095 assert(stream);
2096 assert(ht);
2097
2098 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
2099
2100 pthread_mutex_lock(&consumer_data.lock);
2101 pthread_mutex_lock(&stream->chan->lock);
2102 pthread_mutex_lock(&stream->chan->timer_lock);
2103 pthread_mutex_lock(&stream->lock);
2104
2105 /*
2106 * From here, refcounts are updated so be _careful_ when returning an error
2107 * after this point.
2108 */
2109
2110 rcu_read_lock();
2111
2112 /*
2113 * Lookup the stream just to make sure it does not exist in our internal
2114 * state. This should NEVER happen.
2115 */
2116 lttng_ht_lookup(ht, &stream->key, &iter);
2117 node = lttng_ht_iter_get_node_u64(&iter);
2118 assert(!node);
2119
2120 /*
2121 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2122 * in terms of destroying the associated channel, because the action that
2123 * causes the count to become 0 also causes a stream to be added. The
2124 * channel deletion will thus be triggered by the following removal of this
2125 * stream.
2126 */
2127 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
2128 /* Increment refcount before decrementing nb_init_stream_left */
2129 cmm_smp_wmb();
2130 uatomic_dec(&stream->chan->nb_init_stream_left);
2131 }
2132
2133 lttng_ht_add_unique_u64(ht, &stream->node);
2134
2135 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
2136 &stream->node_channel_id);
2137
2138 /*
2139 * Add stream to the stream_list_ht of the consumer data. No need to steal
2140 * the key since the HT does not use it and we allow to add redundant keys
2141 * into this table.
2142 */
2143 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
2144
2145 rcu_read_unlock();
2146
2147 pthread_mutex_unlock(&stream->lock);
2148 pthread_mutex_unlock(&stream->chan->lock);
2149 pthread_mutex_unlock(&stream->chan->timer_lock);
2150 pthread_mutex_unlock(&consumer_data.lock);
2151 return ret;
2152 }
2153
2154 /*
2155 * Delete data stream that are flagged for deletion (endpoint_status).
2156 */
2157 static void validate_endpoint_status_data_stream(void)
2158 {
2159 struct lttng_ht_iter iter;
2160 struct lttng_consumer_stream *stream;
2161
2162 DBG("Consumer delete flagged data stream");
2163
2164 rcu_read_lock();
2165 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2166 /* Validate delete flag of the stream */
2167 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2168 continue;
2169 }
2170 /* Delete it right now */
2171 consumer_del_stream(stream, data_ht);
2172 }
2173 rcu_read_unlock();
2174 }
2175
2176 /*
2177 * Delete metadata stream that are flagged for deletion (endpoint_status).
2178 */
2179 static void validate_endpoint_status_metadata_stream(
2180 struct lttng_poll_event *pollset)
2181 {
2182 struct lttng_ht_iter iter;
2183 struct lttng_consumer_stream *stream;
2184
2185 DBG("Consumer delete flagged metadata stream");
2186
2187 assert(pollset);
2188
2189 rcu_read_lock();
2190 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2191 /* Validate delete flag of the stream */
2192 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2193 continue;
2194 }
2195 /*
2196 * Remove from pollset so the metadata thread can continue without
2197 * blocking on a deleted stream.
2198 */
2199 lttng_poll_del(pollset, stream->wait_fd);
2200
2201 /* Delete it right now */
2202 consumer_del_metadata_stream(stream, metadata_ht);
2203 }
2204 rcu_read_unlock();
2205 }
2206
2207 /*
2208 * Thread polls on metadata file descriptor and write them on disk or on the
2209 * network.
2210 */
2211 void *consumer_thread_metadata_poll(void *data)
2212 {
2213 int ret, i, pollfd, err = -1;
2214 uint32_t revents, nb_fd;
2215 struct lttng_consumer_stream *stream = NULL;
2216 struct lttng_ht_iter iter;
2217 struct lttng_ht_node_u64 *node;
2218 struct lttng_poll_event events;
2219 struct lttng_consumer_local_data *ctx = data;
2220 ssize_t len;
2221
2222 rcu_register_thread();
2223
2224 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA);
2225
2226 if (testpoint(consumerd_thread_metadata)) {
2227 goto error_testpoint;
2228 }
2229
2230 health_code_update();
2231
2232 DBG("Thread metadata poll started");
2233
2234 /* Size is set to 1 for the consumer_metadata pipe */
2235 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2236 if (ret < 0) {
2237 ERR("Poll set creation failed");
2238 goto end_poll;
2239 }
2240
2241 ret = lttng_poll_add(&events,
2242 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN);
2243 if (ret < 0) {
2244 goto end;
2245 }
2246
2247 /* Main loop */
2248 DBG("Metadata main loop started");
2249
2250 while (1) {
2251 restart:
2252 health_code_update();
2253 health_poll_entry();
2254 DBG("Metadata poll wait");
2255 ret = lttng_poll_wait(&events, -1);
2256 DBG("Metadata poll return from wait with %d fd(s)",
2257 LTTNG_POLL_GETNB(&events));
2258 health_poll_exit();
2259 DBG("Metadata event caught in thread");
2260 if (ret < 0) {
2261 if (errno == EINTR) {
2262 ERR("Poll EINTR caught");
2263 goto restart;
2264 }
2265 if (LTTNG_POLL_GETNB(&events) == 0) {
2266 err = 0; /* All is OK */
2267 }
2268 goto end;
2269 }
2270
2271 nb_fd = ret;
2272
2273 /* From here, the event is a metadata wait fd */
2274 for (i = 0; i < nb_fd; i++) {
2275 health_code_update();
2276
2277 revents = LTTNG_POLL_GETEV(&events, i);
2278 pollfd = LTTNG_POLL_GETFD(&events, i);
2279
2280 if (!revents) {
2281 /* No activity for this FD (poll implementation). */
2282 continue;
2283 }
2284
2285 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
2286 if (revents & LPOLLIN) {
2287 ssize_t pipe_len;
2288
2289 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2290 &stream, sizeof(stream));
2291 if (pipe_len < sizeof(stream)) {
2292 if (pipe_len < 0) {
2293 PERROR("read metadata stream");
2294 }
2295 /*
2296 * Remove the pipe from the poll set and continue the loop
2297 * since their might be data to consume.
2298 */
2299 lttng_poll_del(&events,
2300 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2301 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2302 continue;
2303 }
2304
2305 /* A NULL stream means that the state has changed. */
2306 if (stream == NULL) {
2307 /* Check for deleted streams. */
2308 validate_endpoint_status_metadata_stream(&events);
2309 goto restart;
2310 }
2311
2312 DBG("Adding metadata stream %d to poll set",
2313 stream->wait_fd);
2314
2315 /* Add metadata stream to the global poll events list */
2316 lttng_poll_add(&events, stream->wait_fd,
2317 LPOLLIN | LPOLLPRI | LPOLLHUP);
2318 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2319 DBG("Metadata thread pipe hung up");
2320 /*
2321 * Remove the pipe from the poll set and continue the loop
2322 * since their might be data to consume.
2323 */
2324 lttng_poll_del(&events,
2325 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2326 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2327 continue;
2328 } else {
2329 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2330 goto end;
2331 }
2332
2333 /* Handle other stream */
2334 continue;
2335 }
2336
2337 rcu_read_lock();
2338 {
2339 uint64_t tmp_id = (uint64_t) pollfd;
2340
2341 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2342 }
2343 node = lttng_ht_iter_get_node_u64(&iter);
2344 assert(node);
2345
2346 stream = caa_container_of(node, struct lttng_consumer_stream,
2347 node);
2348
2349 if (revents & (LPOLLIN | LPOLLPRI)) {
2350 /* Get the data out of the metadata file descriptor */
2351 DBG("Metadata available on fd %d", pollfd);
2352 assert(stream->wait_fd == pollfd);
2353
2354 do {
2355 health_code_update();
2356
2357 len = ctx->on_buffer_ready(stream, ctx);
2358 /*
2359 * We don't check the return value here since if we get
2360 * a negative len, it means an error occurred thus we
2361 * simply remove it from the poll set and free the
2362 * stream.
2363 */
2364 } while (len > 0);
2365
2366 /* It's ok to have an unavailable sub-buffer */
2367 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2368 /* Clean up stream from consumer and free it. */
2369 lttng_poll_del(&events, stream->wait_fd);
2370 consumer_del_metadata_stream(stream, metadata_ht);
2371 }
2372 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2373 DBG("Metadata fd %d is hup|err.", pollfd);
2374 if (!stream->hangup_flush_done
2375 && (consumer_data.type == LTTNG_CONSUMER32_UST
2376 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2377 DBG("Attempting to flush and consume the UST buffers");
2378 lttng_ustconsumer_on_stream_hangup(stream);
2379
2380 /* We just flushed the stream now read it. */
2381 do {
2382 health_code_update();
2383
2384 len = ctx->on_buffer_ready(stream, ctx);
2385 /*
2386 * We don't check the return value here since if we get
2387 * a negative len, it means an error occurred thus we
2388 * simply remove it from the poll set and free the
2389 * stream.
2390 */
2391 } while (len > 0);
2392 }
2393
2394 lttng_poll_del(&events, stream->wait_fd);
2395 /*
2396 * This call update the channel states, closes file descriptors
2397 * and securely free the stream.
2398 */
2399 consumer_del_metadata_stream(stream, metadata_ht);
2400 } else {
2401 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2402 rcu_read_unlock();
2403 goto end;
2404 }
2405 /* Release RCU lock for the stream looked up */
2406 rcu_read_unlock();
2407 }
2408 }
2409
2410 /* All is OK */
2411 err = 0;
2412 end:
2413 DBG("Metadata poll thread exiting");
2414
2415 lttng_poll_clean(&events);
2416 end_poll:
2417 error_testpoint:
2418 if (err) {
2419 health_error();
2420 ERR("Health error occurred in %s", __func__);
2421 }
2422 health_unregister(health_consumerd);
2423 rcu_unregister_thread();
2424 return NULL;
2425 }
2426
2427 /*
2428 * This thread polls the fds in the set to consume the data and write
2429 * it to tracefile if necessary.
2430 */
2431 void *consumer_thread_data_poll(void *data)
2432 {
2433 int num_rdy, num_hup, high_prio, ret, i, err = -1;
2434 struct pollfd *pollfd = NULL;
2435 /* local view of the streams */
2436 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2437 /* local view of consumer_data.fds_count */
2438 int nb_fd = 0;
2439 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2440 int nb_inactive_fd = 0;
2441 struct lttng_consumer_local_data *ctx = data;
2442 ssize_t len;
2443
2444 rcu_register_thread();
2445
2446 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA);
2447
2448 if (testpoint(consumerd_thread_data)) {
2449 goto error_testpoint;
2450 }
2451
2452 health_code_update();
2453
2454 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2455 if (local_stream == NULL) {
2456 PERROR("local_stream malloc");
2457 goto end;
2458 }
2459
2460 while (1) {
2461 health_code_update();
2462
2463 high_prio = 0;
2464 num_hup = 0;
2465
2466 /*
2467 * the fds set has been updated, we need to update our
2468 * local array as well
2469 */
2470 pthread_mutex_lock(&consumer_data.lock);
2471 if (consumer_data.need_update) {
2472 free(pollfd);
2473 pollfd = NULL;
2474
2475 free(local_stream);
2476 local_stream = NULL;
2477
2478 /*
2479 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2480 * wake up pipe.
2481 */
2482 pollfd = zmalloc((consumer_data.stream_count + 2) * sizeof(struct pollfd));
2483 if (pollfd == NULL) {
2484 PERROR("pollfd malloc");
2485 pthread_mutex_unlock(&consumer_data.lock);
2486 goto end;
2487 }
2488
2489 local_stream = zmalloc((consumer_data.stream_count + 2) *
2490 sizeof(struct lttng_consumer_stream *));
2491 if (local_stream == NULL) {
2492 PERROR("local_stream malloc");
2493 pthread_mutex_unlock(&consumer_data.lock);
2494 goto end;
2495 }
2496 ret = update_poll_array(ctx, &pollfd, local_stream,
2497 data_ht, &nb_inactive_fd);
2498 if (ret < 0) {
2499 ERR("Error in allocating pollfd or local_outfds");
2500 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2501 pthread_mutex_unlock(&consumer_data.lock);
2502 goto end;
2503 }
2504 nb_fd = ret;
2505 consumer_data.need_update = 0;
2506 }
2507 pthread_mutex_unlock(&consumer_data.lock);
2508
2509 /* No FDs and consumer_quit, consumer_cleanup the thread */
2510 if (nb_fd == 0 && consumer_quit == 1 && nb_inactive_fd == 0) {
2511 err = 0; /* All is OK */
2512 goto end;
2513 }
2514 /* poll on the array of fds */
2515 restart:
2516 DBG("polling on %d fd", nb_fd + 2);
2517 health_poll_entry();
2518 num_rdy = poll(pollfd, nb_fd + 2, -1);
2519 health_poll_exit();
2520 DBG("poll num_rdy : %d", num_rdy);
2521 if (num_rdy == -1) {
2522 /*
2523 * Restart interrupted system call.
2524 */
2525 if (errno == EINTR) {
2526 goto restart;
2527 }
2528 PERROR("Poll error");
2529 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2530 goto end;
2531 } else if (num_rdy == 0) {
2532 DBG("Polling thread timed out");
2533 goto end;
2534 }
2535
2536 /*
2537 * If the consumer_data_pipe triggered poll go directly to the
2538 * beginning of the loop to update the array. We want to prioritize
2539 * array update over low-priority reads.
2540 */
2541 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2542 ssize_t pipe_readlen;
2543
2544 DBG("consumer_data_pipe wake up");
2545 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2546 &new_stream, sizeof(new_stream));
2547 if (pipe_readlen < sizeof(new_stream)) {
2548 PERROR("Consumer data pipe");
2549 /* Continue so we can at least handle the current stream(s). */
2550 continue;
2551 }
2552
2553 /*
2554 * If the stream is NULL, just ignore it. It's also possible that
2555 * the sessiond poll thread changed the consumer_quit state and is
2556 * waking us up to test it.
2557 */
2558 if (new_stream == NULL) {
2559 validate_endpoint_status_data_stream();
2560 continue;
2561 }
2562
2563 /* Continue to update the local streams and handle prio ones */
2564 continue;
2565 }
2566
2567 /* Handle wakeup pipe. */
2568 if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) {
2569 char dummy;
2570 ssize_t pipe_readlen;
2571
2572 pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy,
2573 sizeof(dummy));
2574 if (pipe_readlen < 0) {
2575 PERROR("Consumer data wakeup pipe");
2576 }
2577 /* We've been awakened to handle stream(s). */
2578 ctx->has_wakeup = 0;
2579 }
2580
2581 /* Take care of high priority channels first. */
2582 for (i = 0; i < nb_fd; i++) {
2583 health_code_update();
2584
2585 if (local_stream[i] == NULL) {
2586 continue;
2587 }
2588 if (pollfd[i].revents & POLLPRI) {
2589 DBG("Urgent read on fd %d", pollfd[i].fd);
2590 high_prio = 1;
2591 len = ctx->on_buffer_ready(local_stream[i], ctx);
2592 /* it's ok to have an unavailable sub-buffer */
2593 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2594 /* Clean the stream and free it. */
2595 consumer_del_stream(local_stream[i], data_ht);
2596 local_stream[i] = NULL;
2597 } else if (len > 0) {
2598 local_stream[i]->data_read = 1;
2599 }
2600 }
2601 }
2602
2603 /*
2604 * If we read high prio channel in this loop, try again
2605 * for more high prio data.
2606 */
2607 if (high_prio) {
2608 continue;
2609 }
2610
2611 /* Take care of low priority channels. */
2612 for (i = 0; i < nb_fd; i++) {
2613 health_code_update();
2614
2615 if (local_stream[i] == NULL) {
2616 continue;
2617 }
2618 if ((pollfd[i].revents & POLLIN) ||
2619 local_stream[i]->hangup_flush_done ||
2620 local_stream[i]->has_data) {
2621 DBG("Normal read on fd %d", pollfd[i].fd);
2622 len = ctx->on_buffer_ready(local_stream[i], ctx);
2623 /* it's ok to have an unavailable sub-buffer */
2624 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2625 /* Clean the stream and free it. */
2626 consumer_del_stream(local_stream[i], data_ht);
2627 local_stream[i] = NULL;
2628 } else if (len > 0) {
2629 local_stream[i]->data_read = 1;
2630 }
2631 }
2632 }
2633
2634 /* Handle hangup and errors */
2635 for (i = 0; i < nb_fd; i++) {
2636 health_code_update();
2637
2638 if (local_stream[i] == NULL) {
2639 continue;
2640 }
2641 if (!local_stream[i]->hangup_flush_done
2642 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2643 && (consumer_data.type == LTTNG_CONSUMER32_UST
2644 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2645 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2646 pollfd[i].fd);
2647 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2648 /* Attempt read again, for the data we just flushed. */
2649 local_stream[i]->data_read = 1;
2650 }
2651 /*
2652 * If the poll flag is HUP/ERR/NVAL and we have
2653 * read no data in this pass, we can remove the
2654 * stream from its hash table.
2655 */
2656 if ((pollfd[i].revents & POLLHUP)) {
2657 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2658 if (!local_stream[i]->data_read) {
2659 consumer_del_stream(local_stream[i], data_ht);
2660 local_stream[i] = NULL;
2661 num_hup++;
2662 }
2663 } else if (pollfd[i].revents & POLLERR) {
2664 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2665 if (!local_stream[i]->data_read) {
2666 consumer_del_stream(local_stream[i], data_ht);
2667 local_stream[i] = NULL;
2668 num_hup++;
2669 }
2670 } else if (pollfd[i].revents & POLLNVAL) {
2671 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2672 if (!local_stream[i]->data_read) {
2673 consumer_del_stream(local_stream[i], data_ht);
2674 local_stream[i] = NULL;
2675 num_hup++;
2676 }
2677 }
2678 if (local_stream[i] != NULL) {
2679 local_stream[i]->data_read = 0;
2680 }
2681 }
2682 }
2683 /* All is OK */
2684 err = 0;
2685 end:
2686 DBG("polling thread exiting");
2687 free(pollfd);
2688 free(local_stream);
2689
2690 /*
2691 * Close the write side of the pipe so epoll_wait() in
2692 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2693 * read side of the pipe. If we close them both, epoll_wait strangely does
2694 * not return and could create a endless wait period if the pipe is the
2695 * only tracked fd in the poll set. The thread will take care of closing
2696 * the read side.
2697 */
2698 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
2699
2700 error_testpoint:
2701 if (err) {
2702 health_error();
2703 ERR("Health error occurred in %s", __func__);
2704 }
2705 health_unregister(health_consumerd);
2706
2707 rcu_unregister_thread();
2708 return NULL;
2709 }
2710
2711 /*
2712 * Close wake-up end of each stream belonging to the channel. This will
2713 * allow the poll() on the stream read-side to detect when the
2714 * write-side (application) finally closes them.
2715 */
2716 static
2717 void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2718 {
2719 struct lttng_ht *ht;
2720 struct lttng_consumer_stream *stream;
2721 struct lttng_ht_iter iter;
2722
2723 ht = consumer_data.stream_per_chan_id_ht;
2724
2725 rcu_read_lock();
2726 cds_lfht_for_each_entry_duplicate(ht->ht,
2727 ht->hash_fct(&channel->key, lttng_ht_seed),
2728 ht->match_fct, &channel->key,
2729 &iter.iter, stream, node_channel_id.node) {
2730 /*
2731 * Protect against teardown with mutex.
2732 */
2733 pthread_mutex_lock(&stream->lock);
2734 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2735 goto next;
2736 }
2737 switch (consumer_data.type) {
2738 case LTTNG_CONSUMER_KERNEL:
2739 break;
2740 case LTTNG_CONSUMER32_UST:
2741 case LTTNG_CONSUMER64_UST:
2742 if (stream->metadata_flag) {
2743 /* Safe and protected by the stream lock. */
2744 lttng_ustconsumer_close_metadata(stream->chan);
2745 } else {
2746 /*
2747 * Note: a mutex is taken internally within
2748 * liblttng-ust-ctl to protect timer wakeup_fd
2749 * use from concurrent close.
2750 */
2751 lttng_ustconsumer_close_stream_wakeup(stream);
2752 }
2753 break;
2754 default:
2755 ERR("Unknown consumer_data type");
2756 assert(0);
2757 }
2758 next:
2759 pthread_mutex_unlock(&stream->lock);
2760 }
2761 rcu_read_unlock();
2762 }
2763
2764 static void destroy_channel_ht(struct lttng_ht *ht)
2765 {
2766 struct lttng_ht_iter iter;
2767 struct lttng_consumer_channel *channel;
2768 int ret;
2769
2770 if (ht == NULL) {
2771 return;
2772 }
2773
2774 rcu_read_lock();
2775 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2776 ret = lttng_ht_del(ht, &iter);
2777 assert(ret != 0);
2778 }
2779 rcu_read_unlock();
2780
2781 lttng_ht_destroy(ht);
2782 }
2783
2784 /*
2785 * This thread polls the channel fds to detect when they are being
2786 * closed. It closes all related streams if the channel is detected as
2787 * closed. It is currently only used as a shim layer for UST because the
2788 * consumerd needs to keep the per-stream wakeup end of pipes open for
2789 * periodical flush.
2790 */
2791 void *consumer_thread_channel_poll(void *data)
2792 {
2793 int ret, i, pollfd, err = -1;
2794 uint32_t revents, nb_fd;
2795 struct lttng_consumer_channel *chan = NULL;
2796 struct lttng_ht_iter iter;
2797 struct lttng_ht_node_u64 *node;
2798 struct lttng_poll_event events;
2799 struct lttng_consumer_local_data *ctx = data;
2800 struct lttng_ht *channel_ht;
2801
2802 rcu_register_thread();
2803
2804 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL);
2805
2806 if (testpoint(consumerd_thread_channel)) {
2807 goto error_testpoint;
2808 }
2809
2810 health_code_update();
2811
2812 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2813 if (!channel_ht) {
2814 /* ENOMEM at this point. Better to bail out. */
2815 goto end_ht;
2816 }
2817
2818 DBG("Thread channel poll started");
2819
2820 /* Size is set to 1 for the consumer_channel pipe */
2821 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2822 if (ret < 0) {
2823 ERR("Poll set creation failed");
2824 goto end_poll;
2825 }
2826
2827 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2828 if (ret < 0) {
2829 goto end;
2830 }
2831
2832 /* Main loop */
2833 DBG("Channel main loop started");
2834
2835 while (1) {
2836 restart:
2837 health_code_update();
2838 DBG("Channel poll wait");
2839 health_poll_entry();
2840 ret = lttng_poll_wait(&events, -1);
2841 DBG("Channel poll return from wait with %d fd(s)",
2842 LTTNG_POLL_GETNB(&events));
2843 health_poll_exit();
2844 DBG("Channel event caught in thread");
2845 if (ret < 0) {
2846 if (errno == EINTR) {
2847 ERR("Poll EINTR caught");
2848 goto restart;
2849 }
2850 if (LTTNG_POLL_GETNB(&events) == 0) {
2851 err = 0; /* All is OK */
2852 }
2853 goto end;
2854 }
2855
2856 nb_fd = ret;
2857
2858 /* From here, the event is a channel wait fd */
2859 for (i = 0; i < nb_fd; i++) {
2860 health_code_update();
2861
2862 revents = LTTNG_POLL_GETEV(&events, i);
2863 pollfd = LTTNG_POLL_GETFD(&events, i);
2864
2865 if (!revents) {
2866 /* No activity for this FD (poll implementation). */
2867 continue;
2868 }
2869
2870 if (pollfd == ctx->consumer_channel_pipe[0]) {
2871 if (revents & LPOLLIN) {
2872 enum consumer_channel_action action;
2873 uint64_t key;
2874
2875 ret = read_channel_pipe(ctx, &chan, &key, &action);
2876 if (ret <= 0) {
2877 if (ret < 0) {
2878 ERR("Error reading channel pipe");
2879 }
2880 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2881 continue;
2882 }
2883
2884 switch (action) {
2885 case CONSUMER_CHANNEL_ADD:
2886 DBG("Adding channel %d to poll set",
2887 chan->wait_fd);
2888
2889 lttng_ht_node_init_u64(&chan->wait_fd_node,
2890 chan->wait_fd);
2891 rcu_read_lock();
2892 lttng_ht_add_unique_u64(channel_ht,
2893 &chan->wait_fd_node);
2894 rcu_read_unlock();
2895 /* Add channel to the global poll events list */
2896 lttng_poll_add(&events, chan->wait_fd,
2897 LPOLLERR | LPOLLHUP);
2898 break;
2899 case CONSUMER_CHANNEL_DEL:
2900 {
2901 /*
2902 * This command should never be called if the channel
2903 * has streams monitored by either the data or metadata
2904 * thread. The consumer only notify this thread with a
2905 * channel del. command if it receives a destroy
2906 * channel command from the session daemon that send it
2907 * if a command prior to the GET_CHANNEL failed.
2908 */
2909
2910 rcu_read_lock();
2911 chan = consumer_find_channel(key);
2912 if (!chan) {
2913 rcu_read_unlock();
2914 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
2915 break;
2916 }
2917 lttng_poll_del(&events, chan->wait_fd);
2918 iter.iter.node = &chan->wait_fd_node.node;
2919 ret = lttng_ht_del(channel_ht, &iter);
2920 assert(ret == 0);
2921
2922 switch (consumer_data.type) {
2923 case LTTNG_CONSUMER_KERNEL:
2924 break;
2925 case LTTNG_CONSUMER32_UST:
2926 case LTTNG_CONSUMER64_UST:
2927 health_code_update();
2928 /* Destroy streams that might have been left in the stream list. */
2929 clean_channel_stream_list(chan);
2930 break;
2931 default:
2932 ERR("Unknown consumer_data type");
2933 assert(0);
2934 }
2935
2936 /*
2937 * Release our own refcount. Force channel deletion even if
2938 * streams were not initialized.
2939 */
2940 if (!uatomic_sub_return(&chan->refcount, 1)) {
2941 consumer_del_channel(chan);
2942 }
2943 rcu_read_unlock();
2944 goto restart;
2945 }
2946 case CONSUMER_CHANNEL_QUIT:
2947 /*
2948 * Remove the pipe from the poll set and continue the loop
2949 * since their might be data to consume.
2950 */
2951 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2952 continue;
2953 default:
2954 ERR("Unknown action");
2955 break;
2956 }
2957 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2958 DBG("Channel thread pipe hung up");
2959 /*
2960 * Remove the pipe from the poll set and continue the loop
2961 * since their might be data to consume.
2962 */
2963 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2964 continue;
2965 } else {
2966 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2967 goto end;
2968 }
2969
2970 /* Handle other stream */
2971 continue;
2972 }
2973
2974 rcu_read_lock();
2975 {
2976 uint64_t tmp_id = (uint64_t) pollfd;
2977
2978 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
2979 }
2980 node = lttng_ht_iter_get_node_u64(&iter);
2981 assert(node);
2982
2983 chan = caa_container_of(node, struct lttng_consumer_channel,
2984 wait_fd_node);
2985
2986 /* Check for error event */
2987 if (revents & (LPOLLERR | LPOLLHUP)) {
2988 DBG("Channel fd %d is hup|err.", pollfd);
2989
2990 lttng_poll_del(&events, chan->wait_fd);
2991 ret = lttng_ht_del(channel_ht, &iter);
2992 assert(ret == 0);
2993
2994 /*
2995 * This will close the wait fd for each stream associated to
2996 * this channel AND monitored by the data/metadata thread thus
2997 * will be clean by the right thread.
2998 */
2999 consumer_close_channel_streams(chan);
3000
3001 /* Release our own refcount */
3002 if (!uatomic_sub_return(&chan->refcount, 1)
3003 && !uatomic_read(&chan->nb_init_stream_left)) {
3004 consumer_del_channel(chan);
3005 }
3006 } else {
3007 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3008 rcu_read_unlock();
3009 goto end;
3010 }
3011
3012 /* Release RCU lock for the channel looked up */
3013 rcu_read_unlock();
3014 }
3015 }
3016
3017 /* All is OK */
3018 err = 0;
3019 end:
3020 lttng_poll_clean(&events);
3021 end_poll:
3022 destroy_channel_ht(channel_ht);
3023 end_ht:
3024 error_testpoint:
3025 DBG("Channel poll thread exiting");
3026 if (err) {
3027 health_error();
3028 ERR("Health error occurred in %s", __func__);
3029 }
3030 health_unregister(health_consumerd);
3031 rcu_unregister_thread();
3032 return NULL;
3033 }
3034
3035 static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
3036 struct pollfd *sockpoll, int client_socket)
3037 {
3038 int ret;
3039
3040 assert(ctx);
3041 assert(sockpoll);
3042
3043 ret = lttng_consumer_poll_socket(sockpoll);
3044 if (ret) {
3045 goto error;
3046 }
3047 DBG("Metadata connection on client_socket");
3048
3049 /* Blocking call, waiting for transmission */
3050 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
3051 if (ctx->consumer_metadata_socket < 0) {
3052 WARN("On accept metadata");
3053 ret = -1;
3054 goto error;
3055 }
3056 ret = 0;
3057
3058 error:
3059 return ret;
3060 }
3061
3062 /*
3063 * This thread listens on the consumerd socket and receives the file
3064 * descriptors from the session daemon.
3065 */
3066 void *consumer_thread_sessiond_poll(void *data)
3067 {
3068 int sock = -1, client_socket, ret, err = -1;
3069 /*
3070 * structure to poll for incoming data on communication socket avoids
3071 * making blocking sockets.
3072 */
3073 struct pollfd consumer_sockpoll[2];
3074 struct lttng_consumer_local_data *ctx = data;
3075
3076 rcu_register_thread();
3077
3078 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND);
3079
3080 if (testpoint(consumerd_thread_sessiond)) {
3081 goto error_testpoint;
3082 }
3083
3084 health_code_update();
3085
3086 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
3087 unlink(ctx->consumer_command_sock_path);
3088 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
3089 if (client_socket < 0) {
3090 ERR("Cannot create command socket");
3091 goto end;
3092 }
3093
3094 ret = lttcomm_listen_unix_sock(client_socket);
3095 if (ret < 0) {
3096 goto end;
3097 }
3098
3099 DBG("Sending ready command to lttng-sessiond");
3100 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3101 /* return < 0 on error, but == 0 is not fatal */
3102 if (ret < 0) {
3103 ERR("Error sending ready command to lttng-sessiond");
3104 goto end;
3105 }
3106
3107 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3108 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
3109 consumer_sockpoll[0].events = POLLIN | POLLPRI;
3110 consumer_sockpoll[1].fd = client_socket;
3111 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3112
3113 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3114 if (ret) {
3115 if (ret > 0) {
3116 /* should exit */
3117 err = 0;
3118 }
3119 goto end;
3120 }
3121 DBG("Connection on client_socket");
3122
3123 /* Blocking call, waiting for transmission */
3124 sock = lttcomm_accept_unix_sock(client_socket);
3125 if (sock < 0) {
3126 WARN("On accept");
3127 goto end;
3128 }
3129
3130 /*
3131 * Setup metadata socket which is the second socket connection on the
3132 * command unix socket.
3133 */
3134 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
3135 if (ret) {
3136 if (ret > 0) {
3137 /* should exit */
3138 err = 0;
3139 }
3140 goto end;
3141 }
3142
3143 /* This socket is not useful anymore. */
3144 ret = close(client_socket);
3145 if (ret < 0) {
3146 PERROR("close client_socket");
3147 }
3148 client_socket = -1;
3149
3150 /* update the polling structure to poll on the established socket */
3151 consumer_sockpoll[1].fd = sock;
3152 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3153
3154 while (1) {
3155 health_code_update();
3156
3157 health_poll_entry();
3158 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3159 health_poll_exit();
3160 if (ret) {
3161 if (ret > 0) {
3162 /* should exit */
3163 err = 0;
3164 }
3165 goto end;
3166 }
3167 DBG("Incoming command on sock");
3168 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
3169 if (ret <= 0) {
3170 /*
3171 * This could simply be a session daemon quitting. Don't output
3172 * ERR() here.
3173 */
3174 DBG("Communication interrupted on command socket");
3175 err = 0;
3176 goto end;
3177 }
3178 if (consumer_quit) {
3179 DBG("consumer_thread_receive_fds received quit from signal");
3180 err = 0; /* All is OK */
3181 goto end;
3182 }
3183 DBG("received command on sock");
3184 }
3185 /* All is OK */
3186 err = 0;
3187
3188 end:
3189 DBG("Consumer thread sessiond poll exiting");
3190
3191 /*
3192 * Close metadata streams since the producer is the session daemon which
3193 * just died.
3194 *
3195 * NOTE: for now, this only applies to the UST tracer.
3196 */
3197 lttng_consumer_close_all_metadata();
3198
3199 /*
3200 * when all fds have hung up, the polling thread
3201 * can exit cleanly
3202 */
3203 consumer_quit = 1;
3204
3205 /*
3206 * Notify the data poll thread to poll back again and test the
3207 * consumer_quit state that we just set so to quit gracefully.
3208 */
3209 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
3210
3211 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
3212
3213 notify_health_quit_pipe(health_quit_pipe);
3214
3215 /* Cleaning up possibly open sockets. */
3216 if (sock >= 0) {
3217 ret = close(sock);
3218 if (ret < 0) {
3219 PERROR("close sock sessiond poll");
3220 }
3221 }
3222 if (client_socket >= 0) {
3223 ret = close(client_socket);
3224 if (ret < 0) {
3225 PERROR("close client_socket sessiond poll");
3226 }
3227 }
3228
3229 error_testpoint:
3230 if (err) {
3231 health_error();
3232 ERR("Health error occurred in %s", __func__);
3233 }
3234 health_unregister(health_consumerd);
3235
3236 rcu_unregister_thread();
3237 return NULL;
3238 }
3239
3240 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
3241 struct lttng_consumer_local_data *ctx)
3242 {
3243 ssize_t ret;
3244
3245 pthread_mutex_lock(&stream->lock);
3246 if (stream->metadata_flag) {
3247 pthread_mutex_lock(&stream->metadata_rdv_lock);
3248 }
3249
3250 switch (consumer_data.type) {
3251 case LTTNG_CONSUMER_KERNEL:
3252 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3253 break;
3254 case LTTNG_CONSUMER32_UST:
3255 case LTTNG_CONSUMER64_UST:
3256 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3257 break;
3258 default:
3259 ERR("Unknown consumer_data type");
3260 assert(0);
3261 ret = -ENOSYS;
3262 break;
3263 }
3264
3265 if (stream->metadata_flag) {
3266 pthread_cond_broadcast(&stream->metadata_rdv);
3267 pthread_mutex_unlock(&stream->metadata_rdv_lock);
3268 }
3269 pthread_mutex_unlock(&stream->lock);
3270 return ret;
3271 }
3272
3273 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3274 {
3275 switch (consumer_data.type) {
3276 case LTTNG_CONSUMER_KERNEL:
3277 return lttng_kconsumer_on_recv_stream(stream);
3278 case LTTNG_CONSUMER32_UST:
3279 case LTTNG_CONSUMER64_UST:
3280 return lttng_ustconsumer_on_recv_stream(stream);
3281 default:
3282 ERR("Unknown consumer_data type");
3283 assert(0);
3284 return -ENOSYS;
3285 }
3286 }
3287
3288 /*
3289 * Allocate and set consumer data hash tables.
3290 */
3291 int lttng_consumer_init(void)
3292 {
3293 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3294 if (!consumer_data.channel_ht) {
3295 goto error;
3296 }
3297
3298 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3299 if (!consumer_data.relayd_ht) {
3300 goto error;
3301 }
3302
3303 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3304 if (!consumer_data.stream_list_ht) {
3305 goto error;
3306 }
3307
3308 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3309 if (!consumer_data.stream_per_chan_id_ht) {
3310 goto error;
3311 }
3312
3313 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3314 if (!data_ht) {
3315 goto error;
3316 }
3317
3318 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3319 if (!metadata_ht) {
3320 goto error;
3321 }
3322
3323 return 0;
3324
3325 error:
3326 return -1;
3327 }
3328
3329 /*
3330 * Process the ADD_RELAYD command receive by a consumer.
3331 *
3332 * This will create a relayd socket pair and add it to the relayd hash table.
3333 * The caller MUST acquire a RCU read side lock before calling it.
3334 */
3335 void consumer_add_relayd_socket(uint64_t relayd_id, int sock_type,
3336 struct lttng_consumer_local_data *ctx, int sock,
3337 struct pollfd *consumer_sockpoll,
3338 struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id,
3339 uint64_t relayd_session_id)
3340 {
3341 int fd = -1, ret = -1, relayd_created = 0;
3342 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3343 struct consumer_relayd_sock_pair *relayd = NULL;
3344
3345 assert(ctx);
3346 assert(relayd_sock);
3347
3348 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", relayd_id);
3349
3350 /* Get relayd reference if exists. */
3351 relayd = consumer_find_relayd(relayd_id);
3352 if (relayd == NULL) {
3353 assert(sock_type == LTTNG_STREAM_CONTROL);
3354 /* Not found. Allocate one. */
3355 relayd = consumer_allocate_relayd_sock_pair(relayd_id);
3356 if (relayd == NULL) {
3357 ret = -ENOMEM;
3358 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3359 goto error;
3360 } else {
3361 relayd->sessiond_session_id = sessiond_id;
3362 relayd_created = 1;
3363 }
3364
3365 /*
3366 * This code path MUST continue to the consumer send status message to
3367 * we can notify the session daemon and continue our work without
3368 * killing everything.
3369 */
3370 } else {
3371 /*
3372 * relayd key should never be found for control socket.
3373 */
3374 assert(sock_type != LTTNG_STREAM_CONTROL);
3375 }
3376
3377 /* First send a status message before receiving the fds. */
3378 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
3379 if (ret < 0) {
3380 /* Somehow, the session daemon is not responding anymore. */
3381 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3382 goto error_nosignal;
3383 }
3384
3385 /* Poll on consumer socket. */
3386 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3387 if (ret) {
3388 /* Needing to exit in the middle of a command: error. */
3389 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3390 ret = -EINTR;
3391 goto error_nosignal;
3392 }
3393
3394 /* Get relayd socket from session daemon */
3395 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3396 if (ret != sizeof(fd)) {
3397 ret = -1;
3398 fd = -1; /* Just in case it gets set with an invalid value. */
3399
3400 /*
3401 * Failing to receive FDs might indicate a major problem such as
3402 * reaching a fd limit during the receive where the kernel returns a
3403 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3404 * don't take any chances and stop everything.
3405 *
3406 * XXX: Feature request #558 will fix that and avoid this possible
3407 * issue when reaching the fd limit.
3408 */
3409 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3410 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
3411 goto error;
3412 }
3413
3414 /* Copy socket information and received FD */
3415 switch (sock_type) {
3416 case LTTNG_STREAM_CONTROL:
3417 /* Copy received lttcomm socket */
3418 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3419 ret = lttcomm_create_sock(&relayd->control_sock.sock);
3420 /* Handle create_sock error. */
3421 if (ret < 0) {
3422 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3423 goto error;
3424 }
3425 /*
3426 * Close the socket created internally by
3427 * lttcomm_create_sock, so we can replace it by the one
3428 * received from sessiond.
3429 */
3430 if (close(relayd->control_sock.sock.fd)) {
3431 PERROR("close");
3432 }
3433
3434 /* Assign new file descriptor */
3435 relayd->control_sock.sock.fd = fd;
3436 fd = -1; /* For error path */
3437 /* Assign version values. */
3438 relayd->control_sock.major = relayd_sock->major;
3439 relayd->control_sock.minor = relayd_sock->minor;
3440
3441 relayd->relayd_session_id = relayd_session_id;
3442
3443 break;
3444 case LTTNG_STREAM_DATA:
3445 /* Copy received lttcomm socket */
3446 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3447 ret = lttcomm_create_sock(&relayd->data_sock.sock);
3448 /* Handle create_sock error. */
3449 if (ret < 0) {
3450 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3451 goto error;
3452 }
3453 /*
3454 * Close the socket created internally by
3455 * lttcomm_create_sock, so we can replace it by the one
3456 * received from sessiond.
3457 */
3458 if (close(relayd->data_sock.sock.fd)) {
3459 PERROR("close");
3460 }
3461
3462 /* Assign new file descriptor */
3463 relayd->data_sock.sock.fd = fd;
3464 fd = -1; /* for eventual error paths */
3465 /* Assign version values. */
3466 relayd->data_sock.major = relayd_sock->major;
3467 relayd->data_sock.minor = relayd_sock->minor;
3468 break;
3469 default:
3470 ERR("Unknown relayd socket type (%d)", sock_type);
3471 ret = -1;
3472 ret_code = LTTCOMM_CONSUMERD_FATAL;
3473 goto error;
3474 }
3475
3476 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
3477 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3478 relayd->id, fd);
3479
3480 /* We successfully added the socket. Send status back. */
3481 ret = consumer_send_status_msg(sock, ret_code);
3482 if (ret < 0) {
3483 /* Somehow, the session daemon is not responding anymore. */
3484 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3485 goto error_nosignal;
3486 }
3487
3488 /*
3489 * Add relayd socket pair to consumer data hashtable. If object already
3490 * exists or on error, the function gracefully returns.
3491 */
3492 relayd->ctx = ctx;
3493 add_relayd(relayd);
3494
3495 /* All good! */
3496 return;
3497
3498 error:
3499 if (consumer_send_status_msg(sock, ret_code) < 0) {
3500 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3501 }
3502
3503 error_nosignal:
3504 /* Close received socket if valid. */
3505 if (fd >= 0) {
3506 if (close(fd)) {
3507 PERROR("close received socket");
3508 }
3509 }
3510
3511 if (relayd_created) {
3512 free(relayd);
3513 }
3514 }
3515
3516 /*
3517 * Search for a relayd associated to the session id and return the reference.
3518 *
3519 * A rcu read side lock MUST be acquire before calling this function and locked
3520 * until the relayd object is no longer necessary.
3521 */
3522 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3523 {
3524 struct lttng_ht_iter iter;
3525 struct consumer_relayd_sock_pair *relayd = NULL;
3526
3527 /* Iterate over all relayd since they are indexed by relayd_id. */
3528 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3529 node.node) {
3530 /*
3531 * Check by sessiond id which is unique here where the relayd session
3532 * id might not be when having multiple relayd.
3533 */
3534 if (relayd->sessiond_session_id == id) {
3535 /* Found the relayd. There can be only one per id. */
3536 goto found;
3537 }
3538 }
3539
3540 return NULL;
3541
3542 found:
3543 return relayd;
3544 }
3545
3546 /*
3547 * Check if for a given session id there is still data needed to be extract
3548 * from the buffers.
3549 *
3550 * Return 1 if data is pending or else 0 meaning ready to be read.
3551 */
3552 int consumer_data_pending(uint64_t id)
3553 {
3554 int ret;
3555 struct lttng_ht_iter iter;
3556 struct lttng_ht *ht;
3557 struct lttng_consumer_stream *stream;
3558 struct consumer_relayd_sock_pair *relayd = NULL;
3559 int (*data_pending)(struct lttng_consumer_stream *);
3560
3561 DBG("Consumer data pending command on session id %" PRIu64, id);
3562
3563 rcu_read_lock();
3564 pthread_mutex_lock(&consumer_data.lock);
3565
3566 switch (consumer_data.type) {
3567 case LTTNG_CONSUMER_KERNEL:
3568 data_pending = lttng_kconsumer_data_pending;
3569 break;
3570 case LTTNG_CONSUMER32_UST:
3571 case LTTNG_CONSUMER64_UST:
3572 data_pending = lttng_ustconsumer_data_pending;
3573 break;
3574 default:
3575 ERR("Unknown consumer data type");
3576 assert(0);
3577 }
3578
3579 /* Ease our life a bit */
3580 ht = consumer_data.stream_list_ht;
3581
3582 cds_lfht_for_each_entry_duplicate(ht->ht,
3583 ht->hash_fct(&id, lttng_ht_seed),
3584 ht->match_fct, &id,
3585 &iter.iter, stream, node_session_id.node) {
3586 pthread_mutex_lock(&stream->lock);
3587
3588 /*
3589 * A removed node from the hash table indicates that the stream has
3590 * been deleted thus having a guarantee that the buffers are closed
3591 * on the consumer side. However, data can still be transmitted
3592 * over the network so don't skip the relayd check.
3593 */
3594 ret = cds_lfht_is_node_deleted(&stream->node.node);
3595 if (!ret) {
3596 /* Check the stream if there is data in the buffers. */
3597 ret = data_pending(stream);
3598 if (ret == 1) {
3599 DBG("Data is pending locally on stream %" PRIu64, stream->key);
3600 pthread_mutex_unlock(&stream->lock);
3601 goto data_pending;
3602 }
3603 }
3604
3605 pthread_mutex_unlock(&stream->lock);
3606 }
3607
3608 relayd = find_relayd_by_session_id(id);
3609 if (relayd) {
3610 unsigned int is_data_inflight = 0;
3611
3612 /* Send init command for data pending. */
3613 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3614 ret = relayd_begin_data_pending(&relayd->control_sock,
3615 relayd->relayd_session_id);
3616 if (ret < 0) {
3617 /* Communication error thus the relayd so no data pending. */
3618 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3619 ERR("Relayd begin data pending failed. Cleaning up relayd %" PRIu64".", relayd->id);
3620 lttng_consumer_cleanup_relayd(relayd);
3621 goto data_not_pending;
3622 }
3623
3624 cds_lfht_for_each_entry_duplicate(ht->ht,
3625 ht->hash_fct(&id, lttng_ht_seed),
3626 ht->match_fct, &id,
3627 &iter.iter, stream, node_session_id.node) {
3628 if (stream->metadata_flag) {
3629 ret = relayd_quiescent_control(&relayd->control_sock,
3630 stream->relayd_stream_id);
3631 } else {
3632 ret = relayd_data_pending(&relayd->control_sock,
3633 stream->relayd_stream_id,
3634 stream->next_net_seq_num - 1);
3635 }
3636 if (ret == 1) {
3637 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3638 goto data_pending;
3639 }
3640 if (ret < 0) {
3641 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->id);
3642 lttng_consumer_cleanup_relayd(relayd);
3643 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3644 goto data_not_pending;
3645 }
3646 }
3647
3648 /* Send end command for data pending. */
3649 ret = relayd_end_data_pending(&relayd->control_sock,
3650 relayd->relayd_session_id, &is_data_inflight);
3651 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3652 if (ret < 0) {
3653 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->id);
3654 lttng_consumer_cleanup_relayd(relayd);
3655 goto data_not_pending;
3656 }
3657 if (is_data_inflight) {
3658 DBG("Data is in flight on relayd %" PRIu64, relayd->id);
3659 goto data_pending;
3660 }
3661 }
3662
3663 /*
3664 * Finding _no_ node in the hash table and no inflight data means that the
3665 * stream(s) have been removed thus data is guaranteed to be available for
3666 * analysis from the trace files.
3667 */
3668
3669 data_not_pending:
3670 /* Data is available to be read by a viewer. */
3671 pthread_mutex_unlock(&consumer_data.lock);
3672 rcu_read_unlock();
3673 return 0;
3674
3675 data_pending:
3676 /* Data is still being extracted from buffers. */
3677 pthread_mutex_unlock(&consumer_data.lock);
3678 rcu_read_unlock();
3679 return 1;
3680 }
3681
3682 /*
3683 * Send a ret code status message to the sessiond daemon.
3684 *
3685 * Return the sendmsg() return value.
3686 */
3687 int consumer_send_status_msg(int sock, int ret_code)
3688 {
3689 struct lttcomm_consumer_status_msg msg;
3690
3691 memset(&msg, 0, sizeof(msg));
3692 msg.ret_code = ret_code;
3693
3694 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3695 }
3696
3697 /*
3698 * Send a channel status message to the sessiond daemon.
3699 *
3700 * Return the sendmsg() return value.
3701 */
3702 int consumer_send_status_channel(int sock,
3703 struct lttng_consumer_channel *channel)
3704 {
3705 struct lttcomm_consumer_status_channel msg;
3706
3707 assert(sock >= 0);
3708
3709 memset(&msg, 0, sizeof(msg));
3710 if (!channel) {
3711 msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
3712 } else {
3713 msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3714 msg.key = channel->key;
3715 msg.stream_count = channel->streams.count;
3716 }
3717
3718 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3719 }
3720
3721 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos,
3722 unsigned long produced_pos, uint64_t nb_packets_per_stream,
3723 uint64_t max_sb_size)
3724 {
3725 unsigned long start_pos;
3726
3727 if (!nb_packets_per_stream) {
3728 return consumed_pos; /* Grab everything */
3729 }
3730 start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size);
3731 start_pos -= max_sb_size * nb_packets_per_stream;
3732 if ((long) (start_pos - consumed_pos) < 0) {
3733 return consumed_pos; /* Grab everything */
3734 }
3735 return start_pos;
3736 }
This page took 0.221964 seconds and 5 git commands to generate.