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