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