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