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