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