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