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