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