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