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