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