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