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