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