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