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