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