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