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