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