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