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