Add kernel snapshot support
[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
19#define _GNU_SOURCE
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
990570ed 32#include <common/common.h>
10a8a223 33#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/sessiond-comm/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
acdb9057 37#include <common/pipe.h>
00e2e675 38#include <common/relayd/relayd.h>
fe4477ee 39#include <common/utils.h>
07b86b52 40#include <common/consumer-stream.h>
0857097f 41
10a8a223 42#include "kernel-consumer.h"
3bd1e081
MD
43
44extern struct lttng_consumer_global_data consumer_data;
45extern int consumer_poll_timeout;
46extern volatile int consumer_quit;
47
3bd1e081
MD
48/*
49 * Take a snapshot for a specific fd
50 *
51 * Returns 0 on success, < 0 on error
52 */
ffe60014 53int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
54{
55 int ret = 0;
56 int infd = stream->wait_fd;
57
58 ret = kernctl_snapshot(infd);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
61 perror("Getting sub-buffer snapshot.");
62 }
63
64 return ret;
65}
66
67/*
68 * Get the produced position
69 *
70 * Returns 0 on success, < 0 on error
71 */
ffe60014 72int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
73 unsigned long *pos)
74{
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
87dc6a9c 80 errno = -ret;
3bd1e081
MD
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85}
86
07b86b52
JD
87/*
88 * Get the consumerd position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
93 unsigned long *pos)
94{
95 int ret;
96 int infd = stream->wait_fd;
97
98 ret = kernctl_snapshot_get_consumed(infd, pos);
99 if (ret != 0) {
100 errno = -ret;
101 perror("kernctl_snapshot_get_consumed");
102 }
103
104 return ret;
105}
106
107/*
108 * Find a relayd and send the stream
109 *
110 * Returns 0 on success, < 0 on error
111 */
112static
113int send_relayd_stream(struct lttng_consumer_stream *stream, char *path)
114{
115 struct consumer_relayd_sock_pair *relayd;
116 int ret = 0;
117 char *stream_path;
118
119 if (path != NULL) {
120 stream_path = path;
121 } else {
122 stream_path = stream->chan->pathname;
123 }
124 /* The stream is not metadata. Get relayd reference if exists. */
125 rcu_read_lock();
126 relayd = consumer_find_relayd(stream->net_seq_idx);
127 if (relayd != NULL) {
128 /* Add stream on the relayd */
129 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
130 ret = relayd_add_stream(&relayd->control_sock,
131 stream->name, stream_path,
132 &stream->relayd_stream_id,
133 stream->chan->tracefile_size,
134 stream->chan->tracefile_count);
135 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
136 if (ret < 0) {
137 goto end;
138 }
139 uatomic_inc(&relayd->refcount);
140 } else if (stream->net_seq_idx != (uint64_t) -1ULL) {
141 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
142 stream->net_seq_idx);
143 ret = -1;
144 goto end;
145 }
146
147end:
148 rcu_read_unlock();
149 return ret;
150}
151
152/*
153 * Find a relayd and close the stream
154 */
155static
156void close_relayd_stream(struct lttng_consumer_stream *stream)
157{
158 struct consumer_relayd_sock_pair *relayd;
159
160 /* The stream is not metadata. Get relayd reference if exists. */
161 rcu_read_lock();
162 relayd = consumer_find_relayd(stream->net_seq_idx);
163 if (relayd != NULL) {
164 consumer_stream_relayd_close(stream, relayd);
165 }
166 rcu_read_unlock();
167}
168
169/*
170 * Take a snapshot of all the stream of a channel
171 *
172 * Returns 0 on success, < 0 on error
173 */
174int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
175 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
176{
177 int ret;
178 unsigned long consumed_pos, produced_pos;
179 struct lttng_consumer_channel *channel;
180 struct lttng_consumer_stream *stream;
181
182 DBG("Kernel consumer snapshot channel %lu", key);
183
184 rcu_read_lock();
185
186 channel = consumer_find_channel(key);
187 if (!channel) {
188 ERR("No channel found for key %lu", key);
189 ret = -1;
190 goto end;
191 }
192
193 /* Splice is not supported yet for channel snapshot. */
194 if (channel->output != CONSUMER_CHANNEL_MMAP) {
195 ERR("Unsupported output %d", channel->output);
196 ret = -1;
197 goto end;
198 }
199
200 cds_list_for_each_entry(stream, &channel->stream_no_monitor_list.head,
201 no_monitor_node) {
202 /*
203 * Lock stream because we are about to change its state.
204 */
205 pthread_mutex_lock(&stream->lock);
206
207 stream->net_seq_idx = relayd_id;
208 channel->relayd_id = relayd_id;
209 if (relayd_id != (uint64_t) -1ULL) {
210 ret = send_relayd_stream(stream, path);
211 if (ret < 0) {
212 ERR("sending stream to relayd");
213 goto end_unlock;
214 }
215 DBG("Stream %s sent to the relayd", stream->name);
216 } else {
217 ret = utils_create_stream_file(path, stream->name,
218 stream->chan->tracefile_size, stream->tracefile_count_current,
219 stream->uid, stream->gid);
220 if (ret < 0) {
221 ERR("utils_create_stream_file");
222 goto end_unlock;
223 }
224
225 stream->out_fd = ret;
226 stream->tracefile_size_current = 0;
227
228 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path,
229 stream->name, stream->key);
230 }
231
232 ret = kernctl_buffer_flush(stream->wait_fd);
233 if (ret < 0) {
234 ERR("Failed to flush kernel metadata stream");
235 goto end_unlock;
236 }
237
238 ret = lttng_kconsumer_take_snapshot(stream);
239 if (ret < 0) {
240 ERR("Taking kernel snapshot");
241 goto end_unlock;
242 }
243
244 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
245 if (ret < 0) {
246 ERR("Produced kernel snapshot position");
247 goto end_unlock;
248 }
249
250 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
251 if (ret < 0) {
252 ERR("Consumerd kernel snapshot position");
253 goto end_unlock;
254 }
255
256 if (stream->max_sb_size == 0) {
257 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
258 &stream->max_sb_size);
259 if (ret < 0) {
260 ERR("Getting kernel max_sb_size");
261 goto end_unlock;
262 }
263 }
264
265 while (consumed_pos < produced_pos) {
266 ssize_t read_len;
267 unsigned long len, padded_len;
268
269 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
270
271 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
272 if (ret < 0) {
273 if (errno != EAGAIN) {
274 PERROR("kernctl_get_subbuf snapshot");
275 goto end_unlock;
276 }
277 DBG("Kernel consumer get subbuf failed. Skipping it.");
278 consumed_pos += stream->max_sb_size;
279 continue;
280 }
281
282 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
283 if (ret < 0) {
284 ERR("Snapshot kernctl_get_subbuf_size");
285 goto end_unlock;
286 }
287
288 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
289 if (ret < 0) {
290 ERR("Snapshot kernctl_get_padded_subbuf_size");
291 goto end_unlock;
292 }
293
294 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
295 padded_len - len);
296 /*
297 * We write the padded len in local tracefiles but the
298 * data len when using a relay.
299 * Display the error but continue processing to try to
300 * release the subbuffer.
301 */
302 if (relayd_id != (uint64_t) -1ULL) {
303 if (read_len != len) {
304 ERR("Error sending to the relay (ret: %zd != len: %lu)",
305 read_len, len);
306 }
307 } else {
308 if (read_len != padded_len) {
309 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
310 read_len, padded_len);
311 }
312 }
313
314 ret = kernctl_put_subbuf(stream->wait_fd);
315 if (ret < 0) {
316 ERR("Snapshot kernctl_put_subbuf");
317 goto end_unlock;
318 }
319 consumed_pos += stream->max_sb_size;
320 }
321
322 if (relayd_id == (uint64_t) -1ULL) {
323 ret = close(stream->out_fd);
324 if (ret < 0) {
325 PERROR("Kernel consumer snapshot close out_fd");
326 goto end_unlock;
327 }
328 stream->out_fd = -1;
329 } else {
330 close_relayd_stream(stream);
331 stream->net_seq_idx = (uint64_t) -1ULL;
332 }
333 pthread_mutex_unlock(&stream->lock);
334 }
335
336 /* All good! */
337 ret = 0;
338 goto end;
339
340end_unlock:
341 pthread_mutex_unlock(&stream->lock);
342end:
343 rcu_read_unlock();
344 return ret;
345}
346
347/*
348 * Read the whole metadata available for a snapshot.
349 *
350 * Returns 0 on success, < 0 on error
351 */
352int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
353 struct lttng_consumer_local_data *ctx)
354{
355 struct lttng_consumer_channel *metadata_channel;
356 struct lttng_consumer_stream *metadata_stream;
357 int ret;
358
359 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
360 key, path);
361
362 rcu_read_lock();
363
364 metadata_channel = consumer_find_channel(key);
365 if (!metadata_channel) {
366 ERR("Snapshot kernel metadata channel not found for key %lu", key);
367 ret = -1;
368 goto end;
369 }
370
371 metadata_stream = metadata_channel->metadata_stream;
372 assert(metadata_stream);
373
374 ret = utils_create_stream_file(path, metadata_stream->name,
375 metadata_stream->chan->tracefile_size,
376 metadata_stream->tracefile_count_current,
377 metadata_stream->uid, metadata_stream->gid);
378 if (ret < 0) {
379 goto end;
380 }
381 metadata_stream->out_fd = ret;
382
383 ret = 0;
384 while (ret >= 0) {
385 ret = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
386 if (ret < 0) {
387 if (ret != -EPERM) {
388 ERR("Kernel snapshot reading subbuffer");
389 goto end;
390 }
391 /* "ret" is negative at this point so we will exit the loop. */
392 continue;
393 }
394 }
395
396 ret = 0;
397end:
398 rcu_read_unlock();
399 return ret;
400}
401
1803a064
MD
402/*
403 * Receive command from session daemon and process it.
404 *
405 * Return 1 on success else a negative value or 0.
406 */
3bd1e081
MD
407int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
408 int sock, struct pollfd *consumer_sockpoll)
409{
410 ssize_t ret;
f50f23d9 411 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
412 struct lttcomm_consumer_msg msg;
413
414 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
415 if (ret != sizeof(msg)) {
f73fabfd 416 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
417 if (ret > 0) {
418 ret = -1;
419 }
3bd1e081
MD
420 return ret;
421 }
422 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
423 /*
424 * Notify the session daemon that the command is completed.
425 *
426 * On transport layer error, the function call will print an error
427 * message so handling the returned code is a bit useless since we
428 * return an error code anyway.
429 */
430 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
431 return -ENOENT;
432 }
433
b0b335c8
MD
434 /* relayd needs RCU read-side protection */
435 rcu_read_lock();
436
3bd1e081 437 switch (msg.cmd_type) {
00e2e675
DG
438 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
439 {
f50f23d9 440 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
441 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
442 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 443 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
444 goto end_nosignal;
445 }
3bd1e081
MD
446 case LTTNG_CONSUMER_ADD_CHANNEL:
447 {
448 struct lttng_consumer_channel *new_channel;
e43c41c5 449 int ret_recv;
3bd1e081 450
f50f23d9
DG
451 /* First send a status message before receiving the fds. */
452 ret = consumer_send_status_msg(sock, ret_code);
453 if (ret < 0) {
454 /* Somehow, the session daemon is not responding anymore. */
1803a064 455 goto error_fatal;
f50f23d9 456 }
d88aee68 457 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 458 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
459 msg.u.channel.session_id, msg.u.channel.pathname,
460 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
461 msg.u.channel.relayd_id, msg.u.channel.output,
462 msg.u.channel.tracefile_size,
2bba9e53
DG
463 msg.u.channel.tracefile_count,
464 msg.u.channel.monitor);
3bd1e081 465 if (new_channel == NULL) {
f73fabfd 466 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
467 goto end_nosignal;
468 }
ffe60014
DG
469 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
470
471 /* Translate and save channel type. */
472 switch (msg.u.channel.type) {
473 case CONSUMER_CHANNEL_TYPE_DATA:
474 case CONSUMER_CHANNEL_TYPE_METADATA:
475 new_channel->type = msg.u.channel.type;
476 break;
477 default:
478 assert(0);
479 goto end_nosignal;
480 };
481
3bd1e081 482 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
483 ret_recv = ctx->on_recv_channel(new_channel);
484 if (ret_recv == 0) {
485 ret = consumer_add_channel(new_channel, ctx);
486 } else if (ret_recv < 0) {
3bd1e081
MD
487 goto end_nosignal;
488 }
489 } else {
e43c41c5 490 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 491 }
e43c41c5
JD
492
493 /* If we received an error in add_channel, we need to report it. */
821fffb2 494 if (ret < 0) {
1803a064
MD
495 ret = consumer_send_status_msg(sock, ret);
496 if (ret < 0) {
497 goto error_fatal;
498 }
e43c41c5
JD
499 goto end_nosignal;
500 }
501
3bd1e081
MD
502 goto end_nosignal;
503 }
504 case LTTNG_CONSUMER_ADD_STREAM:
505 {
dae10966
DG
506 int fd;
507 struct lttng_pipe *stream_pipe;
00e2e675 508 struct lttng_consumer_stream *new_stream;
ffe60014 509 struct lttng_consumer_channel *channel;
c80048c6 510 int alloc_ret = 0;
3bd1e081 511
ffe60014
DG
512 /*
513 * Get stream's channel reference. Needed when adding the stream to the
514 * global hash table.
515 */
516 channel = consumer_find_channel(msg.u.stream.channel_key);
517 if (!channel) {
518 /*
519 * We could not find the channel. Can happen if cpu hotplug
520 * happens while tearing down.
521 */
d88aee68 522 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
ffe60014
DG
523 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
524 }
525
f50f23d9
DG
526 /* First send a status message before receiving the fds. */
527 ret = consumer_send_status_msg(sock, ret_code);
1803a064
MD
528 if (ret < 0) {
529 /*
530 * Somehow, the session daemon is not responding
531 * anymore.
532 */
533 goto error_fatal;
534 }
535 if (ret_code != LTTNG_OK) {
ffe60014 536 /*
1803a064 537 * Channel was not found.
ffe60014 538 */
f50f23d9
DG
539 goto end_nosignal;
540 }
541
3bd1e081
MD
542 /* block */
543 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 544 rcu_read_unlock();
3bd1e081
MD
545 return -EINTR;
546 }
00e2e675
DG
547
548 /* Get stream file descriptor from socket */
f2fc6720
MD
549 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
550 if (ret != sizeof(fd)) {
f73fabfd 551 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 552 rcu_read_unlock();
3bd1e081
MD
553 return ret;
554 }
3bd1e081 555
f50f23d9
DG
556 /*
557 * Send status code to session daemon only if the recv works. If the
558 * above recv() failed, the session daemon is notified through the
559 * error socket and the teardown is eventually done.
560 */
561 ret = consumer_send_status_msg(sock, ret_code);
562 if (ret < 0) {
563 /* Somehow, the session daemon is not responding anymore. */
564 goto end_nosignal;
565 }
566
ffe60014
DG
567 new_stream = consumer_allocate_stream(channel->key,
568 fd,
569 LTTNG_CONSUMER_ACTIVE_STREAM,
570 channel->name,
571 channel->uid,
572 channel->gid,
573 channel->relayd_id,
574 channel->session_id,
575 msg.u.stream.cpu,
576 &alloc_ret,
577 channel->type);
3bd1e081 578 if (new_stream == NULL) {
c80048c6
MD
579 switch (alloc_ret) {
580 case -ENOMEM:
581 case -EINVAL:
582 default:
583 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
584 break;
c80048c6 585 }
3f8e211f 586 goto end_nosignal;
3bd1e081 587 }
ffe60014
DG
588 new_stream->chan = channel;
589 new_stream->wait_fd = fd;
07b86b52
JD
590 switch (channel->output) {
591 case CONSUMER_CHANNEL_SPLICE:
592 new_stream->output = LTTNG_EVENT_SPLICE;
593 break;
594 case CONSUMER_CHANNEL_MMAP:
595 new_stream->output = LTTNG_EVENT_MMAP;
596 break;
597 default:
598 ERR("Stream output unknown %d", channel->output);
599 goto end_nosignal;
600 }
00e2e675 601
a0c83db9
DG
602 /*
603 * We've just assigned the channel to the stream so increment the
07b86b52
JD
604 * refcount right now. We don't need to increment the refcount for
605 * streams in no monitor because we handle manually the cleanup of
606 * those. It is very important to make sure there is NO prior
607 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 608 */
07b86b52
JD
609 if (channel->monitor) {
610 uatomic_inc(&new_stream->chan->refcount);
611 }
9d9353f9 612
fb3a43a9
DG
613 /*
614 * The buffer flush is done on the session daemon side for the kernel
615 * so no need for the stream "hangup_flush_done" variable to be
616 * tracked. This is important for a kernel stream since we don't rely
617 * on the flush state of the stream to read data. It's not the case for
618 * user space tracing.
619 */
620 new_stream->hangup_flush_done = 0;
621
07b86b52
JD
622 ret = send_relayd_stream(new_stream, NULL);
623 if (ret < 0) {
e316aad5 624 consumer_del_stream(new_stream, NULL);
3f8e211f 625 goto end_nosignal;
00e2e675
DG
626 }
627
633d0084
DG
628 if (ctx->on_recv_stream) {
629 ret = ctx->on_recv_stream(new_stream);
630 if (ret < 0) {
e316aad5 631 consumer_del_stream(new_stream, NULL);
633d0084 632 goto end_nosignal;
fb3a43a9 633 }
633d0084 634 }
fb3a43a9 635
07b86b52
JD
636 if (new_stream->metadata_flag) {
637 channel->metadata_stream = new_stream;
638 }
639
2bba9e53
DG
640 /* Do not monitor this stream. */
641 if (!channel->monitor) {
6dc3064a
DG
642 DBG("Kernel consumer add stream %s in no monitor mode with"
643 "relayd id %" PRIu64, new_stream->name,
644 new_stream->relayd_stream_id);
07b86b52
JD
645 cds_list_add(&new_stream->no_monitor_node,
646 &channel->stream_no_monitor_list.head);
6dc3064a
DG
647 break;
648 }
649
50f8ae69 650 /* Get the right pipe where the stream will be sent. */
633d0084 651 if (new_stream->metadata_flag) {
dae10966 652 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 653 } else {
dae10966 654 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
655 }
656
dae10966 657 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 658 if (ret < 0) {
dae10966 659 ERR("Consumer write %s stream to pipe %d",
50f8ae69 660 new_stream->metadata_flag ? "metadata" : "data",
dae10966 661 lttng_pipe_get_writefd(stream_pipe));
50f8ae69
DG
662 consumer_del_stream(new_stream, NULL);
663 goto end_nosignal;
3bd1e081 664 }
00e2e675 665
50f8ae69 666 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 667 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
668 break;
669 }
670 case LTTNG_CONSUMER_UPDATE_STREAM:
671 {
3f8e211f
DG
672 rcu_read_unlock();
673 return -ENOSYS;
674 }
675 case LTTNG_CONSUMER_DESTROY_RELAYD:
676 {
a6ba4fe1 677 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
678 struct consumer_relayd_sock_pair *relayd;
679
a6ba4fe1 680 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
681
682 /* Get relayd reference if exists. */
a6ba4fe1 683 relayd = consumer_find_relayd(index);
3f8e211f 684 if (relayd == NULL) {
3448e266 685 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 686 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 687 }
3f8e211f 688
a6ba4fe1
DG
689 /*
690 * Each relayd socket pair has a refcount of stream attached to it
691 * which tells if the relayd is still active or not depending on the
692 * refcount value.
693 *
694 * This will set the destroy flag of the relayd object and destroy it
695 * if the refcount reaches zero when called.
696 *
697 * The destroy can happen either here or when a stream fd hangs up.
698 */
f50f23d9
DG
699 if (relayd) {
700 consumer_flag_relayd_for_destroy(relayd);
701 }
702
703 ret = consumer_send_status_msg(sock, ret_code);
704 if (ret < 0) {
705 /* Somehow, the session daemon is not responding anymore. */
1803a064 706 goto error_fatal;
f50f23d9 707 }
3f8e211f 708
3f8e211f 709 goto end_nosignal;
3bd1e081 710 }
6d805429 711 case LTTNG_CONSUMER_DATA_PENDING:
53632229 712 {
c8f59ee5 713 int32_t ret;
6d805429 714 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 715
6d805429 716 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 717
6d805429 718 ret = consumer_data_pending(id);
c8f59ee5
DG
719
720 /* Send back returned value to session daemon */
721 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
722 if (ret < 0) {
6d805429 723 PERROR("send data pending ret code");
1803a064 724 goto error_fatal;
c8f59ee5 725 }
f50f23d9
DG
726
727 /*
728 * No need to send back a status message since the data pending
729 * returned value is the response.
730 */
c8f59ee5 731 break;
53632229 732 }
6dc3064a
DG
733 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
734 {
07b86b52
JD
735 if (msg.u.snapshot_channel.metadata == 1) {
736 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
737 msg.u.snapshot_channel.pathname, ctx);
738 if (ret < 0) {
739 ERR("Snapshot metadata failed");
740 ret_code = LTTNG_ERR_KERN_META_FAIL;
741 }
742 } else {
743 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
744 msg.u.snapshot_channel.pathname,
745 msg.u.snapshot_channel.relayd_id, ctx);
746 if (ret < 0) {
747 ERR("Snapshot channel failed");
748 ret_code = LTTNG_ERR_KERN_CHAN_FAIL;
749 }
750 }
751
6dc3064a
DG
752 ret = consumer_send_status_msg(sock, ret_code);
753 if (ret < 0) {
754 /* Somehow, the session daemon is not responding anymore. */
755 goto end_nosignal;
756 }
757 break;
758 }
07b86b52
JD
759 case LTTNG_CONSUMER_DESTROY_CHANNEL:
760 {
761 uint64_t key = msg.u.destroy_channel.key;
762 struct lttng_consumer_channel *channel;
763
764 channel = consumer_find_channel(key);
765 if (!channel) {
766 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
767 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
768 }
769
770 ret = consumer_send_status_msg(sock, ret_code);
771 if (ret < 0) {
772 /* Somehow, the session daemon is not responding anymore. */
773 goto end_nosignal;
774 }
775
776 /*
777 * This command should ONLY be issued for channel with streams set in
778 * no monitor mode.
779 */
780 assert(!channel->monitor);
781
782 /*
783 * The refcount should ALWAYS be 0 in the case of a channel in no
784 * monitor mode.
785 */
786 assert(!uatomic_sub_return(&channel->refcount, 1));
787
788 consumer_del_channel(channel);
789
790 goto end_nosignal;
791 }
3bd1e081 792 default:
3f8e211f 793 goto end_nosignal;
3bd1e081 794 }
3f8e211f 795
3bd1e081 796end_nosignal:
b0b335c8 797 rcu_read_unlock();
4cbc1a04
DG
798
799 /*
800 * Return 1 to indicate success since the 0 value can be a socket
801 * shutdown during the recv() or send() call.
802 */
803 return 1;
1803a064
MD
804
805error_fatal:
806 rcu_read_unlock();
807 /* This will issue a consumer stop. */
808 return -1;
3bd1e081 809}
d41f73b7
MD
810
811/*
812 * Consume data on a file descriptor and write it on a trace file.
813 */
4078b776 814ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
815 struct lttng_consumer_local_data *ctx)
816{
1d4dfdef 817 unsigned long len, subbuf_size, padding;
d41f73b7 818 int err;
4078b776 819 ssize_t ret = 0;
d41f73b7
MD
820 int infd = stream->wait_fd;
821
822 DBG("In read_subbuffer (infd : %d)", infd);
823 /* Get the next subbuffer */
824 err = kernctl_get_next_subbuf(infd);
825 if (err != 0) {
1d4dfdef 826 ret = err;
d41f73b7
MD
827 /*
828 * This is a debug message even for single-threaded consumer,
829 * because poll() have more relaxed criterions than get subbuf,
830 * so get_subbuf may fail for short race windows where poll()
831 * would issue wakeups.
832 */
833 DBG("Reserving sub buffer failed (everything is normal, "
834 "it is due to concurrency)");
835 goto end;
836 }
837
1d4dfdef
DG
838 /* Get the full subbuffer size including padding */
839 err = kernctl_get_padded_subbuf_size(infd, &len);
840 if (err != 0) {
841 errno = -err;
842 perror("Getting sub-buffer len failed.");
843 ret = err;
844 goto end;
845 }
846
ffe60014 847 switch (stream->chan->output) {
07b86b52 848 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
849 /*
850 * XXX: The lttng-modules splice "actor" does not handle copying
851 * partial pages hence only using the subbuffer size without the
852 * padding makes the splice fail.
853 */
854 subbuf_size = len;
855 padding = 0;
856
857 /* splice the subbuffer to the tracefile */
91dfef6e
DG
858 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
859 padding);
860 /*
861 * XXX: Splice does not support network streaming so the return value
862 * is simply checked against subbuf_size and not like the mmap() op.
863 */
1d4dfdef
DG
864 if (ret != subbuf_size) {
865 /*
866 * display the error but continue processing to try
867 * to release the subbuffer
868 */
869 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
870 ret, subbuf_size);
871 }
872 break;
07b86b52 873 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
874 /* Get subbuffer size without padding */
875 err = kernctl_get_subbuf_size(infd, &subbuf_size);
876 if (err != 0) {
877 errno = -err;
878 perror("Getting sub-buffer len failed.");
879 ret = err;
880 goto end;
881 }
47e81c02 882
1d4dfdef
DG
883 /* Make sure the tracer is not gone mad on us! */
884 assert(len >= subbuf_size);
885
886 padding = len - subbuf_size;
887
888 /* write the subbuffer to the tracefile */
91dfef6e
DG
889 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
890 padding);
891 /*
892 * The mmap operation should write subbuf_size amount of data when
893 * network streaming or the full padding (len) size when we are _not_
894 * streaming.
895 */
d88aee68
DG
896 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
897 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 898 /*
91dfef6e
DG
899 * Display the error but continue processing to try to release the
900 * subbuffer
1d4dfdef 901 */
91dfef6e
DG
902 ERR("Error writing to tracefile "
903 "(ret: %zd != len: %lu != subbuf_size: %lu)",
904 ret, len, subbuf_size);
1d4dfdef
DG
905 }
906 break;
907 default:
908 ERR("Unknown output method");
909 ret = -1;
d41f73b7
MD
910 }
911
912 err = kernctl_put_next_subbuf(infd);
913 if (err != 0) {
21073eaa 914 errno = -err;
d41f73b7
MD
915 if (errno == EFAULT) {
916 perror("Error in unreserving sub buffer\n");
917 } else if (errno == EIO) {
918 /* Should never happen with newer LTTng versions */
919 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
920 }
21073eaa
DG
921
922 ret = -err;
d41f73b7
MD
923 goto end;
924 }
925
926end:
927 return ret;
928}
929
930int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
931{
932 int ret;
ffe60014
DG
933
934 assert(stream);
935
2bba9e53
DG
936 /*
937 * Don't create anything if this is set for streaming or should not be
938 * monitored.
939 */
940 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
941 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
942 stream->chan->tracefile_size, stream->tracefile_count_current,
943 stream->uid, stream->gid);
944 if (ret < 0) {
945 goto error;
946 }
947 stream->out_fd = ret;
948 stream->tracefile_size_current = 0;
ffe60014 949 }
d41f73b7 950
d41f73b7
MD
951 if (stream->output == LTTNG_EVENT_MMAP) {
952 /* get the len of the mmap region */
953 unsigned long mmap_len;
954
955 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
956 if (ret != 0) {
87dc6a9c 957 errno = -ret;
ffe60014 958 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
959 goto error_close_fd;
960 }
961 stream->mmap_len = (size_t) mmap_len;
962
ffe60014
DG
963 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
964 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 965 if (stream->mmap_base == MAP_FAILED) {
ffe60014 966 PERROR("Error mmaping");
d41f73b7
MD
967 ret = -1;
968 goto error_close_fd;
969 }
970 }
971
972 /* we return 0 to let the library handle the FD internally */
973 return 0;
974
975error_close_fd:
976 {
977 int err;
978
979 err = close(stream->out_fd);
980 assert(!err);
981 }
982error:
983 return ret;
984}
985
ca22feea
DG
986/*
987 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
988 * stream. Consumer data lock MUST be acquired before calling this function
989 * and the stream lock.
ca22feea 990 *
6d805429 991 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
992 * data is available for trace viewer reading.
993 */
6d805429 994int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
995{
996 int ret;
997
998 assert(stream);
999
ca22feea
DG
1000 ret = kernctl_get_next_subbuf(stream->wait_fd);
1001 if (ret == 0) {
1002 /* There is still data so let's put back this subbuffer. */
1003 ret = kernctl_put_subbuf(stream->wait_fd);
1004 assert(ret == 0);
6d805429 1005 ret = 1; /* Data is pending */
4e9a4686 1006 goto end;
ca22feea
DG
1007 }
1008
6d805429
DG
1009 /* Data is NOT pending and ready to be read. */
1010 ret = 0;
ca22feea 1011
6efae65e
DG
1012end:
1013 return ret;
ca22feea 1014}
This page took 0.087033 seconds and 5 git commands to generate.