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