Implement snapshot commands in lttng-sessiond
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
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>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40
41 #include "kernel-consumer.h"
42
43 extern struct lttng_consumer_global_data consumer_data;
44 extern int consumer_poll_timeout;
45 extern volatile int consumer_quit;
46
47 /*
48 * Take a snapshot for a specific fd
49 *
50 * Returns 0 on success, < 0 on error
51 */
52 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
53 {
54 int ret = 0;
55 int infd = stream->wait_fd;
56
57 ret = kernctl_snapshot(infd);
58 if (ret != 0) {
59 errno = -ret;
60 perror("Getting sub-buffer snapshot.");
61 }
62
63 return ret;
64 }
65
66 /*
67 * Get the produced position
68 *
69 * Returns 0 on success, < 0 on error
70 */
71 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
72 unsigned long *pos)
73 {
74 int ret;
75 int infd = stream->wait_fd;
76
77 ret = kernctl_snapshot_get_produced(infd, pos);
78 if (ret != 0) {
79 errno = -ret;
80 perror("kernctl_snapshot_get_produced");
81 }
82
83 return ret;
84 }
85
86 /*
87 * Receive command from session daemon and process it.
88 *
89 * Return 1 on success else a negative value or 0.
90 */
91 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
92 int sock, struct pollfd *consumer_sockpoll)
93 {
94 ssize_t ret;
95 enum lttng_error_code ret_code = LTTNG_OK;
96 struct lttcomm_consumer_msg msg;
97
98 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
99 if (ret != sizeof(msg)) {
100 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
101 if (ret > 0) {
102 ret = -1;
103 }
104 return ret;
105 }
106 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
107 /*
108 * Notify the session daemon that the command is completed.
109 *
110 * On transport layer error, the function call will print an error
111 * message so handling the returned code is a bit useless since we
112 * return an error code anyway.
113 */
114 (void) consumer_send_status_msg(sock, ret_code);
115 return -ENOENT;
116 }
117
118 /* relayd needs RCU read-side protection */
119 rcu_read_lock();
120
121 switch (msg.cmd_type) {
122 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
123 {
124 /* Session daemon status message are handled in the following call. */
125 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
126 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
127 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
128 goto end_nosignal;
129 }
130 case LTTNG_CONSUMER_ADD_CHANNEL:
131 {
132 struct lttng_consumer_channel *new_channel;
133 int ret_recv;
134
135 /* First send a status message before receiving the fds. */
136 ret = consumer_send_status_msg(sock, ret_code);
137 if (ret < 0) {
138 /* Somehow, the session daemon is not responding anymore. */
139 goto error_fatal;
140 }
141 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
142 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
143 msg.u.channel.session_id, msg.u.channel.pathname,
144 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
145 msg.u.channel.relayd_id, msg.u.channel.output,
146 msg.u.channel.tracefile_size,
147 msg.u.channel.tracefile_count);
148 if (new_channel == NULL) {
149 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
150 goto end_nosignal;
151 }
152 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
153
154 /* Translate and save channel type. */
155 switch (msg.u.channel.type) {
156 case CONSUMER_CHANNEL_TYPE_DATA:
157 case CONSUMER_CHANNEL_TYPE_METADATA:
158 new_channel->type = msg.u.channel.type;
159 break;
160 default:
161 assert(0);
162 goto end_nosignal;
163 };
164
165 if (ctx->on_recv_channel != NULL) {
166 ret_recv = ctx->on_recv_channel(new_channel);
167 if (ret_recv == 0) {
168 ret = consumer_add_channel(new_channel, ctx);
169 } else if (ret_recv < 0) {
170 goto end_nosignal;
171 }
172 } else {
173 ret = consumer_add_channel(new_channel, ctx);
174 }
175
176 /* If we received an error in add_channel, we need to report it. */
177 if (ret < 0) {
178 ret = consumer_send_status_msg(sock, ret);
179 if (ret < 0) {
180 goto error_fatal;
181 }
182 goto end_nosignal;
183 }
184
185 goto end_nosignal;
186 }
187 case LTTNG_CONSUMER_ADD_STREAM:
188 {
189 int fd;
190 struct lttng_pipe *stream_pipe;
191 struct consumer_relayd_sock_pair *relayd = NULL;
192 struct lttng_consumer_stream *new_stream;
193 struct lttng_consumer_channel *channel;
194 int alloc_ret = 0;
195
196 /*
197 * Get stream's channel reference. Needed when adding the stream to the
198 * global hash table.
199 */
200 channel = consumer_find_channel(msg.u.stream.channel_key);
201 if (!channel) {
202 /*
203 * We could not find the channel. Can happen if cpu hotplug
204 * happens while tearing down.
205 */
206 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
207 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
208 }
209
210 /* First send a status message before receiving the fds. */
211 ret = consumer_send_status_msg(sock, ret_code);
212 if (ret < 0) {
213 /*
214 * Somehow, the session daemon is not responding
215 * anymore.
216 */
217 goto error_fatal;
218 }
219 if (ret_code != LTTNG_OK) {
220 /*
221 * Channel was not found.
222 */
223 goto end_nosignal;
224 }
225
226 /* block */
227 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
228 rcu_read_unlock();
229 return -EINTR;
230 }
231
232 /* Get stream file descriptor from socket */
233 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
234 if (ret != sizeof(fd)) {
235 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
236 rcu_read_unlock();
237 return ret;
238 }
239
240 /*
241 * Send status code to session daemon only if the recv works. If the
242 * above recv() failed, the session daemon is notified through the
243 * error socket and the teardown is eventually done.
244 */
245 ret = consumer_send_status_msg(sock, ret_code);
246 if (ret < 0) {
247 /* Somehow, the session daemon is not responding anymore. */
248 goto end_nosignal;
249 }
250
251 new_stream = consumer_allocate_stream(channel->key,
252 fd,
253 LTTNG_CONSUMER_ACTIVE_STREAM,
254 channel->name,
255 channel->uid,
256 channel->gid,
257 channel->relayd_id,
258 channel->session_id,
259 msg.u.stream.cpu,
260 &alloc_ret,
261 channel->type);
262 if (new_stream == NULL) {
263 switch (alloc_ret) {
264 case -ENOMEM:
265 case -EINVAL:
266 default:
267 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
268 break;
269 }
270 goto end_nosignal;
271 }
272 new_stream->chan = channel;
273 new_stream->wait_fd = fd;
274
275 /*
276 * We've just assigned the channel to the stream so increment the
277 * refcount right now.
278 */
279 uatomic_inc(&new_stream->chan->refcount);
280
281 /*
282 * The buffer flush is done on the session daemon side for the kernel
283 * so no need for the stream "hangup_flush_done" variable to be
284 * tracked. This is important for a kernel stream since we don't rely
285 * on the flush state of the stream to read data. It's not the case for
286 * user space tracing.
287 */
288 new_stream->hangup_flush_done = 0;
289
290 /* The stream is not metadata. Get relayd reference if exists. */
291 relayd = consumer_find_relayd(new_stream->net_seq_idx);
292 if (relayd != NULL) {
293 /* Add stream on the relayd */
294 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
295 ret = relayd_add_stream(&relayd->control_sock,
296 new_stream->name, new_stream->chan->pathname,
297 &new_stream->relayd_stream_id,
298 new_stream->chan->tracefile_size,
299 new_stream->chan->tracefile_count);
300 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
301 if (ret < 0) {
302 consumer_del_stream(new_stream, NULL);
303 goto end_nosignal;
304 }
305 } else if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
306 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
307 new_stream->net_seq_idx);
308 consumer_del_stream(new_stream, NULL);
309 goto end_nosignal;
310 }
311
312 if (ctx->on_recv_stream) {
313 ret = ctx->on_recv_stream(new_stream);
314 if (ret < 0) {
315 consumer_del_stream(new_stream, NULL);
316 goto end_nosignal;
317 }
318 }
319
320 if (msg.u.stream.no_monitor) {
321 DBG("Kernel consumer add stream %s in no monitor mode with"
322 "relayd id %" PRIu64, new_stream->name,
323 new_stream->relayd_stream_id);
324 break;
325 }
326
327 /* Get the right pipe where the stream will be sent. */
328 if (new_stream->metadata_flag) {
329 stream_pipe = ctx->consumer_metadata_pipe;
330 } else {
331 stream_pipe = ctx->consumer_data_pipe;
332 }
333
334 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
335 if (ret < 0) {
336 ERR("Consumer write %s stream to pipe %d",
337 new_stream->metadata_flag ? "metadata" : "data",
338 lttng_pipe_get_writefd(stream_pipe));
339 consumer_del_stream(new_stream, NULL);
340 goto end_nosignal;
341 }
342
343 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
344 new_stream->name, fd, new_stream->relayd_stream_id);
345 break;
346 }
347 case LTTNG_CONSUMER_UPDATE_STREAM:
348 {
349 rcu_read_unlock();
350 return -ENOSYS;
351 }
352 case LTTNG_CONSUMER_DESTROY_RELAYD:
353 {
354 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
355 struct consumer_relayd_sock_pair *relayd;
356
357 DBG("Kernel consumer destroying relayd %" PRIu64, index);
358
359 /* Get relayd reference if exists. */
360 relayd = consumer_find_relayd(index);
361 if (relayd == NULL) {
362 DBG("Unable to find relayd %" PRIu64, index);
363 ret_code = LTTNG_ERR_NO_CONSUMER;
364 }
365
366 /*
367 * Each relayd socket pair has a refcount of stream attached to it
368 * which tells if the relayd is still active or not depending on the
369 * refcount value.
370 *
371 * This will set the destroy flag of the relayd object and destroy it
372 * if the refcount reaches zero when called.
373 *
374 * The destroy can happen either here or when a stream fd hangs up.
375 */
376 if (relayd) {
377 consumer_flag_relayd_for_destroy(relayd);
378 }
379
380 ret = consumer_send_status_msg(sock, ret_code);
381 if (ret < 0) {
382 /* Somehow, the session daemon is not responding anymore. */
383 goto error_fatal;
384 }
385
386 goto end_nosignal;
387 }
388 case LTTNG_CONSUMER_DATA_PENDING:
389 {
390 int32_t ret;
391 uint64_t id = msg.u.data_pending.session_id;
392
393 DBG("Kernel consumer data pending command for id %" PRIu64, id);
394
395 ret = consumer_data_pending(id);
396
397 /* Send back returned value to session daemon */
398 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
399 if (ret < 0) {
400 PERROR("send data pending ret code");
401 goto error_fatal;
402 }
403
404 /*
405 * No need to send back a status message since the data pending
406 * returned value is the response.
407 */
408 break;
409 }
410 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
411 {
412 ret = consumer_send_status_msg(sock, ret_code);
413 if (ret < 0) {
414 /* Somehow, the session daemon is not responding anymore. */
415 goto end_nosignal;
416 }
417 break;
418 }
419 default:
420 goto end_nosignal;
421 }
422
423 end_nosignal:
424 rcu_read_unlock();
425
426 /*
427 * Return 1 to indicate success since the 0 value can be a socket
428 * shutdown during the recv() or send() call.
429 */
430 return 1;
431
432 error_fatal:
433 rcu_read_unlock();
434 /* This will issue a consumer stop. */
435 return -1;
436 }
437
438 /*
439 * Consume data on a file descriptor and write it on a trace file.
440 */
441 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
442 struct lttng_consumer_local_data *ctx)
443 {
444 unsigned long len, subbuf_size, padding;
445 int err;
446 ssize_t ret = 0;
447 int infd = stream->wait_fd;
448
449 DBG("In read_subbuffer (infd : %d)", infd);
450 /* Get the next subbuffer */
451 err = kernctl_get_next_subbuf(infd);
452 if (err != 0) {
453 ret = err;
454 /*
455 * This is a debug message even for single-threaded consumer,
456 * because poll() have more relaxed criterions than get subbuf,
457 * so get_subbuf may fail for short race windows where poll()
458 * would issue wakeups.
459 */
460 DBG("Reserving sub buffer failed (everything is normal, "
461 "it is due to concurrency)");
462 goto end;
463 }
464
465 /* Get the full subbuffer size including padding */
466 err = kernctl_get_padded_subbuf_size(infd, &len);
467 if (err != 0) {
468 errno = -err;
469 perror("Getting sub-buffer len failed.");
470 ret = err;
471 goto end;
472 }
473
474 switch (stream->chan->output) {
475 case LTTNG_EVENT_SPLICE:
476
477 /*
478 * XXX: The lttng-modules splice "actor" does not handle copying
479 * partial pages hence only using the subbuffer size without the
480 * padding makes the splice fail.
481 */
482 subbuf_size = len;
483 padding = 0;
484
485 /* splice the subbuffer to the tracefile */
486 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
487 padding);
488 /*
489 * XXX: Splice does not support network streaming so the return value
490 * is simply checked against subbuf_size and not like the mmap() op.
491 */
492 if (ret != subbuf_size) {
493 /*
494 * display the error but continue processing to try
495 * to release the subbuffer
496 */
497 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
498 ret, subbuf_size);
499 }
500 break;
501 case LTTNG_EVENT_MMAP:
502 /* Get subbuffer size without padding */
503 err = kernctl_get_subbuf_size(infd, &subbuf_size);
504 if (err != 0) {
505 errno = -err;
506 perror("Getting sub-buffer len failed.");
507 ret = err;
508 goto end;
509 }
510
511 /* Make sure the tracer is not gone mad on us! */
512 assert(len >= subbuf_size);
513
514 padding = len - subbuf_size;
515
516 /* write the subbuffer to the tracefile */
517 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
518 padding);
519 /*
520 * The mmap operation should write subbuf_size amount of data when
521 * network streaming or the full padding (len) size when we are _not_
522 * streaming.
523 */
524 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
525 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
526 /*
527 * Display the error but continue processing to try to release the
528 * subbuffer
529 */
530 ERR("Error writing to tracefile "
531 "(ret: %zd != len: %lu != subbuf_size: %lu)",
532 ret, len, subbuf_size);
533 }
534 break;
535 default:
536 ERR("Unknown output method");
537 ret = -1;
538 }
539
540 err = kernctl_put_next_subbuf(infd);
541 if (err != 0) {
542 errno = -err;
543 if (errno == EFAULT) {
544 perror("Error in unreserving sub buffer\n");
545 } else if (errno == EIO) {
546 /* Should never happen with newer LTTng versions */
547 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
548 }
549
550 ret = -err;
551 goto end;
552 }
553
554 end:
555 return ret;
556 }
557
558 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
559 {
560 int ret;
561
562 assert(stream);
563
564 /* Don't create anything if this is set for streaming. */
565 if (stream->net_seq_idx == (uint64_t) -1ULL) {
566 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
567 stream->chan->tracefile_size, stream->tracefile_count_current,
568 stream->uid, stream->gid);
569 if (ret < 0) {
570 goto error;
571 }
572 stream->out_fd = ret;
573 stream->tracefile_size_current = 0;
574 }
575
576 if (stream->output == LTTNG_EVENT_MMAP) {
577 /* get the len of the mmap region */
578 unsigned long mmap_len;
579
580 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
581 if (ret != 0) {
582 errno = -ret;
583 PERROR("kernctl_get_mmap_len");
584 goto error_close_fd;
585 }
586 stream->mmap_len = (size_t) mmap_len;
587
588 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
589 MAP_PRIVATE, stream->wait_fd, 0);
590 if (stream->mmap_base == MAP_FAILED) {
591 PERROR("Error mmaping");
592 ret = -1;
593 goto error_close_fd;
594 }
595 }
596
597 /* we return 0 to let the library handle the FD internally */
598 return 0;
599
600 error_close_fd:
601 {
602 int err;
603
604 err = close(stream->out_fd);
605 assert(!err);
606 }
607 error:
608 return ret;
609 }
610
611 /*
612 * Check if data is still being extracted from the buffers for a specific
613 * stream. Consumer data lock MUST be acquired before calling this function
614 * and the stream lock.
615 *
616 * Return 1 if the traced data are still getting read else 0 meaning that the
617 * data is available for trace viewer reading.
618 */
619 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
620 {
621 int ret;
622
623 assert(stream);
624
625 ret = kernctl_get_next_subbuf(stream->wait_fd);
626 if (ret == 0) {
627 /* There is still data so let's put back this subbuffer. */
628 ret = kernctl_put_subbuf(stream->wait_fd);
629 assert(ret == 0);
630 ret = 1; /* Data is pending */
631 goto end;
632 }
633
634 /* Data is NOT pending and ready to be read. */
635 ret = 0;
636
637 end:
638 return ret;
639 }
This page took 0.042118 seconds and 5 git commands to generate.