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