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