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