consumer: fix: unaligned accesses to index fields
[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
247e5c44 238 while ((long) (consumed_pos - produced_pos) < 0) {
07b86b52
JD
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 */
f36acc1a 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;
f36acc1a 353 goto error_no_channel;
07b86b52
JD
354 }
355
356 metadata_stream = metadata_channel->metadata_stream;
357 assert(metadata_stream);
f36acc1a 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) {
f36acc1a 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) {
f36acc1a 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);
f36acc1a
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;
f36acc1a
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;
f36acc1a 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. */
7735ef9e
DG
462 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
463 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
d3e2ba59
JD
464 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
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;
18a9df27
JG
1055 uint64_t packet_size, content_size, timestamp_begin, timestamp_end,
1056 events_discarded, stream_id, stream_instance_id,
1057 packet_seq_num;
309167d2 1058
18a9df27 1059 ret = kernctl_get_timestamp_begin(infd, &timestamp_begin);
309167d2
JD
1060 if (ret < 0) {
1061 PERROR("kernctl_get_timestamp_begin");
1062 goto error;
1063 }
309167d2 1064
18a9df27 1065 ret = kernctl_get_timestamp_end(infd, &timestamp_end);
309167d2
JD
1066 if (ret < 0) {
1067 PERROR("kernctl_get_timestamp_end");
1068 goto error;
1069 }
309167d2 1070
18a9df27 1071 ret = kernctl_get_events_discarded(infd, &events_discarded);
309167d2
JD
1072 if (ret < 0) {
1073 PERROR("kernctl_get_events_discarded");
1074 goto error;
1075 }
309167d2 1076
18a9df27 1077 ret = kernctl_get_content_size(infd, &content_size);
309167d2
JD
1078 if (ret < 0) {
1079 PERROR("kernctl_get_content_size");
1080 goto error;
1081 }
309167d2 1082
18a9df27 1083 ret = kernctl_get_packet_size(infd, &packet_size);
309167d2
JD
1084 if (ret < 0) {
1085 PERROR("kernctl_get_packet_size");
1086 goto error;
1087 }
309167d2 1088
18a9df27 1089 ret = kernctl_get_stream_id(infd, &stream_id);
309167d2
JD
1090 if (ret < 0) {
1091 PERROR("kernctl_get_stream_id");
1092 goto error;
1093 }
309167d2 1094
18a9df27 1095 ret = kernctl_get_instance_id(infd, &stream_instance_id);
234cd636 1096 if (ret < 0) {
f0b03c22
MD
1097 if (ret == -ENOTTY) {
1098 /* Command not implemented by lttng-modules. */
18a9df27 1099 stream_instance_id = -1ULL;
f0b03c22
MD
1100 ret = 0;
1101 } else {
1102 PERROR("kernctl_get_instance_id");
1103 goto error;
1104 }
234cd636 1105 }
234cd636 1106
18a9df27 1107 ret = kernctl_get_sequence_number(infd, &packet_seq_num);
234cd636 1108 if (ret < 0) {
f0b03c22
MD
1109 if (ret == -ENOTTY) {
1110 /* Command not implemented by lttng-modules. */
18a9df27 1111 packet_seq_num = -1ULL;
f0b03c22
MD
1112 ret = 0;
1113 } else {
1114 PERROR("kernctl_get_sequence_number");
1115 goto error;
1116 }
234cd636
JD
1117 }
1118 index->packet_seq_num = htobe64(index->packet_seq_num);
1119
18a9df27
JG
1120 *index = (typeof(*index)) {
1121 .offset = index->offset,
1122 .packet_size = htobe64(packet_size),
1123 .content_size = htobe64(content_size),
1124 .timestamp_begin = htobe64(timestamp_begin),
1125 .timestamp_end = htobe64(timestamp_end),
1126 .events_discarded = htobe64(events_discarded),
1127 .stream_id = htobe64(stream_id),
1128 .stream_instance_id = htobe64(stream_instance_id),
1129 .packet_seq_num = htobe64(packet_seq_num),
1130 };
1131
309167d2
JD
1132error:
1133 return ret;
1134}
94d49140
JD
1135/*
1136 * Sync metadata meaning request them to the session daemon and snapshot to the
1137 * metadata thread can consumer them.
1138 *
1139 * Metadata stream lock MUST be acquired.
1140 *
1141 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1142 * is empty or a negative value on error.
1143 */
1144int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1145{
1146 int ret;
1147
1148 assert(metadata);
1149
1150 ret = kernctl_buffer_flush(metadata->wait_fd);
1151 if (ret < 0) {
1152 ERR("Failed to flush kernel stream");
1153 goto end;
1154 }
1155
1156 ret = kernctl_snapshot(metadata->wait_fd);
1157 if (ret < 0) {
32af2c95 1158 if (ret != -EAGAIN) {
94d49140
JD
1159 ERR("Sync metadata, taking kernel snapshot failed.");
1160 goto end;
1161 }
1162 DBG("Sync metadata, no new kernel metadata");
1163 /* No new metadata, exit. */
1164 ret = ENODATA;
1165 goto end;
1166 }
1167
1168end:
1169 return ret;
1170}
309167d2 1171
fb83fe64
JD
1172static
1173int update_stream_stats(struct lttng_consumer_stream *stream)
1174{
1175 int ret;
1176 uint64_t seq, discarded;
1177
1178 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1179 if (ret < 0) {
f0b03c22
MD
1180 if (ret == -ENOTTY) {
1181 /* Command not implemented by lttng-modules. */
1182 seq = -1ULL;
1183 ret = 0;
1184 } else {
1185 PERROR("kernctl_get_sequence_number");
1186 goto end;
1187 }
fb83fe64
JD
1188 }
1189
1190 /*
1191 * Start the sequence when we extract the first packet in case we don't
1192 * start at 0 (for example if a consumer is not connected to the
1193 * session immediately after the beginning).
1194 */
1195 if (stream->last_sequence_number == -1ULL) {
1196 stream->last_sequence_number = seq;
1197 } else if (seq > stream->last_sequence_number) {
1198 stream->chan->lost_packets += seq -
1199 stream->last_sequence_number - 1;
1200 } else {
1201 /* seq <= last_sequence_number */
1202 ERR("Sequence number inconsistent : prev = %" PRIu64
1203 ", current = %" PRIu64,
1204 stream->last_sequence_number, seq);
1205 ret = -1;
1206 goto end;
1207 }
1208 stream->last_sequence_number = seq;
1209
1210 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1211 if (ret < 0) {
1212 PERROR("kernctl_get_events_discarded");
1213 goto end;
1214 }
1215 if (discarded < stream->last_discarded_events) {
1216 /*
83f4233d
MJ
1217 * Overflow has occurred. We assume only one wrap-around
1218 * has occurred.
fb83fe64
JD
1219 */
1220 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1221 stream->last_discarded_events + discarded;
1222 } else {
1223 stream->chan->discarded_events += discarded -
1224 stream->last_discarded_events;
1225 }
1226 stream->last_discarded_events = discarded;
1227 ret = 0;
1228
1229end:
1230 return ret;
1231}
1232
93ec662e
JD
1233/*
1234 * Check if the local version of the metadata stream matches with the version
1235 * of the metadata stream in the kernel. If it was updated, set the reset flag
1236 * on the stream.
1237 */
1238static
1239int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1240{
1241 int ret;
1242 uint64_t cur_version;
1243
1244 ret = kernctl_get_metadata_version(infd, &cur_version);
1245 if (ret < 0) {
f0b03c22
MD
1246 if (ret == -ENOTTY) {
1247 /*
1248 * LTTng-modules does not implement this
1249 * command.
1250 */
1251 ret = 0;
1252 goto end;
1253 }
93ec662e
JD
1254 ERR("Failed to get the metadata version");
1255 goto end;
1256 }
1257
1258 if (stream->metadata_version == cur_version) {
1259 ret = 0;
1260 goto end;
1261 }
1262
1263 DBG("New metadata version detected");
1264 stream->metadata_version = cur_version;
1265 stream->reset_metadata_flag = 1;
1266 ret = 0;
1267
1268end:
1269 return ret;
1270}
1271
d41f73b7
MD
1272/*
1273 * Consume data on a file descriptor and write it on a trace file.
1274 */
4078b776 1275ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
1276 struct lttng_consumer_local_data *ctx)
1277{
1d4dfdef 1278 unsigned long len, subbuf_size, padding;
1c20f0e2 1279 int err, write_index = 1;
4078b776 1280 ssize_t ret = 0;
d41f73b7 1281 int infd = stream->wait_fd;
50adc264 1282 struct ctf_packet_index index;
d41f73b7
MD
1283
1284 DBG("In read_subbuffer (infd : %d)", infd);
309167d2 1285
d41f73b7
MD
1286 /* Get the next subbuffer */
1287 err = kernctl_get_next_subbuf(infd);
1288 if (err != 0) {
d41f73b7
MD
1289 /*
1290 * This is a debug message even for single-threaded consumer,
1291 * because poll() have more relaxed criterions than get subbuf,
1292 * so get_subbuf may fail for short race windows where poll()
1293 * would issue wakeups.
1294 */
1295 DBG("Reserving sub buffer failed (everything is normal, "
1296 "it is due to concurrency)");
32af2c95 1297 ret = err;
d41f73b7
MD
1298 goto end;
1299 }
1300
1d4dfdef
DG
1301 /* Get the full subbuffer size including padding */
1302 err = kernctl_get_padded_subbuf_size(infd, &len);
1303 if (err != 0) {
5a510c9f 1304 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1305 err = kernctl_put_subbuf(infd);
1306 if (err != 0) {
32af2c95 1307 if (err == -EFAULT) {
5a510c9f 1308 PERROR("Error in unreserving sub buffer\n");
32af2c95 1309 } else if (err == -EIO) {
8265f19e 1310 /* Should never happen with newer LTTng versions */
5a510c9f 1311 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1312 }
32af2c95 1313 ret = err;
8265f19e
MD
1314 goto end;
1315 }
32af2c95 1316 ret = err;
1d4dfdef
DG
1317 goto end;
1318 }
1319
1c20f0e2 1320 if (!stream->metadata_flag) {
309167d2
JD
1321 ret = get_index_values(&index, infd);
1322 if (ret < 0) {
8265f19e
MD
1323 err = kernctl_put_subbuf(infd);
1324 if (err != 0) {
32af2c95 1325 if (err == -EFAULT) {
5a510c9f 1326 PERROR("Error in unreserving sub buffer\n");
32af2c95 1327 } else if (err == -EIO) {
8265f19e 1328 /* Should never happen with newer LTTng versions */
5a510c9f 1329 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1330 }
32af2c95 1331 ret = err;
8265f19e
MD
1332 goto end;
1333 }
309167d2
JD
1334 goto end;
1335 }
fb83fe64
JD
1336 ret = update_stream_stats(stream);
1337 if (ret < 0) {
becac7c4
MD
1338 err = kernctl_put_subbuf(infd);
1339 if (err != 0) {
1340 if (err == -EFAULT) {
1341 PERROR("Error in unreserving sub buffer\n");
1342 } else if (err == -EIO) {
1343 /* Should never happen with newer LTTng versions */
1344 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1345 }
1346 ret = err;
1347 goto end;
1348 }
fb83fe64
JD
1349 goto end;
1350 }
1c20f0e2
JD
1351 } else {
1352 write_index = 0;
93ec662e
JD
1353 ret = metadata_stream_check_version(infd, stream);
1354 if (ret < 0) {
becac7c4
MD
1355 err = kernctl_put_subbuf(infd);
1356 if (err != 0) {
1357 if (err == -EFAULT) {
1358 PERROR("Error in unreserving sub buffer\n");
1359 } else if (err == -EIO) {
1360 /* Should never happen with newer LTTng versions */
1361 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1362 }
1363 ret = err;
1364 goto end;
1365 }
93ec662e
JD
1366 goto end;
1367 }
309167d2
JD
1368 }
1369
ffe60014 1370 switch (stream->chan->output) {
07b86b52 1371 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
1372 /*
1373 * XXX: The lttng-modules splice "actor" does not handle copying
1374 * partial pages hence only using the subbuffer size without the
1375 * padding makes the splice fail.
1376 */
1377 subbuf_size = len;
1378 padding = 0;
1379
1380 /* splice the subbuffer to the tracefile */
91dfef6e 1381 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
309167d2 1382 padding, &index);
91dfef6e
DG
1383 /*
1384 * XXX: Splice does not support network streaming so the return value
1385 * is simply checked against subbuf_size and not like the mmap() op.
1386 */
1d4dfdef
DG
1387 if (ret != subbuf_size) {
1388 /*
1389 * display the error but continue processing to try
1390 * to release the subbuffer
1391 */
1392 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1393 ret, subbuf_size);
309167d2 1394 write_index = 0;
1d4dfdef
DG
1395 }
1396 break;
07b86b52 1397 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
1398 /* Get subbuffer size without padding */
1399 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1400 if (err != 0) {
5a510c9f 1401 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1402 err = kernctl_put_subbuf(infd);
1403 if (err != 0) {
32af2c95 1404 if (err == -EFAULT) {
5a510c9f 1405 PERROR("Error in unreserving sub buffer\n");
32af2c95 1406 } else if (err == -EIO) {
8265f19e 1407 /* Should never happen with newer LTTng versions */
5a510c9f 1408 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1409 }
32af2c95 1410 ret = err;
8265f19e
MD
1411 goto end;
1412 }
32af2c95 1413 ret = err;
1d4dfdef
DG
1414 goto end;
1415 }
47e81c02 1416
1d4dfdef
DG
1417 /* Make sure the tracer is not gone mad on us! */
1418 assert(len >= subbuf_size);
1419
1420 padding = len - subbuf_size;
1421
1422 /* write the subbuffer to the tracefile */
91dfef6e 1423 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
309167d2 1424 padding, &index);
91dfef6e
DG
1425 /*
1426 * The mmap operation should write subbuf_size amount of data when
1427 * network streaming or the full padding (len) size when we are _not_
1428 * streaming.
1429 */
d88aee68
DG
1430 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1431 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 1432 /*
91dfef6e 1433 * Display the error but continue processing to try to release the
2336629e
DG
1434 * subbuffer. This is a DBG statement since this is possible to
1435 * happen without being a critical error.
1d4dfdef 1436 */
2336629e 1437 DBG("Error writing to tracefile "
91dfef6e
DG
1438 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1439 ret, len, subbuf_size);
309167d2 1440 write_index = 0;
1d4dfdef
DG
1441 }
1442 break;
1443 default:
1444 ERR("Unknown output method");
56591bac 1445 ret = -EPERM;
d41f73b7
MD
1446 }
1447
1448 err = kernctl_put_next_subbuf(infd);
1449 if (err != 0) {
32af2c95 1450 if (err == -EFAULT) {
5a510c9f 1451 PERROR("Error in unreserving sub buffer\n");
32af2c95 1452 } else if (err == -EIO) {
d41f73b7 1453 /* Should never happen with newer LTTng versions */
5a510c9f 1454 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
d41f73b7 1455 }
32af2c95 1456 ret = err;
d41f73b7
MD
1457 goto end;
1458 }
1459
309167d2 1460 /* Write index if needed. */
1c20f0e2
JD
1461 if (!write_index) {
1462 goto end;
1463 }
1464
94d49140
JD
1465 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1466 /*
1467 * In live, block until all the metadata is sent.
1468 */
c585821b
MD
1469 pthread_mutex_lock(&stream->metadata_timer_lock);
1470 assert(!stream->missed_metadata_flush);
1471 stream->waiting_on_metadata = true;
1472 pthread_mutex_unlock(&stream->metadata_timer_lock);
1473
94d49140 1474 err = consumer_stream_sync_metadata(ctx, stream->session_id);
c585821b
MD
1475
1476 pthread_mutex_lock(&stream->metadata_timer_lock);
1477 stream->waiting_on_metadata = false;
1478 if (stream->missed_metadata_flush) {
1479 stream->missed_metadata_flush = false;
1480 pthread_mutex_unlock(&stream->metadata_timer_lock);
1481 (void) consumer_flush_kernel_index(stream);
1482 } else {
1483 pthread_mutex_unlock(&stream->metadata_timer_lock);
1484 }
94d49140
JD
1485 if (err < 0) {
1486 goto end;
1487 }
1488 }
1489
1c20f0e2
JD
1490 err = consumer_stream_write_index(stream, &index);
1491 if (err < 0) {
1492 goto end;
309167d2
JD
1493 }
1494
d41f73b7
MD
1495end:
1496 return ret;
1497}
1498
1499int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1500{
1501 int ret;
ffe60014
DG
1502
1503 assert(stream);
1504
2bba9e53
DG
1505 /*
1506 * Don't create anything if this is set for streaming or should not be
1507 * monitored.
1508 */
1509 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
1510 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1511 stream->chan->tracefile_size, stream->tracefile_count_current,
309167d2 1512 stream->uid, stream->gid, NULL);
fe4477ee
JD
1513 if (ret < 0) {
1514 goto error;
1515 }
1516 stream->out_fd = ret;
1517 stream->tracefile_size_current = 0;
309167d2
JD
1518
1519 if (!stream->metadata_flag) {
e0547b83
MD
1520 struct lttng_index_file *index_file;
1521
1522 index_file = lttng_index_file_create(stream->chan->pathname,
309167d2
JD
1523 stream->name, stream->uid, stream->gid,
1524 stream->chan->tracefile_size,
e0547b83
MD
1525 stream->tracefile_count_current,
1526 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1527 if (!index_file) {
309167d2
JD
1528 goto error;
1529 }
e0547b83 1530 stream->index_file = index_file;
309167d2 1531 }
ffe60014 1532 }
d41f73b7 1533
d41f73b7
MD
1534 if (stream->output == LTTNG_EVENT_MMAP) {
1535 /* get the len of the mmap region */
1536 unsigned long mmap_len;
1537
1538 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1539 if (ret != 0) {
ffe60014 1540 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
1541 goto error_close_fd;
1542 }
1543 stream->mmap_len = (size_t) mmap_len;
1544
ffe60014
DG
1545 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1546 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 1547 if (stream->mmap_base == MAP_FAILED) {
ffe60014 1548 PERROR("Error mmaping");
d41f73b7
MD
1549 ret = -1;
1550 goto error_close_fd;
1551 }
1552 }
1553
1554 /* we return 0 to let the library handle the FD internally */
1555 return 0;
1556
1557error_close_fd:
2f225ce2 1558 if (stream->out_fd >= 0) {
d41f73b7
MD
1559 int err;
1560
1561 err = close(stream->out_fd);
1562 assert(!err);
2f225ce2 1563 stream->out_fd = -1;
d41f73b7
MD
1564 }
1565error:
1566 return ret;
1567}
1568
ca22feea
DG
1569/*
1570 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1571 * stream. Consumer data lock MUST be acquired before calling this function
1572 * and the stream lock.
ca22feea 1573 *
6d805429 1574 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1575 * data is available for trace viewer reading.
1576 */
6d805429 1577int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1578{
1579 int ret;
1580
1581 assert(stream);
1582
873b9e9a
MD
1583 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1584 ret = 0;
1585 goto end;
1586 }
1587
ca22feea
DG
1588 ret = kernctl_get_next_subbuf(stream->wait_fd);
1589 if (ret == 0) {
1590 /* There is still data so let's put back this subbuffer. */
1591 ret = kernctl_put_subbuf(stream->wait_fd);
1592 assert(ret == 0);
6d805429 1593 ret = 1; /* Data is pending */
4e9a4686 1594 goto end;
ca22feea
DG
1595 }
1596
6d805429
DG
1597 /* Data is NOT pending and ready to be read. */
1598 ret = 0;
ca22feea 1599
6efae65e
DG
1600end:
1601 return ret;
ca22feea 1602}
This page took 0.13629 seconds and 4 git commands to generate.