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