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