Move add data stream to the data thread
[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 <lttng/ust-ctl.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/relayd/relayd.h>
36 #include <common/compat/fcntl.h>
37
38 #include "ust-consumer.h"
39
40 extern struct lttng_consumer_global_data consumer_data;
41 extern int consumer_poll_timeout;
42 extern volatile int consumer_quit;
43
44 /*
45 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
46 * compiled out, we isolate it in this library.
47 */
48 int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
49 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
50 {
51 return ustctl_get_mmap_read_offset(handle, buf, off);
52 };
53
54 /*
55 * Take a snapshot for a specific fd
56 *
57 * Returns 0 on success, < 0 on error
58 */
59 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
60 struct lttng_consumer_stream *stream)
61 {
62 int ret = 0;
63
64 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
65 if (ret != 0) {
66 errno = -ret;
67 PERROR("Getting sub-buffer snapshot.");
68 }
69
70 return ret;
71 }
72
73 /*
74 * Get the produced position
75 *
76 * Returns 0 on success, < 0 on error
77 */
78 int lttng_ustconsumer_get_produced_snapshot(
79 struct lttng_consumer_local_data *ctx,
80 struct lttng_consumer_stream *stream,
81 unsigned long *pos)
82 {
83 int ret;
84
85 ret = ustctl_snapshot_get_produced(stream->chan->handle,
86 stream->buf, pos);
87 if (ret != 0) {
88 errno = -ret;
89 PERROR("kernctl_snapshot_get_produced");
90 }
91
92 return ret;
93 }
94
95 /*
96 * Receive command from session daemon and process it.
97 *
98 * Return 1 on success else a negative value or 0.
99 */
100 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
101 int sock, struct pollfd *consumer_sockpoll)
102 {
103 ssize_t ret;
104 struct lttcomm_consumer_msg msg;
105
106 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
107 if (ret != sizeof(msg)) {
108 DBG("Consumer received unexpected message size %zd (expects %zu)",
109 ret, sizeof(msg));
110 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
111 return ret;
112 }
113 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
114 return -ENOENT;
115 }
116
117 /* relayd needs RCU read-side lock */
118 rcu_read_lock();
119
120 switch (msg.cmd_type) {
121 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
122 {
123 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
124 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
125 &msg.u.relayd_sock.sock);
126 goto end_nosignal;
127 }
128 case LTTNG_CONSUMER_ADD_CHANNEL:
129 {
130 struct lttng_consumer_channel *new_channel;
131 int fds[1];
132 size_t nb_fd = 1;
133
134 DBG("UST Consumer adding channel");
135
136 /* block */
137 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
138 rcu_read_unlock();
139 return -EINTR;
140 }
141 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
142 if (ret != sizeof(fds)) {
143 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
144 rcu_read_unlock();
145 return ret;
146 }
147
148 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
149
150 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
151 fds[0], -1,
152 msg.u.channel.mmap_len,
153 msg.u.channel.max_sb_size,
154 msg.u.channel.nb_init_streams);
155 if (new_channel == NULL) {
156 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
157 goto end_nosignal;
158 }
159 if (ctx->on_recv_channel != NULL) {
160 ret = ctx->on_recv_channel(new_channel);
161 if (ret == 0) {
162 consumer_add_channel(new_channel);
163 } else if (ret < 0) {
164 goto end_nosignal;
165 }
166 } else {
167 consumer_add_channel(new_channel);
168 }
169 goto end_nosignal;
170 }
171 case LTTNG_CONSUMER_ADD_STREAM:
172 {
173 struct lttng_consumer_stream *new_stream;
174 int fds[2];
175 size_t nb_fd = 2;
176 struct consumer_relayd_sock_pair *relayd = NULL;
177 int alloc_ret = 0;
178
179 DBG("UST Consumer adding stream");
180
181 /* block */
182 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
183 rcu_read_unlock();
184 return -EINTR;
185 }
186 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
187 if (ret != sizeof(fds)) {
188 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
189 rcu_read_unlock();
190 return ret;
191 }
192
193 DBG("Consumer command ADD_STREAM chan %d stream %d",
194 msg.u.stream.channel_key, msg.u.stream.stream_key);
195
196 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
197 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
198 msg.u.stream.stream_key,
199 fds[0], fds[1],
200 msg.u.stream.state,
201 msg.u.stream.mmap_len,
202 msg.u.stream.output,
203 msg.u.stream.path_name,
204 msg.u.stream.uid,
205 msg.u.stream.gid,
206 msg.u.stream.net_index,
207 msg.u.stream.metadata_flag,
208 &alloc_ret);
209 if (new_stream == NULL) {
210 switch (alloc_ret) {
211 case -ENOMEM:
212 case -EINVAL:
213 default:
214 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
215 break;
216 case -ENOENT:
217 /*
218 * We could not find the channel. Can happen if cpu hotplug
219 * happens while tearing down.
220 */
221 DBG3("Could not find channel");
222 break;
223 }
224 goto end_nosignal;
225 }
226
227 /* The stream is not metadata. Get relayd reference if exists. */
228 relayd = consumer_find_relayd(msg.u.stream.net_index);
229 if (relayd != NULL) {
230 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
231 /* Add stream on the relayd */
232 ret = relayd_add_stream(&relayd->control_sock,
233 msg.u.stream.name, msg.u.stream.path_name,
234 &new_stream->relayd_stream_id);
235 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
236 if (ret < 0) {
237 consumer_del_stream(new_stream, NULL);
238 goto end_nosignal;
239 }
240 } else if (msg.u.stream.net_index != -1) {
241 ERR("Network sequence index %d unknown. Not adding stream.",
242 msg.u.stream.net_index);
243 consumer_del_stream(new_stream, NULL);
244 goto end_nosignal;
245 }
246
247 /* Do actions once stream has been received. */
248 if (ctx->on_recv_stream) {
249 ret = ctx->on_recv_stream(new_stream);
250 if (ret < 0) {
251 consumer_del_stream(new_stream, NULL);
252 goto end_nosignal;
253 }
254 }
255
256 /* Send stream to the metadata thread */
257 if (new_stream->metadata_flag) {
258 do {
259 ret = write(ctx->consumer_metadata_pipe[1], &new_stream,
260 sizeof(new_stream));
261 } while (ret < 0 && errno == EINTR);
262 if (ret < 0) {
263 PERROR("write metadata pipe");
264 consumer_del_metadata_stream(new_stream, NULL);
265 goto end_nosignal;
266 }
267 } else {
268 do {
269 ret = write(ctx->consumer_poll_pipe[1], &new_stream,
270 sizeof(new_stream));
271 } while (ret < 0 && errno == EINTR);
272 if (ret < 0) {
273 PERROR("write data pipe");
274 consumer_del_stream(new_stream, NULL);
275 goto end_nosignal;
276 }
277 }
278
279 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64,
280 msg.u.stream.path_name, fds[0], fds[1],
281 new_stream->relayd_stream_id);
282 break;
283 }
284 case LTTNG_CONSUMER_DESTROY_RELAYD:
285 {
286 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
287 struct consumer_relayd_sock_pair *relayd;
288
289 DBG("UST consumer destroying relayd %" PRIu64, index);
290
291 /* Get relayd reference if exists. */
292 relayd = consumer_find_relayd(index);
293 if (relayd == NULL) {
294 ERR("Unable to find relayd %" PRIu64, index);
295 goto end_nosignal;
296 }
297
298 /*
299 * Each relayd socket pair has a refcount of stream attached to it
300 * which tells if the relayd is still active or not depending on the
301 * refcount value.
302 *
303 * This will set the destroy flag of the relayd object and destroy it
304 * if the refcount reaches zero when called.
305 *
306 * The destroy can happen either here or when a stream fd hangs up.
307 */
308 consumer_flag_relayd_for_destroy(relayd);
309
310 goto end_nosignal;
311 }
312 case LTTNG_CONSUMER_UPDATE_STREAM:
313 {
314 rcu_read_unlock();
315 return -ENOSYS;
316 #if 0
317 if (ctx->on_update_stream != NULL) {
318 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
319 if (ret == 0) {
320 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
321 } else if (ret < 0) {
322 goto end;
323 }
324 } else {
325 consumer_change_stream_state(msg.u.stream.stream_key,
326 msg.u.stream.state);
327 }
328 break;
329 #endif
330 }
331 default:
332 break;
333 }
334
335 end_nosignal:
336 rcu_read_unlock();
337
338 /*
339 * Return 1 to indicate success since the 0 value can be a socket
340 * shutdown during the recv() or send() call.
341 */
342 return 1;
343 }
344
345 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
346 {
347 struct lttng_ust_object_data obj;
348
349 obj.handle = -1;
350 obj.shm_fd = chan->shm_fd;
351 obj.wait_fd = chan->wait_fd;
352 obj.memory_map_size = chan->mmap_len;
353 chan->handle = ustctl_map_channel(&obj);
354 if (!chan->handle) {
355 return -ENOMEM;
356 }
357 chan->wait_fd_is_copy = 1;
358 chan->shm_fd = -1;
359
360 return 0;
361 }
362
363 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
364 {
365 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
366 stream->hangup_flush_done = 1;
367 }
368
369 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
370 {
371 ustctl_unmap_channel(chan->handle);
372 }
373
374 int lttng_ustconsumer_add_stream(struct lttng_consumer_stream *stream)
375 {
376 struct lttng_ust_object_data obj;
377 int ret;
378
379 obj.handle = -1;
380 obj.shm_fd = stream->shm_fd;
381 obj.wait_fd = stream->wait_fd;
382 obj.memory_map_size = stream->mmap_len;
383 ret = ustctl_add_stream(stream->chan->handle, &obj);
384 if (ret) {
385 ERR("UST ctl add_stream failed with ret %d", ret);
386 goto error;
387 }
388
389 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
390 if (!stream->buf) {
391 ERR("UST ctl open_stream_read failed");
392 ret = -EBUSY;
393 goto error;
394 }
395
396 /* ustctl_open_stream_read has closed the shm fd. */
397 stream->wait_fd_is_copy = 1;
398 stream->shm_fd = -1;
399
400 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
401 if (!stream->mmap_base) {
402 ERR("UST ctl get_mmap_base failed");
403 ret = -EINVAL;
404 goto mmap_error;
405 }
406
407 return 0;
408
409 mmap_error:
410 ustctl_close_stream_read(stream->chan->handle, stream->buf);
411 error:
412 return ret;
413 }
414
415 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
416 {
417 ustctl_close_stream_read(stream->chan->handle, stream->buf);
418 }
419
420
421 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
422 struct lttng_consumer_local_data *ctx)
423 {
424 unsigned long len, subbuf_size, padding;
425 int err;
426 long ret = 0;
427 struct lttng_ust_shm_handle *handle;
428 struct lttng_ust_lib_ring_buffer *buf;
429 char dummy;
430 ssize_t readlen;
431
432 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
433 stream->wait_fd, stream->key);
434
435 /* We can consume the 1 byte written into the wait_fd by UST */
436 if (!stream->hangup_flush_done) {
437 do {
438 readlen = read(stream->wait_fd, &dummy, 1);
439 } while (readlen == -1 && errno == EINTR);
440 if (readlen == -1) {
441 ret = readlen;
442 goto end;
443 }
444 }
445
446 buf = stream->buf;
447 handle = stream->chan->handle;
448 /* Get the next subbuffer */
449 err = ustctl_get_next_subbuf(handle, buf);
450 if (err != 0) {
451 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
452 /*
453 * This is a debug message even for single-threaded consumer,
454 * because poll() have more relaxed criterions than get subbuf,
455 * so get_subbuf may fail for short race windows where poll()
456 * would issue wakeups.
457 */
458 DBG("Reserving sub buffer failed (everything is normal, "
459 "it is due to concurrency)");
460 goto end;
461 }
462 assert(stream->output == LTTNG_EVENT_MMAP);
463 /* Get the full padded subbuffer size */
464 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
465 assert(err == 0);
466
467 /* Get subbuffer data size (without padding) */
468 err = ustctl_get_subbuf_size(handle, buf, &subbuf_size);
469 assert(err == 0);
470
471 /* Make sure we don't get a subbuffer size bigger than the padded */
472 assert(len >= subbuf_size);
473
474 padding = len - subbuf_size;
475 /* write the subbuffer to the tracefile */
476 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding);
477 /*
478 * The mmap operation should write subbuf_size amount of data when network
479 * streaming or the full padding (len) size when we are _not_ streaming.
480 */
481 if ((ret != subbuf_size && stream->net_seq_idx != -1) ||
482 (ret != len && stream->net_seq_idx == -1)) {
483 /*
484 * Display the error but continue processing to try to release the
485 * subbuffer
486 */
487 ERR("Error writing to tracefile "
488 "(ret: %zd != len: %lu != subbuf_size: %lu)",
489 ret, len, subbuf_size);
490 }
491 err = ustctl_put_next_subbuf(handle, buf);
492 assert(err == 0);
493 end:
494 return ret;
495 }
496
497 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
498 {
499 int ret;
500
501 /* Opening the tracefile in write mode */
502 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
503 ret = run_as_open(stream->path_name,
504 O_WRONLY|O_CREAT|O_TRUNC,
505 S_IRWXU|S_IRWXG|S_IRWXO,
506 stream->uid, stream->gid);
507 if (ret < 0) {
508 ERR("Opening %s", stream->path_name);
509 PERROR("open");
510 goto error;
511 }
512 stream->out_fd = ret;
513 }
514
515 ret = lttng_ustconsumer_add_stream(stream);
516 if (ret) {
517 consumer_del_stream(stream, NULL);
518 ret = -1;
519 goto error;
520 }
521
522 /* we return 0 to let the library handle the FD internally */
523 return 0;
524
525 error:
526 return ret;
527 }
This page took 0.039835 seconds and 4 git commands to generate.