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