Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / kernel-consumer / kernel-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) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <poll.h>
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <inttypes.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <stdint.h>
22
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <common/common.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/sessiond-comm/relayd.h>
28 #include <common/compat/fcntl.h>
29 #include <common/compat/endian.h>
30 #include <common/pipe.h>
31 #include <common/relayd/relayd.h>
32 #include <common/utils.h>
33 #include <common/consumer/consumer-stream.h>
34 #include <common/index/index.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/optional.h>
37 #include <common/buffer-view.h>
38 #include <common/consumer/consumer.h>
39 #include <common/consumer/metadata-bucket.h>
40
41 #include "kernel-consumer.h"
42
43 extern struct lttng_consumer_global_data the_consumer_data;
44 extern int consumer_poll_timeout;
45
46 /*
47 * Take a snapshot for a specific fd
48 *
49 * Returns 0 on success, < 0 on error
50 */
51 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
52 {
53 int ret = 0;
54 int infd = stream->wait_fd;
55
56 ret = kernctl_snapshot(infd);
57 /*
58 * -EAGAIN is not an error, it just means that there is no data to
59 * be read.
60 */
61 if (ret != 0 && ret != -EAGAIN) {
62 PERROR("Getting sub-buffer snapshot.");
63 }
64
65 return ret;
66 }
67
68 /*
69 * Sample consumed and produced positions for a specific fd.
70 *
71 * Returns 0 on success, < 0 on error.
72 */
73 int lttng_kconsumer_sample_snapshot_positions(
74 struct lttng_consumer_stream *stream)
75 {
76 LTTNG_ASSERT(stream);
77
78 return kernctl_snapshot_sample_positions(stream->wait_fd);
79 }
80
81 /*
82 * Get the produced position
83 *
84 * Returns 0 on success, < 0 on error
85 */
86 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
87 unsigned long *pos)
88 {
89 int ret;
90 int infd = stream->wait_fd;
91
92 ret = kernctl_snapshot_get_produced(infd, pos);
93 if (ret != 0) {
94 PERROR("kernctl_snapshot_get_produced");
95 }
96
97 return ret;
98 }
99
100 /*
101 * Get the consumerd position
102 *
103 * Returns 0 on success, < 0 on error
104 */
105 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
106 unsigned long *pos)
107 {
108 int ret;
109 int infd = stream->wait_fd;
110
111 ret = kernctl_snapshot_get_consumed(infd, pos);
112 if (ret != 0) {
113 PERROR("kernctl_snapshot_get_consumed");
114 }
115
116 return ret;
117 }
118
119 static
120 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
121 const char **addr)
122 {
123 int ret;
124 unsigned long mmap_offset;
125 const char *mmap_base = stream->mmap_base;
126
127 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
128 if (ret < 0) {
129 PERROR("Failed to get mmap read offset");
130 goto error;
131 }
132
133 *addr = mmap_base + mmap_offset;
134 error:
135 return ret;
136 }
137
138 /*
139 * Take a snapshot of all the stream of a channel
140 * RCU read-side lock must be held across this function to ensure existence of
141 * channel. The channel lock must be held by the caller.
142 *
143 * Returns 0 on success, < 0 on error
144 */
145 static int lttng_kconsumer_snapshot_channel(
146 struct lttng_consumer_channel *channel,
147 uint64_t key, char *path, uint64_t relayd_id,
148 uint64_t nb_packets_per_stream,
149 struct lttng_consumer_local_data *ctx)
150 {
151 int ret;
152 struct lttng_consumer_stream *stream;
153
154 DBG("Kernel consumer snapshot channel %" PRIu64, key);
155
156 rcu_read_lock();
157
158 /* Splice is not supported yet for channel snapshot. */
159 if (channel->output != CONSUMER_CHANNEL_MMAP) {
160 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
161 channel->name);
162 ret = -1;
163 goto end;
164 }
165
166 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
167 unsigned long consumed_pos, produced_pos;
168
169 health_code_update();
170
171 /*
172 * Lock stream because we are about to change its state.
173 */
174 pthread_mutex_lock(&stream->lock);
175
176 LTTNG_ASSERT(channel->trace_chunk);
177 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
178 /*
179 * Can't happen barring an internal error as the channel
180 * holds a reference to the trace chunk.
181 */
182 ERR("Failed to acquire reference to channel's trace chunk");
183 ret = -1;
184 goto end_unlock;
185 }
186 LTTNG_ASSERT(!stream->trace_chunk);
187 stream->trace_chunk = channel->trace_chunk;
188
189 /*
190 * Assign the received relayd ID so we can use it for streaming. The streams
191 * are not visible to anyone so this is OK to change it.
192 */
193 stream->net_seq_idx = relayd_id;
194 channel->relayd_id = relayd_id;
195 if (relayd_id != (uint64_t) -1ULL) {
196 ret = consumer_send_relayd_stream(stream, path);
197 if (ret < 0) {
198 ERR("sending stream to relayd");
199 goto end_unlock;
200 }
201 } else {
202 ret = consumer_stream_create_output_files(stream,
203 false);
204 if (ret < 0) {
205 goto end_unlock;
206 }
207 DBG("Kernel consumer snapshot stream (%" PRIu64 ")",
208 stream->key);
209 }
210
211 ret = kernctl_buffer_flush_empty(stream->wait_fd);
212 if (ret < 0) {
213 /*
214 * Doing a buffer flush which does not take into
215 * account empty packets. This is not perfect
216 * for stream intersection, but required as a
217 * fall-back when "flush_empty" is not
218 * implemented by lttng-modules.
219 */
220 ret = kernctl_buffer_flush(stream->wait_fd);
221 if (ret < 0) {
222 ERR("Failed to flush kernel stream");
223 goto end_unlock;
224 }
225 goto end_unlock;
226 }
227
228 ret = lttng_kconsumer_take_snapshot(stream);
229 if (ret < 0) {
230 ERR("Taking kernel snapshot");
231 goto end_unlock;
232 }
233
234 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
235 if (ret < 0) {
236 ERR("Produced kernel snapshot position");
237 goto end_unlock;
238 }
239
240 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
241 if (ret < 0) {
242 ERR("Consumerd kernel snapshot position");
243 goto end_unlock;
244 }
245
246 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
247 produced_pos, nb_packets_per_stream,
248 stream->max_sb_size);
249
250 while ((long) (consumed_pos - produced_pos) < 0) {
251 ssize_t read_len;
252 unsigned long len, padded_len;
253 const char *subbuf_addr;
254 struct lttng_buffer_view subbuf_view;
255
256 health_code_update();
257 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
258
259 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
260 if (ret < 0) {
261 if (ret != -EAGAIN) {
262 PERROR("kernctl_get_subbuf snapshot");
263 goto end_unlock;
264 }
265 DBG("Kernel consumer get subbuf failed. Skipping it.");
266 consumed_pos += stream->max_sb_size;
267 stream->chan->lost_packets++;
268 continue;
269 }
270
271 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
272 if (ret < 0) {
273 ERR("Snapshot kernctl_get_subbuf_size");
274 goto error_put_subbuf;
275 }
276
277 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
278 if (ret < 0) {
279 ERR("Snapshot kernctl_get_padded_subbuf_size");
280 goto error_put_subbuf;
281 }
282
283 ret = get_current_subbuf_addr(stream, &subbuf_addr);
284 if (ret) {
285 goto error_put_subbuf;
286 }
287
288 subbuf_view = lttng_buffer_view_init(
289 subbuf_addr, 0, padded_len);
290 read_len = lttng_consumer_on_read_subbuffer_mmap(
291 stream, &subbuf_view,
292 padded_len - len);
293 /*
294 * We write the padded len in local tracefiles but the data len
295 * when using a relay. Display the error but continue processing
296 * to try to release the subbuffer.
297 */
298 if (relayd_id != (uint64_t) -1ULL) {
299 if (read_len != len) {
300 ERR("Error sending to the relay (ret: %zd != len: %lu)",
301 read_len, len);
302 }
303 } else {
304 if (read_len != padded_len) {
305 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
306 read_len, padded_len);
307 }
308 }
309
310 ret = kernctl_put_subbuf(stream->wait_fd);
311 if (ret < 0) {
312 ERR("Snapshot kernctl_put_subbuf");
313 goto end_unlock;
314 }
315 consumed_pos += stream->max_sb_size;
316 }
317
318 if (relayd_id == (uint64_t) -1ULL) {
319 if (stream->out_fd >= 0) {
320 ret = close(stream->out_fd);
321 if (ret < 0) {
322 PERROR("Kernel consumer snapshot close out_fd");
323 goto end_unlock;
324 }
325 stream->out_fd = -1;
326 }
327 } else {
328 close_relayd_stream(stream);
329 stream->net_seq_idx = (uint64_t) -1ULL;
330 }
331 lttng_trace_chunk_put(stream->trace_chunk);
332 stream->trace_chunk = NULL;
333 pthread_mutex_unlock(&stream->lock);
334 }
335
336 /* All good! */
337 ret = 0;
338 goto end;
339
340 error_put_subbuf:
341 ret = kernctl_put_subbuf(stream->wait_fd);
342 if (ret < 0) {
343 ERR("Snapshot kernctl_put_subbuf error path");
344 }
345 end_unlock:
346 pthread_mutex_unlock(&stream->lock);
347 end:
348 rcu_read_unlock();
349 return ret;
350 }
351
352 /*
353 * Read the whole metadata available for a snapshot.
354 * RCU read-side lock must be held across this function to ensure existence of
355 * metadata_channel. The channel lock must be held by the caller.
356 *
357 * Returns 0 on success, < 0 on error
358 */
359 static int lttng_kconsumer_snapshot_metadata(
360 struct lttng_consumer_channel *metadata_channel,
361 uint64_t key, char *path, uint64_t relayd_id,
362 struct lttng_consumer_local_data *ctx)
363 {
364 int ret, use_relayd = 0;
365 ssize_t ret_read;
366 struct lttng_consumer_stream *metadata_stream;
367
368 LTTNG_ASSERT(ctx);
369
370 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
371 key, path);
372
373 rcu_read_lock();
374
375 metadata_stream = metadata_channel->metadata_stream;
376 LTTNG_ASSERT(metadata_stream);
377
378 pthread_mutex_lock(&metadata_stream->lock);
379 LTTNG_ASSERT(metadata_channel->trace_chunk);
380 LTTNG_ASSERT(metadata_stream->trace_chunk);
381
382 /* Flag once that we have a valid relayd for the stream. */
383 if (relayd_id != (uint64_t) -1ULL) {
384 use_relayd = 1;
385 }
386
387 if (use_relayd) {
388 ret = consumer_send_relayd_stream(metadata_stream, path);
389 if (ret < 0) {
390 goto error_snapshot;
391 }
392 } else {
393 ret = consumer_stream_create_output_files(metadata_stream,
394 false);
395 if (ret < 0) {
396 goto error_snapshot;
397 }
398 }
399
400 do {
401 health_code_update();
402
403 ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
404 if (ret_read < 0) {
405 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
406 ret_read);
407 ret = ret_read;
408 goto error_snapshot;
409 }
410 } while (ret_read > 0);
411
412 if (use_relayd) {
413 close_relayd_stream(metadata_stream);
414 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
415 } else {
416 if (metadata_stream->out_fd >= 0) {
417 ret = close(metadata_stream->out_fd);
418 if (ret < 0) {
419 PERROR("Kernel consumer snapshot metadata close out_fd");
420 /*
421 * Don't go on error here since the snapshot was successful at this
422 * point but somehow the close failed.
423 */
424 }
425 metadata_stream->out_fd = -1;
426 lttng_trace_chunk_put(metadata_stream->trace_chunk);
427 metadata_stream->trace_chunk = NULL;
428 }
429 }
430
431 ret = 0;
432 error_snapshot:
433 pthread_mutex_unlock(&metadata_stream->lock);
434 cds_list_del(&metadata_stream->send_node);
435 consumer_stream_destroy(metadata_stream, NULL);
436 metadata_channel->metadata_stream = NULL;
437 rcu_read_unlock();
438 return ret;
439 }
440
441 /*
442 * Receive command from session daemon and process it.
443 *
444 * Return 1 on success else a negative value or 0.
445 */
446 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
447 int sock, struct pollfd *consumer_sockpoll)
448 {
449 int ret_func;
450 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
451 struct lttcomm_consumer_msg msg;
452
453 health_code_update();
454
455 {
456 ssize_t ret_recv;
457
458 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
459 if (ret_recv != sizeof(msg)) {
460 if (ret_recv > 0) {
461 lttng_consumer_send_error(ctx,
462 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
463 ret_recv = -1;
464 }
465 return ret_recv;
466 }
467 }
468
469 health_code_update();
470
471 /* Deprecated command */
472 LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP);
473
474 health_code_update();
475
476 /* relayd needs RCU read-side protection */
477 rcu_read_lock();
478
479 switch (msg.cmd_type) {
480 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
481 {
482 /* Session daemon status message are handled in the following call. */
483 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
484 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
485 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
486 msg.u.relayd_sock.relayd_session_id);
487 goto end_nosignal;
488 }
489 case LTTNG_CONSUMER_ADD_CHANNEL:
490 {
491 struct lttng_consumer_channel *new_channel;
492 int ret_send_status, ret_add_channel = 0;
493 const uint64_t chunk_id = msg.u.channel.chunk_id.value;
494
495 health_code_update();
496
497 /* First send a status message before receiving the fds. */
498 ret_send_status = consumer_send_status_msg(sock, ret_code);
499 if (ret_send_status < 0) {
500 /* Somehow, the session daemon is not responding anymore. */
501 goto error_fatal;
502 }
503
504 health_code_update();
505
506 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
507 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
508 msg.u.channel.session_id,
509 msg.u.channel.chunk_id.is_set ?
510 &chunk_id : NULL,
511 msg.u.channel.pathname,
512 msg.u.channel.name,
513 msg.u.channel.relayd_id, msg.u.channel.output,
514 msg.u.channel.tracefile_size,
515 msg.u.channel.tracefile_count, 0,
516 msg.u.channel.monitor,
517 msg.u.channel.live_timer_interval,
518 msg.u.channel.is_live,
519 NULL, NULL);
520 if (new_channel == NULL) {
521 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
522 goto end_nosignal;
523 }
524 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
525 switch (msg.u.channel.output) {
526 case LTTNG_EVENT_SPLICE:
527 new_channel->output = CONSUMER_CHANNEL_SPLICE;
528 break;
529 case LTTNG_EVENT_MMAP:
530 new_channel->output = CONSUMER_CHANNEL_MMAP;
531 break;
532 default:
533 ERR("Channel output unknown %d", msg.u.channel.output);
534 goto end_nosignal;
535 }
536
537 /* Translate and save channel type. */
538 switch (msg.u.channel.type) {
539 case CONSUMER_CHANNEL_TYPE_DATA:
540 case CONSUMER_CHANNEL_TYPE_METADATA:
541 new_channel->type = msg.u.channel.type;
542 break;
543 default:
544 abort();
545 goto end_nosignal;
546 };
547
548 health_code_update();
549
550 if (ctx->on_recv_channel != NULL) {
551 int ret_recv_channel =
552 ctx->on_recv_channel(new_channel);
553 if (ret_recv_channel == 0) {
554 ret_add_channel = consumer_add_channel(
555 new_channel, ctx);
556 } else if (ret_recv_channel < 0) {
557 goto end_nosignal;
558 }
559 } else {
560 ret_add_channel =
561 consumer_add_channel(new_channel, ctx);
562 }
563 if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA &&
564 !ret_add_channel) {
565 int monitor_start_ret;
566
567 DBG("Consumer starting monitor timer");
568 consumer_timer_live_start(new_channel,
569 msg.u.channel.live_timer_interval);
570 monitor_start_ret = consumer_timer_monitor_start(
571 new_channel,
572 msg.u.channel.monitor_timer_interval);
573 if (monitor_start_ret < 0) {
574 ERR("Starting channel monitoring timer failed");
575 goto end_nosignal;
576 }
577 }
578
579 health_code_update();
580
581 /* If we received an error in add_channel, we need to report it. */
582 if (ret_add_channel < 0) {
583 ret_send_status = consumer_send_status_msg(
584 sock, ret_add_channel);
585 if (ret_send_status < 0) {
586 goto error_fatal;
587 }
588 goto end_nosignal;
589 }
590
591 goto end_nosignal;
592 }
593 case LTTNG_CONSUMER_ADD_STREAM:
594 {
595 int fd;
596 struct lttng_pipe *stream_pipe;
597 struct lttng_consumer_stream *new_stream;
598 struct lttng_consumer_channel *channel;
599 int alloc_ret = 0;
600 int ret_send_status, ret_poll, ret_get_max_subbuf_size;
601 ssize_t ret_pipe_write, ret_recv;
602
603 /*
604 * Get stream's channel reference. Needed when adding the stream to the
605 * global hash table.
606 */
607 channel = consumer_find_channel(msg.u.stream.channel_key);
608 if (!channel) {
609 /*
610 * We could not find the channel. Can happen if cpu hotplug
611 * happens while tearing down.
612 */
613 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
614 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
615 }
616
617 health_code_update();
618
619 /* First send a status message before receiving the fds. */
620 ret_send_status = consumer_send_status_msg(sock, ret_code);
621 if (ret_send_status < 0) {
622 /* Somehow, the session daemon is not responding anymore. */
623 goto error_add_stream_fatal;
624 }
625
626 health_code_update();
627
628 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
629 /* Channel was not found. */
630 goto error_add_stream_nosignal;
631 }
632
633 /* Blocking call */
634 health_poll_entry();
635 ret_poll = lttng_consumer_poll_socket(consumer_sockpoll);
636 health_poll_exit();
637 if (ret_poll) {
638 goto error_add_stream_fatal;
639 }
640
641 health_code_update();
642
643 /* Get stream file descriptor from socket */
644 ret_recv = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
645 if (ret_recv != sizeof(fd)) {
646 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
647 ret_func = ret_recv;
648 goto end;
649 }
650
651 health_code_update();
652
653 /*
654 * Send status code to session daemon only if the recv works. If the
655 * above recv() failed, the session daemon is notified through the
656 * error socket and the teardown is eventually done.
657 */
658 ret_send_status = consumer_send_status_msg(sock, ret_code);
659 if (ret_send_status < 0) {
660 /* Somehow, the session daemon is not responding anymore. */
661 goto error_add_stream_nosignal;
662 }
663
664 health_code_update();
665
666 pthread_mutex_lock(&channel->lock);
667 new_stream = consumer_stream_create(
668 channel,
669 channel->key,
670 fd,
671 channel->name,
672 channel->relayd_id,
673 channel->session_id,
674 channel->trace_chunk,
675 msg.u.stream.cpu,
676 &alloc_ret,
677 channel->type,
678 channel->monitor);
679 if (new_stream == NULL) {
680 switch (alloc_ret) {
681 case -ENOMEM:
682 case -EINVAL:
683 default:
684 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
685 break;
686 }
687 pthread_mutex_unlock(&channel->lock);
688 goto error_add_stream_nosignal;
689 }
690
691 new_stream->wait_fd = fd;
692 ret_get_max_subbuf_size = kernctl_get_max_subbuf_size(
693 new_stream->wait_fd, &new_stream->max_sb_size);
694 if (ret_get_max_subbuf_size < 0) {
695 pthread_mutex_unlock(&channel->lock);
696 ERR("Failed to get kernel maximal subbuffer size");
697 goto error_add_stream_nosignal;
698 }
699
700 consumer_stream_update_channel_attributes(new_stream,
701 channel);
702
703 /*
704 * We've just assigned the channel to the stream so increment the
705 * refcount right now. We don't need to increment the refcount for
706 * streams in no monitor because we handle manually the cleanup of
707 * those. It is very important to make sure there is NO prior
708 * consumer_del_stream() calls or else the refcount will be unbalanced.
709 */
710 if (channel->monitor) {
711 uatomic_inc(&new_stream->chan->refcount);
712 }
713
714 /*
715 * The buffer flush is done on the session daemon side for the kernel
716 * so no need for the stream "hangup_flush_done" variable to be
717 * tracked. This is important for a kernel stream since we don't rely
718 * on the flush state of the stream to read data. It's not the case for
719 * user space tracing.
720 */
721 new_stream->hangup_flush_done = 0;
722
723 health_code_update();
724
725 pthread_mutex_lock(&new_stream->lock);
726 if (ctx->on_recv_stream) {
727 int ret_recv_stream = ctx->on_recv_stream(new_stream);
728 if (ret_recv_stream < 0) {
729 pthread_mutex_unlock(&new_stream->lock);
730 pthread_mutex_unlock(&channel->lock);
731 consumer_stream_free(new_stream);
732 goto error_add_stream_nosignal;
733 }
734 }
735 health_code_update();
736
737 if (new_stream->metadata_flag) {
738 channel->metadata_stream = new_stream;
739 }
740
741 /* Do not monitor this stream. */
742 if (!channel->monitor) {
743 DBG("Kernel consumer add stream %s in no monitor mode with "
744 "relayd id %" PRIu64, new_stream->name,
745 new_stream->net_seq_idx);
746 cds_list_add(&new_stream->send_node, &channel->streams.head);
747 pthread_mutex_unlock(&new_stream->lock);
748 pthread_mutex_unlock(&channel->lock);
749 goto end_add_stream;
750 }
751
752 /* Send stream to relayd if the stream has an ID. */
753 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
754 int ret_send_relayd_stream;
755
756 ret_send_relayd_stream = consumer_send_relayd_stream(
757 new_stream, new_stream->chan->pathname);
758 if (ret_send_relayd_stream < 0) {
759 pthread_mutex_unlock(&new_stream->lock);
760 pthread_mutex_unlock(&channel->lock);
761 consumer_stream_free(new_stream);
762 goto error_add_stream_nosignal;
763 }
764
765 /*
766 * If adding an extra stream to an already
767 * existing channel (e.g. cpu hotplug), we need
768 * to send the "streams_sent" command to relayd.
769 */
770 if (channel->streams_sent_to_relayd) {
771 int ret_send_relayd_streams_sent;
772
773 ret_send_relayd_streams_sent =
774 consumer_send_relayd_streams_sent(
775 new_stream->net_seq_idx);
776 if (ret_send_relayd_streams_sent < 0) {
777 pthread_mutex_unlock(&new_stream->lock);
778 pthread_mutex_unlock(&channel->lock);
779 goto error_add_stream_nosignal;
780 }
781 }
782 }
783 pthread_mutex_unlock(&new_stream->lock);
784 pthread_mutex_unlock(&channel->lock);
785
786 /* Get the right pipe where the stream will be sent. */
787 if (new_stream->metadata_flag) {
788 consumer_add_metadata_stream(new_stream);
789 stream_pipe = ctx->consumer_metadata_pipe;
790 } else {
791 consumer_add_data_stream(new_stream);
792 stream_pipe = ctx->consumer_data_pipe;
793 }
794
795 /* Visible to other threads */
796 new_stream->globally_visible = 1;
797
798 health_code_update();
799
800 ret_pipe_write = lttng_pipe_write(
801 stream_pipe, &new_stream, sizeof(new_stream));
802 if (ret_pipe_write < 0) {
803 ERR("Consumer write %s stream to pipe %d",
804 new_stream->metadata_flag ? "metadata" : "data",
805 lttng_pipe_get_writefd(stream_pipe));
806 if (new_stream->metadata_flag) {
807 consumer_del_stream_for_metadata(new_stream);
808 } else {
809 consumer_del_stream_for_data(new_stream);
810 }
811 goto error_add_stream_nosignal;
812 }
813
814 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64,
815 new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id);
816 end_add_stream:
817 break;
818 error_add_stream_nosignal:
819 goto end_nosignal;
820 error_add_stream_fatal:
821 goto error_fatal;
822 }
823 case LTTNG_CONSUMER_STREAMS_SENT:
824 {
825 struct lttng_consumer_channel *channel;
826 int ret_send_status;
827
828 /*
829 * Get stream's channel reference. Needed when adding the stream to the
830 * global hash table.
831 */
832 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
833 if (!channel) {
834 /*
835 * We could not find the channel. Can happen if cpu hotplug
836 * happens while tearing down.
837 */
838 ERR("Unable to find channel key %" PRIu64,
839 msg.u.sent_streams.channel_key);
840 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
841 }
842
843 health_code_update();
844
845 /*
846 * Send status code to session daemon.
847 */
848 ret_send_status = consumer_send_status_msg(sock, ret_code);
849 if (ret_send_status < 0 ||
850 ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
851 /* Somehow, the session daemon is not responding anymore. */
852 goto error_streams_sent_nosignal;
853 }
854
855 health_code_update();
856
857 /*
858 * We should not send this message if we don't monitor the
859 * streams in this channel.
860 */
861 if (!channel->monitor) {
862 goto end_error_streams_sent;
863 }
864
865 health_code_update();
866 /* Send stream to relayd if the stream has an ID. */
867 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
868 int ret_send_relay_streams;
869
870 ret_send_relay_streams = consumer_send_relayd_streams_sent(
871 msg.u.sent_streams.net_seq_idx);
872 if (ret_send_relay_streams < 0) {
873 goto error_streams_sent_nosignal;
874 }
875 channel->streams_sent_to_relayd = true;
876 }
877 end_error_streams_sent:
878 break;
879 error_streams_sent_nosignal:
880 goto end_nosignal;
881 }
882 case LTTNG_CONSUMER_UPDATE_STREAM:
883 {
884 rcu_read_unlock();
885 return -ENOSYS;
886 }
887 case LTTNG_CONSUMER_DESTROY_RELAYD:
888 {
889 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
890 struct consumer_relayd_sock_pair *relayd;
891 int ret_send_status;
892
893 DBG("Kernel consumer destroying relayd %" PRIu64, index);
894
895 /* Get relayd reference if exists. */
896 relayd = consumer_find_relayd(index);
897 if (relayd == NULL) {
898 DBG("Unable to find relayd %" PRIu64, index);
899 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
900 }
901
902 /*
903 * Each relayd socket pair has a refcount of stream attached to it
904 * which tells if the relayd is still active or not depending on the
905 * refcount value.
906 *
907 * This will set the destroy flag of the relayd object and destroy it
908 * if the refcount reaches zero when called.
909 *
910 * The destroy can happen either here or when a stream fd hangs up.
911 */
912 if (relayd) {
913 consumer_flag_relayd_for_destroy(relayd);
914 }
915
916 health_code_update();
917
918 ret_send_status = consumer_send_status_msg(sock, ret_code);
919 if (ret_send_status < 0) {
920 /* Somehow, the session daemon is not responding anymore. */
921 goto error_fatal;
922 }
923
924 goto end_nosignal;
925 }
926 case LTTNG_CONSUMER_DATA_PENDING:
927 {
928 int32_t ret_data_pending;
929 uint64_t id = msg.u.data_pending.session_id;
930 ssize_t ret_send;
931
932 DBG("Kernel consumer data pending command for id %" PRIu64, id);
933
934 ret_data_pending = consumer_data_pending(id);
935
936 health_code_update();
937
938 /* Send back returned value to session daemon */
939 ret_send = lttcomm_send_unix_sock(sock, &ret_data_pending,
940 sizeof(ret_data_pending));
941 if (ret_send < 0) {
942 PERROR("send data pending ret code");
943 goto error_fatal;
944 }
945
946 /*
947 * No need to send back a status message since the data pending
948 * returned value is the response.
949 */
950 break;
951 }
952 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
953 {
954 struct lttng_consumer_channel *channel;
955 uint64_t key = msg.u.snapshot_channel.key;
956 int ret_send_status;
957
958 channel = consumer_find_channel(key);
959 if (!channel) {
960 ERR("Channel %" PRIu64 " not found", key);
961 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
962 } else {
963 pthread_mutex_lock(&channel->lock);
964 if (msg.u.snapshot_channel.metadata == 1) {
965 int ret_snapshot;
966
967 ret_snapshot = lttng_kconsumer_snapshot_metadata(
968 channel, key,
969 msg.u.snapshot_channel.pathname,
970 msg.u.snapshot_channel.relayd_id,
971 ctx);
972 if (ret_snapshot < 0) {
973 ERR("Snapshot metadata failed");
974 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
975 }
976 } else {
977 int ret_snapshot;
978
979 ret_snapshot = lttng_kconsumer_snapshot_channel(
980 channel, key,
981 msg.u.snapshot_channel.pathname,
982 msg.u.snapshot_channel.relayd_id,
983 msg.u.snapshot_channel
984 .nb_packets_per_stream,
985 ctx);
986 if (ret_snapshot < 0) {
987 ERR("Snapshot channel failed");
988 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
989 }
990 }
991 pthread_mutex_unlock(&channel->lock);
992 }
993 health_code_update();
994
995 ret_send_status = consumer_send_status_msg(sock, ret_code);
996 if (ret_send_status < 0) {
997 /* Somehow, the session daemon is not responding anymore. */
998 goto end_nosignal;
999 }
1000 break;
1001 }
1002 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1003 {
1004 uint64_t key = msg.u.destroy_channel.key;
1005 struct lttng_consumer_channel *channel;
1006 int ret_send_status;
1007
1008 channel = consumer_find_channel(key);
1009 if (!channel) {
1010 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
1011 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1012 }
1013
1014 health_code_update();
1015
1016 ret_send_status = consumer_send_status_msg(sock, ret_code);
1017 if (ret_send_status < 0) {
1018 /* Somehow, the session daemon is not responding anymore. */
1019 goto end_destroy_channel;
1020 }
1021
1022 health_code_update();
1023
1024 /* Stop right now if no channel was found. */
1025 if (!channel) {
1026 goto end_destroy_channel;
1027 }
1028
1029 /*
1030 * This command should ONLY be issued for channel with streams set in
1031 * no monitor mode.
1032 */
1033 LTTNG_ASSERT(!channel->monitor);
1034
1035 /*
1036 * The refcount should ALWAYS be 0 in the case of a channel in no
1037 * monitor mode.
1038 */
1039 LTTNG_ASSERT(!uatomic_sub_return(&channel->refcount, 1));
1040
1041 consumer_del_channel(channel);
1042 end_destroy_channel:
1043 goto end_nosignal;
1044 }
1045 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1046 {
1047 ssize_t ret;
1048 uint64_t count;
1049 struct lttng_consumer_channel *channel;
1050 uint64_t id = msg.u.discarded_events.session_id;
1051 uint64_t key = msg.u.discarded_events.channel_key;
1052
1053 DBG("Kernel consumer discarded events command for session id %"
1054 PRIu64 ", channel key %" PRIu64, id, key);
1055
1056 channel = consumer_find_channel(key);
1057 if (!channel) {
1058 ERR("Kernel consumer discarded events channel %"
1059 PRIu64 " not found", key);
1060 count = 0;
1061 } else {
1062 count = channel->discarded_events;
1063 }
1064
1065 health_code_update();
1066
1067 /* Send back returned value to session daemon */
1068 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1069 if (ret < 0) {
1070 PERROR("send discarded events");
1071 goto error_fatal;
1072 }
1073
1074 break;
1075 }
1076 case LTTNG_CONSUMER_LOST_PACKETS:
1077 {
1078 ssize_t ret;
1079 uint64_t count;
1080 struct lttng_consumer_channel *channel;
1081 uint64_t id = msg.u.lost_packets.session_id;
1082 uint64_t key = msg.u.lost_packets.channel_key;
1083
1084 DBG("Kernel consumer lost packets command for session id %"
1085 PRIu64 ", channel key %" PRIu64, id, key);
1086
1087 channel = consumer_find_channel(key);
1088 if (!channel) {
1089 ERR("Kernel consumer lost packets channel %"
1090 PRIu64 " not found", key);
1091 count = 0;
1092 } else {
1093 count = channel->lost_packets;
1094 }
1095
1096 health_code_update();
1097
1098 /* Send back returned value to session daemon */
1099 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1100 if (ret < 0) {
1101 PERROR("send lost packets");
1102 goto error_fatal;
1103 }
1104
1105 break;
1106 }
1107 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1108 {
1109 int channel_monitor_pipe;
1110 int ret_send_status, ret_set_channel_monitor_pipe;
1111 ssize_t ret_recv;
1112
1113 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1114 /* Successfully received the command's type. */
1115 ret_send_status = consumer_send_status_msg(sock, ret_code);
1116 if (ret_send_status < 0) {
1117 goto error_fatal;
1118 }
1119
1120 ret_recv = lttcomm_recv_fds_unix_sock(
1121 sock, &channel_monitor_pipe, 1);
1122 if (ret_recv != sizeof(channel_monitor_pipe)) {
1123 ERR("Failed to receive channel monitor pipe");
1124 goto error_fatal;
1125 }
1126
1127 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1128 ret_set_channel_monitor_pipe =
1129 consumer_timer_thread_set_channel_monitor_pipe(
1130 channel_monitor_pipe);
1131 if (!ret_set_channel_monitor_pipe) {
1132 int flags;
1133 int ret_fcntl;
1134
1135 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1136 /* Set the pipe as non-blocking. */
1137 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
1138 if (ret_fcntl == -1) {
1139 PERROR("fcntl get flags of the channel monitoring pipe");
1140 goto error_fatal;
1141 }
1142 flags = ret_fcntl;
1143
1144 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
1145 flags | O_NONBLOCK);
1146 if (ret_fcntl == -1) {
1147 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1148 goto error_fatal;
1149 }
1150 DBG("Channel monitor pipe set as non-blocking");
1151 } else {
1152 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1153 }
1154 ret_send_status = consumer_send_status_msg(sock, ret_code);
1155 if (ret_send_status < 0) {
1156 goto error_fatal;
1157 }
1158 break;
1159 }
1160 case LTTNG_CONSUMER_ROTATE_CHANNEL:
1161 {
1162 struct lttng_consumer_channel *channel;
1163 uint64_t key = msg.u.rotate_channel.key;
1164 int ret_send_status;
1165
1166 DBG("Consumer rotate channel %" PRIu64, key);
1167
1168 channel = consumer_find_channel(key);
1169 if (!channel) {
1170 ERR("Channel %" PRIu64 " not found", key);
1171 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1172 } else {
1173 /*
1174 * Sample the rotate position of all the streams in this channel.
1175 */
1176 int ret_rotate_channel;
1177
1178 ret_rotate_channel = lttng_consumer_rotate_channel(
1179 channel, key,
1180 msg.u.rotate_channel.relayd_id,
1181 msg.u.rotate_channel.metadata, ctx);
1182 if (ret_rotate_channel < 0) {
1183 ERR("Rotate channel failed");
1184 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
1185 }
1186
1187 health_code_update();
1188 }
1189
1190 ret_send_status = consumer_send_status_msg(sock, ret_code);
1191 if (ret_send_status < 0) {
1192 /* Somehow, the session daemon is not responding anymore. */
1193 goto error_rotate_channel;
1194 }
1195 if (channel) {
1196 /* Rotate the streams that are ready right now. */
1197 int ret_rotate;
1198
1199 ret_rotate = lttng_consumer_rotate_ready_streams(
1200 channel, key, ctx);
1201 if (ret_rotate < 0) {
1202 ERR("Rotate ready streams failed");
1203 }
1204 }
1205 break;
1206 error_rotate_channel:
1207 goto end_nosignal;
1208 }
1209 case LTTNG_CONSUMER_CLEAR_CHANNEL:
1210 {
1211 struct lttng_consumer_channel *channel;
1212 uint64_t key = msg.u.clear_channel.key;
1213 int ret_send_status;
1214
1215 channel = consumer_find_channel(key);
1216 if (!channel) {
1217 DBG("Channel %" PRIu64 " not found", key);
1218 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1219 } else {
1220 int ret_clear_channel;
1221
1222 ret_clear_channel =
1223 lttng_consumer_clear_channel(channel);
1224 if (ret_clear_channel) {
1225 ERR("Clear channel failed");
1226 ret_code = ret_clear_channel;
1227 }
1228
1229 health_code_update();
1230 }
1231
1232 ret_send_status = consumer_send_status_msg(sock, ret_code);
1233 if (ret_send_status < 0) {
1234 /* Somehow, the session daemon is not responding anymore. */
1235 goto end_nosignal;
1236 }
1237
1238 break;
1239 }
1240 case LTTNG_CONSUMER_INIT:
1241 {
1242 int ret_send_status;
1243
1244 ret_code = lttng_consumer_init_command(ctx,
1245 msg.u.init.sessiond_uuid);
1246 health_code_update();
1247 ret_send_status = consumer_send_status_msg(sock, ret_code);
1248 if (ret_send_status < 0) {
1249 /* Somehow, the session daemon is not responding anymore. */
1250 goto end_nosignal;
1251 }
1252 break;
1253 }
1254 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
1255 {
1256 const struct lttng_credentials credentials = {
1257 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
1258 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
1259 };
1260 const bool is_local_trace =
1261 !msg.u.create_trace_chunk.relayd_id.is_set;
1262 const uint64_t relayd_id =
1263 msg.u.create_trace_chunk.relayd_id.value;
1264 const char *chunk_override_name =
1265 *msg.u.create_trace_chunk.override_name ?
1266 msg.u.create_trace_chunk.override_name :
1267 NULL;
1268 struct lttng_directory_handle *chunk_directory_handle = NULL;
1269
1270 /*
1271 * The session daemon will only provide a chunk directory file
1272 * descriptor for local traces.
1273 */
1274 if (is_local_trace) {
1275 int chunk_dirfd;
1276 int ret_send_status;
1277 ssize_t ret_recv;
1278
1279 /* Acnowledge the reception of the command. */
1280 ret_send_status = consumer_send_status_msg(
1281 sock, LTTCOMM_CONSUMERD_SUCCESS);
1282 if (ret_send_status < 0) {
1283 /* Somehow, the session daemon is not responding anymore. */
1284 goto end_nosignal;
1285 }
1286
1287 ret_recv = lttcomm_recv_fds_unix_sock(
1288 sock, &chunk_dirfd, 1);
1289 if (ret_recv != sizeof(chunk_dirfd)) {
1290 ERR("Failed to receive trace chunk directory file descriptor");
1291 goto error_fatal;
1292 }
1293
1294 DBG("Received trace chunk directory fd (%d)",
1295 chunk_dirfd);
1296 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
1297 chunk_dirfd);
1298 if (!chunk_directory_handle) {
1299 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1300 if (close(chunk_dirfd)) {
1301 PERROR("Failed to close chunk directory file descriptor");
1302 }
1303 goto error_fatal;
1304 }
1305 }
1306
1307 ret_code = lttng_consumer_create_trace_chunk(
1308 !is_local_trace ? &relayd_id : NULL,
1309 msg.u.create_trace_chunk.session_id,
1310 msg.u.create_trace_chunk.chunk_id,
1311 (time_t) msg.u.create_trace_chunk
1312 .creation_timestamp,
1313 chunk_override_name,
1314 msg.u.create_trace_chunk.credentials.is_set ?
1315 &credentials :
1316 NULL,
1317 chunk_directory_handle);
1318 lttng_directory_handle_put(chunk_directory_handle);
1319 goto end_msg_sessiond;
1320 }
1321 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
1322 {
1323 enum lttng_trace_chunk_command_type close_command =
1324 msg.u.close_trace_chunk.close_command.value;
1325 const uint64_t relayd_id =
1326 msg.u.close_trace_chunk.relayd_id.value;
1327 struct lttcomm_consumer_close_trace_chunk_reply reply;
1328 char path[LTTNG_PATH_MAX];
1329 ssize_t ret_send;
1330
1331 ret_code = lttng_consumer_close_trace_chunk(
1332 msg.u.close_trace_chunk.relayd_id.is_set ?
1333 &relayd_id :
1334 NULL,
1335 msg.u.close_trace_chunk.session_id,
1336 msg.u.close_trace_chunk.chunk_id,
1337 (time_t) msg.u.close_trace_chunk.close_timestamp,
1338 msg.u.close_trace_chunk.close_command.is_set ?
1339 &close_command :
1340 NULL, path);
1341 reply.ret_code = ret_code;
1342 reply.path_length = strlen(path) + 1;
1343 ret_send = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
1344 if (ret_send != sizeof(reply)) {
1345 goto error_fatal;
1346 }
1347 ret_send = lttcomm_send_unix_sock(
1348 sock, path, reply.path_length);
1349 if (ret_send != reply.path_length) {
1350 goto error_fatal;
1351 }
1352 goto end_nosignal;
1353 }
1354 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
1355 {
1356 const uint64_t relayd_id =
1357 msg.u.trace_chunk_exists.relayd_id.value;
1358
1359 ret_code = lttng_consumer_trace_chunk_exists(
1360 msg.u.trace_chunk_exists.relayd_id.is_set ?
1361 &relayd_id : NULL,
1362 msg.u.trace_chunk_exists.session_id,
1363 msg.u.trace_chunk_exists.chunk_id);
1364 goto end_msg_sessiond;
1365 }
1366 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
1367 {
1368 const uint64_t key = msg.u.open_channel_packets.key;
1369 struct lttng_consumer_channel *channel =
1370 consumer_find_channel(key);
1371
1372 if (channel) {
1373 pthread_mutex_lock(&channel->lock);
1374 ret_code = lttng_consumer_open_channel_packets(channel);
1375 pthread_mutex_unlock(&channel->lock);
1376 } else {
1377 WARN("Channel %" PRIu64 " not found", key);
1378 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1379 }
1380
1381 health_code_update();
1382 goto end_msg_sessiond;
1383 }
1384 default:
1385 goto end_nosignal;
1386 }
1387
1388 end_nosignal:
1389 /*
1390 * Return 1 to indicate success since the 0 value can be a socket
1391 * shutdown during the recv() or send() call.
1392 */
1393 ret_func = 1;
1394 goto end;
1395 error_fatal:
1396 /* This will issue a consumer stop. */
1397 ret_func = -1;
1398 goto end;
1399 end_msg_sessiond:
1400 /*
1401 * The returned value here is not useful since either way we'll return 1 to
1402 * the caller because the session daemon socket management is done
1403 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1404 */
1405 {
1406 int ret_send_status;
1407
1408 ret_send_status = consumer_send_status_msg(sock, ret_code);
1409 if (ret_send_status < 0) {
1410 goto error_fatal;
1411 }
1412 }
1413
1414 ret_func = 1;
1415
1416 end:
1417 health_code_update();
1418 rcu_read_unlock();
1419 return ret_func;
1420 }
1421
1422 /*
1423 * Sync metadata meaning request them to the session daemon and snapshot to the
1424 * metadata thread can consumer them.
1425 *
1426 * Metadata stream lock MUST be acquired.
1427 */
1428 enum sync_metadata_status lttng_kconsumer_sync_metadata(
1429 struct lttng_consumer_stream *metadata)
1430 {
1431 int ret;
1432 enum sync_metadata_status status;
1433
1434 LTTNG_ASSERT(metadata);
1435
1436 ret = kernctl_buffer_flush(metadata->wait_fd);
1437 if (ret < 0) {
1438 ERR("Failed to flush kernel stream");
1439 status = SYNC_METADATA_STATUS_ERROR;
1440 goto end;
1441 }
1442
1443 ret = kernctl_snapshot(metadata->wait_fd);
1444 if (ret < 0) {
1445 if (errno == EAGAIN) {
1446 /* No new metadata, exit. */
1447 DBG("Sync metadata, no new kernel metadata");
1448 status = SYNC_METADATA_STATUS_NO_DATA;
1449 } else {
1450 ERR("Sync metadata, taking kernel snapshot failed.");
1451 status = SYNC_METADATA_STATUS_ERROR;
1452 }
1453 } else {
1454 status = SYNC_METADATA_STATUS_NEW_DATA;
1455 }
1456
1457 end:
1458 return status;
1459 }
1460
1461 static
1462 int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
1463 struct stream_subbuffer *subbuf)
1464 {
1465 int ret;
1466
1467 ret = kernctl_get_subbuf_size(
1468 stream->wait_fd, &subbuf->info.data.subbuf_size);
1469 if (ret) {
1470 goto end;
1471 }
1472
1473 ret = kernctl_get_padded_subbuf_size(
1474 stream->wait_fd, &subbuf->info.data.padded_subbuf_size);
1475 if (ret) {
1476 goto end;
1477 }
1478
1479 end:
1480 return ret;
1481 }
1482
1483 static
1484 int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
1485 struct stream_subbuffer *subbuf)
1486 {
1487 int ret;
1488
1489 ret = extract_common_subbuffer_info(stream, subbuf);
1490 if (ret) {
1491 goto end;
1492 }
1493
1494 ret = kernctl_get_metadata_version(
1495 stream->wait_fd, &subbuf->info.metadata.version);
1496 if (ret) {
1497 goto end;
1498 }
1499
1500 end:
1501 return ret;
1502 }
1503
1504 static
1505 int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
1506 struct stream_subbuffer *subbuf)
1507 {
1508 int ret;
1509
1510 ret = extract_common_subbuffer_info(stream, subbuf);
1511 if (ret) {
1512 goto end;
1513 }
1514
1515 ret = kernctl_get_packet_size(
1516 stream->wait_fd, &subbuf->info.data.packet_size);
1517 if (ret < 0) {
1518 PERROR("Failed to get sub-buffer packet size");
1519 goto end;
1520 }
1521
1522 ret = kernctl_get_content_size(
1523 stream->wait_fd, &subbuf->info.data.content_size);
1524 if (ret < 0) {
1525 PERROR("Failed to get sub-buffer content size");
1526 goto end;
1527 }
1528
1529 ret = kernctl_get_timestamp_begin(
1530 stream->wait_fd, &subbuf->info.data.timestamp_begin);
1531 if (ret < 0) {
1532 PERROR("Failed to get sub-buffer begin timestamp");
1533 goto end;
1534 }
1535
1536 ret = kernctl_get_timestamp_end(
1537 stream->wait_fd, &subbuf->info.data.timestamp_end);
1538 if (ret < 0) {
1539 PERROR("Failed to get sub-buffer end timestamp");
1540 goto end;
1541 }
1542
1543 ret = kernctl_get_events_discarded(
1544 stream->wait_fd, &subbuf->info.data.events_discarded);
1545 if (ret) {
1546 PERROR("Failed to get sub-buffer events discarded count");
1547 goto end;
1548 }
1549
1550 ret = kernctl_get_sequence_number(stream->wait_fd,
1551 &subbuf->info.data.sequence_number.value);
1552 if (ret) {
1553 /* May not be supported by older LTTng-modules. */
1554 if (ret != -ENOTTY) {
1555 PERROR("Failed to get sub-buffer sequence number");
1556 goto end;
1557 }
1558 } else {
1559 subbuf->info.data.sequence_number.is_set = true;
1560 }
1561
1562 ret = kernctl_get_stream_id(
1563 stream->wait_fd, &subbuf->info.data.stream_id);
1564 if (ret < 0) {
1565 PERROR("Failed to get stream id");
1566 goto end;
1567 }
1568
1569 ret = kernctl_get_instance_id(stream->wait_fd,
1570 &subbuf->info.data.stream_instance_id.value);
1571 if (ret) {
1572 /* May not be supported by older LTTng-modules. */
1573 if (ret != -ENOTTY) {
1574 PERROR("Failed to get stream instance id");
1575 goto end;
1576 }
1577 } else {
1578 subbuf->info.data.stream_instance_id.is_set = true;
1579 }
1580 end:
1581 return ret;
1582 }
1583
1584 static
1585 enum get_next_subbuffer_status get_subbuffer_common(
1586 struct lttng_consumer_stream *stream,
1587 struct stream_subbuffer *subbuffer)
1588 {
1589 int ret;
1590 enum get_next_subbuffer_status status;
1591
1592 ret = kernctl_get_next_subbuf(stream->wait_fd);
1593 switch (ret) {
1594 case 0:
1595 status = GET_NEXT_SUBBUFFER_STATUS_OK;
1596 break;
1597 case -ENODATA:
1598 case -EAGAIN:
1599 /*
1600 * The caller only expects -ENODATA when there is no data to
1601 * read, but the kernel tracer returns -EAGAIN when there is
1602 * currently no data for a non-finalized stream, and -ENODATA
1603 * when there is no data for a finalized stream. Those can be
1604 * combined into a -ENODATA return value.
1605 */
1606 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
1607 goto end;
1608 default:
1609 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
1610 goto end;
1611 }
1612
1613 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
1614 stream, subbuffer);
1615 if (ret) {
1616 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
1617 }
1618 end:
1619 return status;
1620 }
1621
1622 static
1623 enum get_next_subbuffer_status get_next_subbuffer_splice(
1624 struct lttng_consumer_stream *stream,
1625 struct stream_subbuffer *subbuffer)
1626 {
1627 const enum get_next_subbuffer_status status =
1628 get_subbuffer_common(stream, subbuffer);
1629
1630 if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
1631 goto end;
1632 }
1633
1634 subbuffer->buffer.fd = stream->wait_fd;
1635 end:
1636 return status;
1637 }
1638
1639 static
1640 enum get_next_subbuffer_status get_next_subbuffer_mmap(
1641 struct lttng_consumer_stream *stream,
1642 struct stream_subbuffer *subbuffer)
1643 {
1644 int ret;
1645 enum get_next_subbuffer_status status;
1646 const char *addr;
1647
1648 status = get_subbuffer_common(stream, subbuffer);
1649 if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
1650 goto end;
1651 }
1652
1653 ret = get_current_subbuf_addr(stream, &addr);
1654 if (ret) {
1655 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
1656 goto end;
1657 }
1658
1659 subbuffer->buffer.buffer = lttng_buffer_view_init(
1660 addr, 0, subbuffer->info.data.padded_subbuf_size);
1661 end:
1662 return status;
1663 }
1664
1665 static
1666 enum get_next_subbuffer_status get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
1667 struct stream_subbuffer *subbuffer)
1668 {
1669 int ret;
1670 const char *addr;
1671 bool coherent;
1672 enum get_next_subbuffer_status status;
1673
1674 ret = kernctl_get_next_subbuf_metadata_check(stream->wait_fd,
1675 &coherent);
1676 if (ret) {
1677 goto end;
1678 }
1679
1680 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
1681 stream, subbuffer);
1682 if (ret) {
1683 goto end;
1684 }
1685
1686 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
1687
1688 ret = get_current_subbuf_addr(stream, &addr);
1689 if (ret) {
1690 goto end;
1691 }
1692
1693 subbuffer->buffer.buffer = lttng_buffer_view_init(
1694 addr, 0, subbuffer->info.data.padded_subbuf_size);
1695 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1696 subbuffer->info.metadata.padded_subbuf_size,
1697 coherent ? "true" : "false");
1698 end:
1699 /*
1700 * The caller only expects -ENODATA when there is no data to read, but
1701 * the kernel tracer returns -EAGAIN when there is currently no data
1702 * for a non-finalized stream, and -ENODATA when there is no data for a
1703 * finalized stream. Those can be combined into a -ENODATA return value.
1704 */
1705 switch (ret) {
1706 case 0:
1707 status = GET_NEXT_SUBBUFFER_STATUS_OK;
1708 break;
1709 case -ENODATA:
1710 case -EAGAIN:
1711 /*
1712 * The caller only expects -ENODATA when there is no data to
1713 * read, but the kernel tracer returns -EAGAIN when there is
1714 * currently no data for a non-finalized stream, and -ENODATA
1715 * when there is no data for a finalized stream. Those can be
1716 * combined into a -ENODATA return value.
1717 */
1718 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
1719 break;
1720 default:
1721 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
1722 break;
1723 }
1724
1725 return status;
1726 }
1727
1728 static
1729 int put_next_subbuffer(struct lttng_consumer_stream *stream,
1730 struct stream_subbuffer *subbuffer)
1731 {
1732 const int ret = kernctl_put_next_subbuf(stream->wait_fd);
1733
1734 if (ret) {
1735 if (ret == -EFAULT) {
1736 PERROR("Error in unreserving sub buffer");
1737 } else if (ret == -EIO) {
1738 /* Should never happen with newer LTTng versions */
1739 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1740 }
1741 }
1742
1743 return ret;
1744 }
1745
1746 static
1747 bool is_get_next_check_metadata_available(int tracer_fd)
1748 {
1749 const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL);
1750 const bool available = ret != -ENOTTY;
1751
1752 if (ret == 0) {
1753 /* get succeeded, make sure to put the subbuffer. */
1754 kernctl_put_subbuf(tracer_fd);
1755 }
1756
1757 return available;
1758 }
1759
1760 static
1761 int signal_metadata(struct lttng_consumer_stream *stream,
1762 struct lttng_consumer_local_data *ctx)
1763 {
1764 ASSERT_LOCKED(stream->metadata_rdv_lock);
1765 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
1766 }
1767
1768 static
1769 int lttng_kconsumer_set_stream_ops(
1770 struct lttng_consumer_stream *stream)
1771 {
1772 int ret = 0;
1773
1774 if (stream->metadata_flag && stream->chan->is_live) {
1775 DBG("Attempting to enable metadata bucketization for live consumers");
1776 if (is_get_next_check_metadata_available(stream->wait_fd)) {
1777 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1778 stream->read_subbuffer_ops.get_next_subbuffer =
1779 get_next_subbuffer_metadata_check;
1780 ret = consumer_stream_enable_metadata_bucketization(
1781 stream);
1782 if (ret) {
1783 goto end;
1784 }
1785 } else {
1786 /*
1787 * The kernel tracer version is too old to indicate
1788 * when the metadata stream has reached a "coherent"
1789 * (parseable) point.
1790 *
1791 * This means that a live viewer may see an incoherent
1792 * sequence of metadata and fail to parse it.
1793 */
1794 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1795 metadata_bucket_destroy(stream->metadata_bucket);
1796 stream->metadata_bucket = NULL;
1797 }
1798
1799 stream->read_subbuffer_ops.on_sleep = signal_metadata;
1800 }
1801
1802 if (!stream->read_subbuffer_ops.get_next_subbuffer) {
1803 if (stream->chan->output == CONSUMER_CHANNEL_MMAP) {
1804 stream->read_subbuffer_ops.get_next_subbuffer =
1805 get_next_subbuffer_mmap;
1806 } else {
1807 stream->read_subbuffer_ops.get_next_subbuffer =
1808 get_next_subbuffer_splice;
1809 }
1810 }
1811
1812 if (stream->metadata_flag) {
1813 stream->read_subbuffer_ops.extract_subbuffer_info =
1814 extract_metadata_subbuffer_info;
1815 } else {
1816 stream->read_subbuffer_ops.extract_subbuffer_info =
1817 extract_data_subbuffer_info;
1818 if (stream->chan->is_live) {
1819 stream->read_subbuffer_ops.send_live_beacon =
1820 consumer_flush_kernel_index;
1821 }
1822 }
1823
1824 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
1825 end:
1826 return ret;
1827 }
1828
1829 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1830 {
1831 int ret;
1832
1833 LTTNG_ASSERT(stream);
1834
1835 /*
1836 * Don't create anything if this is set for streaming or if there is
1837 * no current trace chunk on the parent channel.
1838 */
1839 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
1840 stream->chan->trace_chunk) {
1841 ret = consumer_stream_create_output_files(stream, true);
1842 if (ret) {
1843 goto error;
1844 }
1845 }
1846
1847 if (stream->output == LTTNG_EVENT_MMAP) {
1848 /* get the len of the mmap region */
1849 unsigned long mmap_len;
1850
1851 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1852 if (ret != 0) {
1853 PERROR("kernctl_get_mmap_len");
1854 goto error_close_fd;
1855 }
1856 stream->mmap_len = (size_t) mmap_len;
1857
1858 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1859 MAP_PRIVATE, stream->wait_fd, 0);
1860 if (stream->mmap_base == MAP_FAILED) {
1861 PERROR("Error mmaping");
1862 ret = -1;
1863 goto error_close_fd;
1864 }
1865 }
1866
1867 ret = lttng_kconsumer_set_stream_ops(stream);
1868 if (ret) {
1869 goto error_close_fd;
1870 }
1871
1872 /* we return 0 to let the library handle the FD internally */
1873 return 0;
1874
1875 error_close_fd:
1876 if (stream->out_fd >= 0) {
1877 int err;
1878
1879 err = close(stream->out_fd);
1880 LTTNG_ASSERT(!err);
1881 stream->out_fd = -1;
1882 }
1883 error:
1884 return ret;
1885 }
1886
1887 /*
1888 * Check if data is still being extracted from the buffers for a specific
1889 * stream. Consumer data lock MUST be acquired before calling this function
1890 * and the stream lock.
1891 *
1892 * Return 1 if the traced data are still getting read else 0 meaning that the
1893 * data is available for trace viewer reading.
1894 */
1895 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1896 {
1897 int ret;
1898
1899 LTTNG_ASSERT(stream);
1900
1901 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1902 ret = 0;
1903 goto end;
1904 }
1905
1906 ret = kernctl_get_next_subbuf(stream->wait_fd);
1907 if (ret == 0) {
1908 /* There is still data so let's put back this subbuffer. */
1909 ret = kernctl_put_subbuf(stream->wait_fd);
1910 LTTNG_ASSERT(ret == 0);
1911 ret = 1; /* Data is pending */
1912 goto end;
1913 }
1914
1915 /* Data is NOT pending and ready to be read. */
1916 ret = 0;
1917
1918 end:
1919 return ret;
1920 }
This page took 0.104343 seconds and 4 git commands to generate.