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