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