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