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