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