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