consumerd: pass channel instance to stream creation function
[lttng-tools.git] / src / common / consumer / consumer.c
1 /*
2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <assert.h>
12 #include <poll.h>
13 #include <pthread.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <inttypes.h>
21 #include <signal.h>
22
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <common/common.h>
25 #include <common/utils.h>
26 #include <common/time.h>
27 #include <common/compat/poll.h>
28 #include <common/compat/endian.h>
29 #include <common/index/index.h>
30 #include <common/kernel-ctl/kernel-ctl.h>
31 #include <common/sessiond-comm/relayd.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/kernel-consumer/kernel-consumer.h>
34 #include <common/relayd/relayd.h>
35 #include <common/ust-consumer/ust-consumer.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/consumer/consumer.h>
38 #include <common/consumer/consumer-stream.h>
39 #include <common/consumer/consumer-testpoint.h>
40 #include <common/align.h>
41 #include <common/consumer/consumer-metadata-cache.h>
42 #include <common/trace-chunk.h>
43 #include <common/trace-chunk-registry.h>
44 #include <common/string-utils/format.h>
45 #include <common/dynamic-array.h>
46
47 struct lttng_consumer_global_data consumer_data = {
48 .stream_count = 0,
49 .need_update = 1,
50 .type = LTTNG_CONSUMER_UNKNOWN,
51 };
52
53 enum consumer_channel_action {
54 CONSUMER_CHANNEL_ADD,
55 CONSUMER_CHANNEL_DEL,
56 CONSUMER_CHANNEL_QUIT,
57 };
58
59 struct consumer_channel_msg {
60 enum consumer_channel_action action;
61 struct lttng_consumer_channel *chan; /* add */
62 uint64_t key; /* del */
63 };
64
65 /* Flag used to temporarily pause data consumption from testpoints. */
66 int data_consumption_paused;
67
68 /*
69 * Flag to inform the polling thread to quit when all fd hung up. Updated by
70 * the consumer_thread_receive_fds when it notices that all fds has hung up.
71 * Also updated by the signal handler (consumer_should_exit()). Read by the
72 * polling threads.
73 */
74 int consumer_quit;
75
76 /*
77 * Global hash table containing respectively metadata and data streams. The
78 * stream element in this ht should only be updated by the metadata poll thread
79 * for the metadata and the data poll thread for the data.
80 */
81 static struct lttng_ht *metadata_ht;
82 static struct lttng_ht *data_ht;
83
84 static const char *get_consumer_domain(void)
85 {
86 switch (consumer_data.type) {
87 case LTTNG_CONSUMER_KERNEL:
88 return DEFAULT_KERNEL_TRACE_DIR;
89 case LTTNG_CONSUMER64_UST:
90 /* Fall-through. */
91 case LTTNG_CONSUMER32_UST:
92 return DEFAULT_UST_TRACE_DIR;
93 default:
94 abort();
95 }
96 }
97
98 /*
99 * Notify a thread lttng pipe to poll back again. This usually means that some
100 * global state has changed so we just send back the thread in a poll wait
101 * call.
102 */
103 static void notify_thread_lttng_pipe(struct lttng_pipe *pipe)
104 {
105 struct lttng_consumer_stream *null_stream = NULL;
106
107 assert(pipe);
108
109 (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream));
110 }
111
112 static void notify_health_quit_pipe(int *pipe)
113 {
114 ssize_t ret;
115
116 ret = lttng_write(pipe[1], "4", 1);
117 if (ret < 1) {
118 PERROR("write consumer health quit");
119 }
120 }
121
122 static void notify_channel_pipe(struct lttng_consumer_local_data *ctx,
123 struct lttng_consumer_channel *chan,
124 uint64_t key,
125 enum consumer_channel_action action)
126 {
127 struct consumer_channel_msg msg;
128 ssize_t ret;
129
130 memset(&msg, 0, sizeof(msg));
131
132 msg.action = action;
133 msg.chan = chan;
134 msg.key = key;
135 ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
136 if (ret < sizeof(msg)) {
137 PERROR("notify_channel_pipe write error");
138 }
139 }
140
141 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
142 uint64_t key)
143 {
144 notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL);
145 }
146
147 static int read_channel_pipe(struct lttng_consumer_local_data *ctx,
148 struct lttng_consumer_channel **chan,
149 uint64_t *key,
150 enum consumer_channel_action *action)
151 {
152 struct consumer_channel_msg msg;
153 ssize_t ret;
154
155 ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
156 if (ret < sizeof(msg)) {
157 ret = -1;
158 goto error;
159 }
160 *action = msg.action;
161 *chan = msg.chan;
162 *key = msg.key;
163 error:
164 return (int) ret;
165 }
166
167 /*
168 * Cleanup the stream list of a channel. Those streams are not yet globally
169 * visible
170 */
171 static void clean_channel_stream_list(struct lttng_consumer_channel *channel)
172 {
173 struct lttng_consumer_stream *stream, *stmp;
174
175 assert(channel);
176
177 /* Delete streams that might have been left in the stream list. */
178 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
179 send_node) {
180 cds_list_del(&stream->send_node);
181 /*
182 * Once a stream is added to this list, the buffers were created so we
183 * have a guarantee that this call will succeed. Setting the monitor
184 * mode to 0 so we don't lock nor try to delete the stream from the
185 * global hash table.
186 */
187 stream->monitor = 0;
188 consumer_stream_destroy(stream, NULL);
189 }
190 }
191
192 /*
193 * Find a stream. The consumer_data.lock must be locked during this
194 * call.
195 */
196 static struct lttng_consumer_stream *find_stream(uint64_t key,
197 struct lttng_ht *ht)
198 {
199 struct lttng_ht_iter iter;
200 struct lttng_ht_node_u64 *node;
201 struct lttng_consumer_stream *stream = NULL;
202
203 assert(ht);
204
205 /* -1ULL keys are lookup failures */
206 if (key == (uint64_t) -1ULL) {
207 return NULL;
208 }
209
210 rcu_read_lock();
211
212 lttng_ht_lookup(ht, &key, &iter);
213 node = lttng_ht_iter_get_node_u64(&iter);
214 if (node != NULL) {
215 stream = caa_container_of(node, struct lttng_consumer_stream, node);
216 }
217
218 rcu_read_unlock();
219
220 return stream;
221 }
222
223 static void steal_stream_key(uint64_t key, struct lttng_ht *ht)
224 {
225 struct lttng_consumer_stream *stream;
226
227 rcu_read_lock();
228 stream = find_stream(key, ht);
229 if (stream) {
230 stream->key = (uint64_t) -1ULL;
231 /*
232 * We don't want the lookup to match, but we still need
233 * to iterate on this stream when iterating over the hash table. Just
234 * change the node key.
235 */
236 stream->node.key = (uint64_t) -1ULL;
237 }
238 rcu_read_unlock();
239 }
240
241 /*
242 * Return a channel object for the given key.
243 *
244 * RCU read side lock MUST be acquired before calling this function and
245 * protects the channel ptr.
246 */
247 struct lttng_consumer_channel *consumer_find_channel(uint64_t key)
248 {
249 struct lttng_ht_iter iter;
250 struct lttng_ht_node_u64 *node;
251 struct lttng_consumer_channel *channel = NULL;
252
253 /* -1ULL keys are lookup failures */
254 if (key == (uint64_t) -1ULL) {
255 return NULL;
256 }
257
258 lttng_ht_lookup(consumer_data.channel_ht, &key, &iter);
259 node = lttng_ht_iter_get_node_u64(&iter);
260 if (node != NULL) {
261 channel = caa_container_of(node, struct lttng_consumer_channel, node);
262 }
263
264 return channel;
265 }
266
267 /*
268 * There is a possibility that the consumer does not have enough time between
269 * the close of the channel on the session daemon and the cleanup in here thus
270 * once we have a channel add with an existing key, we know for sure that this
271 * channel will eventually get cleaned up by all streams being closed.
272 *
273 * This function just nullifies the already existing channel key.
274 */
275 static void steal_channel_key(uint64_t key)
276 {
277 struct lttng_consumer_channel *channel;
278
279 rcu_read_lock();
280 channel = consumer_find_channel(key);
281 if (channel) {
282 channel->key = (uint64_t) -1ULL;
283 /*
284 * We don't want the lookup to match, but we still need to iterate on
285 * this channel when iterating over the hash table. Just change the
286 * node key.
287 */
288 channel->node.key = (uint64_t) -1ULL;
289 }
290 rcu_read_unlock();
291 }
292
293 static void free_channel_rcu(struct rcu_head *head)
294 {
295 struct lttng_ht_node_u64 *node =
296 caa_container_of(head, struct lttng_ht_node_u64, head);
297 struct lttng_consumer_channel *channel =
298 caa_container_of(node, struct lttng_consumer_channel, node);
299
300 switch (consumer_data.type) {
301 case LTTNG_CONSUMER_KERNEL:
302 break;
303 case LTTNG_CONSUMER32_UST:
304 case LTTNG_CONSUMER64_UST:
305 lttng_ustconsumer_free_channel(channel);
306 break;
307 default:
308 ERR("Unknown consumer_data type");
309 abort();
310 }
311 free(channel);
312 }
313
314 /*
315 * RCU protected relayd socket pair free.
316 */
317 static void free_relayd_rcu(struct rcu_head *head)
318 {
319 struct lttng_ht_node_u64 *node =
320 caa_container_of(head, struct lttng_ht_node_u64, head);
321 struct consumer_relayd_sock_pair *relayd =
322 caa_container_of(node, struct consumer_relayd_sock_pair, node);
323
324 /*
325 * Close all sockets. This is done in the call RCU since we don't want the
326 * socket fds to be reassigned thus potentially creating bad state of the
327 * relayd object.
328 *
329 * We do not have to lock the control socket mutex here since at this stage
330 * there is no one referencing to this relayd object.
331 */
332 (void) relayd_close(&relayd->control_sock);
333 (void) relayd_close(&relayd->data_sock);
334
335 pthread_mutex_destroy(&relayd->ctrl_sock_mutex);
336 free(relayd);
337 }
338
339 /*
340 * Destroy and free relayd socket pair object.
341 */
342 void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
343 {
344 int ret;
345 struct lttng_ht_iter iter;
346
347 if (relayd == NULL) {
348 return;
349 }
350
351 DBG("Consumer destroy and close relayd socket pair");
352
353 iter.iter.node = &relayd->node.node;
354 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
355 if (ret != 0) {
356 /* We assume the relayd is being or is destroyed */
357 return;
358 }
359
360 /* RCU free() call */
361 call_rcu(&relayd->node.head, free_relayd_rcu);
362 }
363
364 /*
365 * Remove a channel from the global list protected by a mutex. This function is
366 * also responsible for freeing its data structures.
367 */
368 void consumer_del_channel(struct lttng_consumer_channel *channel)
369 {
370 struct lttng_ht_iter iter;
371
372 DBG("Consumer delete channel key %" PRIu64, channel->key);
373
374 pthread_mutex_lock(&consumer_data.lock);
375 pthread_mutex_lock(&channel->lock);
376
377 /* Destroy streams that might have been left in the stream list. */
378 clean_channel_stream_list(channel);
379
380 if (channel->live_timer_enabled == 1) {
381 consumer_timer_live_stop(channel);
382 }
383 if (channel->monitor_timer_enabled == 1) {
384 consumer_timer_monitor_stop(channel);
385 }
386
387 switch (consumer_data.type) {
388 case LTTNG_CONSUMER_KERNEL:
389 break;
390 case LTTNG_CONSUMER32_UST:
391 case LTTNG_CONSUMER64_UST:
392 lttng_ustconsumer_del_channel(channel);
393 break;
394 default:
395 ERR("Unknown consumer_data type");
396 assert(0);
397 goto end;
398 }
399
400 lttng_trace_chunk_put(channel->trace_chunk);
401 channel->trace_chunk = NULL;
402
403 if (channel->is_published) {
404 int ret;
405
406 rcu_read_lock();
407 iter.iter.node = &channel->node.node;
408 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
409 assert(!ret);
410
411 iter.iter.node = &channel->channels_by_session_id_ht_node.node;
412 ret = lttng_ht_del(consumer_data.channels_by_session_id_ht,
413 &iter);
414 assert(!ret);
415 rcu_read_unlock();
416 }
417
418 channel->is_deleted = true;
419 call_rcu(&channel->node.head, free_channel_rcu);
420 end:
421 pthread_mutex_unlock(&channel->lock);
422 pthread_mutex_unlock(&consumer_data.lock);
423 }
424
425 /*
426 * Iterate over the relayd hash table and destroy each element. Finally,
427 * destroy the whole hash table.
428 */
429 static void cleanup_relayd_ht(void)
430 {
431 struct lttng_ht_iter iter;
432 struct consumer_relayd_sock_pair *relayd;
433
434 rcu_read_lock();
435
436 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
437 node.node) {
438 consumer_destroy_relayd(relayd);
439 }
440
441 rcu_read_unlock();
442
443 lttng_ht_destroy(consumer_data.relayd_ht);
444 }
445
446 /*
447 * Update the end point status of all streams having the given network sequence
448 * index (relayd index).
449 *
450 * It's atomically set without having the stream mutex locked which is fine
451 * because we handle the write/read race with a pipe wakeup for each thread.
452 */
453 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx,
454 enum consumer_endpoint_status status)
455 {
456 struct lttng_ht_iter iter;
457 struct lttng_consumer_stream *stream;
458
459 DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx);
460
461 rcu_read_lock();
462
463 /* Let's begin with metadata */
464 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
465 if (stream->net_seq_idx == net_seq_idx) {
466 uatomic_set(&stream->endpoint_status, status);
467 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
468 }
469 }
470
471 /* Follow up by the data streams */
472 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
473 if (stream->net_seq_idx == net_seq_idx) {
474 uatomic_set(&stream->endpoint_status, status);
475 DBG("Delete flag set to data stream %d", stream->wait_fd);
476 }
477 }
478 rcu_read_unlock();
479 }
480
481 /*
482 * Cleanup a relayd object by flagging every associated streams for deletion,
483 * destroying the object meaning removing it from the relayd hash table,
484 * closing the sockets and freeing the memory in a RCU call.
485 *
486 * If a local data context is available, notify the threads that the streams'
487 * state have changed.
488 */
489 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd)
490 {
491 uint64_t netidx;
492
493 assert(relayd);
494
495 DBG("Cleaning up relayd object ID %"PRIu64, relayd->net_seq_idx);
496
497 /* Save the net sequence index before destroying the object */
498 netidx = relayd->net_seq_idx;
499
500 /*
501 * Delete the relayd from the relayd hash table, close the sockets and free
502 * the object in a RCU call.
503 */
504 consumer_destroy_relayd(relayd);
505
506 /* Set inactive endpoint to all streams */
507 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
508
509 /*
510 * With a local data context, notify the threads that the streams' state
511 * have changed. The write() action on the pipe acts as an "implicit"
512 * memory barrier ordering the updates of the end point status from the
513 * read of this status which happens AFTER receiving this notify.
514 */
515 notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe);
516 notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe);
517 }
518
519 /*
520 * Flag a relayd socket pair for destruction. Destroy it if the refcount
521 * reaches zero.
522 *
523 * RCU read side lock MUST be aquired before calling this function.
524 */
525 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
526 {
527 assert(relayd);
528
529 /* Set destroy flag for this object */
530 uatomic_set(&relayd->destroy_flag, 1);
531
532 /* Destroy the relayd if refcount is 0 */
533 if (uatomic_read(&relayd->refcount) == 0) {
534 consumer_destroy_relayd(relayd);
535 }
536 }
537
538 /*
539 * Completly destroy stream from every visiable data structure and the given
540 * hash table if one.
541 *
542 * One this call returns, the stream object is not longer usable nor visible.
543 */
544 void consumer_del_stream(struct lttng_consumer_stream *stream,
545 struct lttng_ht *ht)
546 {
547 consumer_stream_destroy(stream, ht);
548 }
549
550 /*
551 * XXX naming of del vs destroy is all mixed up.
552 */
553 void consumer_del_stream_for_data(struct lttng_consumer_stream *stream)
554 {
555 consumer_stream_destroy(stream, data_ht);
556 }
557
558 void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream)
559 {
560 consumer_stream_destroy(stream, metadata_ht);
561 }
562
563 void consumer_stream_update_channel_attributes(
564 struct lttng_consumer_stream *stream,
565 struct lttng_consumer_channel *channel)
566 {
567 stream->channel_read_only_attributes.tracefile_size =
568 channel->tracefile_size;
569 }
570
571 struct lttng_consumer_stream *consumer_allocate_stream(
572 struct lttng_consumer_channel *channel,
573 uint64_t channel_key,
574 uint64_t stream_key,
575 const char *channel_name,
576 uint64_t relayd_id,
577 uint64_t session_id,
578 struct lttng_trace_chunk *trace_chunk,
579 int cpu,
580 int *alloc_ret,
581 enum consumer_channel_type type,
582 unsigned int monitor)
583 {
584 int ret;
585 struct lttng_consumer_stream *stream;
586
587 stream = zmalloc(sizeof(*stream));
588 if (stream == NULL) {
589 PERROR("malloc struct lttng_consumer_stream");
590 ret = -ENOMEM;
591 goto end;
592 }
593
594 if (trace_chunk && !lttng_trace_chunk_get(trace_chunk)) {
595 ERR("Failed to acquire trace chunk reference during the creation of a stream");
596 ret = -1;
597 goto error;
598 }
599
600 rcu_read_lock();
601 stream->chan = channel;
602 stream->key = stream_key;
603 stream->trace_chunk = trace_chunk;
604 stream->out_fd = -1;
605 stream->out_fd_offset = 0;
606 stream->output_written = 0;
607 stream->net_seq_idx = relayd_id;
608 stream->session_id = session_id;
609 stream->monitor = monitor;
610 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
611 stream->index_file = NULL;
612 stream->last_sequence_number = -1ULL;
613 stream->rotate_position = -1ULL;
614 pthread_mutex_init(&stream->lock, NULL);
615 pthread_mutex_init(&stream->metadata_timer_lock, NULL);
616
617 /* If channel is the metadata, flag this stream as metadata. */
618 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
619 stream->metadata_flag = 1;
620 /* Metadata is flat out. */
621 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
622 /* Live rendez-vous point. */
623 pthread_cond_init(&stream->metadata_rdv, NULL);
624 pthread_mutex_init(&stream->metadata_rdv_lock, NULL);
625 } else {
626 /* Format stream name to <channel_name>_<cpu_number> */
627 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
628 channel_name, cpu);
629 if (ret < 0) {
630 PERROR("snprintf stream name");
631 goto error;
632 }
633 }
634
635 /* Key is always the wait_fd for streams. */
636 lttng_ht_node_init_u64(&stream->node, stream->key);
637
638 /* Init node per channel id key */
639 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
640
641 /* Init session id node with the stream session id */
642 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
643
644 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
645 " relayd_id %" PRIu64 ", session_id %" PRIu64,
646 stream->name, stream->key, channel_key,
647 stream->net_seq_idx, stream->session_id);
648
649 rcu_read_unlock();
650 return stream;
651
652 error:
653 rcu_read_unlock();
654 lttng_trace_chunk_put(stream->trace_chunk);
655 free(stream);
656 end:
657 if (alloc_ret) {
658 *alloc_ret = ret;
659 }
660 return NULL;
661 }
662
663 /*
664 * Add a stream to the global list protected by a mutex.
665 */
666 void consumer_add_data_stream(struct lttng_consumer_stream *stream)
667 {
668 struct lttng_ht *ht = data_ht;
669
670 assert(stream);
671 assert(ht);
672
673 DBG3("Adding consumer stream %" PRIu64, stream->key);
674
675 pthread_mutex_lock(&consumer_data.lock);
676 pthread_mutex_lock(&stream->chan->lock);
677 pthread_mutex_lock(&stream->chan->timer_lock);
678 pthread_mutex_lock(&stream->lock);
679 rcu_read_lock();
680
681 /* Steal stream identifier to avoid having streams with the same key */
682 steal_stream_key(stream->key, ht);
683
684 lttng_ht_add_unique_u64(ht, &stream->node);
685
686 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
687 &stream->node_channel_id);
688
689 /*
690 * Add stream to the stream_list_ht of the consumer data. No need to steal
691 * the key since the HT does not use it and we allow to add redundant keys
692 * into this table.
693 */
694 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
695
696 /*
697 * When nb_init_stream_left reaches 0, we don't need to trigger any action
698 * in terms of destroying the associated channel, because the action that
699 * causes the count to become 0 also causes a stream to be added. The
700 * channel deletion will thus be triggered by the following removal of this
701 * stream.
702 */
703 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
704 /* Increment refcount before decrementing nb_init_stream_left */
705 cmm_smp_wmb();
706 uatomic_dec(&stream->chan->nb_init_stream_left);
707 }
708
709 /* Update consumer data once the node is inserted. */
710 consumer_data.stream_count++;
711 consumer_data.need_update = 1;
712
713 rcu_read_unlock();
714 pthread_mutex_unlock(&stream->lock);
715 pthread_mutex_unlock(&stream->chan->timer_lock);
716 pthread_mutex_unlock(&stream->chan->lock);
717 pthread_mutex_unlock(&consumer_data.lock);
718 }
719
720 /*
721 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
722 * be acquired before calling this.
723 */
724 static int add_relayd(struct consumer_relayd_sock_pair *relayd)
725 {
726 int ret = 0;
727 struct lttng_ht_node_u64 *node;
728 struct lttng_ht_iter iter;
729
730 assert(relayd);
731
732 lttng_ht_lookup(consumer_data.relayd_ht,
733 &relayd->net_seq_idx, &iter);
734 node = lttng_ht_iter_get_node_u64(&iter);
735 if (node != NULL) {
736 goto end;
737 }
738 lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node);
739
740 end:
741 return ret;
742 }
743
744 /*
745 * Allocate and return a consumer relayd socket.
746 */
747 static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
748 uint64_t net_seq_idx)
749 {
750 struct consumer_relayd_sock_pair *obj = NULL;
751
752 /* net sequence index of -1 is a failure */
753 if (net_seq_idx == (uint64_t) -1ULL) {
754 goto error;
755 }
756
757 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
758 if (obj == NULL) {
759 PERROR("zmalloc relayd sock");
760 goto error;
761 }
762
763 obj->net_seq_idx = net_seq_idx;
764 obj->refcount = 0;
765 obj->destroy_flag = 0;
766 obj->control_sock.sock.fd = -1;
767 obj->data_sock.sock.fd = -1;
768 lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx);
769 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
770
771 error:
772 return obj;
773 }
774
775 /*
776 * Find a relayd socket pair in the global consumer data.
777 *
778 * Return the object if found else NULL.
779 * RCU read-side lock must be held across this call and while using the
780 * returned object.
781 */
782 struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key)
783 {
784 struct lttng_ht_iter iter;
785 struct lttng_ht_node_u64 *node;
786 struct consumer_relayd_sock_pair *relayd = NULL;
787
788 /* Negative keys are lookup failures */
789 if (key == (uint64_t) -1ULL) {
790 goto error;
791 }
792
793 lttng_ht_lookup(consumer_data.relayd_ht, &key,
794 &iter);
795 node = lttng_ht_iter_get_node_u64(&iter);
796 if (node != NULL) {
797 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
798 }
799
800 error:
801 return relayd;
802 }
803
804 /*
805 * Find a relayd and send the stream
806 *
807 * Returns 0 on success, < 0 on error
808 */
809 int consumer_send_relayd_stream(struct lttng_consumer_stream *stream,
810 char *path)
811 {
812 int ret = 0;
813 struct consumer_relayd_sock_pair *relayd;
814
815 assert(stream);
816 assert(stream->net_seq_idx != -1ULL);
817 assert(path);
818
819 /* The stream is not metadata. Get relayd reference if exists. */
820 rcu_read_lock();
821 relayd = consumer_find_relayd(stream->net_seq_idx);
822 if (relayd != NULL) {
823 /* Add stream on the relayd */
824 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
825 ret = relayd_add_stream(&relayd->control_sock, stream->name,
826 get_consumer_domain(), path, &stream->relayd_stream_id,
827 stream->chan->tracefile_size,
828 stream->chan->tracefile_count,
829 stream->trace_chunk);
830 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
831 if (ret < 0) {
832 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
833 lttng_consumer_cleanup_relayd(relayd);
834 goto end;
835 }
836
837 uatomic_inc(&relayd->refcount);
838 stream->sent_to_relayd = 1;
839 } else {
840 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.",
841 stream->key, stream->net_seq_idx);
842 ret = -1;
843 goto end;
844 }
845
846 DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64,
847 stream->name, stream->key, stream->net_seq_idx);
848
849 end:
850 rcu_read_unlock();
851 return ret;
852 }
853
854 /*
855 * Find a relayd and send the streams sent message
856 *
857 * Returns 0 on success, < 0 on error
858 */
859 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx)
860 {
861 int ret = 0;
862 struct consumer_relayd_sock_pair *relayd;
863
864 assert(net_seq_idx != -1ULL);
865
866 /* The stream is not metadata. Get relayd reference if exists. */
867 rcu_read_lock();
868 relayd = consumer_find_relayd(net_seq_idx);
869 if (relayd != NULL) {
870 /* Add stream on the relayd */
871 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
872 ret = relayd_streams_sent(&relayd->control_sock);
873 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
874 if (ret < 0) {
875 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
876 lttng_consumer_cleanup_relayd(relayd);
877 goto end;
878 }
879 } else {
880 ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.",
881 net_seq_idx);
882 ret = -1;
883 goto end;
884 }
885
886 ret = 0;
887 DBG("All streams sent relayd id %" PRIu64, net_seq_idx);
888
889 end:
890 rcu_read_unlock();
891 return ret;
892 }
893
894 /*
895 * Find a relayd and close the stream
896 */
897 void close_relayd_stream(struct lttng_consumer_stream *stream)
898 {
899 struct consumer_relayd_sock_pair *relayd;
900
901 /* The stream is not metadata. Get relayd reference if exists. */
902 rcu_read_lock();
903 relayd = consumer_find_relayd(stream->net_seq_idx);
904 if (relayd) {
905 consumer_stream_relayd_close(stream, relayd);
906 }
907 rcu_read_unlock();
908 }
909
910 /*
911 * Handle stream for relayd transmission if the stream applies for network
912 * streaming where the net sequence index is set.
913 *
914 * Return destination file descriptor or negative value on error.
915 */
916 static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
917 size_t data_size, unsigned long padding,
918 struct consumer_relayd_sock_pair *relayd)
919 {
920 int outfd = -1, ret;
921 struct lttcomm_relayd_data_hdr data_hdr;
922
923 /* Safety net */
924 assert(stream);
925 assert(relayd);
926
927 /* Reset data header */
928 memset(&data_hdr, 0, sizeof(data_hdr));
929
930 if (stream->metadata_flag) {
931 /* Caller MUST acquire the relayd control socket lock */
932 ret = relayd_send_metadata(&relayd->control_sock, data_size);
933 if (ret < 0) {
934 goto error;
935 }
936
937 /* Metadata are always sent on the control socket. */
938 outfd = relayd->control_sock.sock.fd;
939 } else {
940 /* Set header with stream information */
941 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
942 data_hdr.data_size = htobe32(data_size);
943 data_hdr.padding_size = htobe32(padding);
944
945 /*
946 * Note that net_seq_num below is assigned with the *current* value of
947 * next_net_seq_num and only after that the next_net_seq_num will be
948 * increment. This is why when issuing a command on the relayd using
949 * this next value, 1 should always be substracted in order to compare
950 * the last seen sequence number on the relayd side to the last sent.
951 */
952 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num);
953 /* Other fields are zeroed previously */
954
955 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
956 sizeof(data_hdr));
957 if (ret < 0) {
958 goto error;
959 }
960
961 ++stream->next_net_seq_num;
962
963 /* Set to go on data socket */
964 outfd = relayd->data_sock.sock.fd;
965 }
966
967 error:
968 return outfd;
969 }
970
971 /*
972 * Trigger a dump of the metadata content. Following/during the succesful
973 * completion of this call, the metadata poll thread will start receiving
974 * metadata packets to consume.
975 *
976 * The caller must hold the channel and stream locks.
977 */
978 static
979 int consumer_metadata_stream_dump(struct lttng_consumer_stream *stream)
980 {
981 int ret;
982
983 ASSERT_LOCKED(stream->chan->lock);
984 ASSERT_LOCKED(stream->lock);
985 assert(stream->metadata_flag);
986 assert(stream->chan->trace_chunk);
987
988 switch (consumer_data.type) {
989 case LTTNG_CONSUMER_KERNEL:
990 /*
991 * Reset the position of what has been read from the
992 * metadata cache to 0 so we can dump it again.
993 */
994 ret = kernctl_metadata_cache_dump(stream->wait_fd);
995 break;
996 case LTTNG_CONSUMER32_UST:
997 case LTTNG_CONSUMER64_UST:
998 /*
999 * Reset the position pushed from the metadata cache so it
1000 * will write from the beginning on the next push.
1001 */
1002 stream->ust_metadata_pushed = 0;
1003 ret = consumer_metadata_wakeup_pipe(stream->chan);
1004 break;
1005 default:
1006 ERR("Unknown consumer_data type");
1007 abort();
1008 }
1009 if (ret < 0) {
1010 ERR("Failed to dump the metadata cache");
1011 }
1012 return ret;
1013 }
1014
1015 static
1016 int lttng_consumer_channel_set_trace_chunk(
1017 struct lttng_consumer_channel *channel,
1018 struct lttng_trace_chunk *new_trace_chunk)
1019 {
1020 pthread_mutex_lock(&channel->lock);
1021 if (channel->is_deleted) {
1022 /*
1023 * The channel has been logically deleted and should no longer
1024 * be used. It has released its reference to its current trace
1025 * chunk and should not acquire a new one.
1026 *
1027 * Return success as there is nothing for the caller to do.
1028 */
1029 goto end;
1030 }
1031
1032 /*
1033 * The acquisition of the reference cannot fail (barring
1034 * a severe internal error) since a reference to the published
1035 * chunk is already held by the caller.
1036 */
1037 if (new_trace_chunk) {
1038 const bool acquired_reference = lttng_trace_chunk_get(
1039 new_trace_chunk);
1040
1041 assert(acquired_reference);
1042 }
1043
1044 lttng_trace_chunk_put(channel->trace_chunk);
1045 channel->trace_chunk = new_trace_chunk;
1046 end:
1047 pthread_mutex_unlock(&channel->lock);
1048 return 0;
1049 }
1050
1051 /*
1052 * Allocate and return a new lttng_consumer_channel object using the given key
1053 * to initialize the hash table node.
1054 *
1055 * On error, return NULL.
1056 */
1057 struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
1058 uint64_t session_id,
1059 const uint64_t *chunk_id,
1060 const char *pathname,
1061 const char *name,
1062 uint64_t relayd_id,
1063 enum lttng_event_output output,
1064 uint64_t tracefile_size,
1065 uint64_t tracefile_count,
1066 uint64_t session_id_per_pid,
1067 unsigned int monitor,
1068 unsigned int live_timer_interval,
1069 const char *root_shm_path,
1070 const char *shm_path)
1071 {
1072 struct lttng_consumer_channel *channel = NULL;
1073 struct lttng_trace_chunk *trace_chunk = NULL;
1074
1075 if (chunk_id) {
1076 trace_chunk = lttng_trace_chunk_registry_find_chunk(
1077 consumer_data.chunk_registry, session_id,
1078 *chunk_id);
1079 if (!trace_chunk) {
1080 ERR("Failed to find trace chunk reference during creation of channel");
1081 goto end;
1082 }
1083 }
1084
1085 channel = zmalloc(sizeof(*channel));
1086 if (channel == NULL) {
1087 PERROR("malloc struct lttng_consumer_channel");
1088 goto end;
1089 }
1090
1091 channel->key = key;
1092 channel->refcount = 0;
1093 channel->session_id = session_id;
1094 channel->session_id_per_pid = session_id_per_pid;
1095 channel->relayd_id = relayd_id;
1096 channel->tracefile_size = tracefile_size;
1097 channel->tracefile_count = tracefile_count;
1098 channel->monitor = monitor;
1099 channel->live_timer_interval = live_timer_interval;
1100 pthread_mutex_init(&channel->lock, NULL);
1101 pthread_mutex_init(&channel->timer_lock, NULL);
1102
1103 switch (output) {
1104 case LTTNG_EVENT_SPLICE:
1105 channel->output = CONSUMER_CHANNEL_SPLICE;
1106 break;
1107 case LTTNG_EVENT_MMAP:
1108 channel->output = CONSUMER_CHANNEL_MMAP;
1109 break;
1110 default:
1111 assert(0);
1112 free(channel);
1113 channel = NULL;
1114 goto end;
1115 }
1116
1117 /*
1118 * In monitor mode, the streams associated with the channel will be put in
1119 * a special list ONLY owned by this channel. So, the refcount is set to 1
1120 * here meaning that the channel itself has streams that are referenced.
1121 *
1122 * On a channel deletion, once the channel is no longer visible, the
1123 * refcount is decremented and checked for a zero value to delete it. With
1124 * streams in no monitor mode, it will now be safe to destroy the channel.
1125 */
1126 if (!channel->monitor) {
1127 channel->refcount = 1;
1128 }
1129
1130 strncpy(channel->pathname, pathname, sizeof(channel->pathname));
1131 channel->pathname[sizeof(channel->pathname) - 1] = '\0';
1132
1133 strncpy(channel->name, name, sizeof(channel->name));
1134 channel->name[sizeof(channel->name) - 1] = '\0';
1135
1136 if (root_shm_path) {
1137 strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path));
1138 channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0';
1139 }
1140 if (shm_path) {
1141 strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path));
1142 channel->shm_path[sizeof(channel->shm_path) - 1] = '\0';
1143 }
1144
1145 lttng_ht_node_init_u64(&channel->node, channel->key);
1146 lttng_ht_node_init_u64(&channel->channels_by_session_id_ht_node,
1147 channel->session_id);
1148
1149 channel->wait_fd = -1;
1150 CDS_INIT_LIST_HEAD(&channel->streams.head);
1151
1152 if (trace_chunk) {
1153 int ret = lttng_consumer_channel_set_trace_chunk(channel,
1154 trace_chunk);
1155 if (ret) {
1156 goto error;
1157 }
1158 }
1159
1160 DBG("Allocated channel (key %" PRIu64 ")", channel->key);
1161
1162 end:
1163 lttng_trace_chunk_put(trace_chunk);
1164 return channel;
1165 error:
1166 consumer_del_channel(channel);
1167 channel = NULL;
1168 goto end;
1169 }
1170
1171 /*
1172 * Add a channel to the global list protected by a mutex.
1173 *
1174 * Always return 0 indicating success.
1175 */
1176 int consumer_add_channel(struct lttng_consumer_channel *channel,
1177 struct lttng_consumer_local_data *ctx)
1178 {
1179 pthread_mutex_lock(&consumer_data.lock);
1180 pthread_mutex_lock(&channel->lock);
1181 pthread_mutex_lock(&channel->timer_lock);
1182
1183 /*
1184 * This gives us a guarantee that the channel we are about to add to the
1185 * channel hash table will be unique. See this function comment on the why
1186 * we need to steel the channel key at this stage.
1187 */
1188 steal_channel_key(channel->key);
1189
1190 rcu_read_lock();
1191 lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node);
1192 lttng_ht_add_u64(consumer_data.channels_by_session_id_ht,
1193 &channel->channels_by_session_id_ht_node);
1194 rcu_read_unlock();
1195 channel->is_published = true;
1196
1197 pthread_mutex_unlock(&channel->timer_lock);
1198 pthread_mutex_unlock(&channel->lock);
1199 pthread_mutex_unlock(&consumer_data.lock);
1200
1201 if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) {
1202 notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD);
1203 }
1204
1205 return 0;
1206 }
1207
1208 /*
1209 * Allocate the pollfd structure and the local view of the out fds to avoid
1210 * doing a lookup in the linked list and concurrency issues when writing is
1211 * needed. Called with consumer_data.lock held.
1212 *
1213 * Returns the number of fds in the structures.
1214 */
1215 static int update_poll_array(struct lttng_consumer_local_data *ctx,
1216 struct pollfd **pollfd, struct lttng_consumer_stream **local_stream,
1217 struct lttng_ht *ht, int *nb_inactive_fd)
1218 {
1219 int i = 0;
1220 struct lttng_ht_iter iter;
1221 struct lttng_consumer_stream *stream;
1222
1223 assert(ctx);
1224 assert(ht);
1225 assert(pollfd);
1226 assert(local_stream);
1227
1228 DBG("Updating poll fd array");
1229 *nb_inactive_fd = 0;
1230 rcu_read_lock();
1231 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1232 /*
1233 * Only active streams with an active end point can be added to the
1234 * poll set and local stream storage of the thread.
1235 *
1236 * There is a potential race here for endpoint_status to be updated
1237 * just after the check. However, this is OK since the stream(s) will
1238 * be deleted once the thread is notified that the end point state has
1239 * changed where this function will be called back again.
1240 *
1241 * We track the number of inactive FDs because they still need to be
1242 * closed by the polling thread after a wakeup on the data_pipe or
1243 * metadata_pipe.
1244 */
1245 if (stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
1246 (*nb_inactive_fd)++;
1247 continue;
1248 }
1249 /*
1250 * This clobbers way too much the debug output. Uncomment that if you
1251 * need it for debugging purposes.
1252 */
1253 (*pollfd)[i].fd = stream->wait_fd;
1254 (*pollfd)[i].events = POLLIN | POLLPRI;
1255 local_stream[i] = stream;
1256 i++;
1257 }
1258 rcu_read_unlock();
1259
1260 /*
1261 * Insert the consumer_data_pipe at the end of the array and don't
1262 * increment i so nb_fd is the number of real FD.
1263 */
1264 (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe);
1265 (*pollfd)[i].events = POLLIN | POLLPRI;
1266
1267 (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe);
1268 (*pollfd)[i + 1].events = POLLIN | POLLPRI;
1269 return i;
1270 }
1271
1272 /*
1273 * Poll on the should_quit pipe and the command socket return -1 on
1274 * error, 1 if should exit, 0 if data is available on the command socket
1275 */
1276 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1277 {
1278 int num_rdy;
1279
1280 restart:
1281 num_rdy = poll(consumer_sockpoll, 2, -1);
1282 if (num_rdy == -1) {
1283 /*
1284 * Restart interrupted system call.
1285 */
1286 if (errno == EINTR) {
1287 goto restart;
1288 }
1289 PERROR("Poll error");
1290 return -1;
1291 }
1292 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
1293 DBG("consumer_should_quit wake up");
1294 return 1;
1295 }
1296 return 0;
1297 }
1298
1299 /*
1300 * Set the error socket.
1301 */
1302 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
1303 int sock)
1304 {
1305 ctx->consumer_error_socket = sock;
1306 }
1307
1308 /*
1309 * Set the command socket path.
1310 */
1311 void lttng_consumer_set_command_sock_path(
1312 struct lttng_consumer_local_data *ctx, char *sock)
1313 {
1314 ctx->consumer_command_sock_path = sock;
1315 }
1316
1317 /*
1318 * Send return code to the session daemon.
1319 * If the socket is not defined, we return 0, it is not a fatal error
1320 */
1321 int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd)
1322 {
1323 if (ctx->consumer_error_socket > 0) {
1324 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1325 sizeof(enum lttcomm_sessiond_command));
1326 }
1327
1328 return 0;
1329 }
1330
1331 /*
1332 * Close all the tracefiles and stream fds and MUST be called when all
1333 * instances are destroyed i.e. when all threads were joined and are ended.
1334 */
1335 void lttng_consumer_cleanup(void)
1336 {
1337 struct lttng_ht_iter iter;
1338 struct lttng_consumer_channel *channel;
1339 unsigned int trace_chunks_left;
1340
1341 rcu_read_lock();
1342
1343 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel,
1344 node.node) {
1345 consumer_del_channel(channel);
1346 }
1347
1348 rcu_read_unlock();
1349
1350 lttng_ht_destroy(consumer_data.channel_ht);
1351 lttng_ht_destroy(consumer_data.channels_by_session_id_ht);
1352
1353 cleanup_relayd_ht();
1354
1355 lttng_ht_destroy(consumer_data.stream_per_chan_id_ht);
1356
1357 /*
1358 * This HT contains streams that are freed by either the metadata thread or
1359 * the data thread so we do *nothing* on the hash table and simply destroy
1360 * it.
1361 */
1362 lttng_ht_destroy(consumer_data.stream_list_ht);
1363
1364 /*
1365 * Trace chunks in the registry may still exist if the session
1366 * daemon has encountered an internal error and could not
1367 * tear down its sessions and/or trace chunks properly.
1368 *
1369 * Release the session daemon's implicit reference to any remaining
1370 * trace chunk and print an error if any trace chunk was found. Note
1371 * that there are _no_ legitimate cases for trace chunks to be left,
1372 * it is a leak. However, it can happen following a crash of the
1373 * session daemon and not emptying the registry would cause an assertion
1374 * to hit.
1375 */
1376 trace_chunks_left = lttng_trace_chunk_registry_put_each_chunk(
1377 consumer_data.chunk_registry);
1378 if (trace_chunks_left) {
1379 ERR("%u trace chunks are leaked by lttng-consumerd. "
1380 "This can be caused by an internal error of the session daemon.",
1381 trace_chunks_left);
1382 }
1383 /* Run all callbacks freeing each chunk. */
1384 rcu_barrier();
1385 lttng_trace_chunk_registry_destroy(consumer_data.chunk_registry);
1386 }
1387
1388 /*
1389 * Called from signal handler.
1390 */
1391 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1392 {
1393 ssize_t ret;
1394
1395 CMM_STORE_SHARED(consumer_quit, 1);
1396 ret = lttng_write(ctx->consumer_should_quit[1], "4", 1);
1397 if (ret < 1) {
1398 PERROR("write consumer quit");
1399 }
1400
1401 DBG("Consumer flag that it should quit");
1402 }
1403
1404
1405 /*
1406 * Flush pending writes to trace output disk file.
1407 */
1408 static
1409 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1410 off_t orig_offset)
1411 {
1412 int ret;
1413 int outfd = stream->out_fd;
1414
1415 /*
1416 * This does a blocking write-and-wait on any page that belongs to the
1417 * subbuffer prior to the one we just wrote.
1418 * Don't care about error values, as these are just hints and ways to
1419 * limit the amount of page cache used.
1420 */
1421 if (orig_offset < stream->max_sb_size) {
1422 return;
1423 }
1424 lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size,
1425 stream->max_sb_size,
1426 SYNC_FILE_RANGE_WAIT_BEFORE
1427 | SYNC_FILE_RANGE_WRITE
1428 | SYNC_FILE_RANGE_WAIT_AFTER);
1429 /*
1430 * Give hints to the kernel about how we access the file:
1431 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1432 * we write it.
1433 *
1434 * We need to call fadvise again after the file grows because the
1435 * kernel does not seem to apply fadvise to non-existing parts of the
1436 * file.
1437 *
1438 * Call fadvise _after_ having waited for the page writeback to
1439 * complete because the dirty page writeback semantic is not well
1440 * defined. So it can be expected to lead to lower throughput in
1441 * streaming.
1442 */
1443 ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size,
1444 stream->max_sb_size, POSIX_FADV_DONTNEED);
1445 if (ret && ret != -ENOSYS) {
1446 errno = ret;
1447 PERROR("posix_fadvise on fd %i", outfd);
1448 }
1449 }
1450
1451 /*
1452 * Initialise the necessary environnement :
1453 * - create a new context
1454 * - create the poll_pipe
1455 * - create the should_quit pipe (for signal handler)
1456 * - create the thread pipe (for splice)
1457 *
1458 * Takes a function pointer as argument, this function is called when data is
1459 * available on a buffer. This function is responsible to do the
1460 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1461 * buffer configuration and then kernctl_put_next_subbuf at the end.
1462 *
1463 * Returns a pointer to the new context or NULL on error.
1464 */
1465 struct lttng_consumer_local_data *lttng_consumer_create(
1466 enum lttng_consumer_type type,
1467 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
1468 struct lttng_consumer_local_data *ctx),
1469 int (*recv_channel)(struct lttng_consumer_channel *channel),
1470 int (*recv_stream)(struct lttng_consumer_stream *stream),
1471 int (*update_stream)(uint64_t stream_key, uint32_t state))
1472 {
1473 int ret;
1474 struct lttng_consumer_local_data *ctx;
1475
1476 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1477 consumer_data.type == type);
1478 consumer_data.type = type;
1479
1480 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
1481 if (ctx == NULL) {
1482 PERROR("allocating context");
1483 goto error;
1484 }
1485
1486 ctx->consumer_error_socket = -1;
1487 ctx->consumer_metadata_socket = -1;
1488 pthread_mutex_init(&ctx->metadata_socket_lock, NULL);
1489 /* assign the callbacks */
1490 ctx->on_buffer_ready = buffer_ready;
1491 ctx->on_recv_channel = recv_channel;
1492 ctx->on_recv_stream = recv_stream;
1493 ctx->on_update_stream = update_stream;
1494
1495 ctx->consumer_data_pipe = lttng_pipe_open(0);
1496 if (!ctx->consumer_data_pipe) {
1497 goto error_poll_pipe;
1498 }
1499
1500 ctx->consumer_wakeup_pipe = lttng_pipe_open(0);
1501 if (!ctx->consumer_wakeup_pipe) {
1502 goto error_wakeup_pipe;
1503 }
1504
1505 ret = pipe(ctx->consumer_should_quit);
1506 if (ret < 0) {
1507 PERROR("Error creating recv pipe");
1508 goto error_quit_pipe;
1509 }
1510
1511 ret = pipe(ctx->consumer_channel_pipe);
1512 if (ret < 0) {
1513 PERROR("Error creating channel pipe");
1514 goto error_channel_pipe;
1515 }
1516
1517 ctx->consumer_metadata_pipe = lttng_pipe_open(0);
1518 if (!ctx->consumer_metadata_pipe) {
1519 goto error_metadata_pipe;
1520 }
1521
1522 ctx->channel_monitor_pipe = -1;
1523
1524 return ctx;
1525
1526 error_metadata_pipe:
1527 utils_close_pipe(ctx->consumer_channel_pipe);
1528 error_channel_pipe:
1529 utils_close_pipe(ctx->consumer_should_quit);
1530 error_quit_pipe:
1531 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
1532 error_wakeup_pipe:
1533 lttng_pipe_destroy(ctx->consumer_data_pipe);
1534 error_poll_pipe:
1535 free(ctx);
1536 error:
1537 return NULL;
1538 }
1539
1540 /*
1541 * Iterate over all streams of the hashtable and free them properly.
1542 */
1543 static void destroy_data_stream_ht(struct lttng_ht *ht)
1544 {
1545 struct lttng_ht_iter iter;
1546 struct lttng_consumer_stream *stream;
1547
1548 if (ht == NULL) {
1549 return;
1550 }
1551
1552 rcu_read_lock();
1553 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1554 /*
1555 * Ignore return value since we are currently cleaning up so any error
1556 * can't be handled.
1557 */
1558 (void) consumer_del_stream(stream, ht);
1559 }
1560 rcu_read_unlock();
1561
1562 lttng_ht_destroy(ht);
1563 }
1564
1565 /*
1566 * Iterate over all streams of the metadata hashtable and free them
1567 * properly.
1568 */
1569 static void destroy_metadata_stream_ht(struct lttng_ht *ht)
1570 {
1571 struct lttng_ht_iter iter;
1572 struct lttng_consumer_stream *stream;
1573
1574 if (ht == NULL) {
1575 return;
1576 }
1577
1578 rcu_read_lock();
1579 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1580 /*
1581 * Ignore return value since we are currently cleaning up so any error
1582 * can't be handled.
1583 */
1584 (void) consumer_del_metadata_stream(stream, ht);
1585 }
1586 rcu_read_unlock();
1587
1588 lttng_ht_destroy(ht);
1589 }
1590
1591 /*
1592 * Close all fds associated with the instance and free the context.
1593 */
1594 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1595 {
1596 int ret;
1597
1598 DBG("Consumer destroying it. Closing everything.");
1599
1600 if (!ctx) {
1601 return;
1602 }
1603
1604 destroy_data_stream_ht(data_ht);
1605 destroy_metadata_stream_ht(metadata_ht);
1606
1607 ret = close(ctx->consumer_error_socket);
1608 if (ret) {
1609 PERROR("close");
1610 }
1611 ret = close(ctx->consumer_metadata_socket);
1612 if (ret) {
1613 PERROR("close");
1614 }
1615 utils_close_pipe(ctx->consumer_channel_pipe);
1616 lttng_pipe_destroy(ctx->consumer_data_pipe);
1617 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1618 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
1619 utils_close_pipe(ctx->consumer_should_quit);
1620
1621 unlink(ctx->consumer_command_sock_path);
1622 free(ctx);
1623 }
1624
1625 /*
1626 * Write the metadata stream id on the specified file descriptor.
1627 */
1628 static int write_relayd_metadata_id(int fd,
1629 struct lttng_consumer_stream *stream,
1630 unsigned long padding)
1631 {
1632 ssize_t ret;
1633 struct lttcomm_relayd_metadata_payload hdr;
1634
1635 hdr.stream_id = htobe64(stream->relayd_stream_id);
1636 hdr.padding_size = htobe32(padding);
1637 ret = lttng_write(fd, (void *) &hdr, sizeof(hdr));
1638 if (ret < sizeof(hdr)) {
1639 /*
1640 * This error means that the fd's end is closed so ignore the PERROR
1641 * not to clubber the error output since this can happen in a normal
1642 * code path.
1643 */
1644 if (errno != EPIPE) {
1645 PERROR("write metadata stream id");
1646 }
1647 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
1648 /*
1649 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1650 * handle writting the missing part so report that as an error and
1651 * don't lie to the caller.
1652 */
1653 ret = -1;
1654 goto end;
1655 }
1656 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1657 stream->relayd_stream_id, padding);
1658
1659 end:
1660 return (int) ret;
1661 }
1662
1663 /*
1664 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1665 * core function for writing trace buffers to either the local filesystem or
1666 * the network.
1667 *
1668 * It must be called with the stream and the channel lock held.
1669 *
1670 * Careful review MUST be put if any changes occur!
1671 *
1672 * Returns the number of bytes written
1673 */
1674 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1675 struct lttng_consumer_local_data *ctx,
1676 struct lttng_consumer_stream *stream,
1677 const struct lttng_buffer_view *buffer,
1678 unsigned long padding,
1679 struct ctf_packet_index *index)
1680 {
1681 ssize_t ret = 0;
1682 off_t orig_offset = stream->out_fd_offset;
1683 /* Default is on the disk */
1684 int outfd = stream->out_fd;
1685 struct consumer_relayd_sock_pair *relayd = NULL;
1686 unsigned int relayd_hang_up = 0;
1687 const size_t subbuf_content_size = buffer->size - padding;
1688 size_t write_len;
1689
1690 /* RCU lock for the relayd pointer */
1691 rcu_read_lock();
1692 assert(stream->net_seq_idx != (uint64_t) -1ULL ||
1693 stream->trace_chunk);
1694
1695 /* Flag that the current stream if set for network streaming. */
1696 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1697 relayd = consumer_find_relayd(stream->net_seq_idx);
1698 if (relayd == NULL) {
1699 ret = -EPIPE;
1700 goto end;
1701 }
1702 }
1703
1704 /* Handle stream on the relayd if the output is on the network */
1705 if (relayd) {
1706 unsigned long netlen = subbuf_content_size;
1707
1708 /*
1709 * Lock the control socket for the complete duration of the function
1710 * since from this point on we will use the socket.
1711 */
1712 if (stream->metadata_flag) {
1713 /* Metadata requires the control socket. */
1714 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1715 if (stream->reset_metadata_flag) {
1716 ret = relayd_reset_metadata(&relayd->control_sock,
1717 stream->relayd_stream_id,
1718 stream->metadata_version);
1719 if (ret < 0) {
1720 relayd_hang_up = 1;
1721 goto write_error;
1722 }
1723 stream->reset_metadata_flag = 0;
1724 }
1725 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
1726 }
1727
1728 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
1729 if (ret < 0) {
1730 relayd_hang_up = 1;
1731 goto write_error;
1732 }
1733 /* Use the returned socket. */
1734 outfd = ret;
1735
1736 /* Write metadata stream id before payload */
1737 if (stream->metadata_flag) {
1738 ret = write_relayd_metadata_id(outfd, stream, padding);
1739 if (ret < 0) {
1740 relayd_hang_up = 1;
1741 goto write_error;
1742 }
1743 }
1744
1745 write_len = subbuf_content_size;
1746 } else {
1747 /* No streaming; we have to write the full padding. */
1748 if (stream->metadata_flag && stream->reset_metadata_flag) {
1749 ret = utils_truncate_stream_file(stream->out_fd, 0);
1750 if (ret < 0) {
1751 ERR("Reset metadata file");
1752 goto end;
1753 }
1754 stream->reset_metadata_flag = 0;
1755 }
1756
1757 /*
1758 * Check if we need to change the tracefile before writing the packet.
1759 */
1760 if (stream->chan->tracefile_size > 0 &&
1761 (stream->tracefile_size_current + buffer->size) >
1762 stream->chan->tracefile_size) {
1763 ret = consumer_stream_rotate_output_files(stream);
1764 if (ret) {
1765 goto end;
1766 }
1767 outfd = stream->out_fd;
1768 orig_offset = 0;
1769 }
1770 stream->tracefile_size_current += buffer->size;
1771 if (index) {
1772 index->offset = htobe64(stream->out_fd_offset);
1773 }
1774
1775 write_len = buffer->size;
1776 }
1777
1778 /*
1779 * This call guarantee that len or less is returned. It's impossible to
1780 * receive a ret value that is bigger than len.
1781 */
1782 ret = lttng_write(outfd, buffer->data, write_len);
1783 DBG("Consumer mmap write() ret %zd (len %lu)", ret, write_len);
1784 if (ret < 0 || ((size_t) ret != write_len)) {
1785 /*
1786 * Report error to caller if nothing was written else at least send the
1787 * amount written.
1788 */
1789 if (ret < 0) {
1790 ret = -errno;
1791 }
1792 relayd_hang_up = 1;
1793
1794 /* Socket operation failed. We consider the relayd dead */
1795 if (errno == EPIPE) {
1796 /*
1797 * This is possible if the fd is closed on the other side
1798 * (outfd) or any write problem. It can be verbose a bit for a
1799 * normal execution if for instance the relayd is stopped
1800 * abruptly. This can happen so set this to a DBG statement.
1801 */
1802 DBG("Consumer mmap write detected relayd hang up");
1803 } else {
1804 /* Unhandled error, print it and stop function right now. */
1805 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret,
1806 write_len);
1807 }
1808 goto write_error;
1809 }
1810 stream->output_written += ret;
1811
1812 /* This call is useless on a socket so better save a syscall. */
1813 if (!relayd) {
1814 /* This won't block, but will start writeout asynchronously */
1815 lttng_sync_file_range(outfd, stream->out_fd_offset, write_len,
1816 SYNC_FILE_RANGE_WRITE);
1817 stream->out_fd_offset += write_len;
1818 lttng_consumer_sync_trace_file(stream, orig_offset);
1819 }
1820
1821 write_error:
1822 /*
1823 * This is a special case that the relayd has closed its socket. Let's
1824 * cleanup the relayd object and all associated streams.
1825 */
1826 if (relayd && relayd_hang_up) {
1827 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
1828 lttng_consumer_cleanup_relayd(relayd);
1829 }
1830
1831 end:
1832 /* Unlock only if ctrl socket used */
1833 if (relayd && stream->metadata_flag) {
1834 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1835 }
1836
1837 rcu_read_unlock();
1838 return ret;
1839 }
1840
1841 /*
1842 * Splice the data from the ring buffer to the tracefile.
1843 *
1844 * It must be called with the stream lock held.
1845 *
1846 * Returns the number of bytes spliced.
1847 */
1848 ssize_t lttng_consumer_on_read_subbuffer_splice(
1849 struct lttng_consumer_local_data *ctx,
1850 struct lttng_consumer_stream *stream, unsigned long len,
1851 unsigned long padding,
1852 struct ctf_packet_index *index)
1853 {
1854 ssize_t ret = 0, written = 0, ret_splice = 0;
1855 loff_t offset = 0;
1856 off_t orig_offset = stream->out_fd_offset;
1857 int fd = stream->wait_fd;
1858 /* Default is on the disk */
1859 int outfd = stream->out_fd;
1860 struct consumer_relayd_sock_pair *relayd = NULL;
1861 int *splice_pipe;
1862 unsigned int relayd_hang_up = 0;
1863
1864 switch (consumer_data.type) {
1865 case LTTNG_CONSUMER_KERNEL:
1866 break;
1867 case LTTNG_CONSUMER32_UST:
1868 case LTTNG_CONSUMER64_UST:
1869 /* Not supported for user space tracing */
1870 return -ENOSYS;
1871 default:
1872 ERR("Unknown consumer_data type");
1873 assert(0);
1874 }
1875
1876 /* RCU lock for the relayd pointer */
1877 rcu_read_lock();
1878
1879 /* Flag that the current stream if set for network streaming. */
1880 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1881 relayd = consumer_find_relayd(stream->net_seq_idx);
1882 if (relayd == NULL) {
1883 written = -ret;
1884 goto end;
1885 }
1886 }
1887 splice_pipe = stream->splice_pipe;
1888
1889 /* Write metadata stream id before payload */
1890 if (relayd) {
1891 unsigned long total_len = len;
1892
1893 if (stream->metadata_flag) {
1894 /*
1895 * Lock the control socket for the complete duration of the function
1896 * since from this point on we will use the socket.
1897 */
1898 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1899
1900 if (stream->reset_metadata_flag) {
1901 ret = relayd_reset_metadata(&relayd->control_sock,
1902 stream->relayd_stream_id,
1903 stream->metadata_version);
1904 if (ret < 0) {
1905 relayd_hang_up = 1;
1906 goto write_error;
1907 }
1908 stream->reset_metadata_flag = 0;
1909 }
1910 ret = write_relayd_metadata_id(splice_pipe[1], stream,
1911 padding);
1912 if (ret < 0) {
1913 written = ret;
1914 relayd_hang_up = 1;
1915 goto write_error;
1916 }
1917
1918 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1919 }
1920
1921 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1922 if (ret < 0) {
1923 written = ret;
1924 relayd_hang_up = 1;
1925 goto write_error;
1926 }
1927 /* Use the returned socket. */
1928 outfd = ret;
1929 } else {
1930 /* No streaming, we have to set the len with the full padding */
1931 len += padding;
1932
1933 if (stream->metadata_flag && stream->reset_metadata_flag) {
1934 ret = utils_truncate_stream_file(stream->out_fd, 0);
1935 if (ret < 0) {
1936 ERR("Reset metadata file");
1937 goto end;
1938 }
1939 stream->reset_metadata_flag = 0;
1940 }
1941 /*
1942 * Check if we need to change the tracefile before writing the packet.
1943 */
1944 if (stream->chan->tracefile_size > 0 &&
1945 (stream->tracefile_size_current + len) >
1946 stream->chan->tracefile_size) {
1947 ret = consumer_stream_rotate_output_files(stream);
1948 if (ret < 0) {
1949 written = ret;
1950 goto end;
1951 }
1952 outfd = stream->out_fd;
1953 orig_offset = 0;
1954 }
1955 stream->tracefile_size_current += len;
1956 index->offset = htobe64(stream->out_fd_offset);
1957 }
1958
1959 while (len > 0) {
1960 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1961 (unsigned long)offset, len, fd, splice_pipe[1]);
1962 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
1963 SPLICE_F_MOVE | SPLICE_F_MORE);
1964 DBG("splice chan to pipe, ret %zd", ret_splice);
1965 if (ret_splice < 0) {
1966 ret = errno;
1967 written = -ret;
1968 PERROR("Error in relay splice");
1969 goto splice_error;
1970 }
1971
1972 /* Handle stream on the relayd if the output is on the network */
1973 if (relayd && stream->metadata_flag) {
1974 size_t metadata_payload_size =
1975 sizeof(struct lttcomm_relayd_metadata_payload);
1976
1977 /* Update counter to fit the spliced data */
1978 ret_splice += metadata_payload_size;
1979 len += metadata_payload_size;
1980 /*
1981 * We do this so the return value can match the len passed as
1982 * argument to this function.
1983 */
1984 written -= metadata_payload_size;
1985 }
1986
1987 /* Splice data out */
1988 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
1989 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1990 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1991 outfd, ret_splice);
1992 if (ret_splice < 0) {
1993 ret = errno;
1994 written = -ret;
1995 relayd_hang_up = 1;
1996 goto write_error;
1997 } else if (ret_splice > len) {
1998 /*
1999 * We don't expect this code path to be executed but you never know
2000 * so this is an extra protection agains a buggy splice().
2001 */
2002 ret = errno;
2003 written += ret_splice;
2004 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice,
2005 len);
2006 goto splice_error;
2007 } else {
2008 /* All good, update current len and continue. */
2009 len -= ret_splice;
2010 }
2011
2012 /* This call is useless on a socket so better save a syscall. */
2013 if (!relayd) {
2014 /* This won't block, but will start writeout asynchronously */
2015 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
2016 SYNC_FILE_RANGE_WRITE);
2017 stream->out_fd_offset += ret_splice;
2018 }
2019 stream->output_written += ret_splice;
2020 written += ret_splice;
2021 }
2022 if (!relayd) {
2023 lttng_consumer_sync_trace_file(stream, orig_offset);
2024 }
2025 goto end;
2026
2027 write_error:
2028 /*
2029 * This is a special case that the relayd has closed its socket. Let's
2030 * cleanup the relayd object and all associated streams.
2031 */
2032 if (relayd && relayd_hang_up) {
2033 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
2034 lttng_consumer_cleanup_relayd(relayd);
2035 /* Skip splice error so the consumer does not fail */
2036 goto end;
2037 }
2038
2039 splice_error:
2040 /* send the appropriate error description to sessiond */
2041 switch (ret) {
2042 case EINVAL:
2043 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
2044 break;
2045 case ENOMEM:
2046 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
2047 break;
2048 case ESPIPE:
2049 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
2050 break;
2051 }
2052
2053 end:
2054 if (relayd && stream->metadata_flag) {
2055 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2056 }
2057
2058 rcu_read_unlock();
2059 return written;
2060 }
2061
2062 /*
2063 * Sample the snapshot positions for a specific fd
2064 *
2065 * Returns 0 on success, < 0 on error
2066 */
2067 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream *stream)
2068 {
2069 switch (consumer_data.type) {
2070 case LTTNG_CONSUMER_KERNEL:
2071 return lttng_kconsumer_sample_snapshot_positions(stream);
2072 case LTTNG_CONSUMER32_UST:
2073 case LTTNG_CONSUMER64_UST:
2074 return lttng_ustconsumer_sample_snapshot_positions(stream);
2075 default:
2076 ERR("Unknown consumer_data type");
2077 assert(0);
2078 return -ENOSYS;
2079 }
2080 }
2081 /*
2082 * Take a snapshot for a specific fd
2083 *
2084 * Returns 0 on success, < 0 on error
2085 */
2086 int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
2087 {
2088 switch (consumer_data.type) {
2089 case LTTNG_CONSUMER_KERNEL:
2090 return lttng_kconsumer_take_snapshot(stream);
2091 case LTTNG_CONSUMER32_UST:
2092 case LTTNG_CONSUMER64_UST:
2093 return lttng_ustconsumer_take_snapshot(stream);
2094 default:
2095 ERR("Unknown consumer_data type");
2096 assert(0);
2097 return -ENOSYS;
2098 }
2099 }
2100
2101 /*
2102 * Get the produced position
2103 *
2104 * Returns 0 on success, < 0 on error
2105 */
2106 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
2107 unsigned long *pos)
2108 {
2109 switch (consumer_data.type) {
2110 case LTTNG_CONSUMER_KERNEL:
2111 return lttng_kconsumer_get_produced_snapshot(stream, pos);
2112 case LTTNG_CONSUMER32_UST:
2113 case LTTNG_CONSUMER64_UST:
2114 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
2115 default:
2116 ERR("Unknown consumer_data type");
2117 assert(0);
2118 return -ENOSYS;
2119 }
2120 }
2121
2122 /*
2123 * Get the consumed position (free-running counter position in bytes).
2124 *
2125 * Returns 0 on success, < 0 on error
2126 */
2127 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
2128 unsigned long *pos)
2129 {
2130 switch (consumer_data.type) {
2131 case LTTNG_CONSUMER_KERNEL:
2132 return lttng_kconsumer_get_consumed_snapshot(stream, pos);
2133 case LTTNG_CONSUMER32_UST:
2134 case LTTNG_CONSUMER64_UST:
2135 return lttng_ustconsumer_get_consumed_snapshot(stream, pos);
2136 default:
2137 ERR("Unknown consumer_data type");
2138 assert(0);
2139 return -ENOSYS;
2140 }
2141 }
2142
2143 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
2144 int sock, struct pollfd *consumer_sockpoll)
2145 {
2146 switch (consumer_data.type) {
2147 case LTTNG_CONSUMER_KERNEL:
2148 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
2149 case LTTNG_CONSUMER32_UST:
2150 case LTTNG_CONSUMER64_UST:
2151 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
2152 default:
2153 ERR("Unknown consumer_data type");
2154 assert(0);
2155 return -ENOSYS;
2156 }
2157 }
2158
2159 static
2160 void lttng_consumer_close_all_metadata(void)
2161 {
2162 switch (consumer_data.type) {
2163 case LTTNG_CONSUMER_KERNEL:
2164 /*
2165 * The Kernel consumer has a different metadata scheme so we don't
2166 * close anything because the stream will be closed by the session
2167 * daemon.
2168 */
2169 break;
2170 case LTTNG_CONSUMER32_UST:
2171 case LTTNG_CONSUMER64_UST:
2172 /*
2173 * Close all metadata streams. The metadata hash table is passed and
2174 * this call iterates over it by closing all wakeup fd. This is safe
2175 * because at this point we are sure that the metadata producer is
2176 * either dead or blocked.
2177 */
2178 lttng_ustconsumer_close_all_metadata(metadata_ht);
2179 break;
2180 default:
2181 ERR("Unknown consumer_data type");
2182 assert(0);
2183 }
2184 }
2185
2186 /*
2187 * Clean up a metadata stream and free its memory.
2188 */
2189 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
2190 struct lttng_ht *ht)
2191 {
2192 struct lttng_consumer_channel *channel = NULL;
2193 bool free_channel = false;
2194
2195 assert(stream);
2196 /*
2197 * This call should NEVER receive regular stream. It must always be
2198 * metadata stream and this is crucial for data structure synchronization.
2199 */
2200 assert(stream->metadata_flag);
2201
2202 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
2203
2204 pthread_mutex_lock(&consumer_data.lock);
2205 /*
2206 * Note that this assumes that a stream's channel is never changed and
2207 * that the stream's lock doesn't need to be taken to sample its
2208 * channel.
2209 */
2210 channel = stream->chan;
2211 pthread_mutex_lock(&channel->lock);
2212 pthread_mutex_lock(&stream->lock);
2213 if (channel->metadata_cache) {
2214 /* Only applicable to userspace consumers. */
2215 pthread_mutex_lock(&channel->metadata_cache->lock);
2216 }
2217
2218 /* Remove any reference to that stream. */
2219 consumer_stream_delete(stream, ht);
2220
2221 /* Close down everything including the relayd if one. */
2222 consumer_stream_close(stream);
2223 /* Destroy tracer buffers of the stream. */
2224 consumer_stream_destroy_buffers(stream);
2225
2226 /* Atomically decrement channel refcount since other threads can use it. */
2227 if (!uatomic_sub_return(&channel->refcount, 1)
2228 && !uatomic_read(&channel->nb_init_stream_left)) {
2229 /* Go for channel deletion! */
2230 free_channel = true;
2231 }
2232 stream->chan = NULL;
2233
2234 /*
2235 * Nullify the stream reference so it is not used after deletion. The
2236 * channel lock MUST be acquired before being able to check for a NULL
2237 * pointer value.
2238 */
2239 channel->metadata_stream = NULL;
2240
2241 if (channel->metadata_cache) {
2242 pthread_mutex_unlock(&channel->metadata_cache->lock);
2243 }
2244 pthread_mutex_unlock(&stream->lock);
2245 pthread_mutex_unlock(&channel->lock);
2246 pthread_mutex_unlock(&consumer_data.lock);
2247
2248 if (free_channel) {
2249 consumer_del_channel(channel);
2250 }
2251
2252 lttng_trace_chunk_put(stream->trace_chunk);
2253 stream->trace_chunk = NULL;
2254 consumer_stream_free(stream);
2255 }
2256
2257 /*
2258 * Action done with the metadata stream when adding it to the consumer internal
2259 * data structures to handle it.
2260 */
2261 void consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
2262 {
2263 struct lttng_ht *ht = metadata_ht;
2264 struct lttng_ht_iter iter;
2265 struct lttng_ht_node_u64 *node;
2266
2267 assert(stream);
2268 assert(ht);
2269
2270 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
2271
2272 pthread_mutex_lock(&consumer_data.lock);
2273 pthread_mutex_lock(&stream->chan->lock);
2274 pthread_mutex_lock(&stream->chan->timer_lock);
2275 pthread_mutex_lock(&stream->lock);
2276
2277 /*
2278 * From here, refcounts are updated so be _careful_ when returning an error
2279 * after this point.
2280 */
2281
2282 rcu_read_lock();
2283
2284 /*
2285 * Lookup the stream just to make sure it does not exist in our internal
2286 * state. This should NEVER happen.
2287 */
2288 lttng_ht_lookup(ht, &stream->key, &iter);
2289 node = lttng_ht_iter_get_node_u64(&iter);
2290 assert(!node);
2291
2292 /*
2293 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2294 * in terms of destroying the associated channel, because the action that
2295 * causes the count to become 0 also causes a stream to be added. The
2296 * channel deletion will thus be triggered by the following removal of this
2297 * stream.
2298 */
2299 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
2300 /* Increment refcount before decrementing nb_init_stream_left */
2301 cmm_smp_wmb();
2302 uatomic_dec(&stream->chan->nb_init_stream_left);
2303 }
2304
2305 lttng_ht_add_unique_u64(ht, &stream->node);
2306
2307 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
2308 &stream->node_channel_id);
2309
2310 /*
2311 * Add stream to the stream_list_ht of the consumer data. No need to steal
2312 * the key since the HT does not use it and we allow to add redundant keys
2313 * into this table.
2314 */
2315 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
2316
2317 rcu_read_unlock();
2318
2319 pthread_mutex_unlock(&stream->lock);
2320 pthread_mutex_unlock(&stream->chan->lock);
2321 pthread_mutex_unlock(&stream->chan->timer_lock);
2322 pthread_mutex_unlock(&consumer_data.lock);
2323 }
2324
2325 /*
2326 * Delete data stream that are flagged for deletion (endpoint_status).
2327 */
2328 static void validate_endpoint_status_data_stream(void)
2329 {
2330 struct lttng_ht_iter iter;
2331 struct lttng_consumer_stream *stream;
2332
2333 DBG("Consumer delete flagged data stream");
2334
2335 rcu_read_lock();
2336 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2337 /* Validate delete flag of the stream */
2338 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2339 continue;
2340 }
2341 /* Delete it right now */
2342 consumer_del_stream(stream, data_ht);
2343 }
2344 rcu_read_unlock();
2345 }
2346
2347 /*
2348 * Delete metadata stream that are flagged for deletion (endpoint_status).
2349 */
2350 static void validate_endpoint_status_metadata_stream(
2351 struct lttng_poll_event *pollset)
2352 {
2353 struct lttng_ht_iter iter;
2354 struct lttng_consumer_stream *stream;
2355
2356 DBG("Consumer delete flagged metadata stream");
2357
2358 assert(pollset);
2359
2360 rcu_read_lock();
2361 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2362 /* Validate delete flag of the stream */
2363 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2364 continue;
2365 }
2366 /*
2367 * Remove from pollset so the metadata thread can continue without
2368 * blocking on a deleted stream.
2369 */
2370 lttng_poll_del(pollset, stream->wait_fd);
2371
2372 /* Delete it right now */
2373 consumer_del_metadata_stream(stream, metadata_ht);
2374 }
2375 rcu_read_unlock();
2376 }
2377
2378 /*
2379 * Thread polls on metadata file descriptor and write them on disk or on the
2380 * network.
2381 */
2382 void *consumer_thread_metadata_poll(void *data)
2383 {
2384 int ret, i, pollfd, err = -1;
2385 uint32_t revents, nb_fd;
2386 struct lttng_consumer_stream *stream = NULL;
2387 struct lttng_ht_iter iter;
2388 struct lttng_ht_node_u64 *node;
2389 struct lttng_poll_event events;
2390 struct lttng_consumer_local_data *ctx = data;
2391 ssize_t len;
2392
2393 rcu_register_thread();
2394
2395 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA);
2396
2397 if (testpoint(consumerd_thread_metadata)) {
2398 goto error_testpoint;
2399 }
2400
2401 health_code_update();
2402
2403 DBG("Thread metadata poll started");
2404
2405 /* Size is set to 1 for the consumer_metadata pipe */
2406 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2407 if (ret < 0) {
2408 ERR("Poll set creation failed");
2409 goto end_poll;
2410 }
2411
2412 ret = lttng_poll_add(&events,
2413 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN);
2414 if (ret < 0) {
2415 goto end;
2416 }
2417
2418 /* Main loop */
2419 DBG("Metadata main loop started");
2420
2421 while (1) {
2422 restart:
2423 health_code_update();
2424 health_poll_entry();
2425 DBG("Metadata poll wait");
2426 ret = lttng_poll_wait(&events, -1);
2427 DBG("Metadata poll return from wait with %d fd(s)",
2428 LTTNG_POLL_GETNB(&events));
2429 health_poll_exit();
2430 DBG("Metadata event caught in thread");
2431 if (ret < 0) {
2432 if (errno == EINTR) {
2433 ERR("Poll EINTR caught");
2434 goto restart;
2435 }
2436 if (LTTNG_POLL_GETNB(&events) == 0) {
2437 err = 0; /* All is OK */
2438 }
2439 goto end;
2440 }
2441
2442 nb_fd = ret;
2443
2444 /* From here, the event is a metadata wait fd */
2445 for (i = 0; i < nb_fd; i++) {
2446 health_code_update();
2447
2448 revents = LTTNG_POLL_GETEV(&events, i);
2449 pollfd = LTTNG_POLL_GETFD(&events, i);
2450
2451 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
2452 if (revents & LPOLLIN) {
2453 ssize_t pipe_len;
2454
2455 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2456 &stream, sizeof(stream));
2457 if (pipe_len < sizeof(stream)) {
2458 if (pipe_len < 0) {
2459 PERROR("read metadata stream");
2460 }
2461 /*
2462 * Remove the pipe from the poll set and continue the loop
2463 * since their might be data to consume.
2464 */
2465 lttng_poll_del(&events,
2466 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2467 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2468 continue;
2469 }
2470
2471 /* A NULL stream means that the state has changed. */
2472 if (stream == NULL) {
2473 /* Check for deleted streams. */
2474 validate_endpoint_status_metadata_stream(&events);
2475 goto restart;
2476 }
2477
2478 DBG("Adding metadata stream %d to poll set",
2479 stream->wait_fd);
2480
2481 /* Add metadata stream to the global poll events list */
2482 lttng_poll_add(&events, stream->wait_fd,
2483 LPOLLIN | LPOLLPRI | LPOLLHUP);
2484 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2485 DBG("Metadata thread pipe hung up");
2486 /*
2487 * Remove the pipe from the poll set and continue the loop
2488 * since their might be data to consume.
2489 */
2490 lttng_poll_del(&events,
2491 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2492 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2493 continue;
2494 } else {
2495 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2496 goto end;
2497 }
2498
2499 /* Handle other stream */
2500 continue;
2501 }
2502
2503 rcu_read_lock();
2504 {
2505 uint64_t tmp_id = (uint64_t) pollfd;
2506
2507 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2508 }
2509 node = lttng_ht_iter_get_node_u64(&iter);
2510 assert(node);
2511
2512 stream = caa_container_of(node, struct lttng_consumer_stream,
2513 node);
2514
2515 if (revents & (LPOLLIN | LPOLLPRI)) {
2516 /* Get the data out of the metadata file descriptor */
2517 DBG("Metadata available on fd %d", pollfd);
2518 assert(stream->wait_fd == pollfd);
2519
2520 do {
2521 health_code_update();
2522
2523 len = ctx->on_buffer_ready(stream, ctx);
2524 /*
2525 * We don't check the return value here since if we get
2526 * a negative len, it means an error occurred thus we
2527 * simply remove it from the poll set and free the
2528 * stream.
2529 */
2530 } while (len > 0);
2531
2532 /* It's ok to have an unavailable sub-buffer */
2533 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2534 /* Clean up stream from consumer and free it. */
2535 lttng_poll_del(&events, stream->wait_fd);
2536 consumer_del_metadata_stream(stream, metadata_ht);
2537 }
2538 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2539 DBG("Metadata fd %d is hup|err.", pollfd);
2540 if (!stream->hangup_flush_done
2541 && (consumer_data.type == LTTNG_CONSUMER32_UST
2542 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2543 DBG("Attempting to flush and consume the UST buffers");
2544 lttng_ustconsumer_on_stream_hangup(stream);
2545
2546 /* We just flushed the stream now read it. */
2547 do {
2548 health_code_update();
2549
2550 len = ctx->on_buffer_ready(stream, ctx);
2551 /*
2552 * We don't check the return value here since if we get
2553 * a negative len, it means an error occurred thus we
2554 * simply remove it from the poll set and free the
2555 * stream.
2556 */
2557 } while (len > 0);
2558 }
2559
2560 lttng_poll_del(&events, stream->wait_fd);
2561 /*
2562 * This call update the channel states, closes file descriptors
2563 * and securely free the stream.
2564 */
2565 consumer_del_metadata_stream(stream, metadata_ht);
2566 } else {
2567 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2568 rcu_read_unlock();
2569 goto end;
2570 }
2571 /* Release RCU lock for the stream looked up */
2572 rcu_read_unlock();
2573 }
2574 }
2575
2576 /* All is OK */
2577 err = 0;
2578 end:
2579 DBG("Metadata poll thread exiting");
2580
2581 lttng_poll_clean(&events);
2582 end_poll:
2583 error_testpoint:
2584 if (err) {
2585 health_error();
2586 ERR("Health error occurred in %s", __func__);
2587 }
2588 health_unregister(health_consumerd);
2589 rcu_unregister_thread();
2590 return NULL;
2591 }
2592
2593 /*
2594 * This thread polls the fds in the set to consume the data and write
2595 * it to tracefile if necessary.
2596 */
2597 void *consumer_thread_data_poll(void *data)
2598 {
2599 int num_rdy, num_hup, high_prio, ret, i, err = -1;
2600 struct pollfd *pollfd = NULL;
2601 /* local view of the streams */
2602 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2603 /* local view of consumer_data.fds_count */
2604 int nb_fd = 0;
2605 /* 2 for the consumer_data_pipe and wake up pipe */
2606 const int nb_pipes_fd = 2;
2607 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2608 int nb_inactive_fd = 0;
2609 struct lttng_consumer_local_data *ctx = data;
2610 ssize_t len;
2611
2612 rcu_register_thread();
2613
2614 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA);
2615
2616 if (testpoint(consumerd_thread_data)) {
2617 goto error_testpoint;
2618 }
2619
2620 health_code_update();
2621
2622 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2623 if (local_stream == NULL) {
2624 PERROR("local_stream malloc");
2625 goto end;
2626 }
2627
2628 while (1) {
2629 health_code_update();
2630
2631 high_prio = 0;
2632 num_hup = 0;
2633
2634 /*
2635 * the fds set has been updated, we need to update our
2636 * local array as well
2637 */
2638 pthread_mutex_lock(&consumer_data.lock);
2639 if (consumer_data.need_update) {
2640 free(pollfd);
2641 pollfd = NULL;
2642
2643 free(local_stream);
2644 local_stream = NULL;
2645
2646 /* Allocate for all fds */
2647 pollfd = zmalloc((consumer_data.stream_count + nb_pipes_fd) * sizeof(struct pollfd));
2648 if (pollfd == NULL) {
2649 PERROR("pollfd malloc");
2650 pthread_mutex_unlock(&consumer_data.lock);
2651 goto end;
2652 }
2653
2654 local_stream = zmalloc((consumer_data.stream_count + nb_pipes_fd) *
2655 sizeof(struct lttng_consumer_stream *));
2656 if (local_stream == NULL) {
2657 PERROR("local_stream malloc");
2658 pthread_mutex_unlock(&consumer_data.lock);
2659 goto end;
2660 }
2661 ret = update_poll_array(ctx, &pollfd, local_stream,
2662 data_ht, &nb_inactive_fd);
2663 if (ret < 0) {
2664 ERR("Error in allocating pollfd or local_outfds");
2665 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2666 pthread_mutex_unlock(&consumer_data.lock);
2667 goto end;
2668 }
2669 nb_fd = ret;
2670 consumer_data.need_update = 0;
2671 }
2672 pthread_mutex_unlock(&consumer_data.lock);
2673
2674 /* No FDs and consumer_quit, consumer_cleanup the thread */
2675 if (nb_fd == 0 && nb_inactive_fd == 0 &&
2676 CMM_LOAD_SHARED(consumer_quit) == 1) {
2677 err = 0; /* All is OK */
2678 goto end;
2679 }
2680 /* poll on the array of fds */
2681 restart:
2682 DBG("polling on %d fd", nb_fd + nb_pipes_fd);
2683 if (testpoint(consumerd_thread_data_poll)) {
2684 goto end;
2685 }
2686 health_poll_entry();
2687 num_rdy = poll(pollfd, nb_fd + nb_pipes_fd, -1);
2688 health_poll_exit();
2689 DBG("poll num_rdy : %d", num_rdy);
2690 if (num_rdy == -1) {
2691 /*
2692 * Restart interrupted system call.
2693 */
2694 if (errno == EINTR) {
2695 goto restart;
2696 }
2697 PERROR("Poll error");
2698 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2699 goto end;
2700 } else if (num_rdy == 0) {
2701 DBG("Polling thread timed out");
2702 goto end;
2703 }
2704
2705 if (caa_unlikely(data_consumption_paused)) {
2706 DBG("Data consumption paused, sleeping...");
2707 sleep(1);
2708 goto restart;
2709 }
2710
2711 /*
2712 * If the consumer_data_pipe triggered poll go directly to the
2713 * beginning of the loop to update the array. We want to prioritize
2714 * array update over low-priority reads.
2715 */
2716 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2717 ssize_t pipe_readlen;
2718
2719 DBG("consumer_data_pipe wake up");
2720 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2721 &new_stream, sizeof(new_stream));
2722 if (pipe_readlen < sizeof(new_stream)) {
2723 PERROR("Consumer data pipe");
2724 /* Continue so we can at least handle the current stream(s). */
2725 continue;
2726 }
2727
2728 /*
2729 * If the stream is NULL, just ignore it. It's also possible that
2730 * the sessiond poll thread changed the consumer_quit state and is
2731 * waking us up to test it.
2732 */
2733 if (new_stream == NULL) {
2734 validate_endpoint_status_data_stream();
2735 continue;
2736 }
2737
2738 /* Continue to update the local streams and handle prio ones */
2739 continue;
2740 }
2741
2742 /* Handle wakeup pipe. */
2743 if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) {
2744 char dummy;
2745 ssize_t pipe_readlen;
2746
2747 pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy,
2748 sizeof(dummy));
2749 if (pipe_readlen < 0) {
2750 PERROR("Consumer data wakeup pipe");
2751 }
2752 /* We've been awakened to handle stream(s). */
2753 ctx->has_wakeup = 0;
2754 }
2755
2756 /* Take care of high priority channels first. */
2757 for (i = 0; i < nb_fd; i++) {
2758 health_code_update();
2759
2760 if (local_stream[i] == NULL) {
2761 continue;
2762 }
2763 if (pollfd[i].revents & POLLPRI) {
2764 DBG("Urgent read on fd %d", pollfd[i].fd);
2765 high_prio = 1;
2766 len = ctx->on_buffer_ready(local_stream[i], ctx);
2767 /* it's ok to have an unavailable sub-buffer */
2768 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2769 /* Clean the stream and free it. */
2770 consumer_del_stream(local_stream[i], data_ht);
2771 local_stream[i] = NULL;
2772 } else if (len > 0) {
2773 local_stream[i]->data_read = 1;
2774 }
2775 }
2776 }
2777
2778 /*
2779 * If we read high prio channel in this loop, try again
2780 * for more high prio data.
2781 */
2782 if (high_prio) {
2783 continue;
2784 }
2785
2786 /* Take care of low priority channels. */
2787 for (i = 0; i < nb_fd; i++) {
2788 health_code_update();
2789
2790 if (local_stream[i] == NULL) {
2791 continue;
2792 }
2793 if ((pollfd[i].revents & POLLIN) ||
2794 local_stream[i]->hangup_flush_done ||
2795 local_stream[i]->has_data) {
2796 DBG("Normal read on fd %d", pollfd[i].fd);
2797 len = ctx->on_buffer_ready(local_stream[i], ctx);
2798 /* it's ok to have an unavailable sub-buffer */
2799 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2800 /* Clean the stream and free it. */
2801 consumer_del_stream(local_stream[i], data_ht);
2802 local_stream[i] = NULL;
2803 } else if (len > 0) {
2804 local_stream[i]->data_read = 1;
2805 }
2806 }
2807 }
2808
2809 /* Handle hangup and errors */
2810 for (i = 0; i < nb_fd; i++) {
2811 health_code_update();
2812
2813 if (local_stream[i] == NULL) {
2814 continue;
2815 }
2816 if (!local_stream[i]->hangup_flush_done
2817 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2818 && (consumer_data.type == LTTNG_CONSUMER32_UST
2819 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2820 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2821 pollfd[i].fd);
2822 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2823 /* Attempt read again, for the data we just flushed. */
2824 local_stream[i]->data_read = 1;
2825 }
2826 /*
2827 * If the poll flag is HUP/ERR/NVAL and we have
2828 * read no data in this pass, we can remove the
2829 * stream from its hash table.
2830 */
2831 if ((pollfd[i].revents & POLLHUP)) {
2832 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2833 if (!local_stream[i]->data_read) {
2834 consumer_del_stream(local_stream[i], data_ht);
2835 local_stream[i] = NULL;
2836 num_hup++;
2837 }
2838 } else if (pollfd[i].revents & POLLERR) {
2839 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2840 if (!local_stream[i]->data_read) {
2841 consumer_del_stream(local_stream[i], data_ht);
2842 local_stream[i] = NULL;
2843 num_hup++;
2844 }
2845 } else if (pollfd[i].revents & POLLNVAL) {
2846 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2847 if (!local_stream[i]->data_read) {
2848 consumer_del_stream(local_stream[i], data_ht);
2849 local_stream[i] = NULL;
2850 num_hup++;
2851 }
2852 }
2853 if (local_stream[i] != NULL) {
2854 local_stream[i]->data_read = 0;
2855 }
2856 }
2857 }
2858 /* All is OK */
2859 err = 0;
2860 end:
2861 DBG("polling thread exiting");
2862 free(pollfd);
2863 free(local_stream);
2864
2865 /*
2866 * Close the write side of the pipe so epoll_wait() in
2867 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2868 * read side of the pipe. If we close them both, epoll_wait strangely does
2869 * not return and could create a endless wait period if the pipe is the
2870 * only tracked fd in the poll set. The thread will take care of closing
2871 * the read side.
2872 */
2873 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
2874
2875 error_testpoint:
2876 if (err) {
2877 health_error();
2878 ERR("Health error occurred in %s", __func__);
2879 }
2880 health_unregister(health_consumerd);
2881
2882 rcu_unregister_thread();
2883 return NULL;
2884 }
2885
2886 /*
2887 * Close wake-up end of each stream belonging to the channel. This will
2888 * allow the poll() on the stream read-side to detect when the
2889 * write-side (application) finally closes them.
2890 */
2891 static
2892 void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2893 {
2894 struct lttng_ht *ht;
2895 struct lttng_consumer_stream *stream;
2896 struct lttng_ht_iter iter;
2897
2898 ht = consumer_data.stream_per_chan_id_ht;
2899
2900 rcu_read_lock();
2901 cds_lfht_for_each_entry_duplicate(ht->ht,
2902 ht->hash_fct(&channel->key, lttng_ht_seed),
2903 ht->match_fct, &channel->key,
2904 &iter.iter, stream, node_channel_id.node) {
2905 /*
2906 * Protect against teardown with mutex.
2907 */
2908 pthread_mutex_lock(&stream->lock);
2909 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2910 goto next;
2911 }
2912 switch (consumer_data.type) {
2913 case LTTNG_CONSUMER_KERNEL:
2914 break;
2915 case LTTNG_CONSUMER32_UST:
2916 case LTTNG_CONSUMER64_UST:
2917 if (stream->metadata_flag) {
2918 /* Safe and protected by the stream lock. */
2919 lttng_ustconsumer_close_metadata(stream->chan);
2920 } else {
2921 /*
2922 * Note: a mutex is taken internally within
2923 * liblttng-ust-ctl to protect timer wakeup_fd
2924 * use from concurrent close.
2925 */
2926 lttng_ustconsumer_close_stream_wakeup(stream);
2927 }
2928 break;
2929 default:
2930 ERR("Unknown consumer_data type");
2931 assert(0);
2932 }
2933 next:
2934 pthread_mutex_unlock(&stream->lock);
2935 }
2936 rcu_read_unlock();
2937 }
2938
2939 static void destroy_channel_ht(struct lttng_ht *ht)
2940 {
2941 struct lttng_ht_iter iter;
2942 struct lttng_consumer_channel *channel;
2943 int ret;
2944
2945 if (ht == NULL) {
2946 return;
2947 }
2948
2949 rcu_read_lock();
2950 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2951 ret = lttng_ht_del(ht, &iter);
2952 assert(ret != 0);
2953 }
2954 rcu_read_unlock();
2955
2956 lttng_ht_destroy(ht);
2957 }
2958
2959 /*
2960 * This thread polls the channel fds to detect when they are being
2961 * closed. It closes all related streams if the channel is detected as
2962 * closed. It is currently only used as a shim layer for UST because the
2963 * consumerd needs to keep the per-stream wakeup end of pipes open for
2964 * periodical flush.
2965 */
2966 void *consumer_thread_channel_poll(void *data)
2967 {
2968 int ret, i, pollfd, err = -1;
2969 uint32_t revents, nb_fd;
2970 struct lttng_consumer_channel *chan = NULL;
2971 struct lttng_ht_iter iter;
2972 struct lttng_ht_node_u64 *node;
2973 struct lttng_poll_event events;
2974 struct lttng_consumer_local_data *ctx = data;
2975 struct lttng_ht *channel_ht;
2976
2977 rcu_register_thread();
2978
2979 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL);
2980
2981 if (testpoint(consumerd_thread_channel)) {
2982 goto error_testpoint;
2983 }
2984
2985 health_code_update();
2986
2987 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2988 if (!channel_ht) {
2989 /* ENOMEM at this point. Better to bail out. */
2990 goto end_ht;
2991 }
2992
2993 DBG("Thread channel poll started");
2994
2995 /* Size is set to 1 for the consumer_channel pipe */
2996 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2997 if (ret < 0) {
2998 ERR("Poll set creation failed");
2999 goto end_poll;
3000 }
3001
3002 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
3003 if (ret < 0) {
3004 goto end;
3005 }
3006
3007 /* Main loop */
3008 DBG("Channel main loop started");
3009
3010 while (1) {
3011 restart:
3012 health_code_update();
3013 DBG("Channel poll wait");
3014 health_poll_entry();
3015 ret = lttng_poll_wait(&events, -1);
3016 DBG("Channel poll return from wait with %d fd(s)",
3017 LTTNG_POLL_GETNB(&events));
3018 health_poll_exit();
3019 DBG("Channel event caught in thread");
3020 if (ret < 0) {
3021 if (errno == EINTR) {
3022 ERR("Poll EINTR caught");
3023 goto restart;
3024 }
3025 if (LTTNG_POLL_GETNB(&events) == 0) {
3026 err = 0; /* All is OK */
3027 }
3028 goto end;
3029 }
3030
3031 nb_fd = ret;
3032
3033 /* From here, the event is a channel wait fd */
3034 for (i = 0; i < nb_fd; i++) {
3035 health_code_update();
3036
3037 revents = LTTNG_POLL_GETEV(&events, i);
3038 pollfd = LTTNG_POLL_GETFD(&events, i);
3039
3040 if (pollfd == ctx->consumer_channel_pipe[0]) {
3041 if (revents & LPOLLIN) {
3042 enum consumer_channel_action action;
3043 uint64_t key;
3044
3045 ret = read_channel_pipe(ctx, &chan, &key, &action);
3046 if (ret <= 0) {
3047 if (ret < 0) {
3048 ERR("Error reading channel pipe");
3049 }
3050 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
3051 continue;
3052 }
3053
3054 switch (action) {
3055 case CONSUMER_CHANNEL_ADD:
3056 DBG("Adding channel %d to poll set",
3057 chan->wait_fd);
3058
3059 lttng_ht_node_init_u64(&chan->wait_fd_node,
3060 chan->wait_fd);
3061 rcu_read_lock();
3062 lttng_ht_add_unique_u64(channel_ht,
3063 &chan->wait_fd_node);
3064 rcu_read_unlock();
3065 /* Add channel to the global poll events list */
3066 lttng_poll_add(&events, chan->wait_fd,
3067 LPOLLERR | LPOLLHUP);
3068 break;
3069 case CONSUMER_CHANNEL_DEL:
3070 {
3071 /*
3072 * This command should never be called if the channel
3073 * has streams monitored by either the data or metadata
3074 * thread. The consumer only notify this thread with a
3075 * channel del. command if it receives a destroy
3076 * channel command from the session daemon that send it
3077 * if a command prior to the GET_CHANNEL failed.
3078 */
3079
3080 rcu_read_lock();
3081 chan = consumer_find_channel(key);
3082 if (!chan) {
3083 rcu_read_unlock();
3084 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
3085 break;
3086 }
3087 lttng_poll_del(&events, chan->wait_fd);
3088 iter.iter.node = &chan->wait_fd_node.node;
3089 ret = lttng_ht_del(channel_ht, &iter);
3090 assert(ret == 0);
3091
3092 switch (consumer_data.type) {
3093 case LTTNG_CONSUMER_KERNEL:
3094 break;
3095 case LTTNG_CONSUMER32_UST:
3096 case LTTNG_CONSUMER64_UST:
3097 health_code_update();
3098 /* Destroy streams that might have been left in the stream list. */
3099 clean_channel_stream_list(chan);
3100 break;
3101 default:
3102 ERR("Unknown consumer_data type");
3103 assert(0);
3104 }
3105
3106 /*
3107 * Release our own refcount. Force channel deletion even if
3108 * streams were not initialized.
3109 */
3110 if (!uatomic_sub_return(&chan->refcount, 1)) {
3111 consumer_del_channel(chan);
3112 }
3113 rcu_read_unlock();
3114 goto restart;
3115 }
3116 case CONSUMER_CHANNEL_QUIT:
3117 /*
3118 * Remove the pipe from the poll set and continue the loop
3119 * since their might be data to consume.
3120 */
3121 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
3122 continue;
3123 default:
3124 ERR("Unknown action");
3125 break;
3126 }
3127 } else if (revents & (LPOLLERR | LPOLLHUP)) {
3128 DBG("Channel thread pipe hung up");
3129 /*
3130 * Remove the pipe from the poll set and continue the loop
3131 * since their might be data to consume.
3132 */
3133 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
3134 continue;
3135 } else {
3136 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3137 goto end;
3138 }
3139
3140 /* Handle other stream */
3141 continue;
3142 }
3143
3144 rcu_read_lock();
3145 {
3146 uint64_t tmp_id = (uint64_t) pollfd;
3147
3148 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
3149 }
3150 node = lttng_ht_iter_get_node_u64(&iter);
3151 assert(node);
3152
3153 chan = caa_container_of(node, struct lttng_consumer_channel,
3154 wait_fd_node);
3155
3156 /* Check for error event */
3157 if (revents & (LPOLLERR | LPOLLHUP)) {
3158 DBG("Channel fd %d is hup|err.", pollfd);
3159
3160 lttng_poll_del(&events, chan->wait_fd);
3161 ret = lttng_ht_del(channel_ht, &iter);
3162 assert(ret == 0);
3163
3164 /*
3165 * This will close the wait fd for each stream associated to
3166 * this channel AND monitored by the data/metadata thread thus
3167 * will be clean by the right thread.
3168 */
3169 consumer_close_channel_streams(chan);
3170
3171 /* Release our own refcount */
3172 if (!uatomic_sub_return(&chan->refcount, 1)
3173 && !uatomic_read(&chan->nb_init_stream_left)) {
3174 consumer_del_channel(chan);
3175 }
3176 } else {
3177 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3178 rcu_read_unlock();
3179 goto end;
3180 }
3181
3182 /* Release RCU lock for the channel looked up */
3183 rcu_read_unlock();
3184 }
3185 }
3186
3187 /* All is OK */
3188 err = 0;
3189 end:
3190 lttng_poll_clean(&events);
3191 end_poll:
3192 destroy_channel_ht(channel_ht);
3193 end_ht:
3194 error_testpoint:
3195 DBG("Channel poll thread exiting");
3196 if (err) {
3197 health_error();
3198 ERR("Health error occurred in %s", __func__);
3199 }
3200 health_unregister(health_consumerd);
3201 rcu_unregister_thread();
3202 return NULL;
3203 }
3204
3205 static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
3206 struct pollfd *sockpoll, int client_socket)
3207 {
3208 int ret;
3209
3210 assert(ctx);
3211 assert(sockpoll);
3212
3213 ret = lttng_consumer_poll_socket(sockpoll);
3214 if (ret) {
3215 goto error;
3216 }
3217 DBG("Metadata connection on client_socket");
3218
3219 /* Blocking call, waiting for transmission */
3220 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
3221 if (ctx->consumer_metadata_socket < 0) {
3222 WARN("On accept metadata");
3223 ret = -1;
3224 goto error;
3225 }
3226 ret = 0;
3227
3228 error:
3229 return ret;
3230 }
3231
3232 /*
3233 * This thread listens on the consumerd socket and receives the file
3234 * descriptors from the session daemon.
3235 */
3236 void *consumer_thread_sessiond_poll(void *data)
3237 {
3238 int sock = -1, client_socket, ret, err = -1;
3239 /*
3240 * structure to poll for incoming data on communication socket avoids
3241 * making blocking sockets.
3242 */
3243 struct pollfd consumer_sockpoll[2];
3244 struct lttng_consumer_local_data *ctx = data;
3245
3246 rcu_register_thread();
3247
3248 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND);
3249
3250 if (testpoint(consumerd_thread_sessiond)) {
3251 goto error_testpoint;
3252 }
3253
3254 health_code_update();
3255
3256 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
3257 unlink(ctx->consumer_command_sock_path);
3258 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
3259 if (client_socket < 0) {
3260 ERR("Cannot create command socket");
3261 goto end;
3262 }
3263
3264 ret = lttcomm_listen_unix_sock(client_socket);
3265 if (ret < 0) {
3266 goto end;
3267 }
3268
3269 DBG("Sending ready command to lttng-sessiond");
3270 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3271 /* return < 0 on error, but == 0 is not fatal */
3272 if (ret < 0) {
3273 ERR("Error sending ready command to lttng-sessiond");
3274 goto end;
3275 }
3276
3277 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3278 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
3279 consumer_sockpoll[0].events = POLLIN | POLLPRI;
3280 consumer_sockpoll[1].fd = client_socket;
3281 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3282
3283 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3284 if (ret) {
3285 if (ret > 0) {
3286 /* should exit */
3287 err = 0;
3288 }
3289 goto end;
3290 }
3291 DBG("Connection on client_socket");
3292
3293 /* Blocking call, waiting for transmission */
3294 sock = lttcomm_accept_unix_sock(client_socket);
3295 if (sock < 0) {
3296 WARN("On accept");
3297 goto end;
3298 }
3299
3300 /*
3301 * Setup metadata socket which is the second socket connection on the
3302 * command unix socket.
3303 */
3304 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
3305 if (ret) {
3306 if (ret > 0) {
3307 /* should exit */
3308 err = 0;
3309 }
3310 goto end;
3311 }
3312
3313 /* This socket is not useful anymore. */
3314 ret = close(client_socket);
3315 if (ret < 0) {
3316 PERROR("close client_socket");
3317 }
3318 client_socket = -1;
3319
3320 /* update the polling structure to poll on the established socket */
3321 consumer_sockpoll[1].fd = sock;
3322 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3323
3324 while (1) {
3325 health_code_update();
3326
3327 health_poll_entry();
3328 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3329 health_poll_exit();
3330 if (ret) {
3331 if (ret > 0) {
3332 /* should exit */
3333 err = 0;
3334 }
3335 goto end;
3336 }
3337 DBG("Incoming command on sock");
3338 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
3339 if (ret <= 0) {
3340 /*
3341 * This could simply be a session daemon quitting. Don't output
3342 * ERR() here.
3343 */
3344 DBG("Communication interrupted on command socket");
3345 err = 0;
3346 goto end;
3347 }
3348 if (CMM_LOAD_SHARED(consumer_quit)) {
3349 DBG("consumer_thread_receive_fds received quit from signal");
3350 err = 0; /* All is OK */
3351 goto end;
3352 }
3353 DBG("received command on sock");
3354 }
3355 /* All is OK */
3356 err = 0;
3357
3358 end:
3359 DBG("Consumer thread sessiond poll exiting");
3360
3361 /*
3362 * Close metadata streams since the producer is the session daemon which
3363 * just died.
3364 *
3365 * NOTE: for now, this only applies to the UST tracer.
3366 */
3367 lttng_consumer_close_all_metadata();
3368
3369 /*
3370 * when all fds have hung up, the polling thread
3371 * can exit cleanly
3372 */
3373 CMM_STORE_SHARED(consumer_quit, 1);
3374
3375 /*
3376 * Notify the data poll thread to poll back again and test the
3377 * consumer_quit state that we just set so to quit gracefully.
3378 */
3379 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
3380
3381 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
3382
3383 notify_health_quit_pipe(health_quit_pipe);
3384
3385 /* Cleaning up possibly open sockets. */
3386 if (sock >= 0) {
3387 ret = close(sock);
3388 if (ret < 0) {
3389 PERROR("close sock sessiond poll");
3390 }
3391 }
3392 if (client_socket >= 0) {
3393 ret = close(client_socket);
3394 if (ret < 0) {
3395 PERROR("close client_socket sessiond poll");
3396 }
3397 }
3398
3399 error_testpoint:
3400 if (err) {
3401 health_error();
3402 ERR("Health error occurred in %s", __func__);
3403 }
3404 health_unregister(health_consumerd);
3405
3406 rcu_unregister_thread();
3407 return NULL;
3408 }
3409
3410 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
3411 struct lttng_consumer_local_data *ctx)
3412 {
3413 ssize_t ret;
3414
3415 pthread_mutex_lock(&stream->chan->lock);
3416 pthread_mutex_lock(&stream->lock);
3417 if (stream->metadata_flag) {
3418 pthread_mutex_lock(&stream->metadata_rdv_lock);
3419 }
3420
3421 switch (consumer_data.type) {
3422 case LTTNG_CONSUMER_KERNEL:
3423 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3424 break;
3425 case LTTNG_CONSUMER32_UST:
3426 case LTTNG_CONSUMER64_UST:
3427 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3428 break;
3429 default:
3430 ERR("Unknown consumer_data type");
3431 assert(0);
3432 ret = -ENOSYS;
3433 break;
3434 }
3435
3436 if (stream->metadata_flag) {
3437 pthread_cond_broadcast(&stream->metadata_rdv);
3438 pthread_mutex_unlock(&stream->metadata_rdv_lock);
3439 }
3440 pthread_mutex_unlock(&stream->lock);
3441 pthread_mutex_unlock(&stream->chan->lock);
3442
3443 return ret;
3444 }
3445
3446 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3447 {
3448 switch (consumer_data.type) {
3449 case LTTNG_CONSUMER_KERNEL:
3450 return lttng_kconsumer_on_recv_stream(stream);
3451 case LTTNG_CONSUMER32_UST:
3452 case LTTNG_CONSUMER64_UST:
3453 return lttng_ustconsumer_on_recv_stream(stream);
3454 default:
3455 ERR("Unknown consumer_data type");
3456 assert(0);
3457 return -ENOSYS;
3458 }
3459 }
3460
3461 /*
3462 * Allocate and set consumer data hash tables.
3463 */
3464 int lttng_consumer_init(void)
3465 {
3466 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3467 if (!consumer_data.channel_ht) {
3468 goto error;
3469 }
3470
3471 consumer_data.channels_by_session_id_ht =
3472 lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3473 if (!consumer_data.channels_by_session_id_ht) {
3474 goto error;
3475 }
3476
3477 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3478 if (!consumer_data.relayd_ht) {
3479 goto error;
3480 }
3481
3482 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3483 if (!consumer_data.stream_list_ht) {
3484 goto error;
3485 }
3486
3487 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3488 if (!consumer_data.stream_per_chan_id_ht) {
3489 goto error;
3490 }
3491
3492 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3493 if (!data_ht) {
3494 goto error;
3495 }
3496
3497 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3498 if (!metadata_ht) {
3499 goto error;
3500 }
3501
3502 consumer_data.chunk_registry = lttng_trace_chunk_registry_create();
3503 if (!consumer_data.chunk_registry) {
3504 goto error;
3505 }
3506
3507 return 0;
3508
3509 error:
3510 return -1;
3511 }
3512
3513 /*
3514 * Process the ADD_RELAYD command receive by a consumer.
3515 *
3516 * This will create a relayd socket pair and add it to the relayd hash table.
3517 * The caller MUST acquire a RCU read side lock before calling it.
3518 */
3519 void consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type,
3520 struct lttng_consumer_local_data *ctx, int sock,
3521 struct pollfd *consumer_sockpoll,
3522 struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id,
3523 uint64_t relayd_session_id)
3524 {
3525 int fd = -1, ret = -1, relayd_created = 0;
3526 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3527 struct consumer_relayd_sock_pair *relayd = NULL;
3528
3529 assert(ctx);
3530 assert(relayd_sock);
3531
3532 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx);
3533
3534 /* Get relayd reference if exists. */
3535 relayd = consumer_find_relayd(net_seq_idx);
3536 if (relayd == NULL) {
3537 assert(sock_type == LTTNG_STREAM_CONTROL);
3538 /* Not found. Allocate one. */
3539 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3540 if (relayd == NULL) {
3541 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3542 goto error;
3543 } else {
3544 relayd->sessiond_session_id = sessiond_id;
3545 relayd_created = 1;
3546 }
3547
3548 /*
3549 * This code path MUST continue to the consumer send status message to
3550 * we can notify the session daemon and continue our work without
3551 * killing everything.
3552 */
3553 } else {
3554 /*
3555 * relayd key should never be found for control socket.
3556 */
3557 assert(sock_type != LTTNG_STREAM_CONTROL);
3558 }
3559
3560 /* First send a status message before receiving the fds. */
3561 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
3562 if (ret < 0) {
3563 /* Somehow, the session daemon is not responding anymore. */
3564 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3565 goto error_nosignal;
3566 }
3567
3568 /* Poll on consumer socket. */
3569 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3570 if (ret) {
3571 /* Needing to exit in the middle of a command: error. */
3572 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3573 goto error_nosignal;
3574 }
3575
3576 /* Get relayd socket from session daemon */
3577 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3578 if (ret != sizeof(fd)) {
3579 fd = -1; /* Just in case it gets set with an invalid value. */
3580
3581 /*
3582 * Failing to receive FDs might indicate a major problem such as
3583 * reaching a fd limit during the receive where the kernel returns a
3584 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3585 * don't take any chances and stop everything.
3586 *
3587 * XXX: Feature request #558 will fix that and avoid this possible
3588 * issue when reaching the fd limit.
3589 */
3590 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3591 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
3592 goto error;
3593 }
3594
3595 /* Copy socket information and received FD */
3596 switch (sock_type) {
3597 case LTTNG_STREAM_CONTROL:
3598 /* Copy received lttcomm socket */
3599 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3600 ret = lttcomm_create_sock(&relayd->control_sock.sock);
3601 /* Handle create_sock error. */
3602 if (ret < 0) {
3603 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3604 goto error;
3605 }
3606 /*
3607 * Close the socket created internally by
3608 * lttcomm_create_sock, so we can replace it by the one
3609 * received from sessiond.
3610 */
3611 if (close(relayd->control_sock.sock.fd)) {
3612 PERROR("close");
3613 }
3614
3615 /* Assign new file descriptor */
3616 relayd->control_sock.sock.fd = fd;
3617 /* Assign version values. */
3618 relayd->control_sock.major = relayd_sock->major;
3619 relayd->control_sock.minor = relayd_sock->minor;
3620
3621 relayd->relayd_session_id = relayd_session_id;
3622
3623 break;
3624 case LTTNG_STREAM_DATA:
3625 /* Copy received lttcomm socket */
3626 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3627 ret = lttcomm_create_sock(&relayd->data_sock.sock);
3628 /* Handle create_sock error. */
3629 if (ret < 0) {
3630 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3631 goto error;
3632 }
3633 /*
3634 * Close the socket created internally by
3635 * lttcomm_create_sock, so we can replace it by the one
3636 * received from sessiond.
3637 */
3638 if (close(relayd->data_sock.sock.fd)) {
3639 PERROR("close");
3640 }
3641
3642 /* Assign new file descriptor */
3643 relayd->data_sock.sock.fd = fd;
3644 /* Assign version values. */
3645 relayd->data_sock.major = relayd_sock->major;
3646 relayd->data_sock.minor = relayd_sock->minor;
3647 break;
3648 default:
3649 ERR("Unknown relayd socket type (%d)", sock_type);
3650 ret_code = LTTCOMM_CONSUMERD_FATAL;
3651 goto error;
3652 }
3653
3654 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
3655 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3656 relayd->net_seq_idx, fd);
3657 /*
3658 * We gave the ownership of the fd to the relayd structure. Set the
3659 * fd to -1 so we don't call close() on it in the error path below.
3660 */
3661 fd = -1;
3662
3663 /* We successfully added the socket. Send status back. */
3664 ret = consumer_send_status_msg(sock, ret_code);
3665 if (ret < 0) {
3666 /* Somehow, the session daemon is not responding anymore. */
3667 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3668 goto error_nosignal;
3669 }
3670
3671 /*
3672 * Add relayd socket pair to consumer data hashtable. If object already
3673 * exists or on error, the function gracefully returns.
3674 */
3675 relayd->ctx = ctx;
3676 add_relayd(relayd);
3677
3678 /* All good! */
3679 return;
3680
3681 error:
3682 if (consumer_send_status_msg(sock, ret_code) < 0) {
3683 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3684 }
3685
3686 error_nosignal:
3687 /* Close received socket if valid. */
3688 if (fd >= 0) {
3689 if (close(fd)) {
3690 PERROR("close received socket");
3691 }
3692 }
3693
3694 if (relayd_created) {
3695 free(relayd);
3696 }
3697 }
3698
3699 /*
3700 * Search for a relayd associated to the session id and return the reference.
3701 *
3702 * A rcu read side lock MUST be acquire before calling this function and locked
3703 * until the relayd object is no longer necessary.
3704 */
3705 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3706 {
3707 struct lttng_ht_iter iter;
3708 struct consumer_relayd_sock_pair *relayd = NULL;
3709
3710 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3711 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3712 node.node) {
3713 /*
3714 * Check by sessiond id which is unique here where the relayd session
3715 * id might not be when having multiple relayd.
3716 */
3717 if (relayd->sessiond_session_id == id) {
3718 /* Found the relayd. There can be only one per id. */
3719 goto found;
3720 }
3721 }
3722
3723 return NULL;
3724
3725 found:
3726 return relayd;
3727 }
3728
3729 /*
3730 * Check if for a given session id there is still data needed to be extract
3731 * from the buffers.
3732 *
3733 * Return 1 if data is pending or else 0 meaning ready to be read.
3734 */
3735 int consumer_data_pending(uint64_t id)
3736 {
3737 int ret;
3738 struct lttng_ht_iter iter;
3739 struct lttng_ht *ht;
3740 struct lttng_consumer_stream *stream;
3741 struct consumer_relayd_sock_pair *relayd = NULL;
3742 int (*data_pending)(struct lttng_consumer_stream *);
3743
3744 DBG("Consumer data pending command on session id %" PRIu64, id);
3745
3746 rcu_read_lock();
3747 pthread_mutex_lock(&consumer_data.lock);
3748
3749 switch (consumer_data.type) {
3750 case LTTNG_CONSUMER_KERNEL:
3751 data_pending = lttng_kconsumer_data_pending;
3752 break;
3753 case LTTNG_CONSUMER32_UST:
3754 case LTTNG_CONSUMER64_UST:
3755 data_pending = lttng_ustconsumer_data_pending;
3756 break;
3757 default:
3758 ERR("Unknown consumer data type");
3759 assert(0);
3760 }
3761
3762 /* Ease our life a bit */
3763 ht = consumer_data.stream_list_ht;
3764
3765 cds_lfht_for_each_entry_duplicate(ht->ht,
3766 ht->hash_fct(&id, lttng_ht_seed),
3767 ht->match_fct, &id,
3768 &iter.iter, stream, node_session_id.node) {
3769 pthread_mutex_lock(&stream->lock);
3770
3771 /*
3772 * A removed node from the hash table indicates that the stream has
3773 * been deleted thus having a guarantee that the buffers are closed
3774 * on the consumer side. However, data can still be transmitted
3775 * over the network so don't skip the relayd check.
3776 */
3777 ret = cds_lfht_is_node_deleted(&stream->node.node);
3778 if (!ret) {
3779 /* Check the stream if there is data in the buffers. */
3780 ret = data_pending(stream);
3781 if (ret == 1) {
3782 pthread_mutex_unlock(&stream->lock);
3783 goto data_pending;
3784 }
3785 }
3786
3787 pthread_mutex_unlock(&stream->lock);
3788 }
3789
3790 relayd = find_relayd_by_session_id(id);
3791 if (relayd) {
3792 unsigned int is_data_inflight = 0;
3793
3794 /* Send init command for data pending. */
3795 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3796 ret = relayd_begin_data_pending(&relayd->control_sock,
3797 relayd->relayd_session_id);
3798 if (ret < 0) {
3799 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3800 /* Communication error thus the relayd so no data pending. */
3801 goto data_not_pending;
3802 }
3803
3804 cds_lfht_for_each_entry_duplicate(ht->ht,
3805 ht->hash_fct(&id, lttng_ht_seed),
3806 ht->match_fct, &id,
3807 &iter.iter, stream, node_session_id.node) {
3808 if (stream->metadata_flag) {
3809 ret = relayd_quiescent_control(&relayd->control_sock,
3810 stream->relayd_stream_id);
3811 } else {
3812 ret = relayd_data_pending(&relayd->control_sock,
3813 stream->relayd_stream_id,
3814 stream->next_net_seq_num - 1);
3815 }
3816
3817 if (ret == 1) {
3818 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3819 goto data_pending;
3820 } else if (ret < 0) {
3821 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
3822 lttng_consumer_cleanup_relayd(relayd);
3823 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3824 goto data_not_pending;
3825 }
3826 }
3827
3828 /* Send end command for data pending. */
3829 ret = relayd_end_data_pending(&relayd->control_sock,
3830 relayd->relayd_session_id, &is_data_inflight);
3831 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3832 if (ret < 0) {
3833 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
3834 lttng_consumer_cleanup_relayd(relayd);
3835 goto data_not_pending;
3836 }
3837 if (is_data_inflight) {
3838 goto data_pending;
3839 }
3840 }
3841
3842 /*
3843 * Finding _no_ node in the hash table and no inflight data means that the
3844 * stream(s) have been removed thus data is guaranteed to be available for
3845 * analysis from the trace files.
3846 */
3847
3848 data_not_pending:
3849 /* Data is available to be read by a viewer. */
3850 pthread_mutex_unlock(&consumer_data.lock);
3851 rcu_read_unlock();
3852 return 0;
3853
3854 data_pending:
3855 /* Data is still being extracted from buffers. */
3856 pthread_mutex_unlock(&consumer_data.lock);
3857 rcu_read_unlock();
3858 return 1;
3859 }
3860
3861 /*
3862 * Send a ret code status message to the sessiond daemon.
3863 *
3864 * Return the sendmsg() return value.
3865 */
3866 int consumer_send_status_msg(int sock, int ret_code)
3867 {
3868 struct lttcomm_consumer_status_msg msg;
3869
3870 memset(&msg, 0, sizeof(msg));
3871 msg.ret_code = ret_code;
3872
3873 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3874 }
3875
3876 /*
3877 * Send a channel status message to the sessiond daemon.
3878 *
3879 * Return the sendmsg() return value.
3880 */
3881 int consumer_send_status_channel(int sock,
3882 struct lttng_consumer_channel *channel)
3883 {
3884 struct lttcomm_consumer_status_channel msg;
3885
3886 assert(sock >= 0);
3887
3888 memset(&msg, 0, sizeof(msg));
3889 if (!channel) {
3890 msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
3891 } else {
3892 msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3893 msg.key = channel->key;
3894 msg.stream_count = channel->streams.count;
3895 }
3896
3897 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3898 }
3899
3900 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos,
3901 unsigned long produced_pos, uint64_t nb_packets_per_stream,
3902 uint64_t max_sb_size)
3903 {
3904 unsigned long start_pos;
3905
3906 if (!nb_packets_per_stream) {
3907 return consumed_pos; /* Grab everything */
3908 }
3909 start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size);
3910 start_pos -= max_sb_size * nb_packets_per_stream;
3911 if ((long) (start_pos - consumed_pos) < 0) {
3912 return consumed_pos; /* Grab everything */
3913 }
3914 return start_pos;
3915 }
3916
3917 static
3918 int consumer_flush_buffer(struct lttng_consumer_stream *stream, int producer_active)
3919 {
3920 int ret = 0;
3921
3922 switch (consumer_data.type) {
3923 case LTTNG_CONSUMER_KERNEL:
3924 if (producer_active) {
3925 ret = kernctl_buffer_flush(stream->wait_fd);
3926 if (ret < 0) {
3927 ERR("Failed to flush kernel stream");
3928 goto end;
3929 }
3930 } else {
3931 ret = kernctl_buffer_flush_empty(stream->wait_fd);
3932 if (ret < 0) {
3933 /*
3934 * Doing a buffer flush which does not take into
3935 * account empty packets. This is not perfect,
3936 * but required as a fall-back when
3937 * "flush_empty" is not implemented by
3938 * lttng-modules.
3939 */
3940 ret = kernctl_buffer_flush(stream->wait_fd);
3941 if (ret < 0) {
3942 ERR("Failed to flush kernel stream");
3943 goto end;
3944 }
3945 }
3946 }
3947 break;
3948 case LTTNG_CONSUMER32_UST:
3949 case LTTNG_CONSUMER64_UST:
3950 lttng_ustconsumer_flush_buffer(stream, producer_active);
3951 break;
3952 default:
3953 ERR("Unknown consumer_data type");
3954 abort();
3955 }
3956
3957 end:
3958 return ret;
3959 }
3960
3961 /*
3962 * Sample the rotate position for all the streams of a channel. If a stream
3963 * is already at the rotate position (produced == consumed), we flag it as
3964 * ready for rotation. The rotation of ready streams occurs after we have
3965 * replied to the session daemon that we have finished sampling the positions.
3966 * Must be called with RCU read-side lock held to ensure existence of channel.
3967 *
3968 * Returns 0 on success, < 0 on error
3969 */
3970 int lttng_consumer_rotate_channel(struct lttng_consumer_channel *channel,
3971 uint64_t key, uint64_t relayd_id, uint32_t metadata,
3972 struct lttng_consumer_local_data *ctx)
3973 {
3974 int ret;
3975 struct lttng_consumer_stream *stream;
3976 struct lttng_ht_iter iter;
3977 struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht;
3978 struct lttng_dynamic_array stream_rotation_positions;
3979 uint64_t next_chunk_id, stream_count = 0;
3980 enum lttng_trace_chunk_status chunk_status;
3981 const bool is_local_trace = relayd_id == -1ULL;
3982 struct consumer_relayd_sock_pair *relayd = NULL;
3983 bool rotating_to_new_chunk = true;
3984
3985 DBG("Consumer sample rotate position for channel %" PRIu64, key);
3986
3987 lttng_dynamic_array_init(&stream_rotation_positions,
3988 sizeof(struct relayd_stream_rotation_position), NULL);
3989
3990 rcu_read_lock();
3991
3992 pthread_mutex_lock(&channel->lock);
3993 assert(channel->trace_chunk);
3994 chunk_status = lttng_trace_chunk_get_id(channel->trace_chunk,
3995 &next_chunk_id);
3996 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
3997 ret = -1;
3998 goto end_unlock_channel;
3999 }
4000
4001 cds_lfht_for_each_entry_duplicate(ht->ht,
4002 ht->hash_fct(&channel->key, lttng_ht_seed),
4003 ht->match_fct, &channel->key, &iter.iter,
4004 stream, node_channel_id.node) {
4005 unsigned long produced_pos = 0, consumed_pos = 0;
4006
4007 health_code_update();
4008
4009 /*
4010 * Lock stream because we are about to change its state.
4011 */
4012 pthread_mutex_lock(&stream->lock);
4013
4014 if (stream->trace_chunk == stream->chan->trace_chunk) {
4015 rotating_to_new_chunk = false;
4016 }
4017
4018 /*
4019 * Do not flush an empty packet when rotating from a NULL trace
4020 * chunk. The stream has no means to output data, and the prior
4021 * rotation which rotated to NULL performed that side-effect already.
4022 */
4023 if (stream->trace_chunk) {
4024 /*
4025 * For metadata stream, do an active flush, which does not
4026 * produce empty packets. For data streams, empty-flush;
4027 * ensures we have at least one packet in each stream per trace
4028 * chunk, even if no data was produced.
4029 */
4030 ret = consumer_flush_buffer(stream, stream->metadata_flag ? 1 : 0);
4031 if (ret < 0) {
4032 ERR("Failed to flush stream %" PRIu64 " during channel rotation",
4033 stream->key);
4034 goto end_unlock_stream;
4035 }
4036 }
4037
4038 ret = lttng_consumer_take_snapshot(stream);
4039 if (ret < 0 && ret != -ENODATA && ret != -EAGAIN) {
4040 ERR("Failed to sample snapshot position during channel rotation");
4041 goto end_unlock_stream;
4042 }
4043 if (!ret) {
4044 ret = lttng_consumer_get_produced_snapshot(stream,
4045 &produced_pos);
4046 if (ret < 0) {
4047 ERR("Failed to sample produced position during channel rotation");
4048 goto end_unlock_stream;
4049 }
4050
4051 ret = lttng_consumer_get_consumed_snapshot(stream,
4052 &consumed_pos);
4053 if (ret < 0) {
4054 ERR("Failed to sample consumed position during channel rotation");
4055 goto end_unlock_stream;
4056 }
4057 }
4058 /*
4059 * Align produced position on the start-of-packet boundary of the first
4060 * packet going into the next trace chunk.
4061 */
4062 produced_pos = ALIGN_FLOOR(produced_pos, stream->max_sb_size);
4063 if (consumed_pos == produced_pos) {
4064 DBG("Set rotate ready for stream %" PRIu64 " produced = %lu consumed = %lu",
4065 stream->key, produced_pos, consumed_pos);
4066 stream->rotate_ready = true;
4067 } else {
4068 DBG("Different consumed and produced positions "
4069 "for stream %" PRIu64 " produced = %lu consumed = %lu",
4070 stream->key, produced_pos, consumed_pos);
4071 }
4072 /*
4073 * The rotation position is based on the packet_seq_num of the
4074 * packet following the last packet that was consumed for this
4075 * stream, incremented by the offset between produced and
4076 * consumed positions. This rotation position is a lower bound
4077 * (inclusive) at which the next trace chunk starts. Since it
4078 * is a lower bound, it is OK if the packet_seq_num does not
4079 * correspond exactly to the same packet identified by the
4080 * consumed_pos, which can happen in overwrite mode.
4081 */
4082 if (stream->sequence_number_unavailable) {
4083 /*
4084 * Rotation should never be performed on a session which
4085 * interacts with a pre-2.8 lttng-modules, which does
4086 * not implement packet sequence number.
4087 */
4088 ERR("Failure to rotate stream %" PRIu64 ": sequence number unavailable",
4089 stream->key);
4090 ret = -1;
4091 goto end_unlock_stream;
4092 }
4093 stream->rotate_position = stream->last_sequence_number + 1 +
4094 ((produced_pos - consumed_pos) / stream->max_sb_size);
4095 DBG("Set rotation position for stream %" PRIu64 " at position %" PRIu64,
4096 stream->key, stream->rotate_position);
4097
4098 if (!is_local_trace) {
4099 /*
4100 * The relay daemon control protocol expects a rotation
4101 * position as "the sequence number of the first packet
4102 * _after_ the current trace chunk".
4103 */
4104 const struct relayd_stream_rotation_position position = {
4105 .stream_id = stream->relayd_stream_id,
4106 .rotate_at_seq_num = stream->rotate_position,
4107 };
4108
4109 ret = lttng_dynamic_array_add_element(
4110 &stream_rotation_positions,
4111 &position);
4112 if (ret) {
4113 ERR("Failed to allocate stream rotation position");
4114 goto end_unlock_stream;
4115 }
4116 stream_count++;
4117 }
4118 pthread_mutex_unlock(&stream->lock);
4119 }
4120 stream = NULL;
4121 pthread_mutex_unlock(&channel->lock);
4122
4123 if (is_local_trace) {
4124 ret = 0;
4125 goto end;
4126 }
4127
4128 relayd = consumer_find_relayd(relayd_id);
4129 if (!relayd) {
4130 ERR("Failed to find relayd %" PRIu64, relayd_id);
4131 ret = -1;
4132 goto end;
4133 }
4134
4135 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4136 ret = relayd_rotate_streams(&relayd->control_sock, stream_count,
4137 rotating_to_new_chunk ? &next_chunk_id : NULL,
4138 (const struct relayd_stream_rotation_position *)
4139 stream_rotation_positions.buffer.data);
4140 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4141 if (ret < 0) {
4142 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64,
4143 relayd->net_seq_idx);
4144 lttng_consumer_cleanup_relayd(relayd);
4145 goto end;
4146 }
4147
4148 ret = 0;
4149 goto end;
4150
4151 end_unlock_stream:
4152 pthread_mutex_unlock(&stream->lock);
4153 end_unlock_channel:
4154 pthread_mutex_unlock(&channel->lock);
4155 end:
4156 rcu_read_unlock();
4157 lttng_dynamic_array_reset(&stream_rotation_positions);
4158 return ret;
4159 }
4160
4161 static
4162 int consumer_clear_buffer(struct lttng_consumer_stream *stream)
4163 {
4164 int ret = 0;
4165 unsigned long consumed_pos_before, consumed_pos_after;
4166
4167 ret = lttng_consumer_sample_snapshot_positions(stream);
4168 if (ret < 0) {
4169 ERR("Taking snapshot positions");
4170 goto end;
4171 }
4172
4173 ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_before);
4174 if (ret < 0) {
4175 ERR("Consumed snapshot position");
4176 goto end;
4177 }
4178
4179 switch (consumer_data.type) {
4180 case LTTNG_CONSUMER_KERNEL:
4181 ret = kernctl_buffer_clear(stream->wait_fd);
4182 if (ret < 0) {
4183 ERR("Failed to clear kernel stream (ret = %d)", ret);
4184 goto end;
4185 }
4186 break;
4187 case LTTNG_CONSUMER32_UST:
4188 case LTTNG_CONSUMER64_UST:
4189 lttng_ustconsumer_clear_buffer(stream);
4190 break;
4191 default:
4192 ERR("Unknown consumer_data type");
4193 abort();
4194 }
4195
4196 ret = lttng_consumer_sample_snapshot_positions(stream);
4197 if (ret < 0) {
4198 ERR("Taking snapshot positions");
4199 goto end;
4200 }
4201 ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_after);
4202 if (ret < 0) {
4203 ERR("Consumed snapshot position");
4204 goto end;
4205 }
4206 DBG("clear: before: %lu after: %lu", consumed_pos_before, consumed_pos_after);
4207 end:
4208 return ret;
4209 }
4210
4211 static
4212 int consumer_clear_stream(struct lttng_consumer_stream *stream)
4213 {
4214 int ret;
4215
4216 ret = consumer_flush_buffer(stream, 1);
4217 if (ret < 0) {
4218 ERR("Failed to flush stream %" PRIu64 " during channel clear",
4219 stream->key);
4220 ret = LTTCOMM_CONSUMERD_FATAL;
4221 goto error;
4222 }
4223
4224 ret = consumer_clear_buffer(stream);
4225 if (ret < 0) {
4226 ERR("Failed to clear stream %" PRIu64 " during channel clear",
4227 stream->key);
4228 ret = LTTCOMM_CONSUMERD_FATAL;
4229 goto error;
4230 }
4231
4232 ret = LTTCOMM_CONSUMERD_SUCCESS;
4233 error:
4234 return ret;
4235 }
4236
4237 static
4238 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel *channel)
4239 {
4240 int ret;
4241 struct lttng_consumer_stream *stream;
4242
4243 rcu_read_lock();
4244 pthread_mutex_lock(&channel->lock);
4245 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
4246 health_code_update();
4247 pthread_mutex_lock(&stream->lock);
4248 ret = consumer_clear_stream(stream);
4249 if (ret) {
4250 goto error_unlock;
4251 }
4252 pthread_mutex_unlock(&stream->lock);
4253 }
4254 pthread_mutex_unlock(&channel->lock);
4255 rcu_read_unlock();
4256 return 0;
4257
4258 error_unlock:
4259 pthread_mutex_unlock(&stream->lock);
4260 pthread_mutex_unlock(&channel->lock);
4261 rcu_read_unlock();
4262 return ret;
4263 }
4264
4265 /*
4266 * Check if a stream is ready to be rotated after extracting it.
4267 *
4268 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4269 * error. Stream lock must be held.
4270 */
4271 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream *stream)
4272 {
4273 DBG("Check is rotate ready for stream %" PRIu64
4274 " ready %u rotate_position %" PRIu64
4275 " last_sequence_number %" PRIu64,
4276 stream->key, stream->rotate_ready,
4277 stream->rotate_position, stream->last_sequence_number);
4278 if (stream->rotate_ready) {
4279 return 1;
4280 }
4281
4282 /*
4283 * If packet seq num is unavailable, it means we are interacting
4284 * with a pre-2.8 lttng-modules which does not implement the
4285 * sequence number. Rotation should never be used by sessiond in this
4286 * scenario.
4287 */
4288 if (stream->sequence_number_unavailable) {
4289 ERR("Internal error: rotation used on stream %" PRIu64
4290 " with unavailable sequence number",
4291 stream->key);
4292 return -1;
4293 }
4294
4295 if (stream->rotate_position == -1ULL ||
4296 stream->last_sequence_number == -1ULL) {
4297 return 0;
4298 }
4299
4300 /*
4301 * Rotate position not reached yet. The stream rotate position is
4302 * the position of the next packet belonging to the next trace chunk,
4303 * but consumerd considers rotation ready when reaching the last
4304 * packet of the current chunk, hence the "rotate_position - 1".
4305 */
4306
4307 DBG("Check is rotate ready for stream %" PRIu64
4308 " last_sequence_number %" PRIu64
4309 " rotate_position %" PRIu64,
4310 stream->key, stream->last_sequence_number,
4311 stream->rotate_position);
4312 if (stream->last_sequence_number >= stream->rotate_position - 1) {
4313 return 1;
4314 }
4315
4316 return 0;
4317 }
4318
4319 /*
4320 * Reset the state for a stream after a rotation occurred.
4321 */
4322 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream *stream)
4323 {
4324 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64,
4325 stream->key);
4326 stream->rotate_position = -1ULL;
4327 stream->rotate_ready = false;
4328 }
4329
4330 /*
4331 * Perform the rotation a local stream file.
4332 */
4333 static
4334 int rotate_local_stream(struct lttng_consumer_local_data *ctx,
4335 struct lttng_consumer_stream *stream)
4336 {
4337 int ret = 0;
4338
4339 DBG("Rotate local stream: stream key %" PRIu64 ", channel key %" PRIu64,
4340 stream->key,
4341 stream->chan->key);
4342 stream->tracefile_size_current = 0;
4343 stream->tracefile_count_current = 0;
4344
4345 if (stream->out_fd >= 0) {
4346 ret = close(stream->out_fd);
4347 if (ret) {
4348 PERROR("Failed to close stream out_fd of channel \"%s\"",
4349 stream->chan->name);
4350 }
4351 stream->out_fd = -1;
4352 }
4353
4354 if (stream->index_file) {
4355 lttng_index_file_put(stream->index_file);
4356 stream->index_file = NULL;
4357 }
4358
4359 if (!stream->trace_chunk) {
4360 goto end;
4361 }
4362
4363 ret = consumer_stream_create_output_files(stream, true);
4364 end:
4365 return ret;
4366 }
4367
4368 /*
4369 * Performs the stream rotation for the rotate session feature if needed.
4370 * It must be called with the channel and stream locks held.
4371 *
4372 * Return 0 on success, a negative number of error.
4373 */
4374 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data *ctx,
4375 struct lttng_consumer_stream *stream)
4376 {
4377 int ret;
4378
4379 DBG("Consumer rotate stream %" PRIu64, stream->key);
4380
4381 /*
4382 * Update the stream's 'current' chunk to the session's (channel)
4383 * now-current chunk.
4384 */
4385 lttng_trace_chunk_put(stream->trace_chunk);
4386 if (stream->chan->trace_chunk == stream->trace_chunk) {
4387 /*
4388 * A channel can be rotated and not have a "next" chunk
4389 * to transition to. In that case, the channel's "current chunk"
4390 * has not been closed yet, but it has not been updated to
4391 * a "next" trace chunk either. Hence, the stream, like its
4392 * parent channel, becomes part of no chunk and can't output
4393 * anything until a new trace chunk is created.
4394 */
4395 stream->trace_chunk = NULL;
4396 } else if (stream->chan->trace_chunk &&
4397 !lttng_trace_chunk_get(stream->chan->trace_chunk)) {
4398 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4399 ret = -1;
4400 goto error;
4401 } else {
4402 /*
4403 * Update the stream's trace chunk to its parent channel's
4404 * current trace chunk.
4405 */
4406 stream->trace_chunk = stream->chan->trace_chunk;
4407 }
4408
4409 if (stream->net_seq_idx == (uint64_t) -1ULL) {
4410 ret = rotate_local_stream(ctx, stream);
4411 if (ret < 0) {
4412 ERR("Failed to rotate stream, ret = %i", ret);
4413 goto error;
4414 }
4415 }
4416
4417 if (stream->metadata_flag && stream->trace_chunk) {
4418 /*
4419 * If the stream has transitioned to a new trace
4420 * chunk, the metadata should be re-dumped to the
4421 * newest chunk.
4422 *
4423 * However, it is possible for a stream to transition to
4424 * a "no-chunk" state. This can happen if a rotation
4425 * occurs on an inactive session. In such cases, the metadata
4426 * regeneration will happen when the next trace chunk is
4427 * created.
4428 */
4429 ret = consumer_metadata_stream_dump(stream);
4430 if (ret) {
4431 goto error;
4432 }
4433 }
4434 lttng_consumer_reset_stream_rotate_state(stream);
4435
4436 ret = 0;
4437
4438 error:
4439 return ret;
4440 }
4441
4442 /*
4443 * Rotate all the ready streams now.
4444 *
4445 * This is especially important for low throughput streams that have already
4446 * been consumed, we cannot wait for their next packet to perform the
4447 * rotation.
4448 * Need to be called with RCU read-side lock held to ensure existence of
4449 * channel.
4450 *
4451 * Returns 0 on success, < 0 on error
4452 */
4453 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel *channel,
4454 uint64_t key, struct lttng_consumer_local_data *ctx)
4455 {
4456 int ret;
4457 struct lttng_consumer_stream *stream;
4458 struct lttng_ht_iter iter;
4459 struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht;
4460
4461 rcu_read_lock();
4462
4463 DBG("Consumer rotate ready streams in channel %" PRIu64, key);
4464
4465 cds_lfht_for_each_entry_duplicate(ht->ht,
4466 ht->hash_fct(&channel->key, lttng_ht_seed),
4467 ht->match_fct, &channel->key, &iter.iter,
4468 stream, node_channel_id.node) {
4469 health_code_update();
4470
4471 pthread_mutex_lock(&stream->chan->lock);
4472 pthread_mutex_lock(&stream->lock);
4473
4474 if (!stream->rotate_ready) {
4475 pthread_mutex_unlock(&stream->lock);
4476 pthread_mutex_unlock(&stream->chan->lock);
4477 continue;
4478 }
4479 DBG("Consumer rotate ready stream %" PRIu64, stream->key);
4480
4481 ret = lttng_consumer_rotate_stream(ctx, stream);
4482 pthread_mutex_unlock(&stream->lock);
4483 pthread_mutex_unlock(&stream->chan->lock);
4484 if (ret) {
4485 goto end;
4486 }
4487 }
4488
4489 ret = 0;
4490
4491 end:
4492 rcu_read_unlock();
4493 return ret;
4494 }
4495
4496 enum lttcomm_return_code lttng_consumer_init_command(
4497 struct lttng_consumer_local_data *ctx,
4498 const lttng_uuid sessiond_uuid)
4499 {
4500 enum lttcomm_return_code ret;
4501 char uuid_str[LTTNG_UUID_STR_LEN];
4502
4503 if (ctx->sessiond_uuid.is_set) {
4504 ret = LTTCOMM_CONSUMERD_ALREADY_SET;
4505 goto end;
4506 }
4507
4508 ctx->sessiond_uuid.is_set = true;
4509 memcpy(ctx->sessiond_uuid.value, sessiond_uuid, sizeof(lttng_uuid));
4510 ret = LTTCOMM_CONSUMERD_SUCCESS;
4511 lttng_uuid_to_str(sessiond_uuid, uuid_str);
4512 DBG("Received session daemon UUID: %s", uuid_str);
4513 end:
4514 return ret;
4515 }
4516
4517 enum lttcomm_return_code lttng_consumer_create_trace_chunk(
4518 const uint64_t *relayd_id, uint64_t session_id,
4519 uint64_t chunk_id,
4520 time_t chunk_creation_timestamp,
4521 const char *chunk_override_name,
4522 const struct lttng_credentials *credentials,
4523 struct lttng_directory_handle *chunk_directory_handle)
4524 {
4525 int ret;
4526 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
4527 struct lttng_trace_chunk *created_chunk = NULL, *published_chunk = NULL;
4528 enum lttng_trace_chunk_status chunk_status;
4529 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
4530 char creation_timestamp_buffer[ISO8601_STR_LEN];
4531 const char *relayd_id_str = "(none)";
4532 const char *creation_timestamp_str;
4533 struct lttng_ht_iter iter;
4534 struct lttng_consumer_channel *channel;
4535
4536 if (relayd_id) {
4537 /* Only used for logging purposes. */
4538 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
4539 "%" PRIu64, *relayd_id);
4540 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
4541 relayd_id_str = relayd_id_buffer;
4542 } else {
4543 relayd_id_str = "(formatting error)";
4544 }
4545 }
4546
4547 /* Local protocol error. */
4548 assert(chunk_creation_timestamp);
4549 ret = time_to_iso8601_str(chunk_creation_timestamp,
4550 creation_timestamp_buffer,
4551 sizeof(creation_timestamp_buffer));
4552 creation_timestamp_str = !ret ? creation_timestamp_buffer :
4553 "(formatting error)";
4554
4555 DBG("Consumer create trace chunk command: relay_id = %s"
4556 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
4557 ", chunk_override_name = %s"
4558 ", chunk_creation_timestamp = %s",
4559 relayd_id_str, session_id, chunk_id,
4560 chunk_override_name ? : "(none)",
4561 creation_timestamp_str);
4562
4563 /*
4564 * The trace chunk registry, as used by the consumer daemon, implicitly
4565 * owns the trace chunks. This is only needed in the consumer since
4566 * the consumer has no notion of a session beyond session IDs being
4567 * used to identify other objects.
4568 *
4569 * The lttng_trace_chunk_registry_publish() call below provides a
4570 * reference which is not released; it implicitly becomes the session
4571 * daemon's reference to the chunk in the consumer daemon.
4572 *
4573 * The lifetime of trace chunks in the consumer daemon is managed by
4574 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4575 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4576 */
4577 created_chunk = lttng_trace_chunk_create(chunk_id,
4578 chunk_creation_timestamp, NULL);
4579 if (!created_chunk) {
4580 ERR("Failed to create trace chunk");
4581 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4582 goto error;
4583 }
4584
4585 if (chunk_override_name) {
4586 chunk_status = lttng_trace_chunk_override_name(created_chunk,
4587 chunk_override_name);
4588 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4589 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4590 goto error;
4591 }
4592 }
4593
4594 if (chunk_directory_handle) {
4595 chunk_status = lttng_trace_chunk_set_credentials(created_chunk,
4596 credentials);
4597 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4598 ERR("Failed to set trace chunk credentials");
4599 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4600 goto error;
4601 }
4602 /*
4603 * The consumer daemon has no ownership of the chunk output
4604 * directory.
4605 */
4606 chunk_status = lttng_trace_chunk_set_as_user(created_chunk,
4607 chunk_directory_handle);
4608 chunk_directory_handle = NULL;
4609 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4610 ERR("Failed to set trace chunk's directory handle");
4611 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4612 goto error;
4613 }
4614 }
4615
4616 published_chunk = lttng_trace_chunk_registry_publish_chunk(
4617 consumer_data.chunk_registry, session_id,
4618 created_chunk);
4619 lttng_trace_chunk_put(created_chunk);
4620 created_chunk = NULL;
4621 if (!published_chunk) {
4622 ERR("Failed to publish trace chunk");
4623 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4624 goto error;
4625 }
4626
4627 rcu_read_lock();
4628 cds_lfht_for_each_entry_duplicate(consumer_data.channels_by_session_id_ht->ht,
4629 consumer_data.channels_by_session_id_ht->hash_fct(
4630 &session_id, lttng_ht_seed),
4631 consumer_data.channels_by_session_id_ht->match_fct,
4632 &session_id, &iter.iter, channel,
4633 channels_by_session_id_ht_node.node) {
4634 ret = lttng_consumer_channel_set_trace_chunk(channel,
4635 published_chunk);
4636 if (ret) {
4637 /*
4638 * Roll-back the creation of this chunk.
4639 *
4640 * This is important since the session daemon will
4641 * assume that the creation of this chunk failed and
4642 * will never ask for it to be closed, resulting
4643 * in a leak and an inconsistent state for some
4644 * channels.
4645 */
4646 enum lttcomm_return_code close_ret;
4647 char path[LTTNG_PATH_MAX];
4648
4649 DBG("Failed to set new trace chunk on existing channels, rolling back");
4650 close_ret = lttng_consumer_close_trace_chunk(relayd_id,
4651 session_id, chunk_id,
4652 chunk_creation_timestamp, NULL,
4653 path);
4654 if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) {
4655 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64,
4656 session_id, chunk_id);
4657 }
4658
4659 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4660 break;
4661 }
4662 }
4663
4664 if (relayd_id) {
4665 struct consumer_relayd_sock_pair *relayd;
4666
4667 relayd = consumer_find_relayd(*relayd_id);
4668 if (relayd) {
4669 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4670 ret = relayd_create_trace_chunk(
4671 &relayd->control_sock, published_chunk);
4672 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4673 } else {
4674 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, *relayd_id);
4675 }
4676
4677 if (!relayd || ret) {
4678 enum lttcomm_return_code close_ret;
4679 char path[LTTNG_PATH_MAX];
4680
4681 close_ret = lttng_consumer_close_trace_chunk(relayd_id,
4682 session_id,
4683 chunk_id,
4684 chunk_creation_timestamp,
4685 NULL, path);
4686 if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) {
4687 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64,
4688 session_id,
4689 chunk_id);
4690 }
4691
4692 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4693 goto error_unlock;
4694 }
4695 }
4696 error_unlock:
4697 rcu_read_unlock();
4698 error:
4699 /* Release the reference returned by the "publish" operation. */
4700 lttng_trace_chunk_put(published_chunk);
4701 lttng_trace_chunk_put(created_chunk);
4702 return ret_code;
4703 }
4704
4705 enum lttcomm_return_code lttng_consumer_close_trace_chunk(
4706 const uint64_t *relayd_id, uint64_t session_id,
4707 uint64_t chunk_id, time_t chunk_close_timestamp,
4708 const enum lttng_trace_chunk_command_type *close_command,
4709 char *path)
4710 {
4711 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
4712 struct lttng_trace_chunk *chunk;
4713 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
4714 const char *relayd_id_str = "(none)";
4715 const char *close_command_name = "none";
4716 struct lttng_ht_iter iter;
4717 struct lttng_consumer_channel *channel;
4718 enum lttng_trace_chunk_status chunk_status;
4719
4720 if (relayd_id) {
4721 int ret;
4722
4723 /* Only used for logging purposes. */
4724 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
4725 "%" PRIu64, *relayd_id);
4726 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
4727 relayd_id_str = relayd_id_buffer;
4728 } else {
4729 relayd_id_str = "(formatting error)";
4730 }
4731 }
4732 if (close_command) {
4733 close_command_name = lttng_trace_chunk_command_type_get_name(
4734 *close_command);
4735 }
4736
4737 DBG("Consumer close trace chunk command: relayd_id = %s"
4738 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
4739 ", close command = %s",
4740 relayd_id_str, session_id, chunk_id,
4741 close_command_name);
4742
4743 chunk = lttng_trace_chunk_registry_find_chunk(
4744 consumer_data.chunk_registry, session_id, chunk_id);
4745 if (!chunk) {
4746 ERR("Failed to find chunk: session_id = %" PRIu64
4747 ", chunk_id = %" PRIu64,
4748 session_id, chunk_id);
4749 ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
4750 goto end;
4751 }
4752
4753 chunk_status = lttng_trace_chunk_set_close_timestamp(chunk,
4754 chunk_close_timestamp);
4755 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4756 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4757 goto end;
4758 }
4759
4760 if (close_command) {
4761 chunk_status = lttng_trace_chunk_set_close_command(
4762 chunk, *close_command);
4763 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4764 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4765 goto end;
4766 }
4767 }
4768
4769 /*
4770 * chunk is now invalid to access as we no longer hold a reference to
4771 * it; it is only kept around to compare it (by address) to the
4772 * current chunk found in the session's channels.
4773 */
4774 rcu_read_lock();
4775 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter,
4776 channel, node.node) {
4777 int ret;
4778
4779 /*
4780 * Only change the channel's chunk to NULL if it still
4781 * references the chunk being closed. The channel may
4782 * reference a newer channel in the case of a session
4783 * rotation. When a session rotation occurs, the "next"
4784 * chunk is created before the "current" chunk is closed.
4785 */
4786 if (channel->trace_chunk != chunk) {
4787 continue;
4788 }
4789 ret = lttng_consumer_channel_set_trace_chunk(channel, NULL);
4790 if (ret) {
4791 /*
4792 * Attempt to close the chunk on as many channels as
4793 * possible.
4794 */
4795 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4796 }
4797 }
4798
4799 if (relayd_id) {
4800 int ret;
4801 struct consumer_relayd_sock_pair *relayd;
4802
4803 relayd = consumer_find_relayd(*relayd_id);
4804 if (relayd) {
4805 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4806 ret = relayd_close_trace_chunk(
4807 &relayd->control_sock, chunk,
4808 path);
4809 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4810 } else {
4811 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64,
4812 *relayd_id);
4813 }
4814
4815 if (!relayd || ret) {
4816 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4817 goto error_unlock;
4818 }
4819 }
4820 error_unlock:
4821 rcu_read_unlock();
4822 end:
4823 /*
4824 * Release the reference returned by the "find" operation and
4825 * the session daemon's implicit reference to the chunk.
4826 */
4827 lttng_trace_chunk_put(chunk);
4828 lttng_trace_chunk_put(chunk);
4829
4830 return ret_code;
4831 }
4832
4833 enum lttcomm_return_code lttng_consumer_trace_chunk_exists(
4834 const uint64_t *relayd_id, uint64_t session_id,
4835 uint64_t chunk_id)
4836 {
4837 int ret;
4838 enum lttcomm_return_code ret_code;
4839 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
4840 const char *relayd_id_str = "(none)";
4841 const bool is_local_trace = !relayd_id;
4842 struct consumer_relayd_sock_pair *relayd = NULL;
4843 bool chunk_exists_local, chunk_exists_remote;
4844
4845 if (relayd_id) {
4846 int ret;
4847
4848 /* Only used for logging purposes. */
4849 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
4850 "%" PRIu64, *relayd_id);
4851 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
4852 relayd_id_str = relayd_id_buffer;
4853 } else {
4854 relayd_id_str = "(formatting error)";
4855 }
4856 }
4857
4858 DBG("Consumer trace chunk exists command: relayd_id = %s"
4859 ", chunk_id = %" PRIu64, relayd_id_str,
4860 chunk_id);
4861 ret = lttng_trace_chunk_registry_chunk_exists(
4862 consumer_data.chunk_registry, session_id,
4863 chunk_id, &chunk_exists_local);
4864 if (ret) {
4865 /* Internal error. */
4866 ERR("Failed to query the existence of a trace chunk");
4867 ret_code = LTTCOMM_CONSUMERD_FATAL;
4868 goto end;
4869 }
4870 DBG("Trace chunk %s locally",
4871 chunk_exists_local ? "exists" : "does not exist");
4872 if (chunk_exists_local) {
4873 ret_code = LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL;
4874 goto end;
4875 } else if (is_local_trace) {
4876 ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
4877 goto end;
4878 }
4879
4880 rcu_read_lock();
4881 relayd = consumer_find_relayd(*relayd_id);
4882 if (!relayd) {
4883 ERR("Failed to find relayd %" PRIu64, *relayd_id);
4884 ret_code = LTTCOMM_CONSUMERD_INVALID_PARAMETERS;
4885 goto end_rcu_unlock;
4886 }
4887 DBG("Looking up existence of trace chunk on relay daemon");
4888 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4889 ret = relayd_trace_chunk_exists(&relayd->control_sock, chunk_id,
4890 &chunk_exists_remote);
4891 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4892 if (ret < 0) {
4893 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4894 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
4895 goto end_rcu_unlock;
4896 }
4897
4898 ret_code = chunk_exists_remote ?
4899 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE :
4900 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
4901 DBG("Trace chunk %s on relay daemon",
4902 chunk_exists_remote ? "exists" : "does not exist");
4903
4904 end_rcu_unlock:
4905 rcu_read_unlock();
4906 end:
4907 return ret_code;
4908 }
4909
4910 static
4911 int consumer_clear_monitored_channel(struct lttng_consumer_channel *channel)
4912 {
4913 struct lttng_ht *ht;
4914 struct lttng_consumer_stream *stream;
4915 struct lttng_ht_iter iter;
4916 int ret;
4917
4918 ht = consumer_data.stream_per_chan_id_ht;
4919
4920 rcu_read_lock();
4921 cds_lfht_for_each_entry_duplicate(ht->ht,
4922 ht->hash_fct(&channel->key, lttng_ht_seed),
4923 ht->match_fct, &channel->key,
4924 &iter.iter, stream, node_channel_id.node) {
4925 /*
4926 * Protect against teardown with mutex.
4927 */
4928 pthread_mutex_lock(&stream->lock);
4929 if (cds_lfht_is_node_deleted(&stream->node.node)) {
4930 goto next;
4931 }
4932 ret = consumer_clear_stream(stream);
4933 if (ret) {
4934 goto error_unlock;
4935 }
4936 next:
4937 pthread_mutex_unlock(&stream->lock);
4938 }
4939 rcu_read_unlock();
4940 return LTTCOMM_CONSUMERD_SUCCESS;
4941
4942 error_unlock:
4943 pthread_mutex_unlock(&stream->lock);
4944 rcu_read_unlock();
4945 return ret;
4946 }
4947
4948 int lttng_consumer_clear_channel(struct lttng_consumer_channel *channel)
4949 {
4950 int ret;
4951
4952 DBG("Consumer clear channel %" PRIu64, channel->key);
4953
4954 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
4955 /*
4956 * Nothing to do for the metadata channel/stream.
4957 * Snapshot mechanism already take care of the metadata
4958 * handling/generation, and monitored channels only need to
4959 * have their data stream cleared..
4960 */
4961 ret = LTTCOMM_CONSUMERD_SUCCESS;
4962 goto end;
4963 }
4964
4965 if (!channel->monitor) {
4966 ret = consumer_clear_unmonitored_channel(channel);
4967 } else {
4968 ret = consumer_clear_monitored_channel(channel);
4969 }
4970 end:
4971 return ret;
4972 }
This page took 0.196999 seconds and 5 git commands to generate.