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