consumerd: Implement clear stream/session commands
[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 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <inttypes.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/compat/endian.h>
40 #include <common/pipe.h>
41 #include <common/relayd/relayd.h>
42 #include <common/utils.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer/consumer-timer.h>
46
47 #include "kernel-consumer.h"
48
49 extern struct lttng_consumer_global_data consumer_data;
50 extern int consumer_poll_timeout;
51
52 /*
53 * Take a snapshot for a specific fd
54 *
55 * Returns 0 on success, < 0 on error
56 */
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
58 {
59 int ret = 0;
60 int infd = stream->wait_fd;
61
62 ret = kernctl_snapshot(infd);
63 /*
64 * -EAGAIN is not an error, it just means that there is no data to
65 * be read.
66 */
67 if (ret != 0 && ret != -EAGAIN) {
68 PERROR("Getting sub-buffer snapshot.");
69 }
70
71 return ret;
72 }
73
74 /*
75 * Sample consumed and produced positions for a specific fd.
76 *
77 * Returns 0 on success, < 0 on error.
78 */
79 int lttng_kconsumer_sample_snapshot_positions(
80 struct lttng_consumer_stream *stream)
81 {
82 assert(stream);
83
84 return kernctl_snapshot_sample_positions(stream->wait_fd);
85 }
86
87 /*
88 * Get the produced position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92 int lttng_kconsumer_get_produced_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_produced(infd, pos);
99 if (ret != 0) {
100 PERROR("kernctl_snapshot_get_produced");
101 }
102
103 return ret;
104 }
105
106 /*
107 * Get the consumerd position
108 *
109 * Returns 0 on success, < 0 on error
110 */
111 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
112 unsigned long *pos)
113 {
114 int ret;
115 int infd = stream->wait_fd;
116
117 ret = kernctl_snapshot_get_consumed(infd, pos);
118 if (ret != 0) {
119 PERROR("kernctl_snapshot_get_consumed");
120 }
121
122 return ret;
123 }
124
125 /*
126 * Take a snapshot of all the stream of a channel
127 *
128 * Returns 0 on success, < 0 on error
129 */
130 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
131 uint64_t relayd_id, uint64_t nb_packets_per_stream,
132 struct lttng_consumer_local_data *ctx)
133 {
134 int ret;
135 struct lttng_consumer_channel *channel;
136 struct lttng_consumer_stream *stream;
137
138 DBG("Kernel consumer snapshot channel %" PRIu64, key);
139
140 rcu_read_lock();
141
142 channel = consumer_find_channel(key);
143 if (!channel) {
144 ERR("No channel found for key %" PRIu64, key);
145 ret = -1;
146 goto end;
147 }
148
149 /* Splice is not supported yet for channel snapshot. */
150 if (channel->output != CONSUMER_CHANNEL_MMAP) {
151 ERR("Unsupported output %d", channel->output);
152 ret = -1;
153 goto end;
154 }
155
156 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
157 unsigned long consumed_pos, produced_pos;
158
159 health_code_update();
160
161 /*
162 * Lock stream because we are about to change its state.
163 */
164 pthread_mutex_lock(&stream->lock);
165
166 /*
167 * Assign the received relayd ID so we can use it for streaming. The streams
168 * are not visible to anyone so this is OK to change it.
169 */
170 stream->net_seq_idx = relayd_id;
171 channel->relayd_id = relayd_id;
172 if (relayd_id != (uint64_t) -1ULL) {
173 ret = consumer_send_relayd_stream(stream, path);
174 if (ret < 0) {
175 ERR("sending stream to relayd");
176 goto end_unlock;
177 }
178 } else {
179 ret = utils_create_stream_file(path, stream->name,
180 stream->chan->tracefile_size,
181 stream->tracefile_count_current,
182 stream->uid, stream->gid, NULL);
183 if (ret < 0) {
184 ERR("utils_create_stream_file");
185 goto end_unlock;
186 }
187
188 stream->out_fd = ret;
189 stream->tracefile_size_current = 0;
190
191 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
192 path, stream->name, stream->key);
193 }
194
195 ret = kernctl_buffer_flush_empty(stream->wait_fd);
196 if (ret < 0) {
197 /*
198 * Doing a buffer flush which does not take into
199 * account empty packets. This is not perfect
200 * for stream intersection, but required as a
201 * fall-back when "flush_empty" is not
202 * implemented by lttng-modules.
203 */
204 ret = kernctl_buffer_flush(stream->wait_fd);
205 if (ret < 0) {
206 ERR("Failed to flush kernel stream");
207 goto end_unlock;
208 }
209 goto end_unlock;
210 }
211
212 ret = lttng_kconsumer_take_snapshot(stream);
213 if (ret < 0) {
214 ERR("Taking kernel snapshot");
215 goto end_unlock;
216 }
217
218 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
219 if (ret < 0) {
220 ERR("Produced kernel snapshot position");
221 goto end_unlock;
222 }
223
224 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
225 if (ret < 0) {
226 ERR("Consumerd kernel snapshot position");
227 goto end_unlock;
228 }
229
230 if (stream->max_sb_size == 0) {
231 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
232 &stream->max_sb_size);
233 if (ret < 0) {
234 ERR("Getting kernel max_sb_size");
235 goto end_unlock;
236 }
237 }
238
239 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
240 produced_pos, nb_packets_per_stream,
241 stream->max_sb_size);
242
243 while (consumed_pos < produced_pos) {
244 ssize_t read_len;
245 unsigned long len, padded_len;
246
247 health_code_update();
248
249 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
250
251 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
252 if (ret < 0) {
253 if (ret != -EAGAIN) {
254 PERROR("kernctl_get_subbuf snapshot");
255 goto end_unlock;
256 }
257 DBG("Kernel consumer get subbuf failed. Skipping it.");
258 consumed_pos += stream->max_sb_size;
259 stream->chan->lost_packets++;
260 continue;
261 }
262
263 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
264 if (ret < 0) {
265 ERR("Snapshot kernctl_get_subbuf_size");
266 goto error_put_subbuf;
267 }
268
269 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
270 if (ret < 0) {
271 ERR("Snapshot kernctl_get_padded_subbuf_size");
272 goto error_put_subbuf;
273 }
274
275 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
276 padded_len - len, NULL);
277 /*
278 * We write the padded len in local tracefiles but the data len
279 * when using a relay. Display the error but continue processing
280 * to try to release the subbuffer.
281 */
282 if (relayd_id != (uint64_t) -1ULL) {
283 if (read_len != len) {
284 ERR("Error sending to the relay (ret: %zd != len: %lu)",
285 read_len, len);
286 }
287 } else {
288 if (read_len != padded_len) {
289 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
290 read_len, padded_len);
291 }
292 }
293
294 ret = kernctl_put_subbuf(stream->wait_fd);
295 if (ret < 0) {
296 ERR("Snapshot kernctl_put_subbuf");
297 goto end_unlock;
298 }
299 consumed_pos += stream->max_sb_size;
300 }
301
302 if (relayd_id == (uint64_t) -1ULL) {
303 if (stream->out_fd >= 0) {
304 ret = close(stream->out_fd);
305 if (ret < 0) {
306 PERROR("Kernel consumer snapshot close out_fd");
307 goto end_unlock;
308 }
309 stream->out_fd = -1;
310 }
311 } else {
312 close_relayd_stream(stream);
313 stream->net_seq_idx = (uint64_t) -1ULL;
314 }
315 pthread_mutex_unlock(&stream->lock);
316 }
317
318 /* All good! */
319 ret = 0;
320 goto end;
321
322 error_put_subbuf:
323 ret = kernctl_put_subbuf(stream->wait_fd);
324 if (ret < 0) {
325 ERR("Snapshot kernctl_put_subbuf error path");
326 }
327 end_unlock:
328 pthread_mutex_unlock(&stream->lock);
329 end:
330 rcu_read_unlock();
331 return ret;
332 }
333
334 /*
335 * Read the whole metadata available for a snapshot.
336 *
337 * Returns 0 on success, < 0 on error
338 */
339 static int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
340 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
341 {
342 int ret, use_relayd = 0;
343 ssize_t ret_read;
344 struct lttng_consumer_channel *metadata_channel;
345 struct lttng_consumer_stream *metadata_stream;
346
347 assert(ctx);
348
349 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
350 key, path);
351
352 rcu_read_lock();
353
354 metadata_channel = consumer_find_channel(key);
355 if (!metadata_channel) {
356 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
357 ret = -1;
358 goto error_no_channel;
359 }
360
361 metadata_stream = metadata_channel->metadata_stream;
362 assert(metadata_stream);
363 pthread_mutex_lock(&metadata_stream->lock);
364
365 /* Flag once that we have a valid relayd for the stream. */
366 if (relayd_id != (uint64_t) -1ULL) {
367 use_relayd = 1;
368 }
369
370 if (use_relayd) {
371 ret = consumer_send_relayd_stream(metadata_stream, path);
372 if (ret < 0) {
373 goto error_snapshot;
374 }
375 } else {
376 ret = utils_create_stream_file(path, metadata_stream->name,
377 metadata_stream->chan->tracefile_size,
378 metadata_stream->tracefile_count_current,
379 metadata_stream->uid, metadata_stream->gid, NULL);
380 if (ret < 0) {
381 goto error_snapshot;
382 }
383 metadata_stream->out_fd = ret;
384 }
385
386 do {
387 health_code_update();
388
389 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
390 if (ret_read < 0) {
391 if (ret_read != -EAGAIN) {
392 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
393 ret_read);
394 ret = ret_read;
395 goto error_snapshot;
396 }
397 /* ret_read is negative at this point so we will exit the loop. */
398 continue;
399 }
400 } while (ret_read >= 0);
401
402 if (use_relayd) {
403 close_relayd_stream(metadata_stream);
404 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
405 } else {
406 if (metadata_stream->out_fd >= 0) {
407 ret = close(metadata_stream->out_fd);
408 if (ret < 0) {
409 PERROR("Kernel consumer snapshot metadata close out_fd");
410 /*
411 * Don't go on error here since the snapshot was successful at this
412 * point but somehow the close failed.
413 */
414 }
415 metadata_stream->out_fd = -1;
416 }
417 }
418
419 ret = 0;
420 error_snapshot:
421 pthread_mutex_unlock(&metadata_stream->lock);
422 cds_list_del(&metadata_stream->send_node);
423 consumer_stream_destroy(metadata_stream, NULL);
424 metadata_channel->metadata_stream = NULL;
425 error_no_channel:
426 rcu_read_unlock();
427 return ret;
428 }
429
430 /*
431 * Receive command from session daemon and process it.
432 *
433 * Return 1 on success else a negative value or 0.
434 */
435 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
436 int sock, struct pollfd *consumer_sockpoll)
437 {
438 ssize_t ret;
439 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
440 struct lttcomm_consumer_msg msg;
441
442 health_code_update();
443
444 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
445 if (ret != sizeof(msg)) {
446 if (ret > 0) {
447 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
448 ret = -1;
449 }
450 return ret;
451 }
452
453 health_code_update();
454
455 /* Deprecated command */
456 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
457
458 health_code_update();
459
460 /* relayd needs RCU read-side protection */
461 rcu_read_lock();
462
463 switch (msg.cmd_type) {
464 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
465 {
466 /* Session daemon status message are handled in the following call. */
467 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
468 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
469 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
470 msg.u.relayd_sock.relayd_session_id);
471 goto end_nosignal;
472 }
473 case LTTNG_CONSUMER_ADD_CHANNEL:
474 {
475 struct lttng_consumer_channel *new_channel;
476 int ret_recv;
477
478 health_code_update();
479
480 /* First send a status message before receiving the fds. */
481 ret = consumer_send_status_msg(sock, ret_code);
482 if (ret < 0) {
483 /* Somehow, the session daemon is not responding anymore. */
484 goto error_fatal;
485 }
486
487 health_code_update();
488
489 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
490 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
491 msg.u.channel.session_id, msg.u.channel.pathname,
492 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
493 msg.u.channel.relayd_id, msg.u.channel.output,
494 msg.u.channel.tracefile_size,
495 msg.u.channel.tracefile_count, 0,
496 msg.u.channel.monitor,
497 msg.u.channel.live_timer_interval,
498 NULL, NULL);
499 if (new_channel == NULL) {
500 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
501 goto end_nosignal;
502 }
503 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
504 switch (msg.u.channel.output) {
505 case LTTNG_EVENT_SPLICE:
506 new_channel->output = CONSUMER_CHANNEL_SPLICE;
507 break;
508 case LTTNG_EVENT_MMAP:
509 new_channel->output = CONSUMER_CHANNEL_MMAP;
510 break;
511 default:
512 ERR("Channel output unknown %d", msg.u.channel.output);
513 goto end_nosignal;
514 }
515
516 /* Translate and save channel type. */
517 switch (msg.u.channel.type) {
518 case CONSUMER_CHANNEL_TYPE_DATA:
519 case CONSUMER_CHANNEL_TYPE_METADATA:
520 new_channel->type = msg.u.channel.type;
521 break;
522 default:
523 assert(0);
524 goto end_nosignal;
525 };
526
527 health_code_update();
528
529 if (ctx->on_recv_channel != NULL) {
530 ret_recv = ctx->on_recv_channel(new_channel);
531 if (ret_recv == 0) {
532 ret = consumer_add_channel(new_channel, ctx);
533 } else if (ret_recv < 0) {
534 goto end_nosignal;
535 }
536 } else {
537 ret = consumer_add_channel(new_channel, ctx);
538 }
539 if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) {
540 int monitor_start_ret;
541
542 DBG("Consumer starting monitor timer");
543 consumer_timer_live_start(new_channel,
544 msg.u.channel.live_timer_interval);
545 monitor_start_ret = consumer_timer_monitor_start(
546 new_channel,
547 msg.u.channel.monitor_timer_interval);
548 if (monitor_start_ret < 0) {
549 ERR("Starting channel monitoring timer failed");
550 goto end_nosignal;
551 }
552
553 }
554
555 health_code_update();
556
557 /* If we received an error in add_channel, we need to report it. */
558 if (ret < 0) {
559 ret = consumer_send_status_msg(sock, ret);
560 if (ret < 0) {
561 goto error_fatal;
562 }
563 goto end_nosignal;
564 }
565
566 goto end_nosignal;
567 }
568 case LTTNG_CONSUMER_ADD_STREAM:
569 {
570 int fd;
571 struct lttng_pipe *stream_pipe;
572 struct lttng_consumer_stream *new_stream;
573 struct lttng_consumer_channel *channel;
574 int alloc_ret = 0;
575
576 /*
577 * Get stream's channel reference. Needed when adding the stream to the
578 * global hash table.
579 */
580 channel = consumer_find_channel(msg.u.stream.channel_key);
581 if (!channel) {
582 /*
583 * We could not find the channel. Can happen if cpu hotplug
584 * happens while tearing down.
585 */
586 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
587 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
588 }
589
590 health_code_update();
591
592 /* First send a status message before receiving the fds. */
593 ret = consumer_send_status_msg(sock, ret_code);
594 if (ret < 0) {
595 /* Somehow, the session daemon is not responding anymore. */
596 goto error_fatal;
597 }
598
599 health_code_update();
600
601 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
602 /* Channel was not found. */
603 goto end_nosignal;
604 }
605
606 /* Blocking call */
607 health_poll_entry();
608 ret = lttng_consumer_poll_socket(consumer_sockpoll);
609 health_poll_exit();
610 if (ret) {
611 goto error_fatal;
612 }
613
614 health_code_update();
615
616 /* Get stream file descriptor from socket */
617 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
618 if (ret != sizeof(fd)) {
619 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
620 rcu_read_unlock();
621 return ret;
622 }
623
624 health_code_update();
625
626 /*
627 * Send status code to session daemon only if the recv works. If the
628 * above recv() failed, the session daemon is notified through the
629 * error socket and the teardown is eventually done.
630 */
631 ret = consumer_send_status_msg(sock, ret_code);
632 if (ret < 0) {
633 /* Somehow, the session daemon is not responding anymore. */
634 goto end_nosignal;
635 }
636
637 health_code_update();
638
639 new_stream = consumer_allocate_stream(channel->key,
640 fd,
641 LTTNG_CONSUMER_ACTIVE_STREAM,
642 channel->name,
643 channel->uid,
644 channel->gid,
645 channel->relayd_id,
646 channel->session_id,
647 msg.u.stream.cpu,
648 &alloc_ret,
649 channel->type,
650 channel->monitor);
651 if (new_stream == NULL) {
652 switch (alloc_ret) {
653 case -ENOMEM:
654 case -EINVAL:
655 default:
656 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
657 break;
658 }
659 goto end_nosignal;
660 }
661
662 new_stream->chan = channel;
663 new_stream->wait_fd = fd;
664 switch (channel->output) {
665 case CONSUMER_CHANNEL_SPLICE:
666 new_stream->output = LTTNG_EVENT_SPLICE;
667 ret = utils_create_pipe(new_stream->splice_pipe);
668 if (ret < 0) {
669 goto end_nosignal;
670 }
671 break;
672 case CONSUMER_CHANNEL_MMAP:
673 new_stream->output = LTTNG_EVENT_MMAP;
674 break;
675 default:
676 ERR("Stream output unknown %d", channel->output);
677 goto end_nosignal;
678 }
679
680 /*
681 * We've just assigned the channel to the stream so increment the
682 * refcount right now. We don't need to increment the refcount for
683 * streams in no monitor because we handle manually the cleanup of
684 * those. It is very important to make sure there is NO prior
685 * consumer_del_stream() calls or else the refcount will be unbalanced.
686 */
687 if (channel->monitor) {
688 uatomic_inc(&new_stream->chan->refcount);
689 }
690
691 /*
692 * The buffer flush is done on the session daemon side for the kernel
693 * so no need for the stream "hangup_flush_done" variable to be
694 * tracked. This is important for a kernel stream since we don't rely
695 * on the flush state of the stream to read data. It's not the case for
696 * user space tracing.
697 */
698 new_stream->hangup_flush_done = 0;
699
700 health_code_update();
701
702 if (ctx->on_recv_stream) {
703 ret = ctx->on_recv_stream(new_stream);
704 if (ret < 0) {
705 consumer_stream_free(new_stream);
706 goto end_nosignal;
707 }
708 }
709
710 health_code_update();
711
712 if (new_stream->metadata_flag) {
713 channel->metadata_stream = new_stream;
714 }
715
716 /* Do not monitor this stream. */
717 if (!channel->monitor) {
718 DBG("Kernel consumer add stream %s in no monitor mode with "
719 "relayd id %" PRIu64, new_stream->name,
720 new_stream->net_seq_idx);
721 cds_list_add(&new_stream->send_node, &channel->streams.head);
722 break;
723 }
724
725 /* Send stream to relayd if the stream has an ID. */
726 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
727 ret = consumer_send_relayd_stream(new_stream,
728 new_stream->chan->pathname);
729 if (ret < 0) {
730 consumer_stream_free(new_stream);
731 goto end_nosignal;
732 }
733
734 /*
735 * If adding an extra stream to an already
736 * existing channel (e.g. cpu hotplug), we need
737 * to send the "streams_sent" command to relayd.
738 */
739 if (channel->streams_sent_to_relayd) {
740 ret = consumer_send_relayd_streams_sent(
741 new_stream->net_seq_idx);
742 if (ret < 0) {
743 goto end_nosignal;
744 }
745 }
746 }
747
748 /* Get the right pipe where the stream will be sent. */
749 if (new_stream->metadata_flag) {
750 ret = consumer_add_metadata_stream(new_stream);
751 if (ret) {
752 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
753 new_stream->key);
754 consumer_stream_free(new_stream);
755 goto end_nosignal;
756 }
757 stream_pipe = ctx->consumer_metadata_pipe;
758 } else {
759 ret = consumer_add_data_stream(new_stream);
760 if (ret) {
761 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
762 new_stream->key);
763 consumer_stream_free(new_stream);
764 goto end_nosignal;
765 }
766 stream_pipe = ctx->consumer_data_pipe;
767 }
768
769 /* Vitible to other threads */
770 new_stream->globally_visible = 1;
771
772 health_code_update();
773
774 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
775 if (ret < 0) {
776 ERR("Consumer write %s stream to pipe %d",
777 new_stream->metadata_flag ? "metadata" : "data",
778 lttng_pipe_get_writefd(stream_pipe));
779 if (new_stream->metadata_flag) {
780 consumer_del_stream_for_metadata(new_stream);
781 } else {
782 consumer_del_stream_for_data(new_stream);
783 }
784 goto end_nosignal;
785 }
786
787 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
788 new_stream->name, fd, new_stream->relayd_stream_id);
789 break;
790 }
791 case LTTNG_CONSUMER_STREAMS_SENT:
792 {
793 struct lttng_consumer_channel *channel;
794
795 /*
796 * Get stream's channel reference. Needed when adding the stream to the
797 * global hash table.
798 */
799 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
800 if (!channel) {
801 /*
802 * We could not find the channel. Can happen if cpu hotplug
803 * happens while tearing down.
804 */
805 ERR("Unable to find channel key %" PRIu64,
806 msg.u.sent_streams.channel_key);
807 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
808 }
809
810 health_code_update();
811
812 /*
813 * Send status code to session daemon.
814 */
815 ret = consumer_send_status_msg(sock, ret_code);
816 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
817 /* Somehow, the session daemon is not responding anymore. */
818 goto end_nosignal;
819 }
820
821 health_code_update();
822
823 /*
824 * We should not send this message if we don't monitor the
825 * streams in this channel.
826 */
827 if (!channel->monitor) {
828 break;
829 }
830
831 health_code_update();
832 /* Send stream to relayd if the stream has an ID. */
833 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
834 ret = consumer_send_relayd_streams_sent(
835 msg.u.sent_streams.net_seq_idx);
836 if (ret < 0) {
837 goto end_nosignal;
838 }
839 channel->streams_sent_to_relayd = true;
840 }
841 break;
842 }
843 case LTTNG_CONSUMER_UPDATE_STREAM:
844 {
845 rcu_read_unlock();
846 return -ENOSYS;
847 }
848 case LTTNG_CONSUMER_DESTROY_RELAYD:
849 {
850 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
851 struct consumer_relayd_sock_pair *relayd;
852
853 DBG("Kernel consumer destroying relayd %" PRIu64, index);
854
855 /* Get relayd reference if exists. */
856 relayd = consumer_find_relayd(index);
857 if (relayd == NULL) {
858 DBG("Unable to find relayd %" PRIu64, index);
859 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
860 }
861
862 /*
863 * Each relayd socket pair has a refcount of stream attached to it
864 * which tells if the relayd is still active or not depending on the
865 * refcount value.
866 *
867 * This will set the destroy flag of the relayd object and destroy it
868 * if the refcount reaches zero when called.
869 *
870 * The destroy can happen either here or when a stream fd hangs up.
871 */
872 if (relayd) {
873 consumer_flag_relayd_for_destroy(relayd);
874 }
875
876 health_code_update();
877
878 ret = consumer_send_status_msg(sock, ret_code);
879 if (ret < 0) {
880 /* Somehow, the session daemon is not responding anymore. */
881 goto error_fatal;
882 }
883
884 goto end_nosignal;
885 }
886 case LTTNG_CONSUMER_DATA_PENDING:
887 {
888 int32_t ret;
889 uint64_t id = msg.u.data_pending.session_id;
890
891 DBG("Kernel consumer data pending command for id %" PRIu64, id);
892
893 ret = consumer_data_pending(id);
894
895 health_code_update();
896
897 /* Send back returned value to session daemon */
898 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
899 if (ret < 0) {
900 PERROR("send data pending ret code");
901 goto error_fatal;
902 }
903
904 /*
905 * No need to send back a status message since the data pending
906 * returned value is the response.
907 */
908 break;
909 }
910 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
911 {
912 if (msg.u.snapshot_channel.metadata == 1) {
913 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
914 msg.u.snapshot_channel.pathname,
915 msg.u.snapshot_channel.relayd_id, ctx);
916 if (ret < 0) {
917 ERR("Snapshot metadata failed");
918 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
919 }
920 } else {
921 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
922 msg.u.snapshot_channel.pathname,
923 msg.u.snapshot_channel.relayd_id,
924 msg.u.snapshot_channel.nb_packets_per_stream,
925 ctx);
926 if (ret < 0) {
927 ERR("Snapshot channel failed");
928 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
929 }
930 }
931
932 health_code_update();
933
934 ret = consumer_send_status_msg(sock, ret_code);
935 if (ret < 0) {
936 /* Somehow, the session daemon is not responding anymore. */
937 goto end_nosignal;
938 }
939 break;
940 }
941 case LTTNG_CONSUMER_DESTROY_CHANNEL:
942 {
943 uint64_t key = msg.u.destroy_channel.key;
944 struct lttng_consumer_channel *channel;
945
946 channel = consumer_find_channel(key);
947 if (!channel) {
948 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
949 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
950 }
951
952 health_code_update();
953
954 ret = consumer_send_status_msg(sock, ret_code);
955 if (ret < 0) {
956 /* Somehow, the session daemon is not responding anymore. */
957 goto end_nosignal;
958 }
959
960 health_code_update();
961
962 /* Stop right now if no channel was found. */
963 if (!channel) {
964 goto end_nosignal;
965 }
966
967 /*
968 * This command should ONLY be issued for channel with streams set in
969 * no monitor mode.
970 */
971 assert(!channel->monitor);
972
973 /*
974 * The refcount should ALWAYS be 0 in the case of a channel in no
975 * monitor mode.
976 */
977 assert(!uatomic_sub_return(&channel->refcount, 1));
978
979 consumer_del_channel(channel);
980
981 goto end_nosignal;
982 }
983 case LTTNG_CONSUMER_DISCARDED_EVENTS:
984 {
985 ssize_t ret;
986 uint64_t count;
987 struct lttng_consumer_channel *channel;
988 uint64_t id = msg.u.discarded_events.session_id;
989 uint64_t key = msg.u.discarded_events.channel_key;
990
991 DBG("Kernel consumer discarded events command for session id %"
992 PRIu64 ", channel key %" PRIu64, id, key);
993
994 channel = consumer_find_channel(key);
995 if (!channel) {
996 ERR("Kernel consumer discarded events channel %"
997 PRIu64 " not found", key);
998 count = 0;
999 } else {
1000 count = channel->discarded_events;
1001 }
1002
1003 health_code_update();
1004
1005 /* Send back returned value to session daemon */
1006 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1007 if (ret < 0) {
1008 PERROR("send discarded events");
1009 goto error_fatal;
1010 }
1011
1012 break;
1013 }
1014 case LTTNG_CONSUMER_LOST_PACKETS:
1015 {
1016 ssize_t ret;
1017 uint64_t count;
1018 struct lttng_consumer_channel *channel;
1019 uint64_t id = msg.u.lost_packets.session_id;
1020 uint64_t key = msg.u.lost_packets.channel_key;
1021
1022 DBG("Kernel consumer lost packets command for session id %"
1023 PRIu64 ", channel key %" PRIu64, id, key);
1024
1025 channel = consumer_find_channel(key);
1026 if (!channel) {
1027 ERR("Kernel consumer lost packets channel %"
1028 PRIu64 " not found", key);
1029 count = 0;
1030 } else {
1031 count = channel->lost_packets;
1032 }
1033
1034 health_code_update();
1035
1036 /* Send back returned value to session daemon */
1037 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1038 if (ret < 0) {
1039 PERROR("send lost packets");
1040 goto error_fatal;
1041 }
1042
1043 break;
1044 }
1045 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1046 {
1047 int channel_monitor_pipe;
1048
1049 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1050 /* Successfully received the command's type. */
1051 ret = consumer_send_status_msg(sock, ret_code);
1052 if (ret < 0) {
1053 goto error_fatal;
1054 }
1055
1056 ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe,
1057 1);
1058 if (ret != sizeof(channel_monitor_pipe)) {
1059 ERR("Failed to receive channel monitor pipe");
1060 goto error_fatal;
1061 }
1062
1063 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1064 ret = consumer_timer_thread_set_channel_monitor_pipe(
1065 channel_monitor_pipe);
1066 if (!ret) {
1067 int flags;
1068
1069 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1070 /* Set the pipe as non-blocking. */
1071 ret = fcntl(channel_monitor_pipe, F_GETFL, 0);
1072 if (ret == -1) {
1073 PERROR("fcntl get flags of the channel monitoring pipe");
1074 goto error_fatal;
1075 }
1076 flags = ret;
1077
1078 ret = fcntl(channel_monitor_pipe, F_SETFL,
1079 flags | O_NONBLOCK);
1080 if (ret == -1) {
1081 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1082 goto error_fatal;
1083 }
1084 DBG("Channel monitor pipe set as non-blocking");
1085 } else {
1086 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1087 }
1088 ret = consumer_send_status_msg(sock, ret_code);
1089 if (ret < 0) {
1090 goto error_fatal;
1091 }
1092 break;
1093 }
1094 case LTTNG_CONSUMER_CLEAR_CHANNEL:
1095 {
1096 struct lttng_consumer_channel *channel;
1097 uint64_t key = msg.u.clear_channel.key;
1098
1099 channel = consumer_find_channel(key);
1100 if (!channel) {
1101 DBG("Channel %" PRIu64 " not found", key);
1102 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1103 } else {
1104 ret = lttng_consumer_clear_channel(channel);
1105 if (ret) {
1106 ERR("Clear channel failed");
1107 ret_code = ret;
1108 }
1109
1110 health_code_update();
1111 }
1112 ret = consumer_send_status_msg(sock, ret_code);
1113 if (ret < 0) {
1114 /* Somehow, the session daemon is not responding anymore. */
1115 goto end_nosignal;
1116 }
1117 break;
1118 }
1119 case LTTNG_CONSUMER_CLEAR_SESSION:
1120 {
1121 uint64_t session_id = msg.u.clear_session.session_id;
1122
1123 ret = lttng_consumer_clear_session(session_id);
1124 if (ret) {
1125 ERR("Clear session failed session_id %" PRIu64, session_id);
1126 ret_code = ret;
1127 }
1128
1129 health_code_update();
1130 ret = consumer_send_status_msg(sock, ret_code);
1131 if (ret < 0) {
1132 /* Somehow, the session daemon is not responding anymore. */
1133 goto end_nosignal;
1134 }
1135 break;
1136 }
1137 default:
1138 goto end_nosignal;
1139 }
1140
1141 end_nosignal:
1142 rcu_read_unlock();
1143
1144 /*
1145 * Return 1 to indicate success since the 0 value can be a socket
1146 * shutdown during the recv() or send() call.
1147 */
1148 health_code_update();
1149 return 1;
1150
1151 error_fatal:
1152 rcu_read_unlock();
1153 /* This will issue a consumer stop. */
1154 return -1;
1155 }
1156
1157 /*
1158 * Populate index values of a kernel stream. Values are set in big endian order.
1159 *
1160 * Return 0 on success or else a negative value.
1161 */
1162 static int get_index_values(struct ctf_packet_index *index, int infd)
1163 {
1164 int ret;
1165
1166 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
1167 if (ret < 0) {
1168 PERROR("kernctl_get_timestamp_begin");
1169 goto error;
1170 }
1171 index->timestamp_begin = htobe64(index->timestamp_begin);
1172
1173 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
1174 if (ret < 0) {
1175 PERROR("kernctl_get_timestamp_end");
1176 goto error;
1177 }
1178 index->timestamp_end = htobe64(index->timestamp_end);
1179
1180 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
1181 if (ret < 0) {
1182 PERROR("kernctl_get_events_discarded");
1183 goto error;
1184 }
1185 index->events_discarded = htobe64(index->events_discarded);
1186
1187 ret = kernctl_get_content_size(infd, &index->content_size);
1188 if (ret < 0) {
1189 PERROR("kernctl_get_content_size");
1190 goto error;
1191 }
1192 index->content_size = htobe64(index->content_size);
1193
1194 ret = kernctl_get_packet_size(infd, &index->packet_size);
1195 if (ret < 0) {
1196 PERROR("kernctl_get_packet_size");
1197 goto error;
1198 }
1199 index->packet_size = htobe64(index->packet_size);
1200
1201 ret = kernctl_get_stream_id(infd, &index->stream_id);
1202 if (ret < 0) {
1203 PERROR("kernctl_get_stream_id");
1204 goto error;
1205 }
1206 index->stream_id = htobe64(index->stream_id);
1207
1208 ret = kernctl_get_instance_id(infd, &index->stream_instance_id);
1209 if (ret < 0) {
1210 if (ret == -ENOTTY) {
1211 /* Command not implemented by lttng-modules. */
1212 index->stream_instance_id = -1ULL;
1213 } else {
1214 PERROR("kernctl_get_instance_id");
1215 goto error;
1216 }
1217 }
1218 index->stream_instance_id = htobe64(index->stream_instance_id);
1219
1220 ret = kernctl_get_sequence_number(infd, &index->packet_seq_num);
1221 if (ret < 0) {
1222 if (ret == -ENOTTY) {
1223 /* Command not implemented by lttng-modules. */
1224 index->packet_seq_num = -1ULL;
1225 ret = 0;
1226 } else {
1227 PERROR("kernctl_get_sequence_number");
1228 goto error;
1229 }
1230 }
1231 index->packet_seq_num = htobe64(index->packet_seq_num);
1232
1233 error:
1234 return ret;
1235 }
1236 /*
1237 * Sync metadata meaning request them to the session daemon and snapshot to the
1238 * metadata thread can consumer them.
1239 *
1240 * Metadata stream lock MUST be acquired.
1241 *
1242 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1243 * is empty or a negative value on error.
1244 */
1245 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1246 {
1247 int ret;
1248
1249 assert(metadata);
1250
1251 ret = kernctl_buffer_flush(metadata->wait_fd);
1252 if (ret < 0) {
1253 ERR("Failed to flush kernel stream");
1254 goto end;
1255 }
1256
1257 ret = kernctl_snapshot(metadata->wait_fd);
1258 if (ret < 0) {
1259 if (ret != -EAGAIN) {
1260 ERR("Sync metadata, taking kernel snapshot failed.");
1261 goto end;
1262 }
1263 DBG("Sync metadata, no new kernel metadata");
1264 /* No new metadata, exit. */
1265 ret = ENODATA;
1266 goto end;
1267 }
1268
1269 end:
1270 return ret;
1271 }
1272
1273 static
1274 int update_stream_stats(struct lttng_consumer_stream *stream)
1275 {
1276 int ret;
1277 uint64_t seq, discarded;
1278
1279 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1280 if (ret < 0) {
1281 if (ret == -ENOTTY) {
1282 /* Command not implemented by lttng-modules. */
1283 seq = -1ULL;
1284 } else {
1285 PERROR("kernctl_get_sequence_number");
1286 goto end;
1287 }
1288 }
1289
1290 /*
1291 * Start the sequence when we extract the first packet in case we don't
1292 * start at 0 (for example if a consumer is not connected to the
1293 * session immediately after the beginning).
1294 */
1295 if (stream->last_sequence_number == -1ULL) {
1296 stream->last_sequence_number = seq;
1297 } else if (seq > stream->last_sequence_number) {
1298 stream->chan->lost_packets += seq -
1299 stream->last_sequence_number - 1;
1300 } else {
1301 /* seq <= last_sequence_number */
1302 ERR("Sequence number inconsistent : prev = %" PRIu64
1303 ", current = %" PRIu64,
1304 stream->last_sequence_number, seq);
1305 ret = -1;
1306 goto end;
1307 }
1308 stream->last_sequence_number = seq;
1309
1310 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1311 if (ret < 0) {
1312 PERROR("kernctl_get_events_discarded");
1313 goto end;
1314 }
1315 if (discarded < stream->last_discarded_events) {
1316 /*
1317 * Overflow has occurred. We assume only one wrap-around
1318 * has occurred.
1319 */
1320 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1321 stream->last_discarded_events + discarded;
1322 } else {
1323 stream->chan->discarded_events += discarded -
1324 stream->last_discarded_events;
1325 }
1326 stream->last_discarded_events = discarded;
1327 ret = 0;
1328
1329 end:
1330 return ret;
1331 }
1332
1333 /*
1334 * Check if the local version of the metadata stream matches with the version
1335 * of the metadata stream in the kernel. If it was updated, set the reset flag
1336 * on the stream.
1337 */
1338 static
1339 int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1340 {
1341 int ret;
1342 uint64_t cur_version;
1343
1344 ret = kernctl_get_metadata_version(infd, &cur_version);
1345 if (ret < 0) {
1346 if (ret == -ENOTTY) {
1347 /*
1348 * LTTng-modules does not implement this
1349 * command.
1350 */
1351 ret = 0;
1352 goto end;
1353 }
1354 ERR("Failed to get the metadata version");
1355 goto end;
1356 }
1357
1358 if (stream->metadata_version == cur_version) {
1359 ret = 0;
1360 goto end;
1361 }
1362
1363 DBG("New metadata version detected");
1364 stream->metadata_version = cur_version;
1365 stream->reset_metadata_flag = 1;
1366 ret = 0;
1367
1368 end:
1369 return ret;
1370 }
1371
1372 /*
1373 * Consume data on a file descriptor and write it on a trace file.
1374 */
1375 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1376 struct lttng_consumer_local_data *ctx)
1377 {
1378 unsigned long len, subbuf_size, padding;
1379 int err, write_index = 1;
1380 ssize_t ret = 0;
1381 int infd = stream->wait_fd;
1382 struct ctf_packet_index index;
1383
1384 DBG("In read_subbuffer (infd : %d)", infd);
1385
1386 /* Get the next subbuffer */
1387 err = kernctl_get_next_subbuf(infd);
1388 if (err != 0) {
1389 /*
1390 * This is a debug message even for single-threaded consumer,
1391 * because poll() have more relaxed criterions than get subbuf,
1392 * so get_subbuf may fail for short race windows where poll()
1393 * would issue wakeups.
1394 */
1395 DBG("Reserving sub buffer failed (everything is normal, "
1396 "it is due to concurrency)");
1397 ret = err;
1398 goto end;
1399 }
1400
1401 /* Get the full subbuffer size including padding */
1402 err = kernctl_get_padded_subbuf_size(infd, &len);
1403 if (err != 0) {
1404 PERROR("Getting sub-buffer len failed.");
1405 err = kernctl_put_subbuf(infd);
1406 if (err != 0) {
1407 if (err == -EFAULT) {
1408 PERROR("Error in unreserving sub buffer\n");
1409 } else if (err == -EIO) {
1410 /* Should never happen with newer LTTng versions */
1411 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1412 }
1413 ret = err;
1414 goto end;
1415 }
1416 ret = err;
1417 goto end;
1418 }
1419
1420 if (!stream->metadata_flag) {
1421 ret = get_index_values(&index, infd);
1422 if (ret < 0) {
1423 err = kernctl_put_subbuf(infd);
1424 if (err != 0) {
1425 if (err == -EFAULT) {
1426 PERROR("Error in unreserving sub buffer\n");
1427 } else if (err == -EIO) {
1428 /* Should never happen with newer LTTng versions */
1429 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1430 }
1431 ret = err;
1432 goto end;
1433 }
1434 goto end;
1435 }
1436 ret = update_stream_stats(stream);
1437 if (ret < 0) {
1438 err = kernctl_put_subbuf(infd);
1439 if (err != 0) {
1440 if (err == -EFAULT) {
1441 PERROR("Error in unreserving sub buffer\n");
1442 } else if (err == -EIO) {
1443 /* Should never happen with newer LTTng versions */
1444 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1445 }
1446 ret = err;
1447 goto end;
1448 }
1449 goto end;
1450 }
1451 } else {
1452 write_index = 0;
1453 ret = metadata_stream_check_version(infd, stream);
1454 if (ret < 0) {
1455 err = kernctl_put_subbuf(infd);
1456 if (err != 0) {
1457 if (err == -EFAULT) {
1458 PERROR("Error in unreserving sub buffer\n");
1459 } else if (err == -EIO) {
1460 /* Should never happen with newer LTTng versions */
1461 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1462 }
1463 ret = err;
1464 goto end;
1465 }
1466 goto end;
1467 }
1468 }
1469
1470 switch (stream->chan->output) {
1471 case CONSUMER_CHANNEL_SPLICE:
1472 /*
1473 * XXX: The lttng-modules splice "actor" does not handle copying
1474 * partial pages hence only using the subbuffer size without the
1475 * padding makes the splice fail.
1476 */
1477 subbuf_size = len;
1478 padding = 0;
1479
1480 /* splice the subbuffer to the tracefile */
1481 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
1482 padding, &index);
1483 /*
1484 * XXX: Splice does not support network streaming so the return value
1485 * is simply checked against subbuf_size and not like the mmap() op.
1486 */
1487 if (ret != subbuf_size) {
1488 /*
1489 * display the error but continue processing to try
1490 * to release the subbuffer
1491 */
1492 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1493 ret, subbuf_size);
1494 write_index = 0;
1495 }
1496 break;
1497 case CONSUMER_CHANNEL_MMAP:
1498 /* Get subbuffer size without padding */
1499 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1500 if (err != 0) {
1501 PERROR("Getting sub-buffer len failed.");
1502 err = kernctl_put_subbuf(infd);
1503 if (err != 0) {
1504 if (err == -EFAULT) {
1505 PERROR("Error in unreserving sub buffer\n");
1506 } else if (err == -EIO) {
1507 /* Should never happen with newer LTTng versions */
1508 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1509 }
1510 ret = err;
1511 goto end;
1512 }
1513 ret = err;
1514 goto end;
1515 }
1516
1517 /* Make sure the tracer is not gone mad on us! */
1518 assert(len >= subbuf_size);
1519
1520 padding = len - subbuf_size;
1521
1522 /* write the subbuffer to the tracefile */
1523 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
1524 padding, &index);
1525 /*
1526 * The mmap operation should write subbuf_size amount of data when
1527 * network streaming or the full padding (len) size when we are _not_
1528 * streaming.
1529 */
1530 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1531 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1532 /*
1533 * Display the error but continue processing to try to release the
1534 * subbuffer. This is a DBG statement since this is possible to
1535 * happen without being a critical error.
1536 */
1537 DBG("Error writing to tracefile "
1538 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1539 ret, len, subbuf_size);
1540 write_index = 0;
1541 }
1542 break;
1543 default:
1544 ERR("Unknown output method");
1545 ret = -EPERM;
1546 }
1547
1548 err = kernctl_put_next_subbuf(infd);
1549 if (err != 0) {
1550 if (err == -EFAULT) {
1551 PERROR("Error in unreserving sub buffer\n");
1552 } else if (err == -EIO) {
1553 /* Should never happen with newer LTTng versions */
1554 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1555 }
1556 ret = err;
1557 goto end;
1558 }
1559
1560 /* Write index if needed. */
1561 if (!write_index) {
1562 goto end;
1563 }
1564
1565 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1566 /*
1567 * In live, block until all the metadata is sent.
1568 */
1569 pthread_mutex_lock(&stream->metadata_timer_lock);
1570 assert(!stream->missed_metadata_flush);
1571 stream->waiting_on_metadata = true;
1572 pthread_mutex_unlock(&stream->metadata_timer_lock);
1573
1574 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1575
1576 pthread_mutex_lock(&stream->metadata_timer_lock);
1577 stream->waiting_on_metadata = false;
1578 if (stream->missed_metadata_flush) {
1579 stream->missed_metadata_flush = false;
1580 pthread_mutex_unlock(&stream->metadata_timer_lock);
1581 (void) consumer_flush_kernel_index(stream);
1582 } else {
1583 pthread_mutex_unlock(&stream->metadata_timer_lock);
1584 }
1585 if (err < 0) {
1586 goto end;
1587 }
1588 }
1589
1590 err = consumer_stream_write_index(stream, &index);
1591 if (err < 0) {
1592 goto end;
1593 }
1594
1595 end:
1596 return ret;
1597 }
1598
1599 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1600 {
1601 int ret;
1602
1603 assert(stream);
1604
1605 /*
1606 * Don't create anything if this is set for streaming or should not be
1607 * monitored.
1608 */
1609 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
1610 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1611 stream->chan->tracefile_size, stream->tracefile_count_current,
1612 stream->uid, stream->gid, NULL);
1613 if (ret < 0) {
1614 goto error;
1615 }
1616 stream->out_fd = ret;
1617 stream->tracefile_size_current = 0;
1618
1619 if (!stream->metadata_flag) {
1620 struct lttng_index_file *index_file;
1621
1622 index_file = lttng_index_file_create(stream->chan->pathname,
1623 stream->name, stream->uid, stream->gid,
1624 stream->chan->tracefile_size,
1625 stream->tracefile_count_current,
1626 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1627 if (!index_file) {
1628 goto error;
1629 }
1630 assert(!stream->index_file);
1631 stream->index_file = index_file;
1632 }
1633 }
1634
1635 if (stream->output == LTTNG_EVENT_MMAP) {
1636 /* get the len of the mmap region */
1637 unsigned long mmap_len;
1638
1639 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1640 if (ret != 0) {
1641 PERROR("kernctl_get_mmap_len");
1642 goto error_close_fd;
1643 }
1644 stream->mmap_len = (size_t) mmap_len;
1645
1646 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1647 MAP_PRIVATE, stream->wait_fd, 0);
1648 if (stream->mmap_base == MAP_FAILED) {
1649 PERROR("Error mmaping");
1650 ret = -1;
1651 goto error_close_fd;
1652 }
1653 }
1654
1655 /* we return 0 to let the library handle the FD internally */
1656 return 0;
1657
1658 error_close_fd:
1659 if (stream->out_fd >= 0) {
1660 int err;
1661
1662 err = close(stream->out_fd);
1663 assert(!err);
1664 stream->out_fd = -1;
1665 }
1666 error:
1667 return ret;
1668 }
1669
1670 /*
1671 * Check if data is still being extracted from the buffers for a specific
1672 * stream. Consumer data lock MUST be acquired before calling this function
1673 * and the stream lock.
1674 *
1675 * Return 1 if the traced data are still getting read else 0 meaning that the
1676 * data is available for trace viewer reading.
1677 */
1678 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1679 {
1680 int ret;
1681
1682 assert(stream);
1683
1684 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1685 ret = 0;
1686 goto end;
1687 }
1688
1689 ret = kernctl_get_next_subbuf(stream->wait_fd);
1690 if (ret == 0) {
1691 /* There is still data so let's put back this subbuffer. */
1692 ret = kernctl_put_subbuf(stream->wait_fd);
1693 assert(ret == 0);
1694 ret = 1; /* Data is pending */
1695 goto end;
1696 }
1697
1698 /* Data is NOT pending and ready to be read. */
1699 ret = 0;
1700
1701 end:
1702 return ret;
1703 }
This page took 0.104279 seconds and 5 git commands to generate.