Fix: relayd metadata size
[lttng-tools.git] / src / common / ust-consumer / ust-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/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <lttng/ust-ctl.h>
31
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/relayd/relayd.h>
35 #include <common/compat/fcntl.h>
36
37 #include "ust-consumer.h"
38
39 extern struct lttng_consumer_global_data consumer_data;
40 extern int consumer_poll_timeout;
41 extern volatile int consumer_quit;
42
43 /*
44 * Mmap the ring buffer, read it and write the data to the tracefile.
45 *
46 * Returns the number of bytes written, else negative value on error.
47 */
48 ssize_t lttng_ustconsumer_on_read_subbuffer_mmap(
49 struct lttng_consumer_local_data *ctx,
50 struct lttng_consumer_stream *stream, unsigned long len)
51 {
52 unsigned long mmap_offset;
53 long ret = 0, written = 0;
54 off_t orig_offset = stream->out_fd_offset;
55 int outfd = stream->out_fd;
56 uint64_t metadata_id;
57 struct consumer_relayd_sock_pair *relayd = NULL;
58
59 /* RCU lock for the relayd pointer */
60 rcu_read_lock();
61
62 /* Flag that the current stream if set for network streaming. */
63 if (stream->net_seq_idx != -1) {
64 relayd = consumer_find_relayd(stream->net_seq_idx);
65 if (relayd == NULL) {
66 ERR("UST consumer mmap(), unable to find relay for index %d",
67 stream->net_seq_idx);
68 goto end;
69 }
70 }
71
72 /* get the offset inside the fd to mmap */
73 ret = ustctl_get_mmap_read_offset(stream->chan->handle,
74 stream->buf, &mmap_offset);
75 if (ret != 0) {
76 errno = -ret;
77 PERROR("ustctl_get_mmap_read_offset");
78 written = ret;
79 goto end;
80 }
81
82 /* Handle stream on the relayd if the output is on the network */
83 if (relayd) {
84 unsigned long netlen = len;
85
86 if (stream->metadata_flag) {
87 /* Only lock if metadata since we use the control socket. */
88 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
89 netlen += sizeof(stream->relayd_stream_id);
90 }
91
92 ret = consumer_handle_stream_before_relayd(stream, netlen);
93 if (ret >= 0) {
94 outfd = ret;
95
96 /* Write metadata stream id before payload */
97 if (stream->metadata_flag) {
98 metadata_id = htobe64(stream->relayd_stream_id);
99 do {
100 ret = write(outfd, (void *) &metadata_id,
101 sizeof(stream->relayd_stream_id));
102 } while (ret < 0 && errno == EINTR);
103 if (ret < 0) {
104 PERROR("write metadata stream id");
105 written = ret;
106 goto end;
107 }
108 DBG("Metadata stream id %zu written before data",
109 stream->relayd_stream_id);
110 }
111 }
112 /* Else, use the default set before which is the filesystem. */
113 }
114
115 while (len > 0) {
116 do {
117 ret = write(outfd, stream->mmap_base + mmap_offset, len);
118 } while (ret < 0 && errno == EINTR);
119 if (ret < 0) {
120 PERROR("Error in file write");
121 if (written == 0) {
122 written = ret;
123 }
124 goto end;
125 } else if (ret > len) {
126 PERROR("ret %ld > len %lu", ret, len);
127 written += ret;
128 goto end;
129 } else {
130 len -= ret;
131 mmap_offset += ret;
132 }
133 DBG("UST mmap write() ret %ld (len %lu)", ret, len);
134
135 /* This call is useless on a socket so better save a syscall. */
136 if (!relayd) {
137 /* This won't block, but will start writeout asynchronously */
138 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
139 SYNC_FILE_RANGE_WRITE);
140 stream->out_fd_offset += ret;
141 }
142 written += ret;
143 }
144 lttng_consumer_sync_trace_file(stream, orig_offset);
145
146 end:
147 if (relayd && stream->metadata_flag) {
148 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
149 }
150 rcu_read_unlock();
151 return written;
152 }
153
154 /*
155 * Splice the data from the ring buffer to the tracefile.
156 *
157 * Returns the number of bytes spliced.
158 */
159 ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
160 struct lttng_consumer_local_data *ctx,
161 struct lttng_consumer_stream *stream, unsigned long len)
162 {
163 return -ENOSYS;
164 }
165
166 /*
167 * Take a snapshot for a specific fd
168 *
169 * Returns 0 on success, < 0 on error
170 */
171 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
172 struct lttng_consumer_stream *stream)
173 {
174 int ret = 0;
175
176 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
177 if (ret != 0) {
178 errno = -ret;
179 PERROR("Getting sub-buffer snapshot.");
180 }
181
182 return ret;
183 }
184
185 /*
186 * Get the produced position
187 *
188 * Returns 0 on success, < 0 on error
189 */
190 int lttng_ustconsumer_get_produced_snapshot(
191 struct lttng_consumer_local_data *ctx,
192 struct lttng_consumer_stream *stream,
193 unsigned long *pos)
194 {
195 int ret;
196
197 ret = ustctl_snapshot_get_produced(stream->chan->handle,
198 stream->buf, pos);
199 if (ret != 0) {
200 errno = -ret;
201 PERROR("kernctl_snapshot_get_produced");
202 }
203
204 return ret;
205 }
206
207 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
208 int sock, struct pollfd *consumer_sockpoll)
209 {
210 ssize_t ret;
211 struct lttcomm_consumer_msg msg;
212
213 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
214 if (ret != sizeof(msg)) {
215 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
216 return ret;
217 }
218 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
219 return -ENOENT;
220 }
221
222 /* relayd need RCU read-side lock */
223 rcu_read_lock();
224
225 switch (msg.cmd_type) {
226 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
227 {
228 int fd;
229 struct consumer_relayd_sock_pair *relayd;
230
231 DBG("UST Consumer adding relayd socket");
232
233 /* Get relayd reference if exists. */
234 relayd = consumer_find_relayd(msg.u.relayd_sock.net_index);
235 if (relayd == NULL) {
236 /* Not found. Allocate one. */
237 relayd = consumer_allocate_relayd_sock_pair(
238 msg.u.relayd_sock.net_index);
239 if (relayd == NULL) {
240 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
241 goto end_nosignal;
242 }
243 }
244
245 /* Poll on consumer socket. */
246 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
247 return -EINTR;
248 }
249
250 /* Get relayd socket from session daemon */
251 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
252 if (ret != sizeof(fd)) {
253 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
254 goto end_nosignal;
255 }
256
257 /* Copy socket information and received FD */
258 switch (msg.u.relayd_sock.type) {
259 case LTTNG_STREAM_CONTROL:
260 /* Copy received lttcomm socket */
261 lttcomm_copy_sock(&relayd->control_sock, &msg.u.relayd_sock.sock);
262 ret = lttcomm_create_sock(&relayd->control_sock);
263 if (ret < 0) {
264 goto end_nosignal;
265 }
266
267 /* Close the created socket fd which is useless */
268 close(relayd->control_sock.fd);
269
270 /* Assign new file descriptor */
271 relayd->control_sock.fd = fd;
272 break;
273 case LTTNG_STREAM_DATA:
274 /* Copy received lttcomm socket */
275 lttcomm_copy_sock(&relayd->data_sock, &msg.u.relayd_sock.sock);
276 ret = lttcomm_create_sock(&relayd->data_sock);
277 if (ret < 0) {
278 goto end_nosignal;
279 }
280
281 /* Close the created socket fd which is useless */
282 close(relayd->data_sock.fd);
283
284 /* Assign new file descriptor */
285 relayd->data_sock.fd = fd;
286 break;
287 default:
288 ERR("Unknown relayd socket type");
289 goto end_nosignal;
290 }
291
292 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
293 msg.u.relayd_sock.type == LTTNG_STREAM_CONTROL ? "control" : "data",
294 relayd->net_seq_idx, fd);
295
296 /*
297 * Add relayd socket pair to consumer data hashtable. If object already
298 * exists or on error, the function gracefully returns.
299 */
300 consumer_add_relayd(relayd);
301
302 goto end_nosignal;
303 }
304 case LTTNG_CONSUMER_ADD_CHANNEL:
305 {
306 struct lttng_consumer_channel *new_channel;
307 int fds[1];
308 size_t nb_fd = 1;
309
310 /* block */
311 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
312 return -EINTR;
313 }
314 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
315 if (ret != sizeof(fds)) {
316 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
317 return ret;
318 }
319
320 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
321
322 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
323 fds[0], -1,
324 msg.u.channel.mmap_len,
325 msg.u.channel.max_sb_size);
326 if (new_channel == NULL) {
327 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
328 goto end_nosignal;
329 }
330 if (ctx->on_recv_channel != NULL) {
331 ret = ctx->on_recv_channel(new_channel);
332 if (ret == 0) {
333 consumer_add_channel(new_channel);
334 } else if (ret < 0) {
335 goto end_nosignal;
336 }
337 } else {
338 consumer_add_channel(new_channel);
339 }
340 goto end_nosignal;
341 }
342 case LTTNG_CONSUMER_ADD_STREAM:
343 {
344 struct lttng_consumer_stream *new_stream;
345 int fds[2];
346 size_t nb_fd = 2;
347 struct consumer_relayd_sock_pair *relayd = NULL;
348
349 /* block */
350 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
351 return -EINTR;
352 }
353 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
354 if (ret != sizeof(fds)) {
355 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
356 return ret;
357 }
358
359 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
360 new_stream = consumer_allocate_stream(msg.u.channel.channel_key,
361 msg.u.stream.stream_key,
362 fds[0], fds[1],
363 msg.u.stream.state,
364 msg.u.stream.mmap_len,
365 msg.u.stream.output,
366 msg.u.stream.path_name,
367 msg.u.stream.uid,
368 msg.u.stream.gid,
369 msg.u.stream.net_index,
370 msg.u.stream.metadata_flag);
371 if (new_stream == NULL) {
372 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
373 goto end;
374 }
375
376 /* The stream is not metadata. Get relayd reference if exists. */
377 relayd = consumer_find_relayd(msg.u.stream.net_index);
378 if (relayd != NULL) {
379 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
380 /* Add stream on the relayd */
381 ret = relayd_add_stream(&relayd->control_sock,
382 msg.u.stream.name, msg.u.stream.path_name,
383 &new_stream->relayd_stream_id);
384 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
385 if (ret < 0) {
386 goto end;
387 }
388 } else if (msg.u.stream.net_index != -1) {
389 ERR("Network sequence index %d unknown. Not adding stream.",
390 msg.u.stream.net_index);
391 free(new_stream);
392 goto end;
393 }
394
395 if (ctx->on_recv_stream != NULL) {
396 ret = ctx->on_recv_stream(new_stream);
397 if (ret == 0) {
398 consumer_add_stream(new_stream);
399 } else if (ret < 0) {
400 goto end;
401 }
402 } else {
403 consumer_add_stream(new_stream);
404 }
405
406 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
407 msg.u.stream.path_name, fds[0], fds[1],
408 new_stream->relayd_stream_id);
409 break;
410 }
411 case LTTNG_CONSUMER_UPDATE_STREAM:
412 {
413 return -ENOSYS;
414 #if 0
415 if (ctx->on_update_stream != NULL) {
416 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
417 if (ret == 0) {
418 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
419 } else if (ret < 0) {
420 goto end;
421 }
422 } else {
423 consumer_change_stream_state(msg.u.stream.stream_key,
424 msg.u.stream.state);
425 }
426 #endif
427 break;
428 }
429 default:
430 break;
431 }
432 end:
433 /*
434 * Wake-up the other end by writing a null byte in the pipe
435 * (non-blocking). Important note: Because writing into the
436 * pipe is non-blocking (and therefore we allow dropping wakeup
437 * data, as long as there is wakeup data present in the pipe
438 * buffer to wake up the other end), the other end should
439 * perform the following sequence for waiting:
440 * 1) empty the pipe (reads).
441 * 2) perform update operation.
442 * 3) wait on the pipe (poll).
443 */
444 do {
445 ret = write(ctx->consumer_poll_pipe[1], "", 1);
446 } while (ret < 0 && errno == EINTR);
447 end_nosignal:
448 rcu_read_unlock();
449 return 0;
450 }
451
452 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
453 {
454 struct lttng_ust_object_data obj;
455
456 obj.handle = -1;
457 obj.shm_fd = chan->shm_fd;
458 obj.wait_fd = chan->wait_fd;
459 obj.memory_map_size = chan->mmap_len;
460 chan->handle = ustctl_map_channel(&obj);
461 if (!chan->handle) {
462 return -ENOMEM;
463 }
464 chan->wait_fd_is_copy = 1;
465 chan->shm_fd = -1;
466
467 return 0;
468 }
469
470 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
471 {
472 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
473 stream->hangup_flush_done = 1;
474 }
475
476 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
477 {
478 ustctl_unmap_channel(chan->handle);
479 }
480
481 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
482 {
483 struct lttng_ust_object_data obj;
484 int ret;
485
486 obj.handle = -1;
487 obj.shm_fd = stream->shm_fd;
488 obj.wait_fd = stream->wait_fd;
489 obj.memory_map_size = stream->mmap_len;
490 ret = ustctl_add_stream(stream->chan->handle, &obj);
491 if (ret)
492 return ret;
493 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
494 if (!stream->buf)
495 return -EBUSY;
496 /* ustctl_open_stream_read has closed the shm fd. */
497 stream->wait_fd_is_copy = 1;
498 stream->shm_fd = -1;
499
500 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
501 if (!stream->mmap_base) {
502 return -EINVAL;
503 }
504
505 return 0;
506 }
507
508 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
509 {
510 ustctl_close_stream_read(stream->chan->handle, stream->buf);
511 }
512
513
514 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
515 struct lttng_consumer_local_data *ctx)
516 {
517 unsigned long len;
518 int err;
519 long ret = 0;
520 struct lttng_ust_shm_handle *handle;
521 struct lttng_ust_lib_ring_buffer *buf;
522 char dummy;
523 ssize_t readlen;
524
525 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
526 stream->wait_fd, stream->key);
527
528 /* We can consume the 1 byte written into the wait_fd by UST */
529 if (!stream->hangup_flush_done) {
530 do {
531 readlen = read(stream->wait_fd, &dummy, 1);
532 } while (readlen == -1 && errno == EINTR);
533 if (readlen == -1) {
534 ret = readlen;
535 goto end;
536 }
537 }
538
539 buf = stream->buf;
540 handle = stream->chan->handle;
541 /* Get the next subbuffer */
542 err = ustctl_get_next_subbuf(handle, buf);
543 if (err != 0) {
544 ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
545 /*
546 * This is a debug message even for single-threaded consumer,
547 * because poll() have more relaxed criterions than get subbuf,
548 * so get_subbuf may fail for short race windows where poll()
549 * would issue wakeups.
550 */
551 DBG("Reserving sub buffer failed (everything is normal, "
552 "it is due to concurrency)");
553 goto end;
554 }
555 assert(stream->output == LTTNG_EVENT_MMAP);
556 /* read the used subbuffer size */
557 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
558 assert(err == 0);
559 /* write the subbuffer to the tracefile */
560 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
561 if (ret != len) {
562 /*
563 * display the error but continue processing to try
564 * to release the subbuffer
565 */
566 ERR("Error writing to tracefile");
567 }
568 err = ustctl_put_next_subbuf(handle, buf);
569 assert(err == 0);
570 end:
571 return ret;
572 }
573
574 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
575 {
576 int ret;
577
578 /* Opening the tracefile in write mode */
579 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
580 ret = run_as_open(stream->path_name,
581 O_WRONLY|O_CREAT|O_TRUNC,
582 S_IRWXU|S_IRWXG|S_IRWXO,
583 stream->uid, stream->gid);
584 if (ret < 0) {
585 ERR("Opening %s", stream->path_name);
586 PERROR("open");
587 goto error;
588 }
589 stream->out_fd = ret;
590 }
591
592 /* we return 0 to let the library handle the FD internally */
593 return 0;
594
595 error:
596 return ret;
597 }
This page took 0.041395 seconds and 5 git commands to generate.