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