Don't send the subbuffer padding for streaming
[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,
121 msg.u.channel.max_sb_size);
122 if (new_channel == NULL) {
f73fabfd 123 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
124 goto end_nosignal;
125 }
126 if (ctx->on_recv_channel != NULL) {
127 ret = ctx->on_recv_channel(new_channel);
128 if (ret == 0) {
129 consumer_add_channel(new_channel);
130 } else if (ret < 0) {
131 goto end_nosignal;
132 }
133 } else {
134 consumer_add_channel(new_channel);
135 }
136 goto end_nosignal;
137 }
138 case LTTNG_CONSUMER_ADD_STREAM:
139 {
f2fc6720 140 int fd;
00e2e675
DG
141 struct consumer_relayd_sock_pair *relayd = NULL;
142 struct lttng_consumer_stream *new_stream;
3bd1e081
MD
143
144 /* block */
145 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 146 rcu_read_unlock();
3bd1e081
MD
147 return -EINTR;
148 }
00e2e675
DG
149
150 /* Get stream file descriptor from socket */
f2fc6720
MD
151 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
152 if (ret != sizeof(fd)) {
f73fabfd 153 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 154 rcu_read_unlock();
3bd1e081
MD
155 return ret;
156 }
3bd1e081 157
3bd1e081
MD
158 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
159 msg.u.stream.stream_key,
f2fc6720 160 fd, fd,
3bd1e081
MD
161 msg.u.stream.state,
162 msg.u.stream.mmap_len,
163 msg.u.stream.output,
6df2e2c9
MD
164 msg.u.stream.path_name,
165 msg.u.stream.uid,
00e2e675
DG
166 msg.u.stream.gid,
167 msg.u.stream.net_index,
168 msg.u.stream.metadata_flag);
3bd1e081 169 if (new_stream == NULL) {
f73fabfd 170 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3f8e211f 171 goto end_nosignal;
3bd1e081 172 }
00e2e675 173
fb3a43a9
DG
174 /*
175 * The buffer flush is done on the session daemon side for the kernel
176 * so no need for the stream "hangup_flush_done" variable to be
177 * tracked. This is important for a kernel stream since we don't rely
178 * on the flush state of the stream to read data. It's not the case for
179 * user space tracing.
180 */
181 new_stream->hangup_flush_done = 0;
182
00e2e675
DG
183 /* The stream is not metadata. Get relayd reference if exists. */
184 relayd = consumer_find_relayd(msg.u.stream.net_index);
185 if (relayd != NULL) {
186 /* Add stream on the relayd */
187 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
188 ret = relayd_add_stream(&relayd->control_sock,
189 msg.u.stream.name, msg.u.stream.path_name,
190 &new_stream->relayd_stream_id);
191 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
192 if (ret < 0) {
3f8e211f 193 goto end_nosignal;
00e2e675
DG
194 }
195 } else if (msg.u.stream.net_index != -1) {
196 ERR("Network sequence index %d unknown. Not adding stream.",
197 msg.u.stream.net_index);
198 free(new_stream);
3f8e211f 199 goto end_nosignal;
00e2e675
DG
200 }
201
fb3a43a9
DG
202 /* Send stream to the metadata thread */
203 if (new_stream->metadata_flag) {
204 if (ctx->on_recv_stream) {
205 ret = ctx->on_recv_stream(new_stream);
206 if (ret < 0) {
207 goto end_nosignal;
208 }
209 }
210
211 do {
212 ret = write(ctx->consumer_metadata_pipe[1], new_stream,
213 sizeof(struct lttng_consumer_stream));
214 } while (ret < 0 && errno == EINTR);
215 if (ret < 0) {
216 PERROR("write metadata pipe");
3bd1e081
MD
217 }
218 } else {
fb3a43a9
DG
219 if (ctx->on_recv_stream) {
220 ret = ctx->on_recv_stream(new_stream);
221 if (ret < 0) {
222 goto end_nosignal;
223 }
224 }
3bd1e081
MD
225 consumer_add_stream(new_stream);
226 }
00e2e675
DG
227
228 DBG("Kernel consumer_add_stream (%d)", fd);
3bd1e081
MD
229 break;
230 }
231 case LTTNG_CONSUMER_UPDATE_STREAM:
232 {
3f8e211f
DG
233 rcu_read_unlock();
234 return -ENOSYS;
235 }
236 case LTTNG_CONSUMER_DESTROY_RELAYD:
237 {
a6ba4fe1 238 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
239 struct consumer_relayd_sock_pair *relayd;
240
a6ba4fe1 241 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
242
243 /* Get relayd reference if exists. */
a6ba4fe1 244 relayd = consumer_find_relayd(index);
3f8e211f 245 if (relayd == NULL) {
a6ba4fe1 246 ERR("Unable to find relayd %" PRIu64, index);
3f8e211f 247 goto end_nosignal;
3bd1e081 248 }
3f8e211f 249
a6ba4fe1
DG
250 /*
251 * Each relayd socket pair has a refcount of stream attached to it
252 * which tells if the relayd is still active or not depending on the
253 * refcount value.
254 *
255 * This will set the destroy flag of the relayd object and destroy it
256 * if the refcount reaches zero when called.
257 *
258 * The destroy can happen either here or when a stream fd hangs up.
259 */
260 consumer_flag_relayd_for_destroy(relayd);
3f8e211f 261
3f8e211f 262 goto end_nosignal;
3bd1e081
MD
263 }
264 default:
3f8e211f 265 goto end_nosignal;
3bd1e081 266 }
3f8e211f 267
04fdd819 268 /*
3f8e211f
DG
269 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
270 * Important note: Because writing into the pipe is non-blocking (and
271 * therefore we allow dropping wakeup data, as long as there is wakeup data
272 * present in the pipe buffer to wake up the other end), the other end
273 * should perform the following sequence for waiting:
274 *
04fdd819
MD
275 * 1) empty the pipe (reads).
276 * 2) perform update operation.
277 * 3) wait on the pipe (poll).
278 */
279 do {
280 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 281 } while (ret < 0 && errno == EINTR);
3bd1e081 282end_nosignal:
b0b335c8 283 rcu_read_unlock();
4cbc1a04
DG
284
285 /*
286 * Return 1 to indicate success since the 0 value can be a socket
287 * shutdown during the recv() or send() call.
288 */
289 return 1;
3bd1e081 290}
d41f73b7
MD
291
292/*
293 * Consume data on a file descriptor and write it on a trace file.
294 */
4078b776 295ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
296 struct lttng_consumer_local_data *ctx)
297{
1d4dfdef 298 unsigned long len, subbuf_size, padding;
d41f73b7 299 int err;
4078b776 300 ssize_t ret = 0;
d41f73b7
MD
301 int infd = stream->wait_fd;
302
303 DBG("In read_subbuffer (infd : %d)", infd);
304 /* Get the next subbuffer */
305 err = kernctl_get_next_subbuf(infd);
306 if (err != 0) {
1d4dfdef 307 ret = err;
d41f73b7
MD
308 /*
309 * This is a debug message even for single-threaded consumer,
310 * because poll() have more relaxed criterions than get subbuf,
311 * so get_subbuf may fail for short race windows where poll()
312 * would issue wakeups.
313 */
314 DBG("Reserving sub buffer failed (everything is normal, "
315 "it is due to concurrency)");
316 goto end;
317 }
318
1d4dfdef
DG
319 /* Get the full subbuffer size including padding */
320 err = kernctl_get_padded_subbuf_size(infd, &len);
321 if (err != 0) {
322 errno = -err;
323 perror("Getting sub-buffer len failed.");
324 ret = err;
325 goto end;
326 }
327
d41f73b7 328 switch (stream->output) {
1d4dfdef 329 case LTTNG_EVENT_SPLICE:
d41f73b7 330
1d4dfdef
DG
331 /*
332 * XXX: The lttng-modules splice "actor" does not handle copying
333 * partial pages hence only using the subbuffer size without the
334 * padding makes the splice fail.
335 */
336 subbuf_size = len;
337 padding = 0;
338
339 /* splice the subbuffer to the tracefile */
340 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream,
341 subbuf_size, padding);
342 if (ret != subbuf_size) {
343 /*
344 * display the error but continue processing to try
345 * to release the subbuffer
346 */
347 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
348 ret, subbuf_size);
349 }
350 break;
351 case LTTNG_EVENT_MMAP:
352 /* Get subbuffer size without padding */
353 err = kernctl_get_subbuf_size(infd, &subbuf_size);
354 if (err != 0) {
355 errno = -err;
356 perror("Getting sub-buffer len failed.");
357 ret = err;
358 goto end;
359 }
47e81c02 360
1d4dfdef
DG
361 /* Make sure the tracer is not gone mad on us! */
362 assert(len >= subbuf_size);
363
364 padding = len - subbuf_size;
365
366 /* write the subbuffer to the tracefile */
367 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream,
368 subbuf_size, padding);
369 if (ret != subbuf_size) {
370 /*
371 * display the error but continue processing to try
372 * to release the subbuffer
373 */
374 ERR("Error writing to tracefile (ret: %zd != len: %lu",
375 ret, subbuf_size);
376 }
377 break;
378 default:
379 ERR("Unknown output method");
380 ret = -1;
d41f73b7
MD
381 }
382
383 err = kernctl_put_next_subbuf(infd);
384 if (err != 0) {
21073eaa 385 errno = -err;
d41f73b7
MD
386 if (errno == EFAULT) {
387 perror("Error in unreserving sub buffer\n");
388 } else if (errno == EIO) {
389 /* Should never happen with newer LTTng versions */
390 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
391 }
21073eaa
DG
392
393 ret = -err;
d41f73b7
MD
394 goto end;
395 }
396
397end:
398 return ret;
399}
400
401int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
402{
403 int ret;
404
405 /* Opening the tracefile in write mode */
00e2e675 406 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
e11d277b 407 ret = run_as_open(stream->path_name,
60b6c79c
MD
408 O_WRONLY|O_CREAT|O_TRUNC,
409 S_IRWXU|S_IRWXG|S_IRWXO,
410 stream->uid, stream->gid);
d41f73b7
MD
411 if (ret < 0) {
412 ERR("Opening %s", stream->path_name);
413 perror("open");
414 goto error;
415 }
416 stream->out_fd = ret;
417 }
418
419 if (stream->output == LTTNG_EVENT_MMAP) {
420 /* get the len of the mmap region */
421 unsigned long mmap_len;
422
423 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
424 if (ret != 0) {
87dc6a9c 425 errno = -ret;
d41f73b7
MD
426 perror("kernctl_get_mmap_len");
427 goto error_close_fd;
428 }
429 stream->mmap_len = (size_t) mmap_len;
430
431 stream->mmap_base = mmap(NULL, stream->mmap_len,
432 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
433 if (stream->mmap_base == MAP_FAILED) {
434 perror("Error mmaping");
435 ret = -1;
436 goto error_close_fd;
437 }
438 }
439
440 /* we return 0 to let the library handle the FD internally */
441 return 0;
442
443error_close_fd:
444 {
445 int err;
446
447 err = close(stream->out_fd);
448 assert(!err);
449 }
450error:
451 return ret;
452}
453
This page took 0.057714 seconds and 5 git commands to generate.