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