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