consumerd: move rotation logic to domain-agnostic read path
[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,
3ef395a9 526 msg.u.channel.is_live,
3d071855 527 NULL, NULL);
3bd1e081 528 if (new_channel == NULL) {
f73fabfd 529 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
530 goto end_nosignal;
531 }
ffe60014 532 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
533 switch (msg.u.channel.output) {
534 case LTTNG_EVENT_SPLICE:
535 new_channel->output = CONSUMER_CHANNEL_SPLICE;
536 break;
537 case LTTNG_EVENT_MMAP:
538 new_channel->output = CONSUMER_CHANNEL_MMAP;
539 break;
540 default:
541 ERR("Channel output unknown %d", msg.u.channel.output);
542 goto end_nosignal;
543 }
ffe60014
DG
544
545 /* Translate and save channel type. */
546 switch (msg.u.channel.type) {
547 case CONSUMER_CHANNEL_TYPE_DATA:
548 case CONSUMER_CHANNEL_TYPE_METADATA:
549 new_channel->type = msg.u.channel.type;
550 break;
551 default:
552 assert(0);
553 goto end_nosignal;
554 };
555
9ce5646a
MD
556 health_code_update();
557
3bd1e081 558 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
559 ret_recv = ctx->on_recv_channel(new_channel);
560 if (ret_recv == 0) {
561 ret = consumer_add_channel(new_channel, ctx);
562 } else if (ret_recv < 0) {
3bd1e081
MD
563 goto end_nosignal;
564 }
565 } else {
e43c41c5 566 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 567 }
e9404c27
JG
568 if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) {
569 int monitor_start_ret;
570
571 DBG("Consumer starting monitor timer");
94d49140
JD
572 consumer_timer_live_start(new_channel,
573 msg.u.channel.live_timer_interval);
e9404c27
JG
574 monitor_start_ret = consumer_timer_monitor_start(
575 new_channel,
576 msg.u.channel.monitor_timer_interval);
577 if (monitor_start_ret < 0) {
578 ERR("Starting channel monitoring timer failed");
579 goto end_nosignal;
580 }
581
94d49140 582 }
e43c41c5 583
9ce5646a
MD
584 health_code_update();
585
e43c41c5 586 /* If we received an error in add_channel, we need to report it. */
821fffb2 587 if (ret < 0) {
1803a064
MD
588 ret = consumer_send_status_msg(sock, ret);
589 if (ret < 0) {
590 goto error_fatal;
591 }
e43c41c5
JD
592 goto end_nosignal;
593 }
594
3bd1e081
MD
595 goto end_nosignal;
596 }
597 case LTTNG_CONSUMER_ADD_STREAM:
598 {
dae10966
DG
599 int fd;
600 struct lttng_pipe *stream_pipe;
00e2e675 601 struct lttng_consumer_stream *new_stream;
ffe60014 602 struct lttng_consumer_channel *channel;
c80048c6 603 int alloc_ret = 0;
3bd1e081 604
ffe60014
DG
605 /*
606 * Get stream's channel reference. Needed when adding the stream to the
607 * global hash table.
608 */
609 channel = consumer_find_channel(msg.u.stream.channel_key);
610 if (!channel) {
611 /*
612 * We could not find the channel. Can happen if cpu hotplug
613 * happens while tearing down.
614 */
d88aee68 615 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
e462382a 616 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
ffe60014
DG
617 }
618
9ce5646a
MD
619 health_code_update();
620
f50f23d9
DG
621 /* First send a status message before receiving the fds. */
622 ret = consumer_send_status_msg(sock, ret_code);
1803a064 623 if (ret < 0) {
d771f832 624 /* Somehow, the session daemon is not responding anymore. */
e0e7757d 625 goto error_add_stream_fatal;
1803a064 626 }
9ce5646a
MD
627
628 health_code_update();
629
0c759fc9 630 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
d771f832 631 /* Channel was not found. */
e0e7757d 632 goto error_add_stream_nosignal;
f50f23d9
DG
633 }
634
d771f832 635 /* Blocking call */
9ce5646a
MD
636 health_poll_entry();
637 ret = lttng_consumer_poll_socket(consumer_sockpoll);
638 health_poll_exit();
84382d49 639 if (ret) {
e0e7757d 640 goto error_add_stream_fatal;
3bd1e081 641 }
00e2e675 642
9ce5646a
MD
643 health_code_update();
644
00e2e675 645 /* Get stream file descriptor from socket */
f2fc6720
MD
646 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
647 if (ret != sizeof(fd)) {
f73fabfd 648 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
e0e7757d 649 goto end;
3bd1e081 650 }
3bd1e081 651
9ce5646a
MD
652 health_code_update();
653
f50f23d9
DG
654 /*
655 * Send status code to session daemon only if the recv works. If the
656 * above recv() failed, the session daemon is notified through the
657 * error socket and the teardown is eventually done.
658 */
659 ret = consumer_send_status_msg(sock, ret_code);
660 if (ret < 0) {
661 /* Somehow, the session daemon is not responding anymore. */
e0e7757d 662 goto error_add_stream_nosignal;
f50f23d9
DG
663 }
664
9ce5646a
MD
665 health_code_update();
666
e5148e25 667 pthread_mutex_lock(&channel->lock);
c0c85e4f
JG
668 new_stream = consumer_allocate_stream(
669 channel,
670 channel->key,
ffe60014 671 fd,
ffe60014 672 channel->name,
ffe60014
DG
673 channel->relayd_id,
674 channel->session_id,
e5148e25 675 channel->trace_chunk,
ffe60014
DG
676 msg.u.stream.cpu,
677 &alloc_ret,
4891ece8 678 channel->type,
e5148e25 679 channel->monitor);
3bd1e081 680 if (new_stream == NULL) {
c80048c6
MD
681 switch (alloc_ret) {
682 case -ENOMEM:
683 case -EINVAL:
684 default:
685 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
686 break;
c80048c6 687 }
e5148e25 688 pthread_mutex_unlock(&channel->lock);
e0e7757d 689 goto error_add_stream_nosignal;
3bd1e081 690 }
d771f832 691
ffe60014 692 new_stream->wait_fd = fd;
2c42aa5c
JG
693 ret = kernctl_get_max_subbuf_size(new_stream->wait_fd,
694 &new_stream->max_sb_size);
695 if (ret < 0) {
696 pthread_mutex_unlock(&channel->lock);
697 ERR("Failed to get kernel maximal subbuffer size");
e0e7757d 698 goto error_add_stream_nosignal;
2c42aa5c
JG
699 }
700
d9a2e16e
JD
701 consumer_stream_update_channel_attributes(new_stream,
702 channel);
07b86b52
JD
703 switch (channel->output) {
704 case CONSUMER_CHANNEL_SPLICE:
705 new_stream->output = LTTNG_EVENT_SPLICE;
a2361a61
JD
706 ret = utils_create_pipe(new_stream->splice_pipe);
707 if (ret < 0) {
e5148e25 708 pthread_mutex_unlock(&channel->lock);
e0e7757d 709 goto error_add_stream_nosignal;
a2361a61 710 }
07b86b52
JD
711 break;
712 case CONSUMER_CHANNEL_MMAP:
713 new_stream->output = LTTNG_EVENT_MMAP;
714 break;
715 default:
716 ERR("Stream output unknown %d", channel->output);
e5148e25 717 pthread_mutex_unlock(&channel->lock);
e0e7757d 718 goto error_add_stream_nosignal;
07b86b52 719 }
00e2e675 720
a0c83db9
DG
721 /*
722 * We've just assigned the channel to the stream so increment the
07b86b52
JD
723 * refcount right now. We don't need to increment the refcount for
724 * streams in no monitor because we handle manually the cleanup of
725 * those. It is very important to make sure there is NO prior
726 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 727 */
07b86b52
JD
728 if (channel->monitor) {
729 uatomic_inc(&new_stream->chan->refcount);
730 }
9d9353f9 731
fb3a43a9
DG
732 /*
733 * The buffer flush is done on the session daemon side for the kernel
734 * so no need for the stream "hangup_flush_done" variable to be
735 * tracked. This is important for a kernel stream since we don't rely
736 * on the flush state of the stream to read data. It's not the case for
737 * user space tracing.
738 */
739 new_stream->hangup_flush_done = 0;
740
9ce5646a
MD
741 health_code_update();
742
e5148e25 743 pthread_mutex_lock(&new_stream->lock);
633d0084
DG
744 if (ctx->on_recv_stream) {
745 ret = ctx->on_recv_stream(new_stream);
746 if (ret < 0) {
e5148e25
JG
747 pthread_mutex_unlock(&new_stream->lock);
748 pthread_mutex_unlock(&channel->lock);
d771f832 749 consumer_stream_free(new_stream);
e0e7757d 750 goto error_add_stream_nosignal;
fb3a43a9 751 }
633d0084 752 }
9ce5646a
MD
753 health_code_update();
754
07b86b52
JD
755 if (new_stream->metadata_flag) {
756 channel->metadata_stream = new_stream;
757 }
758
2bba9e53
DG
759 /* Do not monitor this stream. */
760 if (!channel->monitor) {
5eecee74 761 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 762 "relayd id %" PRIu64, new_stream->name,
5eecee74 763 new_stream->net_seq_idx);
10a50311 764 cds_list_add(&new_stream->send_node, &channel->streams.head);
e5148e25
JG
765 pthread_mutex_unlock(&new_stream->lock);
766 pthread_mutex_unlock(&channel->lock);
e0e7757d 767 goto end_add_stream;
6dc3064a
DG
768 }
769
e1b71bdc
DG
770 /* Send stream to relayd if the stream has an ID. */
771 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
772 ret = consumer_send_relayd_stream(new_stream,
773 new_stream->chan->pathname);
e1b71bdc 774 if (ret < 0) {
e5148e25
JG
775 pthread_mutex_unlock(&new_stream->lock);
776 pthread_mutex_unlock(&channel->lock);
e1b71bdc 777 consumer_stream_free(new_stream);
e0e7757d 778 goto error_add_stream_nosignal;
e1b71bdc 779 }
001b7e62
MD
780
781 /*
782 * If adding an extra stream to an already
783 * existing channel (e.g. cpu hotplug), we need
784 * to send the "streams_sent" command to relayd.
785 */
786 if (channel->streams_sent_to_relayd) {
787 ret = consumer_send_relayd_streams_sent(
788 new_stream->net_seq_idx);
789 if (ret < 0) {
e5148e25
JG
790 pthread_mutex_unlock(&new_stream->lock);
791 pthread_mutex_unlock(&channel->lock);
e0e7757d 792 goto error_add_stream_nosignal;
001b7e62
MD
793 }
794 }
e2039c7a 795 }
e5148e25
JG
796 pthread_mutex_unlock(&new_stream->lock);
797 pthread_mutex_unlock(&channel->lock);
e2039c7a 798
50f8ae69 799 /* Get the right pipe where the stream will be sent. */
633d0084 800 if (new_stream->metadata_flag) {
66d583dc 801 consumer_add_metadata_stream(new_stream);
dae10966 802 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 803 } else {
66d583dc 804 consumer_add_data_stream(new_stream);
dae10966 805 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
806 }
807
66d583dc 808 /* Visible to other threads */
5ab66908
MD
809 new_stream->globally_visible = 1;
810
9ce5646a
MD
811 health_code_update();
812
dae10966 813 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 814 if (ret < 0) {
dae10966 815 ERR("Consumer write %s stream to pipe %d",
50f8ae69 816 new_stream->metadata_flag ? "metadata" : "data",
dae10966 817 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
818 if (new_stream->metadata_flag) {
819 consumer_del_stream_for_metadata(new_stream);
820 } else {
821 consumer_del_stream_for_data(new_stream);
822 }
e0e7757d 823 goto error_add_stream_nosignal;
3bd1e081 824 }
00e2e675 825
02d02e31
JD
826 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64,
827 new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id);
e0e7757d 828end_add_stream:
3bd1e081 829 break;
e0e7757d
JG
830error_add_stream_nosignal:
831 goto end_nosignal;
832error_add_stream_fatal:
833 goto error_fatal;
3bd1e081 834 }
a4baae1b
JD
835 case LTTNG_CONSUMER_STREAMS_SENT:
836 {
837 struct lttng_consumer_channel *channel;
838
839 /*
840 * Get stream's channel reference. Needed when adding the stream to the
841 * global hash table.
842 */
843 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
844 if (!channel) {
845 /*
846 * We could not find the channel. Can happen if cpu hotplug
847 * happens while tearing down.
848 */
849 ERR("Unable to find channel key %" PRIu64,
850 msg.u.sent_streams.channel_key);
e462382a 851 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
a4baae1b
JD
852 }
853
854 health_code_update();
855
856 /*
857 * Send status code to session daemon.
858 */
859 ret = consumer_send_status_msg(sock, ret_code);
f261ad0a 860 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
a4baae1b 861 /* Somehow, the session daemon is not responding anymore. */
6ec577e9 862 goto error_streams_sent_nosignal;
a4baae1b
JD
863 }
864
865 health_code_update();
866
867 /*
868 * We should not send this message if we don't monitor the
869 * streams in this channel.
870 */
871 if (!channel->monitor) {
6ec577e9 872 goto end_error_streams_sent;
a4baae1b
JD
873 }
874
875 health_code_update();
876 /* Send stream to relayd if the stream has an ID. */
877 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
878 ret = consumer_send_relayd_streams_sent(
879 msg.u.sent_streams.net_seq_idx);
880 if (ret < 0) {
6ec577e9 881 goto error_streams_sent_nosignal;
a4baae1b 882 }
001b7e62 883 channel->streams_sent_to_relayd = true;
a4baae1b 884 }
6ec577e9 885end_error_streams_sent:
a4baae1b 886 break;
6ec577e9
JG
887error_streams_sent_nosignal:
888 goto end_nosignal;
a4baae1b 889 }
3bd1e081
MD
890 case LTTNG_CONSUMER_UPDATE_STREAM:
891 {
3f8e211f
DG
892 rcu_read_unlock();
893 return -ENOSYS;
894 }
895 case LTTNG_CONSUMER_DESTROY_RELAYD:
896 {
a6ba4fe1 897 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
898 struct consumer_relayd_sock_pair *relayd;
899
a6ba4fe1 900 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
901
902 /* Get relayd reference if exists. */
a6ba4fe1 903 relayd = consumer_find_relayd(index);
3f8e211f 904 if (relayd == NULL) {
3448e266 905 DBG("Unable to find relayd %" PRIu64, index);
e462382a 906 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3bd1e081 907 }
3f8e211f 908
a6ba4fe1
DG
909 /*
910 * Each relayd socket pair has a refcount of stream attached to it
911 * which tells if the relayd is still active or not depending on the
912 * refcount value.
913 *
914 * This will set the destroy flag of the relayd object and destroy it
915 * if the refcount reaches zero when called.
916 *
917 * The destroy can happen either here or when a stream fd hangs up.
918 */
f50f23d9
DG
919 if (relayd) {
920 consumer_flag_relayd_for_destroy(relayd);
921 }
922
9ce5646a
MD
923 health_code_update();
924
f50f23d9
DG
925 ret = consumer_send_status_msg(sock, ret_code);
926 if (ret < 0) {
927 /* Somehow, the session daemon is not responding anymore. */
1803a064 928 goto error_fatal;
f50f23d9 929 }
3f8e211f 930
3f8e211f 931 goto end_nosignal;
3bd1e081 932 }
6d805429 933 case LTTNG_CONSUMER_DATA_PENDING:
53632229 934 {
c8f59ee5 935 int32_t ret;
6d805429 936 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 937
6d805429 938 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 939
6d805429 940 ret = consumer_data_pending(id);
c8f59ee5 941
9ce5646a
MD
942 health_code_update();
943
c8f59ee5
DG
944 /* Send back returned value to session daemon */
945 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
946 if (ret < 0) {
6d805429 947 PERROR("send data pending ret code");
1803a064 948 goto error_fatal;
c8f59ee5 949 }
f50f23d9
DG
950
951 /*
952 * No need to send back a status message since the data pending
953 * returned value is the response.
954 */
c8f59ee5 955 break;
53632229 956 }
6dc3064a
DG
957 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
958 {
b0226bd4
MD
959 struct lttng_consumer_channel *channel;
960 uint64_t key = msg.u.snapshot_channel.key;
961
962 channel = consumer_find_channel(key);
963 if (!channel) {
964 ERR("Channel %" PRIu64 " not found", key);
965 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52 966 } else {
e5148e25 967 pthread_mutex_lock(&channel->lock);
b0226bd4
MD
968 if (msg.u.snapshot_channel.metadata == 1) {
969 ret = lttng_kconsumer_snapshot_metadata(channel, key,
970 msg.u.snapshot_channel.pathname,
971 msg.u.snapshot_channel.relayd_id, ctx);
972 if (ret < 0) {
973 ERR("Snapshot metadata failed");
974 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
975 }
976 } else {
977 ret = lttng_kconsumer_snapshot_channel(channel, key,
978 msg.u.snapshot_channel.pathname,
979 msg.u.snapshot_channel.relayd_id,
980 msg.u.snapshot_channel.nb_packets_per_stream,
981 ctx);
982 if (ret < 0) {
983 ERR("Snapshot channel failed");
984 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
985 }
07b86b52 986 }
e5148e25 987 pthread_mutex_unlock(&channel->lock);
07b86b52 988 }
9ce5646a
MD
989 health_code_update();
990
6dc3064a
DG
991 ret = consumer_send_status_msg(sock, ret_code);
992 if (ret < 0) {
993 /* Somehow, the session daemon is not responding anymore. */
994 goto end_nosignal;
995 }
996 break;
997 }
07b86b52
JD
998 case LTTNG_CONSUMER_DESTROY_CHANNEL:
999 {
1000 uint64_t key = msg.u.destroy_channel.key;
1001 struct lttng_consumer_channel *channel;
1002
1003 channel = consumer_find_channel(key);
1004 if (!channel) {
1005 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
e462382a 1006 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
1007 }
1008
9ce5646a
MD
1009 health_code_update();
1010
07b86b52
JD
1011 ret = consumer_send_status_msg(sock, ret_code);
1012 if (ret < 0) {
1013 /* Somehow, the session daemon is not responding anymore. */
09d3dbc0 1014 goto end_destroy_channel;
07b86b52
JD
1015 }
1016
9ce5646a
MD
1017 health_code_update();
1018
15dc512a
DG
1019 /* Stop right now if no channel was found. */
1020 if (!channel) {
09d3dbc0 1021 goto end_destroy_channel;
15dc512a
DG
1022 }
1023
07b86b52
JD
1024 /*
1025 * This command should ONLY be issued for channel with streams set in
1026 * no monitor mode.
1027 */
1028 assert(!channel->monitor);
1029
1030 /*
1031 * The refcount should ALWAYS be 0 in the case of a channel in no
1032 * monitor mode.
1033 */
1034 assert(!uatomic_sub_return(&channel->refcount, 1));
1035
1036 consumer_del_channel(channel);
09d3dbc0 1037end_destroy_channel:
07b86b52
JD
1038 goto end_nosignal;
1039 }
fb83fe64
JD
1040 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1041 {
66ab32be
JD
1042 ssize_t ret;
1043 uint64_t count;
fb83fe64
JD
1044 struct lttng_consumer_channel *channel;
1045 uint64_t id = msg.u.discarded_events.session_id;
1046 uint64_t key = msg.u.discarded_events.channel_key;
1047
e5742757
MD
1048 DBG("Kernel consumer discarded events command for session id %"
1049 PRIu64 ", channel key %" PRIu64, id, key);
1050
fb83fe64
JD
1051 channel = consumer_find_channel(key);
1052 if (!channel) {
1053 ERR("Kernel consumer discarded events channel %"
1054 PRIu64 " not found", key);
66ab32be 1055 count = 0;
e5742757 1056 } else {
66ab32be 1057 count = channel->discarded_events;
fb83fe64
JD
1058 }
1059
fb83fe64
JD
1060 health_code_update();
1061
1062 /* Send back returned value to session daemon */
66ab32be 1063 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
fb83fe64
JD
1064 if (ret < 0) {
1065 PERROR("send discarded events");
1066 goto error_fatal;
1067 }
1068
1069 break;
1070 }
1071 case LTTNG_CONSUMER_LOST_PACKETS:
1072 {
66ab32be
JD
1073 ssize_t ret;
1074 uint64_t count;
fb83fe64
JD
1075 struct lttng_consumer_channel *channel;
1076 uint64_t id = msg.u.lost_packets.session_id;
1077 uint64_t key = msg.u.lost_packets.channel_key;
1078
e5742757
MD
1079 DBG("Kernel consumer lost packets command for session id %"
1080 PRIu64 ", channel key %" PRIu64, id, key);
1081
fb83fe64
JD
1082 channel = consumer_find_channel(key);
1083 if (!channel) {
1084 ERR("Kernel consumer lost packets channel %"
1085 PRIu64 " not found", key);
66ab32be 1086 count = 0;
e5742757 1087 } else {
66ab32be 1088 count = channel->lost_packets;
fb83fe64
JD
1089 }
1090
fb83fe64
JD
1091 health_code_update();
1092
1093 /* Send back returned value to session daemon */
66ab32be 1094 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
fb83fe64
JD
1095 if (ret < 0) {
1096 PERROR("send lost packets");
1097 goto error_fatal;
1098 }
1099
1100 break;
1101 }
b3530820
JG
1102 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1103 {
1104 int channel_monitor_pipe;
1105
1106 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1107 /* Successfully received the command's type. */
1108 ret = consumer_send_status_msg(sock, ret_code);
1109 if (ret < 0) {
1110 goto error_fatal;
1111 }
1112
1113 ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe,
1114 1);
1115 if (ret != sizeof(channel_monitor_pipe)) {
1116 ERR("Failed to receive channel monitor pipe");
1117 goto error_fatal;
1118 }
1119
1120 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1121 ret = consumer_timer_thread_set_channel_monitor_pipe(
1122 channel_monitor_pipe);
1123 if (!ret) {
1124 int flags;
1125
1126 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1127 /* Set the pipe as non-blocking. */
1128 ret = fcntl(channel_monitor_pipe, F_GETFL, 0);
1129 if (ret == -1) {
1130 PERROR("fcntl get flags of the channel monitoring pipe");
1131 goto error_fatal;
1132 }
1133 flags = ret;
1134
1135 ret = fcntl(channel_monitor_pipe, F_SETFL,
1136 flags | O_NONBLOCK);
1137 if (ret == -1) {
1138 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1139 goto error_fatal;
1140 }
1141 DBG("Channel monitor pipe set as non-blocking");
1142 } else {
1143 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1144 }
1145 ret = consumer_send_status_msg(sock, ret_code);
1146 if (ret < 0) {
1147 goto error_fatal;
1148 }
1149 break;
1150 }
b99a8d42
JD
1151 case LTTNG_CONSUMER_ROTATE_CHANNEL:
1152 {
e96d66b4
MD
1153 struct lttng_consumer_channel *channel;
1154 uint64_t key = msg.u.rotate_channel.key;
b99a8d42 1155
e96d66b4 1156 DBG("Consumer rotate channel %" PRIu64, key);
b99a8d42 1157
e96d66b4
MD
1158 channel = consumer_find_channel(key);
1159 if (!channel) {
1160 ERR("Channel %" PRIu64 " not found", key);
1161 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1162 } else {
1163 /*
1164 * Sample the rotate position of all the streams in this channel.
1165 */
1166 ret = lttng_consumer_rotate_channel(channel, key,
e96d66b4
MD
1167 msg.u.rotate_channel.relayd_id,
1168 msg.u.rotate_channel.metadata,
e96d66b4
MD
1169 ctx);
1170 if (ret < 0) {
1171 ERR("Rotate channel failed");
1172 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
1173 }
b99a8d42 1174
e96d66b4
MD
1175 health_code_update();
1176 }
b99a8d42
JD
1177 ret = consumer_send_status_msg(sock, ret_code);
1178 if (ret < 0) {
1179 /* Somehow, the session daemon is not responding anymore. */
812e4d26 1180 goto error_rotate_channel;
b99a8d42 1181 }
e96d66b4
MD
1182 if (channel) {
1183 /* Rotate the streams that are ready right now. */
1184 ret = lttng_consumer_rotate_ready_streams(
1185 channel, key, ctx);
1186 if (ret < 0) {
1187 ERR("Rotate ready streams failed");
1188 }
b99a8d42 1189 }
b99a8d42 1190 break;
812e4d26
JG
1191error_rotate_channel:
1192 goto end_nosignal;
b99a8d42 1193 }
e5148e25 1194 case LTTNG_CONSUMER_INIT:
00fb02ac 1195 {
e5148e25
JG
1196 ret_code = lttng_consumer_init_command(ctx,
1197 msg.u.init.sessiond_uuid);
00fb02ac 1198 health_code_update();
00fb02ac
JD
1199 ret = consumer_send_status_msg(sock, ret_code);
1200 if (ret < 0) {
1201 /* Somehow, the session daemon is not responding anymore. */
1202 goto end_nosignal;
1203 }
1204 break;
1205 }
e5148e25 1206 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
d88744a4 1207 {
e5148e25 1208 const struct lttng_credentials credentials = {
0ebdafe0
JG
1209 .uid = msg.u.create_trace_chunk.credentials.value.uid,
1210 .gid = msg.u.create_trace_chunk.credentials.value.gid,
e5148e25
JG
1211 };
1212 const bool is_local_trace =
1213 !msg.u.create_trace_chunk.relayd_id.is_set;
1214 const uint64_t relayd_id =
1215 msg.u.create_trace_chunk.relayd_id.value;
1216 const char *chunk_override_name =
1217 *msg.u.create_trace_chunk.override_name ?
1218 msg.u.create_trace_chunk.override_name :
1219 NULL;
1220 LTTNG_OPTIONAL(struct lttng_directory_handle) chunk_directory_handle =
1221 LTTNG_OPTIONAL_INIT;
d88744a4 1222
e5148e25
JG
1223 /*
1224 * The session daemon will only provide a chunk directory file
1225 * descriptor for local traces.
1226 */
1227 if (is_local_trace) {
1228 int chunk_dirfd;
19990ed5 1229
e5148e25
JG
1230 /* Acnowledge the reception of the command. */
1231 ret = consumer_send_status_msg(sock,
1232 LTTCOMM_CONSUMERD_SUCCESS);
1233 if (ret < 0) {
1234 /* Somehow, the session daemon is not responding anymore. */
1235 goto end_nosignal;
1236 }
82528808 1237
e5148e25
JG
1238 ret = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1);
1239 if (ret != sizeof(chunk_dirfd)) {
1240 ERR("Failed to receive trace chunk directory file descriptor");
1241 goto error_fatal;
1242 }
82528808 1243
e5148e25
JG
1244 DBG("Received trace chunk directory fd (%d)",
1245 chunk_dirfd);
1246 ret = lttng_directory_handle_init_from_dirfd(
1247 &chunk_directory_handle.value,
1248 chunk_dirfd);
1249 if (ret) {
1250 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1251 if (close(chunk_dirfd)) {
1252 PERROR("Failed to close chunk directory file descriptor");
1253 }
1254 goto error_fatal;
1255 }
1256 chunk_directory_handle.is_set = true;
82528808
JG
1257 }
1258
e5148e25
JG
1259 ret_code = lttng_consumer_create_trace_chunk(
1260 !is_local_trace ? &relayd_id : NULL,
1261 msg.u.create_trace_chunk.session_id,
1262 msg.u.create_trace_chunk.chunk_id,
0ebdafe0
JG
1263 (time_t) msg.u.create_trace_chunk
1264 .creation_timestamp,
e5148e25 1265 chunk_override_name,
0ebdafe0
JG
1266 msg.u.create_trace_chunk.credentials.is_set ?
1267 &credentials :
1268 NULL,
e5148e25
JG
1269 chunk_directory_handle.is_set ?
1270 &chunk_directory_handle.value :
1271 NULL);
82528808 1272
e5148e25
JG
1273 if (chunk_directory_handle.is_set) {
1274 lttng_directory_handle_fini(
1275 &chunk_directory_handle.value);
d88744a4 1276 }
e5148e25 1277 goto end_msg_sessiond;
d88744a4 1278 }
e5148e25 1279 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
a1ae2ea5 1280 {
6bbcff33
JG
1281 enum lttng_trace_chunk_command_type close_command =
1282 msg.u.close_trace_chunk.close_command.value;
e5148e25
JG
1283 const uint64_t relayd_id =
1284 msg.u.close_trace_chunk.relayd_id.value;
41b23598
MD
1285 struct lttcomm_consumer_close_trace_chunk_reply reply;
1286 char path[LTTNG_PATH_MAX];
e5148e25
JG
1287
1288 ret_code = lttng_consumer_close_trace_chunk(
1289 msg.u.close_trace_chunk.relayd_id.is_set ?
6bbcff33
JG
1290 &relayd_id :
1291 NULL,
e5148e25
JG
1292 msg.u.close_trace_chunk.session_id,
1293 msg.u.close_trace_chunk.chunk_id,
6bbcff33
JG
1294 (time_t) msg.u.close_trace_chunk.close_timestamp,
1295 msg.u.close_trace_chunk.close_command.is_set ?
1296 &close_command :
41b23598
MD
1297 NULL, path);
1298 reply.ret_code = ret_code;
1299 reply.path_length = strlen(path) + 1;
1300 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
1301 if (ret != sizeof(reply)) {
1302 goto error_fatal;
1303 }
1304 ret = lttcomm_send_unix_sock(sock, path, reply.path_length);
1305 if (ret != reply.path_length) {
1306 goto error_fatal;
1307 }
1308 goto end_nosignal;
fc181d72 1309 }
e5148e25 1310 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
fc181d72 1311 {
e5148e25
JG
1312 const uint64_t relayd_id =
1313 msg.u.trace_chunk_exists.relayd_id.value;
1314
1315 ret_code = lttng_consumer_trace_chunk_exists(
1316 msg.u.trace_chunk_exists.relayd_id.is_set ?
1317 &relayd_id : NULL,
1318 msg.u.trace_chunk_exists.session_id,
1319 msg.u.trace_chunk_exists.chunk_id);
1320 goto end_msg_sessiond;
a1ae2ea5 1321 }
3bd1e081 1322 default:
3f8e211f 1323 goto end_nosignal;
3bd1e081 1324 }
3f8e211f 1325
3bd1e081 1326end_nosignal:
4cbc1a04
DG
1327 /*
1328 * Return 1 to indicate success since the 0 value can be a socket
1329 * shutdown during the recv() or send() call.
1330 */
e0e7757d
JG
1331 ret = 1;
1332 goto end;
1333error_fatal:
1334 /* This will issue a consumer stop. */
1335 ret = -1;
1336 goto end;
e5148e25
JG
1337end_msg_sessiond:
1338 /*
1339 * The returned value here is not useful since either way we'll return 1 to
1340 * the caller because the session daemon socket management is done
1341 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1342 */
1343 ret = consumer_send_status_msg(sock, ret_code);
1344 if (ret < 0) {
1345 goto error_fatal;
1346 }
e0e7757d
JG
1347 ret = 1;
1348end:
e5148e25 1349 health_code_update();
1803a064 1350 rcu_read_unlock();
e0e7757d 1351 return ret;
3bd1e081 1352}
d41f73b7 1353
309167d2
JD
1354/*
1355 * Populate index values of a kernel stream. Values are set in big endian order.
1356 *
1357 * Return 0 on success or else a negative value.
1358 */
50adc264 1359static int get_index_values(struct ctf_packet_index *index, int infd)
309167d2
JD
1360{
1361 int ret;
641e8873
JG
1362 uint64_t packet_size, content_size, timestamp_begin, timestamp_end,
1363 events_discarded, stream_id, stream_instance_id,
1364 packet_seq_num;
309167d2 1365
641e8873 1366 ret = kernctl_get_timestamp_begin(infd, &timestamp_begin);
309167d2
JD
1367 if (ret < 0) {
1368 PERROR("kernctl_get_timestamp_begin");
1369 goto error;
1370 }
309167d2 1371
641e8873 1372 ret = kernctl_get_timestamp_end(infd, &timestamp_end);
309167d2
JD
1373 if (ret < 0) {
1374 PERROR("kernctl_get_timestamp_end");
1375 goto error;
1376 }
309167d2 1377
641e8873 1378 ret = kernctl_get_events_discarded(infd, &events_discarded);
309167d2
JD
1379 if (ret < 0) {
1380 PERROR("kernctl_get_events_discarded");
1381 goto error;
1382 }
309167d2 1383
641e8873 1384 ret = kernctl_get_content_size(infd, &content_size);
309167d2
JD
1385 if (ret < 0) {
1386 PERROR("kernctl_get_content_size");
1387 goto error;
1388 }
309167d2 1389
641e8873 1390 ret = kernctl_get_packet_size(infd, &packet_size);
309167d2
JD
1391 if (ret < 0) {
1392 PERROR("kernctl_get_packet_size");
1393 goto error;
1394 }
309167d2 1395
641e8873 1396 ret = kernctl_get_stream_id(infd, &stream_id);
309167d2
JD
1397 if (ret < 0) {
1398 PERROR("kernctl_get_stream_id");
1399 goto error;
1400 }
309167d2 1401
641e8873 1402 ret = kernctl_get_instance_id(infd, &stream_instance_id);
234cd636 1403 if (ret < 0) {
f0b03c22
MD
1404 if (ret == -ENOTTY) {
1405 /* Command not implemented by lttng-modules. */
641e8873 1406 stream_instance_id = -1ULL;
f0b03c22
MD
1407 } else {
1408 PERROR("kernctl_get_instance_id");
1409 goto error;
1410 }
234cd636 1411 }
234cd636 1412
641e8873 1413 ret = kernctl_get_sequence_number(infd, &packet_seq_num);
234cd636 1414 if (ret < 0) {
f0b03c22
MD
1415 if (ret == -ENOTTY) {
1416 /* Command not implemented by lttng-modules. */
641e8873 1417 packet_seq_num = -1ULL;
f0b03c22
MD
1418 ret = 0;
1419 } else {
1420 PERROR("kernctl_get_sequence_number");
1421 goto error;
1422 }
234cd636
JD
1423 }
1424 index->packet_seq_num = htobe64(index->packet_seq_num);
1425
641e8873
JG
1426 *index = (typeof(*index)) {
1427 .offset = index->offset,
1428 .packet_size = htobe64(packet_size),
1429 .content_size = htobe64(content_size),
1430 .timestamp_begin = htobe64(timestamp_begin),
1431 .timestamp_end = htobe64(timestamp_end),
1432 .events_discarded = htobe64(events_discarded),
1433 .stream_id = htobe64(stream_id),
1434 .stream_instance_id = htobe64(stream_instance_id),
1435 .packet_seq_num = htobe64(packet_seq_num),
1436 };
1437
309167d2
JD
1438error:
1439 return ret;
1440}
94d49140
JD
1441/*
1442 * Sync metadata meaning request them to the session daemon and snapshot to the
1443 * metadata thread can consumer them.
1444 *
1445 * Metadata stream lock MUST be acquired.
1446 *
1447 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1448 * is empty or a negative value on error.
1449 */
1450int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1451{
1452 int ret;
1453
1454 assert(metadata);
1455
1456 ret = kernctl_buffer_flush(metadata->wait_fd);
1457 if (ret < 0) {
1458 ERR("Failed to flush kernel stream");
1459 goto end;
1460 }
1461
1462 ret = kernctl_snapshot(metadata->wait_fd);
1463 if (ret < 0) {
32af2c95 1464 if (ret != -EAGAIN) {
94d49140
JD
1465 ERR("Sync metadata, taking kernel snapshot failed.");
1466 goto end;
1467 }
1468 DBG("Sync metadata, no new kernel metadata");
1469 /* No new metadata, exit. */
1470 ret = ENODATA;
1471 goto end;
1472 }
1473
1474end:
1475 return ret;
1476}
309167d2 1477
fb83fe64
JD
1478static
1479int update_stream_stats(struct lttng_consumer_stream *stream)
1480{
1481 int ret;
1482 uint64_t seq, discarded;
1483
1484 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1485 if (ret < 0) {
f0b03c22
MD
1486 if (ret == -ENOTTY) {
1487 /* Command not implemented by lttng-modules. */
1488 seq = -1ULL;
c8eabe73 1489 stream->sequence_number_unavailable = true;
f0b03c22
MD
1490 } else {
1491 PERROR("kernctl_get_sequence_number");
1492 goto end;
1493 }
fb83fe64
JD
1494 }
1495
1496 /*
1497 * Start the sequence when we extract the first packet in case we don't
1498 * start at 0 (for example if a consumer is not connected to the
1499 * session immediately after the beginning).
1500 */
1501 if (stream->last_sequence_number == -1ULL) {
1502 stream->last_sequence_number = seq;
1503 } else if (seq > stream->last_sequence_number) {
1504 stream->chan->lost_packets += seq -
1505 stream->last_sequence_number - 1;
1506 } else {
1507 /* seq <= last_sequence_number */
1508 ERR("Sequence number inconsistent : prev = %" PRIu64
1509 ", current = %" PRIu64,
1510 stream->last_sequence_number, seq);
1511 ret = -1;
1512 goto end;
1513 }
1514 stream->last_sequence_number = seq;
1515
1516 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1517 if (ret < 0) {
1518 PERROR("kernctl_get_events_discarded");
1519 goto end;
1520 }
1521 if (discarded < stream->last_discarded_events) {
1522 /*
83f4233d
MJ
1523 * Overflow has occurred. We assume only one wrap-around
1524 * has occurred.
fb83fe64
JD
1525 */
1526 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1527 stream->last_discarded_events + discarded;
1528 } else {
1529 stream->chan->discarded_events += discarded -
1530 stream->last_discarded_events;
1531 }
1532 stream->last_discarded_events = discarded;
1533 ret = 0;
1534
1535end:
1536 return ret;
1537}
1538
93ec662e
JD
1539/*
1540 * Check if the local version of the metadata stream matches with the version
1541 * of the metadata stream in the kernel. If it was updated, set the reset flag
1542 * on the stream.
1543 */
1544static
1545int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1546{
1547 int ret;
1548 uint64_t cur_version;
1549
1550 ret = kernctl_get_metadata_version(infd, &cur_version);
1551 if (ret < 0) {
f0b03c22
MD
1552 if (ret == -ENOTTY) {
1553 /*
1554 * LTTng-modules does not implement this
1555 * command.
1556 */
1557 ret = 0;
1558 goto end;
1559 }
93ec662e
JD
1560 ERR("Failed to get the metadata version");
1561 goto end;
1562 }
1563
1564 if (stream->metadata_version == cur_version) {
1565 ret = 0;
1566 goto end;
1567 }
1568
1569 DBG("New metadata version detected");
1570 stream->metadata_version = cur_version;
1571 stream->reset_metadata_flag = 1;
1572 ret = 0;
1573
1574end:
1575 return ret;
1576}
1577
d41f73b7
MD
1578/*
1579 * Consume data on a file descriptor and write it on a trace file.
e5148e25 1580 * The stream and channel locks must be held by the caller.
d41f73b7 1581 */
4078b776 1582ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
e5148e25 1583 struct lttng_consumer_local_data *ctx)
d41f73b7 1584{
1d4dfdef 1585 unsigned long len, subbuf_size, padding;
ab1f27f2 1586 int err, write_index = 1;
4078b776 1587 ssize_t ret = 0;
d41f73b7 1588 int infd = stream->wait_fd;
b2d8a465 1589 struct ctf_packet_index index = {};
ab1f27f2 1590 bool in_error_state = false;
d41f73b7
MD
1591
1592 DBG("In read_subbuffer (infd : %d)", infd);
309167d2 1593
02d02e31 1594
d41f73b7
MD
1595 /* Get the next subbuffer */
1596 err = kernctl_get_next_subbuf(infd);
1597 if (err != 0) {
d41f73b7
MD
1598 /*
1599 * This is a debug message even for single-threaded consumer,
1600 * because poll() have more relaxed criterions than get subbuf,
1601 * so get_subbuf may fail for short race windows where poll()
1602 * would issue wakeups.
1603 */
1604 DBG("Reserving sub buffer failed (everything is normal, "
1605 "it is due to concurrency)");
32af2c95 1606 ret = err;
02d02e31 1607 goto error;
d41f73b7
MD
1608 }
1609
1d4dfdef
DG
1610 /* Get the full subbuffer size including padding */
1611 err = kernctl_get_padded_subbuf_size(infd, &len);
1612 if (err != 0) {
5a510c9f 1613 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1614 err = kernctl_put_subbuf(infd);
1615 if (err != 0) {
32af2c95 1616 if (err == -EFAULT) {
5a510c9f 1617 PERROR("Error in unreserving sub buffer\n");
32af2c95 1618 } else if (err == -EIO) {
8265f19e 1619 /* Should never happen with newer LTTng versions */
5a510c9f 1620 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1621 }
32af2c95 1622 ret = err;
02d02e31 1623 goto error;
8265f19e 1624 }
32af2c95 1625 ret = err;
02d02e31 1626 goto error;
1d4dfdef
DG
1627 }
1628
1c20f0e2 1629 if (!stream->metadata_flag) {
309167d2
JD
1630 ret = get_index_values(&index, infd);
1631 if (ret < 0) {
8265f19e
MD
1632 err = kernctl_put_subbuf(infd);
1633 if (err != 0) {
32af2c95 1634 if (err == -EFAULT) {
5a510c9f 1635 PERROR("Error in unreserving sub buffer\n");
32af2c95 1636 } else if (err == -EIO) {
8265f19e 1637 /* Should never happen with newer LTTng versions */
5a510c9f 1638 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1639 }
32af2c95 1640 ret = err;
02d02e31 1641 goto error;
8265f19e 1642 }
02d02e31 1643 goto error;
309167d2 1644 }
fb83fe64
JD
1645 ret = update_stream_stats(stream);
1646 if (ret < 0) {
7b87473d
MD
1647 err = kernctl_put_subbuf(infd);
1648 if (err != 0) {
1649 if (err == -EFAULT) {
1650 PERROR("Error in unreserving sub buffer\n");
1651 } else if (err == -EIO) {
1652 /* Should never happen with newer LTTng versions */
1653 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1654 }
1655 ret = err;
02d02e31 1656 goto error;
7b87473d 1657 }
02d02e31 1658 goto error;
fb83fe64 1659 }
1c20f0e2
JD
1660 } else {
1661 write_index = 0;
93ec662e
JD
1662 ret = metadata_stream_check_version(infd, stream);
1663 if (ret < 0) {
7b87473d
MD
1664 err = kernctl_put_subbuf(infd);
1665 if (err != 0) {
1666 if (err == -EFAULT) {
1667 PERROR("Error in unreserving sub buffer\n");
1668 } else if (err == -EIO) {
1669 /* Should never happen with newer LTTng versions */
1670 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1671 }
1672 ret = err;
02d02e31 1673 goto error;
7b87473d 1674 }
02d02e31 1675 goto error;
93ec662e 1676 }
309167d2
JD
1677 }
1678
ffe60014 1679 switch (stream->chan->output) {
07b86b52 1680 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
1681 /*
1682 * XXX: The lttng-modules splice "actor" does not handle copying
1683 * partial pages hence only using the subbuffer size without the
1684 * padding makes the splice fail.
1685 */
1686 subbuf_size = len;
1687 padding = 0;
1688
1689 /* splice the subbuffer to the tracefile */
91dfef6e 1690 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
309167d2 1691 padding, &index);
91dfef6e
DG
1692 /*
1693 * XXX: Splice does not support network streaming so the return value
1694 * is simply checked against subbuf_size and not like the mmap() op.
1695 */
1d4dfdef
DG
1696 if (ret != subbuf_size) {
1697 /*
1698 * display the error but continue processing to try
1699 * to release the subbuffer
1700 */
1701 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1702 ret, subbuf_size);
309167d2 1703 write_index = 0;
1d4dfdef
DG
1704 }
1705 break;
07b86b52 1706 case CONSUMER_CHANNEL_MMAP:
7775df52
JG
1707 {
1708 const char *subbuf_addr;
b770aa7f 1709 struct lttng_buffer_view subbuf_view;
7775df52 1710
1d4dfdef
DG
1711 /* Get subbuffer size without padding */
1712 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1713 if (err != 0) {
5a510c9f 1714 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1715 err = kernctl_put_subbuf(infd);
1716 if (err != 0) {
32af2c95 1717 if (err == -EFAULT) {
5a510c9f 1718 PERROR("Error in unreserving sub buffer\n");
32af2c95 1719 } else if (err == -EIO) {
8265f19e 1720 /* Should never happen with newer LTTng versions */
5a510c9f 1721 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1722 }
32af2c95 1723 ret = err;
02d02e31 1724 goto error;
8265f19e 1725 }
32af2c95 1726 ret = err;
02d02e31 1727 goto error;
1d4dfdef 1728 }
47e81c02 1729
7775df52
JG
1730 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1731 if (ret) {
1732 goto error_put_subbuf;
1733 }
1734
1d4dfdef
DG
1735 /* Make sure the tracer is not gone mad on us! */
1736 assert(len >= subbuf_size);
1737
1738 padding = len - subbuf_size;
1739
b770aa7f
JG
1740 subbuf_view = lttng_buffer_view_init(subbuf_addr, 0, len);
1741
1d4dfdef 1742 /* write the subbuffer to the tracefile */
b770aa7f
JG
1743 ret = lttng_consumer_on_read_subbuffer_mmap(
1744 ctx, stream, &subbuf_view, padding, &index);
91dfef6e 1745 /*
b770aa7f
JG
1746 * The mmap operation should write subbuf_size amount of data
1747 * when network streaming or the full padding (len) size when we
1748 * are _not_ streaming.
91dfef6e 1749 */
d88aee68
DG
1750 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1751 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 1752 /*
91dfef6e 1753 * Display the error but continue processing to try to release the
2336629e
DG
1754 * subbuffer. This is a DBG statement since this is possible to
1755 * happen without being a critical error.
1d4dfdef 1756 */
2336629e 1757 DBG("Error writing to tracefile "
91dfef6e
DG
1758 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1759 ret, len, subbuf_size);
309167d2 1760 write_index = 0;
1d4dfdef
DG
1761 }
1762 break;
7775df52 1763 }
1d4dfdef
DG
1764 default:
1765 ERR("Unknown output method");
56591bac 1766 ret = -EPERM;
d41f73b7 1767 }
7775df52 1768error_put_subbuf:
d41f73b7
MD
1769 err = kernctl_put_next_subbuf(infd);
1770 if (err != 0) {
32af2c95 1771 if (err == -EFAULT) {
5a510c9f 1772 PERROR("Error in unreserving sub buffer\n");
32af2c95 1773 } else if (err == -EIO) {
d41f73b7 1774 /* Should never happen with newer LTTng versions */
5a510c9f 1775 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
d41f73b7 1776 }
32af2c95 1777 ret = err;
02d02e31 1778 goto error;
ab1f27f2
JG
1779 } else if (in_error_state) {
1780 goto error;
d41f73b7
MD
1781 }
1782
309167d2 1783 /* Write index if needed. */
1c20f0e2 1784 if (!write_index) {
ab1f27f2 1785 goto end;
1c20f0e2
JD
1786 }
1787
94d49140
JD
1788 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1789 /*
1790 * In live, block until all the metadata is sent.
1791 */
c585821b
MD
1792 pthread_mutex_lock(&stream->metadata_timer_lock);
1793 assert(!stream->missed_metadata_flush);
1794 stream->waiting_on_metadata = true;
1795 pthread_mutex_unlock(&stream->metadata_timer_lock);
1796
94d49140 1797 err = consumer_stream_sync_metadata(ctx, stream->session_id);
c585821b
MD
1798
1799 pthread_mutex_lock(&stream->metadata_timer_lock);
1800 stream->waiting_on_metadata = false;
1801 if (stream->missed_metadata_flush) {
1802 stream->missed_metadata_flush = false;
1803 pthread_mutex_unlock(&stream->metadata_timer_lock);
1804 (void) consumer_flush_kernel_index(stream);
1805 } else {
1806 pthread_mutex_unlock(&stream->metadata_timer_lock);
1807 }
94d49140 1808 if (err < 0) {
02d02e31 1809 goto error;
94d49140
JD
1810 }
1811 }
1812
1c20f0e2
JD
1813 err = consumer_stream_write_index(stream, &index);
1814 if (err < 0) {
02d02e31 1815 goto error;
309167d2
JD
1816 }
1817
ab1f27f2 1818end:
02d02e31 1819error:
d41f73b7
MD
1820 return ret;
1821}
1822
1823int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1824{
1825 int ret;
ffe60014
DG
1826
1827 assert(stream);
1828
2bba9e53 1829 /*
e5148e25
JG
1830 * Don't create anything if this is set for streaming or if there is
1831 * no current trace chunk on the parent channel.
2bba9e53 1832 */
e5148e25
JG
1833 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
1834 stream->chan->trace_chunk) {
1835 ret = consumer_stream_create_output_files(stream, true);
1836 if (ret) {
fe4477ee
JD
1837 goto error;
1838 }
ffe60014 1839 }
d41f73b7 1840
d41f73b7
MD
1841 if (stream->output == LTTNG_EVENT_MMAP) {
1842 /* get the len of the mmap region */
1843 unsigned long mmap_len;
1844
1845 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1846 if (ret != 0) {
ffe60014 1847 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
1848 goto error_close_fd;
1849 }
1850 stream->mmap_len = (size_t) mmap_len;
1851
ffe60014
DG
1852 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1853 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 1854 if (stream->mmap_base == MAP_FAILED) {
ffe60014 1855 PERROR("Error mmaping");
d41f73b7
MD
1856 ret = -1;
1857 goto error_close_fd;
1858 }
1859 }
1860
1861 /* we return 0 to let the library handle the FD internally */
1862 return 0;
1863
1864error_close_fd:
2f225ce2 1865 if (stream->out_fd >= 0) {
d41f73b7
MD
1866 int err;
1867
1868 err = close(stream->out_fd);
1869 assert(!err);
2f225ce2 1870 stream->out_fd = -1;
d41f73b7
MD
1871 }
1872error:
1873 return ret;
1874}
1875
ca22feea
DG
1876/*
1877 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1878 * stream. Consumer data lock MUST be acquired before calling this function
1879 * and the stream lock.
ca22feea 1880 *
6d805429 1881 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1882 * data is available for trace viewer reading.
1883 */
6d805429 1884int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1885{
1886 int ret;
1887
1888 assert(stream);
1889
873b9e9a
MD
1890 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1891 ret = 0;
1892 goto end;
1893 }
1894
ca22feea
DG
1895 ret = kernctl_get_next_subbuf(stream->wait_fd);
1896 if (ret == 0) {
1897 /* There is still data so let's put back this subbuffer. */
1898 ret = kernctl_put_subbuf(stream->wait_fd);
1899 assert(ret == 0);
6d805429 1900 ret = 1; /* Data is pending */
4e9a4686 1901 goto end;
ca22feea
DG
1902 }
1903
6d805429
DG
1904 /* Data is NOT pending and ready to be read. */
1905 ret = 0;
ca22feea 1906
6efae65e
DG
1907end:
1908 return ret;
ca22feea 1909}
This page took 0.172809 seconds and 4 git commands to generate.