Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <assert.h> | |
f02e1e8a | 21 | #include <lttng/ust-ctl.h> |
3bd1e081 MD |
22 | #include <poll.h> |
23 | #include <pthread.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/socket.h> | |
dbb5dfe6 | 28 | #include <sys/stat.h> |
3bd1e081 | 29 | #include <sys/types.h> |
77c7c900 | 30 | #include <inttypes.h> |
3bd1e081 | 31 | #include <unistd.h> |
ffe60014 | 32 | #include <urcu/list.h> |
331744e3 | 33 | #include <signal.h> |
0857097f | 34 | |
990570ed | 35 | #include <common/common.h> |
10a8a223 | 36 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 37 | #include <common/relayd/relayd.h> |
dbb5dfe6 | 38 | #include <common/compat/fcntl.h> |
331744e3 | 39 | #include <common/consumer-metadata-cache.h> |
10a50311 | 40 | #include <common/consumer-stream.h> |
331744e3 | 41 | #include <common/consumer-timer.h> |
fe4477ee | 42 | #include <common/utils.h> |
309167d2 | 43 | #include <common/index/index.h> |
10a8a223 DG |
44 | |
45 | #include "ust-consumer.h" | |
3bd1e081 MD |
46 | |
47 | extern struct lttng_consumer_global_data consumer_data; | |
48 | extern int consumer_poll_timeout; | |
49 | extern volatile int consumer_quit; | |
50 | ||
51 | /* | |
ffe60014 DG |
52 | * Free channel object and all streams associated with it. This MUST be used |
53 | * only and only if the channel has _NEVER_ been added to the global channel | |
54 | * hash table. | |
3bd1e081 | 55 | */ |
ffe60014 | 56 | static void destroy_channel(struct lttng_consumer_channel *channel) |
3bd1e081 | 57 | { |
ffe60014 DG |
58 | struct lttng_consumer_stream *stream, *stmp; |
59 | ||
60 | assert(channel); | |
61 | ||
62 | DBG("UST consumer cleaning stream list"); | |
63 | ||
64 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
65 | send_node) { | |
66 | cds_list_del(&stream->send_node); | |
67 | ustctl_destroy_stream(stream->ustream); | |
68 | free(stream); | |
69 | } | |
70 | ||
71 | /* | |
72 | * If a channel is available meaning that was created before the streams | |
73 | * were, delete it. | |
74 | */ | |
75 | if (channel->uchan) { | |
76 | lttng_ustconsumer_del_channel(channel); | |
77 | } | |
78 | free(channel); | |
79 | } | |
3bd1e081 MD |
80 | |
81 | /* | |
ffe60014 | 82 | * Add channel to internal consumer state. |
3bd1e081 | 83 | * |
ffe60014 | 84 | * Returns 0 on success or else a negative value. |
3bd1e081 | 85 | */ |
ffe60014 DG |
86 | static int add_channel(struct lttng_consumer_channel *channel, |
87 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 MD |
88 | { |
89 | int ret = 0; | |
90 | ||
ffe60014 DG |
91 | assert(channel); |
92 | assert(ctx); | |
93 | ||
94 | if (ctx->on_recv_channel != NULL) { | |
95 | ret = ctx->on_recv_channel(channel); | |
96 | if (ret == 0) { | |
d8ef542d | 97 | ret = consumer_add_channel(channel, ctx); |
ffe60014 DG |
98 | } else if (ret < 0) { |
99 | /* Most likely an ENOMEM. */ | |
100 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
101 | goto error; | |
102 | } | |
103 | } else { | |
d8ef542d | 104 | ret = consumer_add_channel(channel, ctx); |
3bd1e081 MD |
105 | } |
106 | ||
d88aee68 | 107 | DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key); |
ffe60014 DG |
108 | |
109 | error: | |
3bd1e081 MD |
110 | return ret; |
111 | } | |
112 | ||
113 | /* | |
ffe60014 DG |
114 | * Allocate and return a consumer channel object. |
115 | */ | |
116 | static struct lttng_consumer_channel *allocate_channel(uint64_t session_id, | |
117 | const char *pathname, const char *name, uid_t uid, gid_t gid, | |
da009f2c | 118 | uint64_t relayd_id, uint64_t key, enum lttng_event_output output, |
2bba9e53 | 119 | uint64_t tracefile_size, uint64_t tracefile_count, |
1950109e | 120 | uint64_t session_id_per_pid, unsigned int monitor) |
ffe60014 DG |
121 | { |
122 | assert(pathname); | |
123 | assert(name); | |
124 | ||
1950109e JD |
125 | return consumer_allocate_channel(key, session_id, pathname, name, uid, |
126 | gid, relayd_id, output, tracefile_size, | |
127 | tracefile_count, session_id_per_pid, monitor); | |
ffe60014 DG |
128 | } |
129 | ||
130 | /* | |
131 | * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the | |
132 | * error value if applicable is set in it else it is kept untouched. | |
3bd1e081 | 133 | * |
ffe60014 | 134 | * Return NULL on error else the newly allocated stream object. |
3bd1e081 | 135 | */ |
ffe60014 DG |
136 | static struct lttng_consumer_stream *allocate_stream(int cpu, int key, |
137 | struct lttng_consumer_channel *channel, | |
138 | struct lttng_consumer_local_data *ctx, int *_alloc_ret) | |
139 | { | |
140 | int alloc_ret; | |
141 | struct lttng_consumer_stream *stream = NULL; | |
142 | ||
143 | assert(channel); | |
144 | assert(ctx); | |
145 | ||
146 | stream = consumer_allocate_stream(channel->key, | |
147 | key, | |
148 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
149 | channel->name, | |
150 | channel->uid, | |
151 | channel->gid, | |
152 | channel->relayd_id, | |
153 | channel->session_id, | |
154 | cpu, | |
155 | &alloc_ret, | |
4891ece8 DG |
156 | channel->type, |
157 | channel->monitor); | |
ffe60014 DG |
158 | if (stream == NULL) { |
159 | switch (alloc_ret) { | |
160 | case -ENOENT: | |
161 | /* | |
162 | * We could not find the channel. Can happen if cpu hotplug | |
163 | * happens while tearing down. | |
164 | */ | |
165 | DBG3("Could not find channel"); | |
166 | break; | |
167 | case -ENOMEM: | |
168 | case -EINVAL: | |
169 | default: | |
170 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
171 | break; | |
172 | } | |
173 | goto error; | |
174 | } | |
175 | ||
176 | stream->chan = channel; | |
177 | ||
178 | error: | |
179 | if (_alloc_ret) { | |
180 | *_alloc_ret = alloc_ret; | |
181 | } | |
182 | return stream; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Send the given stream pointer to the corresponding thread. | |
187 | * | |
188 | * Returns 0 on success else a negative value. | |
189 | */ | |
190 | static int send_stream_to_thread(struct lttng_consumer_stream *stream, | |
191 | struct lttng_consumer_local_data *ctx) | |
192 | { | |
dae10966 DG |
193 | int ret; |
194 | struct lttng_pipe *stream_pipe; | |
ffe60014 DG |
195 | |
196 | /* Get the right pipe where the stream will be sent. */ | |
197 | if (stream->metadata_flag) { | |
5ab66908 MD |
198 | ret = consumer_add_metadata_stream(stream); |
199 | if (ret) { | |
200 | ERR("Consumer add metadata stream %" PRIu64 " failed.", | |
201 | stream->key); | |
202 | goto error; | |
203 | } | |
dae10966 | 204 | stream_pipe = ctx->consumer_metadata_pipe; |
ffe60014 | 205 | } else { |
5ab66908 MD |
206 | ret = consumer_add_data_stream(stream); |
207 | if (ret) { | |
208 | ERR("Consumer add stream %" PRIu64 " failed.", | |
209 | stream->key); | |
210 | goto error; | |
211 | } | |
dae10966 | 212 | stream_pipe = ctx->consumer_data_pipe; |
ffe60014 DG |
213 | } |
214 | ||
5ab66908 MD |
215 | /* |
216 | * From this point on, the stream's ownership has been moved away from | |
217 | * the channel and becomes globally visible. | |
218 | */ | |
219 | stream->globally_visible = 1; | |
220 | ||
dae10966 | 221 | ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream)); |
ffe60014 | 222 | if (ret < 0) { |
dae10966 DG |
223 | ERR("Consumer write %s stream to pipe %d", |
224 | stream->metadata_flag ? "metadata" : "data", | |
225 | lttng_pipe_get_writefd(stream_pipe)); | |
5ab66908 MD |
226 | if (stream->metadata_flag) { |
227 | consumer_del_stream_for_metadata(stream); | |
228 | } else { | |
229 | consumer_del_stream_for_data(stream); | |
230 | } | |
ffe60014 | 231 | } |
5ab66908 | 232 | error: |
ffe60014 DG |
233 | return ret; |
234 | } | |
235 | ||
d88aee68 DG |
236 | /* |
237 | * Create streams for the given channel using liblttng-ust-ctl. | |
238 | * | |
239 | * Return 0 on success else a negative value. | |
240 | */ | |
ffe60014 DG |
241 | static int create_ust_streams(struct lttng_consumer_channel *channel, |
242 | struct lttng_consumer_local_data *ctx) | |
243 | { | |
244 | int ret, cpu = 0; | |
245 | struct ustctl_consumer_stream *ustream; | |
246 | struct lttng_consumer_stream *stream; | |
247 | ||
248 | assert(channel); | |
249 | assert(ctx); | |
250 | ||
251 | /* | |
252 | * While a stream is available from ustctl. When NULL is returned, we've | |
253 | * reached the end of the possible stream for the channel. | |
254 | */ | |
255 | while ((ustream = ustctl_create_stream(channel->uchan, cpu))) { | |
256 | int wait_fd; | |
04ef1097 | 257 | int ust_metadata_pipe[2]; |
ffe60014 | 258 | |
04ef1097 MD |
259 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) { |
260 | ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe); | |
261 | if (ret < 0) { | |
262 | ERR("Create ust metadata poll pipe"); | |
263 | goto error; | |
264 | } | |
265 | wait_fd = ust_metadata_pipe[0]; | |
266 | } else { | |
267 | wait_fd = ustctl_stream_get_wait_fd(ustream); | |
268 | } | |
ffe60014 DG |
269 | |
270 | /* Allocate consumer stream object. */ | |
271 | stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret); | |
272 | if (!stream) { | |
273 | goto error_alloc; | |
274 | } | |
275 | stream->ustream = ustream; | |
276 | /* | |
277 | * Store it so we can save multiple function calls afterwards since | |
278 | * this value is used heavily in the stream threads. This is UST | |
279 | * specific so this is why it's done after allocation. | |
280 | */ | |
281 | stream->wait_fd = wait_fd; | |
282 | ||
b31398bb DG |
283 | /* |
284 | * Increment channel refcount since the channel reference has now been | |
285 | * assigned in the allocation process above. | |
286 | */ | |
10a50311 JD |
287 | if (stream->chan->monitor) { |
288 | uatomic_inc(&stream->chan->refcount); | |
289 | } | |
b31398bb | 290 | |
ffe60014 DG |
291 | /* |
292 | * Order is important this is why a list is used. On error, the caller | |
293 | * should clean this list. | |
294 | */ | |
295 | cds_list_add_tail(&stream->send_node, &channel->streams.head); | |
296 | ||
297 | ret = ustctl_get_max_subbuf_size(stream->ustream, | |
298 | &stream->max_sb_size); | |
299 | if (ret < 0) { | |
300 | ERR("ustctl_get_max_subbuf_size failed for stream %s", | |
301 | stream->name); | |
302 | goto error; | |
303 | } | |
304 | ||
305 | /* Do actions once stream has been received. */ | |
306 | if (ctx->on_recv_stream) { | |
307 | ret = ctx->on_recv_stream(stream); | |
308 | if (ret < 0) { | |
309 | goto error; | |
310 | } | |
311 | } | |
312 | ||
d88aee68 | 313 | DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64, |
ffe60014 DG |
314 | stream->name, stream->key, stream->relayd_stream_id); |
315 | ||
316 | /* Set next CPU stream. */ | |
317 | channel->streams.count = ++cpu; | |
d88aee68 DG |
318 | |
319 | /* Keep stream reference when creating metadata. */ | |
320 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
321 | channel->metadata_stream = stream; | |
04ef1097 MD |
322 | stream->ust_metadata_poll_pipe[0] = ust_metadata_pipe[0]; |
323 | stream->ust_metadata_poll_pipe[1] = ust_metadata_pipe[1]; | |
d88aee68 | 324 | } |
ffe60014 DG |
325 | } |
326 | ||
327 | return 0; | |
328 | ||
329 | error: | |
330 | error_alloc: | |
331 | return ret; | |
332 | } | |
333 | ||
334 | /* | |
335 | * Create an UST channel with the given attributes and send it to the session | |
336 | * daemon using the ust ctl API. | |
337 | * | |
338 | * Return 0 on success or else a negative value. | |
339 | */ | |
340 | static int create_ust_channel(struct ustctl_consumer_channel_attr *attr, | |
341 | struct ustctl_consumer_channel **chanp) | |
342 | { | |
343 | int ret; | |
344 | struct ustctl_consumer_channel *channel; | |
345 | ||
346 | assert(attr); | |
347 | assert(chanp); | |
348 | ||
349 | DBG3("Creating channel to ustctl with attr: [overwrite: %d, " | |
350 | "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", " | |
351 | "switch_timer_interval: %u, read_timer_interval: %u, " | |
352 | "output: %d, type: %d", attr->overwrite, attr->subbuf_size, | |
353 | attr->num_subbuf, attr->switch_timer_interval, | |
354 | attr->read_timer_interval, attr->output, attr->type); | |
355 | ||
356 | channel = ustctl_create_channel(attr); | |
357 | if (!channel) { | |
358 | ret = -1; | |
359 | goto error_create; | |
360 | } | |
361 | ||
362 | *chanp = channel; | |
363 | ||
364 | return 0; | |
365 | ||
366 | error_create: | |
367 | return ret; | |
368 | } | |
369 | ||
d88aee68 DG |
370 | /* |
371 | * Send a single given stream to the session daemon using the sock. | |
372 | * | |
373 | * Return 0 on success else a negative value. | |
374 | */ | |
ffe60014 DG |
375 | static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream) |
376 | { | |
377 | int ret; | |
378 | ||
379 | assert(stream); | |
380 | assert(sock >= 0); | |
381 | ||
3eb914c0 | 382 | DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key); |
ffe60014 DG |
383 | |
384 | /* Send stream to session daemon. */ | |
385 | ret = ustctl_send_stream_to_sessiond(sock, stream->ustream); | |
386 | if (ret < 0) { | |
387 | goto error; | |
388 | } | |
389 | ||
ffe60014 DG |
390 | error: |
391 | return ret; | |
392 | } | |
393 | ||
394 | /* | |
395 | * Send channel to sessiond. | |
396 | * | |
d88aee68 | 397 | * Return 0 on success or else a negative value. |
ffe60014 DG |
398 | */ |
399 | static int send_sessiond_channel(int sock, | |
400 | struct lttng_consumer_channel *channel, | |
401 | struct lttng_consumer_local_data *ctx, int *relayd_error) | |
402 | { | |
f2a444f1 | 403 | int ret, ret_code = LTTNG_OK; |
ffe60014 DG |
404 | struct lttng_consumer_stream *stream; |
405 | ||
406 | assert(channel); | |
407 | assert(ctx); | |
408 | assert(sock >= 0); | |
409 | ||
410 | DBG("UST consumer sending channel %s to sessiond", channel->name); | |
411 | ||
62285ea4 DG |
412 | if (channel->relayd_id != (uint64_t) -1ULL) { |
413 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
414 | /* Try to send the stream to the relayd if one is available. */ | |
415 | ret = consumer_send_relayd_stream(stream, stream->chan->pathname); | |
416 | if (ret < 0) { | |
417 | /* | |
418 | * Flag that the relayd was the problem here probably due to a | |
419 | * communicaton error on the socket. | |
420 | */ | |
421 | if (relayd_error) { | |
422 | *relayd_error = 1; | |
423 | } | |
424 | ret_code = LTTNG_ERR_RELAYD_CONNECT_FAIL; | |
ffe60014 | 425 | } |
ffe60014 | 426 | } |
f2a444f1 | 427 | } |
ffe60014 | 428 | |
f2a444f1 DG |
429 | /* Inform sessiond that we are about to send channel and streams. */ |
430 | ret = consumer_send_status_msg(sock, ret_code); | |
431 | if (ret < 0 || ret_code != LTTNG_OK) { | |
432 | /* | |
433 | * Either the session daemon is not responding or the relayd died so we | |
434 | * stop now. | |
435 | */ | |
436 | goto error; | |
437 | } | |
438 | ||
439 | /* Send channel to sessiond. */ | |
440 | ret = ustctl_send_channel_to_sessiond(sock, channel->uchan); | |
441 | if (ret < 0) { | |
442 | goto error; | |
443 | } | |
444 | ||
445 | ret = ustctl_channel_close_wakeup_fd(channel->uchan); | |
446 | if (ret < 0) { | |
447 | goto error; | |
448 | } | |
449 | ||
450 | /* The channel was sent successfully to the sessiond at this point. */ | |
451 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
ffe60014 DG |
452 | /* Send stream to session daemon. */ |
453 | ret = send_sessiond_stream(sock, stream); | |
454 | if (ret < 0) { | |
455 | goto error; | |
456 | } | |
457 | } | |
458 | ||
459 | /* Tell sessiond there is no more stream. */ | |
460 | ret = ustctl_send_stream_to_sessiond(sock, NULL); | |
461 | if (ret < 0) { | |
462 | goto error; | |
463 | } | |
464 | ||
465 | DBG("UST consumer NULL stream sent to sessiond"); | |
466 | ||
467 | return 0; | |
468 | ||
469 | error: | |
f2a444f1 DG |
470 | if (ret_code != LTTNG_OK) { |
471 | ret = -1; | |
472 | } | |
ffe60014 DG |
473 | return ret; |
474 | } | |
475 | ||
476 | /* | |
477 | * Creates a channel and streams and add the channel it to the channel internal | |
478 | * state. The created stream must ONLY be sent once the GET_CHANNEL command is | |
479 | * received. | |
480 | * | |
481 | * Return 0 on success or else, a negative value is returned and the channel | |
482 | * MUST be destroyed by consumer_del_channel(). | |
483 | */ | |
484 | static int ask_channel(struct lttng_consumer_local_data *ctx, int sock, | |
485 | struct lttng_consumer_channel *channel, | |
486 | struct ustctl_consumer_channel_attr *attr) | |
3bd1e081 MD |
487 | { |
488 | int ret; | |
489 | ||
ffe60014 DG |
490 | assert(ctx); |
491 | assert(channel); | |
492 | assert(attr); | |
493 | ||
494 | /* | |
495 | * This value is still used by the kernel consumer since for the kernel, | |
496 | * the stream ownership is not IN the consumer so we need to have the | |
497 | * number of left stream that needs to be initialized so we can know when | |
498 | * to delete the channel (see consumer.c). | |
499 | * | |
500 | * As for the user space tracer now, the consumer creates and sends the | |
501 | * stream to the session daemon which only sends them to the application | |
502 | * once every stream of a channel is received making this value useless | |
503 | * because we they will be added to the poll thread before the application | |
504 | * receives them. This ensures that a stream can not hang up during | |
505 | * initilization of a channel. | |
506 | */ | |
507 | channel->nb_init_stream_left = 0; | |
508 | ||
509 | /* The reply msg status is handled in the following call. */ | |
510 | ret = create_ust_channel(attr, &channel->uchan); | |
511 | if (ret < 0) { | |
10a50311 | 512 | goto end; |
3bd1e081 MD |
513 | } |
514 | ||
d8ef542d MD |
515 | channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan); |
516 | ||
10a50311 JD |
517 | /* |
518 | * For the snapshots (no monitor), we create the metadata streams | |
519 | * on demand, not during the channel creation. | |
520 | */ | |
521 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) { | |
522 | ret = 0; | |
523 | goto end; | |
524 | } | |
525 | ||
ffe60014 DG |
526 | /* Open all streams for this channel. */ |
527 | ret = create_ust_streams(channel, ctx); | |
528 | if (ret < 0) { | |
10a50311 | 529 | goto end; |
ffe60014 DG |
530 | } |
531 | ||
10a50311 | 532 | end: |
3bd1e081 MD |
533 | return ret; |
534 | } | |
535 | ||
d88aee68 DG |
536 | /* |
537 | * Send all stream of a channel to the right thread handling it. | |
538 | * | |
539 | * On error, return a negative value else 0 on success. | |
540 | */ | |
541 | static int send_streams_to_thread(struct lttng_consumer_channel *channel, | |
542 | struct lttng_consumer_local_data *ctx) | |
543 | { | |
544 | int ret = 0; | |
545 | struct lttng_consumer_stream *stream, *stmp; | |
546 | ||
547 | assert(channel); | |
548 | assert(ctx); | |
549 | ||
550 | /* Send streams to the corresponding thread. */ | |
551 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
552 | send_node) { | |
553 | /* Sending the stream to the thread. */ | |
554 | ret = send_stream_to_thread(stream, ctx); | |
555 | if (ret < 0) { | |
556 | /* | |
557 | * If we are unable to send the stream to the thread, there is | |
558 | * a big problem so just stop everything. | |
559 | */ | |
5ab66908 MD |
560 | /* Remove node from the channel stream list. */ |
561 | cds_list_del(&stream->send_node); | |
d88aee68 DG |
562 | goto error; |
563 | } | |
564 | ||
565 | /* Remove node from the channel stream list. */ | |
566 | cds_list_del(&stream->send_node); | |
4891ece8 | 567 | |
d88aee68 DG |
568 | } |
569 | ||
570 | error: | |
571 | return ret; | |
572 | } | |
573 | ||
7972aab2 DG |
574 | /* |
575 | * Flush channel's streams using the given key to retrieve the channel. | |
576 | * | |
577 | * Return 0 on success else an LTTng error code. | |
578 | */ | |
579 | static int flush_channel(uint64_t chan_key) | |
580 | { | |
581 | int ret = 0; | |
582 | struct lttng_consumer_channel *channel; | |
583 | struct lttng_consumer_stream *stream; | |
584 | struct lttng_ht *ht; | |
585 | struct lttng_ht_iter iter; | |
586 | ||
8fd623e0 | 587 | DBG("UST consumer flush channel key %" PRIu64, chan_key); |
7972aab2 | 588 | |
a500c257 | 589 | rcu_read_lock(); |
7972aab2 DG |
590 | channel = consumer_find_channel(chan_key); |
591 | if (!channel) { | |
8fd623e0 | 592 | ERR("UST consumer flush channel %" PRIu64 " not found", chan_key); |
7972aab2 DG |
593 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
594 | goto error; | |
595 | } | |
596 | ||
597 | ht = consumer_data.stream_per_chan_id_ht; | |
598 | ||
599 | /* For each stream of the channel id, flush it. */ | |
7972aab2 DG |
600 | cds_lfht_for_each_entry_duplicate(ht->ht, |
601 | ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct, | |
602 | &channel->key, &iter.iter, stream, node_channel_id.node) { | |
b8086166 | 603 | ustctl_flush_buffer(stream->ustream, 1); |
7972aab2 | 604 | } |
7972aab2 | 605 | error: |
a500c257 | 606 | rcu_read_unlock(); |
7972aab2 DG |
607 | return ret; |
608 | } | |
be2b50c7 DG |
609 | /* |
610 | * Close metadata stream wakeup_fd using the given key to retrieve the channel. | |
611 | * RCU read side lock MUST be acquired before calling this function. | |
612 | * | |
613 | * NOTE: This function does NOT take any channel nor stream lock. | |
614 | * | |
615 | * Return 0 on success else LTTng error code. | |
616 | */ | |
617 | static int _close_metadata(struct lttng_consumer_channel *channel) | |
618 | { | |
619 | int ret = LTTNG_OK; | |
620 | ||
621 | assert(channel); | |
622 | assert(channel->type == CONSUMER_CHANNEL_TYPE_METADATA); | |
623 | ||
624 | if (channel->switch_timer_enabled == 1) { | |
625 | DBG("Deleting timer on metadata channel"); | |
626 | consumer_timer_switch_stop(channel); | |
627 | } | |
628 | ||
629 | if (channel->metadata_stream) { | |
630 | ret = ustctl_stream_close_wakeup_fd(channel->metadata_stream->ustream); | |
631 | if (ret < 0) { | |
632 | ERR("UST consumer unable to close fd of metadata (ret: %d)", ret); | |
633 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
634 | } | |
635 | ||
636 | if (channel->monitor) { | |
637 | /* Close the read-side in consumer_del_metadata_stream */ | |
638 | ret = close(channel->metadata_stream->ust_metadata_poll_pipe[1]); | |
639 | if (ret < 0) { | |
640 | PERROR("Close UST metadata write-side poll pipe"); | |
641 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
642 | } | |
643 | } | |
644 | } | |
645 | ||
646 | return ret; | |
647 | } | |
7972aab2 | 648 | |
d88aee68 DG |
649 | /* |
650 | * Close metadata stream wakeup_fd using the given key to retrieve the channel. | |
a500c257 | 651 | * RCU read side lock MUST be acquired before calling this function. |
d88aee68 DG |
652 | * |
653 | * Return 0 on success else an LTTng error code. | |
654 | */ | |
655 | static int close_metadata(uint64_t chan_key) | |
656 | { | |
ea88ca2a | 657 | int ret = 0; |
d88aee68 DG |
658 | struct lttng_consumer_channel *channel; |
659 | ||
8fd623e0 | 660 | DBG("UST consumer close metadata key %" PRIu64, chan_key); |
d88aee68 DG |
661 | |
662 | channel = consumer_find_channel(chan_key); | |
663 | if (!channel) { | |
84cc9aa0 DG |
664 | /* |
665 | * This is possible if the metadata thread has issue a delete because | |
666 | * the endpoint point of the stream hung up. There is no way the | |
667 | * session daemon can know about it thus use a DBG instead of an actual | |
668 | * error. | |
669 | */ | |
670 | DBG("UST consumer close metadata %" PRIu64 " not found", chan_key); | |
d88aee68 DG |
671 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
672 | goto error; | |
673 | } | |
674 | ||
ea88ca2a | 675 | pthread_mutex_lock(&consumer_data.lock); |
a9838785 | 676 | pthread_mutex_lock(&channel->lock); |
73811ecc DG |
677 | |
678 | if (cds_lfht_is_node_deleted(&channel->node.node)) { | |
679 | goto error_unlock; | |
680 | } | |
681 | ||
be2b50c7 | 682 | ret = _close_metadata(channel); |
d88aee68 | 683 | |
ea88ca2a | 684 | error_unlock: |
a9838785 | 685 | pthread_mutex_unlock(&channel->lock); |
ea88ca2a | 686 | pthread_mutex_unlock(&consumer_data.lock); |
d88aee68 DG |
687 | error: |
688 | return ret; | |
689 | } | |
690 | ||
691 | /* | |
692 | * RCU read side lock MUST be acquired before calling this function. | |
693 | * | |
694 | * Return 0 on success else an LTTng error code. | |
695 | */ | |
696 | static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key) | |
697 | { | |
698 | int ret; | |
699 | struct lttng_consumer_channel *metadata; | |
700 | ||
8fd623e0 | 701 | DBG("UST consumer setup metadata key %" PRIu64, key); |
d88aee68 DG |
702 | |
703 | metadata = consumer_find_channel(key); | |
704 | if (!metadata) { | |
705 | ERR("UST consumer push metadata %" PRIu64 " not found", key); | |
706 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
10a50311 JD |
707 | goto end; |
708 | } | |
709 | ||
710 | /* | |
711 | * In no monitor mode, the metadata channel has no stream(s) so skip the | |
712 | * ownership transfer to the metadata thread. | |
713 | */ | |
714 | if (!metadata->monitor) { | |
715 | DBG("Metadata channel in no monitor"); | |
716 | ret = 0; | |
717 | goto end; | |
d88aee68 DG |
718 | } |
719 | ||
720 | /* | |
721 | * Send metadata stream to relayd if one available. Availability is | |
722 | * known if the stream is still in the list of the channel. | |
723 | */ | |
724 | if (cds_list_empty(&metadata->streams.head)) { | |
725 | ERR("Metadata channel key %" PRIu64 ", no stream available.", key); | |
726 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
f5a0c9cf | 727 | goto error_no_stream; |
d88aee68 DG |
728 | } |
729 | ||
730 | /* Send metadata stream to relayd if needed. */ | |
62285ea4 DG |
731 | if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) { |
732 | ret = consumer_send_relayd_stream(metadata->metadata_stream, | |
733 | metadata->pathname); | |
734 | if (ret < 0) { | |
735 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
736 | goto error; | |
737 | } | |
d88aee68 DG |
738 | } |
739 | ||
740 | ret = send_streams_to_thread(metadata, ctx); | |
741 | if (ret < 0) { | |
742 | /* | |
743 | * If we are unable to send the stream to the thread, there is | |
744 | * a big problem so just stop everything. | |
745 | */ | |
746 | ret = LTTCOMM_CONSUMERD_FATAL; | |
747 | goto error; | |
748 | } | |
749 | /* List MUST be empty after or else it could be reused. */ | |
750 | assert(cds_list_empty(&metadata->streams.head)); | |
751 | ||
10a50311 JD |
752 | ret = 0; |
753 | goto end; | |
d88aee68 DG |
754 | |
755 | error: | |
f2a444f1 DG |
756 | /* |
757 | * Delete metadata channel on error. At this point, the metadata stream can | |
758 | * NOT be monitored by the metadata thread thus having the guarantee that | |
759 | * the stream is still in the local stream list of the channel. This call | |
760 | * will make sure to clean that list. | |
761 | */ | |
f5a0c9cf DG |
762 | cds_list_del(&metadata->metadata_stream->send_node); |
763 | consumer_stream_destroy(metadata->metadata_stream, NULL); | |
764 | error_no_stream: | |
10a50311 JD |
765 | end: |
766 | return ret; | |
767 | } | |
768 | ||
769 | /* | |
770 | * Snapshot the whole metadata. | |
771 | * | |
772 | * Returns 0 on success, < 0 on error | |
773 | */ | |
774 | static int snapshot_metadata(uint64_t key, char *path, uint64_t relayd_id, | |
775 | struct lttng_consumer_local_data *ctx) | |
776 | { | |
777 | int ret = 0; | |
10a50311 JD |
778 | struct lttng_consumer_channel *metadata_channel; |
779 | struct lttng_consumer_stream *metadata_stream; | |
780 | ||
781 | assert(path); | |
782 | assert(ctx); | |
783 | ||
784 | DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s", | |
785 | key, path); | |
786 | ||
787 | rcu_read_lock(); | |
788 | ||
789 | metadata_channel = consumer_find_channel(key); | |
790 | if (!metadata_channel) { | |
6a00837f MD |
791 | ERR("UST snapshot metadata channel not found for key %" PRIu64, |
792 | key); | |
10a50311 JD |
793 | ret = -1; |
794 | goto error; | |
795 | } | |
796 | assert(!metadata_channel->monitor); | |
797 | ||
798 | /* | |
799 | * Ask the sessiond if we have new metadata waiting and update the | |
800 | * consumer metadata cache. | |
801 | */ | |
5e41ebe1 | 802 | ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0); |
10a50311 JD |
803 | if (ret < 0) { |
804 | goto error; | |
805 | } | |
806 | ||
807 | /* | |
808 | * The metadata stream is NOT created in no monitor mode when the channel | |
809 | * is created on a sessiond ask channel command. | |
810 | */ | |
811 | ret = create_ust_streams(metadata_channel, ctx); | |
812 | if (ret < 0) { | |
813 | goto error; | |
814 | } | |
815 | ||
816 | metadata_stream = metadata_channel->metadata_stream; | |
817 | assert(metadata_stream); | |
818 | ||
819 | if (relayd_id != (uint64_t) -1ULL) { | |
820 | metadata_stream->net_seq_idx = relayd_id; | |
821 | ret = consumer_send_relayd_stream(metadata_stream, path); | |
822 | if (ret < 0) { | |
823 | goto error_stream; | |
824 | } | |
825 | } else { | |
826 | ret = utils_create_stream_file(path, metadata_stream->name, | |
827 | metadata_stream->chan->tracefile_size, | |
828 | metadata_stream->tracefile_count_current, | |
309167d2 | 829 | metadata_stream->uid, metadata_stream->gid, NULL); |
10a50311 JD |
830 | if (ret < 0) { |
831 | goto error_stream; | |
832 | } | |
833 | metadata_stream->out_fd = ret; | |
834 | metadata_stream->tracefile_size_current = 0; | |
835 | } | |
836 | ||
837 | pthread_mutex_lock(&metadata_channel->metadata_cache->lock); | |
10a50311 | 838 | |
04ef1097 | 839 | do { |
10a50311 JD |
840 | ret = lttng_consumer_read_subbuffer(metadata_stream, ctx); |
841 | if (ret < 0) { | |
842 | goto error_unlock; | |
843 | } | |
04ef1097 | 844 | } while (ret > 0); |
10a50311 JD |
845 | |
846 | error_unlock: | |
847 | pthread_mutex_unlock(&metadata_channel->metadata_cache->lock); | |
848 | ||
849 | error_stream: | |
850 | /* | |
851 | * Clean up the stream completly because the next snapshot will use a new | |
852 | * metadata stream. | |
853 | */ | |
854 | cds_list_del(&metadata_stream->send_node); | |
855 | consumer_stream_destroy(metadata_stream, NULL); | |
856 | metadata_channel->metadata_stream = NULL; | |
857 | ||
858 | error: | |
859 | rcu_read_unlock(); | |
860 | return ret; | |
861 | } | |
862 | ||
863 | /* | |
864 | * Take a snapshot of all the stream of a channel. | |
865 | * | |
866 | * Returns 0 on success, < 0 on error | |
867 | */ | |
868 | static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id, | |
5c786ded | 869 | uint64_t max_stream_size, struct lttng_consumer_local_data *ctx) |
10a50311 JD |
870 | { |
871 | int ret; | |
872 | unsigned use_relayd = 0; | |
873 | unsigned long consumed_pos, produced_pos; | |
874 | struct lttng_consumer_channel *channel; | |
875 | struct lttng_consumer_stream *stream; | |
876 | ||
877 | assert(path); | |
878 | assert(ctx); | |
879 | ||
880 | rcu_read_lock(); | |
881 | ||
882 | if (relayd_id != (uint64_t) -1ULL) { | |
883 | use_relayd = 1; | |
884 | } | |
885 | ||
886 | channel = consumer_find_channel(key); | |
887 | if (!channel) { | |
6a00837f | 888 | ERR("UST snapshot channel not found for key %" PRIu64, key); |
10a50311 JD |
889 | ret = -1; |
890 | goto error; | |
891 | } | |
892 | assert(!channel->monitor); | |
6a00837f | 893 | DBG("UST consumer snapshot channel %" PRIu64, key); |
10a50311 JD |
894 | |
895 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
896 | /* Lock stream because we are about to change its state. */ | |
897 | pthread_mutex_lock(&stream->lock); | |
898 | stream->net_seq_idx = relayd_id; | |
899 | ||
900 | if (use_relayd) { | |
901 | ret = consumer_send_relayd_stream(stream, path); | |
902 | if (ret < 0) { | |
903 | goto error_unlock; | |
904 | } | |
905 | } else { | |
906 | ret = utils_create_stream_file(path, stream->name, | |
907 | stream->chan->tracefile_size, | |
908 | stream->tracefile_count_current, | |
309167d2 | 909 | stream->uid, stream->gid, NULL); |
10a50311 JD |
910 | if (ret < 0) { |
911 | goto error_unlock; | |
912 | } | |
913 | stream->out_fd = ret; | |
914 | stream->tracefile_size_current = 0; | |
915 | ||
916 | DBG("UST consumer snapshot stream %s/%s (%" PRIu64 ")", path, | |
917 | stream->name, stream->key); | |
918 | } | |
919 | ||
920 | ustctl_flush_buffer(stream->ustream, 1); | |
921 | ||
922 | ret = lttng_ustconsumer_take_snapshot(stream); | |
923 | if (ret < 0) { | |
924 | ERR("Taking UST snapshot"); | |
925 | goto error_unlock; | |
926 | } | |
927 | ||
928 | ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos); | |
929 | if (ret < 0) { | |
930 | ERR("Produced UST snapshot position"); | |
931 | goto error_unlock; | |
932 | } | |
933 | ||
934 | ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
935 | if (ret < 0) { | |
936 | ERR("Consumerd UST snapshot position"); | |
937 | goto error_unlock; | |
938 | } | |
939 | ||
5c786ded JD |
940 | /* |
941 | * The original value is sent back if max stream size is larger than | |
942 | * the possible size of the snapshot. Also, we asume that the session | |
943 | * daemon should never send a maximum stream size that is lower than | |
944 | * subbuffer size. | |
945 | */ | |
946 | consumed_pos = consumer_get_consumed_maxsize(consumed_pos, | |
947 | produced_pos, max_stream_size); | |
948 | ||
10a50311 JD |
949 | while (consumed_pos < produced_pos) { |
950 | ssize_t read_len; | |
951 | unsigned long len, padded_len; | |
952 | ||
953 | DBG("UST consumer taking snapshot at pos %lu", consumed_pos); | |
954 | ||
955 | ret = ustctl_get_subbuf(stream->ustream, &consumed_pos); | |
956 | if (ret < 0) { | |
957 | if (ret != -EAGAIN) { | |
958 | PERROR("ustctl_get_subbuf snapshot"); | |
959 | goto error_close_stream; | |
960 | } | |
961 | DBG("UST consumer get subbuf failed. Skipping it."); | |
962 | consumed_pos += stream->max_sb_size; | |
963 | continue; | |
964 | } | |
965 | ||
966 | ret = ustctl_get_subbuf_size(stream->ustream, &len); | |
967 | if (ret < 0) { | |
968 | ERR("Snapshot ustctl_get_subbuf_size"); | |
969 | goto error_put_subbuf; | |
970 | } | |
971 | ||
972 | ret = ustctl_get_padded_subbuf_size(stream->ustream, &padded_len); | |
973 | if (ret < 0) { | |
974 | ERR("Snapshot ustctl_get_padded_subbuf_size"); | |
975 | goto error_put_subbuf; | |
976 | } | |
977 | ||
978 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
309167d2 | 979 | padded_len - len, NULL); |
10a50311 JD |
980 | if (use_relayd) { |
981 | if (read_len != len) { | |
56591bac | 982 | ret = -EPERM; |
10a50311 JD |
983 | goto error_put_subbuf; |
984 | } | |
985 | } else { | |
986 | if (read_len != padded_len) { | |
56591bac | 987 | ret = -EPERM; |
10a50311 JD |
988 | goto error_put_subbuf; |
989 | } | |
990 | } | |
991 | ||
992 | ret = ustctl_put_subbuf(stream->ustream); | |
993 | if (ret < 0) { | |
994 | ERR("Snapshot ustctl_put_subbuf"); | |
995 | goto error_close_stream; | |
996 | } | |
997 | consumed_pos += stream->max_sb_size; | |
998 | } | |
999 | ||
1000 | /* Simply close the stream so we can use it on the next snapshot. */ | |
1001 | consumer_stream_close(stream); | |
1002 | pthread_mutex_unlock(&stream->lock); | |
1003 | } | |
1004 | ||
1005 | rcu_read_unlock(); | |
1006 | return 0; | |
1007 | ||
1008 | error_put_subbuf: | |
1009 | if (ustctl_put_subbuf(stream->ustream) < 0) { | |
1010 | ERR("Snapshot ustctl_put_subbuf"); | |
1011 | } | |
1012 | error_close_stream: | |
1013 | consumer_stream_close(stream); | |
1014 | error_unlock: | |
1015 | pthread_mutex_unlock(&stream->lock); | |
1016 | error: | |
1017 | rcu_read_unlock(); | |
d88aee68 DG |
1018 | return ret; |
1019 | } | |
1020 | ||
331744e3 JD |
1021 | /* |
1022 | * Receive the metadata updates from the sessiond. | |
1023 | */ | |
1024 | int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset, | |
5e41ebe1 MD |
1025 | uint64_t len, struct lttng_consumer_channel *channel, |
1026 | int timer) | |
331744e3 JD |
1027 | { |
1028 | int ret, ret_code = LTTNG_OK; | |
1029 | char *metadata_str; | |
1030 | ||
8fd623e0 | 1031 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); |
331744e3 JD |
1032 | |
1033 | metadata_str = zmalloc(len * sizeof(char)); | |
1034 | if (!metadata_str) { | |
1035 | PERROR("zmalloc metadata string"); | |
1036 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; | |
1037 | goto end; | |
1038 | } | |
1039 | ||
1040 | /* Receive metadata string. */ | |
1041 | ret = lttcomm_recv_unix_sock(sock, metadata_str, len); | |
1042 | if (ret < 0) { | |
1043 | /* Session daemon is dead so return gracefully. */ | |
1044 | ret_code = ret; | |
1045 | goto end_free; | |
1046 | } | |
1047 | ||
1048 | pthread_mutex_lock(&channel->metadata_cache->lock); | |
1049 | ret = consumer_metadata_cache_write(channel, offset, len, metadata_str); | |
1050 | if (ret < 0) { | |
1051 | /* Unable to handle metadata. Notify session daemon. */ | |
1052 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
a32bd775 DG |
1053 | /* |
1054 | * Skip metadata flush on write error since the offset and len might | |
1055 | * not have been updated which could create an infinite loop below when | |
1056 | * waiting for the metadata cache to be flushed. | |
1057 | */ | |
1058 | pthread_mutex_unlock(&channel->metadata_cache->lock); | |
a32bd775 | 1059 | goto end_free; |
331744e3 JD |
1060 | } |
1061 | pthread_mutex_unlock(&channel->metadata_cache->lock); | |
1062 | ||
5e41ebe1 | 1063 | while (consumer_metadata_cache_flushed(channel, offset + len, timer)) { |
331744e3 JD |
1064 | DBG("Waiting for metadata to be flushed"); |
1065 | usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME); | |
1066 | } | |
1067 | ||
1068 | end_free: | |
1069 | free(metadata_str); | |
1070 | end: | |
1071 | return ret_code; | |
1072 | } | |
1073 | ||
4cbc1a04 DG |
1074 | /* |
1075 | * Receive command from session daemon and process it. | |
1076 | * | |
1077 | * Return 1 on success else a negative value or 0. | |
1078 | */ | |
3bd1e081 MD |
1079 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
1080 | int sock, struct pollfd *consumer_sockpoll) | |
1081 | { | |
1082 | ssize_t ret; | |
f50f23d9 | 1083 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 | 1084 | struct lttcomm_consumer_msg msg; |
ffe60014 | 1085 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 MD |
1086 | |
1087 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
1088 | if (ret != sizeof(msg)) { | |
173af62f DG |
1089 | DBG("Consumer received unexpected message size %zd (expects %zu)", |
1090 | ret, sizeof(msg)); | |
3be74084 DG |
1091 | /* |
1092 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
1093 | * since the caller handles this. | |
1094 | */ | |
489f70e9 | 1095 | if (ret > 0) { |
c6857fcf | 1096 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
489f70e9 MD |
1097 | ret = -1; |
1098 | } | |
3bd1e081 MD |
1099 | return ret; |
1100 | } | |
1101 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
1102 | /* |
1103 | * Notify the session daemon that the command is completed. | |
1104 | * | |
1105 | * On transport layer error, the function call will print an error | |
1106 | * message so handling the returned code is a bit useless since we | |
1107 | * return an error code anyway. | |
1108 | */ | |
1109 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
1110 | return -ENOENT; |
1111 | } | |
1112 | ||
3f8e211f | 1113 | /* relayd needs RCU read-side lock */ |
b0b335c8 MD |
1114 | rcu_read_lock(); |
1115 | ||
3bd1e081 | 1116 | switch (msg.cmd_type) { |
00e2e675 DG |
1117 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
1118 | { | |
f50f23d9 | 1119 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
1120 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
1121 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 1122 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
1123 | goto end_nosignal; |
1124 | } | |
173af62f DG |
1125 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
1126 | { | |
a6ba4fe1 | 1127 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
1128 | struct consumer_relayd_sock_pair *relayd; |
1129 | ||
a6ba4fe1 | 1130 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
1131 | |
1132 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 1133 | relayd = consumer_find_relayd(index); |
173af62f | 1134 | if (relayd == NULL) { |
3448e266 | 1135 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 1136 | ret_code = LTTNG_ERR_NO_CONSUMER; |
173af62f DG |
1137 | } |
1138 | ||
a6ba4fe1 DG |
1139 | /* |
1140 | * Each relayd socket pair has a refcount of stream attached to it | |
1141 | * which tells if the relayd is still active or not depending on the | |
1142 | * refcount value. | |
1143 | * | |
1144 | * This will set the destroy flag of the relayd object and destroy it | |
1145 | * if the refcount reaches zero when called. | |
1146 | * | |
1147 | * The destroy can happen either here or when a stream fd hangs up. | |
1148 | */ | |
f50f23d9 DG |
1149 | if (relayd) { |
1150 | consumer_flag_relayd_for_destroy(relayd); | |
1151 | } | |
1152 | ||
d88aee68 | 1153 | goto end_msg_sessiond; |
173af62f | 1154 | } |
3bd1e081 MD |
1155 | case LTTNG_CONSUMER_UPDATE_STREAM: |
1156 | { | |
3f8e211f | 1157 | rcu_read_unlock(); |
7ad0a0cb | 1158 | return -ENOSYS; |
3bd1e081 | 1159 | } |
6d805429 | 1160 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 1161 | { |
3be74084 | 1162 | int ret, is_data_pending; |
6d805429 | 1163 | uint64_t id = msg.u.data_pending.session_id; |
ca22feea | 1164 | |
6d805429 | 1165 | DBG("UST consumer data pending command for id %" PRIu64, id); |
ca22feea | 1166 | |
3be74084 | 1167 | is_data_pending = consumer_data_pending(id); |
ca22feea DG |
1168 | |
1169 | /* Send back returned value to session daemon */ | |
3be74084 DG |
1170 | ret = lttcomm_send_unix_sock(sock, &is_data_pending, |
1171 | sizeof(is_data_pending)); | |
ca22feea | 1172 | if (ret < 0) { |
3be74084 | 1173 | DBG("Error when sending the data pending ret code: %d", ret); |
489f70e9 | 1174 | goto error_fatal; |
ca22feea | 1175 | } |
f50f23d9 DG |
1176 | |
1177 | /* | |
1178 | * No need to send back a status message since the data pending | |
1179 | * returned value is the response. | |
1180 | */ | |
ca22feea | 1181 | break; |
53632229 | 1182 | } |
ffe60014 DG |
1183 | case LTTNG_CONSUMER_ASK_CHANNEL_CREATION: |
1184 | { | |
1185 | int ret; | |
1186 | struct ustctl_consumer_channel_attr attr; | |
1187 | ||
1188 | /* Create a plain object and reserve a channel key. */ | |
1189 | channel = allocate_channel(msg.u.ask_channel.session_id, | |
1190 | msg.u.ask_channel.pathname, msg.u.ask_channel.name, | |
1191 | msg.u.ask_channel.uid, msg.u.ask_channel.gid, | |
1192 | msg.u.ask_channel.relayd_id, msg.u.ask_channel.key, | |
1624d5b7 JD |
1193 | (enum lttng_event_output) msg.u.ask_channel.output, |
1194 | msg.u.ask_channel.tracefile_size, | |
2bba9e53 | 1195 | msg.u.ask_channel.tracefile_count, |
1950109e | 1196 | msg.u.ask_channel.session_id_per_pid, |
2bba9e53 | 1197 | msg.u.ask_channel.monitor); |
ffe60014 DG |
1198 | if (!channel) { |
1199 | goto end_channel_error; | |
1200 | } | |
1201 | ||
567eb353 DG |
1202 | /* |
1203 | * Assign UST application UID to the channel. This value is ignored for | |
1204 | * per PID buffers. This is specific to UST thus setting this after the | |
1205 | * allocation. | |
1206 | */ | |
1207 | channel->ust_app_uid = msg.u.ask_channel.ust_app_uid; | |
1208 | ||
ffe60014 DG |
1209 | /* Build channel attributes from received message. */ |
1210 | attr.subbuf_size = msg.u.ask_channel.subbuf_size; | |
1211 | attr.num_subbuf = msg.u.ask_channel.num_subbuf; | |
1212 | attr.overwrite = msg.u.ask_channel.overwrite; | |
1213 | attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval; | |
1214 | attr.read_timer_interval = msg.u.ask_channel.read_timer_interval; | |
7972aab2 | 1215 | attr.chan_id = msg.u.ask_channel.chan_id; |
10a50311 | 1216 | attr.output = msg.u.ask_channel.output; |
ffe60014 DG |
1217 | memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid)); |
1218 | ||
ffe60014 DG |
1219 | /* Translate and save channel type. */ |
1220 | switch (msg.u.ask_channel.type) { | |
1221 | case LTTNG_UST_CHAN_PER_CPU: | |
1222 | channel->type = CONSUMER_CHANNEL_TYPE_DATA; | |
1223 | attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8633d6e3 MD |
1224 | /* |
1225 | * Set refcount to 1 for owner. Below, we will | |
1226 | * pass ownership to the | |
1227 | * consumer_thread_channel_poll() thread. | |
1228 | */ | |
1229 | channel->refcount = 1; | |
ffe60014 DG |
1230 | break; |
1231 | case LTTNG_UST_CHAN_METADATA: | |
1232 | channel->type = CONSUMER_CHANNEL_TYPE_METADATA; | |
1233 | attr.type = LTTNG_UST_CHAN_METADATA; | |
1234 | break; | |
1235 | default: | |
1236 | assert(0); | |
1237 | goto error_fatal; | |
1238 | }; | |
1239 | ||
1240 | ret = ask_channel(ctx, sock, channel, &attr); | |
1241 | if (ret < 0) { | |
1242 | goto end_channel_error; | |
1243 | } | |
1244 | ||
fc643247 MD |
1245 | if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) { |
1246 | ret = consumer_metadata_cache_allocate(channel); | |
1247 | if (ret < 0) { | |
1248 | ERR("Allocating metadata cache"); | |
1249 | goto end_channel_error; | |
1250 | } | |
1251 | consumer_timer_switch_start(channel, attr.switch_timer_interval); | |
1252 | attr.switch_timer_interval = 0; | |
1253 | } | |
1254 | ||
ffe60014 DG |
1255 | /* |
1256 | * Add the channel to the internal state AFTER all streams were created | |
1257 | * and successfully sent to session daemon. This way, all streams must | |
1258 | * be ready before this channel is visible to the threads. | |
fc643247 MD |
1259 | * If add_channel succeeds, ownership of the channel is |
1260 | * passed to consumer_thread_channel_poll(). | |
ffe60014 DG |
1261 | */ |
1262 | ret = add_channel(channel, ctx); | |
1263 | if (ret < 0) { | |
ea88ca2a MD |
1264 | if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) { |
1265 | if (channel->switch_timer_enabled == 1) { | |
1266 | consumer_timer_switch_stop(channel); | |
1267 | } | |
1268 | consumer_metadata_cache_destroy(channel); | |
1269 | } | |
ffe60014 DG |
1270 | goto end_channel_error; |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Channel and streams are now created. Inform the session daemon that | |
1275 | * everything went well and should wait to receive the channel and | |
1276 | * streams with ustctl API. | |
1277 | */ | |
1278 | ret = consumer_send_status_channel(sock, channel); | |
1279 | if (ret < 0) { | |
1280 | /* | |
489f70e9 | 1281 | * There is probably a problem on the socket. |
ffe60014 | 1282 | */ |
489f70e9 | 1283 | goto error_fatal; |
ffe60014 DG |
1284 | } |
1285 | ||
1286 | break; | |
1287 | } | |
1288 | case LTTNG_CONSUMER_GET_CHANNEL: | |
1289 | { | |
1290 | int ret, relayd_err = 0; | |
d88aee68 | 1291 | uint64_t key = msg.u.get_channel.key; |
ffe60014 | 1292 | struct lttng_consumer_channel *channel; |
ffe60014 DG |
1293 | |
1294 | channel = consumer_find_channel(key); | |
1295 | if (!channel) { | |
8fd623e0 | 1296 | ERR("UST consumer get channel key %" PRIu64 " not found", key); |
ffe60014 DG |
1297 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
1298 | goto end_msg_sessiond; | |
1299 | } | |
1300 | ||
ffe60014 DG |
1301 | /* Send everything to sessiond. */ |
1302 | ret = send_sessiond_channel(sock, channel, ctx, &relayd_err); | |
1303 | if (ret < 0) { | |
1304 | if (relayd_err) { | |
1305 | /* | |
1306 | * We were unable to send to the relayd the stream so avoid | |
1307 | * sending back a fatal error to the thread since this is OK | |
f2a444f1 DG |
1308 | * and the consumer can continue its work. The above call |
1309 | * has sent the error status message to the sessiond. | |
ffe60014 | 1310 | */ |
f2a444f1 | 1311 | goto end_nosignal; |
ffe60014 DG |
1312 | } |
1313 | /* | |
1314 | * The communicaton was broken hence there is a bad state between | |
1315 | * the consumer and sessiond so stop everything. | |
1316 | */ | |
1317 | goto error_fatal; | |
1318 | } | |
1319 | ||
10a50311 JD |
1320 | /* |
1321 | * In no monitor mode, the streams ownership is kept inside the channel | |
1322 | * so don't send them to the data thread. | |
1323 | */ | |
1324 | if (!channel->monitor) { | |
1325 | goto end_msg_sessiond; | |
1326 | } | |
1327 | ||
d88aee68 DG |
1328 | ret = send_streams_to_thread(channel, ctx); |
1329 | if (ret < 0) { | |
1330 | /* | |
1331 | * If we are unable to send the stream to the thread, there is | |
1332 | * a big problem so just stop everything. | |
1333 | */ | |
1334 | goto error_fatal; | |
ffe60014 | 1335 | } |
ffe60014 DG |
1336 | /* List MUST be empty after or else it could be reused. */ |
1337 | assert(cds_list_empty(&channel->streams.head)); | |
d88aee68 DG |
1338 | goto end_msg_sessiond; |
1339 | } | |
1340 | case LTTNG_CONSUMER_DESTROY_CHANNEL: | |
1341 | { | |
1342 | uint64_t key = msg.u.destroy_channel.key; | |
d88aee68 | 1343 | |
a0cbdd2e MD |
1344 | /* |
1345 | * Only called if streams have not been sent to stream | |
1346 | * manager thread. However, channel has been sent to | |
1347 | * channel manager thread. | |
1348 | */ | |
1349 | notify_thread_del_channel(ctx, key); | |
d88aee68 | 1350 | goto end_msg_sessiond; |
ffe60014 | 1351 | } |
d88aee68 DG |
1352 | case LTTNG_CONSUMER_CLOSE_METADATA: |
1353 | { | |
1354 | int ret; | |
1355 | ||
1356 | ret = close_metadata(msg.u.close_metadata.key); | |
1357 | if (ret != 0) { | |
1358 | ret_code = ret; | |
1359 | } | |
1360 | ||
1361 | goto end_msg_sessiond; | |
1362 | } | |
7972aab2 DG |
1363 | case LTTNG_CONSUMER_FLUSH_CHANNEL: |
1364 | { | |
1365 | int ret; | |
1366 | ||
1367 | ret = flush_channel(msg.u.flush_channel.key); | |
1368 | if (ret != 0) { | |
1369 | ret_code = ret; | |
1370 | } | |
1371 | ||
1372 | goto end_msg_sessiond; | |
1373 | } | |
d88aee68 | 1374 | case LTTNG_CONSUMER_PUSH_METADATA: |
ffe60014 DG |
1375 | { |
1376 | int ret; | |
d88aee68 | 1377 | uint64_t len = msg.u.push_metadata.len; |
d88aee68 | 1378 | uint64_t key = msg.u.push_metadata.key; |
331744e3 | 1379 | uint64_t offset = msg.u.push_metadata.target_offset; |
ffe60014 DG |
1380 | struct lttng_consumer_channel *channel; |
1381 | ||
8fd623e0 DG |
1382 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, |
1383 | len); | |
ffe60014 DG |
1384 | |
1385 | channel = consumer_find_channel(key); | |
1386 | if (!channel) { | |
8fd623e0 | 1387 | ERR("UST consumer push metadata %" PRIu64 " not found", key); |
ffe60014 | 1388 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
4a2eb0ca | 1389 | goto end_msg_sessiond; |
d88aee68 DG |
1390 | } |
1391 | ||
1392 | /* Tell session daemon we are ready to receive the metadata. */ | |
ffe60014 DG |
1393 | ret = consumer_send_status_msg(sock, LTTNG_OK); |
1394 | if (ret < 0) { | |
1395 | /* Somehow, the session daemon is not responding anymore. */ | |
d88aee68 DG |
1396 | goto error_fatal; |
1397 | } | |
1398 | ||
1399 | /* Wait for more data. */ | |
1400 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
489f70e9 | 1401 | goto error_fatal; |
d88aee68 DG |
1402 | } |
1403 | ||
331744e3 | 1404 | ret = lttng_ustconsumer_recv_metadata(sock, key, offset, |
5e41ebe1 | 1405 | len, channel, 0); |
d88aee68 | 1406 | if (ret < 0) { |
331744e3 | 1407 | /* error receiving from sessiond */ |
489f70e9 | 1408 | goto error_fatal; |
331744e3 JD |
1409 | } else { |
1410 | ret_code = ret; | |
d88aee68 DG |
1411 | goto end_msg_sessiond; |
1412 | } | |
d88aee68 DG |
1413 | } |
1414 | case LTTNG_CONSUMER_SETUP_METADATA: | |
1415 | { | |
1416 | int ret; | |
1417 | ||
1418 | ret = setup_metadata(ctx, msg.u.setup_metadata.key); | |
1419 | if (ret) { | |
1420 | ret_code = ret; | |
1421 | } | |
1422 | goto end_msg_sessiond; | |
ffe60014 | 1423 | } |
6dc3064a DG |
1424 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
1425 | { | |
10a50311 JD |
1426 | if (msg.u.snapshot_channel.metadata) { |
1427 | ret = snapshot_metadata(msg.u.snapshot_channel.key, | |
1428 | msg.u.snapshot_channel.pathname, | |
1429 | msg.u.snapshot_channel.relayd_id, | |
1430 | ctx); | |
1431 | if (ret < 0) { | |
1432 | ERR("Snapshot metadata failed"); | |
1433 | ret_code = LTTNG_ERR_UST_META_FAIL; | |
1434 | } | |
1435 | } else { | |
1436 | ret = snapshot_channel(msg.u.snapshot_channel.key, | |
1437 | msg.u.snapshot_channel.pathname, | |
1438 | msg.u.snapshot_channel.relayd_id, | |
5c786ded | 1439 | msg.u.snapshot_channel.max_stream_size, |
10a50311 JD |
1440 | ctx); |
1441 | if (ret < 0) { | |
1442 | ERR("Snapshot channel failed"); | |
1443 | ret_code = LTTNG_ERR_UST_CHAN_FAIL; | |
1444 | } | |
1445 | } | |
1446 | ||
6dc3064a DG |
1447 | ret = consumer_send_status_msg(sock, ret_code); |
1448 | if (ret < 0) { | |
1449 | /* Somehow, the session daemon is not responding anymore. */ | |
1450 | goto end_nosignal; | |
1451 | } | |
1452 | break; | |
1453 | } | |
3bd1e081 MD |
1454 | default: |
1455 | break; | |
1456 | } | |
3f8e211f | 1457 | |
3bd1e081 | 1458 | end_nosignal: |
b0b335c8 | 1459 | rcu_read_unlock(); |
4cbc1a04 DG |
1460 | |
1461 | /* | |
1462 | * Return 1 to indicate success since the 0 value can be a socket | |
1463 | * shutdown during the recv() or send() call. | |
1464 | */ | |
1465 | return 1; | |
ffe60014 DG |
1466 | |
1467 | end_msg_sessiond: | |
1468 | /* | |
1469 | * The returned value here is not useful since either way we'll return 1 to | |
1470 | * the caller because the session daemon socket management is done | |
1471 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. | |
1472 | */ | |
489f70e9 MD |
1473 | ret = consumer_send_status_msg(sock, ret_code); |
1474 | if (ret < 0) { | |
1475 | goto error_fatal; | |
1476 | } | |
ffe60014 DG |
1477 | rcu_read_unlock(); |
1478 | return 1; | |
1479 | end_channel_error: | |
1480 | if (channel) { | |
1481 | /* | |
1482 | * Free channel here since no one has a reference to it. We don't | |
1483 | * free after that because a stream can store this pointer. | |
1484 | */ | |
1485 | destroy_channel(channel); | |
1486 | } | |
1487 | /* We have to send a status channel message indicating an error. */ | |
1488 | ret = consumer_send_status_channel(sock, NULL); | |
1489 | if (ret < 0) { | |
1490 | /* Stop everything if session daemon can not be notified. */ | |
1491 | goto error_fatal; | |
1492 | } | |
1493 | rcu_read_unlock(); | |
1494 | return 1; | |
1495 | error_fatal: | |
1496 | rcu_read_unlock(); | |
1497 | /* This will issue a consumer stop. */ | |
1498 | return -1; | |
3bd1e081 MD |
1499 | } |
1500 | ||
ffe60014 DG |
1501 | /* |
1502 | * Wrapper over the mmap() read offset from ust-ctl library. Since this can be | |
1503 | * compiled out, we isolate it in this library. | |
1504 | */ | |
1505 | int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream *stream, | |
1506 | unsigned long *off) | |
3bd1e081 | 1507 | { |
ffe60014 DG |
1508 | assert(stream); |
1509 | assert(stream->ustream); | |
b5c5fc29 | 1510 | |
ffe60014 | 1511 | return ustctl_get_mmap_read_offset(stream->ustream, off); |
3bd1e081 MD |
1512 | } |
1513 | ||
ffe60014 DG |
1514 | /* |
1515 | * Wrapper over the mmap() read offset from ust-ctl library. Since this can be | |
1516 | * compiled out, we isolate it in this library. | |
1517 | */ | |
1518 | void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream) | |
d056b477 | 1519 | { |
ffe60014 DG |
1520 | assert(stream); |
1521 | assert(stream->ustream); | |
1522 | ||
1523 | return ustctl_get_mmap_base(stream->ustream); | |
d056b477 MD |
1524 | } |
1525 | ||
ffe60014 DG |
1526 | /* |
1527 | * Take a snapshot for a specific fd | |
1528 | * | |
1529 | * Returns 0 on success, < 0 on error | |
1530 | */ | |
1531 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream) | |
3bd1e081 | 1532 | { |
ffe60014 DG |
1533 | assert(stream); |
1534 | assert(stream->ustream); | |
1535 | ||
1536 | return ustctl_snapshot(stream->ustream); | |
3bd1e081 MD |
1537 | } |
1538 | ||
ffe60014 DG |
1539 | /* |
1540 | * Get the produced position | |
1541 | * | |
1542 | * Returns 0 on success, < 0 on error | |
1543 | */ | |
1544 | int lttng_ustconsumer_get_produced_snapshot( | |
1545 | struct lttng_consumer_stream *stream, unsigned long *pos) | |
3bd1e081 | 1546 | { |
ffe60014 DG |
1547 | assert(stream); |
1548 | assert(stream->ustream); | |
1549 | assert(pos); | |
7a57cf92 | 1550 | |
ffe60014 DG |
1551 | return ustctl_snapshot_get_produced(stream->ustream, pos); |
1552 | } | |
7a57cf92 | 1553 | |
10a50311 JD |
1554 | /* |
1555 | * Get the consumed position | |
1556 | * | |
1557 | * Returns 0 on success, < 0 on error | |
1558 | */ | |
1559 | int lttng_ustconsumer_get_consumed_snapshot( | |
1560 | struct lttng_consumer_stream *stream, unsigned long *pos) | |
1561 | { | |
1562 | assert(stream); | |
1563 | assert(stream->ustream); | |
1564 | assert(pos); | |
1565 | ||
1566 | return ustctl_snapshot_get_consumed(stream->ustream, pos); | |
1567 | } | |
1568 | ||
ffe60014 DG |
1569 | /* |
1570 | * Called when the stream signal the consumer that it has hang up. | |
1571 | */ | |
1572 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) | |
1573 | { | |
1574 | assert(stream); | |
1575 | assert(stream->ustream); | |
2c1dd183 | 1576 | |
ffe60014 DG |
1577 | ustctl_flush_buffer(stream->ustream, 0); |
1578 | stream->hangup_flush_done = 1; | |
1579 | } | |
ee77a7b0 | 1580 | |
ffe60014 DG |
1581 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
1582 | { | |
1583 | assert(chan); | |
1584 | assert(chan->uchan); | |
e316aad5 | 1585 | |
ea88ca2a MD |
1586 | if (chan->switch_timer_enabled == 1) { |
1587 | consumer_timer_switch_stop(chan); | |
1588 | } | |
1589 | consumer_metadata_cache_destroy(chan); | |
ffe60014 | 1590 | ustctl_destroy_channel(chan->uchan); |
3bd1e081 MD |
1591 | } |
1592 | ||
1593 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
1594 | { | |
ffe60014 DG |
1595 | assert(stream); |
1596 | assert(stream->ustream); | |
d41f73b7 | 1597 | |
ea88ca2a MD |
1598 | if (stream->chan->switch_timer_enabled == 1) { |
1599 | consumer_timer_switch_stop(stream->chan); | |
1600 | } | |
ffe60014 DG |
1601 | ustctl_destroy_stream(stream->ustream); |
1602 | } | |
d41f73b7 | 1603 | |
309167d2 JD |
1604 | /* |
1605 | * Populate index values of a UST stream. Values are set in big endian order. | |
1606 | * | |
1607 | * Return 0 on success or else a negative value. | |
1608 | */ | |
1609 | static int get_index_values(struct lttng_packet_index *index, | |
1610 | struct ustctl_consumer_stream *ustream) | |
1611 | { | |
1612 | int ret; | |
1613 | ||
1614 | ret = ustctl_get_timestamp_begin(ustream, &index->timestamp_begin); | |
1615 | if (ret < 0) { | |
1616 | PERROR("ustctl_get_timestamp_begin"); | |
1617 | goto error; | |
1618 | } | |
1619 | index->timestamp_begin = htobe64(index->timestamp_begin); | |
1620 | ||
1621 | ret = ustctl_get_timestamp_end(ustream, &index->timestamp_end); | |
1622 | if (ret < 0) { | |
1623 | PERROR("ustctl_get_timestamp_end"); | |
1624 | goto error; | |
1625 | } | |
1626 | index->timestamp_end = htobe64(index->timestamp_end); | |
1627 | ||
1628 | ret = ustctl_get_events_discarded(ustream, &index->events_discarded); | |
1629 | if (ret < 0) { | |
1630 | PERROR("ustctl_get_events_discarded"); | |
1631 | goto error; | |
1632 | } | |
1633 | index->events_discarded = htobe64(index->events_discarded); | |
1634 | ||
1635 | ret = ustctl_get_content_size(ustream, &index->content_size); | |
1636 | if (ret < 0) { | |
1637 | PERROR("ustctl_get_content_size"); | |
1638 | goto error; | |
1639 | } | |
1640 | index->content_size = htobe64(index->content_size); | |
1641 | ||
1642 | ret = ustctl_get_packet_size(ustream, &index->packet_size); | |
1643 | if (ret < 0) { | |
1644 | PERROR("ustctl_get_packet_size"); | |
1645 | goto error; | |
1646 | } | |
1647 | index->packet_size = htobe64(index->packet_size); | |
1648 | ||
1649 | ret = ustctl_get_stream_id(ustream, &index->stream_id); | |
1650 | if (ret < 0) { | |
1651 | PERROR("ustctl_get_stream_id"); | |
1652 | goto error; | |
1653 | } | |
1654 | index->stream_id = htobe64(index->stream_id); | |
1655 | ||
1656 | error: | |
1657 | return ret; | |
1658 | } | |
1659 | ||
1660 | ||
d41f73b7 MD |
1661 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
1662 | struct lttng_consumer_local_data *ctx) | |
1663 | { | |
1d4dfdef | 1664 | unsigned long len, subbuf_size, padding; |
309167d2 | 1665 | int err, write_index = 0; |
d41f73b7 | 1666 | long ret = 0; |
d41f73b7 | 1667 | char dummy; |
ffe60014 | 1668 | struct ustctl_consumer_stream *ustream; |
309167d2 | 1669 | struct lttng_packet_index index; |
ffe60014 DG |
1670 | |
1671 | assert(stream); | |
1672 | assert(stream->ustream); | |
1673 | assert(ctx); | |
d41f73b7 | 1674 | |
3eb914c0 | 1675 | DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd, |
ffe60014 DG |
1676 | stream->name); |
1677 | ||
1678 | /* Ease our life for what's next. */ | |
1679 | ustream = stream->ustream; | |
d41f73b7 | 1680 | |
309167d2 JD |
1681 | /* Indicate that for this stream we have to write the index. */ |
1682 | if (stream->index_fd >= 0) { | |
1683 | write_index = 1; | |
1684 | } | |
1685 | ||
d41f73b7 | 1686 | /* We can consume the 1 byte written into the wait_fd by UST */ |
04ef1097 | 1687 | if (stream->monitor && !stream->hangup_flush_done) { |
c617c0c6 MD |
1688 | ssize_t readlen; |
1689 | ||
effcf122 MD |
1690 | do { |
1691 | readlen = read(stream->wait_fd, &dummy, 1); | |
87dc6a9c | 1692 | } while (readlen == -1 && errno == EINTR); |
04ef1097 | 1693 | if (readlen == -1 && errno != EAGAIN && errno != EWOULDBLOCK) { |
effcf122 MD |
1694 | ret = readlen; |
1695 | goto end; | |
1696 | } | |
d41f73b7 MD |
1697 | } |
1698 | ||
04ef1097 | 1699 | retry: |
d41f73b7 | 1700 | /* Get the next subbuffer */ |
ffe60014 | 1701 | err = ustctl_get_next_subbuf(ustream); |
d41f73b7 | 1702 | if (err != 0) { |
04ef1097 MD |
1703 | /* |
1704 | * Populate metadata info if the existing info has | |
1705 | * already been read. | |
1706 | */ | |
1707 | if (stream->metadata_flag) { | |
1708 | ssize_t write_len; | |
1709 | ||
1710 | if (stream->chan->metadata_cache->contiguous | |
1711 | == stream->ust_metadata_pushed) { | |
1712 | ret = 0; | |
1713 | goto end; | |
1714 | } | |
1715 | ||
1716 | write_len = ustctl_write_one_packet_to_channel(stream->chan->uchan, | |
1717 | &stream->chan->metadata_cache->data[stream->ust_metadata_pushed], | |
1718 | stream->chan->metadata_cache->contiguous | |
1719 | - stream->ust_metadata_pushed); | |
1720 | assert(write_len != 0); | |
1721 | if (write_len < 0) { | |
1722 | ERR("Writing one metadata packet"); | |
1723 | ret = -1; | |
1724 | goto end; | |
1725 | } | |
1726 | stream->ust_metadata_pushed += write_len; | |
1727 | ustctl_flush_buffer(stream->ustream, 1); | |
1728 | goto retry; | |
1729 | } | |
1730 | ||
1d4dfdef | 1731 | ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
1732 | /* |
1733 | * This is a debug message even for single-threaded consumer, | |
1734 | * because poll() have more relaxed criterions than get subbuf, | |
1735 | * so get_subbuf may fail for short race windows where poll() | |
1736 | * would issue wakeups. | |
1737 | */ | |
1738 | DBG("Reserving sub buffer failed (everything is normal, " | |
ffe60014 | 1739 | "it is due to concurrency) [ret: %d]", err); |
d41f73b7 MD |
1740 | goto end; |
1741 | } | |
ffe60014 | 1742 | assert(stream->chan->output == CONSUMER_CHANNEL_MMAP); |
309167d2 JD |
1743 | |
1744 | if (!stream->metadata_flag && write_index) { | |
1745 | index.offset = htobe64(stream->out_fd_offset); | |
1746 | ret = get_index_values(&index, ustream); | |
1747 | if (ret < 0) { | |
1748 | goto end; | |
1749 | } | |
1750 | } | |
1751 | ||
1d4dfdef | 1752 | /* Get the full padded subbuffer size */ |
ffe60014 | 1753 | err = ustctl_get_padded_subbuf_size(ustream, &len); |
effcf122 | 1754 | assert(err == 0); |
1d4dfdef DG |
1755 | |
1756 | /* Get subbuffer data size (without padding) */ | |
ffe60014 | 1757 | err = ustctl_get_subbuf_size(ustream, &subbuf_size); |
1d4dfdef DG |
1758 | assert(err == 0); |
1759 | ||
1760 | /* Make sure we don't get a subbuffer size bigger than the padded */ | |
1761 | assert(len >= subbuf_size); | |
1762 | ||
1763 | padding = len - subbuf_size; | |
d41f73b7 | 1764 | /* write the subbuffer to the tracefile */ |
309167d2 | 1765 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding, &index); |
91dfef6e DG |
1766 | /* |
1767 | * The mmap operation should write subbuf_size amount of data when network | |
1768 | * streaming or the full padding (len) size when we are _not_ streaming. | |
1769 | */ | |
d88aee68 DG |
1770 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1771 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
d41f73b7 | 1772 | /* |
91dfef6e | 1773 | * Display the error but continue processing to try to release the |
c5c45efa DG |
1774 | * subbuffer. This is a DBG statement since any unexpected kill or |
1775 | * signal, the application gets unregistered, relayd gets closed or | |
1776 | * anything that affects the buffer lifetime will trigger this error. | |
1777 | * So, for the sake of the user, don't print this error since it can | |
1778 | * happen and it is OK with the code flow. | |
d41f73b7 | 1779 | */ |
c5c45efa | 1780 | DBG("Error writing to tracefile " |
8fd623e0 | 1781 | "(ret: %ld != len: %lu != subbuf_size: %lu)", |
91dfef6e | 1782 | ret, len, subbuf_size); |
309167d2 | 1783 | write_index = 0; |
d41f73b7 | 1784 | } |
ffe60014 | 1785 | err = ustctl_put_next_subbuf(ustream); |
effcf122 | 1786 | assert(err == 0); |
331744e3 | 1787 | |
309167d2 JD |
1788 | /* Write index if needed. */ |
1789 | if (write_index) { | |
1790 | err = index_write(stream->index_fd, &index, sizeof(index)); | |
1791 | if (err < 0) { | |
1792 | ret = -1; | |
1793 | goto end; | |
1794 | } | |
1795 | } | |
1796 | ||
d41f73b7 MD |
1797 | end: |
1798 | return ret; | |
1799 | } | |
1800 | ||
ffe60014 DG |
1801 | /* |
1802 | * Called when a stream is created. | |
fe4477ee JD |
1803 | * |
1804 | * Return 0 on success or else a negative value. | |
ffe60014 | 1805 | */ |
d41f73b7 MD |
1806 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
1807 | { | |
fe4477ee JD |
1808 | int ret; |
1809 | ||
10a50311 JD |
1810 | assert(stream); |
1811 | ||
fe4477ee | 1812 | /* Don't create anything if this is set for streaming. */ |
10a50311 | 1813 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { |
fe4477ee JD |
1814 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
1815 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
309167d2 | 1816 | stream->uid, stream->gid, NULL); |
fe4477ee JD |
1817 | if (ret < 0) { |
1818 | goto error; | |
1819 | } | |
1820 | stream->out_fd = ret; | |
1821 | stream->tracefile_size_current = 0; | |
309167d2 JD |
1822 | |
1823 | if (!stream->metadata_flag) { | |
1824 | ret = index_create_file(stream->chan->pathname, | |
1825 | stream->name, stream->uid, stream->gid, | |
1826 | stream->chan->tracefile_size, | |
1827 | stream->tracefile_count_current); | |
1828 | if (ret < 0) { | |
1829 | goto error; | |
1830 | } | |
1831 | stream->index_fd = ret; | |
1832 | } | |
fe4477ee JD |
1833 | } |
1834 | ret = 0; | |
1835 | ||
1836 | error: | |
1837 | return ret; | |
d41f73b7 | 1838 | } |
ca22feea DG |
1839 | |
1840 | /* | |
1841 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1842 | * stream. Consumer data lock MUST be acquired before calling this function |
1843 | * and the stream lock. | |
ca22feea | 1844 | * |
6d805429 | 1845 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1846 | * data is available for trace viewer reading. |
1847 | */ | |
6d805429 | 1848 | int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1849 | { |
1850 | int ret; | |
1851 | ||
1852 | assert(stream); | |
ffe60014 | 1853 | assert(stream->ustream); |
ca22feea | 1854 | |
6d805429 | 1855 | DBG("UST consumer checking data pending"); |
c8f59ee5 | 1856 | |
ca6b395f MD |
1857 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1858 | ret = 0; | |
1859 | goto end; | |
1860 | } | |
1861 | ||
04ef1097 | 1862 | if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) { |
e6ee4eab DG |
1863 | uint64_t contiguous, pushed; |
1864 | ||
1865 | /* Ease our life a bit. */ | |
1866 | contiguous = stream->chan->metadata_cache->contiguous; | |
1867 | pushed = stream->ust_metadata_pushed; | |
1868 | ||
04ef1097 MD |
1869 | /* |
1870 | * We can simply check whether all contiguously available data | |
1871 | * has been pushed to the ring buffer, since the push operation | |
1872 | * is performed within get_next_subbuf(), and because both | |
1873 | * get_next_subbuf() and put_next_subbuf() are issued atomically | |
1874 | * thanks to the stream lock within | |
1875 | * lttng_ustconsumer_read_subbuffer(). This basically means that | |
1876 | * whetnever ust_metadata_pushed is incremented, the associated | |
1877 | * metadata has been consumed from the metadata stream. | |
1878 | */ | |
1879 | DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64, | |
e6ee4eab | 1880 | contiguous, pushed); |
6acdf328 | 1881 | assert(((int64_t) contiguous - pushed) >= 0); |
e6ee4eab | 1882 | if ((contiguous != pushed) || |
6acdf328 | 1883 | (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) { |
04ef1097 MD |
1884 | ret = 1; /* Data is pending */ |
1885 | goto end; | |
1886 | } | |
1887 | } else { | |
1888 | ret = ustctl_get_next_subbuf(stream->ustream); | |
1889 | if (ret == 0) { | |
1890 | /* | |
1891 | * There is still data so let's put back this | |
1892 | * subbuffer. | |
1893 | */ | |
1894 | ret = ustctl_put_subbuf(stream->ustream); | |
1895 | assert(ret == 0); | |
1896 | ret = 1; /* Data is pending */ | |
1897 | goto end; | |
1898 | } | |
ca22feea DG |
1899 | } |
1900 | ||
6d805429 DG |
1901 | /* Data is NOT pending so ready to be read. */ |
1902 | ret = 0; | |
ca22feea | 1903 | |
6efae65e DG |
1904 | end: |
1905 | return ret; | |
ca22feea | 1906 | } |
d88aee68 DG |
1907 | |
1908 | /* | |
1909 | * Close every metadata stream wait fd of the metadata hash table. This | |
1910 | * function MUST be used very carefully so not to run into a race between the | |
1911 | * metadata thread handling streams and this function closing their wait fd. | |
1912 | * | |
1913 | * For UST, this is used when the session daemon hangs up. Its the metadata | |
1914 | * producer so calling this is safe because we are assured that no state change | |
1915 | * can occur in the metadata thread for the streams in the hash table. | |
1916 | */ | |
1917 | void lttng_ustconsumer_close_metadata(struct lttng_ht *metadata_ht) | |
1918 | { | |
d88aee68 DG |
1919 | struct lttng_ht_iter iter; |
1920 | struct lttng_consumer_stream *stream; | |
1921 | ||
1922 | assert(metadata_ht); | |
1923 | assert(metadata_ht->ht); | |
1924 | ||
1925 | DBG("UST consumer closing all metadata streams"); | |
1926 | ||
1927 | rcu_read_lock(); | |
1928 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, | |
1929 | node.node) { | |
be2b50c7 | 1930 | pthread_mutex_lock(&stream->chan->lock); |
d88aee68 | 1931 | /* |
be2b50c7 DG |
1932 | * Whatever returned value, we must continue to try to close everything |
1933 | * so ignore it. | |
d88aee68 | 1934 | */ |
be2b50c7 DG |
1935 | (void) _close_metadata(stream->chan); |
1936 | DBG("Metadata wait fd %d and poll pipe fd %d closed", stream->wait_fd, | |
1937 | stream->ust_metadata_poll_pipe[1]); | |
1938 | pthread_mutex_unlock(&stream->chan->lock); | |
1939 | ||
d88aee68 DG |
1940 | } |
1941 | rcu_read_unlock(); | |
1942 | } | |
d8ef542d MD |
1943 | |
1944 | void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream) | |
1945 | { | |
1946 | int ret; | |
1947 | ||
1948 | ret = ustctl_stream_close_wakeup_fd(stream->ustream); | |
1949 | if (ret < 0) { | |
1950 | ERR("Unable to close wakeup fd"); | |
1951 | } | |
1952 | } | |
331744e3 | 1953 | |
f666ae70 MD |
1954 | /* |
1955 | * Please refer to consumer-timer.c before adding any lock within this | |
1956 | * function or any of its callees. Timers have a very strict locking | |
1957 | * semantic with respect to teardown. Failure to respect this semantic | |
1958 | * introduces deadlocks. | |
1959 | */ | |
331744e3 | 1960 | int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx, |
5e41ebe1 | 1961 | struct lttng_consumer_channel *channel, int timer) |
331744e3 JD |
1962 | { |
1963 | struct lttcomm_metadata_request_msg request; | |
1964 | struct lttcomm_consumer_msg msg; | |
1965 | enum lttng_error_code ret_code = LTTNG_OK; | |
1966 | uint64_t len, key, offset; | |
1967 | int ret; | |
1968 | ||
1969 | assert(channel); | |
1970 | assert(channel->metadata_cache); | |
1971 | ||
1972 | /* send the metadata request to sessiond */ | |
1973 | switch (consumer_data.type) { | |
1974 | case LTTNG_CONSUMER64_UST: | |
1975 | request.bits_per_long = 64; | |
1976 | break; | |
1977 | case LTTNG_CONSUMER32_UST: | |
1978 | request.bits_per_long = 32; | |
1979 | break; | |
1980 | default: | |
1981 | request.bits_per_long = 0; | |
1982 | break; | |
1983 | } | |
1984 | ||
1985 | request.session_id = channel->session_id; | |
1950109e | 1986 | request.session_id_per_pid = channel->session_id_per_pid; |
567eb353 DG |
1987 | /* |
1988 | * Request the application UID here so the metadata of that application can | |
1989 | * be sent back. The channel UID corresponds to the user UID of the session | |
1990 | * used for the rights on the stream file(s). | |
1991 | */ | |
1992 | request.uid = channel->ust_app_uid; | |
331744e3 | 1993 | request.key = channel->key; |
567eb353 | 1994 | |
1950109e | 1995 | DBG("Sending metadata request to sessiond, session id %" PRIu64 |
567eb353 DG |
1996 | ", per-pid %" PRIu64 ", app UID %u and channek key %" PRIu64, |
1997 | request.session_id, request.session_id_per_pid, request.uid, | |
1998 | request.key); | |
331744e3 | 1999 | |
75d83e50 | 2000 | pthread_mutex_lock(&ctx->metadata_socket_lock); |
331744e3 JD |
2001 | ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request, |
2002 | sizeof(request)); | |
2003 | if (ret < 0) { | |
2004 | ERR("Asking metadata to sessiond"); | |
2005 | goto end; | |
2006 | } | |
2007 | ||
2008 | /* Receive the metadata from sessiond */ | |
2009 | ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg, | |
2010 | sizeof(msg)); | |
2011 | if (ret != sizeof(msg)) { | |
8fd623e0 | 2012 | DBG("Consumer received unexpected message size %d (expects %zu)", |
331744e3 JD |
2013 | ret, sizeof(msg)); |
2014 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); | |
2015 | /* | |
2016 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
2017 | * since the caller handles this. | |
2018 | */ | |
2019 | goto end; | |
2020 | } | |
2021 | ||
2022 | if (msg.cmd_type == LTTNG_ERR_UND) { | |
2023 | /* No registry found */ | |
2024 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, | |
2025 | ret_code); | |
2026 | ret = 0; | |
2027 | goto end; | |
2028 | } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) { | |
2029 | ERR("Unexpected cmd_type received %d", msg.cmd_type); | |
2030 | ret = -1; | |
2031 | goto end; | |
2032 | } | |
2033 | ||
2034 | len = msg.u.push_metadata.len; | |
2035 | key = msg.u.push_metadata.key; | |
2036 | offset = msg.u.push_metadata.target_offset; | |
2037 | ||
2038 | assert(key == channel->key); | |
2039 | if (len == 0) { | |
2040 | DBG("No new metadata to receive for key %" PRIu64, key); | |
2041 | } | |
2042 | ||
2043 | /* Tell session daemon we are ready to receive the metadata. */ | |
2044 | ret = consumer_send_status_msg(ctx->consumer_metadata_socket, | |
2045 | LTTNG_OK); | |
2046 | if (ret < 0 || len == 0) { | |
2047 | /* | |
2048 | * Somehow, the session daemon is not responding anymore or there is | |
2049 | * nothing to receive. | |
2050 | */ | |
2051 | goto end; | |
2052 | } | |
2053 | ||
2054 | ret_code = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket, | |
5e41ebe1 | 2055 | key, offset, len, channel, timer); |
f2a444f1 DG |
2056 | if (ret_code >= 0) { |
2057 | /* | |
2058 | * Only send the status msg if the sessiond is alive meaning a positive | |
2059 | * ret code. | |
2060 | */ | |
2061 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret_code); | |
2062 | } | |
331744e3 JD |
2063 | ret = 0; |
2064 | ||
2065 | end: | |
75d83e50 | 2066 | pthread_mutex_unlock(&ctx->metadata_socket_lock); |
331744e3 JD |
2067 | return ret; |
2068 | } |