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