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