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