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