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