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