Relayd data available command 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>
00e2e675 37#include <common/relayd/relayd.h>
0857097f 38
10a8a223 39#include "kernel-consumer.h"
3bd1e081
MD
40
41extern struct lttng_consumer_global_data consumer_data;
42extern int consumer_poll_timeout;
43extern volatile int consumer_quit;
44
3bd1e081
MD
45/*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
50int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
51 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) {
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 */
70int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data *ctx,
72 struct lttng_consumer_stream *stream,
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
87int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
88 int sock, struct pollfd *consumer_sockpoll)
89{
90 ssize_t ret;
91 struct lttcomm_consumer_msg msg;
92
93 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
94 if (ret != sizeof(msg)) {
f73fabfd 95 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3bd1e081
MD
96 return ret;
97 }
98 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
99 return -ENOENT;
100 }
101
b0b335c8
MD
102 /* relayd needs RCU read-side protection */
103 rcu_read_lock();
104
3bd1e081 105 switch (msg.cmd_type) {
00e2e675
DG
106 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
107 {
7735ef9e
DG
108 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
109 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
110 &msg.u.relayd_sock.sock);
00e2e675
DG
111 goto end_nosignal;
112 }
3bd1e081
MD
113 case LTTNG_CONSUMER_ADD_CHANNEL:
114 {
115 struct lttng_consumer_channel *new_channel;
116
117 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
118 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
119 -1, -1,
120 msg.u.channel.mmap_len,
c30aaa51
MD
121 msg.u.channel.max_sb_size,
122 msg.u.channel.nb_init_streams);
3bd1e081 123 if (new_channel == NULL) {
f73fabfd 124 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
125 goto end_nosignal;
126 }
127 if (ctx->on_recv_channel != NULL) {
128 ret = ctx->on_recv_channel(new_channel);
129 if (ret == 0) {
130 consumer_add_channel(new_channel);
131 } else if (ret < 0) {
132 goto end_nosignal;
133 }
134 } else {
135 consumer_add_channel(new_channel);
136 }
137 goto end_nosignal;
138 }
139 case LTTNG_CONSUMER_ADD_STREAM:
140 {
50f8ae69 141 int fd, stream_pipe;
00e2e675
DG
142 struct consumer_relayd_sock_pair *relayd = NULL;
143 struct lttng_consumer_stream *new_stream;
c80048c6 144 int alloc_ret = 0;
3bd1e081
MD
145
146 /* block */
147 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 148 rcu_read_unlock();
3bd1e081
MD
149 return -EINTR;
150 }
00e2e675
DG
151
152 /* Get stream file descriptor from socket */
f2fc6720
MD
153 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
154 if (ret != sizeof(fd)) {
f73fabfd 155 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 156 rcu_read_unlock();
3bd1e081
MD
157 return ret;
158 }
3bd1e081 159
3bd1e081
MD
160 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
161 msg.u.stream.stream_key,
f2fc6720 162 fd, fd,
3bd1e081
MD
163 msg.u.stream.state,
164 msg.u.stream.mmap_len,
165 msg.u.stream.output,
6df2e2c9
MD
166 msg.u.stream.path_name,
167 msg.u.stream.uid,
00e2e675
DG
168 msg.u.stream.gid,
169 msg.u.stream.net_index,
c80048c6 170 msg.u.stream.metadata_flag,
53632229 171 msg.u.stream.session_id,
c80048c6 172 &alloc_ret);
3bd1e081 173 if (new_stream == NULL) {
c80048c6
MD
174 switch (alloc_ret) {
175 case -ENOMEM:
176 case -EINVAL:
177 default:
178 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
179 break;
180 case -ENOENT:
181 /*
182 * We could not find the channel. Can happen if cpu hotplug
183 * happens while tearing down.
184 */
185 DBG3("Could not find channel");
186 break;
187 }
3f8e211f 188 goto end_nosignal;
3bd1e081 189 }
00e2e675 190
fb3a43a9
DG
191 /*
192 * The buffer flush is done on the session daemon side for the kernel
193 * so no need for the stream "hangup_flush_done" variable to be
194 * tracked. This is important for a kernel stream since we don't rely
195 * on the flush state of the stream to read data. It's not the case for
196 * user space tracing.
197 */
198 new_stream->hangup_flush_done = 0;
199
00e2e675
DG
200 /* The stream is not metadata. Get relayd reference if exists. */
201 relayd = consumer_find_relayd(msg.u.stream.net_index);
202 if (relayd != NULL) {
203 /* Add stream on the relayd */
204 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
205 ret = relayd_add_stream(&relayd->control_sock,
206 msg.u.stream.name, msg.u.stream.path_name,
207 &new_stream->relayd_stream_id);
208 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
209 if (ret < 0) {
e316aad5 210 consumer_del_stream(new_stream, NULL);
3f8e211f 211 goto end_nosignal;
00e2e675
DG
212 }
213 } else if (msg.u.stream.net_index != -1) {
214 ERR("Network sequence index %d unknown. Not adding stream.",
215 msg.u.stream.net_index);
e316aad5 216 consumer_del_stream(new_stream, NULL);
3f8e211f 217 goto end_nosignal;
00e2e675
DG
218 }
219
633d0084
DG
220 if (ctx->on_recv_stream) {
221 ret = ctx->on_recv_stream(new_stream);
222 if (ret < 0) {
e316aad5 223 consumer_del_stream(new_stream, NULL);
633d0084 224 goto end_nosignal;
fb3a43a9 225 }
633d0084 226 }
fb3a43a9 227
50f8ae69 228 /* Get the right pipe where the stream will be sent. */
633d0084 229 if (new_stream->metadata_flag) {
50f8ae69 230 stream_pipe = ctx->consumer_metadata_pipe[1];
3bd1e081 231 } else {
50f8ae69
DG
232 stream_pipe = ctx->consumer_data_pipe[1];
233 }
234
235 do {
236 ret = write(stream_pipe, &new_stream, sizeof(new_stream));
237 } while (ret < 0 && errno == EINTR);
238 if (ret < 0) {
239 PERROR("Consumer write %s stream to pipe %d",
240 new_stream->metadata_flag ? "metadata" : "data",
241 stream_pipe);
242 consumer_del_stream(new_stream, NULL);
243 goto end_nosignal;
3bd1e081 244 }
00e2e675 245
50f8ae69
DG
246 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
247 msg.u.stream.path_name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
248 break;
249 }
250 case LTTNG_CONSUMER_UPDATE_STREAM:
251 {
3f8e211f
DG
252 rcu_read_unlock();
253 return -ENOSYS;
254 }
255 case LTTNG_CONSUMER_DESTROY_RELAYD:
256 {
a6ba4fe1 257 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
258 struct consumer_relayd_sock_pair *relayd;
259
a6ba4fe1 260 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
261
262 /* Get relayd reference if exists. */
a6ba4fe1 263 relayd = consumer_find_relayd(index);
3f8e211f 264 if (relayd == NULL) {
a6ba4fe1 265 ERR("Unable to find relayd %" PRIu64, index);
3f8e211f 266 goto end_nosignal;
3bd1e081 267 }
3f8e211f 268
a6ba4fe1
DG
269 /*
270 * Each relayd socket pair has a refcount of stream attached to it
271 * which tells if the relayd is still active or not depending on the
272 * refcount value.
273 *
274 * This will set the destroy flag of the relayd object and destroy it
275 * if the refcount reaches zero when called.
276 *
277 * The destroy can happen either here or when a stream fd hangs up.
278 */
279 consumer_flag_relayd_for_destroy(relayd);
3f8e211f 280
3f8e211f 281 goto end_nosignal;
3bd1e081 282 }
53632229
DG
283 case LTTNG_CONSUMER_DATA_AVAILABLE:
284 {
c8f59ee5
DG
285 int32_t ret;
286 uint64_t id = msg.u.data_available.session_id;
287
288 DBG("Kernel consumer data available command for id %" PRIu64, id);
289
290 ret = consumer_data_available(id);
291
292 /* Send back returned value to session daemon */
293 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
294 if (ret < 0) {
295 PERROR("send data available ret code");
296 }
297 break;
53632229 298 }
3bd1e081 299 default:
3f8e211f 300 goto end_nosignal;
3bd1e081 301 }
3f8e211f 302
3bd1e081 303end_nosignal:
b0b335c8 304 rcu_read_unlock();
4cbc1a04
DG
305
306 /*
307 * Return 1 to indicate success since the 0 value can be a socket
308 * shutdown during the recv() or send() call.
309 */
310 return 1;
3bd1e081 311}
d41f73b7
MD
312
313/*
314 * Consume data on a file descriptor and write it on a trace file.
315 */
4078b776 316ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
317 struct lttng_consumer_local_data *ctx)
318{
1d4dfdef 319 unsigned long len, subbuf_size, padding;
d41f73b7 320 int err;
4078b776 321 ssize_t ret = 0;
d41f73b7
MD
322 int infd = stream->wait_fd;
323
324 DBG("In read_subbuffer (infd : %d)", infd);
325 /* Get the next subbuffer */
326 err = kernctl_get_next_subbuf(infd);
327 if (err != 0) {
1d4dfdef 328 ret = err;
d41f73b7
MD
329 /*
330 * This is a debug message even for single-threaded consumer,
331 * because poll() have more relaxed criterions than get subbuf,
332 * so get_subbuf may fail for short race windows where poll()
333 * would issue wakeups.
334 */
335 DBG("Reserving sub buffer failed (everything is normal, "
336 "it is due to concurrency)");
337 goto end;
338 }
339
1d4dfdef
DG
340 /* Get the full subbuffer size including padding */
341 err = kernctl_get_padded_subbuf_size(infd, &len);
342 if (err != 0) {
343 errno = -err;
344 perror("Getting sub-buffer len failed.");
345 ret = err;
346 goto end;
347 }
348
d41f73b7 349 switch (stream->output) {
1d4dfdef 350 case LTTNG_EVENT_SPLICE:
d41f73b7 351
1d4dfdef
DG
352 /*
353 * XXX: The lttng-modules splice "actor" does not handle copying
354 * partial pages hence only using the subbuffer size without the
355 * padding makes the splice fail.
356 */
357 subbuf_size = len;
358 padding = 0;
359
360 /* splice the subbuffer to the tracefile */
91dfef6e
DG
361 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
362 padding);
363 /*
364 * XXX: Splice does not support network streaming so the return value
365 * is simply checked against subbuf_size and not like the mmap() op.
366 */
1d4dfdef
DG
367 if (ret != subbuf_size) {
368 /*
369 * display the error but continue processing to try
370 * to release the subbuffer
371 */
372 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
373 ret, subbuf_size);
374 }
375 break;
376 case LTTNG_EVENT_MMAP:
377 /* Get subbuffer size without padding */
378 err = kernctl_get_subbuf_size(infd, &subbuf_size);
379 if (err != 0) {
380 errno = -err;
381 perror("Getting sub-buffer len failed.");
382 ret = err;
383 goto end;
384 }
47e81c02 385
1d4dfdef
DG
386 /* Make sure the tracer is not gone mad on us! */
387 assert(len >= subbuf_size);
388
389 padding = len - subbuf_size;
390
391 /* write the subbuffer to the tracefile */
91dfef6e
DG
392 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
393 padding);
394 /*
395 * The mmap operation should write subbuf_size amount of data when
396 * network streaming or the full padding (len) size when we are _not_
397 * streaming.
398 */
399 if ((ret != subbuf_size && stream->net_seq_idx != -1) ||
400 (ret != len && stream->net_seq_idx == -1)) {
1d4dfdef 401 /*
91dfef6e
DG
402 * Display the error but continue processing to try to release the
403 * subbuffer
1d4dfdef 404 */
91dfef6e
DG
405 ERR("Error writing to tracefile "
406 "(ret: %zd != len: %lu != subbuf_size: %lu)",
407 ret, len, subbuf_size);
1d4dfdef
DG
408 }
409 break;
410 default:
411 ERR("Unknown output method");
412 ret = -1;
d41f73b7
MD
413 }
414
415 err = kernctl_put_next_subbuf(infd);
416 if (err != 0) {
21073eaa 417 errno = -err;
d41f73b7
MD
418 if (errno == EFAULT) {
419 perror("Error in unreserving sub buffer\n");
420 } else if (errno == EIO) {
421 /* Should never happen with newer LTTng versions */
422 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
423 }
21073eaa
DG
424
425 ret = -err;
d41f73b7
MD
426 goto end;
427 }
428
429end:
430 return ret;
431}
432
433int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
434{
435 int ret;
436
437 /* Opening the tracefile in write mode */
00e2e675 438 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
e11d277b 439 ret = run_as_open(stream->path_name,
60b6c79c
MD
440 O_WRONLY|O_CREAT|O_TRUNC,
441 S_IRWXU|S_IRWXG|S_IRWXO,
442 stream->uid, stream->gid);
d41f73b7
MD
443 if (ret < 0) {
444 ERR("Opening %s", stream->path_name);
445 perror("open");
446 goto error;
447 }
448 stream->out_fd = ret;
449 }
450
451 if (stream->output == LTTNG_EVENT_MMAP) {
452 /* get the len of the mmap region */
453 unsigned long mmap_len;
454
455 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
456 if (ret != 0) {
87dc6a9c 457 errno = -ret;
d41f73b7
MD
458 perror("kernctl_get_mmap_len");
459 goto error_close_fd;
460 }
461 stream->mmap_len = (size_t) mmap_len;
462
463 stream->mmap_base = mmap(NULL, stream->mmap_len,
464 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
465 if (stream->mmap_base == MAP_FAILED) {
466 perror("Error mmaping");
467 ret = -1;
468 goto error_close_fd;
469 }
470 }
471
472 /* we return 0 to let the library handle the FD internally */
473 return 0;
474
475error_close_fd:
476 {
477 int err;
478
479 err = close(stream->out_fd);
480 assert(!err);
481 }
482error:
483 return ret;
484}
485
ca22feea
DG
486/*
487 * Check if data is still being extracted from the buffers for a specific
488 * stream. Consumer data lock MUST be acquired before calling this function.
489 *
490 * Return 0 if the traced data are still getting read else 1 meaning that the
491 * data is available for trace viewer reading.
492 */
493int lttng_kconsumer_data_available(struct lttng_consumer_stream *stream)
494{
495 int ret;
496
497 assert(stream);
498
499 /*
500 * Try to lock the stream mutex. On failure, we know that the stream is
501 * being used else where hence there is data still being extracted.
502 */
503 ret = pthread_mutex_trylock(&stream->lock);
504 if (ret == EBUSY) {
505 goto data_not_available;
506 }
507 /* The stream is now locked so we can do our ustctl calls */
508
509 ret = kernctl_get_next_subbuf(stream->wait_fd);
510 if (ret == 0) {
511 /* There is still data so let's put back this subbuffer. */
512 ret = kernctl_put_subbuf(stream->wait_fd);
513 assert(ret == 0);
514 pthread_mutex_unlock(&stream->lock);
515 goto data_not_available;
516 }
517
518 /* Data is available to be read for this stream. */
519 pthread_mutex_unlock(&stream->lock);
520 return 1;
521
522data_not_available:
523 return 0;
524}
This page took 0.088113 seconds and 4 git commands to generate.