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