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