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