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