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