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