Rename LTTng index in CTF index
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <poll.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/pipe.h>
39 #include <common/relayd/relayd.h>
40 #include <common/utils.h>
41 #include <common/consumer-stream.h>
42 #include <common/index/index.h>
43 #include <common/consumer-timer.h>
44
45 #include "kernel-consumer.h"
46
47 extern struct lttng_consumer_global_data consumer_data;
48 extern int consumer_poll_timeout;
49 extern volatile int consumer_quit;
50
51 /*
52 * Take a snapshot for a specific fd
53 *
54 * Returns 0 on success, < 0 on error
55 */
56 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
57 {
58 int ret = 0;
59 int infd = stream->wait_fd;
60
61 ret = kernctl_snapshot(infd);
62 if (ret != 0) {
63 perror("Getting sub-buffer snapshot.");
64 ret = -errno;
65 }
66
67 return ret;
68 }
69
70 /*
71 * Get the produced position
72 *
73 * Returns 0 on success, < 0 on error
74 */
75 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
76 unsigned long *pos)
77 {
78 int ret;
79 int infd = stream->wait_fd;
80
81 ret = kernctl_snapshot_get_produced(infd, pos);
82 if (ret != 0) {
83 perror("kernctl_snapshot_get_produced");
84 ret = -errno;
85 }
86
87 return ret;
88 }
89
90 /*
91 * Get the consumerd position
92 *
93 * Returns 0 on success, < 0 on error
94 */
95 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
96 unsigned long *pos)
97 {
98 int ret;
99 int infd = stream->wait_fd;
100
101 ret = kernctl_snapshot_get_consumed(infd, pos);
102 if (ret != 0) {
103 perror("kernctl_snapshot_get_consumed");
104 ret = -errno;
105 }
106
107 return ret;
108 }
109
110 /*
111 * Take a snapshot of all the stream of a channel
112 *
113 * Returns 0 on success, < 0 on error
114 */
115 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
116 uint64_t relayd_id, uint64_t max_stream_size,
117 struct lttng_consumer_local_data *ctx)
118 {
119 int ret;
120 unsigned long consumed_pos, produced_pos;
121 struct lttng_consumer_channel *channel;
122 struct lttng_consumer_stream *stream;
123
124 DBG("Kernel consumer snapshot channel %" PRIu64, key);
125
126 rcu_read_lock();
127
128 channel = consumer_find_channel(key);
129 if (!channel) {
130 ERR("No channel found for key %" PRIu64, key);
131 ret = -1;
132 goto end;
133 }
134
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel->output != CONSUMER_CHANNEL_MMAP) {
137 ERR("Unsupported output %d", channel->output);
138 ret = -1;
139 goto end;
140 }
141
142 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
143
144 health_code_update();
145
146 /*
147 * Lock stream because we are about to change its state.
148 */
149 pthread_mutex_lock(&stream->lock);
150
151 /*
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
154 */
155 stream->net_seq_idx = relayd_id;
156 channel->relayd_id = relayd_id;
157 if (relayd_id != (uint64_t) -1ULL) {
158 ret = consumer_send_relayd_stream(stream, path);
159 if (ret < 0) {
160 ERR("sending stream to relayd");
161 goto end_unlock;
162 }
163 } else {
164 ret = utils_create_stream_file(path, stream->name,
165 stream->chan->tracefile_size,
166 stream->tracefile_count_current,
167 stream->uid, stream->gid, NULL);
168 if (ret < 0) {
169 ERR("utils_create_stream_file");
170 goto end_unlock;
171 }
172
173 stream->out_fd = ret;
174 stream->tracefile_size_current = 0;
175
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
177 path, stream->name, stream->key);
178 }
179
180 ret = kernctl_buffer_flush(stream->wait_fd);
181 if (ret < 0) {
182 ERR("Failed to flush kernel stream");
183 ret = -errno;
184 goto end_unlock;
185 }
186
187 ret = lttng_kconsumer_take_snapshot(stream);
188 if (ret < 0) {
189 ERR("Taking kernel snapshot");
190 goto end_unlock;
191 }
192
193 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
194 if (ret < 0) {
195 ERR("Produced kernel snapshot position");
196 goto end_unlock;
197 }
198
199 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
200 if (ret < 0) {
201 ERR("Consumerd kernel snapshot position");
202 goto end_unlock;
203 }
204
205 if (stream->max_sb_size == 0) {
206 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
207 &stream->max_sb_size);
208 if (ret < 0) {
209 ERR("Getting kernel max_sb_size");
210 ret = -errno;
211 goto end_unlock;
212 }
213 }
214
215 /*
216 * The original value is sent back if max stream size is larger than
217 * the possible size of the snapshot. Also, we asume that the session
218 * daemon should never send a maximum stream size that is lower than
219 * subbuffer size.
220 */
221 consumed_pos = consumer_get_consumed_maxsize(consumed_pos,
222 produced_pos, max_stream_size);
223
224 while (consumed_pos < produced_pos) {
225 ssize_t read_len;
226 unsigned long len, padded_len;
227
228 health_code_update();
229
230 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
231
232 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
233 if (ret < 0) {
234 if (errno != EAGAIN) {
235 PERROR("kernctl_get_subbuf snapshot");
236 ret = -errno;
237 goto end_unlock;
238 }
239 DBG("Kernel consumer get subbuf failed. Skipping it.");
240 consumed_pos += stream->max_sb_size;
241 continue;
242 }
243
244 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
245 if (ret < 0) {
246 ERR("Snapshot kernctl_get_subbuf_size");
247 ret = -errno;
248 goto error_put_subbuf;
249 }
250
251 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
252 if (ret < 0) {
253 ERR("Snapshot kernctl_get_padded_subbuf_size");
254 ret = -errno;
255 goto error_put_subbuf;
256 }
257
258 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
259 padded_len - len, NULL);
260 /*
261 * We write the padded len in local tracefiles but the data len
262 * when using a relay. Display the error but continue processing
263 * to try to release the subbuffer.
264 */
265 if (relayd_id != (uint64_t) -1ULL) {
266 if (read_len != len) {
267 ERR("Error sending to the relay (ret: %zd != len: %lu)",
268 read_len, len);
269 }
270 } else {
271 if (read_len != padded_len) {
272 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
273 read_len, padded_len);
274 }
275 }
276
277 ret = kernctl_put_subbuf(stream->wait_fd);
278 if (ret < 0) {
279 ERR("Snapshot kernctl_put_subbuf");
280 ret = -errno;
281 goto end_unlock;
282 }
283 consumed_pos += stream->max_sb_size;
284 }
285
286 if (relayd_id == (uint64_t) -1ULL) {
287 if (stream->out_fd >= 0) {
288 ret = close(stream->out_fd);
289 if (ret < 0) {
290 PERROR("Kernel consumer snapshot close out_fd");
291 goto end_unlock;
292 }
293 stream->out_fd = -1;
294 }
295 } else {
296 close_relayd_stream(stream);
297 stream->net_seq_idx = (uint64_t) -1ULL;
298 }
299 pthread_mutex_unlock(&stream->lock);
300 }
301
302 /* All good! */
303 ret = 0;
304 goto end;
305
306 error_put_subbuf:
307 ret = kernctl_put_subbuf(stream->wait_fd);
308 if (ret < 0) {
309 ret = -errno;
310 ERR("Snapshot kernctl_put_subbuf error path");
311 }
312 end_unlock:
313 pthread_mutex_unlock(&stream->lock);
314 end:
315 rcu_read_unlock();
316 return ret;
317 }
318
319 /*
320 * Read the whole metadata available for a snapshot.
321 *
322 * Returns 0 on success, < 0 on error
323 */
324 int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
325 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
326 {
327 int ret, use_relayd = 0;
328 ssize_t ret_read;
329 struct lttng_consumer_channel *metadata_channel;
330 struct lttng_consumer_stream *metadata_stream;
331
332 assert(ctx);
333
334 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
335 key, path);
336
337 rcu_read_lock();
338
339 metadata_channel = consumer_find_channel(key);
340 if (!metadata_channel) {
341 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
342 ret = -1;
343 goto error;
344 }
345
346 metadata_stream = metadata_channel->metadata_stream;
347 assert(metadata_stream);
348
349 /* Flag once that we have a valid relayd for the stream. */
350 if (relayd_id != (uint64_t) -1ULL) {
351 use_relayd = 1;
352 }
353
354 if (use_relayd) {
355 ret = consumer_send_relayd_stream(metadata_stream, path);
356 if (ret < 0) {
357 goto error;
358 }
359 } else {
360 ret = utils_create_stream_file(path, metadata_stream->name,
361 metadata_stream->chan->tracefile_size,
362 metadata_stream->tracefile_count_current,
363 metadata_stream->uid, metadata_stream->gid, NULL);
364 if (ret < 0) {
365 goto error;
366 }
367 metadata_stream->out_fd = ret;
368 }
369
370 do {
371 health_code_update();
372
373 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
374 if (ret_read < 0) {
375 if (ret_read != -EAGAIN) {
376 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
377 ret_read);
378 goto error;
379 }
380 /* ret_read is negative at this point so we will exit the loop. */
381 continue;
382 }
383 } while (ret_read >= 0);
384
385 if (use_relayd) {
386 close_relayd_stream(metadata_stream);
387 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
388 } else {
389 if (metadata_stream->out_fd >= 0) {
390 ret = close(metadata_stream->out_fd);
391 if (ret < 0) {
392 PERROR("Kernel consumer snapshot metadata close out_fd");
393 /*
394 * Don't go on error here since the snapshot was successful at this
395 * point but somehow the close failed.
396 */
397 }
398 metadata_stream->out_fd = -1;
399 }
400 }
401
402 ret = 0;
403
404 cds_list_del(&metadata_stream->send_node);
405 consumer_stream_destroy(metadata_stream, NULL);
406 metadata_channel->metadata_stream = NULL;
407 error:
408 rcu_read_unlock();
409 return ret;
410 }
411
412 /*
413 * Receive command from session daemon and process it.
414 *
415 * Return 1 on success else a negative value or 0.
416 */
417 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
418 int sock, struct pollfd *consumer_sockpoll)
419 {
420 ssize_t ret;
421 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
422 struct lttcomm_consumer_msg msg;
423
424 health_code_update();
425
426 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
427 if (ret != sizeof(msg)) {
428 if (ret > 0) {
429 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
430 ret = -1;
431 }
432 return ret;
433 }
434
435 health_code_update();
436
437 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
438 /*
439 * Notify the session daemon that the command is completed.
440 *
441 * On transport layer error, the function call will print an error
442 * message so handling the returned code is a bit useless since we
443 * return an error code anyway.
444 */
445 (void) consumer_send_status_msg(sock, ret_code);
446 return -ENOENT;
447 }
448
449 health_code_update();
450
451 /* relayd needs RCU read-side protection */
452 rcu_read_lock();
453
454 switch (msg.cmd_type) {
455 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
456 {
457 /* Session daemon status message are handled in the following call. */
458 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
459 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
460 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
461 msg.u.relayd_sock.relayd_session_id);
462 goto end_nosignal;
463 }
464 case LTTNG_CONSUMER_ADD_CHANNEL:
465 {
466 struct lttng_consumer_channel *new_channel;
467 int ret_recv;
468
469 health_code_update();
470
471 /* First send a status message before receiving the fds. */
472 ret = consumer_send_status_msg(sock, ret_code);
473 if (ret < 0) {
474 /* Somehow, the session daemon is not responding anymore. */
475 goto error_fatal;
476 }
477
478 health_code_update();
479
480 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
481 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
482 msg.u.channel.session_id, msg.u.channel.pathname,
483 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
484 msg.u.channel.relayd_id, msg.u.channel.output,
485 msg.u.channel.tracefile_size,
486 msg.u.channel.tracefile_count, 0,
487 msg.u.channel.monitor,
488 msg.u.channel.live_timer_interval);
489 if (new_channel == NULL) {
490 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
491 goto end_nosignal;
492 }
493 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
494 switch (msg.u.channel.output) {
495 case LTTNG_EVENT_SPLICE:
496 new_channel->output = CONSUMER_CHANNEL_SPLICE;
497 break;
498 case LTTNG_EVENT_MMAP:
499 new_channel->output = CONSUMER_CHANNEL_MMAP;
500 break;
501 default:
502 ERR("Channel output unknown %d", msg.u.channel.output);
503 goto end_nosignal;
504 }
505
506 /* Translate and save channel type. */
507 switch (msg.u.channel.type) {
508 case CONSUMER_CHANNEL_TYPE_DATA:
509 case CONSUMER_CHANNEL_TYPE_METADATA:
510 new_channel->type = msg.u.channel.type;
511 break;
512 default:
513 assert(0);
514 goto end_nosignal;
515 };
516
517 health_code_update();
518
519 if (ctx->on_recv_channel != NULL) {
520 ret_recv = ctx->on_recv_channel(new_channel);
521 if (ret_recv == 0) {
522 ret = consumer_add_channel(new_channel, ctx);
523 } else if (ret_recv < 0) {
524 goto end_nosignal;
525 }
526 } else {
527 ret = consumer_add_channel(new_channel, ctx);
528 }
529 if (CONSUMER_CHANNEL_TYPE_DATA) {
530 consumer_timer_live_start(new_channel,
531 msg.u.channel.live_timer_interval);
532 }
533
534 health_code_update();
535
536 /* If we received an error in add_channel, we need to report it. */
537 if (ret < 0) {
538 ret = consumer_send_status_msg(sock, ret);
539 if (ret < 0) {
540 goto error_fatal;
541 }
542 goto end_nosignal;
543 }
544
545 goto end_nosignal;
546 }
547 case LTTNG_CONSUMER_ADD_STREAM:
548 {
549 int fd;
550 struct lttng_pipe *stream_pipe;
551 struct lttng_consumer_stream *new_stream;
552 struct lttng_consumer_channel *channel;
553 int alloc_ret = 0;
554
555 /*
556 * Get stream's channel reference. Needed when adding the stream to the
557 * global hash table.
558 */
559 channel = consumer_find_channel(msg.u.stream.channel_key);
560 if (!channel) {
561 /*
562 * We could not find the channel. Can happen if cpu hotplug
563 * happens while tearing down.
564 */
565 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
566 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
567 }
568
569 health_code_update();
570
571 /* First send a status message before receiving the fds. */
572 ret = consumer_send_status_msg(sock, ret_code);
573 if (ret < 0) {
574 /* Somehow, the session daemon is not responding anymore. */
575 goto error_fatal;
576 }
577
578 health_code_update();
579
580 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
581 /* Channel was not found. */
582 goto end_nosignal;
583 }
584
585 /* Blocking call */
586 health_poll_entry();
587 ret = lttng_consumer_poll_socket(consumer_sockpoll);
588 health_poll_exit();
589 if (ret < 0) {
590 rcu_read_unlock();
591 return -EINTR;
592 }
593
594 health_code_update();
595
596 /* Get stream file descriptor from socket */
597 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
598 if (ret != sizeof(fd)) {
599 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
600 rcu_read_unlock();
601 return ret;
602 }
603
604 health_code_update();
605
606 /*
607 * Send status code to session daemon only if the recv works. If the
608 * above recv() failed, the session daemon is notified through the
609 * error socket and the teardown is eventually done.
610 */
611 ret = consumer_send_status_msg(sock, ret_code);
612 if (ret < 0) {
613 /* Somehow, the session daemon is not responding anymore. */
614 goto end_nosignal;
615 }
616
617 health_code_update();
618
619 new_stream = consumer_allocate_stream(channel->key,
620 fd,
621 LTTNG_CONSUMER_ACTIVE_STREAM,
622 channel->name,
623 channel->uid,
624 channel->gid,
625 channel->relayd_id,
626 channel->session_id,
627 msg.u.stream.cpu,
628 &alloc_ret,
629 channel->type,
630 channel->monitor);
631 if (new_stream == NULL) {
632 switch (alloc_ret) {
633 case -ENOMEM:
634 case -EINVAL:
635 default:
636 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
637 break;
638 }
639 goto end_nosignal;
640 }
641
642 new_stream->chan = channel;
643 new_stream->wait_fd = fd;
644 switch (channel->output) {
645 case CONSUMER_CHANNEL_SPLICE:
646 new_stream->output = LTTNG_EVENT_SPLICE;
647 break;
648 case CONSUMER_CHANNEL_MMAP:
649 new_stream->output = LTTNG_EVENT_MMAP;
650 break;
651 default:
652 ERR("Stream output unknown %d", channel->output);
653 goto end_nosignal;
654 }
655
656 /*
657 * We've just assigned the channel to the stream so increment the
658 * refcount right now. We don't need to increment the refcount for
659 * streams in no monitor because we handle manually the cleanup of
660 * those. It is very important to make sure there is NO prior
661 * consumer_del_stream() calls or else the refcount will be unbalanced.
662 */
663 if (channel->monitor) {
664 uatomic_inc(&new_stream->chan->refcount);
665 }
666
667 /*
668 * The buffer flush is done on the session daemon side for the kernel
669 * so no need for the stream "hangup_flush_done" variable to be
670 * tracked. This is important for a kernel stream since we don't rely
671 * on the flush state of the stream to read data. It's not the case for
672 * user space tracing.
673 */
674 new_stream->hangup_flush_done = 0;
675
676 health_code_update();
677
678 if (ctx->on_recv_stream) {
679 ret = ctx->on_recv_stream(new_stream);
680 if (ret < 0) {
681 consumer_stream_free(new_stream);
682 goto end_nosignal;
683 }
684 }
685
686 health_code_update();
687
688 if (new_stream->metadata_flag) {
689 channel->metadata_stream = new_stream;
690 }
691
692 /* Do not monitor this stream. */
693 if (!channel->monitor) {
694 DBG("Kernel consumer add stream %s in no monitor mode with "
695 "relayd id %" PRIu64, new_stream->name,
696 new_stream->net_seq_idx);
697 cds_list_add(&new_stream->send_node, &channel->streams.head);
698 break;
699 }
700
701 /* Send stream to relayd if the stream has an ID. */
702 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
703 ret = consumer_send_relayd_stream(new_stream,
704 new_stream->chan->pathname);
705 if (ret < 0) {
706 consumer_stream_free(new_stream);
707 goto end_nosignal;
708 }
709 }
710
711 /* Get the right pipe where the stream will be sent. */
712 if (new_stream->metadata_flag) {
713 ret = consumer_add_metadata_stream(new_stream);
714 if (ret) {
715 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
716 new_stream->key);
717 consumer_stream_free(new_stream);
718 goto end_nosignal;
719 }
720 stream_pipe = ctx->consumer_metadata_pipe;
721 } else {
722 ret = consumer_add_data_stream(new_stream);
723 if (ret) {
724 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
725 new_stream->key);
726 consumer_stream_free(new_stream);
727 goto end_nosignal;
728 }
729 stream_pipe = ctx->consumer_data_pipe;
730 }
731
732 /* Vitible to other threads */
733 new_stream->globally_visible = 1;
734
735 health_code_update();
736
737 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
738 if (ret < 0) {
739 ERR("Consumer write %s stream to pipe %d",
740 new_stream->metadata_flag ? "metadata" : "data",
741 lttng_pipe_get_writefd(stream_pipe));
742 if (new_stream->metadata_flag) {
743 consumer_del_stream_for_metadata(new_stream);
744 } else {
745 consumer_del_stream_for_data(new_stream);
746 }
747 goto end_nosignal;
748 }
749
750 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
751 new_stream->name, fd, new_stream->relayd_stream_id);
752 break;
753 }
754 case LTTNG_CONSUMER_UPDATE_STREAM:
755 {
756 rcu_read_unlock();
757 return -ENOSYS;
758 }
759 case LTTNG_CONSUMER_DESTROY_RELAYD:
760 {
761 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
762 struct consumer_relayd_sock_pair *relayd;
763
764 DBG("Kernel consumer destroying relayd %" PRIu64, index);
765
766 /* Get relayd reference if exists. */
767 relayd = consumer_find_relayd(index);
768 if (relayd == NULL) {
769 DBG("Unable to find relayd %" PRIu64, index);
770 ret_code = LTTNG_ERR_NO_CONSUMER;
771 }
772
773 /*
774 * Each relayd socket pair has a refcount of stream attached to it
775 * which tells if the relayd is still active or not depending on the
776 * refcount value.
777 *
778 * This will set the destroy flag of the relayd object and destroy it
779 * if the refcount reaches zero when called.
780 *
781 * The destroy can happen either here or when a stream fd hangs up.
782 */
783 if (relayd) {
784 consumer_flag_relayd_for_destroy(relayd);
785 }
786
787 health_code_update();
788
789 ret = consumer_send_status_msg(sock, ret_code);
790 if (ret < 0) {
791 /* Somehow, the session daemon is not responding anymore. */
792 goto error_fatal;
793 }
794
795 goto end_nosignal;
796 }
797 case LTTNG_CONSUMER_DATA_PENDING:
798 {
799 int32_t ret;
800 uint64_t id = msg.u.data_pending.session_id;
801
802 DBG("Kernel consumer data pending command for id %" PRIu64, id);
803
804 ret = consumer_data_pending(id);
805
806 health_code_update();
807
808 /* Send back returned value to session daemon */
809 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
810 if (ret < 0) {
811 PERROR("send data pending ret code");
812 goto error_fatal;
813 }
814
815 /*
816 * No need to send back a status message since the data pending
817 * returned value is the response.
818 */
819 break;
820 }
821 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
822 {
823 if (msg.u.snapshot_channel.metadata == 1) {
824 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
825 msg.u.snapshot_channel.pathname,
826 msg.u.snapshot_channel.relayd_id, ctx);
827 if (ret < 0) {
828 ERR("Snapshot metadata failed");
829 ret_code = LTTNG_ERR_KERN_META_FAIL;
830 }
831 } else {
832 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
833 msg.u.snapshot_channel.pathname,
834 msg.u.snapshot_channel.relayd_id,
835 msg.u.snapshot_channel.max_stream_size,
836 ctx);
837 if (ret < 0) {
838 ERR("Snapshot channel failed");
839 ret_code = LTTNG_ERR_KERN_CHAN_FAIL;
840 }
841 }
842
843 health_code_update();
844
845 ret = consumer_send_status_msg(sock, ret_code);
846 if (ret < 0) {
847 /* Somehow, the session daemon is not responding anymore. */
848 goto end_nosignal;
849 }
850 break;
851 }
852 case LTTNG_CONSUMER_DESTROY_CHANNEL:
853 {
854 uint64_t key = msg.u.destroy_channel.key;
855 struct lttng_consumer_channel *channel;
856
857 channel = consumer_find_channel(key);
858 if (!channel) {
859 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
860 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
861 }
862
863 health_code_update();
864
865 ret = consumer_send_status_msg(sock, ret_code);
866 if (ret < 0) {
867 /* Somehow, the session daemon is not responding anymore. */
868 goto end_nosignal;
869 }
870
871 health_code_update();
872
873 /*
874 * This command should ONLY be issued for channel with streams set in
875 * no monitor mode.
876 */
877 assert(!channel->monitor);
878
879 /*
880 * The refcount should ALWAYS be 0 in the case of a channel in no
881 * monitor mode.
882 */
883 assert(!uatomic_sub_return(&channel->refcount, 1));
884
885 consumer_del_channel(channel);
886
887 goto end_nosignal;
888 }
889 default:
890 goto end_nosignal;
891 }
892
893 end_nosignal:
894 rcu_read_unlock();
895
896 /*
897 * Return 1 to indicate success since the 0 value can be a socket
898 * shutdown during the recv() or send() call.
899 */
900 health_code_update();
901 return 1;
902
903 error_fatal:
904 rcu_read_unlock();
905 /* This will issue a consumer stop. */
906 return -1;
907 }
908
909 /*
910 * Populate index values of a kernel stream. Values are set in big endian order.
911 *
912 * Return 0 on success or else a negative value.
913 */
914 static int get_index_values(struct ctf_packet_index *index, int infd)
915 {
916 int ret;
917
918 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
919 if (ret < 0) {
920 PERROR("kernctl_get_timestamp_begin");
921 goto error;
922 }
923 index->timestamp_begin = htobe64(index->timestamp_begin);
924
925 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
926 if (ret < 0) {
927 PERROR("kernctl_get_timestamp_end");
928 goto error;
929 }
930 index->timestamp_end = htobe64(index->timestamp_end);
931
932 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
933 if (ret < 0) {
934 PERROR("kernctl_get_events_discarded");
935 goto error;
936 }
937 index->events_discarded = htobe64(index->events_discarded);
938
939 ret = kernctl_get_content_size(infd, &index->content_size);
940 if (ret < 0) {
941 PERROR("kernctl_get_content_size");
942 goto error;
943 }
944 index->content_size = htobe64(index->content_size);
945
946 ret = kernctl_get_packet_size(infd, &index->packet_size);
947 if (ret < 0) {
948 PERROR("kernctl_get_packet_size");
949 goto error;
950 }
951 index->packet_size = htobe64(index->packet_size);
952
953 ret = kernctl_get_stream_id(infd, &index->stream_id);
954 if (ret < 0) {
955 PERROR("kernctl_get_stream_id");
956 goto error;
957 }
958 index->stream_id = htobe64(index->stream_id);
959
960 error:
961 return ret;
962 }
963 /*
964 * Sync metadata meaning request them to the session daemon and snapshot to the
965 * metadata thread can consumer them.
966 *
967 * Metadata stream lock MUST be acquired.
968 *
969 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
970 * is empty or a negative value on error.
971 */
972 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
973 {
974 int ret;
975
976 assert(metadata);
977
978 ret = kernctl_buffer_flush(metadata->wait_fd);
979 if (ret < 0) {
980 ERR("Failed to flush kernel stream");
981 goto end;
982 }
983
984 ret = kernctl_snapshot(metadata->wait_fd);
985 if (ret < 0) {
986 if (errno != EAGAIN) {
987 ERR("Sync metadata, taking kernel snapshot failed.");
988 goto end;
989 }
990 DBG("Sync metadata, no new kernel metadata");
991 /* No new metadata, exit. */
992 ret = ENODATA;
993 goto end;
994 }
995
996 end:
997 return ret;
998 }
999
1000 /*
1001 * Consume data on a file descriptor and write it on a trace file.
1002 */
1003 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1004 struct lttng_consumer_local_data *ctx)
1005 {
1006 unsigned long len, subbuf_size, padding;
1007 int err, write_index = 1;
1008 ssize_t ret = 0;
1009 int infd = stream->wait_fd;
1010 struct ctf_packet_index index;
1011
1012 DBG("In read_subbuffer (infd : %d)", infd);
1013
1014 /* Get the next subbuffer */
1015 err = kernctl_get_next_subbuf(infd);
1016 if (err != 0) {
1017 /*
1018 * This is a debug message even for single-threaded consumer,
1019 * because poll() have more relaxed criterions than get subbuf,
1020 * so get_subbuf may fail for short race windows where poll()
1021 * would issue wakeups.
1022 */
1023 DBG("Reserving sub buffer failed (everything is normal, "
1024 "it is due to concurrency)");
1025 ret = -errno;
1026 goto end;
1027 }
1028
1029 /* Get the full subbuffer size including padding */
1030 err = kernctl_get_padded_subbuf_size(infd, &len);
1031 if (err != 0) {
1032 perror("Getting sub-buffer len failed.");
1033 ret = -errno;
1034 goto end;
1035 }
1036
1037 if (!stream->metadata_flag) {
1038 ret = get_index_values(&index, infd);
1039 if (ret < 0) {
1040 goto end;
1041 }
1042 } else {
1043 write_index = 0;
1044 }
1045
1046 switch (stream->chan->output) {
1047 case CONSUMER_CHANNEL_SPLICE:
1048 /*
1049 * XXX: The lttng-modules splice "actor" does not handle copying
1050 * partial pages hence only using the subbuffer size without the
1051 * padding makes the splice fail.
1052 */
1053 subbuf_size = len;
1054 padding = 0;
1055
1056 /* splice the subbuffer to the tracefile */
1057 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
1058 padding, &index);
1059 /*
1060 * XXX: Splice does not support network streaming so the return value
1061 * is simply checked against subbuf_size and not like the mmap() op.
1062 */
1063 if (ret != subbuf_size) {
1064 /*
1065 * display the error but continue processing to try
1066 * to release the subbuffer
1067 */
1068 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1069 ret, subbuf_size);
1070 write_index = 0;
1071 }
1072 break;
1073 case CONSUMER_CHANNEL_MMAP:
1074 /* Get subbuffer size without padding */
1075 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1076 if (err != 0) {
1077 perror("Getting sub-buffer len failed.");
1078 ret = -errno;
1079 goto end;
1080 }
1081
1082 /* Make sure the tracer is not gone mad on us! */
1083 assert(len >= subbuf_size);
1084
1085 padding = len - subbuf_size;
1086
1087 /* write the subbuffer to the tracefile */
1088 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
1089 padding, &index);
1090 /*
1091 * The mmap operation should write subbuf_size amount of data when
1092 * network streaming or the full padding (len) size when we are _not_
1093 * streaming.
1094 */
1095 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1096 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1097 /*
1098 * Display the error but continue processing to try to release the
1099 * subbuffer
1100 */
1101 ERR("Error writing to tracefile "
1102 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1103 ret, len, subbuf_size);
1104 write_index = 0;
1105 }
1106 break;
1107 default:
1108 ERR("Unknown output method");
1109 ret = -EPERM;
1110 }
1111
1112 err = kernctl_put_next_subbuf(infd);
1113 if (err != 0) {
1114 if (errno == EFAULT) {
1115 perror("Error in unreserving sub buffer\n");
1116 } else if (errno == EIO) {
1117 /* Should never happen with newer LTTng versions */
1118 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1119 }
1120 ret = -errno;
1121 goto end;
1122 }
1123
1124 /* Write index if needed. */
1125 if (!write_index) {
1126 goto end;
1127 }
1128
1129 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1130 /*
1131 * In live, block until all the metadata is sent.
1132 */
1133 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1134 if (err < 0) {
1135 goto end;
1136 }
1137 }
1138
1139 err = consumer_stream_write_index(stream, &index);
1140 if (err < 0) {
1141 goto end;
1142 }
1143
1144 end:
1145 return ret;
1146 }
1147
1148 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1149 {
1150 int ret;
1151
1152 assert(stream);
1153
1154 /*
1155 * Don't create anything if this is set for streaming or should not be
1156 * monitored.
1157 */
1158 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
1159 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1160 stream->chan->tracefile_size, stream->tracefile_count_current,
1161 stream->uid, stream->gid, NULL);
1162 if (ret < 0) {
1163 goto error;
1164 }
1165 stream->out_fd = ret;
1166 stream->tracefile_size_current = 0;
1167
1168 if (!stream->metadata_flag) {
1169 ret = index_create_file(stream->chan->pathname,
1170 stream->name, stream->uid, stream->gid,
1171 stream->chan->tracefile_size,
1172 stream->tracefile_count_current);
1173 if (ret < 0) {
1174 goto error;
1175 }
1176 stream->index_fd = ret;
1177 }
1178 }
1179
1180 if (stream->output == LTTNG_EVENT_MMAP) {
1181 /* get the len of the mmap region */
1182 unsigned long mmap_len;
1183
1184 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1185 if (ret != 0) {
1186 PERROR("kernctl_get_mmap_len");
1187 ret = -errno;
1188 goto error_close_fd;
1189 }
1190 stream->mmap_len = (size_t) mmap_len;
1191
1192 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1193 MAP_PRIVATE, stream->wait_fd, 0);
1194 if (stream->mmap_base == MAP_FAILED) {
1195 PERROR("Error mmaping");
1196 ret = -1;
1197 goto error_close_fd;
1198 }
1199 }
1200
1201 /* we return 0 to let the library handle the FD internally */
1202 return 0;
1203
1204 error_close_fd:
1205 if (stream->out_fd >= 0) {
1206 int err;
1207
1208 err = close(stream->out_fd);
1209 assert(!err);
1210 stream->out_fd = -1;
1211 }
1212 error:
1213 return ret;
1214 }
1215
1216 /*
1217 * Check if data is still being extracted from the buffers for a specific
1218 * stream. Consumer data lock MUST be acquired before calling this function
1219 * and the stream lock.
1220 *
1221 * Return 1 if the traced data are still getting read else 0 meaning that the
1222 * data is available for trace viewer reading.
1223 */
1224 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1225 {
1226 int ret;
1227
1228 assert(stream);
1229
1230 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1231 ret = 0;
1232 goto end;
1233 }
1234
1235 ret = kernctl_get_next_subbuf(stream->wait_fd);
1236 if (ret == 0) {
1237 /* There is still data so let's put back this subbuffer. */
1238 ret = kernctl_put_subbuf(stream->wait_fd);
1239 assert(ret == 0);
1240 ret = 1; /* Data is pending */
1241 goto end;
1242 }
1243
1244 /* Data is NOT pending and ready to be read. */
1245 ret = 0;
1246
1247 end:
1248 return ret;
1249 }
This page took 0.092276 seconds and 4 git commands to generate.