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