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