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