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