Fix: consumerd: slow metadata push slows down application registration
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
CommitLineData
3bd1e081 1/*
90c106c6 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3bd1e081 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
3bd1e081 7 *
3bd1e081
MD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
3bd1e081 11#include <assert.h>
f02e1e8a 12#include <lttng/ust-ctl.h>
d85707b0 13#include <lttng/ust-sigbus.h>
3bd1e081
MD
14#include <poll.h>
15#include <pthread.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/mman.h>
19#include <sys/socket.h>
dbb5dfe6 20#include <sys/stat.h>
3bd1e081 21#include <sys/types.h>
77c7c900 22#include <inttypes.h>
3bd1e081 23#include <unistd.h>
ffe60014 24#include <urcu/list.h>
331744e3 25#include <signal.h>
02d02e31 26#include <stdbool.h>
6f9449c2 27#include <stdint.h>
0857097f 28
51a9e1c7 29#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 30#include <common/common.h>
10a8a223 31#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 32#include <common/relayd/relayd.h>
dbb5dfe6 33#include <common/compat/fcntl.h>
f263b7fd 34#include <common/compat/endian.h>
c8fea79c
JR
35#include <common/consumer/consumer-metadata-cache.h>
36#include <common/consumer/consumer-stream.h>
37#include <common/consumer/consumer-timer.h>
fe4477ee 38#include <common/utils.h>
309167d2 39#include <common/index/index.h>
6f9449c2 40#include <common/consumer/consumer.h>
b7fc068d 41#include <common/shm.h>
f5ba75b4 42#include <common/optional.h>
10a8a223
DG
43
44#include "ust-consumer.h"
3bd1e081 45
45863397 46#define INT_MAX_STR_LEN 12 /* includes \0 */
4628484a 47
fa29bfbf 48extern struct lttng_consumer_global_data the_consumer_data;
3bd1e081 49extern int consumer_poll_timeout;
3bd1e081 50
d85707b0
MD
51DEFINE_LTTNG_UST_SIGBUS_STATE();
52
3bd1e081 53/*
ffe60014
DG
54 * Free channel object and all streams associated with it. This MUST be used
55 * only and only if the channel has _NEVER_ been added to the global channel
56 * hash table.
3bd1e081 57 */
ffe60014 58static void destroy_channel(struct lttng_consumer_channel *channel)
3bd1e081 59{
ffe60014
DG
60 struct lttng_consumer_stream *stream, *stmp;
61
62 assert(channel);
63
64 DBG("UST consumer cleaning stream list");
65
66 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
67 send_node) {
9ce5646a
MD
68
69 health_code_update();
70
18acc0a4 71 cds_list_del_init(&stream->send_node);
b623cb6a 72 lttng_ust_ctl_destroy_stream(stream->ustream);
d2956687 73 lttng_trace_chunk_put(stream->trace_chunk);
ffe60014
DG
74 free(stream);
75 }
76
77 /*
78 * If a channel is available meaning that was created before the streams
79 * were, delete it.
80 */
81 if (channel->uchan) {
82 lttng_ustconsumer_del_channel(channel);
b83e03c4 83 lttng_ustconsumer_free_channel(channel);
ffe60014 84 }
88ebf5a7
JR
85
86 if (channel->trace_chunk) {
87 lttng_trace_chunk_put(channel->trace_chunk);
88 }
89
ffe60014
DG
90 free(channel);
91}
3bd1e081
MD
92
93/*
ffe60014 94 * Add channel to internal consumer state.
3bd1e081 95 *
ffe60014 96 * Returns 0 on success or else a negative value.
3bd1e081 97 */
ffe60014
DG
98static int add_channel(struct lttng_consumer_channel *channel,
99 struct lttng_consumer_local_data *ctx)
3bd1e081
MD
100{
101 int ret = 0;
102
ffe60014
DG
103 assert(channel);
104 assert(ctx);
105
106 if (ctx->on_recv_channel != NULL) {
107 ret = ctx->on_recv_channel(channel);
108 if (ret == 0) {
d8ef542d 109 ret = consumer_add_channel(channel, ctx);
ffe60014
DG
110 } else if (ret < 0) {
111 /* Most likely an ENOMEM. */
112 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
113 goto error;
114 }
115 } else {
d8ef542d 116 ret = consumer_add_channel(channel, ctx);
3bd1e081
MD
117 }
118
d88aee68 119 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
ffe60014
DG
120
121error:
3bd1e081
MD
122 return ret;
123}
124
ffe60014
DG
125/*
126 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
127 * error value if applicable is set in it else it is kept untouched.
3bd1e081 128 *
ffe60014 129 * Return NULL on error else the newly allocated stream object.
3bd1e081 130 */
ffe60014
DG
131static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
132 struct lttng_consumer_channel *channel,
d2956687 133 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
ffe60014
DG
134{
135 int alloc_ret;
136 struct lttng_consumer_stream *stream = NULL;
137
138 assert(channel);
139 assert(ctx);
140
6f9449c2 141 stream = consumer_stream_create(
49f45573
JG
142 channel,
143 channel->key,
ffe60014 144 key,
ffe60014 145 channel->name,
ffe60014
DG
146 channel->relayd_id,
147 channel->session_id,
d2956687 148 channel->trace_chunk,
ffe60014
DG
149 cpu,
150 &alloc_ret,
4891ece8 151 channel->type,
d2956687 152 channel->monitor);
ffe60014
DG
153 if (stream == NULL) {
154 switch (alloc_ret) {
155 case -ENOENT:
156 /*
157 * We could not find the channel. Can happen if cpu hotplug
158 * happens while tearing down.
159 */
160 DBG3("Could not find channel");
161 break;
162 case -ENOMEM:
163 case -EINVAL:
164 default:
165 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
166 break;
167 }
168 goto error;
169 }
170
d9a2e16e 171 consumer_stream_update_channel_attributes(stream, channel);
ffe60014
DG
172
173error:
174 if (_alloc_ret) {
175 *_alloc_ret = alloc_ret;
176 }
177 return stream;
178}
179
180/*
181 * Send the given stream pointer to the corresponding thread.
182 *
183 * Returns 0 on success else a negative value.
184 */
185static int send_stream_to_thread(struct lttng_consumer_stream *stream,
186 struct lttng_consumer_local_data *ctx)
187{
dae10966
DG
188 int ret;
189 struct lttng_pipe *stream_pipe;
ffe60014
DG
190
191 /* Get the right pipe where the stream will be sent. */
192 if (stream->metadata_flag) {
66d583dc 193 consumer_add_metadata_stream(stream);
dae10966 194 stream_pipe = ctx->consumer_metadata_pipe;
ffe60014 195 } else {
66d583dc 196 consumer_add_data_stream(stream);
dae10966 197 stream_pipe = ctx->consumer_data_pipe;
ffe60014
DG
198 }
199
5ab66908
MD
200 /*
201 * From this point on, the stream's ownership has been moved away from
a8086cf4
JR
202 * the channel and it becomes globally visible. Hence, remove it from
203 * the local stream list to prevent the stream from being both local and
204 * global.
5ab66908
MD
205 */
206 stream->globally_visible = 1;
18acc0a4 207 cds_list_del_init(&stream->send_node);
5ab66908 208
dae10966 209 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
ffe60014 210 if (ret < 0) {
dae10966
DG
211 ERR("Consumer write %s stream to pipe %d",
212 stream->metadata_flag ? "metadata" : "data",
213 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
214 if (stream->metadata_flag) {
215 consumer_del_stream_for_metadata(stream);
216 } else {
217 consumer_del_stream_for_data(stream);
218 }
a8086cf4 219 goto error;
ffe60014 220 }
a8086cf4 221
5ab66908 222error:
ffe60014
DG
223 return ret;
224}
225
4628484a
MD
226static
227int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
228{
45863397 229 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
4628484a
MD
230 int ret;
231
232 strncpy(stream_shm_path, shm_path, PATH_MAX);
233 stream_shm_path[PATH_MAX - 1] = '\0';
45863397 234 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
67f8cb8d
MD
235 if (ret < 0) {
236 PERROR("snprintf");
4628484a
MD
237 goto end;
238 }
239 strncat(stream_shm_path, cpu_nr,
240 PATH_MAX - strlen(stream_shm_path) - 1);
241 ret = 0;
242end:
243 return ret;
244}
245
d88aee68
DG
246/*
247 * Create streams for the given channel using liblttng-ust-ctl.
d2956687 248 * The channel lock must be acquired by the caller.
d88aee68
DG
249 *
250 * Return 0 on success else a negative value.
251 */
ffe60014 252static int create_ust_streams(struct lttng_consumer_channel *channel,
d2956687 253 struct lttng_consumer_local_data *ctx)
ffe60014
DG
254{
255 int ret, cpu = 0;
b623cb6a 256 struct lttng_ust_ctl_consumer_stream *ustream;
ffe60014 257 struct lttng_consumer_stream *stream;
d2956687 258 pthread_mutex_t *current_stream_lock = NULL;
ffe60014
DG
259
260 assert(channel);
261 assert(ctx);
262
263 /*
264 * While a stream is available from ustctl. When NULL is returned, we've
265 * reached the end of the possible stream for the channel.
266 */
b623cb6a 267 while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) {
ffe60014 268 int wait_fd;
04ef1097 269 int ust_metadata_pipe[2];
ffe60014 270
9ce5646a
MD
271 health_code_update();
272
04ef1097
MD
273 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
274 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
275 if (ret < 0) {
276 ERR("Create ust metadata poll pipe");
277 goto error;
278 }
279 wait_fd = ust_metadata_pipe[0];
280 } else {
b623cb6a 281 wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream);
04ef1097 282 }
ffe60014
DG
283
284 /* Allocate consumer stream object. */
d2956687 285 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
ffe60014
DG
286 if (!stream) {
287 goto error_alloc;
288 }
289 stream->ustream = ustream;
290 /*
291 * Store it so we can save multiple function calls afterwards since
292 * this value is used heavily in the stream threads. This is UST
293 * specific so this is why it's done after allocation.
294 */
295 stream->wait_fd = wait_fd;
296
b31398bb
DG
297 /*
298 * Increment channel refcount since the channel reference has now been
299 * assigned in the allocation process above.
300 */
10a50311
JD
301 if (stream->chan->monitor) {
302 uatomic_inc(&stream->chan->refcount);
303 }
b31398bb 304
d2956687
JG
305 pthread_mutex_lock(&stream->lock);
306 current_stream_lock = &stream->lock;
ffe60014
DG
307 /*
308 * Order is important this is why a list is used. On error, the caller
309 * should clean this list.
310 */
311 cds_list_add_tail(&stream->send_node, &channel->streams.head);
312
b623cb6a 313 ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream,
ffe60014
DG
314 &stream->max_sb_size);
315 if (ret < 0) {
b623cb6a 316 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s",
ffe60014
DG
317 stream->name);
318 goto error;
319 }
320
321 /* Do actions once stream has been received. */
322 if (ctx->on_recv_stream) {
323 ret = ctx->on_recv_stream(stream);
324 if (ret < 0) {
325 goto error;
326 }
327 }
328
d88aee68 329 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
ffe60014
DG
330 stream->name, stream->key, stream->relayd_stream_id);
331
332 /* Set next CPU stream. */
333 channel->streams.count = ++cpu;
d88aee68
DG
334
335 /* Keep stream reference when creating metadata. */
336 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
337 channel->metadata_stream = stream;
8de4f941
JG
338 if (channel->monitor) {
339 /* Set metadata poll pipe if we created one */
340 memcpy(stream->ust_metadata_poll_pipe,
341 ust_metadata_pipe,
342 sizeof(ust_metadata_pipe));
343 }
d88aee68 344 }
d2956687
JG
345 pthread_mutex_unlock(&stream->lock);
346 current_stream_lock = NULL;
ffe60014
DG
347 }
348
349 return 0;
350
351error:
352error_alloc:
d2956687
JG
353 if (current_stream_lock) {
354 pthread_mutex_unlock(current_stream_lock);
355 }
ffe60014
DG
356 return ret;
357}
358
d2956687
JG
359static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
360 const struct lttng_credentials *session_credentials)
4628484a
MD
361{
362 char shm_path[PATH_MAX];
363 int ret;
364
365 if (!channel->shm_path[0]) {
b7fc068d 366 return shm_create_anonymous("ust-consumer");
4628484a
MD
367 }
368 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
369 if (ret) {
370 goto error_shm_path;
371 }
372 return run_as_open(shm_path,
373 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
ff588497
JR
374 lttng_credentials_get_uid(session_credentials),
375 lttng_credentials_get_gid(session_credentials));
4628484a
MD
376
377error_shm_path:
378 return -1;
379}
380
ffe60014
DG
381/*
382 * Create an UST channel with the given attributes and send it to the session
383 * daemon using the ust ctl API.
384 *
385 * Return 0 on success or else a negative value.
386 */
4628484a 387static int create_ust_channel(struct lttng_consumer_channel *channel,
b623cb6a
MJ
388 struct lttng_ust_ctl_consumer_channel_attr *attr,
389 struct lttng_ust_ctl_consumer_channel **ust_chanp)
ffe60014 390{
4628484a
MD
391 int ret, nr_stream_fds, i, j;
392 int *stream_fds;
b623cb6a 393 struct lttng_ust_ctl_consumer_channel *ust_channel;
ffe60014 394
4628484a 395 assert(channel);
ffe60014 396 assert(attr);
4628484a 397 assert(ust_chanp);
d2956687 398 assert(channel->buffer_credentials.is_set);
ffe60014
DG
399
400 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
401 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
402 "switch_timer_interval: %u, read_timer_interval: %u, "
403 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
404 attr->num_subbuf, attr->switch_timer_interval,
405 attr->read_timer_interval, attr->output, attr->type);
406
4628484a
MD
407 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
408 nr_stream_fds = 1;
409 else
b623cb6a 410 nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel();
4628484a
MD
411 stream_fds = zmalloc(nr_stream_fds * sizeof(*stream_fds));
412 if (!stream_fds) {
413 ret = -1;
414 goto error_alloc;
415 }
416 for (i = 0; i < nr_stream_fds; i++) {
d2956687
JG
417 stream_fds[i] = open_ust_stream_fd(channel, i,
418 &channel->buffer_credentials.value);
4628484a
MD
419 if (stream_fds[i] < 0) {
420 ret = -1;
421 goto error_open;
422 }
423 }
b623cb6a 424 ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds);
4628484a 425 if (!ust_channel) {
ffe60014
DG
426 ret = -1;
427 goto error_create;
428 }
4628484a
MD
429 channel->nr_stream_fds = nr_stream_fds;
430 channel->stream_fds = stream_fds;
431 *ust_chanp = ust_channel;
ffe60014
DG
432
433 return 0;
434
435error_create:
4628484a
MD
436error_open:
437 for (j = i - 1; j >= 0; j--) {
438 int closeret;
439
440 closeret = close(stream_fds[j]);
441 if (closeret) {
442 PERROR("close");
443 }
444 if (channel->shm_path[0]) {
445 char shm_path[PATH_MAX];
446
447 closeret = get_stream_shm_path(shm_path,
448 channel->shm_path, j);
449 if (closeret) {
450 ERR("Cannot get stream shm path");
451 }
452 closeret = run_as_unlink(shm_path,
ff588497
JR
453 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
454 channel->buffer_credentials)),
455 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
456 channel->buffer_credentials)));
4628484a 457 if (closeret) {
4628484a
MD
458 PERROR("unlink %s", shm_path);
459 }
460 }
461 }
462 /* Try to rmdir all directories under shm_path root. */
463 if (channel->root_shm_path[0]) {
602766ec 464 (void) run_as_rmdir_recursive(channel->root_shm_path,
ff588497
JR
465 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
466 channel->buffer_credentials)),
467 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
468 channel->buffer_credentials)),
f75c5439 469 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
4628484a
MD
470 }
471 free(stream_fds);
472error_alloc:
ffe60014
DG
473 return ret;
474}
475
d88aee68
DG
476/*
477 * Send a single given stream to the session daemon using the sock.
478 *
479 * Return 0 on success else a negative value.
480 */
ffe60014
DG
481static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
482{
483 int ret;
484
485 assert(stream);
486 assert(sock >= 0);
487
3eb914c0 488 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
ffe60014
DG
489
490 /* Send stream to session daemon. */
b623cb6a 491 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream);
ffe60014
DG
492 if (ret < 0) {
493 goto error;
494 }
495
ffe60014
DG
496error:
497 return ret;
498}
499
500/*
a3a86f35 501 * Send channel to sessiond and relayd if applicable.
ffe60014 502 *
d88aee68 503 * Return 0 on success or else a negative value.
ffe60014 504 */
a3a86f35 505static int send_channel_to_sessiond_and_relayd(int sock,
ffe60014
DG
506 struct lttng_consumer_channel *channel,
507 struct lttng_consumer_local_data *ctx, int *relayd_error)
508{
0c759fc9 509 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
ffe60014 510 struct lttng_consumer_stream *stream;
a4baae1b 511 uint64_t net_seq_idx = -1ULL;
ffe60014
DG
512
513 assert(channel);
514 assert(ctx);
515 assert(sock >= 0);
516
517 DBG("UST consumer sending channel %s to sessiond", channel->name);
518
62285ea4
DG
519 if (channel->relayd_id != (uint64_t) -1ULL) {
520 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
521
522 health_code_update();
523
62285ea4 524 /* Try to send the stream to the relayd if one is available. */
a3a86f35
JG
525 DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd",
526 stream->key, channel->name);
62285ea4
DG
527 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
528 if (ret < 0) {
529 /*
530 * Flag that the relayd was the problem here probably due to a
531 * communicaton error on the socket.
532 */
533 if (relayd_error) {
534 *relayd_error = 1;
535 }
725d28b2 536 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
ffe60014 537 }
a4baae1b
JD
538 if (net_seq_idx == -1ULL) {
539 net_seq_idx = stream->net_seq_idx;
540 }
541 }
f2a444f1 542 }
ffe60014 543
f2a444f1
DG
544 /* Inform sessiond that we are about to send channel and streams. */
545 ret = consumer_send_status_msg(sock, ret_code);
0c759fc9 546 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
547 /*
548 * Either the session daemon is not responding or the relayd died so we
549 * stop now.
550 */
551 goto error;
552 }
553
554 /* Send channel to sessiond. */
b623cb6a 555 ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan);
f2a444f1
DG
556 if (ret < 0) {
557 goto error;
558 }
559
b623cb6a 560 ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan);
f2a444f1
DG
561 if (ret < 0) {
562 goto error;
563 }
564
565 /* The channel was sent successfully to the sessiond at this point. */
566 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
567
568 health_code_update();
569
ffe60014
DG
570 /* Send stream to session daemon. */
571 ret = send_sessiond_stream(sock, stream);
572 if (ret < 0) {
573 goto error;
574 }
575 }
576
577 /* Tell sessiond there is no more stream. */
b623cb6a 578 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, NULL);
ffe60014
DG
579 if (ret < 0) {
580 goto error;
581 }
582
583 DBG("UST consumer NULL stream sent to sessiond");
584
585 return 0;
586
587error:
0c759fc9 588 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
589 ret = -1;
590 }
ffe60014
DG
591 return ret;
592}
593
594/*
595 * Creates a channel and streams and add the channel it to the channel internal
596 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
597 * received.
598 *
599 * Return 0 on success or else, a negative value is returned and the channel
600 * MUST be destroyed by consumer_del_channel().
601 */
75cfe9e6 602static int ask_channel(struct lttng_consumer_local_data *ctx,
ffe60014 603 struct lttng_consumer_channel *channel,
b623cb6a 604 struct lttng_ust_ctl_consumer_channel_attr *attr)
3bd1e081
MD
605{
606 int ret;
607
ffe60014
DG
608 assert(ctx);
609 assert(channel);
610 assert(attr);
611
612 /*
613 * This value is still used by the kernel consumer since for the kernel,
614 * the stream ownership is not IN the consumer so we need to have the
615 * number of left stream that needs to be initialized so we can know when
616 * to delete the channel (see consumer.c).
617 *
618 * As for the user space tracer now, the consumer creates and sends the
619 * stream to the session daemon which only sends them to the application
620 * once every stream of a channel is received making this value useless
621 * because we they will be added to the poll thread before the application
622 * receives them. This ensures that a stream can not hang up during
623 * initilization of a channel.
624 */
625 channel->nb_init_stream_left = 0;
626
627 /* The reply msg status is handled in the following call. */
4628484a 628 ret = create_ust_channel(channel, attr, &channel->uchan);
ffe60014 629 if (ret < 0) {
10a50311 630 goto end;
3bd1e081
MD
631 }
632
b623cb6a 633 channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan);
d8ef542d 634
10a50311
JD
635 /*
636 * For the snapshots (no monitor), we create the metadata streams
637 * on demand, not during the channel creation.
638 */
639 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
640 ret = 0;
641 goto end;
642 }
643
ffe60014 644 /* Open all streams for this channel. */
d2956687
JG
645 pthread_mutex_lock(&channel->lock);
646 ret = create_ust_streams(channel, ctx);
647 pthread_mutex_unlock(&channel->lock);
ffe60014 648 if (ret < 0) {
10a50311 649 goto end;
ffe60014
DG
650 }
651
10a50311 652end:
3bd1e081
MD
653 return ret;
654}
655
d88aee68
DG
656/*
657 * Send all stream of a channel to the right thread handling it.
658 *
659 * On error, return a negative value else 0 on success.
660 */
661static int send_streams_to_thread(struct lttng_consumer_channel *channel,
662 struct lttng_consumer_local_data *ctx)
663{
664 int ret = 0;
665 struct lttng_consumer_stream *stream, *stmp;
666
667 assert(channel);
668 assert(ctx);
669
670 /* Send streams to the corresponding thread. */
671 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
672 send_node) {
9ce5646a
MD
673
674 health_code_update();
675
d88aee68
DG
676 /* Sending the stream to the thread. */
677 ret = send_stream_to_thread(stream, ctx);
678 if (ret < 0) {
679 /*
680 * If we are unable to send the stream to the thread, there is
681 * a big problem so just stop everything.
682 */
683 goto error;
684 }
d88aee68
DG
685 }
686
687error:
688 return ret;
689}
690
7972aab2
DG
691/*
692 * Flush channel's streams using the given key to retrieve the channel.
693 *
694 * Return 0 on success else an LTTng error code.
695 */
696static int flush_channel(uint64_t chan_key)
697{
698 int ret = 0;
699 struct lttng_consumer_channel *channel;
700 struct lttng_consumer_stream *stream;
701 struct lttng_ht *ht;
702 struct lttng_ht_iter iter;
703
8fd623e0 704 DBG("UST consumer flush channel key %" PRIu64, chan_key);
7972aab2 705
a500c257 706 rcu_read_lock();
7972aab2
DG
707 channel = consumer_find_channel(chan_key);
708 if (!channel) {
8fd623e0 709 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
7972aab2
DG
710 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
711 goto error;
712 }
713
fa29bfbf 714 ht = the_consumer_data.stream_per_chan_id_ht;
7972aab2
DG
715
716 /* For each stream of the channel id, flush it. */
7972aab2
DG
717 cds_lfht_for_each_entry_duplicate(ht->ht,
718 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
719 &channel->key, &iter.iter, stream, node_channel_id.node) {
9ce5646a
MD
720
721 health_code_update();
722
0dd01979 723 pthread_mutex_lock(&stream->lock);
5cfcab67
JR
724
725 /*
726 * Protect against concurrent teardown of a stream.
727 */
728 if (cds_lfht_is_node_deleted(&stream->node.node)) {
729 goto next;
730 }
731
0dd01979 732 if (!stream->quiescent) {
d85707b0
MD
733 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
734 if (ret) {
735 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64 ", channel name = '%s'",
736 chan_key, channel->name);
737 ret = LTTNG_ERR_BUFFER_FLUSH_FAILED;
738 pthread_mutex_unlock(&stream->lock);
739 goto error;
740 }
0dd01979
MD
741 stream->quiescent = true;
742 }
5cfcab67 743next:
0dd01979
MD
744 pthread_mutex_unlock(&stream->lock);
745 }
746error:
747 rcu_read_unlock();
748 return ret;
749}
750
751/*
752 * Clear quiescent state from channel's streams using the given key to
753 * retrieve the channel.
754 *
755 * Return 0 on success else an LTTng error code.
756 */
757static int clear_quiescent_channel(uint64_t chan_key)
758{
759 int ret = 0;
760 struct lttng_consumer_channel *channel;
761 struct lttng_consumer_stream *stream;
762 struct lttng_ht *ht;
763 struct lttng_ht_iter iter;
764
765 DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key);
766
767 rcu_read_lock();
768 channel = consumer_find_channel(chan_key);
769 if (!channel) {
770 ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key);
771 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
772 goto error;
773 }
774
fa29bfbf 775 ht = the_consumer_data.stream_per_chan_id_ht;
0dd01979
MD
776
777 /* For each stream of the channel id, clear quiescent state. */
778 cds_lfht_for_each_entry_duplicate(ht->ht,
779 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
780 &channel->key, &iter.iter, stream, node_channel_id.node) {
781
782 health_code_update();
783
784 pthread_mutex_lock(&stream->lock);
785 stream->quiescent = false;
786 pthread_mutex_unlock(&stream->lock);
7972aab2 787 }
7972aab2 788error:
a500c257 789 rcu_read_unlock();
7972aab2
DG
790 return ret;
791}
792
d88aee68
DG
793/*
794 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
795 *
796 * Return 0 on success else an LTTng error code.
797 */
798static int close_metadata(uint64_t chan_key)
799{
ea88ca2a 800 int ret = 0;
d88aee68 801 struct lttng_consumer_channel *channel;
f65a74be 802 unsigned int channel_monitor;
d88aee68 803
8fd623e0 804 DBG("UST consumer close metadata key %" PRIu64, chan_key);
d88aee68
DG
805
806 channel = consumer_find_channel(chan_key);
807 if (!channel) {
84cc9aa0
DG
808 /*
809 * This is possible if the metadata thread has issue a delete because
810 * the endpoint point of the stream hung up. There is no way the
811 * session daemon can know about it thus use a DBG instead of an actual
812 * error.
813 */
814 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
d88aee68
DG
815 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
816 goto error;
817 }
818
fa29bfbf 819 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 820 pthread_mutex_lock(&channel->lock);
f65a74be 821 channel_monitor = channel->monitor;
73811ecc
DG
822 if (cds_lfht_is_node_deleted(&channel->node.node)) {
823 goto error_unlock;
824 }
825
6d574024 826 lttng_ustconsumer_close_metadata(channel);
f65a74be 827 pthread_mutex_unlock(&channel->lock);
fa29bfbf 828 pthread_mutex_unlock(&the_consumer_data.lock);
d88aee68 829
f65a74be
JG
830 /*
831 * The ownership of a metadata channel depends on the type of
832 * session to which it belongs. In effect, the monitor flag is checked
833 * to determine if this metadata channel is in "snapshot" mode or not.
834 *
835 * In the non-snapshot case, the metadata channel is created along with
836 * a single stream which will remain present until the metadata channel
837 * is destroyed (on the destruction of its session). In this case, the
838 * metadata stream in "monitored" by the metadata poll thread and holds
839 * the ownership of its channel.
840 *
841 * Closing the metadata will cause the metadata stream's "metadata poll
842 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
843 * thread which will teardown the metadata stream which, in return,
844 * deletes the metadata channel.
845 *
846 * In the snapshot case, the metadata stream is created and destroyed
847 * on every snapshot record. Since the channel doesn't have an owner
848 * other than the session daemon, it is safe to destroy it immediately
849 * on reception of the CLOSE_METADATA command.
850 */
851 if (!channel_monitor) {
852 /*
853 * The channel and consumer_data locks must be
854 * released before this call since consumer_del_channel
855 * re-acquires the channel and consumer_data locks to teardown
856 * the channel and queue its reclamation by the "call_rcu"
857 * worker thread.
858 */
859 consumer_del_channel(channel);
860 }
861
862 return ret;
ea88ca2a 863error_unlock:
a9838785 864 pthread_mutex_unlock(&channel->lock);
fa29bfbf 865 pthread_mutex_unlock(&the_consumer_data.lock);
d88aee68
DG
866error:
867 return ret;
868}
869
870/*
871 * RCU read side lock MUST be acquired before calling this function.
872 *
873 * Return 0 on success else an LTTng error code.
874 */
875static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
876{
877 int ret;
878 struct lttng_consumer_channel *metadata;
879
8fd623e0 880 DBG("UST consumer setup metadata key %" PRIu64, key);
d88aee68
DG
881
882 metadata = consumer_find_channel(key);
883 if (!metadata) {
884 ERR("UST consumer push metadata %" PRIu64 " not found", key);
885 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
10a50311
JD
886 goto end;
887 }
888
889 /*
890 * In no monitor mode, the metadata channel has no stream(s) so skip the
891 * ownership transfer to the metadata thread.
892 */
893 if (!metadata->monitor) {
894 DBG("Metadata channel in no monitor");
895 ret = 0;
896 goto end;
d88aee68
DG
897 }
898
899 /*
900 * Send metadata stream to relayd if one available. Availability is
901 * known if the stream is still in the list of the channel.
902 */
903 if (cds_list_empty(&metadata->streams.head)) {
904 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
905 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
f5a0c9cf 906 goto error_no_stream;
d88aee68
DG
907 }
908
909 /* Send metadata stream to relayd if needed. */
62285ea4
DG
910 if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
911 ret = consumer_send_relayd_stream(metadata->metadata_stream,
912 metadata->pathname);
913 if (ret < 0) {
914 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
915 goto error;
916 }
601262d6
JD
917 ret = consumer_send_relayd_streams_sent(
918 metadata->metadata_stream->net_seq_idx);
919 if (ret < 0) {
920 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
921 goto error;
922 }
d88aee68
DG
923 }
924
a8086cf4
JR
925 /*
926 * Ownership of metadata stream is passed along. Freeing is handled by
927 * the callee.
928 */
d88aee68
DG
929 ret = send_streams_to_thread(metadata, ctx);
930 if (ret < 0) {
931 /*
932 * If we are unable to send the stream to the thread, there is
933 * a big problem so just stop everything.
934 */
935 ret = LTTCOMM_CONSUMERD_FATAL;
a8086cf4 936 goto send_streams_error;
d88aee68
DG
937 }
938 /* List MUST be empty after or else it could be reused. */
939 assert(cds_list_empty(&metadata->streams.head));
940
10a50311
JD
941 ret = 0;
942 goto end;
d88aee68
DG
943
944error:
f2a444f1
DG
945 /*
946 * Delete metadata channel on error. At this point, the metadata stream can
947 * NOT be monitored by the metadata thread thus having the guarantee that
948 * the stream is still in the local stream list of the channel. This call
949 * will make sure to clean that list.
950 */
f5a0c9cf 951 consumer_stream_destroy(metadata->metadata_stream, NULL);
212d67a2 952 metadata->metadata_stream = NULL;
95671f53
JG
953 lttng_wait_queue_wake_all(&metadata->metadata_pushed_wait_queue);
954
a8086cf4 955send_streams_error:
f5a0c9cf 956error_no_stream:
10a50311
JD
957end:
958 return ret;
959}
960
961/*
962 * Snapshot the whole metadata.
d2956687 963 * RCU read-side lock must be held by the caller.
10a50311
JD
964 *
965 * Returns 0 on success, < 0 on error
966 */
3eb928aa
MD
967static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel,
968 uint64_t key, char *path, uint64_t relayd_id,
d2956687 969 struct lttng_consumer_local_data *ctx)
10a50311
JD
970{
971 int ret = 0;
10a50311
JD
972 struct lttng_consumer_stream *metadata_stream;
973
974 assert(path);
975 assert(ctx);
976
977 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
978 key, path);
979
980 rcu_read_lock();
981
10a50311
JD
982 assert(!metadata_channel->monitor);
983
9ce5646a
MD
984 health_code_update();
985
10a50311
JD
986 /*
987 * Ask the sessiond if we have new metadata waiting and update the
988 * consumer metadata cache.
989 */
95671f53 990 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 1);
10a50311
JD
991 if (ret < 0) {
992 goto error;
993 }
994
9ce5646a
MD
995 health_code_update();
996
10a50311
JD
997 /*
998 * The metadata stream is NOT created in no monitor mode when the channel
999 * is created on a sessiond ask channel command.
1000 */
d2956687 1001 ret = create_ust_streams(metadata_channel, ctx);
10a50311
JD
1002 if (ret < 0) {
1003 goto error;
1004 }
1005
1006 metadata_stream = metadata_channel->metadata_stream;
1007 assert(metadata_stream);
1008
a48ac75a 1009 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
10a50311
JD
1010 if (relayd_id != (uint64_t) -1ULL) {
1011 metadata_stream->net_seq_idx = relayd_id;
1012 ret = consumer_send_relayd_stream(metadata_stream, path);
10a50311 1013 } else {
d2956687
JG
1014 ret = consumer_stream_create_output_files(metadata_stream,
1015 false);
1016 }
d2956687
JG
1017 if (ret < 0) {
1018 goto error_stream;
10a50311
JD
1019 }
1020
04ef1097 1021 do {
9ce5646a 1022 health_code_update();
6f9449c2 1023 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
10a50311 1024 if (ret < 0) {
94d49140 1025 goto error_stream;
10a50311 1026 }
04ef1097 1027 } while (ret > 0);
10a50311 1028
10a50311 1029error_stream:
a48ac75a 1030 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
10a50311 1031 /*
a48ac75a
JR
1032 * Clean up the stream completely because the next snapshot will use a
1033 * new metadata stream.
10a50311 1034 */
10a50311
JD
1035 consumer_stream_destroy(metadata_stream, NULL);
1036 metadata_channel->metadata_stream = NULL;
95671f53 1037 lttng_wait_queue_wake_all(&metadata_channel->metadata_pushed_wait_queue);
10a50311
JD
1038
1039error:
1040 rcu_read_unlock();
1041 return ret;
1042}
1043
128708c3
JG
1044static
1045int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1046 const char **addr)
1047{
1048 int ret;
1049 unsigned long mmap_offset;
1050 const char *mmap_base;
1051
b623cb6a 1052 mmap_base = lttng_ust_ctl_get_mmap_base(stream->ustream);
128708c3
JG
1053 if (!mmap_base) {
1054 ERR("Failed to get mmap base for stream `%s`",
1055 stream->name);
1056 ret = -EPERM;
1057 goto error;
1058 }
1059
b623cb6a 1060 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
128708c3
JG
1061 if (ret != 0) {
1062 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1063 ret = -EINVAL;
1064 goto error;
1065 }
1066
1067 *addr = mmap_base + mmap_offset;
1068error:
1069 return ret;
1070
1071}
1072
10a50311
JD
1073/*
1074 * Take a snapshot of all the stream of a channel.
d2956687 1075 * RCU read-side lock and the channel lock must be held by the caller.
10a50311
JD
1076 *
1077 * Returns 0 on success, < 0 on error
1078 */
3eb928aa
MD
1079static int snapshot_channel(struct lttng_consumer_channel *channel,
1080 uint64_t key, char *path, uint64_t relayd_id,
e098433c
JG
1081 uint64_t nb_packets_per_stream,
1082 struct lttng_consumer_local_data *ctx)
10a50311
JD
1083{
1084 int ret;
1085 unsigned use_relayd = 0;
1086 unsigned long consumed_pos, produced_pos;
10a50311
JD
1087 struct lttng_consumer_stream *stream;
1088
1089 assert(path);
1090 assert(ctx);
1091
1092 rcu_read_lock();
1093
1094 if (relayd_id != (uint64_t) -1ULL) {
1095 use_relayd = 1;
1096 }
1097
10a50311 1098 assert(!channel->monitor);
6a00837f 1099 DBG("UST consumer snapshot channel %" PRIu64, key);
10a50311
JD
1100
1101 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
1102 health_code_update();
1103
10a50311
JD
1104 /* Lock stream because we are about to change its state. */
1105 pthread_mutex_lock(&stream->lock);
d2956687
JG
1106 assert(channel->trace_chunk);
1107 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1108 /*
1109 * Can't happen barring an internal error as the channel
1110 * holds a reference to the trace chunk.
1111 */
1112 ERR("Failed to acquire reference to channel's trace chunk");
1113 ret = -1;
1114 goto error_unlock;
1115 }
1116 assert(!stream->trace_chunk);
1117 stream->trace_chunk = channel->trace_chunk;
1118
10a50311
JD
1119 stream->net_seq_idx = relayd_id;
1120
1121 if (use_relayd) {
1122 ret = consumer_send_relayd_stream(stream, path);
1123 if (ret < 0) {
dee2d256 1124 goto error_close_stream;
10a50311
JD
1125 }
1126 } else {
d2956687
JG
1127 ret = consumer_stream_create_output_files(stream,
1128 false);
10a50311 1129 if (ret < 0) {
dee2d256 1130 goto error_close_stream;
10a50311 1131 }
d2956687
JG
1132 DBG("UST consumer snapshot stream (%" PRIu64 ")",
1133 stream->key);
10a50311
JD
1134 }
1135
d4d80f77
MD
1136 /*
1137 * If tracing is active, we want to perform a "full" buffer flush.
1138 * Else, if quiescent, it has already been done by the prior stop.
1139 */
1140 if (!stream->quiescent) {
d85707b0
MD
1141 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1142 if (ret < 0) {
1143 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'",
1144 channel->key, channel->name);
1145 goto error_unlock;
1146 }
d4d80f77 1147 }
10a50311
JD
1148
1149 ret = lttng_ustconsumer_take_snapshot(stream);
1150 if (ret < 0) {
1151 ERR("Taking UST snapshot");
dee2d256 1152 goto error_close_stream;
10a50311
JD
1153 }
1154
1155 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1156 if (ret < 0) {
1157 ERR("Produced UST snapshot position");
dee2d256 1158 goto error_close_stream;
10a50311
JD
1159 }
1160
1161 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1162 if (ret < 0) {
1163 ERR("Consumerd UST snapshot position");
dee2d256 1164 goto error_close_stream;
10a50311
JD
1165 }
1166
5c786ded
JD
1167 /*
1168 * The original value is sent back if max stream size is larger than
d07ceecd 1169 * the possible size of the snapshot. Also, we assume that the session
5c786ded
JD
1170 * daemon should never send a maximum stream size that is lower than
1171 * subbuffer size.
1172 */
d07ceecd
MD
1173 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1174 produced_pos, nb_packets_per_stream,
1175 stream->max_sb_size);
5c786ded 1176
9377d830 1177 while ((long) (consumed_pos - produced_pos) < 0) {
10a50311
JD
1178 ssize_t read_len;
1179 unsigned long len, padded_len;
128708c3 1180 const char *subbuf_addr;
fd424d99 1181 struct lttng_buffer_view subbuf_view;
10a50311 1182
9ce5646a
MD
1183 health_code_update();
1184
10a50311
JD
1185 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1186
b623cb6a 1187 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
10a50311
JD
1188 if (ret < 0) {
1189 if (ret != -EAGAIN) {
b623cb6a 1190 PERROR("lttng_ust_ctl_get_subbuf snapshot");
10a50311
JD
1191 goto error_close_stream;
1192 }
1193 DBG("UST consumer get subbuf failed. Skipping it.");
1194 consumed_pos += stream->max_sb_size;
ddc93ee4 1195 stream->chan->lost_packets++;
10a50311
JD
1196 continue;
1197 }
1198
b623cb6a 1199 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
10a50311 1200 if (ret < 0) {
b623cb6a 1201 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
10a50311
JD
1202 goto error_put_subbuf;
1203 }
1204
b623cb6a 1205 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
10a50311 1206 if (ret < 0) {
b623cb6a 1207 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
10a50311
JD
1208 goto error_put_subbuf;
1209 }
1210
128708c3
JG
1211 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1212 if (ret) {
1213 goto error_put_subbuf;
1214 }
1215
fd424d99
JG
1216 subbuf_view = lttng_buffer_view_init(
1217 subbuf_addr, 0, padded_len);
f5ba75b4 1218 read_len = lttng_consumer_on_read_subbuffer_mmap(
6f9449c2 1219 stream, &subbuf_view, padded_len - len);
10a50311
JD
1220 if (use_relayd) {
1221 if (read_len != len) {
56591bac 1222 ret = -EPERM;
10a50311
JD
1223 goto error_put_subbuf;
1224 }
1225 } else {
1226 if (read_len != padded_len) {
56591bac 1227 ret = -EPERM;
10a50311
JD
1228 goto error_put_subbuf;
1229 }
1230 }
1231
b623cb6a 1232 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
10a50311 1233 if (ret < 0) {
b623cb6a 1234 ERR("Snapshot lttng_ust_ctl_put_subbuf");
10a50311
JD
1235 goto error_close_stream;
1236 }
1237 consumed_pos += stream->max_sb_size;
1238 }
1239
1240 /* Simply close the stream so we can use it on the next snapshot. */
1241 consumer_stream_close(stream);
1242 pthread_mutex_unlock(&stream->lock);
1243 }
1244
1245 rcu_read_unlock();
1246 return 0;
1247
1248error_put_subbuf:
b623cb6a
MJ
1249 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1250 ERR("Snapshot lttng_ust_ctl_put_subbuf");
10a50311
JD
1251 }
1252error_close_stream:
1253 consumer_stream_close(stream);
1254error_unlock:
1255 pthread_mutex_unlock(&stream->lock);
10a50311 1256 rcu_read_unlock();
d88aee68
DG
1257 return ret;
1258}
1259
b1316da1
JG
1260static
1261void metadata_stream_reset_cache_consumed_position(
1262 struct lttng_consumer_stream *stream)
1263{
1264 ASSERT_LOCKED(stream->lock);
1265
1266 DBG("Reset metadata cache of session %" PRIu64,
1267 stream->chan->session_id);
1268 stream->ust_metadata_pushed = 0;
1269}
1270
331744e3 1271/*
c585821b
MD
1272 * Receive the metadata updates from the sessiond. Supports receiving
1273 * overlapping metadata, but is needs to always belong to a contiguous
1274 * range starting from 0.
1275 * Be careful about the locks held when calling this function: it needs
1276 * the metadata cache flush to concurrently progress in order to
1277 * complete.
331744e3
JD
1278 */
1279int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
93ec662e 1280 uint64_t len, uint64_t version,
95671f53 1281 struct lttng_consumer_channel *channel, bool invoked_by_timer, int wait)
331744e3 1282{
0c759fc9 1283 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
331744e3 1284 char *metadata_str;
b1316da1 1285 enum consumer_metadata_cache_write_status cache_write_status;
331744e3 1286
8fd623e0 1287 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
331744e3
JD
1288
1289 metadata_str = zmalloc(len * sizeof(char));
1290 if (!metadata_str) {
1291 PERROR("zmalloc metadata string");
1292 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1293 goto end;
1294 }
1295
9ce5646a
MD
1296 health_code_update();
1297
331744e3
JD
1298 /* Receive metadata string. */
1299 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1300 if (ret < 0) {
1301 /* Session daemon is dead so return gracefully. */
1302 ret_code = ret;
1303 goto end_free;
1304 }
1305
9ce5646a
MD
1306 health_code_update();
1307
331744e3 1308 pthread_mutex_lock(&channel->metadata_cache->lock);
b1316da1 1309 cache_write_status = consumer_metadata_cache_write(
a25d34bc
JG
1310 channel->metadata_cache, offset, len, version,
1311 metadata_str);
3bdc49f3 1312 pthread_mutex_unlock(&channel->metadata_cache->lock);
b1316da1
JG
1313 switch (cache_write_status) {
1314 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1315 /*
1316 * The write entirely overlapped with existing contents of the
1317 * same metadata version (same content); there is nothing to do.
1318 */
1319 break;
1320 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1321 /*
1322 * The metadata cache was invalidated (previously pushed
1323 * content has been overwritten). Reset the stream's consumed
1324 * metadata position to ensure the metadata poll thread consumes
1325 * the whole cache.
1326 */
a48ac75a
JR
1327
1328 /*
1329 * channel::metadata_stream can be null when the metadata
1330 * channel is under a snapshot session type. No need to update
1331 * the stream position in that scenario.
1332 */
1333 if (channel->metadata_stream != NULL) {
1334 pthread_mutex_lock(&channel->metadata_stream->lock);
1335 metadata_stream_reset_cache_consumed_position(
1336 channel->metadata_stream);
1337 pthread_mutex_unlock(&channel->metadata_stream->lock);
1338 } else {
1339 /* Validate we are in snapshot mode. */
1340 assert(!channel->monitor);
1341 }
b1316da1
JG
1342 /* Fall-through. */
1343 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1344 /*
1345 * In both cases, the metadata poll thread has new data to
1346 * consume.
1347 */
1348 ret = consumer_metadata_wakeup_pipe(channel);
1349 if (ret) {
1350 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1351 goto end_free;
1352 }
1353 break;
1354 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
331744e3
JD
1355 /* Unable to handle metadata. Notify session daemon. */
1356 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
a32bd775
DG
1357 /*
1358 * Skip metadata flush on write error since the offset and len might
1359 * not have been updated which could create an infinite loop below when
1360 * waiting for the metadata cache to be flushed.
1361 */
a32bd775 1362 goto end_free;
b1316da1
JG
1363 default:
1364 abort();
331744e3 1365 }
331744e3 1366
94d49140
JD
1367 if (!wait) {
1368 goto end_free;
1369 }
9ce5646a 1370
95671f53 1371 consumer_wait_metadata_cache_flushed(channel, offset + len, invoked_by_timer);
331744e3
JD
1372
1373end_free:
1374 free(metadata_str);
1375end:
1376 return ret_code;
1377}
1378
4cbc1a04
DG
1379/*
1380 * Receive command from session daemon and process it.
1381 *
1382 * Return 1 on success else a negative value or 0.
1383 */
3bd1e081
MD
1384int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1385 int sock, struct pollfd *consumer_sockpoll)
1386{
594c7c00 1387 int ret_func;
0c759fc9 1388 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081 1389 struct lttcomm_consumer_msg msg;
ffe60014 1390 struct lttng_consumer_channel *channel = NULL;
3bd1e081 1391
9ce5646a
MD
1392 health_code_update();
1393
594c7c00
SM
1394 {
1395 ssize_t ret_recv;
1396
1397 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1398 if (ret_recv != sizeof(msg)) {
1399 DBG("Consumer received unexpected message size %zd (expects %zu)",
1400 ret_recv, sizeof(msg));
1401 /*
1402 * The ret value might 0 meaning an orderly shutdown but this is ok
1403 * since the caller handles this.
1404 */
1405 if (ret_recv > 0) {
1406 lttng_consumer_send_error(ctx,
1407 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1408 ret_recv = -1;
1409 }
1410 return ret_recv;
489f70e9 1411 }
3bd1e081 1412 }
9ce5646a
MD
1413
1414 health_code_update();
1415
84382d49
MD
1416 /* deprecated */
1417 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 1418
9ce5646a
MD
1419 health_code_update();
1420
3f8e211f 1421 /* relayd needs RCU read-side lock */
b0b335c8
MD
1422 rcu_read_lock();
1423
3bd1e081 1424 switch (msg.cmd_type) {
00e2e675
DG
1425 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1426 {
fe7bf564
JR
1427 uint32_t major = msg.u.relayd_sock.major;
1428 uint32_t minor = msg.u.relayd_sock.minor;
1429 enum lttcomm_sock_proto protocol =
1430 (enum lttcomm_sock_proto) msg.u.relayd_sock
1431 .relayd_socket_protocol;
1432
f50f23d9 1433 /* Session daemon status message are handled in the following call. */
2527bf85 1434 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
fe7bf564
JR
1435 msg.u.relayd_sock.type, ctx, sock,
1436 consumer_sockpoll, msg.u.relayd_sock.session_id,
1437 msg.u.relayd_sock.relayd_session_id, major,
1438 minor, protocol);
00e2e675
DG
1439 goto end_nosignal;
1440 }
173af62f
DG
1441 case LTTNG_CONSUMER_DESTROY_RELAYD:
1442 {
a6ba4fe1 1443 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
173af62f
DG
1444 struct consumer_relayd_sock_pair *relayd;
1445
a6ba4fe1 1446 DBG("UST consumer destroying relayd %" PRIu64, index);
173af62f
DG
1447
1448 /* Get relayd reference if exists. */
a6ba4fe1 1449 relayd = consumer_find_relayd(index);
173af62f 1450 if (relayd == NULL) {
3448e266 1451 DBG("Unable to find relayd %" PRIu64, index);
e462382a 1452 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
173af62f
DG
1453 }
1454
a6ba4fe1
DG
1455 /*
1456 * Each relayd socket pair has a refcount of stream attached to it
1457 * which tells if the relayd is still active or not depending on the
1458 * refcount value.
1459 *
1460 * This will set the destroy flag of the relayd object and destroy it
1461 * if the refcount reaches zero when called.
1462 *
1463 * The destroy can happen either here or when a stream fd hangs up.
1464 */
f50f23d9
DG
1465 if (relayd) {
1466 consumer_flag_relayd_for_destroy(relayd);
1467 }
1468
d88aee68 1469 goto end_msg_sessiond;
173af62f 1470 }
3bd1e081
MD
1471 case LTTNG_CONSUMER_UPDATE_STREAM:
1472 {
3f8e211f 1473 rcu_read_unlock();
7ad0a0cb 1474 return -ENOSYS;
3bd1e081 1475 }
6d805429 1476 case LTTNG_CONSUMER_DATA_PENDING:
53632229 1477 {
594c7c00
SM
1478 int is_data_pending;
1479 ssize_t ret_send;
6d805429 1480 uint64_t id = msg.u.data_pending.session_id;
ca22feea 1481
6d805429 1482 DBG("UST consumer data pending command for id %" PRIu64, id);
ca22feea 1483
3be74084 1484 is_data_pending = consumer_data_pending(id);
ca22feea
DG
1485
1486 /* Send back returned value to session daemon */
594c7c00 1487 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending,
3be74084 1488 sizeof(is_data_pending));
594c7c00
SM
1489 if (ret_send < 0) {
1490 DBG("Error when sending the data pending ret code: %zd",
1491 ret_send);
489f70e9 1492 goto error_fatal;
ca22feea 1493 }
f50f23d9
DG
1494
1495 /*
1496 * No need to send back a status message since the data pending
1497 * returned value is the response.
1498 */
ca22feea 1499 break;
53632229 1500 }
ffe60014
DG
1501 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1502 {
594c7c00 1503 int ret_ask_channel, ret_add_channel, ret_send;
b623cb6a 1504 struct lttng_ust_ctl_consumer_channel_attr attr;
d2956687
JG
1505 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1506 const struct lttng_credentials buffer_credentials = {
ff588497
JR
1507 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1508 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
d2956687 1509 };
ffe60014
DG
1510
1511 /* Create a plain object and reserve a channel key. */
a2814ea7
JG
1512 channel = consumer_allocate_channel(
1513 msg.u.ask_channel.key,
1514 msg.u.ask_channel.session_id,
d2956687
JG
1515 msg.u.ask_channel.chunk_id.is_set ?
1516 &chunk_id : NULL,
1517 msg.u.ask_channel.pathname,
1518 msg.u.ask_channel.name,
1519 msg.u.ask_channel.relayd_id,
1624d5b7
JD
1520 (enum lttng_event_output) msg.u.ask_channel.output,
1521 msg.u.ask_channel.tracefile_size,
2bba9e53 1522 msg.u.ask_channel.tracefile_count,
1950109e 1523 msg.u.ask_channel.session_id_per_pid,
ecc48a90 1524 msg.u.ask_channel.monitor,
d7ba1388 1525 msg.u.ask_channel.live_timer_interval,
a2814ea7 1526 msg.u.ask_channel.is_live,
3d071855 1527 msg.u.ask_channel.root_shm_path,
d7ba1388 1528 msg.u.ask_channel.shm_path);
ffe60014
DG
1529 if (!channel) {
1530 goto end_channel_error;
1531 }
1532
d2956687
JG
1533 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1534 buffer_credentials);
1535
567eb353
DG
1536 /*
1537 * Assign UST application UID to the channel. This value is ignored for
1538 * per PID buffers. This is specific to UST thus setting this after the
1539 * allocation.
1540 */
1541 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1542
ffe60014
DG
1543 /* Build channel attributes from received message. */
1544 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1545 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1546 attr.overwrite = msg.u.ask_channel.overwrite;
1547 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1548 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
7972aab2 1549 attr.chan_id = msg.u.ask_channel.chan_id;
ffe60014 1550 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
491d1539 1551 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
ffe60014 1552
0c759fc9
DG
1553 /* Match channel buffer type to the UST abi. */
1554 switch (msg.u.ask_channel.output) {
1555 case LTTNG_EVENT_MMAP:
1556 default:
fc4b93fa 1557 attr.output = LTTNG_UST_ABI_MMAP;
0c759fc9
DG
1558 break;
1559 }
1560
ffe60014
DG
1561 /* Translate and save channel type. */
1562 switch (msg.u.ask_channel.type) {
fc4b93fa 1563 case LTTNG_UST_ABI_CHAN_PER_CPU:
ffe60014 1564 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
fc4b93fa 1565 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
8633d6e3
MD
1566 /*
1567 * Set refcount to 1 for owner. Below, we will
1568 * pass ownership to the
1569 * consumer_thread_channel_poll() thread.
1570 */
1571 channel->refcount = 1;
ffe60014 1572 break;
fc4b93fa 1573 case LTTNG_UST_ABI_CHAN_METADATA:
ffe60014 1574 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
fc4b93fa 1575 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
ffe60014
DG
1576 break;
1577 default:
1578 assert(0);
1579 goto error_fatal;
1580 };
1581
9ce5646a
MD
1582 health_code_update();
1583
594c7c00
SM
1584 ret_ask_channel = ask_channel(ctx, channel, &attr);
1585 if (ret_ask_channel < 0) {
ffe60014
DG
1586 goto end_channel_error;
1587 }
1588
fc4b93fa 1589 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
594c7c00
SM
1590 int ret_allocate;
1591
1592 ret_allocate = consumer_metadata_cache_allocate(
1593 channel);
1594 if (ret_allocate < 0) {
fc643247
MD
1595 ERR("Allocating metadata cache");
1596 goto end_channel_error;
1597 }
1598 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1599 attr.switch_timer_interval = 0;
94d49140 1600 } else {
e9404c27
JG
1601 int monitor_start_ret;
1602
94d49140
JD
1603 consumer_timer_live_start(channel,
1604 msg.u.ask_channel.live_timer_interval);
e9404c27
JG
1605 monitor_start_ret = consumer_timer_monitor_start(
1606 channel,
1607 msg.u.ask_channel.monitor_timer_interval);
1608 if (monitor_start_ret < 0) {
1609 ERR("Starting channel monitoring timer failed");
1610 goto end_channel_error;
1611 }
fc643247
MD
1612 }
1613
9ce5646a
MD
1614 health_code_update();
1615
ffe60014
DG
1616 /*
1617 * Add the channel to the internal state AFTER all streams were created
1618 * and successfully sent to session daemon. This way, all streams must
1619 * be ready before this channel is visible to the threads.
fc643247
MD
1620 * If add_channel succeeds, ownership of the channel is
1621 * passed to consumer_thread_channel_poll().
ffe60014 1622 */
594c7c00
SM
1623 ret_add_channel = add_channel(channel, ctx);
1624 if (ret_add_channel < 0) {
fc4b93fa 1625 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
ea88ca2a
MD
1626 if (channel->switch_timer_enabled == 1) {
1627 consumer_timer_switch_stop(channel);
1628 }
1629 consumer_metadata_cache_destroy(channel);
1630 }
d3e2ba59
JD
1631 if (channel->live_timer_enabled == 1) {
1632 consumer_timer_live_stop(channel);
1633 }
e9404c27
JG
1634 if (channel->monitor_timer_enabled == 1) {
1635 consumer_timer_monitor_stop(channel);
1636 }
ffe60014
DG
1637 goto end_channel_error;
1638 }
1639
9ce5646a
MD
1640 health_code_update();
1641
ffe60014
DG
1642 /*
1643 * Channel and streams are now created. Inform the session daemon that
1644 * everything went well and should wait to receive the channel and
1645 * streams with ustctl API.
1646 */
594c7c00
SM
1647 ret_send = consumer_send_status_channel(sock, channel);
1648 if (ret_send < 0) {
ffe60014 1649 /*
489f70e9 1650 * There is probably a problem on the socket.
ffe60014 1651 */
489f70e9 1652 goto error_fatal;
ffe60014
DG
1653 }
1654
1655 break;
1656 }
1657 case LTTNG_CONSUMER_GET_CHANNEL:
1658 {
1659 int ret, relayd_err = 0;
d88aee68 1660 uint64_t key = msg.u.get_channel.key;
594c7c00 1661 struct lttng_consumer_channel *found_channel;
ffe60014 1662
594c7c00
SM
1663 found_channel = consumer_find_channel(key);
1664 if (!found_channel) {
8fd623e0 1665 ERR("UST consumer get channel key %" PRIu64 " not found", key);
e462382a 1666 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
f3a1fc7b 1667 goto end_get_channel;
ffe60014
DG
1668 }
1669
9ce5646a
MD
1670 health_code_update();
1671
a3a86f35 1672 /* Send the channel to sessiond (and relayd, if applicable). */
594c7c00
SM
1673 ret = send_channel_to_sessiond_and_relayd(
1674 sock, found_channel, ctx, &relayd_err);
ffe60014
DG
1675 if (ret < 0) {
1676 if (relayd_err) {
1677 /*
1678 * We were unable to send to the relayd the stream so avoid
1679 * sending back a fatal error to the thread since this is OK
f2a444f1
DG
1680 * and the consumer can continue its work. The above call
1681 * has sent the error status message to the sessiond.
ffe60014 1682 */
f3a1fc7b 1683 goto end_get_channel_nosignal;
ffe60014
DG
1684 }
1685 /*
1686 * The communicaton was broken hence there is a bad state between
1687 * the consumer and sessiond so stop everything.
1688 */
f3a1fc7b 1689 goto error_get_channel_fatal;
ffe60014
DG
1690 }
1691
9ce5646a
MD
1692 health_code_update();
1693
10a50311
JD
1694 /*
1695 * In no monitor mode, the streams ownership is kept inside the channel
1696 * so don't send them to the data thread.
1697 */
594c7c00 1698 if (!found_channel->monitor) {
f3a1fc7b 1699 goto end_get_channel;
10a50311
JD
1700 }
1701
594c7c00 1702 ret = send_streams_to_thread(found_channel, ctx);
d88aee68
DG
1703 if (ret < 0) {
1704 /*
1705 * If we are unable to send the stream to the thread, there is
1706 * a big problem so just stop everything.
1707 */
f3a1fc7b 1708 goto error_get_channel_fatal;
ffe60014 1709 }
ffe60014 1710 /* List MUST be empty after or else it could be reused. */
594c7c00 1711 assert(cds_list_empty(&found_channel->streams.head));
f3a1fc7b 1712end_get_channel:
d88aee68 1713 goto end_msg_sessiond;
f3a1fc7b
JG
1714error_get_channel_fatal:
1715 goto error_fatal;
1716end_get_channel_nosignal:
1717 goto end_nosignal;
d88aee68
DG
1718 }
1719 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1720 {
1721 uint64_t key = msg.u.destroy_channel.key;
d88aee68 1722
a0cbdd2e
MD
1723 /*
1724 * Only called if streams have not been sent to stream
1725 * manager thread. However, channel has been sent to
1726 * channel manager thread.
1727 */
1728 notify_thread_del_channel(ctx, key);
d88aee68 1729 goto end_msg_sessiond;
ffe60014 1730 }
d88aee68
DG
1731 case LTTNG_CONSUMER_CLOSE_METADATA:
1732 {
1733 int ret;
1734
1735 ret = close_metadata(msg.u.close_metadata.key);
1736 if (ret != 0) {
1737 ret_code = ret;
1738 }
1739
1740 goto end_msg_sessiond;
1741 }
7972aab2
DG
1742 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1743 {
1744 int ret;
1745
1746 ret = flush_channel(msg.u.flush_channel.key);
1747 if (ret != 0) {
1748 ret_code = ret;
1749 }
1750
1751 goto end_msg_sessiond;
1752 }
0dd01979
MD
1753 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1754 {
1755 int ret;
1756
1757 ret = clear_quiescent_channel(
1758 msg.u.clear_quiescent_channel.key);
1759 if (ret != 0) {
1760 ret_code = ret;
1761 }
1762
1763 goto end_msg_sessiond;
1764 }
d88aee68 1765 case LTTNG_CONSUMER_PUSH_METADATA:
ffe60014
DG
1766 {
1767 int ret;
d88aee68 1768 uint64_t len = msg.u.push_metadata.len;
d88aee68 1769 uint64_t key = msg.u.push_metadata.key;
331744e3 1770 uint64_t offset = msg.u.push_metadata.target_offset;
93ec662e 1771 uint64_t version = msg.u.push_metadata.version;
594c7c00 1772 struct lttng_consumer_channel *found_channel;
ffe60014 1773
8fd623e0
DG
1774 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1775 len);
ffe60014 1776
594c7c00
SM
1777 found_channel = consumer_find_channel(key);
1778 if (!found_channel) {
000baf6a
DG
1779 /*
1780 * This is possible if the metadata creation on the consumer side
1781 * is in flight vis-a-vis a concurrent push metadata from the
1782 * session daemon. Simply return that the channel failed and the
1783 * session daemon will handle that message correctly considering
1784 * that this race is acceptable thus the DBG() statement here.
1785 */
1786 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1787 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
a8ffe244 1788 goto end_push_metadata_msg_sessiond;
d88aee68
DG
1789 }
1790
9ce5646a
MD
1791 health_code_update();
1792
c585821b
MD
1793 if (!len) {
1794 /*
1795 * There is nothing to receive. We have simply
1796 * checked whether the channel can be found.
1797 */
1798 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
a8ffe244 1799 goto end_push_metadata_msg_sessiond;
c585821b
MD
1800 }
1801
d88aee68 1802 /* Tell session daemon we are ready to receive the metadata. */
0c759fc9 1803 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
ffe60014
DG
1804 if (ret < 0) {
1805 /* Somehow, the session daemon is not responding anymore. */
a8ffe244 1806 goto error_push_metadata_fatal;
d88aee68
DG
1807 }
1808
9ce5646a
MD
1809 health_code_update();
1810
d88aee68 1811 /* Wait for more data. */
9ce5646a
MD
1812 health_poll_entry();
1813 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1814 health_poll_exit();
84382d49 1815 if (ret) {
a8ffe244 1816 goto error_push_metadata_fatal;
d88aee68
DG
1817 }
1818
9ce5646a
MD
1819 health_code_update();
1820
594c7c00 1821 ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len,
95671f53 1822 version, found_channel, false, 1);
d88aee68 1823 if (ret < 0) {
331744e3 1824 /* error receiving from sessiond */
a8ffe244 1825 goto error_push_metadata_fatal;
331744e3
JD
1826 } else {
1827 ret_code = ret;
a8ffe244 1828 goto end_push_metadata_msg_sessiond;
d88aee68 1829 }
a8ffe244
JG
1830end_push_metadata_msg_sessiond:
1831 goto end_msg_sessiond;
1832error_push_metadata_fatal:
1833 goto error_fatal;
d88aee68
DG
1834 }
1835 case LTTNG_CONSUMER_SETUP_METADATA:
1836 {
1837 int ret;
1838
1839 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1840 if (ret) {
1841 ret_code = ret;
1842 }
1843 goto end_msg_sessiond;
ffe60014 1844 }
6dc3064a
DG
1845 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1846 {
594c7c00 1847 struct lttng_consumer_channel *found_channel;
3eb928aa 1848 uint64_t key = msg.u.snapshot_channel.key;
594c7c00 1849 int ret_send;
3eb928aa 1850
594c7c00
SM
1851 found_channel = consumer_find_channel(key);
1852 if (!found_channel) {
3eb928aa
MD
1853 DBG("UST snapshot channel not found for key %" PRIu64, key);
1854 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
10a50311 1855 } else {
3eb928aa 1856 if (msg.u.snapshot_channel.metadata) {
594c7c00
SM
1857 int ret_snapshot;
1858
1859 ret_snapshot = snapshot_metadata(found_channel,
1860 key,
3eb928aa
MD
1861 msg.u.snapshot_channel.pathname,
1862 msg.u.snapshot_channel.relayd_id,
d2956687 1863 ctx);
594c7c00 1864 if (ret_snapshot < 0) {
3eb928aa
MD
1865 ERR("Snapshot metadata failed");
1866 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1867 }
1868 } else {
594c7c00
SM
1869 int ret_snapshot;
1870
1871 ret_snapshot = snapshot_channel(found_channel,
1872 key,
3eb928aa
MD
1873 msg.u.snapshot_channel.pathname,
1874 msg.u.snapshot_channel.relayd_id,
594c7c00
SM
1875 msg.u.snapshot_channel
1876 .nb_packets_per_stream,
3eb928aa 1877 ctx);
594c7c00 1878 if (ret_snapshot < 0) {
3eb928aa
MD
1879 ERR("Snapshot channel failed");
1880 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1881 }
10a50311
JD
1882 }
1883 }
9ce5646a 1884 health_code_update();
594c7c00
SM
1885 ret_send = consumer_send_status_msg(sock, ret_code);
1886 if (ret_send < 0) {
6dc3064a
DG
1887 /* Somehow, the session daemon is not responding anymore. */
1888 goto end_nosignal;
1889 }
9ce5646a 1890 health_code_update();
6dc3064a
DG
1891 break;
1892 }
fb83fe64
JD
1893 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1894 {
beb59458
MJ
1895 int ret = 0;
1896 uint64_t discarded_events;
fb83fe64
JD
1897 struct lttng_ht_iter iter;
1898 struct lttng_ht *ht;
1899 struct lttng_consumer_stream *stream;
1900 uint64_t id = msg.u.discarded_events.session_id;
1901 uint64_t key = msg.u.discarded_events.channel_key;
1902
1903 DBG("UST consumer discarded events command for session id %"
1904 PRIu64, id);
1905 rcu_read_lock();
fa29bfbf 1906 pthread_mutex_lock(&the_consumer_data.lock);
fb83fe64 1907
fa29bfbf 1908 ht = the_consumer_data.stream_list_ht;
fb83fe64
JD
1909
1910 /*
1911 * We only need a reference to the channel, but they are not
1912 * directly indexed, so we just use the first matching stream
1913 * to extract the information we need, we default to 0 if not
1914 * found (no events are dropped if the channel is not yet in
1915 * use).
1916 */
beb59458 1917 discarded_events = 0;
fb83fe64
JD
1918 cds_lfht_for_each_entry_duplicate(ht->ht,
1919 ht->hash_fct(&id, lttng_ht_seed),
1920 ht->match_fct, &id,
1921 &iter.iter, stream, node_session_id.node) {
1922 if (stream->chan->key == key) {
beb59458 1923 discarded_events = stream->chan->discarded_events;
fb83fe64
JD
1924 break;
1925 }
1926 }
fa29bfbf 1927 pthread_mutex_unlock(&the_consumer_data.lock);
fb83fe64
JD
1928 rcu_read_unlock();
1929
1930 DBG("UST consumer discarded events command for session id %"
1931 PRIu64 ", channel key %" PRIu64, id, key);
1932
1933 health_code_update();
1934
1935 /* Send back returned value to session daemon */
beb59458 1936 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
fb83fe64
JD
1937 if (ret < 0) {
1938 PERROR("send discarded events");
1939 goto error_fatal;
1940 }
1941
1942 break;
1943 }
1944 case LTTNG_CONSUMER_LOST_PACKETS:
1945 {
9a06e8d4
JG
1946 int ret;
1947 uint64_t lost_packets;
fb83fe64
JD
1948 struct lttng_ht_iter iter;
1949 struct lttng_ht *ht;
1950 struct lttng_consumer_stream *stream;
1951 uint64_t id = msg.u.lost_packets.session_id;
1952 uint64_t key = msg.u.lost_packets.channel_key;
1953
1954 DBG("UST consumer lost packets command for session id %"
1955 PRIu64, id);
1956 rcu_read_lock();
fa29bfbf 1957 pthread_mutex_lock(&the_consumer_data.lock);
fb83fe64 1958
fa29bfbf 1959 ht = the_consumer_data.stream_list_ht;
fb83fe64
JD
1960
1961 /*
1962 * We only need a reference to the channel, but they are not
1963 * directly indexed, so we just use the first matching stream
1964 * to extract the information we need, we default to 0 if not
1965 * found (no packets lost if the channel is not yet in use).
1966 */
9a06e8d4 1967 lost_packets = 0;
fb83fe64
JD
1968 cds_lfht_for_each_entry_duplicate(ht->ht,
1969 ht->hash_fct(&id, lttng_ht_seed),
1970 ht->match_fct, &id,
1971 &iter.iter, stream, node_session_id.node) {
1972 if (stream->chan->key == key) {
9a06e8d4 1973 lost_packets = stream->chan->lost_packets;
fb83fe64
JD
1974 break;
1975 }
1976 }
fa29bfbf 1977 pthread_mutex_unlock(&the_consumer_data.lock);
fb83fe64
JD
1978 rcu_read_unlock();
1979
1980 DBG("UST consumer lost packets command for session id %"
1981 PRIu64 ", channel key %" PRIu64, id, key);
1982
1983 health_code_update();
1984
1985 /* Send back returned value to session daemon */
9a06e8d4
JG
1986 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1987 sizeof(lost_packets));
fb83fe64
JD
1988 if (ret < 0) {
1989 PERROR("send lost packets");
1990 goto error_fatal;
1991 }
1992
1993 break;
1994 }
b3530820
JG
1995 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1996 {
594c7c00
SM
1997 int channel_monitor_pipe, ret_send,
1998 ret_set_channel_monitor_pipe;
1999 ssize_t ret_recv;
b3530820
JG
2000
2001 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2002 /* Successfully received the command's type. */
594c7c00
SM
2003 ret_send = consumer_send_status_msg(sock, ret_code);
2004 if (ret_send < 0) {
b3530820
JG
2005 goto error_fatal;
2006 }
2007
594c7c00
SM
2008 ret_recv = lttcomm_recv_fds_unix_sock(
2009 sock, &channel_monitor_pipe, 1);
2010 if (ret_recv != sizeof(channel_monitor_pipe)) {
b3530820
JG
2011 ERR("Failed to receive channel monitor pipe");
2012 goto error_fatal;
2013 }
2014
2015 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
594c7c00
SM
2016 ret_set_channel_monitor_pipe =
2017 consumer_timer_thread_set_channel_monitor_pipe(
2018 channel_monitor_pipe);
2019 if (!ret_set_channel_monitor_pipe) {
b3530820 2020 int flags;
594c7c00 2021 int ret_fcntl;
b3530820
JG
2022
2023 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2024 /* Set the pipe as non-blocking. */
594c7c00
SM
2025 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
2026 if (ret_fcntl == -1) {
b3530820
JG
2027 PERROR("fcntl get flags of the channel monitoring pipe");
2028 goto error_fatal;
2029 }
594c7c00 2030 flags = ret_fcntl;
b3530820 2031
594c7c00 2032 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
b3530820 2033 flags | O_NONBLOCK);
594c7c00 2034 if (ret_fcntl == -1) {
b3530820
JG
2035 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2036 goto error_fatal;
2037 }
2038 DBG("Channel monitor pipe set as non-blocking");
2039 } else {
2040 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2041 }
2042 goto end_msg_sessiond;
2043 }
b99a8d42
JD
2044 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2045 {
594c7c00 2046 struct lttng_consumer_channel *found_channel;
92b7a7f8 2047 uint64_t key = msg.u.rotate_channel.key;
594c7c00 2048 int ret_send_status;
b99a8d42 2049
594c7c00
SM
2050 found_channel = consumer_find_channel(key);
2051 if (!found_channel) {
92b7a7f8
MD
2052 DBG("Channel %" PRIu64 " not found", key);
2053 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2054 } else {
594c7c00
SM
2055 int rotate_channel;
2056
92b7a7f8
MD
2057 /*
2058 * Sample the rotate position of all the streams in
2059 * this channel.
2060 */
594c7c00
SM
2061 rotate_channel = lttng_consumer_rotate_channel(
2062 found_channel, key,
92b7a7f8 2063 msg.u.rotate_channel.relayd_id,
594c7c00
SM
2064 msg.u.rotate_channel.metadata, ctx);
2065 if (rotate_channel < 0) {
92b7a7f8
MD
2066 ERR("Rotate channel failed");
2067 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2068 }
b99a8d42 2069
92b7a7f8
MD
2070 health_code_update();
2071 }
594c7c00
SM
2072
2073 ret_send_status = consumer_send_status_msg(sock, ret_code);
2074 if (ret_send_status < 0) {
b99a8d42 2075 /* Somehow, the session daemon is not responding anymore. */
41c7a76d 2076 goto end_rotate_channel_nosignal;
b99a8d42
JD
2077 }
2078
2079 /*
2080 * Rotate the streams that are ready right now.
2081 * FIXME: this is a second consecutive iteration over the
2082 * streams in a channel, there is probably a better way to
2083 * handle this, but it needs to be after the
2084 * consumer_send_status_msg() call.
2085 */
594c7c00
SM
2086 if (found_channel) {
2087 int ret_rotate_read_streams;
2088
2089 ret_rotate_read_streams =
2090 lttng_consumer_rotate_ready_streams(
2091 found_channel, key,
2092 ctx);
2093 if (ret_rotate_read_streams < 0) {
92b7a7f8
MD
2094 ERR("Rotate channel failed");
2095 }
b99a8d42
JD
2096 }
2097 break;
41c7a76d
JG
2098end_rotate_channel_nosignal:
2099 goto end_nosignal;
b99a8d42 2100 }
5f3aff8b
MD
2101 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2102 {
594c7c00 2103 struct lttng_consumer_channel *found_channel;
5f3aff8b 2104 uint64_t key = msg.u.clear_channel.key;
594c7c00 2105 int ret_send_status;
5f3aff8b 2106
594c7c00
SM
2107 found_channel = consumer_find_channel(key);
2108 if (!found_channel) {
5f3aff8b
MD
2109 DBG("Channel %" PRIu64 " not found", key);
2110 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2111 } else {
594c7c00
SM
2112 int ret_clear_channel;
2113
2114 ret_clear_channel = lttng_consumer_clear_channel(
2115 found_channel);
2116 if (ret_clear_channel) {
5f3aff8b 2117 ERR("Clear channel failed key %" PRIu64, key);
594c7c00 2118 ret_code = ret_clear_channel;
5f3aff8b
MD
2119 }
2120
2121 health_code_update();
2122 }
594c7c00
SM
2123 ret_send_status = consumer_send_status_msg(sock, ret_code);
2124 if (ret_send_status < 0) {
5f3aff8b
MD
2125 /* Somehow, the session daemon is not responding anymore. */
2126 goto end_nosignal;
2127 }
2128 break;
2129 }
d2956687 2130 case LTTNG_CONSUMER_INIT:
00fb02ac 2131 {
594c7c00
SM
2132 int ret_send_status;
2133
d2956687
JG
2134 ret_code = lttng_consumer_init_command(ctx,
2135 msg.u.init.sessiond_uuid);
d88744a4 2136 health_code_update();
594c7c00
SM
2137 ret_send_status = consumer_send_status_msg(sock, ret_code);
2138 if (ret_send_status < 0) {
d88744a4
JD
2139 /* Somehow, the session daemon is not responding anymore. */
2140 goto end_nosignal;
2141 }
2142 break;
2143 }
d2956687 2144 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
d88744a4 2145 {
d2956687 2146 const struct lttng_credentials credentials = {
ff588497
JR
2147 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2148 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
d2956687
JG
2149 };
2150 const bool is_local_trace =
2151 !msg.u.create_trace_chunk.relayd_id.is_set;
2152 const uint64_t relayd_id =
2153 msg.u.create_trace_chunk.relayd_id.value;
2154 const char *chunk_override_name =
2155 *msg.u.create_trace_chunk.override_name ?
2156 msg.u.create_trace_chunk.override_name :
2157 NULL;
cbf53d23 2158 struct lttng_directory_handle *chunk_directory_handle = NULL;
d88744a4 2159
d2956687
JG
2160 /*
2161 * The session daemon will only provide a chunk directory file
2162 * descriptor for local traces.
2163 */
2164 if (is_local_trace) {
2165 int chunk_dirfd;
594c7c00
SM
2166 int ret_send_status;
2167 ssize_t ret_recv;
19990ed5 2168
d2956687 2169 /* Acnowledge the reception of the command. */
594c7c00
SM
2170 ret_send_status = consumer_send_status_msg(
2171 sock, LTTCOMM_CONSUMERD_SUCCESS);
2172 if (ret_send_status < 0) {
d2956687
JG
2173 /* Somehow, the session daemon is not responding anymore. */
2174 goto end_nosignal;
2175 }
92816cc3 2176
5da88b0f
MD
2177 /*
2178 * Receive trace chunk domain dirfd.
2179 */
594c7c00
SM
2180 ret_recv = lttcomm_recv_fds_unix_sock(
2181 sock, &chunk_dirfd, 1);
2182 if (ret_recv != sizeof(chunk_dirfd)) {
5da88b0f 2183 ERR("Failed to receive trace chunk domain directory file descriptor");
d2956687
JG
2184 goto error_fatal;
2185 }
92816cc3 2186
5da88b0f 2187 DBG("Received trace chunk domain directory fd (%d)",
d2956687 2188 chunk_dirfd);
cbf53d23 2189 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
d2956687 2190 chunk_dirfd);
cbf53d23 2191 if (!chunk_directory_handle) {
5da88b0f 2192 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
d2956687
JG
2193 if (close(chunk_dirfd)) {
2194 PERROR("Failed to close chunk directory file descriptor");
2195 }
2196 goto error_fatal;
2197 }
92816cc3
JG
2198 }
2199
d2956687
JG
2200 ret_code = lttng_consumer_create_trace_chunk(
2201 !is_local_trace ? &relayd_id : NULL,
2202 msg.u.create_trace_chunk.session_id,
2203 msg.u.create_trace_chunk.chunk_id,
e5add6d0
JG
2204 (time_t) msg.u.create_trace_chunk
2205 .creation_timestamp,
d2956687 2206 chunk_override_name,
e5add6d0
JG
2207 msg.u.create_trace_chunk.credentials.is_set ?
2208 &credentials :
2209 NULL,
cbf53d23
JG
2210 chunk_directory_handle);
2211 lttng_directory_handle_put(chunk_directory_handle);
d2956687 2212 goto end_msg_sessiond;
00fb02ac 2213 }
d2956687 2214 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
a1ae2ea5 2215 {
bbc4768c
JG
2216 enum lttng_trace_chunk_command_type close_command =
2217 msg.u.close_trace_chunk.close_command.value;
d2956687
JG
2218 const uint64_t relayd_id =
2219 msg.u.close_trace_chunk.relayd_id.value;
ecd1a12f 2220 struct lttcomm_consumer_close_trace_chunk_reply reply;
d00fb490 2221 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
ecd1a12f 2222 int ret;
d2956687
JG
2223
2224 ret_code = lttng_consumer_close_trace_chunk(
2225 msg.u.close_trace_chunk.relayd_id.is_set ?
bbc4768c
JG
2226 &relayd_id :
2227 NULL,
d2956687
JG
2228 msg.u.close_trace_chunk.session_id,
2229 msg.u.close_trace_chunk.chunk_id,
bbc4768c
JG
2230 (time_t) msg.u.close_trace_chunk.close_timestamp,
2231 msg.u.close_trace_chunk.close_command.is_set ?
2232 &close_command :
ecd1a12f
MD
2233 NULL, closed_trace_chunk_path);
2234 reply.ret_code = ret_code;
2235 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2236 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2237 if (ret != sizeof(reply)) {
2238 goto error_fatal;
2239 }
2240 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2241 reply.path_length);
2242 if (ret != reply.path_length) {
2243 goto error_fatal;
2244 }
2245 goto end_nosignal;
a1ae2ea5 2246 }
d2956687 2247 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
3654ed19 2248 {
d2956687
JG
2249 const uint64_t relayd_id =
2250 msg.u.trace_chunk_exists.relayd_id.value;
2251
2252 ret_code = lttng_consumer_trace_chunk_exists(
2253 msg.u.trace_chunk_exists.relayd_id.is_set ?
2254 &relayd_id : NULL,
2255 msg.u.trace_chunk_exists.session_id,
2256 msg.u.trace_chunk_exists.chunk_id);
2257 goto end_msg_sessiond;
04ed9e10
JG
2258 }
2259 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2260 {
2261 const uint64_t key = msg.u.open_channel_packets.key;
594c7c00 2262 struct lttng_consumer_channel *found_channel =
04ed9e10
JG
2263 consumer_find_channel(key);
2264
594c7c00
SM
2265 if (found_channel) {
2266 pthread_mutex_lock(&found_channel->lock);
2267 ret_code = lttng_consumer_open_channel_packets(
2268 found_channel);
2269 pthread_mutex_unlock(&found_channel->lock);
04ed9e10
JG
2270 } else {
2271 /*
2272 * The channel could have disappeared in per-pid
2273 * buffering mode.
2274 */
2275 DBG("Channel %" PRIu64 " not found", key);
2276 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2277 }
2278
2279 health_code_update();
2280 goto end_msg_sessiond;
3654ed19 2281 }
3bd1e081
MD
2282 default:
2283 break;
2284 }
3f8e211f 2285
3bd1e081 2286end_nosignal:
4cbc1a04
DG
2287 /*
2288 * Return 1 to indicate success since the 0 value can be a socket
2289 * shutdown during the recv() or send() call.
2290 */
594c7c00 2291 ret_func = 1;
f3a1fc7b 2292 goto end;
ffe60014
DG
2293
2294end_msg_sessiond:
2295 /*
2296 * The returned value here is not useful since either way we'll return 1 to
2297 * the caller because the session daemon socket management is done
2298 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2299 */
594c7c00
SM
2300 {
2301 int ret_send_status;
2302
2303 ret_send_status = consumer_send_status_msg(sock, ret_code);
2304 if (ret_send_status < 0) {
2305 goto error_fatal;
2306 }
489f70e9 2307 }
594c7c00
SM
2308
2309 ret_func = 1;
f3a1fc7b 2310 goto end;
9ce5646a 2311
ffe60014
DG
2312end_channel_error:
2313 if (channel) {
2314 /*
2315 * Free channel here since no one has a reference to it. We don't
2316 * free after that because a stream can store this pointer.
2317 */
2318 destroy_channel(channel);
2319 }
2320 /* We have to send a status channel message indicating an error. */
594c7c00
SM
2321 {
2322 int ret_send_status;
2323
2324 ret_send_status = consumer_send_status_channel(sock, NULL);
2325 if (ret_send_status < 0) {
2326 /* Stop everything if session daemon can not be notified. */
2327 goto error_fatal;
2328 }
ffe60014 2329 }
594c7c00
SM
2330
2331 ret_func = 1;
f3a1fc7b 2332 goto end;
9ce5646a 2333
ffe60014 2334error_fatal:
ffe60014 2335 /* This will issue a consumer stop. */
594c7c00 2336 ret_func = -1;
f3a1fc7b
JG
2337 goto end;
2338
2339end:
2340 rcu_read_unlock();
2341 health_code_update();
594c7c00 2342 return ret_func;
3bd1e081
MD
2343}
2344
d85707b0
MD
2345int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream,
2346 int producer_active)
fc6d7a51
JD
2347{
2348 assert(stream);
2349 assert(stream->ustream);
2350
d85707b0 2351 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
fc6d7a51
JD
2352}
2353
ffe60014 2354/*
e9404c27 2355 * Take a snapshot for a specific stream.
ffe60014
DG
2356 *
2357 * Returns 0 on success, < 0 on error
2358 */
2359int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081 2360{
ffe60014
DG
2361 assert(stream);
2362 assert(stream->ustream);
2363
b623cb6a 2364 return lttng_ust_ctl_snapshot(stream->ustream);
3bd1e081
MD
2365}
2366
e9404c27
JG
2367/*
2368 * Sample consumed and produced positions for a specific stream.
2369 *
2370 * Returns 0 on success, < 0 on error.
2371 */
2372int lttng_ustconsumer_sample_snapshot_positions(
2373 struct lttng_consumer_stream *stream)
2374{
2375 assert(stream);
2376 assert(stream->ustream);
2377
b623cb6a 2378 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
e9404c27
JG
2379}
2380
ffe60014
DG
2381/*
2382 * Get the produced position
2383 *
2384 * Returns 0 on success, < 0 on error
2385 */
2386int lttng_ustconsumer_get_produced_snapshot(
2387 struct lttng_consumer_stream *stream, unsigned long *pos)
3bd1e081 2388{
ffe60014
DG
2389 assert(stream);
2390 assert(stream->ustream);
2391 assert(pos);
7a57cf92 2392
b623cb6a 2393 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
ffe60014 2394}
7a57cf92 2395
10a50311
JD
2396/*
2397 * Get the consumed position
2398 *
2399 * Returns 0 on success, < 0 on error
2400 */
2401int lttng_ustconsumer_get_consumed_snapshot(
2402 struct lttng_consumer_stream *stream, unsigned long *pos)
2403{
2404 assert(stream);
2405 assert(stream->ustream);
2406 assert(pos);
2407
b623cb6a 2408 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
10a50311
JD
2409}
2410
d85707b0 2411int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
84a182ce
DG
2412 int producer)
2413{
2414 assert(stream);
2415 assert(stream->ustream);
2416
d85707b0 2417 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
84a182ce
DG
2418}
2419
d85707b0 2420int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
214f70e0
JR
2421{
2422 assert(stream);
2423 assert(stream->ustream);
2424
d85707b0 2425 return lttng_ust_ctl_clear_buffer(stream->ustream);
214f70e0
JR
2426}
2427
84a182ce
DG
2428int lttng_ustconsumer_get_current_timestamp(
2429 struct lttng_consumer_stream *stream, uint64_t *ts)
2430{
2431 assert(stream);
2432 assert(stream->ustream);
2433 assert(ts);
2434
b623cb6a 2435 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
84a182ce
DG
2436}
2437
fb83fe64
JD
2438int lttng_ustconsumer_get_sequence_number(
2439 struct lttng_consumer_stream *stream, uint64_t *seq)
2440{
2441 assert(stream);
2442 assert(stream->ustream);
2443 assert(seq);
2444
b623cb6a 2445 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
fb83fe64
JD
2446}
2447
ffe60014 2448/*
0dd01979 2449 * Called when the stream signals the consumer that it has hung up.
ffe60014
DG
2450 */
2451void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2452{
2453 assert(stream);
2454 assert(stream->ustream);
2c1dd183 2455
0dd01979
MD
2456 pthread_mutex_lock(&stream->lock);
2457 if (!stream->quiescent) {
d85707b0
MD
2458 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2459 ERR("Failed to flush buffer on stream hang-up");
2460 } else {
2461 stream->quiescent = true;
2462 }
0dd01979 2463 }
d2dc232d 2464
ffe60014 2465 stream->hangup_flush_done = 1;
d2dc232d 2466 pthread_mutex_unlock(&stream->lock);
ffe60014 2467}
ee77a7b0 2468
ffe60014
DG
2469void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2470{
4628484a
MD
2471 int i;
2472
ffe60014
DG
2473 assert(chan);
2474 assert(chan->uchan);
d2956687 2475 assert(chan->buffer_credentials.is_set);
e316aad5 2476
ea88ca2a
MD
2477 if (chan->switch_timer_enabled == 1) {
2478 consumer_timer_switch_stop(chan);
2479 }
4628484a
MD
2480 for (i = 0; i < chan->nr_stream_fds; i++) {
2481 int ret;
2482
2483 ret = close(chan->stream_fds[i]);
2484 if (ret) {
2485 PERROR("close");
2486 }
2487 if (chan->shm_path[0]) {
2488 char shm_path[PATH_MAX];
2489
2490 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2491 if (ret) {
2492 ERR("Cannot get stream shm path");
2493 }
d2956687 2494 ret = run_as_unlink(shm_path,
ff588497
JR
2495 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2496 chan->buffer_credentials)),
2497 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2498 chan->buffer_credentials)));
4628484a 2499 if (ret) {
4628484a
MD
2500 PERROR("unlink %s", shm_path);
2501 }
2502 }
2503 }
3bd1e081
MD
2504}
2505
b83e03c4
MD
2506void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2507{
2508 assert(chan);
2509 assert(chan->uchan);
d2956687 2510 assert(chan->buffer_credentials.is_set);
b83e03c4
MD
2511
2512 consumer_metadata_cache_destroy(chan);
b623cb6a 2513 lttng_ust_ctl_destroy_channel(chan->uchan);
ea853771
JR
2514 /* Try to rmdir all directories under shm_path root. */
2515 if (chan->root_shm_path[0]) {
602766ec 2516 (void) run_as_rmdir_recursive(chan->root_shm_path,
ff588497
JR
2517 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2518 chan->buffer_credentials)),
2519 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2520 chan->buffer_credentials)),
f75c5439 2521 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
ea853771 2522 }
b83e03c4
MD
2523 free(chan->stream_fds);
2524}
2525
3bd1e081
MD
2526void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2527{
ffe60014
DG
2528 assert(stream);
2529 assert(stream->ustream);
d41f73b7 2530
ea88ca2a
MD
2531 if (stream->chan->switch_timer_enabled == 1) {
2532 consumer_timer_switch_stop(stream->chan);
2533 }
b623cb6a 2534 lttng_ust_ctl_destroy_stream(stream->ustream);
ffe60014 2535}
d41f73b7 2536
6d574024
DG
2537int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2538{
2539 assert(stream);
2540 assert(stream->ustream);
2541
b623cb6a 2542 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
6d574024
DG
2543}
2544
2545int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2546{
2547 assert(stream);
2548 assert(stream->ustream);
2549
b623cb6a 2550 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
6d574024
DG
2551}
2552
94d49140
JD
2553/*
2554 * Write up to one packet from the metadata cache to the channel.
2555 *
577eea73
JG
2556 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2557 * negative value on error.
94d49140
JD
2558 */
2559static
2560int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2561{
2562 ssize_t write_len;
2563 int ret;
2564
2565 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828
JG
2566 if (stream->chan->metadata_cache->contents.size ==
2567 stream->ust_metadata_pushed) {
55954e07
JG
2568 /*
2569 * In the context of a user space metadata channel, a
2570 * change in version can be detected in two ways:
2571 * 1) During the pre-consume of the `read_subbuffer` loop,
2572 * 2) When populating the metadata ring buffer (i.e. here).
2573 *
2574 * This function is invoked when there is no metadata
2575 * available in the ring-buffer. If all data was consumed
2576 * up to the size of the metadata cache, there is no metadata
2577 * to insert in the ring-buffer.
2578 *
2579 * However, the metadata version could still have changed (a
2580 * regeneration without any new data will yield the same cache
2581 * size).
2582 *
2583 * The cache's version is checked for a version change and the
2584 * consumed position is reset if one occurred.
2585 *
2586 * This check is only necessary for the user space domain as
2587 * it has to manage the cache explicitly. If this reset was not
2588 * performed, no metadata would be consumed (and no reset would
2589 * occur as part of the pre-consume) until the metadata size
2590 * exceeded the cache size.
2591 */
2592 if (stream->metadata_version !=
2593 stream->chan->metadata_cache->version) {
2594 metadata_stream_reset_cache_consumed_position(stream);
2595 consumer_stream_metadata_set_version(stream,
2596 stream->chan->metadata_cache->version);
2597 } else {
2598 ret = 0;
2599 goto end;
2600 }
94d49140
JD
2601 }
2602
b623cb6a 2603 write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan,
9eac9828
JG
2604 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2605 stream->chan->metadata_cache->contents.size -
2606 stream->ust_metadata_pushed);
94d49140
JD
2607 assert(write_len != 0);
2608 if (write_len < 0) {
2609 ERR("Writing one metadata packet");
f5ba75b4 2610 ret = write_len;
94d49140
JD
2611 goto end;
2612 }
2613 stream->ust_metadata_pushed += write_len;
95671f53 2614 lttng_wait_queue_wake_all(&stream->chan->metadata_pushed_wait_queue);
94d49140 2615
9eac9828 2616 assert(stream->chan->metadata_cache->contents.size >=
94d49140
JD
2617 stream->ust_metadata_pushed);
2618 ret = write_len;
2619
0d88e046
JG
2620 /*
2621 * Switch packet (but don't open the next one) on every commit of
2622 * a metadata packet. Since the subbuffer is fully filled (with padding,
2623 * if needed), the stream is "quiescent" after this commit.
2624 */
d85707b0
MD
2625 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
2626 ERR("Failed to flush buffer while commiting one metadata packet");
2627 ret = -EIO;
2628 } else {
2629 stream->quiescent = true;
2630 }
94d49140
JD
2631end:
2632 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2633 return ret;
2634}
2635
309167d2 2636
94d49140
JD
2637/*
2638 * Sync metadata meaning request them to the session daemon and snapshot to the
2639 * metadata thread can consumer them.
2640 *
c585821b
MD
2641 * Metadata stream lock is held here, but we need to release it when
2642 * interacting with sessiond, else we cause a deadlock with live
2643 * awaiting on metadata to be pushed out.
94d49140 2644 *
cdb72e4e 2645 * The RCU read side lock must be held by the caller.
94d49140 2646 */
577eea73
JG
2647enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2648 struct lttng_consumer_local_data *ctx,
cdb72e4e 2649 struct lttng_consumer_stream *metadata_stream)
94d49140
JD
2650{
2651 int ret;
577eea73 2652 enum sync_metadata_status status;
cdb72e4e 2653 struct lttng_consumer_channel *metadata_channel;
94d49140
JD
2654
2655 assert(ctx);
cdb72e4e 2656 assert(metadata_stream);
94d49140 2657
cdb72e4e
JG
2658 metadata_channel = metadata_stream->chan;
2659 pthread_mutex_unlock(&metadata_stream->lock);
94d49140
JD
2660 /*
2661 * Request metadata from the sessiond, but don't wait for the flush
2662 * because we locked the metadata thread.
2663 */
95671f53 2664 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 0);
cdb72e4e 2665 pthread_mutex_lock(&metadata_stream->lock);
94d49140 2666 if (ret < 0) {
577eea73 2667 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2668 goto end;
2669 }
2670
cdb72e4e
JG
2671 /*
2672 * The metadata stream and channel can be deleted while the
2673 * metadata stream lock was released. The streamed is checked
2674 * for deletion before we use it further.
2675 *
2676 * Note that it is safe to access a logically-deleted stream since its
2677 * existence is still guaranteed by the RCU read side lock. However,
2678 * it should no longer be used. The close/deletion of the metadata
2679 * channel and stream already guarantees that all metadata has been
2680 * consumed. Therefore, there is nothing left to do in this function.
2681 */
2682 if (consumer_stream_is_deleted(metadata_stream)) {
2683 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2684 metadata_stream->key);
577eea73 2685 status = SYNC_METADATA_STATUS_NO_DATA;
cdb72e4e
JG
2686 goto end;
2687 }
2688
2689 ret = commit_one_metadata_packet(metadata_stream);
577eea73
JG
2690 if (ret < 0) {
2691 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2692 goto end;
2693 } else if (ret > 0) {
577eea73
JG
2694 status = SYNC_METADATA_STATUS_NEW_DATA;
2695 } else /* ret == 0 */ {
2696 status = SYNC_METADATA_STATUS_NO_DATA;
2697 goto end;
94d49140
JD
2698 }
2699
b623cb6a 2700 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
94d49140 2701 if (ret < 0) {
577eea73
JG
2702 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2703 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2704 goto end;
2705 }
2706
94d49140 2707end:
577eea73 2708 return status;
94d49140
JD
2709}
2710
02b3d176
DG
2711/*
2712 * Return 0 on success else a negative value.
2713 */
2714static int notify_if_more_data(struct lttng_consumer_stream *stream,
2715 struct lttng_consumer_local_data *ctx)
2716{
2717 int ret;
b623cb6a 2718 struct lttng_ust_ctl_consumer_stream *ustream;
02b3d176
DG
2719
2720 assert(stream);
2721 assert(ctx);
2722
2723 ustream = stream->ustream;
2724
2725 /*
2726 * First, we are going to check if there is a new subbuffer available
2727 * before reading the stream wait_fd.
2728 */
2729 /* Get the next subbuffer */
b623cb6a 2730 ret = lttng_ust_ctl_get_next_subbuf(ustream);
02b3d176
DG
2731 if (ret) {
2732 /* No more data found, flag the stream. */
2733 stream->has_data = 0;
2734 ret = 0;
2735 goto end;
2736 }
2737
b623cb6a 2738 ret = lttng_ust_ctl_put_subbuf(ustream);
02b3d176
DG
2739 assert(!ret);
2740
2741 /* This stream still has data. Flag it and wake up the data thread. */
2742 stream->has_data = 1;
2743
2744 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2745 ssize_t writelen;
2746
2747 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2748 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2749 ret = writelen;
2750 goto end;
2751 }
2752
2753 /* The wake up pipe has been notified. */
2754 ctx->has_wakeup = 1;
2755 }
2756 ret = 0;
2757
2758end:
2759 return ret;
2760}
2761
6f9449c2 2762static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
fb83fe64 2763{
6f9449c2 2764 int ret = 0;
fb83fe64 2765
fb83fe64 2766 /*
6f9449c2
JG
2767 * We can consume the 1 byte written into the wait_fd by
2768 * UST. Don't trigger error if we cannot read this one byte
2769 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2770 *
2771 * This is only done when the stream is monitored by a thread,
2772 * before the flush is done after a hangup and if the stream
2773 * is not flagged with data since there might be nothing to
2774 * consume in the wait fd but still have data available
2775 * flagged by the consumer wake up pipe.
fb83fe64 2776 */
6f9449c2
JG
2777 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2778 char dummy;
2779 ssize_t readlen;
2780
2781 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2782 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2783 ret = readlen;
2784 }
fb83fe64 2785 }
fb83fe64 2786
6f9449c2
JG
2787 return ret;
2788}
2789
2790static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2791 struct stream_subbuffer *subbuf)
2792{
2793 int ret;
2794
b623cb6a 2795 ret = lttng_ust_ctl_get_subbuf_size(
6f9449c2
JG
2796 stream->ustream, &subbuf->info.data.subbuf_size);
2797 if (ret) {
fb83fe64
JD
2798 goto end;
2799 }
6f9449c2 2800
b623cb6a 2801 ret = lttng_ust_ctl_get_padded_subbuf_size(
6f9449c2
JG
2802 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2803 if (ret) {
2804 goto end;
fb83fe64 2805 }
fb83fe64
JD
2806
2807end:
2808 return ret;
2809}
2810
6f9449c2
JG
2811static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2812 struct stream_subbuffer *subbuf)
d41f73b7 2813{
6f9449c2 2814 int ret;
ffe60014 2815
6f9449c2
JG
2816 ret = extract_common_subbuffer_info(stream, subbuf);
2817 if (ret) {
2818 goto end;
2819 }
d41f73b7 2820
55954e07 2821 subbuf->info.metadata.version = stream->metadata_version;
ffe60014 2822
6f9449c2
JG
2823end:
2824 return ret;
2825}
d41f73b7 2826
6f9449c2
JG
2827static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2828 struct stream_subbuffer *subbuf)
2829{
2830 int ret;
c617c0c6 2831
6f9449c2
JG
2832 ret = extract_common_subbuffer_info(stream, subbuf);
2833 if (ret) {
2834 goto end;
02d02e31
JD
2835 }
2836
b623cb6a 2837 ret = lttng_ust_ctl_get_packet_size(
6f9449c2
JG
2838 stream->ustream, &subbuf->info.data.packet_size);
2839 if (ret < 0) {
2840 PERROR("Failed to get sub-buffer packet size");
2841 goto end;
2842 }
04ef1097 2843
b623cb6a 2844 ret = lttng_ust_ctl_get_content_size(
6f9449c2
JG
2845 stream->ustream, &subbuf->info.data.content_size);
2846 if (ret < 0) {
2847 PERROR("Failed to get sub-buffer content size");
2848 goto end;
d41f73b7 2849 }
309167d2 2850
b623cb6a 2851 ret = lttng_ust_ctl_get_timestamp_begin(
6f9449c2
JG
2852 stream->ustream, &subbuf->info.data.timestamp_begin);
2853 if (ret < 0) {
2854 PERROR("Failed to get sub-buffer begin timestamp");
2855 goto end;
2856 }
fb83fe64 2857
b623cb6a 2858 ret = lttng_ust_ctl_get_timestamp_end(
6f9449c2
JG
2859 stream->ustream, &subbuf->info.data.timestamp_end);
2860 if (ret < 0) {
2861 PERROR("Failed to get sub-buffer end timestamp");
2862 goto end;
2863 }
2864
b623cb6a 2865 ret = lttng_ust_ctl_get_events_discarded(
6f9449c2
JG
2866 stream->ustream, &subbuf->info.data.events_discarded);
2867 if (ret) {
2868 PERROR("Failed to get sub-buffer events discarded count");
2869 goto end;
2870 }
2871
b623cb6a 2872 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
6f9449c2
JG
2873 &subbuf->info.data.sequence_number.value);
2874 if (ret) {
2875 /* May not be supported by older LTTng-modules. */
2876 if (ret != -ENOTTY) {
2877 PERROR("Failed to get sub-buffer sequence number");
2878 goto end;
fb83fe64 2879 }
1c20f0e2 2880 } else {
6f9449c2 2881 subbuf->info.data.sequence_number.is_set = true;
309167d2
JD
2882 }
2883
b623cb6a 2884 ret = lttng_ust_ctl_get_stream_id(
6f9449c2
JG
2885 stream->ustream, &subbuf->info.data.stream_id);
2886 if (ret < 0) {
2887 PERROR("Failed to get stream id");
2888 goto end;
2889 }
1d4dfdef 2890
b623cb6a 2891 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
6f9449c2
JG
2892 &subbuf->info.data.stream_instance_id.value);
2893 if (ret) {
2894 /* May not be supported by older LTTng-modules. */
2895 if (ret != -ENOTTY) {
2896 PERROR("Failed to get stream instance id");
2897 goto end;
2898 }
2899 } else {
2900 subbuf->info.data.stream_instance_id.is_set = true;
2901 }
2902end:
2903 return ret;
2904}
1d4dfdef 2905
6f9449c2
JG
2906static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2907 struct stream_subbuffer *subbuffer)
2908{
2909 int ret;
2910 const char *addr;
1d4dfdef 2911
6f9449c2
JG
2912 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2913 stream, subbuffer);
2914 if (ret) {
2915 goto end;
2916 }
02d02e31 2917
6f9449c2 2918 ret = get_current_subbuf_addr(stream, &addr);
128708c3 2919 if (ret) {
6f9449c2 2920 goto end;
128708c3
JG
2921 }
2922
6f9449c2
JG
2923 subbuffer->buffer.buffer = lttng_buffer_view_init(
2924 addr, 0, subbuffer->info.data.padded_subbuf_size);
2925 assert(subbuffer->buffer.buffer.data != NULL);
2926end:
2927 return ret;
2928}
fd424d99 2929
823a3926
JG
2930static enum get_next_subbuffer_status get_next_subbuffer(
2931 struct lttng_consumer_stream *stream,
6f9449c2
JG
2932 struct stream_subbuffer *subbuffer)
2933{
2934 int ret;
823a3926 2935 enum get_next_subbuffer_status status;
331744e3 2936
b623cb6a 2937 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
823a3926
JG
2938 switch (ret) {
2939 case 0:
2940 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2941 break;
2942 case -ENODATA:
2943 case -EAGAIN:
2944 /*
2945 * The caller only expects -ENODATA when there is no data to
2946 * read, but the kernel tracer returns -EAGAIN when there is
2947 * currently no data for a non-finalized stream, and -ENODATA
2948 * when there is no data for a finalized stream. Those can be
2949 * combined into a -ENODATA return value.
2950 */
2951 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2952 goto end;
2953 default:
2954 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
6f9449c2 2955 goto end;
02b3d176
DG
2956 }
2957
6f9449c2
JG
2958 ret = get_next_subbuffer_common(stream, subbuffer);
2959 if (ret) {
823a3926 2960 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
23d56598 2961 goto end;
1c20f0e2 2962 }
6f9449c2 2963end:
823a3926 2964 return status;
6f9449c2 2965}
1c20f0e2 2966
823a3926
JG
2967static enum get_next_subbuffer_status get_next_subbuffer_metadata(
2968 struct lttng_consumer_stream *stream,
6f9449c2
JG
2969 struct stream_subbuffer *subbuffer)
2970{
2971 int ret;
f5ba75b4
JG
2972 bool cache_empty;
2973 bool got_subbuffer;
2974 bool coherent;
2975 bool buffer_empty;
2976 unsigned long consumed_pos, produced_pos;
823a3926 2977 enum get_next_subbuffer_status status;
6f9449c2 2978
f5ba75b4 2979 do {
b623cb6a 2980 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
f5ba75b4
JG
2981 if (ret == 0) {
2982 got_subbuffer = true;
2983 } else {
2984 got_subbuffer = false;
2985 if (ret != -EAGAIN) {
2986 /* Fatal error. */
823a3926 2987 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
2988 goto end;
2989 }
c585821b
MD
2990 }
2991
f5ba75b4
JG
2992 /*
2993 * Determine if the cache is empty and ensure that a sub-buffer
2994 * is made available if the cache is not empty.
2995 */
2996 if (!got_subbuffer) {
2997 ret = commit_one_metadata_packet(stream);
2998 if (ret < 0 && ret != -ENOBUFS) {
823a3926 2999 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
3000 goto end;
3001 } else if (ret == 0) {
3002 /* Not an error, the cache is empty. */
3003 cache_empty = true;
823a3926 3004 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
f5ba75b4
JG
3005 goto end;
3006 } else {
3007 cache_empty = false;
3008 }
3009 } else {
3010 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828
JG
3011 cache_empty = stream->chan->metadata_cache->contents.size ==
3012 stream->ust_metadata_pushed;
f5ba75b4 3013 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
94d49140 3014 }
f5ba75b4 3015 } while (!got_subbuffer);
94d49140 3016
f5ba75b4 3017 /* Populate sub-buffer infos and view. */
6f9449c2
JG
3018 ret = get_next_subbuffer_common(stream, subbuffer);
3019 if (ret) {
823a3926 3020 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
6f9449c2 3021 goto end;
309167d2 3022 }
f5ba75b4
JG
3023
3024 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
3025 if (ret < 0) {
3026 /*
3027 * -EAGAIN is not expected since we got a sub-buffer and haven't
3028 * pushed the consumption position yet (on put_next).
3029 */
3030 PERROR("Failed to take a snapshot of metadata buffer positions");
823a3926 3031 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
3032 goto end;
3033 }
3034
3035 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
3036 if (ret) {
3037 PERROR("Failed to get metadata consumed position");
823a3926 3038 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
3039 goto end;
3040 }
3041
3042 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
3043 if (ret) {
3044 PERROR("Failed to get metadata produced position");
823a3926 3045 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
3046 goto end;
3047 }
3048
3049 /* Last sub-buffer of the ring buffer ? */
3050 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
3051
3052 /*
3053 * The sessiond registry lock ensures that coherent units of metadata
3054 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3055 * acquired, the cache is empty, and it is the only available sub-buffer
3056 * available, it is safe to assume that it is "coherent".
3057 */
3058 coherent = got_subbuffer && cache_empty && buffer_empty;
3059
3060 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
823a3926 3061 status = GET_NEXT_SUBBUFFER_STATUS_OK;
23d56598 3062end:
823a3926 3063 return status;
d41f73b7
MD
3064}
3065
6f9449c2
JG
3066static int put_next_subbuffer(struct lttng_consumer_stream *stream,
3067 struct stream_subbuffer *subbuffer)
3068{
b623cb6a 3069 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
6f9449c2
JG
3070
3071 assert(ret == 0);
3072 return ret;
3073}
3074
3075static int signal_metadata(struct lttng_consumer_stream *stream,
3076 struct lttng_consumer_local_data *ctx)
3077{
8db3acaf 3078 ASSERT_LOCKED(stream->metadata_rdv_lock);
6f9449c2
JG
3079 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3080}
3081
f5ba75b4 3082static int lttng_ustconsumer_set_stream_ops(
6f9449c2
JG
3083 struct lttng_consumer_stream *stream)
3084{
f5ba75b4
JG
3085 int ret = 0;
3086
6f9449c2
JG
3087 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3088 if (stream->metadata_flag) {
3089 stream->read_subbuffer_ops.get_next_subbuffer =
3090 get_next_subbuffer_metadata;
3091 stream->read_subbuffer_ops.extract_subbuffer_info =
3092 extract_metadata_subbuffer_info;
3093 stream->read_subbuffer_ops.reset_metadata =
55954e07 3094 metadata_stream_reset_cache_consumed_position;
f5ba75b4
JG
3095 if (stream->chan->is_live) {
3096 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3097 ret = consumer_stream_enable_metadata_bucketization(
3098 stream);
3099 if (ret) {
3100 goto end;
3101 }
3102 }
6f9449c2
JG
3103 } else {
3104 stream->read_subbuffer_ops.get_next_subbuffer =
3105 get_next_subbuffer;
3106 stream->read_subbuffer_ops.extract_subbuffer_info =
3107 extract_data_subbuffer_info;
3108 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3109 if (stream->chan->is_live) {
3110 stream->read_subbuffer_ops.send_live_beacon =
3111 consumer_flush_ust_index;
3112 }
3113 }
3114
3115 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
f5ba75b4
JG
3116end:
3117 return ret;
6f9449c2
JG
3118}
3119
ffe60014
DG
3120/*
3121 * Called when a stream is created.
fe4477ee
JD
3122 *
3123 * Return 0 on success or else a negative value.
ffe60014 3124 */
d41f73b7
MD
3125int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3126{
fe4477ee
JD
3127 int ret;
3128
10a50311
JD
3129 assert(stream);
3130
d2956687
JG
3131 /*
3132 * Don't create anything if this is set for streaming or if there is
3133 * no current trace chunk on the parent channel.
3134 */
3135 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3136 stream->chan->trace_chunk) {
3137 ret = consumer_stream_create_output_files(stream, true);
3138 if (ret) {
fe4477ee
JD
3139 goto error;
3140 }
fe4477ee 3141 }
6f9449c2
JG
3142
3143 lttng_ustconsumer_set_stream_ops(stream);
fe4477ee
JD
3144 ret = 0;
3145
3146error:
3147 return ret;
d41f73b7 3148}
ca22feea
DG
3149
3150/*
3151 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
3152 * stream. Consumer data lock MUST be acquired before calling this function
3153 * and the stream lock.
ca22feea 3154 *
6d805429 3155 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
3156 * data is available for trace viewer reading.
3157 */
6d805429 3158int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
3159{
3160 int ret;
3161
3162 assert(stream);
ffe60014 3163 assert(stream->ustream);
b1316da1 3164 ASSERT_LOCKED(stream->lock);
ca22feea 3165
6d805429 3166 DBG("UST consumer checking data pending");
c8f59ee5 3167
ca6b395f
MD
3168 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3169 ret = 0;
3170 goto end;
3171 }
3172
04ef1097 3173 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
e6ee4eab
DG
3174 uint64_t contiguous, pushed;
3175
3176 /* Ease our life a bit. */
934ba8fd 3177 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828 3178 contiguous = stream->chan->metadata_cache->contents.size;
934ba8fd 3179 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
e6ee4eab
DG
3180 pushed = stream->ust_metadata_pushed;
3181
04ef1097
MD
3182 /*
3183 * We can simply check whether all contiguously available data
3184 * has been pushed to the ring buffer, since the push operation
3185 * is performed within get_next_subbuf(), and because both
3186 * get_next_subbuf() and put_next_subbuf() are issued atomically
3187 * thanks to the stream lock within
3188 * lttng_ustconsumer_read_subbuffer(). This basically means that
3189 * whetnever ust_metadata_pushed is incremented, the associated
3190 * metadata has been consumed from the metadata stream.
3191 */
3192 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
e6ee4eab 3193 contiguous, pushed);
aa01b94c 3194 assert(((int64_t) (contiguous - pushed)) >= 0);
e6ee4eab 3195 if ((contiguous != pushed) ||
6acdf328 3196 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
04ef1097
MD
3197 ret = 1; /* Data is pending */
3198 goto end;
3199 }
3200 } else {
b623cb6a 3201 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
04ef1097
MD
3202 if (ret == 0) {
3203 /*
3204 * There is still data so let's put back this
3205 * subbuffer.
3206 */
b623cb6a 3207 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
04ef1097
MD
3208 assert(ret == 0);
3209 ret = 1; /* Data is pending */
3210 goto end;
3211 }
ca22feea
DG
3212 }
3213
6d805429
DG
3214 /* Data is NOT pending so ready to be read. */
3215 ret = 0;
ca22feea 3216
6efae65e
DG
3217end:
3218 return ret;
ca22feea 3219}
d88aee68 3220
6d574024
DG
3221/*
3222 * Stop a given metadata channel timer if enabled and close the wait fd which
3223 * is the poll pipe of the metadata stream.
3224 *
d2c82a5a 3225 * This MUST be called with the metadata channel lock acquired.
6d574024
DG
3226 */
3227void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3228{
3229 int ret;
3230
3231 assert(metadata);
3232 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3233
3234 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3235
3236 if (metadata->switch_timer_enabled == 1) {
3237 consumer_timer_switch_stop(metadata);
3238 }
3239
3240 if (!metadata->metadata_stream) {
3241 goto end;
3242 }
3243
3244 /*
3245 * Closing write side so the thread monitoring the stream wakes up if any
3246 * and clean the metadata stream.
3247 */
3248 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3249 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3250 if (ret < 0) {
3251 PERROR("closing metadata pipe write side");
3252 }
3253 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3254 }
3255
3256end:
3257 return;
3258}
3259
d88aee68
DG
3260/*
3261 * Close every metadata stream wait fd of the metadata hash table. This
3262 * function MUST be used very carefully so not to run into a race between the
3263 * metadata thread handling streams and this function closing their wait fd.
3264 *
3265 * For UST, this is used when the session daemon hangs up. Its the metadata
3266 * producer so calling this is safe because we are assured that no state change
3267 * can occur in the metadata thread for the streams in the hash table.
3268 */
6d574024 3269void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
d88aee68 3270{
d88aee68
DG
3271 struct lttng_ht_iter iter;
3272 struct lttng_consumer_stream *stream;
3273
3274 assert(metadata_ht);
3275 assert(metadata_ht->ht);
3276
3277 DBG("UST consumer closing all metadata streams");
3278
3279 rcu_read_lock();
3280 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3281 node.node) {
9ce5646a
MD
3282
3283 health_code_update();
3284
be2b50c7 3285 pthread_mutex_lock(&stream->chan->lock);
6d574024 3286 lttng_ustconsumer_close_metadata(stream->chan);
be2b50c7
DG
3287 pthread_mutex_unlock(&stream->chan->lock);
3288
d88aee68
DG
3289 }
3290 rcu_read_unlock();
3291}
d8ef542d
MD
3292
3293void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3294{
3295 int ret;
3296
b623cb6a 3297 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
d8ef542d
MD
3298 if (ret < 0) {
3299 ERR("Unable to close wakeup fd");
3300 }
3301}
331744e3 3302
f666ae70
MD
3303/*
3304 * Please refer to consumer-timer.c before adding any lock within this
3305 * function or any of its callees. Timers have a very strict locking
3306 * semantic with respect to teardown. Failure to respect this semantic
3307 * introduces deadlocks.
c585821b
MD
3308 *
3309 * DON'T hold the metadata lock when calling this function, else this
3310 * can cause deadlock involving consumer awaiting for metadata to be
3311 * pushed out due to concurrent interaction with the session daemon.
f666ae70 3312 */
331744e3 3313int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
95671f53 3314 struct lttng_consumer_channel *channel, bool invoked_by_timer, int wait)
331744e3
JD
3315{
3316 struct lttcomm_metadata_request_msg request;
3317 struct lttcomm_consumer_msg msg;
0c759fc9 3318 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
93ec662e 3319 uint64_t len, key, offset, version;
331744e3
JD
3320 int ret;
3321
3322 assert(channel);
3323 assert(channel->metadata_cache);
3324
53efb85a
MD
3325 memset(&request, 0, sizeof(request));
3326
331744e3 3327 /* send the metadata request to sessiond */
fa29bfbf 3328 switch (the_consumer_data.type) {
331744e3
JD
3329 case LTTNG_CONSUMER64_UST:
3330 request.bits_per_long = 64;
3331 break;
3332 case LTTNG_CONSUMER32_UST:
3333 request.bits_per_long = 32;
3334 break;
3335 default:
3336 request.bits_per_long = 0;
3337 break;
3338 }
3339
3340 request.session_id = channel->session_id;
1950109e 3341 request.session_id_per_pid = channel->session_id_per_pid;
567eb353
DG
3342 /*
3343 * Request the application UID here so the metadata of that application can
3344 * be sent back. The channel UID corresponds to the user UID of the session
3345 * used for the rights on the stream file(s).
3346 */
3347 request.uid = channel->ust_app_uid;
331744e3 3348 request.key = channel->key;
567eb353 3349
1950109e 3350 DBG("Sending metadata request to sessiond, session id %" PRIu64
3e25d926 3351 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
567eb353
DG
3352 request.session_id, request.session_id_per_pid, request.uid,
3353 request.key);
331744e3 3354
75d83e50 3355 pthread_mutex_lock(&ctx->metadata_socket_lock);
9ce5646a
MD
3356
3357 health_code_update();
3358
331744e3
JD
3359 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3360 sizeof(request));
3361 if (ret < 0) {
3362 ERR("Asking metadata to sessiond");
3363 goto end;
3364 }
3365
9ce5646a
MD
3366 health_code_update();
3367
331744e3
JD
3368 /* Receive the metadata from sessiond */
3369 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3370 sizeof(msg));
3371 if (ret != sizeof(msg)) {
8fd623e0 3372 DBG("Consumer received unexpected message size %d (expects %zu)",
331744e3
JD
3373 ret, sizeof(msg));
3374 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3375 /*
3376 * The ret value might 0 meaning an orderly shutdown but this is ok
3377 * since the caller handles this.
3378 */
3379 goto end;
3380 }
3381
9ce5646a
MD
3382 health_code_update();
3383
331744e3
JD
3384 if (msg.cmd_type == LTTNG_ERR_UND) {
3385 /* No registry found */
3386 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3387 ret_code);
3388 ret = 0;
3389 goto end;
3390 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3391 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3392 ret = -1;
3393 goto end;
3394 }
3395
3396 len = msg.u.push_metadata.len;
3397 key = msg.u.push_metadata.key;
3398 offset = msg.u.push_metadata.target_offset;
93ec662e 3399 version = msg.u.push_metadata.version;
331744e3
JD
3400
3401 assert(key == channel->key);
3402 if (len == 0) {
3403 DBG("No new metadata to receive for key %" PRIu64, key);
3404 }
3405
9ce5646a
MD
3406 health_code_update();
3407
331744e3
JD
3408 /* Tell session daemon we are ready to receive the metadata. */
3409 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
0c759fc9 3410 LTTCOMM_CONSUMERD_SUCCESS);
331744e3
JD
3411 if (ret < 0 || len == 0) {
3412 /*
3413 * Somehow, the session daemon is not responding anymore or there is
3414 * nothing to receive.
3415 */
3416 goto end;
3417 }
3418
9ce5646a
MD
3419 health_code_update();
3420
1eb682be 3421 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
95671f53 3422 key, offset, len, version, channel, invoked_by_timer, wait);
1eb682be 3423 if (ret >= 0) {
f2a444f1
DG
3424 /*
3425 * Only send the status msg if the sessiond is alive meaning a positive
3426 * ret code.
3427 */
1eb682be 3428 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
f2a444f1 3429 }
331744e3
JD
3430 ret = 0;
3431
3432end:
9ce5646a
MD
3433 health_code_update();
3434
75d83e50 3435 pthread_mutex_unlock(&ctx->metadata_socket_lock);
331744e3
JD
3436 return ret;
3437}
70190e1c
DG
3438
3439/*
3440 * Return the ustctl call for the get stream id.
3441 */
3442int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3443 uint64_t *stream_id)
3444{
3445 assert(stream);
3446 assert(stream_id);
3447
b623cb6a 3448 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
70190e1c 3449}
d85707b0
MD
3450
3451void lttng_ustconsumer_sigbus_handle(void *addr)
3452{
3453 lttng_ust_ctl_sigbus_handle(addr);
3454}
This page took 0.293523 seconds and 4 git commands to generate.