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