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