Fix: kernel consumer: data_pending check if endpoint active
[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>
acdb9057 37#include <common/pipe.h>
00e2e675 38#include <common/relayd/relayd.h>
fe4477ee 39#include <common/utils.h>
07b86b52 40#include <common/consumer-stream.h>
0857097f 41
10a8a223 42#include "kernel-consumer.h"
3bd1e081
MD
43
44extern struct lttng_consumer_global_data consumer_data;
45extern int consumer_poll_timeout;
46extern volatile int consumer_quit;
47
3bd1e081
MD
48/*
49 * Take a snapshot for a specific fd
50 *
51 * Returns 0 on success, < 0 on error
52 */
ffe60014 53int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
54{
55 int ret = 0;
56 int infd = stream->wait_fd;
57
58 ret = kernctl_snapshot(infd);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
61 perror("Getting sub-buffer snapshot.");
62 }
63
64 return ret;
65}
66
67/*
68 * Get the produced position
69 *
70 * Returns 0 on success, < 0 on error
71 */
ffe60014 72int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
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
07b86b52
JD
87/*
88 * Get the consumerd position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
93 unsigned long *pos)
94{
95 int ret;
96 int infd = stream->wait_fd;
97
98 ret = kernctl_snapshot_get_consumed(infd, pos);
99 if (ret != 0) {
100 errno = -ret;
101 perror("kernctl_snapshot_get_consumed");
102 }
103
104 return ret;
105}
106
07b86b52
JD
107/*
108 * Take a snapshot of all the stream of a channel
109 *
110 * Returns 0 on success, < 0 on error
111 */
112int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
5c786ded
JD
113 uint64_t relayd_id, uint64_t max_stream_size,
114 struct lttng_consumer_local_data *ctx)
07b86b52
JD
115{
116 int ret;
117 unsigned long consumed_pos, produced_pos;
118 struct lttng_consumer_channel *channel;
119 struct lttng_consumer_stream *stream;
120
121 DBG("Kernel consumer snapshot channel %lu", key);
122
123 rcu_read_lock();
124
125 channel = consumer_find_channel(key);
126 if (!channel) {
127 ERR("No channel found for key %lu", key);
128 ret = -1;
129 goto end;
130 }
131
132 /* Splice is not supported yet for channel snapshot. */
133 if (channel->output != CONSUMER_CHANNEL_MMAP) {
134 ERR("Unsupported output %d", channel->output);
135 ret = -1;
136 goto end;
137 }
138
10a50311 139 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
07b86b52
JD
140 /*
141 * Lock stream because we are about to change its state.
142 */
143 pthread_mutex_lock(&stream->lock);
144
29decac3
DG
145 /*
146 * Assign the received relayd ID so we can use it for streaming. The streams
147 * are not visible to anyone so this is OK to change it.
148 */
07b86b52
JD
149 stream->net_seq_idx = relayd_id;
150 channel->relayd_id = relayd_id;
151 if (relayd_id != (uint64_t) -1ULL) {
10a50311 152 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
153 if (ret < 0) {
154 ERR("sending stream to relayd");
155 goto end_unlock;
156 }
07b86b52
JD
157 } else {
158 ret = utils_create_stream_file(path, stream->name,
10a50311
JD
159 stream->chan->tracefile_size,
160 stream->tracefile_count_current,
07b86b52
JD
161 stream->uid, stream->gid);
162 if (ret < 0) {
163 ERR("utils_create_stream_file");
164 goto end_unlock;
165 }
166
167 stream->out_fd = ret;
168 stream->tracefile_size_current = 0;
169
170 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path,
171 stream->name, stream->key);
172 }
173
174 ret = kernctl_buffer_flush(stream->wait_fd);
175 if (ret < 0) {
10a50311 176 ERR("Failed to flush kernel stream");
07b86b52
JD
177 goto end_unlock;
178 }
179
180 ret = lttng_kconsumer_take_snapshot(stream);
181 if (ret < 0) {
182 ERR("Taking kernel snapshot");
183 goto end_unlock;
184 }
185
186 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
187 if (ret < 0) {
188 ERR("Produced kernel snapshot position");
189 goto end_unlock;
190 }
191
192 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
193 if (ret < 0) {
194 ERR("Consumerd kernel snapshot position");
195 goto end_unlock;
196 }
197
198 if (stream->max_sb_size == 0) {
199 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
200 &stream->max_sb_size);
201 if (ret < 0) {
202 ERR("Getting kernel max_sb_size");
203 goto end_unlock;
204 }
205 }
206
5c786ded
JD
207 /*
208 * The original value is sent back if max stream size is larger than
209 * the possible size of the snapshot. Also, we asume that the session
210 * daemon should never send a maximum stream size that is lower than
211 * subbuffer size.
212 */
213 consumed_pos = consumer_get_consumed_maxsize(consumed_pos,
214 produced_pos, max_stream_size);
215
07b86b52
JD
216 while (consumed_pos < produced_pos) {
217 ssize_t read_len;
218 unsigned long len, padded_len;
219
220 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
221
222 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
223 if (ret < 0) {
224 if (errno != EAGAIN) {
225 PERROR("kernctl_get_subbuf snapshot");
226 goto end_unlock;
227 }
228 DBG("Kernel consumer get subbuf failed. Skipping it.");
229 consumed_pos += stream->max_sb_size;
230 continue;
231 }
232
233 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
234 if (ret < 0) {
235 ERR("Snapshot kernctl_get_subbuf_size");
29decac3 236 goto error_put_subbuf;
07b86b52
JD
237 }
238
239 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
240 if (ret < 0) {
241 ERR("Snapshot kernctl_get_padded_subbuf_size");
29decac3 242 goto error_put_subbuf;
07b86b52
JD
243 }
244
245 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
246 padded_len - len);
247 /*
29decac3
DG
248 * We write the padded len in local tracefiles but the data len
249 * when using a relay. Display the error but continue processing
250 * to try to release the subbuffer.
07b86b52
JD
251 */
252 if (relayd_id != (uint64_t) -1ULL) {
253 if (read_len != len) {
254 ERR("Error sending to the relay (ret: %zd != len: %lu)",
255 read_len, len);
256 }
257 } else {
258 if (read_len != padded_len) {
259 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
260 read_len, padded_len);
261 }
262 }
263
264 ret = kernctl_put_subbuf(stream->wait_fd);
265 if (ret < 0) {
266 ERR("Snapshot kernctl_put_subbuf");
267 goto end_unlock;
268 }
269 consumed_pos += stream->max_sb_size;
270 }
271
272 if (relayd_id == (uint64_t) -1ULL) {
fdf9986c
MD
273 if (stream->out_fd >= 0) {
274 ret = close(stream->out_fd);
275 if (ret < 0) {
276 PERROR("Kernel consumer snapshot close out_fd");
277 goto end_unlock;
278 }
279 stream->out_fd = -1;
07b86b52 280 }
07b86b52
JD
281 } else {
282 close_relayd_stream(stream);
283 stream->net_seq_idx = (uint64_t) -1ULL;
284 }
285 pthread_mutex_unlock(&stream->lock);
286 }
287
288 /* All good! */
289 ret = 0;
290 goto end;
291
29decac3
DG
292error_put_subbuf:
293 ret = kernctl_put_subbuf(stream->wait_fd);
294 if (ret < 0) {
295 ERR("Snapshot kernctl_put_subbuf error path");
296 }
07b86b52
JD
297end_unlock:
298 pthread_mutex_unlock(&stream->lock);
299end:
300 rcu_read_unlock();
301 return ret;
302}
303
304/*
305 * Read the whole metadata available for a snapshot.
306 *
307 * Returns 0 on success, < 0 on error
308 */
309int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
e2039c7a 310 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
07b86b52 311{
d771f832
DG
312 int ret, use_relayd = 0;
313 ssize_t ret_read;
07b86b52
JD
314 struct lttng_consumer_channel *metadata_channel;
315 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
316
317 assert(ctx);
07b86b52
JD
318
319 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
320 key, path);
321
322 rcu_read_lock();
323
324 metadata_channel = consumer_find_channel(key);
325 if (!metadata_channel) {
d771f832 326 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
07b86b52 327 ret = -1;
d771f832 328 goto error;
07b86b52
JD
329 }
330
331 metadata_stream = metadata_channel->metadata_stream;
332 assert(metadata_stream);
333
d771f832 334 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 335 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
336 use_relayd = 1;
337 }
338
339 if (use_relayd) {
10a50311 340 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 341 if (ret < 0) {
d771f832 342 goto error;
e2039c7a 343 }
e2039c7a
JD
344 } else {
345 ret = utils_create_stream_file(path, metadata_stream->name,
346 metadata_stream->chan->tracefile_size,
347 metadata_stream->tracefile_count_current,
348 metadata_stream->uid, metadata_stream->gid);
349 if (ret < 0) {
d771f832 350 goto error;
e2039c7a
JD
351 }
352 metadata_stream->out_fd = ret;
07b86b52 353 }
07b86b52 354
d771f832
DG
355 do {
356 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
357 if (ret_read < 0) {
358 if (ret_read != -EPERM) {
359 ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)",
360 ret_read);
361 goto error;
07b86b52 362 }
d771f832 363 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
364 continue;
365 }
d771f832 366 } while (ret_read >= 0);
07b86b52 367
d771f832
DG
368 if (use_relayd) {
369 close_relayd_stream(metadata_stream);
370 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
371 } else {
fdf9986c
MD
372 if (metadata_stream->out_fd >= 0) {
373 ret = close(metadata_stream->out_fd);
374 if (ret < 0) {
375 PERROR("Kernel consumer snapshot metadata close out_fd");
376 /*
377 * Don't go on error here since the snapshot was successful at this
378 * point but somehow the close failed.
379 */
380 }
381 metadata_stream->out_fd = -1;
e2039c7a 382 }
e2039c7a
JD
383 }
384
07b86b52 385 ret = 0;
d771f832 386
cf53a8a6
JD
387 cds_list_del(&metadata_stream->send_node);
388 consumer_stream_destroy(metadata_stream, NULL);
389 metadata_channel->metadata_stream = NULL;
d771f832 390error:
07b86b52
JD
391 rcu_read_unlock();
392 return ret;
393}
394
1803a064
MD
395/*
396 * Receive command from session daemon and process it.
397 *
398 * Return 1 on success else a negative value or 0.
399 */
3bd1e081
MD
400int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
401 int sock, struct pollfd *consumer_sockpoll)
402{
403 ssize_t ret;
f50f23d9 404 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
405 struct lttcomm_consumer_msg msg;
406
407 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
408 if (ret != sizeof(msg)) {
1803a064 409 if (ret > 0) {
c6857fcf 410 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
411 ret = -1;
412 }
3bd1e081
MD
413 return ret;
414 }
415 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
416 /*
417 * Notify the session daemon that the command is completed.
418 *
419 * On transport layer error, the function call will print an error
420 * message so handling the returned code is a bit useless since we
421 * return an error code anyway.
422 */
423 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
424 return -ENOENT;
425 }
426
b0b335c8
MD
427 /* relayd needs RCU read-side protection */
428 rcu_read_lock();
429
3bd1e081 430 switch (msg.cmd_type) {
00e2e675
DG
431 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
432 {
f50f23d9 433 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
434 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
435 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 436 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
437 goto end_nosignal;
438 }
3bd1e081
MD
439 case LTTNG_CONSUMER_ADD_CHANNEL:
440 {
441 struct lttng_consumer_channel *new_channel;
e43c41c5 442 int ret_recv;
3bd1e081 443
f50f23d9
DG
444 /* First send a status message before receiving the fds. */
445 ret = consumer_send_status_msg(sock, ret_code);
446 if (ret < 0) {
447 /* Somehow, the session daemon is not responding anymore. */
1803a064 448 goto error_fatal;
f50f23d9 449 }
d88aee68 450 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 451 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
452 msg.u.channel.session_id, msg.u.channel.pathname,
453 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
454 msg.u.channel.relayd_id, msg.u.channel.output,
455 msg.u.channel.tracefile_size,
1950109e 456 msg.u.channel.tracefile_count, 0,
2bba9e53 457 msg.u.channel.monitor);
3bd1e081 458 if (new_channel == NULL) {
f73fabfd 459 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
460 goto end_nosignal;
461 }
ffe60014 462 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
463 switch (msg.u.channel.output) {
464 case LTTNG_EVENT_SPLICE:
465 new_channel->output = CONSUMER_CHANNEL_SPLICE;
466 break;
467 case LTTNG_EVENT_MMAP:
468 new_channel->output = CONSUMER_CHANNEL_MMAP;
469 break;
470 default:
471 ERR("Channel output unknown %d", msg.u.channel.output);
472 goto end_nosignal;
473 }
ffe60014
DG
474
475 /* Translate and save channel type. */
476 switch (msg.u.channel.type) {
477 case CONSUMER_CHANNEL_TYPE_DATA:
478 case CONSUMER_CHANNEL_TYPE_METADATA:
479 new_channel->type = msg.u.channel.type;
480 break;
481 default:
482 assert(0);
483 goto end_nosignal;
484 };
485
3bd1e081 486 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
487 ret_recv = ctx->on_recv_channel(new_channel);
488 if (ret_recv == 0) {
489 ret = consumer_add_channel(new_channel, ctx);
490 } else if (ret_recv < 0) {
3bd1e081
MD
491 goto end_nosignal;
492 }
493 } else {
e43c41c5 494 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 495 }
e43c41c5
JD
496
497 /* If we received an error in add_channel, we need to report it. */
821fffb2 498 if (ret < 0) {
1803a064
MD
499 ret = consumer_send_status_msg(sock, ret);
500 if (ret < 0) {
501 goto error_fatal;
502 }
e43c41c5
JD
503 goto end_nosignal;
504 }
505
3bd1e081
MD
506 goto end_nosignal;
507 }
508 case LTTNG_CONSUMER_ADD_STREAM:
509 {
dae10966
DG
510 int fd;
511 struct lttng_pipe *stream_pipe;
00e2e675 512 struct lttng_consumer_stream *new_stream;
ffe60014 513 struct lttng_consumer_channel *channel;
c80048c6 514 int alloc_ret = 0;
3bd1e081 515
ffe60014
DG
516 /*
517 * Get stream's channel reference. Needed when adding the stream to the
518 * global hash table.
519 */
520 channel = consumer_find_channel(msg.u.stream.channel_key);
521 if (!channel) {
522 /*
523 * We could not find the channel. Can happen if cpu hotplug
524 * happens while tearing down.
525 */
d88aee68 526 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
ffe60014
DG
527 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
528 }
529
f50f23d9
DG
530 /* First send a status message before receiving the fds. */
531 ret = consumer_send_status_msg(sock, ret_code);
1803a064 532 if (ret < 0) {
d771f832 533 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
534 goto error_fatal;
535 }
536 if (ret_code != LTTNG_OK) {
d771f832 537 /* Channel was not found. */
f50f23d9
DG
538 goto end_nosignal;
539 }
540
d771f832 541 /* Blocking call */
3bd1e081 542 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 543 rcu_read_unlock();
3bd1e081
MD
544 return -EINTR;
545 }
00e2e675
DG
546
547 /* Get stream file descriptor from socket */
f2fc6720
MD
548 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
549 if (ret != sizeof(fd)) {
f73fabfd 550 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 551 rcu_read_unlock();
3bd1e081
MD
552 return ret;
553 }
3bd1e081 554
f50f23d9
DG
555 /*
556 * Send status code to session daemon only if the recv works. If the
557 * above recv() failed, the session daemon is notified through the
558 * error socket and the teardown is eventually done.
559 */
560 ret = consumer_send_status_msg(sock, ret_code);
561 if (ret < 0) {
562 /* Somehow, the session daemon is not responding anymore. */
563 goto end_nosignal;
564 }
565
ffe60014
DG
566 new_stream = consumer_allocate_stream(channel->key,
567 fd,
568 LTTNG_CONSUMER_ACTIVE_STREAM,
569 channel->name,
570 channel->uid,
571 channel->gid,
572 channel->relayd_id,
573 channel->session_id,
574 msg.u.stream.cpu,
575 &alloc_ret,
4891ece8
DG
576 channel->type,
577 channel->monitor);
3bd1e081 578 if (new_stream == NULL) {
c80048c6
MD
579 switch (alloc_ret) {
580 case -ENOMEM:
581 case -EINVAL:
582 default:
583 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
584 break;
c80048c6 585 }
3f8e211f 586 goto end_nosignal;
3bd1e081 587 }
d771f832 588
ffe60014
DG
589 new_stream->chan = channel;
590 new_stream->wait_fd = fd;
07b86b52
JD
591 switch (channel->output) {
592 case CONSUMER_CHANNEL_SPLICE:
593 new_stream->output = LTTNG_EVENT_SPLICE;
594 break;
595 case CONSUMER_CHANNEL_MMAP:
596 new_stream->output = LTTNG_EVENT_MMAP;
597 break;
598 default:
599 ERR("Stream output unknown %d", channel->output);
600 goto end_nosignal;
601 }
00e2e675 602
a0c83db9
DG
603 /*
604 * We've just assigned the channel to the stream so increment the
07b86b52
JD
605 * refcount right now. We don't need to increment the refcount for
606 * streams in no monitor because we handle manually the cleanup of
607 * those. It is very important to make sure there is NO prior
608 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 609 */
07b86b52
JD
610 if (channel->monitor) {
611 uatomic_inc(&new_stream->chan->refcount);
612 }
9d9353f9 613
fb3a43a9
DG
614 /*
615 * The buffer flush is done on the session daemon side for the kernel
616 * so no need for the stream "hangup_flush_done" variable to be
617 * tracked. This is important for a kernel stream since we don't rely
618 * on the flush state of the stream to read data. It's not the case for
619 * user space tracing.
620 */
621 new_stream->hangup_flush_done = 0;
622
633d0084
DG
623 if (ctx->on_recv_stream) {
624 ret = ctx->on_recv_stream(new_stream);
625 if (ret < 0) {
d771f832 626 consumer_stream_free(new_stream);
633d0084 627 goto end_nosignal;
fb3a43a9 628 }
633d0084 629 }
fb3a43a9 630
07b86b52
JD
631 if (new_stream->metadata_flag) {
632 channel->metadata_stream = new_stream;
633 }
634
2bba9e53
DG
635 /* Do not monitor this stream. */
636 if (!channel->monitor) {
5eecee74 637 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 638 "relayd id %" PRIu64, new_stream->name,
5eecee74 639 new_stream->net_seq_idx);
10a50311 640 cds_list_add(&new_stream->send_node, &channel->streams.head);
6dc3064a
DG
641 break;
642 }
643
e1b71bdc
DG
644 /* Send stream to relayd if the stream has an ID. */
645 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
646 ret = consumer_send_relayd_stream(new_stream,
647 new_stream->chan->pathname);
e1b71bdc
DG
648 if (ret < 0) {
649 consumer_stream_free(new_stream);
650 goto end_nosignal;
651 }
e2039c7a
JD
652 }
653
50f8ae69 654 /* Get the right pipe where the stream will be sent. */
633d0084 655 if (new_stream->metadata_flag) {
dae10966 656 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 657 } else {
dae10966 658 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
659 }
660
dae10966 661 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 662 if (ret < 0) {
dae10966 663 ERR("Consumer write %s stream to pipe %d",
50f8ae69 664 new_stream->metadata_flag ? "metadata" : "data",
dae10966 665 lttng_pipe_get_writefd(stream_pipe));
d771f832 666 consumer_stream_free(new_stream);
50f8ae69 667 goto end_nosignal;
3bd1e081 668 }
d9904f84
DG
669 /* Successfully sent to the right thread. */
670 new_stream->globally_visible = 1;
00e2e675 671
50f8ae69 672 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 673 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
674 break;
675 }
676 case LTTNG_CONSUMER_UPDATE_STREAM:
677 {
3f8e211f
DG
678 rcu_read_unlock();
679 return -ENOSYS;
680 }
681 case LTTNG_CONSUMER_DESTROY_RELAYD:
682 {
a6ba4fe1 683 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
684 struct consumer_relayd_sock_pair *relayd;
685
a6ba4fe1 686 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
687
688 /* Get relayd reference if exists. */
a6ba4fe1 689 relayd = consumer_find_relayd(index);
3f8e211f 690 if (relayd == NULL) {
3448e266 691 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 692 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 693 }
3f8e211f 694
a6ba4fe1
DG
695 /*
696 * Each relayd socket pair has a refcount of stream attached to it
697 * which tells if the relayd is still active or not depending on the
698 * refcount value.
699 *
700 * This will set the destroy flag of the relayd object and destroy it
701 * if the refcount reaches zero when called.
702 *
703 * The destroy can happen either here or when a stream fd hangs up.
704 */
f50f23d9
DG
705 if (relayd) {
706 consumer_flag_relayd_for_destroy(relayd);
707 }
708
709 ret = consumer_send_status_msg(sock, ret_code);
710 if (ret < 0) {
711 /* Somehow, the session daemon is not responding anymore. */
1803a064 712 goto error_fatal;
f50f23d9 713 }
3f8e211f 714
3f8e211f 715 goto end_nosignal;
3bd1e081 716 }
6d805429 717 case LTTNG_CONSUMER_DATA_PENDING:
53632229 718 {
c8f59ee5 719 int32_t ret;
6d805429 720 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 721
6d805429 722 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 723
6d805429 724 ret = consumer_data_pending(id);
c8f59ee5
DG
725
726 /* Send back returned value to session daemon */
727 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
728 if (ret < 0) {
6d805429 729 PERROR("send data pending ret code");
1803a064 730 goto error_fatal;
c8f59ee5 731 }
f50f23d9
DG
732
733 /*
734 * No need to send back a status message since the data pending
735 * returned value is the response.
736 */
c8f59ee5 737 break;
53632229 738 }
6dc3064a
DG
739 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
740 {
07b86b52
JD
741 if (msg.u.snapshot_channel.metadata == 1) {
742 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
e2039c7a
JD
743 msg.u.snapshot_channel.pathname,
744 msg.u.snapshot_channel.relayd_id, ctx);
07b86b52
JD
745 if (ret < 0) {
746 ERR("Snapshot metadata failed");
747 ret_code = LTTNG_ERR_KERN_META_FAIL;
748 }
749 } else {
750 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
751 msg.u.snapshot_channel.pathname,
5c786ded
JD
752 msg.u.snapshot_channel.relayd_id,
753 msg.u.snapshot_channel.max_stream_size,
754 ctx);
07b86b52
JD
755 if (ret < 0) {
756 ERR("Snapshot channel failed");
757 ret_code = LTTNG_ERR_KERN_CHAN_FAIL;
758 }
759 }
760
6dc3064a
DG
761 ret = consumer_send_status_msg(sock, ret_code);
762 if (ret < 0) {
763 /* Somehow, the session daemon is not responding anymore. */
764 goto end_nosignal;
765 }
766 break;
767 }
07b86b52
JD
768 case LTTNG_CONSUMER_DESTROY_CHANNEL:
769 {
770 uint64_t key = msg.u.destroy_channel.key;
771 struct lttng_consumer_channel *channel;
772
773 channel = consumer_find_channel(key);
774 if (!channel) {
775 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
776 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
777 }
778
779 ret = consumer_send_status_msg(sock, ret_code);
780 if (ret < 0) {
781 /* Somehow, the session daemon is not responding anymore. */
782 goto end_nosignal;
783 }
784
785 /*
786 * This command should ONLY be issued for channel with streams set in
787 * no monitor mode.
788 */
789 assert(!channel->monitor);
790
791 /*
792 * The refcount should ALWAYS be 0 in the case of a channel in no
793 * monitor mode.
794 */
795 assert(!uatomic_sub_return(&channel->refcount, 1));
796
797 consumer_del_channel(channel);
798
799 goto end_nosignal;
800 }
3bd1e081 801 default:
3f8e211f 802 goto end_nosignal;
3bd1e081 803 }
3f8e211f 804
3bd1e081 805end_nosignal:
b0b335c8 806 rcu_read_unlock();
4cbc1a04
DG
807
808 /*
809 * Return 1 to indicate success since the 0 value can be a socket
810 * shutdown during the recv() or send() call.
811 */
812 return 1;
1803a064
MD
813
814error_fatal:
815 rcu_read_unlock();
816 /* This will issue a consumer stop. */
817 return -1;
3bd1e081 818}
d41f73b7
MD
819
820/*
821 * Consume data on a file descriptor and write it on a trace file.
822 */
4078b776 823ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
824 struct lttng_consumer_local_data *ctx)
825{
1d4dfdef 826 unsigned long len, subbuf_size, padding;
d41f73b7 827 int err;
4078b776 828 ssize_t ret = 0;
d41f73b7
MD
829 int infd = stream->wait_fd;
830
831 DBG("In read_subbuffer (infd : %d)", infd);
832 /* Get the next subbuffer */
833 err = kernctl_get_next_subbuf(infd);
834 if (err != 0) {
1d4dfdef 835 ret = err;
d41f73b7
MD
836 /*
837 * This is a debug message even for single-threaded consumer,
838 * because poll() have more relaxed criterions than get subbuf,
839 * so get_subbuf may fail for short race windows where poll()
840 * would issue wakeups.
841 */
842 DBG("Reserving sub buffer failed (everything is normal, "
843 "it is due to concurrency)");
844 goto end;
845 }
846
1d4dfdef
DG
847 /* Get the full subbuffer size including padding */
848 err = kernctl_get_padded_subbuf_size(infd, &len);
849 if (err != 0) {
850 errno = -err;
851 perror("Getting sub-buffer len failed.");
852 ret = err;
853 goto end;
854 }
855
ffe60014 856 switch (stream->chan->output) {
07b86b52 857 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
858 /*
859 * XXX: The lttng-modules splice "actor" does not handle copying
860 * partial pages hence only using the subbuffer size without the
861 * padding makes the splice fail.
862 */
863 subbuf_size = len;
864 padding = 0;
865
866 /* splice the subbuffer to the tracefile */
91dfef6e
DG
867 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
868 padding);
869 /*
870 * XXX: Splice does not support network streaming so the return value
871 * is simply checked against subbuf_size and not like the mmap() op.
872 */
1d4dfdef
DG
873 if (ret != subbuf_size) {
874 /*
875 * display the error but continue processing to try
876 * to release the subbuffer
877 */
878 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
879 ret, subbuf_size);
880 }
881 break;
07b86b52 882 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
883 /* Get subbuffer size without padding */
884 err = kernctl_get_subbuf_size(infd, &subbuf_size);
885 if (err != 0) {
886 errno = -err;
887 perror("Getting sub-buffer len failed.");
888 ret = err;
889 goto end;
890 }
47e81c02 891
1d4dfdef
DG
892 /* Make sure the tracer is not gone mad on us! */
893 assert(len >= subbuf_size);
894
895 padding = len - subbuf_size;
896
897 /* write the subbuffer to the tracefile */
91dfef6e
DG
898 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
899 padding);
900 /*
901 * The mmap operation should write subbuf_size amount of data when
902 * network streaming or the full padding (len) size when we are _not_
903 * streaming.
904 */
d88aee68
DG
905 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
906 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 907 /*
91dfef6e
DG
908 * Display the error but continue processing to try to release the
909 * subbuffer
1d4dfdef 910 */
91dfef6e
DG
911 ERR("Error writing to tracefile "
912 "(ret: %zd != len: %lu != subbuf_size: %lu)",
913 ret, len, subbuf_size);
1d4dfdef
DG
914 }
915 break;
916 default:
917 ERR("Unknown output method");
918 ret = -1;
d41f73b7
MD
919 }
920
921 err = kernctl_put_next_subbuf(infd);
922 if (err != 0) {
21073eaa 923 errno = -err;
d41f73b7
MD
924 if (errno == EFAULT) {
925 perror("Error in unreserving sub buffer\n");
926 } else if (errno == EIO) {
927 /* Should never happen with newer LTTng versions */
928 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
929 }
21073eaa
DG
930
931 ret = -err;
d41f73b7
MD
932 goto end;
933 }
934
935end:
936 return ret;
937}
938
939int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
940{
941 int ret;
ffe60014
DG
942
943 assert(stream);
944
2bba9e53
DG
945 /*
946 * Don't create anything if this is set for streaming or should not be
947 * monitored.
948 */
949 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
950 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
951 stream->chan->tracefile_size, stream->tracefile_count_current,
952 stream->uid, stream->gid);
953 if (ret < 0) {
954 goto error;
955 }
956 stream->out_fd = ret;
957 stream->tracefile_size_current = 0;
ffe60014 958 }
d41f73b7 959
d41f73b7
MD
960 if (stream->output == LTTNG_EVENT_MMAP) {
961 /* get the len of the mmap region */
962 unsigned long mmap_len;
963
964 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
965 if (ret != 0) {
87dc6a9c 966 errno = -ret;
ffe60014 967 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
968 goto error_close_fd;
969 }
970 stream->mmap_len = (size_t) mmap_len;
971
ffe60014
DG
972 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
973 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 974 if (stream->mmap_base == MAP_FAILED) {
ffe60014 975 PERROR("Error mmaping");
d41f73b7
MD
976 ret = -1;
977 goto error_close_fd;
978 }
979 }
980
981 /* we return 0 to let the library handle the FD internally */
982 return 0;
983
984error_close_fd:
2f225ce2 985 if (stream->out_fd >= 0) {
d41f73b7
MD
986 int err;
987
988 err = close(stream->out_fd);
989 assert(!err);
2f225ce2 990 stream->out_fd = -1;
d41f73b7
MD
991 }
992error:
993 return ret;
994}
995
ca22feea
DG
996/*
997 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
998 * stream. Consumer data lock MUST be acquired before calling this function
999 * and the stream lock.
ca22feea 1000 *
6d805429 1001 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1002 * data is available for trace viewer reading.
1003 */
6d805429 1004int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1005{
1006 int ret;
1007
1008 assert(stream);
1009
873b9e9a
MD
1010 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1011 ret = 0;
1012 goto end;
1013 }
1014
ca22feea
DG
1015 ret = kernctl_get_next_subbuf(stream->wait_fd);
1016 if (ret == 0) {
1017 /* There is still data so let's put back this subbuffer. */
1018 ret = kernctl_put_subbuf(stream->wait_fd);
1019 assert(ret == 0);
6d805429 1020 ret = 1; /* Data is pending */
4e9a4686 1021 goto end;
ca22feea
DG
1022 }
1023
6d805429
DG
1024 /* Data is NOT pending and ready to be read. */
1025 ret = 0;
ca22feea 1026
6efae65e
DG
1027end:
1028 return ret;
ca22feea 1029}
This page took 0.086827 seconds and 4 git commands to generate.