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