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