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