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