Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
00e2e675 | 4 | * 2012 - David Goulet <dgoulet@efficios.com> |
3bd1e081 | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
3bd1e081 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
3bd1e081 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
18 | */ |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
3bd1e081 | 21 | #include <assert.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> | |
28 | #include <sys/types.h> | |
29 | #include <unistd.h> | |
77c7c900 | 30 | #include <inttypes.h> |
331744e3 | 31 | #include <signal.h> |
3bd1e081 | 32 | |
51a9e1c7 | 33 | #include <bin/lttng-consumerd/health-consumerd.h> |
990570ed | 34 | #include <common/common.h> |
fb3a43a9 DG |
35 | #include <common/utils.h> |
36 | #include <common/compat/poll.h> | |
f263b7fd | 37 | #include <common/compat/endian.h> |
309167d2 | 38 | #include <common/index/index.h> |
10a8a223 | 39 | #include <common/kernel-ctl/kernel-ctl.h> |
00e2e675 | 40 | #include <common/sessiond-comm/relayd.h> |
10a8a223 DG |
41 | #include <common/sessiond-comm/sessiond-comm.h> |
42 | #include <common/kernel-consumer/kernel-consumer.h> | |
00e2e675 | 43 | #include <common/relayd/relayd.h> |
10a8a223 | 44 | #include <common/ust-consumer/ust-consumer.h> |
c8fea79c JR |
45 | #include <common/consumer/consumer-timer.h> |
46 | #include <common/consumer/consumer.h> | |
47 | #include <common/consumer/consumer-stream.h> | |
48 | #include <common/consumer/consumer-testpoint.h> | |
49 | #include <common/align.h> | |
5feafd41 | 50 | #include <common/consumer/consumer-metadata-cache.h> |
3bd1e081 MD |
51 | |
52 | struct lttng_consumer_global_data consumer_data = { | |
3bd1e081 MD |
53 | .stream_count = 0, |
54 | .need_update = 1, | |
55 | .type = LTTNG_CONSUMER_UNKNOWN, | |
56 | }; | |
57 | ||
d8ef542d MD |
58 | enum consumer_channel_action { |
59 | CONSUMER_CHANNEL_ADD, | |
a0cbdd2e | 60 | CONSUMER_CHANNEL_DEL, |
d8ef542d MD |
61 | CONSUMER_CHANNEL_QUIT, |
62 | }; | |
63 | ||
64 | struct consumer_channel_msg { | |
65 | enum consumer_channel_action action; | |
a0cbdd2e MD |
66 | struct lttng_consumer_channel *chan; /* add */ |
67 | uint64_t key; /* del */ | |
d8ef542d MD |
68 | }; |
69 | ||
80957876 | 70 | /* Flag used to temporarily pause data consumption from testpoints. */ |
cf0bcb51 JG |
71 | int data_consumption_paused; |
72 | ||
3bd1e081 MD |
73 | /* |
74 | * Flag to inform the polling thread to quit when all fd hung up. Updated by | |
75 | * the consumer_thread_receive_fds when it notices that all fds has hung up. | |
76 | * Also updated by the signal handler (consumer_should_exit()). Read by the | |
77 | * polling threads. | |
78 | */ | |
10211f5c | 79 | int consumer_quit; |
3bd1e081 | 80 | |
43c34bc3 | 81 | /* |
43c34bc3 DG |
82 | * Global hash table containing respectively metadata and data streams. The |
83 | * stream element in this ht should only be updated by the metadata poll thread | |
84 | * for the metadata and the data poll thread for the data. | |
85 | */ | |
40dc48e0 DG |
86 | static struct lttng_ht *metadata_ht; |
87 | static struct lttng_ht *data_ht; | |
43c34bc3 | 88 | |
acdb9057 DG |
89 | /* |
90 | * Notify a thread lttng pipe to poll back again. This usually means that some | |
91 | * global state has changed so we just send back the thread in a poll wait | |
92 | * call. | |
93 | */ | |
94 | static void notify_thread_lttng_pipe(struct lttng_pipe *pipe) | |
95 | { | |
96 | struct lttng_consumer_stream *null_stream = NULL; | |
97 | ||
98 | assert(pipe); | |
99 | ||
100 | (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream)); | |
101 | } | |
102 | ||
5c635c72 MD |
103 | static void notify_health_quit_pipe(int *pipe) |
104 | { | |
6cd525e8 | 105 | ssize_t ret; |
5c635c72 | 106 | |
6cd525e8 MD |
107 | ret = lttng_write(pipe[1], "4", 1); |
108 | if (ret < 1) { | |
5c635c72 MD |
109 | PERROR("write consumer health quit"); |
110 | } | |
111 | } | |
112 | ||
d8ef542d MD |
113 | static void notify_channel_pipe(struct lttng_consumer_local_data *ctx, |
114 | struct lttng_consumer_channel *chan, | |
a0cbdd2e | 115 | uint64_t key, |
d8ef542d MD |
116 | enum consumer_channel_action action) |
117 | { | |
118 | struct consumer_channel_msg msg; | |
6cd525e8 | 119 | ssize_t ret; |
d8ef542d | 120 | |
e56251fc DG |
121 | memset(&msg, 0, sizeof(msg)); |
122 | ||
d8ef542d MD |
123 | msg.action = action; |
124 | msg.chan = chan; | |
f21dae48 | 125 | msg.key = key; |
6cd525e8 MD |
126 | ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg)); |
127 | if (ret < sizeof(msg)) { | |
128 | PERROR("notify_channel_pipe write error"); | |
129 | } | |
d8ef542d MD |
130 | } |
131 | ||
a0cbdd2e MD |
132 | void notify_thread_del_channel(struct lttng_consumer_local_data *ctx, |
133 | uint64_t key) | |
134 | { | |
135 | notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL); | |
136 | } | |
137 | ||
d8ef542d MD |
138 | static int read_channel_pipe(struct lttng_consumer_local_data *ctx, |
139 | struct lttng_consumer_channel **chan, | |
a0cbdd2e | 140 | uint64_t *key, |
d8ef542d MD |
141 | enum consumer_channel_action *action) |
142 | { | |
143 | struct consumer_channel_msg msg; | |
6cd525e8 | 144 | ssize_t ret; |
d8ef542d | 145 | |
6cd525e8 MD |
146 | ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg)); |
147 | if (ret < sizeof(msg)) { | |
148 | ret = -1; | |
149 | goto error; | |
d8ef542d | 150 | } |
6cd525e8 MD |
151 | *action = msg.action; |
152 | *chan = msg.chan; | |
153 | *key = msg.key; | |
154 | error: | |
155 | return (int) ret; | |
d8ef542d MD |
156 | } |
157 | ||
212d67a2 DG |
158 | /* |
159 | * Cleanup the stream list of a channel. Those streams are not yet globally | |
160 | * visible | |
161 | */ | |
162 | static void clean_channel_stream_list(struct lttng_consumer_channel *channel) | |
163 | { | |
164 | struct lttng_consumer_stream *stream, *stmp; | |
165 | ||
166 | assert(channel); | |
167 | ||
168 | /* Delete streams that might have been left in the stream list. */ | |
169 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
170 | send_node) { | |
171 | cds_list_del(&stream->send_node); | |
172 | /* | |
173 | * Once a stream is added to this list, the buffers were created so we | |
174 | * have a guarantee that this call will succeed. Setting the monitor | |
175 | * mode to 0 so we don't lock nor try to delete the stream from the | |
176 | * global hash table. | |
177 | */ | |
178 | stream->monitor = 0; | |
179 | consumer_stream_destroy(stream, NULL); | |
180 | } | |
181 | } | |
182 | ||
3bd1e081 MD |
183 | /* |
184 | * Find a stream. The consumer_data.lock must be locked during this | |
185 | * call. | |
186 | */ | |
d88aee68 | 187 | static struct lttng_consumer_stream *find_stream(uint64_t key, |
8389e4f8 | 188 | struct lttng_ht *ht) |
3bd1e081 | 189 | { |
e4421fec | 190 | struct lttng_ht_iter iter; |
d88aee68 | 191 | struct lttng_ht_node_u64 *node; |
e4421fec | 192 | struct lttng_consumer_stream *stream = NULL; |
3bd1e081 | 193 | |
8389e4f8 DG |
194 | assert(ht); |
195 | ||
d88aee68 DG |
196 | /* -1ULL keys are lookup failures */ |
197 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 198 | return NULL; |
7a57cf92 | 199 | } |
e4421fec | 200 | |
6065ceec DG |
201 | rcu_read_lock(); |
202 | ||
d88aee68 DG |
203 | lttng_ht_lookup(ht, &key, &iter); |
204 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
205 | if (node != NULL) { |
206 | stream = caa_container_of(node, struct lttng_consumer_stream, node); | |
3bd1e081 | 207 | } |
e4421fec | 208 | |
6065ceec DG |
209 | rcu_read_unlock(); |
210 | ||
e4421fec | 211 | return stream; |
3bd1e081 MD |
212 | } |
213 | ||
da009f2c | 214 | static void steal_stream_key(uint64_t key, struct lttng_ht *ht) |
7ad0a0cb MD |
215 | { |
216 | struct lttng_consumer_stream *stream; | |
217 | ||
04253271 | 218 | rcu_read_lock(); |
ffe60014 | 219 | stream = find_stream(key, ht); |
04253271 | 220 | if (stream) { |
da009f2c | 221 | stream->key = (uint64_t) -1ULL; |
04253271 MD |
222 | /* |
223 | * We don't want the lookup to match, but we still need | |
224 | * to iterate on this stream when iterating over the hash table. Just | |
225 | * change the node key. | |
226 | */ | |
da009f2c | 227 | stream->node.key = (uint64_t) -1ULL; |
04253271 MD |
228 | } |
229 | rcu_read_unlock(); | |
7ad0a0cb MD |
230 | } |
231 | ||
d56db448 DG |
232 | /* |
233 | * Return a channel object for the given key. | |
234 | * | |
235 | * RCU read side lock MUST be acquired before calling this function and | |
236 | * protects the channel ptr. | |
237 | */ | |
d88aee68 | 238 | struct lttng_consumer_channel *consumer_find_channel(uint64_t key) |
3bd1e081 | 239 | { |
e4421fec | 240 | struct lttng_ht_iter iter; |
d88aee68 | 241 | struct lttng_ht_node_u64 *node; |
e4421fec | 242 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 | 243 | |
d88aee68 DG |
244 | /* -1ULL keys are lookup failures */ |
245 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 246 | return NULL; |
7a57cf92 | 247 | } |
e4421fec | 248 | |
d88aee68 DG |
249 | lttng_ht_lookup(consumer_data.channel_ht, &key, &iter); |
250 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
251 | if (node != NULL) { |
252 | channel = caa_container_of(node, struct lttng_consumer_channel, node); | |
3bd1e081 | 253 | } |
e4421fec DG |
254 | |
255 | return channel; | |
3bd1e081 MD |
256 | } |
257 | ||
b5a6470f DG |
258 | /* |
259 | * There is a possibility that the consumer does not have enough time between | |
260 | * the close of the channel on the session daemon and the cleanup in here thus | |
261 | * once we have a channel add with an existing key, we know for sure that this | |
262 | * channel will eventually get cleaned up by all streams being closed. | |
263 | * | |
264 | * This function just nullifies the already existing channel key. | |
265 | */ | |
266 | static void steal_channel_key(uint64_t key) | |
267 | { | |
268 | struct lttng_consumer_channel *channel; | |
269 | ||
270 | rcu_read_lock(); | |
271 | channel = consumer_find_channel(key); | |
272 | if (channel) { | |
273 | channel->key = (uint64_t) -1ULL; | |
274 | /* | |
275 | * We don't want the lookup to match, but we still need to iterate on | |
276 | * this channel when iterating over the hash table. Just change the | |
277 | * node key. | |
278 | */ | |
279 | channel->node.key = (uint64_t) -1ULL; | |
280 | } | |
281 | rcu_read_unlock(); | |
282 | } | |
283 | ||
ffe60014 | 284 | static void free_channel_rcu(struct rcu_head *head) |
702b1ea4 | 285 | { |
d88aee68 DG |
286 | struct lttng_ht_node_u64 *node = |
287 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
ffe60014 DG |
288 | struct lttng_consumer_channel *channel = |
289 | caa_container_of(node, struct lttng_consumer_channel, node); | |
702b1ea4 | 290 | |
b83e03c4 MD |
291 | switch (consumer_data.type) { |
292 | case LTTNG_CONSUMER_KERNEL: | |
293 | break; | |
294 | case LTTNG_CONSUMER32_UST: | |
295 | case LTTNG_CONSUMER64_UST: | |
296 | lttng_ustconsumer_free_channel(channel); | |
297 | break; | |
298 | default: | |
299 | ERR("Unknown consumer_data type"); | |
300 | abort(); | |
301 | } | |
ffe60014 | 302 | free(channel); |
702b1ea4 MD |
303 | } |
304 | ||
00e2e675 DG |
305 | /* |
306 | * RCU protected relayd socket pair free. | |
307 | */ | |
ffe60014 | 308 | static void free_relayd_rcu(struct rcu_head *head) |
00e2e675 | 309 | { |
d88aee68 DG |
310 | struct lttng_ht_node_u64 *node = |
311 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
00e2e675 DG |
312 | struct consumer_relayd_sock_pair *relayd = |
313 | caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
314 | ||
8994307f DG |
315 | /* |
316 | * Close all sockets. This is done in the call RCU since we don't want the | |
317 | * socket fds to be reassigned thus potentially creating bad state of the | |
318 | * relayd object. | |
319 | * | |
320 | * We do not have to lock the control socket mutex here since at this stage | |
321 | * there is no one referencing to this relayd object. | |
322 | */ | |
323 | (void) relayd_close(&relayd->control_sock); | |
324 | (void) relayd_close(&relayd->data_sock); | |
325 | ||
70053766 | 326 | pthread_mutex_destroy(&relayd->ctrl_sock_mutex); |
00e2e675 DG |
327 | free(relayd); |
328 | } | |
329 | ||
330 | /* | |
331 | * Destroy and free relayd socket pair object. | |
00e2e675 | 332 | */ |
51230d70 | 333 | void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
334 | { |
335 | int ret; | |
336 | struct lttng_ht_iter iter; | |
337 | ||
173af62f DG |
338 | if (relayd == NULL) { |
339 | return; | |
340 | } | |
341 | ||
00e2e675 DG |
342 | DBG("Consumer destroy and close relayd socket pair"); |
343 | ||
344 | iter.iter.node = &relayd->node.node; | |
345 | ret = lttng_ht_del(consumer_data.relayd_ht, &iter); | |
173af62f | 346 | if (ret != 0) { |
8994307f | 347 | /* We assume the relayd is being or is destroyed */ |
173af62f DG |
348 | return; |
349 | } | |
00e2e675 | 350 | |
00e2e675 | 351 | /* RCU free() call */ |
ffe60014 DG |
352 | call_rcu(&relayd->node.head, free_relayd_rcu); |
353 | } | |
354 | ||
355 | /* | |
356 | * Remove a channel from the global list protected by a mutex. This function is | |
357 | * also responsible for freeing its data structures. | |
358 | */ | |
359 | void consumer_del_channel(struct lttng_consumer_channel *channel) | |
360 | { | |
361 | int ret; | |
362 | struct lttng_ht_iter iter; | |
363 | ||
d88aee68 | 364 | DBG("Consumer delete channel key %" PRIu64, channel->key); |
ffe60014 DG |
365 | |
366 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 367 | pthread_mutex_lock(&channel->lock); |
ffe60014 | 368 | |
212d67a2 DG |
369 | /* Destroy streams that might have been left in the stream list. */ |
370 | clean_channel_stream_list(channel); | |
51e762e5 | 371 | |
d3e2ba59 JD |
372 | if (channel->live_timer_enabled == 1) { |
373 | consumer_timer_live_stop(channel); | |
374 | } | |
e9404c27 JG |
375 | if (channel->monitor_timer_enabled == 1) { |
376 | consumer_timer_monitor_stop(channel); | |
377 | } | |
d3e2ba59 | 378 | |
ffe60014 DG |
379 | switch (consumer_data.type) { |
380 | case LTTNG_CONSUMER_KERNEL: | |
381 | break; | |
382 | case LTTNG_CONSUMER32_UST: | |
383 | case LTTNG_CONSUMER64_UST: | |
384 | lttng_ustconsumer_del_channel(channel); | |
385 | break; | |
386 | default: | |
387 | ERR("Unknown consumer_data type"); | |
388 | assert(0); | |
389 | goto end; | |
390 | } | |
391 | ||
392 | rcu_read_lock(); | |
393 | iter.iter.node = &channel->node.node; | |
394 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); | |
395 | assert(!ret); | |
0fc67d9b JG |
396 | |
397 | iter.iter.node = &channel->channels_by_session_id_ht_node.node; | |
398 | ret = lttng_ht_del(consumer_data.channels_by_session_id_ht, &iter); | |
399 | assert(!ret); | |
ffe60014 DG |
400 | rcu_read_unlock(); |
401 | ||
402 | call_rcu(&channel->node.head, free_channel_rcu); | |
403 | end: | |
a9838785 | 404 | pthread_mutex_unlock(&channel->lock); |
ffe60014 | 405 | pthread_mutex_unlock(&consumer_data.lock); |
00e2e675 DG |
406 | } |
407 | ||
228b5bf7 DG |
408 | /* |
409 | * Iterate over the relayd hash table and destroy each element. Finally, | |
410 | * destroy the whole hash table. | |
411 | */ | |
412 | static void cleanup_relayd_ht(void) | |
413 | { | |
414 | struct lttng_ht_iter iter; | |
415 | struct consumer_relayd_sock_pair *relayd; | |
416 | ||
417 | rcu_read_lock(); | |
418 | ||
419 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
420 | node.node) { | |
51230d70 | 421 | consumer_destroy_relayd(relayd); |
228b5bf7 DG |
422 | } |
423 | ||
228b5bf7 | 424 | rcu_read_unlock(); |
36b588ed MD |
425 | |
426 | lttng_ht_destroy(consumer_data.relayd_ht); | |
228b5bf7 DG |
427 | } |
428 | ||
8994307f DG |
429 | /* |
430 | * Update the end point status of all streams having the given network sequence | |
431 | * index (relayd index). | |
432 | * | |
433 | * It's atomically set without having the stream mutex locked which is fine | |
434 | * because we handle the write/read race with a pipe wakeup for each thread. | |
435 | */ | |
da009f2c | 436 | static void update_endpoint_status_by_netidx(uint64_t net_seq_idx, |
8994307f DG |
437 | enum consumer_endpoint_status status) |
438 | { | |
439 | struct lttng_ht_iter iter; | |
440 | struct lttng_consumer_stream *stream; | |
441 | ||
da009f2c | 442 | DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx); |
8994307f DG |
443 | |
444 | rcu_read_lock(); | |
445 | ||
446 | /* Let's begin with metadata */ | |
447 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
448 | if (stream->net_seq_idx == net_seq_idx) { | |
449 | uatomic_set(&stream->endpoint_status, status); | |
450 | DBG("Delete flag set to metadata stream %d", stream->wait_fd); | |
451 | } | |
452 | } | |
453 | ||
454 | /* Follow up by the data streams */ | |
455 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
456 | if (stream->net_seq_idx == net_seq_idx) { | |
457 | uatomic_set(&stream->endpoint_status, status); | |
458 | DBG("Delete flag set to data stream %d", stream->wait_fd); | |
459 | } | |
460 | } | |
461 | rcu_read_unlock(); | |
462 | } | |
463 | ||
464 | /* | |
465 | * Cleanup a relayd object by flagging every associated streams for deletion, | |
466 | * destroying the object meaning removing it from the relayd hash table, | |
467 | * closing the sockets and freeing the memory in a RCU call. | |
468 | * | |
469 | * If a local data context is available, notify the threads that the streams' | |
470 | * state have changed. | |
471 | */ | |
11413bb9 | 472 | void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd) |
8994307f | 473 | { |
da009f2c | 474 | uint64_t netidx; |
8994307f DG |
475 | |
476 | assert(relayd); | |
477 | ||
11413bb9 | 478 | DBG("Cleaning up relayd object ID %"PRIu64, relayd->net_seq_idx); |
9617607b | 479 | |
8994307f DG |
480 | /* Save the net sequence index before destroying the object */ |
481 | netidx = relayd->net_seq_idx; | |
482 | ||
483 | /* | |
484 | * Delete the relayd from the relayd hash table, close the sockets and free | |
485 | * the object in a RCU call. | |
486 | */ | |
51230d70 | 487 | consumer_destroy_relayd(relayd); |
8994307f DG |
488 | |
489 | /* Set inactive endpoint to all streams */ | |
490 | update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE); | |
491 | ||
492 | /* | |
493 | * With a local data context, notify the threads that the streams' state | |
494 | * have changed. The write() action on the pipe acts as an "implicit" | |
495 | * memory barrier ordering the updates of the end point status from the | |
496 | * read of this status which happens AFTER receiving this notify. | |
497 | */ | |
11413bb9 JR |
498 | notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe); |
499 | notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe); | |
8994307f DG |
500 | } |
501 | ||
a6ba4fe1 DG |
502 | /* |
503 | * Flag a relayd socket pair for destruction. Destroy it if the refcount | |
504 | * reaches zero. | |
505 | * | |
506 | * RCU read side lock MUST be aquired before calling this function. | |
507 | */ | |
508 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) | |
509 | { | |
510 | assert(relayd); | |
511 | ||
512 | /* Set destroy flag for this object */ | |
513 | uatomic_set(&relayd->destroy_flag, 1); | |
514 | ||
515 | /* Destroy the relayd if refcount is 0 */ | |
516 | if (uatomic_read(&relayd->refcount) == 0) { | |
51230d70 | 517 | consumer_destroy_relayd(relayd); |
a6ba4fe1 DG |
518 | } |
519 | } | |
520 | ||
3bd1e081 | 521 | /* |
1d1a276c DG |
522 | * Completly destroy stream from every visiable data structure and the given |
523 | * hash table if one. | |
524 | * | |
525 | * One this call returns, the stream object is not longer usable nor visible. | |
3bd1e081 | 526 | */ |
e316aad5 DG |
527 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
528 | struct lttng_ht *ht) | |
3bd1e081 | 529 | { |
1d1a276c | 530 | consumer_stream_destroy(stream, ht); |
3bd1e081 MD |
531 | } |
532 | ||
5ab66908 MD |
533 | /* |
534 | * XXX naming of del vs destroy is all mixed up. | |
535 | */ | |
536 | void consumer_del_stream_for_data(struct lttng_consumer_stream *stream) | |
537 | { | |
538 | consumer_stream_destroy(stream, data_ht); | |
539 | } | |
540 | ||
541 | void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream) | |
542 | { | |
543 | consumer_stream_destroy(stream, metadata_ht); | |
544 | } | |
545 | ||
d9a2e16e JD |
546 | void consumer_stream_update_channel_attributes( |
547 | struct lttng_consumer_stream *stream, | |
548 | struct lttng_consumer_channel *channel) | |
549 | { | |
550 | stream->channel_read_only_attributes.tracefile_size = | |
551 | channel->tracefile_size; | |
552 | memcpy(stream->channel_read_only_attributes.path, channel->pathname, | |
553 | sizeof(stream->channel_read_only_attributes.path)); | |
554 | } | |
555 | ||
d88aee68 DG |
556 | struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key, |
557 | uint64_t stream_key, | |
3bd1e081 | 558 | enum lttng_consumer_stream_state state, |
ffe60014 | 559 | const char *channel_name, |
6df2e2c9 | 560 | uid_t uid, |
00e2e675 | 561 | gid_t gid, |
57a269f2 | 562 | uint64_t relayd_id, |
53632229 | 563 | uint64_t session_id, |
ffe60014 DG |
564 | int cpu, |
565 | int *alloc_ret, | |
4891ece8 | 566 | enum consumer_channel_type type, |
e098433c JG |
567 | unsigned int monitor, |
568 | uint64_t trace_archive_id) | |
3bd1e081 | 569 | { |
ffe60014 | 570 | int ret; |
3bd1e081 | 571 | struct lttng_consumer_stream *stream; |
3bd1e081 | 572 | |
effcf122 | 573 | stream = zmalloc(sizeof(*stream)); |
3bd1e081 | 574 | if (stream == NULL) { |
7a57cf92 | 575 | PERROR("malloc struct lttng_consumer_stream"); |
ffe60014 | 576 | ret = -ENOMEM; |
7a57cf92 | 577 | goto end; |
3bd1e081 | 578 | } |
7a57cf92 | 579 | |
d56db448 DG |
580 | rcu_read_lock(); |
581 | ||
3bd1e081 | 582 | stream->key = stream_key; |
3bd1e081 MD |
583 | stream->out_fd = -1; |
584 | stream->out_fd_offset = 0; | |
e5d1a9b3 | 585 | stream->output_written = 0; |
3bd1e081 | 586 | stream->state = state; |
6df2e2c9 MD |
587 | stream->uid = uid; |
588 | stream->gid = gid; | |
ffe60014 | 589 | stream->net_seq_idx = relayd_id; |
53632229 | 590 | stream->session_id = session_id; |
4891ece8 | 591 | stream->monitor = monitor; |
774d490c | 592 | stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE; |
f8f3885c | 593 | stream->index_file = NULL; |
fb83fe64 | 594 | stream->last_sequence_number = -1ULL; |
e098433c | 595 | stream->trace_archive_id = trace_archive_id; |
53632229 | 596 | pthread_mutex_init(&stream->lock, NULL); |
c585821b | 597 | pthread_mutex_init(&stream->metadata_timer_lock, NULL); |
58b1f425 | 598 | |
ffe60014 DG |
599 | /* If channel is the metadata, flag this stream as metadata. */ |
600 | if (type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
601 | stream->metadata_flag = 1; | |
602 | /* Metadata is flat out. */ | |
603 | strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name)); | |
94d49140 JD |
604 | /* Live rendez-vous point. */ |
605 | pthread_cond_init(&stream->metadata_rdv, NULL); | |
606 | pthread_mutex_init(&stream->metadata_rdv_lock, NULL); | |
58b1f425 | 607 | } else { |
ffe60014 DG |
608 | /* Format stream name to <channel_name>_<cpu_number> */ |
609 | ret = snprintf(stream->name, sizeof(stream->name), "%s_%d", | |
610 | channel_name, cpu); | |
611 | if (ret < 0) { | |
612 | PERROR("snprintf stream name"); | |
613 | goto error; | |
614 | } | |
58b1f425 | 615 | } |
c30aaa51 | 616 | |
ffe60014 | 617 | /* Key is always the wait_fd for streams. */ |
d88aee68 | 618 | lttng_ht_node_init_u64(&stream->node, stream->key); |
ffe60014 | 619 | |
d8ef542d MD |
620 | /* Init node per channel id key */ |
621 | lttng_ht_node_init_u64(&stream->node_channel_id, channel_key); | |
622 | ||
53632229 | 623 | /* Init session id node with the stream session id */ |
d88aee68 | 624 | lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id); |
53632229 | 625 | |
07b86b52 JD |
626 | DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64 |
627 | " relayd_id %" PRIu64 ", session_id %" PRIu64, | |
628 | stream->name, stream->key, channel_key, | |
629 | stream->net_seq_idx, stream->session_id); | |
d56db448 DG |
630 | |
631 | rcu_read_unlock(); | |
3bd1e081 | 632 | return stream; |
c80048c6 MD |
633 | |
634 | error: | |
d56db448 | 635 | rcu_read_unlock(); |
c80048c6 | 636 | free(stream); |
7a57cf92 | 637 | end: |
ffe60014 DG |
638 | if (alloc_ret) { |
639 | *alloc_ret = ret; | |
640 | } | |
c80048c6 | 641 | return NULL; |
3bd1e081 MD |
642 | } |
643 | ||
644 | /* | |
645 | * Add a stream to the global list protected by a mutex. | |
646 | */ | |
66d583dc | 647 | void consumer_add_data_stream(struct lttng_consumer_stream *stream) |
3bd1e081 | 648 | { |
5ab66908 | 649 | struct lttng_ht *ht = data_ht; |
3bd1e081 | 650 | |
e316aad5 | 651 | assert(stream); |
43c34bc3 | 652 | assert(ht); |
c77fc10a | 653 | |
d88aee68 | 654 | DBG3("Adding consumer stream %" PRIu64, stream->key); |
e316aad5 DG |
655 | |
656 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 657 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 658 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 659 | pthread_mutex_lock(&stream->lock); |
b0b335c8 | 660 | rcu_read_lock(); |
e316aad5 | 661 | |
43c34bc3 | 662 | /* Steal stream identifier to avoid having streams with the same key */ |
ffe60014 | 663 | steal_stream_key(stream->key, ht); |
43c34bc3 | 664 | |
d88aee68 | 665 | lttng_ht_add_unique_u64(ht, &stream->node); |
00e2e675 | 666 | |
d8ef542d MD |
667 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
668 | &stream->node_channel_id); | |
669 | ||
ca22feea DG |
670 | /* |
671 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
672 | * the key since the HT does not use it and we allow to add redundant keys | |
673 | * into this table. | |
674 | */ | |
d88aee68 | 675 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 676 | |
e316aad5 | 677 | /* |
ffe60014 DG |
678 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
679 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
680 | * causes the count to become 0 also causes a stream to be added. The |
681 | * channel deletion will thus be triggered by the following removal of this | |
682 | * stream. | |
683 | */ | |
ffe60014 | 684 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
685 | /* Increment refcount before decrementing nb_init_stream_left */ |
686 | cmm_smp_wmb(); | |
ffe60014 | 687 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
688 | } |
689 | ||
690 | /* Update consumer data once the node is inserted. */ | |
3bd1e081 MD |
691 | consumer_data.stream_count++; |
692 | consumer_data.need_update = 1; | |
693 | ||
e316aad5 | 694 | rcu_read_unlock(); |
2e818a6a | 695 | pthread_mutex_unlock(&stream->lock); |
ec6ea7d0 | 696 | pthread_mutex_unlock(&stream->chan->timer_lock); |
a9838785 | 697 | pthread_mutex_unlock(&stream->chan->lock); |
3bd1e081 | 698 | pthread_mutex_unlock(&consumer_data.lock); |
3bd1e081 MD |
699 | } |
700 | ||
5ab66908 MD |
701 | void consumer_del_data_stream(struct lttng_consumer_stream *stream) |
702 | { | |
703 | consumer_del_stream(stream, data_ht); | |
704 | } | |
705 | ||
00e2e675 | 706 | /* |
3f8e211f DG |
707 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
708 | * be acquired before calling this. | |
00e2e675 | 709 | */ |
d09e1200 | 710 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
711 | { |
712 | int ret = 0; | |
d88aee68 | 713 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
714 | struct lttng_ht_iter iter; |
715 | ||
ffe60014 | 716 | assert(relayd); |
00e2e675 | 717 | |
00e2e675 | 718 | lttng_ht_lookup(consumer_data.relayd_ht, |
d88aee68 DG |
719 | &relayd->net_seq_idx, &iter); |
720 | node = lttng_ht_iter_get_node_u64(&iter); | |
00e2e675 | 721 | if (node != NULL) { |
00e2e675 DG |
722 | goto end; |
723 | } | |
d88aee68 | 724 | lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node); |
00e2e675 | 725 | |
00e2e675 DG |
726 | end: |
727 | return ret; | |
728 | } | |
729 | ||
730 | /* | |
731 | * Allocate and return a consumer relayd socket. | |
732 | */ | |
027a694f | 733 | static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
da009f2c | 734 | uint64_t net_seq_idx) |
00e2e675 DG |
735 | { |
736 | struct consumer_relayd_sock_pair *obj = NULL; | |
737 | ||
da009f2c MD |
738 | /* net sequence index of -1 is a failure */ |
739 | if (net_seq_idx == (uint64_t) -1ULL) { | |
00e2e675 DG |
740 | goto error; |
741 | } | |
742 | ||
743 | obj = zmalloc(sizeof(struct consumer_relayd_sock_pair)); | |
744 | if (obj == NULL) { | |
745 | PERROR("zmalloc relayd sock"); | |
746 | goto error; | |
747 | } | |
748 | ||
749 | obj->net_seq_idx = net_seq_idx; | |
750 | obj->refcount = 0; | |
173af62f | 751 | obj->destroy_flag = 0; |
f96e4545 MD |
752 | obj->control_sock.sock.fd = -1; |
753 | obj->data_sock.sock.fd = -1; | |
d88aee68 | 754 | lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx); |
00e2e675 DG |
755 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
756 | ||
757 | error: | |
758 | return obj; | |
759 | } | |
760 | ||
761 | /* | |
762 | * Find a relayd socket pair in the global consumer data. | |
763 | * | |
764 | * Return the object if found else NULL. | |
b0b335c8 MD |
765 | * RCU read-side lock must be held across this call and while using the |
766 | * returned object. | |
00e2e675 | 767 | */ |
d88aee68 | 768 | struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key) |
00e2e675 DG |
769 | { |
770 | struct lttng_ht_iter iter; | |
d88aee68 | 771 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
772 | struct consumer_relayd_sock_pair *relayd = NULL; |
773 | ||
774 | /* Negative keys are lookup failures */ | |
d88aee68 | 775 | if (key == (uint64_t) -1ULL) { |
00e2e675 DG |
776 | goto error; |
777 | } | |
778 | ||
d88aee68 | 779 | lttng_ht_lookup(consumer_data.relayd_ht, &key, |
00e2e675 | 780 | &iter); |
d88aee68 | 781 | node = lttng_ht_iter_get_node_u64(&iter); |
00e2e675 DG |
782 | if (node != NULL) { |
783 | relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
784 | } | |
785 | ||
00e2e675 DG |
786 | error: |
787 | return relayd; | |
788 | } | |
789 | ||
10a50311 JD |
790 | /* |
791 | * Find a relayd and send the stream | |
792 | * | |
793 | * Returns 0 on success, < 0 on error | |
794 | */ | |
795 | int consumer_send_relayd_stream(struct lttng_consumer_stream *stream, | |
796 | char *path) | |
797 | { | |
798 | int ret = 0; | |
799 | struct consumer_relayd_sock_pair *relayd; | |
800 | ||
801 | assert(stream); | |
802 | assert(stream->net_seq_idx != -1ULL); | |
803 | assert(path); | |
804 | ||
805 | /* The stream is not metadata. Get relayd reference if exists. */ | |
806 | rcu_read_lock(); | |
807 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
808 | if (relayd != NULL) { | |
809 | /* Add stream on the relayd */ | |
810 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
811 | ret = relayd_add_stream(&relayd->control_sock, stream->name, | |
812 | path, &stream->relayd_stream_id, | |
0b50e4b3 JG |
813 | stream->chan->tracefile_size, stream->chan->tracefile_count, |
814 | stream->trace_archive_id); | |
10a50311 JD |
815 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
816 | if (ret < 0) { | |
11413bb9 JR |
817 | ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
818 | lttng_consumer_cleanup_relayd(relayd); | |
10a50311 JD |
819 | goto end; |
820 | } | |
1c20f0e2 | 821 | |
10a50311 | 822 | uatomic_inc(&relayd->refcount); |
d01178b6 | 823 | stream->sent_to_relayd = 1; |
10a50311 JD |
824 | } else { |
825 | ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.", | |
826 | stream->key, stream->net_seq_idx); | |
827 | ret = -1; | |
828 | goto end; | |
829 | } | |
830 | ||
831 | DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64, | |
832 | stream->name, stream->key, stream->net_seq_idx); | |
833 | ||
834 | end: | |
835 | rcu_read_unlock(); | |
836 | return ret; | |
837 | } | |
838 | ||
a4baae1b JD |
839 | /* |
840 | * Find a relayd and send the streams sent message | |
841 | * | |
842 | * Returns 0 on success, < 0 on error | |
843 | */ | |
844 | int consumer_send_relayd_streams_sent(uint64_t net_seq_idx) | |
845 | { | |
846 | int ret = 0; | |
847 | struct consumer_relayd_sock_pair *relayd; | |
848 | ||
849 | assert(net_seq_idx != -1ULL); | |
850 | ||
851 | /* The stream is not metadata. Get relayd reference if exists. */ | |
852 | rcu_read_lock(); | |
853 | relayd = consumer_find_relayd(net_seq_idx); | |
854 | if (relayd != NULL) { | |
855 | /* Add stream on the relayd */ | |
856 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
857 | ret = relayd_streams_sent(&relayd->control_sock); | |
858 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
859 | if (ret < 0) { | |
11413bb9 JR |
860 | ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
861 | lttng_consumer_cleanup_relayd(relayd); | |
a4baae1b JD |
862 | goto end; |
863 | } | |
864 | } else { | |
865 | ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.", | |
866 | net_seq_idx); | |
867 | ret = -1; | |
868 | goto end; | |
869 | } | |
870 | ||
871 | ret = 0; | |
872 | DBG("All streams sent relayd id %" PRIu64, net_seq_idx); | |
873 | ||
874 | end: | |
875 | rcu_read_unlock(); | |
876 | return ret; | |
877 | } | |
878 | ||
10a50311 JD |
879 | /* |
880 | * Find a relayd and close the stream | |
881 | */ | |
882 | void close_relayd_stream(struct lttng_consumer_stream *stream) | |
883 | { | |
884 | struct consumer_relayd_sock_pair *relayd; | |
885 | ||
886 | /* The stream is not metadata. Get relayd reference if exists. */ | |
887 | rcu_read_lock(); | |
888 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
889 | if (relayd) { | |
890 | consumer_stream_relayd_close(stream, relayd); | |
891 | } | |
892 | rcu_read_unlock(); | |
893 | } | |
894 | ||
00e2e675 DG |
895 | /* |
896 | * Handle stream for relayd transmission if the stream applies for network | |
897 | * streaming where the net sequence index is set. | |
898 | * | |
899 | * Return destination file descriptor or negative value on error. | |
900 | */ | |
6197aea7 | 901 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
1d4dfdef DG |
902 | size_t data_size, unsigned long padding, |
903 | struct consumer_relayd_sock_pair *relayd) | |
00e2e675 DG |
904 | { |
905 | int outfd = -1, ret; | |
00e2e675 DG |
906 | struct lttcomm_relayd_data_hdr data_hdr; |
907 | ||
908 | /* Safety net */ | |
909 | assert(stream); | |
6197aea7 | 910 | assert(relayd); |
00e2e675 DG |
911 | |
912 | /* Reset data header */ | |
913 | memset(&data_hdr, 0, sizeof(data_hdr)); | |
914 | ||
00e2e675 DG |
915 | if (stream->metadata_flag) { |
916 | /* Caller MUST acquire the relayd control socket lock */ | |
917 | ret = relayd_send_metadata(&relayd->control_sock, data_size); | |
918 | if (ret < 0) { | |
919 | goto error; | |
920 | } | |
921 | ||
922 | /* Metadata are always sent on the control socket. */ | |
6151a90f | 923 | outfd = relayd->control_sock.sock.fd; |
00e2e675 DG |
924 | } else { |
925 | /* Set header with stream information */ | |
926 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); | |
927 | data_hdr.data_size = htobe32(data_size); | |
1d4dfdef | 928 | data_hdr.padding_size = htobe32(padding); |
39df6d9f DG |
929 | /* |
930 | * Note that net_seq_num below is assigned with the *current* value of | |
931 | * next_net_seq_num and only after that the next_net_seq_num will be | |
932 | * increment. This is why when issuing a command on the relayd using | |
933 | * this next value, 1 should always be substracted in order to compare | |
934 | * the last seen sequence number on the relayd side to the last sent. | |
935 | */ | |
3604f373 | 936 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num); |
00e2e675 DG |
937 | /* Other fields are zeroed previously */ |
938 | ||
939 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, | |
940 | sizeof(data_hdr)); | |
941 | if (ret < 0) { | |
942 | goto error; | |
943 | } | |
944 | ||
3604f373 DG |
945 | ++stream->next_net_seq_num; |
946 | ||
00e2e675 | 947 | /* Set to go on data socket */ |
6151a90f | 948 | outfd = relayd->data_sock.sock.fd; |
00e2e675 DG |
949 | } |
950 | ||
951 | error: | |
952 | return outfd; | |
953 | } | |
954 | ||
3bd1e081 | 955 | /* |
ffe60014 DG |
956 | * Allocate and return a new lttng_consumer_channel object using the given key |
957 | * to initialize the hash table node. | |
958 | * | |
959 | * On error, return NULL. | |
3bd1e081 | 960 | */ |
886224ff | 961 | struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, |
ffe60014 DG |
962 | uint64_t session_id, |
963 | const char *pathname, | |
964 | const char *name, | |
965 | uid_t uid, | |
966 | gid_t gid, | |
57a269f2 | 967 | uint64_t relayd_id, |
1624d5b7 JD |
968 | enum lttng_event_output output, |
969 | uint64_t tracefile_size, | |
2bba9e53 | 970 | uint64_t tracefile_count, |
1950109e | 971 | uint64_t session_id_per_pid, |
ecc48a90 | 972 | unsigned int monitor, |
d7ba1388 | 973 | unsigned int live_timer_interval, |
3d071855 | 974 | const char *root_shm_path, |
d7ba1388 | 975 | const char *shm_path) |
3bd1e081 MD |
976 | { |
977 | struct lttng_consumer_channel *channel; | |
3bd1e081 | 978 | |
276b26d1 | 979 | channel = zmalloc(sizeof(*channel)); |
3bd1e081 | 980 | if (channel == NULL) { |
7a57cf92 | 981 | PERROR("malloc struct lttng_consumer_channel"); |
3bd1e081 MD |
982 | goto end; |
983 | } | |
ffe60014 DG |
984 | |
985 | channel->key = key; | |
3bd1e081 | 986 | channel->refcount = 0; |
ffe60014 | 987 | channel->session_id = session_id; |
1950109e | 988 | channel->session_id_per_pid = session_id_per_pid; |
ffe60014 DG |
989 | channel->uid = uid; |
990 | channel->gid = gid; | |
991 | channel->relayd_id = relayd_id; | |
1624d5b7 JD |
992 | channel->tracefile_size = tracefile_size; |
993 | channel->tracefile_count = tracefile_count; | |
2bba9e53 | 994 | channel->monitor = monitor; |
ecc48a90 | 995 | channel->live_timer_interval = live_timer_interval; |
a9838785 | 996 | pthread_mutex_init(&channel->lock, NULL); |
ec6ea7d0 | 997 | pthread_mutex_init(&channel->timer_lock, NULL); |
ffe60014 | 998 | |
0c759fc9 DG |
999 | switch (output) { |
1000 | case LTTNG_EVENT_SPLICE: | |
1001 | channel->output = CONSUMER_CHANNEL_SPLICE; | |
1002 | break; | |
1003 | case LTTNG_EVENT_MMAP: | |
1004 | channel->output = CONSUMER_CHANNEL_MMAP; | |
1005 | break; | |
1006 | default: | |
1007 | assert(0); | |
1008 | free(channel); | |
1009 | channel = NULL; | |
1010 | goto end; | |
1011 | } | |
1012 | ||
07b86b52 JD |
1013 | /* |
1014 | * In monitor mode, the streams associated with the channel will be put in | |
1015 | * a special list ONLY owned by this channel. So, the refcount is set to 1 | |
1016 | * here meaning that the channel itself has streams that are referenced. | |
1017 | * | |
1018 | * On a channel deletion, once the channel is no longer visible, the | |
1019 | * refcount is decremented and checked for a zero value to delete it. With | |
1020 | * streams in no monitor mode, it will now be safe to destroy the channel. | |
1021 | */ | |
1022 | if (!channel->monitor) { | |
1023 | channel->refcount = 1; | |
1024 | } | |
1025 | ||
ffe60014 DG |
1026 | strncpy(channel->pathname, pathname, sizeof(channel->pathname)); |
1027 | channel->pathname[sizeof(channel->pathname) - 1] = '\0'; | |
1028 | ||
1029 | strncpy(channel->name, name, sizeof(channel->name)); | |
1030 | channel->name[sizeof(channel->name) - 1] = '\0'; | |
1031 | ||
3d071855 MD |
1032 | if (root_shm_path) { |
1033 | strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path)); | |
1034 | channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0'; | |
1035 | } | |
d7ba1388 MD |
1036 | if (shm_path) { |
1037 | strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path)); | |
1038 | channel->shm_path[sizeof(channel->shm_path) - 1] = '\0'; | |
1039 | } | |
1040 | ||
d88aee68 | 1041 | lttng_ht_node_init_u64(&channel->node, channel->key); |
0fc67d9b JG |
1042 | lttng_ht_node_init_u64(&channel->channels_by_session_id_ht_node, |
1043 | channel->session_id); | |
d8ef542d MD |
1044 | |
1045 | channel->wait_fd = -1; | |
1046 | ||
ffe60014 DG |
1047 | CDS_INIT_LIST_HEAD(&channel->streams.head); |
1048 | ||
62a7b8ed | 1049 | DBG("Allocated channel (key %" PRIu64 ")", channel->key); |
3bd1e081 | 1050 | |
3bd1e081 MD |
1051 | end: |
1052 | return channel; | |
1053 | } | |
1054 | ||
1055 | /* | |
1056 | * Add a channel to the global list protected by a mutex. | |
821fffb2 | 1057 | * |
b5a6470f | 1058 | * Always return 0 indicating success. |
3bd1e081 | 1059 | */ |
d8ef542d MD |
1060 | int consumer_add_channel(struct lttng_consumer_channel *channel, |
1061 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 | 1062 | { |
3bd1e081 | 1063 | pthread_mutex_lock(&consumer_data.lock); |
a9838785 | 1064 | pthread_mutex_lock(&channel->lock); |
ec6ea7d0 | 1065 | pthread_mutex_lock(&channel->timer_lock); |
c77fc10a | 1066 | |
b5a6470f DG |
1067 | /* |
1068 | * This gives us a guarantee that the channel we are about to add to the | |
1069 | * channel hash table will be unique. See this function comment on the why | |
1070 | * we need to steel the channel key at this stage. | |
1071 | */ | |
1072 | steal_channel_key(channel->key); | |
c77fc10a | 1073 | |
b5a6470f | 1074 | rcu_read_lock(); |
d88aee68 | 1075 | lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node); |
0fc67d9b JG |
1076 | lttng_ht_add_u64(consumer_data.channels_by_session_id_ht, |
1077 | &channel->channels_by_session_id_ht_node); | |
6065ceec | 1078 | rcu_read_unlock(); |
b5a6470f | 1079 | |
ec6ea7d0 | 1080 | pthread_mutex_unlock(&channel->timer_lock); |
a9838785 | 1081 | pthread_mutex_unlock(&channel->lock); |
3bd1e081 | 1082 | pthread_mutex_unlock(&consumer_data.lock); |
702b1ea4 | 1083 | |
b5a6470f | 1084 | if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) { |
a0cbdd2e | 1085 | notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD); |
d8ef542d | 1086 | } |
b5a6470f DG |
1087 | |
1088 | return 0; | |
3bd1e081 MD |
1089 | } |
1090 | ||
1091 | /* | |
1092 | * Allocate the pollfd structure and the local view of the out fds to avoid | |
1093 | * doing a lookup in the linked list and concurrency issues when writing is | |
1094 | * needed. Called with consumer_data.lock held. | |
1095 | * | |
1096 | * Returns the number of fds in the structures. | |
1097 | */ | |
ffe60014 DG |
1098 | static int update_poll_array(struct lttng_consumer_local_data *ctx, |
1099 | struct pollfd **pollfd, struct lttng_consumer_stream **local_stream, | |
9a2fcf78 | 1100 | struct lttng_ht *ht, int *nb_inactive_fd) |
3bd1e081 | 1101 | { |
3bd1e081 | 1102 | int i = 0; |
e4421fec DG |
1103 | struct lttng_ht_iter iter; |
1104 | struct lttng_consumer_stream *stream; | |
3bd1e081 | 1105 | |
ffe60014 DG |
1106 | assert(ctx); |
1107 | assert(ht); | |
1108 | assert(pollfd); | |
1109 | assert(local_stream); | |
1110 | ||
3bd1e081 | 1111 | DBG("Updating poll fd array"); |
9a2fcf78 | 1112 | *nb_inactive_fd = 0; |
481d6c57 | 1113 | rcu_read_lock(); |
43c34bc3 | 1114 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
8994307f DG |
1115 | /* |
1116 | * Only active streams with an active end point can be added to the | |
1117 | * poll set and local stream storage of the thread. | |
1118 | * | |
1119 | * There is a potential race here for endpoint_status to be updated | |
1120 | * just after the check. However, this is OK since the stream(s) will | |
1121 | * be deleted once the thread is notified that the end point state has | |
1122 | * changed where this function will be called back again. | |
9a2fcf78 JD |
1123 | * |
1124 | * We track the number of inactive FDs because they still need to be | |
1125 | * closed by the polling thread after a wakeup on the data_pipe or | |
1126 | * metadata_pipe. | |
8994307f DG |
1127 | */ |
1128 | if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM || | |
79d4ffb7 | 1129 | stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) { |
9a2fcf78 | 1130 | (*nb_inactive_fd)++; |
3bd1e081 MD |
1131 | continue; |
1132 | } | |
7972aab2 DG |
1133 | /* |
1134 | * This clobbers way too much the debug output. Uncomment that if you | |
1135 | * need it for debugging purposes. | |
1136 | * | |
1137 | * DBG("Active FD %d", stream->wait_fd); | |
1138 | */ | |
e4421fec | 1139 | (*pollfd)[i].fd = stream->wait_fd; |
3bd1e081 | 1140 | (*pollfd)[i].events = POLLIN | POLLPRI; |
e4421fec | 1141 | local_stream[i] = stream; |
3bd1e081 MD |
1142 | i++; |
1143 | } | |
481d6c57 | 1144 | rcu_read_unlock(); |
3bd1e081 MD |
1145 | |
1146 | /* | |
50f8ae69 | 1147 | * Insert the consumer_data_pipe at the end of the array and don't |
3bd1e081 MD |
1148 | * increment i so nb_fd is the number of real FD. |
1149 | */ | |
acdb9057 | 1150 | (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe); |
509bb1cf | 1151 | (*pollfd)[i].events = POLLIN | POLLPRI; |
02b3d176 DG |
1152 | |
1153 | (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe); | |
1154 | (*pollfd)[i + 1].events = POLLIN | POLLPRI; | |
3bd1e081 MD |
1155 | return i; |
1156 | } | |
1157 | ||
1158 | /* | |
84382d49 MD |
1159 | * Poll on the should_quit pipe and the command socket return -1 on |
1160 | * error, 1 if should exit, 0 if data is available on the command socket | |
3bd1e081 MD |
1161 | */ |
1162 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) | |
1163 | { | |
1164 | int num_rdy; | |
1165 | ||
88f2b785 | 1166 | restart: |
3bd1e081 MD |
1167 | num_rdy = poll(consumer_sockpoll, 2, -1); |
1168 | if (num_rdy == -1) { | |
88f2b785 MD |
1169 | /* |
1170 | * Restart interrupted system call. | |
1171 | */ | |
1172 | if (errno == EINTR) { | |
1173 | goto restart; | |
1174 | } | |
7a57cf92 | 1175 | PERROR("Poll error"); |
84382d49 | 1176 | return -1; |
3bd1e081 | 1177 | } |
509bb1cf | 1178 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
3bd1e081 | 1179 | DBG("consumer_should_quit wake up"); |
84382d49 | 1180 | return 1; |
3bd1e081 MD |
1181 | } |
1182 | return 0; | |
3bd1e081 MD |
1183 | } |
1184 | ||
1185 | /* | |
1186 | * Set the error socket. | |
1187 | */ | |
ffe60014 DG |
1188 | void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx, |
1189 | int sock) | |
3bd1e081 MD |
1190 | { |
1191 | ctx->consumer_error_socket = sock; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * Set the command socket path. | |
1196 | */ | |
3bd1e081 MD |
1197 | void lttng_consumer_set_command_sock_path( |
1198 | struct lttng_consumer_local_data *ctx, char *sock) | |
1199 | { | |
1200 | ctx->consumer_command_sock_path = sock; | |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * Send return code to the session daemon. | |
1205 | * If the socket is not defined, we return 0, it is not a fatal error | |
1206 | */ | |
ffe60014 | 1207 | int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd) |
3bd1e081 MD |
1208 | { |
1209 | if (ctx->consumer_error_socket > 0) { | |
1210 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, | |
1211 | sizeof(enum lttcomm_sessiond_command)); | |
1212 | } | |
1213 | ||
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | /* | |
228b5bf7 DG |
1218 | * Close all the tracefiles and stream fds and MUST be called when all |
1219 | * instances are destroyed i.e. when all threads were joined and are ended. | |
3bd1e081 MD |
1220 | */ |
1221 | void lttng_consumer_cleanup(void) | |
1222 | { | |
e4421fec | 1223 | struct lttng_ht_iter iter; |
ffe60014 | 1224 | struct lttng_consumer_channel *channel; |
6065ceec DG |
1225 | |
1226 | rcu_read_lock(); | |
3bd1e081 | 1227 | |
ffe60014 DG |
1228 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel, |
1229 | node.node) { | |
702b1ea4 | 1230 | consumer_del_channel(channel); |
3bd1e081 | 1231 | } |
6065ceec DG |
1232 | |
1233 | rcu_read_unlock(); | |
d6ce1df2 | 1234 | |
d6ce1df2 | 1235 | lttng_ht_destroy(consumer_data.channel_ht); |
0fc67d9b | 1236 | lttng_ht_destroy(consumer_data.channels_by_session_id_ht); |
228b5bf7 DG |
1237 | |
1238 | cleanup_relayd_ht(); | |
1239 | ||
d8ef542d MD |
1240 | lttng_ht_destroy(consumer_data.stream_per_chan_id_ht); |
1241 | ||
228b5bf7 DG |
1242 | /* |
1243 | * This HT contains streams that are freed by either the metadata thread or | |
1244 | * the data thread so we do *nothing* on the hash table and simply destroy | |
1245 | * it. | |
1246 | */ | |
1247 | lttng_ht_destroy(consumer_data.stream_list_ht); | |
a335e585 JG |
1248 | |
1249 | lttng_trace_chunk_registry_destroy(consumer_data.chunk_registry); | |
3bd1e081 MD |
1250 | } |
1251 | ||
1252 | /* | |
1253 | * Called from signal handler. | |
1254 | */ | |
1255 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) | |
1256 | { | |
6cd525e8 MD |
1257 | ssize_t ret; |
1258 | ||
10211f5c | 1259 | CMM_STORE_SHARED(consumer_quit, 1); |
6cd525e8 MD |
1260 | ret = lttng_write(ctx->consumer_should_quit[1], "4", 1); |
1261 | if (ret < 1) { | |
7a57cf92 | 1262 | PERROR("write consumer quit"); |
3bd1e081 | 1263 | } |
ab1027f4 DG |
1264 | |
1265 | DBG("Consumer flag that it should quit"); | |
3bd1e081 MD |
1266 | } |
1267 | ||
5199ffc4 JG |
1268 | |
1269 | /* | |
1270 | * Flush pending writes to trace output disk file. | |
1271 | */ | |
1272 | static | |
00e2e675 DG |
1273 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
1274 | off_t orig_offset) | |
3bd1e081 | 1275 | { |
c7a78aab | 1276 | int ret; |
3bd1e081 MD |
1277 | int outfd = stream->out_fd; |
1278 | ||
1279 | /* | |
1280 | * This does a blocking write-and-wait on any page that belongs to the | |
1281 | * subbuffer prior to the one we just wrote. | |
1282 | * Don't care about error values, as these are just hints and ways to | |
1283 | * limit the amount of page cache used. | |
1284 | */ | |
ffe60014 | 1285 | if (orig_offset < stream->max_sb_size) { |
3bd1e081 MD |
1286 | return; |
1287 | } | |
ffe60014 DG |
1288 | lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size, |
1289 | stream->max_sb_size, | |
3bd1e081 MD |
1290 | SYNC_FILE_RANGE_WAIT_BEFORE |
1291 | | SYNC_FILE_RANGE_WRITE | |
1292 | | SYNC_FILE_RANGE_WAIT_AFTER); | |
1293 | /* | |
1294 | * Give hints to the kernel about how we access the file: | |
1295 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after | |
1296 | * we write it. | |
1297 | * | |
1298 | * We need to call fadvise again after the file grows because the | |
1299 | * kernel does not seem to apply fadvise to non-existing parts of the | |
1300 | * file. | |
1301 | * | |
1302 | * Call fadvise _after_ having waited for the page writeback to | |
1303 | * complete because the dirty page writeback semantic is not well | |
1304 | * defined. So it can be expected to lead to lower throughput in | |
1305 | * streaming. | |
1306 | */ | |
c7a78aab | 1307 | ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size, |
ffe60014 | 1308 | stream->max_sb_size, POSIX_FADV_DONTNEED); |
a0d0e127 | 1309 | if (ret && ret != -ENOSYS) { |
a74a5f4a JG |
1310 | errno = ret; |
1311 | PERROR("posix_fadvise on fd %i", outfd); | |
c7a78aab | 1312 | } |
3bd1e081 MD |
1313 | } |
1314 | ||
1315 | /* | |
1316 | * Initialise the necessary environnement : | |
1317 | * - create a new context | |
1318 | * - create the poll_pipe | |
1319 | * - create the should_quit pipe (for signal handler) | |
1320 | * - create the thread pipe (for splice) | |
1321 | * | |
1322 | * Takes a function pointer as argument, this function is called when data is | |
1323 | * available on a buffer. This function is responsible to do the | |
1324 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the | |
1325 | * buffer configuration and then kernctl_put_next_subbuf at the end. | |
1326 | * | |
1327 | * Returns a pointer to the new context or NULL on error. | |
1328 | */ | |
1329 | struct lttng_consumer_local_data *lttng_consumer_create( | |
1330 | enum lttng_consumer_type type, | |
4078b776 | 1331 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
d41f73b7 | 1332 | struct lttng_consumer_local_data *ctx), |
3bd1e081 MD |
1333 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
1334 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
30319bcb | 1335 | int (*update_stream)(uint64_t stream_key, uint32_t state)) |
3bd1e081 | 1336 | { |
d8ef542d | 1337 | int ret; |
3bd1e081 MD |
1338 | struct lttng_consumer_local_data *ctx; |
1339 | ||
1340 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || | |
1341 | consumer_data.type == type); | |
1342 | consumer_data.type = type; | |
1343 | ||
effcf122 | 1344 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
3bd1e081 | 1345 | if (ctx == NULL) { |
7a57cf92 | 1346 | PERROR("allocating context"); |
3bd1e081 MD |
1347 | goto error; |
1348 | } | |
1349 | ||
1350 | ctx->consumer_error_socket = -1; | |
331744e3 | 1351 | ctx->consumer_metadata_socket = -1; |
75d83e50 | 1352 | pthread_mutex_init(&ctx->metadata_socket_lock, NULL); |
3bd1e081 MD |
1353 | /* assign the callbacks */ |
1354 | ctx->on_buffer_ready = buffer_ready; | |
1355 | ctx->on_recv_channel = recv_channel; | |
1356 | ctx->on_recv_stream = recv_stream; | |
1357 | ctx->on_update_stream = update_stream; | |
1358 | ||
acdb9057 DG |
1359 | ctx->consumer_data_pipe = lttng_pipe_open(0); |
1360 | if (!ctx->consumer_data_pipe) { | |
3bd1e081 MD |
1361 | goto error_poll_pipe; |
1362 | } | |
1363 | ||
02b3d176 DG |
1364 | ctx->consumer_wakeup_pipe = lttng_pipe_open(0); |
1365 | if (!ctx->consumer_wakeup_pipe) { | |
1366 | goto error_wakeup_pipe; | |
1367 | } | |
1368 | ||
3bd1e081 MD |
1369 | ret = pipe(ctx->consumer_should_quit); |
1370 | if (ret < 0) { | |
7a57cf92 | 1371 | PERROR("Error creating recv pipe"); |
3bd1e081 MD |
1372 | goto error_quit_pipe; |
1373 | } | |
1374 | ||
d8ef542d MD |
1375 | ret = pipe(ctx->consumer_channel_pipe); |
1376 | if (ret < 0) { | |
1377 | PERROR("Error creating channel pipe"); | |
1378 | goto error_channel_pipe; | |
1379 | } | |
1380 | ||
13886d2d DG |
1381 | ctx->consumer_metadata_pipe = lttng_pipe_open(0); |
1382 | if (!ctx->consumer_metadata_pipe) { | |
fb3a43a9 DG |
1383 | goto error_metadata_pipe; |
1384 | } | |
3bd1e081 | 1385 | |
e9404c27 JG |
1386 | ctx->channel_monitor_pipe = -1; |
1387 | ||
fb3a43a9 | 1388 | return ctx; |
3bd1e081 | 1389 | |
fb3a43a9 | 1390 | error_metadata_pipe: |
d8ef542d MD |
1391 | utils_close_pipe(ctx->consumer_channel_pipe); |
1392 | error_channel_pipe: | |
d8ef542d | 1393 | utils_close_pipe(ctx->consumer_should_quit); |
3bd1e081 | 1394 | error_quit_pipe: |
02b3d176 DG |
1395 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
1396 | error_wakeup_pipe: | |
acdb9057 | 1397 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
3bd1e081 MD |
1398 | error_poll_pipe: |
1399 | free(ctx); | |
1400 | error: | |
1401 | return NULL; | |
1402 | } | |
1403 | ||
282dadbc MD |
1404 | /* |
1405 | * Iterate over all streams of the hashtable and free them properly. | |
1406 | */ | |
1407 | static void destroy_data_stream_ht(struct lttng_ht *ht) | |
1408 | { | |
1409 | struct lttng_ht_iter iter; | |
1410 | struct lttng_consumer_stream *stream; | |
1411 | ||
1412 | if (ht == NULL) { | |
1413 | return; | |
1414 | } | |
1415 | ||
1416 | rcu_read_lock(); | |
1417 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1418 | /* | |
1419 | * Ignore return value since we are currently cleaning up so any error | |
1420 | * can't be handled. | |
1421 | */ | |
1422 | (void) consumer_del_stream(stream, ht); | |
1423 | } | |
1424 | rcu_read_unlock(); | |
1425 | ||
1426 | lttng_ht_destroy(ht); | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * Iterate over all streams of the metadata hashtable and free them | |
1431 | * properly. | |
1432 | */ | |
1433 | static void destroy_metadata_stream_ht(struct lttng_ht *ht) | |
1434 | { | |
1435 | struct lttng_ht_iter iter; | |
1436 | struct lttng_consumer_stream *stream; | |
1437 | ||
1438 | if (ht == NULL) { | |
1439 | return; | |
1440 | } | |
1441 | ||
1442 | rcu_read_lock(); | |
1443 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1444 | /* | |
1445 | * Ignore return value since we are currently cleaning up so any error | |
1446 | * can't be handled. | |
1447 | */ | |
1448 | (void) consumer_del_metadata_stream(stream, ht); | |
1449 | } | |
1450 | rcu_read_unlock(); | |
1451 | ||
1452 | lttng_ht_destroy(ht); | |
1453 | } | |
1454 | ||
3bd1e081 MD |
1455 | /* |
1456 | * Close all fds associated with the instance and free the context. | |
1457 | */ | |
1458 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) | |
1459 | { | |
4c462e79 MD |
1460 | int ret; |
1461 | ||
ab1027f4 DG |
1462 | DBG("Consumer destroying it. Closing everything."); |
1463 | ||
4f2e75b9 DG |
1464 | if (!ctx) { |
1465 | return; | |
1466 | } | |
1467 | ||
282dadbc MD |
1468 | destroy_data_stream_ht(data_ht); |
1469 | destroy_metadata_stream_ht(metadata_ht); | |
1470 | ||
4c462e79 MD |
1471 | ret = close(ctx->consumer_error_socket); |
1472 | if (ret) { | |
1473 | PERROR("close"); | |
1474 | } | |
331744e3 JD |
1475 | ret = close(ctx->consumer_metadata_socket); |
1476 | if (ret) { | |
1477 | PERROR("close"); | |
1478 | } | |
d8ef542d | 1479 | utils_close_pipe(ctx->consumer_channel_pipe); |
acdb9057 | 1480 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
13886d2d | 1481 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
02b3d176 | 1482 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
d8ef542d | 1483 | utils_close_pipe(ctx->consumer_should_quit); |
fb3a43a9 | 1484 | |
3bd1e081 MD |
1485 | unlink(ctx->consumer_command_sock_path); |
1486 | free(ctx); | |
1487 | } | |
1488 | ||
6197aea7 DG |
1489 | /* |
1490 | * Write the metadata stream id on the specified file descriptor. | |
1491 | */ | |
1492 | static int write_relayd_metadata_id(int fd, | |
1493 | struct lttng_consumer_stream *stream, | |
239f61af | 1494 | unsigned long padding) |
6197aea7 | 1495 | { |
6cd525e8 | 1496 | ssize_t ret; |
1d4dfdef | 1497 | struct lttcomm_relayd_metadata_payload hdr; |
6197aea7 | 1498 | |
1d4dfdef DG |
1499 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
1500 | hdr.padding_size = htobe32(padding); | |
6cd525e8 MD |
1501 | ret = lttng_write(fd, (void *) &hdr, sizeof(hdr)); |
1502 | if (ret < sizeof(hdr)) { | |
d7b75ec8 | 1503 | /* |
6f04ed72 | 1504 | * This error means that the fd's end is closed so ignore the PERROR |
d7b75ec8 DG |
1505 | * not to clubber the error output since this can happen in a normal |
1506 | * code path. | |
1507 | */ | |
1508 | if (errno != EPIPE) { | |
1509 | PERROR("write metadata stream id"); | |
1510 | } | |
1511 | DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno); | |
534d2592 DG |
1512 | /* |
1513 | * Set ret to a negative value because if ret != sizeof(hdr), we don't | |
1514 | * handle writting the missing part so report that as an error and | |
1515 | * don't lie to the caller. | |
1516 | */ | |
1517 | ret = -1; | |
6197aea7 DG |
1518 | goto end; |
1519 | } | |
1d4dfdef DG |
1520 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
1521 | stream->relayd_stream_id, padding); | |
6197aea7 DG |
1522 | |
1523 | end: | |
6cd525e8 | 1524 | return (int) ret; |
6197aea7 DG |
1525 | } |
1526 | ||
3bd1e081 | 1527 | /* |
09e26845 DG |
1528 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
1529 | * core function for writing trace buffers to either the local filesystem or | |
1530 | * the network. | |
1531 | * | |
79d4ffb7 DG |
1532 | * It must be called with the stream lock held. |
1533 | * | |
09e26845 | 1534 | * Careful review MUST be put if any changes occur! |
3bd1e081 MD |
1535 | * |
1536 | * Returns the number of bytes written | |
1537 | */ | |
4078b776 | 1538 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
3bd1e081 | 1539 | struct lttng_consumer_local_data *ctx, |
1d4dfdef | 1540 | struct lttng_consumer_stream *stream, unsigned long len, |
309167d2 | 1541 | unsigned long padding, |
50adc264 | 1542 | struct ctf_packet_index *index) |
3bd1e081 | 1543 | { |
f02e1e8a | 1544 | unsigned long mmap_offset; |
ffe60014 | 1545 | void *mmap_base; |
994ab360 | 1546 | ssize_t ret = 0; |
f02e1e8a DG |
1547 | off_t orig_offset = stream->out_fd_offset; |
1548 | /* Default is on the disk */ | |
1549 | int outfd = stream->out_fd; | |
f02e1e8a | 1550 | struct consumer_relayd_sock_pair *relayd = NULL; |
8994307f | 1551 | unsigned int relayd_hang_up = 0; |
f02e1e8a DG |
1552 | |
1553 | /* RCU lock for the relayd pointer */ | |
1554 | rcu_read_lock(); | |
1555 | ||
1556 | /* Flag that the current stream if set for network streaming. */ | |
da009f2c | 1557 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1558 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1559 | if (relayd == NULL) { | |
56591bac | 1560 | ret = -EPIPE; |
f02e1e8a DG |
1561 | goto end; |
1562 | } | |
1563 | } | |
1564 | ||
1565 | /* get the offset inside the fd to mmap */ | |
3bd1e081 MD |
1566 | switch (consumer_data.type) { |
1567 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 1568 | mmap_base = stream->mmap_base; |
f02e1e8a | 1569 | ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset); |
994ab360 | 1570 | if (ret < 0) { |
56591bac | 1571 | PERROR("tracer ctl get_mmap_read_offset"); |
56591bac MD |
1572 | goto end; |
1573 | } | |
f02e1e8a | 1574 | break; |
7753dea8 MD |
1575 | case LTTNG_CONSUMER32_UST: |
1576 | case LTTNG_CONSUMER64_UST: | |
ffe60014 DG |
1577 | mmap_base = lttng_ustctl_get_mmap_base(stream); |
1578 | if (!mmap_base) { | |
1579 | ERR("read mmap get mmap base for stream %s", stream->name); | |
994ab360 | 1580 | ret = -EPERM; |
ffe60014 DG |
1581 | goto end; |
1582 | } | |
1583 | ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset); | |
56591bac MD |
1584 | if (ret != 0) { |
1585 | PERROR("tracer ctl get_mmap_read_offset"); | |
994ab360 | 1586 | ret = -EINVAL; |
56591bac MD |
1587 | goto end; |
1588 | } | |
f02e1e8a | 1589 | break; |
3bd1e081 MD |
1590 | default: |
1591 | ERR("Unknown consumer_data type"); | |
1592 | assert(0); | |
1593 | } | |
b9182dd9 | 1594 | |
f02e1e8a DG |
1595 | /* Handle stream on the relayd if the output is on the network */ |
1596 | if (relayd) { | |
1597 | unsigned long netlen = len; | |
1598 | ||
1599 | /* | |
1600 | * Lock the control socket for the complete duration of the function | |
1601 | * since from this point on we will use the socket. | |
1602 | */ | |
1603 | if (stream->metadata_flag) { | |
1604 | /* Metadata requires the control socket. */ | |
1605 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
93ec662e JD |
1606 | if (stream->reset_metadata_flag) { |
1607 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1608 | stream->relayd_stream_id, | |
1609 | stream->metadata_version); | |
1610 | if (ret < 0) { | |
1611 | relayd_hang_up = 1; | |
1612 | goto write_error; | |
1613 | } | |
1614 | stream->reset_metadata_flag = 0; | |
1615 | } | |
1d4dfdef | 1616 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
f02e1e8a DG |
1617 | } |
1618 | ||
1d4dfdef | 1619 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
994ab360 DG |
1620 | if (ret < 0) { |
1621 | relayd_hang_up = 1; | |
1622 | goto write_error; | |
1623 | } | |
1624 | /* Use the returned socket. */ | |
1625 | outfd = ret; | |
f02e1e8a | 1626 | |
994ab360 DG |
1627 | /* Write metadata stream id before payload */ |
1628 | if (stream->metadata_flag) { | |
239f61af | 1629 | ret = write_relayd_metadata_id(outfd, stream, padding); |
994ab360 | 1630 | if (ret < 0) { |
8994307f DG |
1631 | relayd_hang_up = 1; |
1632 | goto write_error; | |
1633 | } | |
f02e1e8a | 1634 | } |
1d4dfdef DG |
1635 | } else { |
1636 | /* No streaming, we have to set the len with the full padding */ | |
1637 | len += padding; | |
1624d5b7 | 1638 | |
93ec662e JD |
1639 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1640 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1641 | if (ret < 0) { | |
1642 | ERR("Reset metadata file"); | |
1643 | goto end; | |
1644 | } | |
1645 | stream->reset_metadata_flag = 0; | |
1646 | } | |
1647 | ||
1624d5b7 JD |
1648 | /* |
1649 | * Check if we need to change the tracefile before writing the packet. | |
1650 | */ | |
1651 | if (stream->chan->tracefile_size > 0 && | |
1652 | (stream->tracefile_size_current + len) > | |
1653 | stream->chan->tracefile_size) { | |
fe4477ee JD |
1654 | ret = utils_rotate_stream_file(stream->chan->pathname, |
1655 | stream->name, stream->chan->tracefile_size, | |
1656 | stream->chan->tracefile_count, stream->uid, stream->gid, | |
309167d2 JD |
1657 | stream->out_fd, &(stream->tracefile_count_current), |
1658 | &stream->out_fd); | |
1624d5b7 JD |
1659 | if (ret < 0) { |
1660 | ERR("Rotating output file"); | |
1661 | goto end; | |
1662 | } | |
309167d2 JD |
1663 | outfd = stream->out_fd; |
1664 | ||
f8f3885c MD |
1665 | if (stream->index_file) { |
1666 | lttng_index_file_put(stream->index_file); | |
1667 | stream->index_file = lttng_index_file_create(stream->chan->pathname, | |
309167d2 JD |
1668 | stream->name, stream->uid, stream->gid, |
1669 | stream->chan->tracefile_size, | |
f8f3885c MD |
1670 | stream->tracefile_count_current, |
1671 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
1672 | if (!stream->index_file) { | |
309167d2 JD |
1673 | goto end; |
1674 | } | |
309167d2 JD |
1675 | } |
1676 | ||
a6976990 DG |
1677 | /* Reset current size because we just perform a rotation. */ |
1678 | stream->tracefile_size_current = 0; | |
a1ae300f JD |
1679 | stream->out_fd_offset = 0; |
1680 | orig_offset = 0; | |
1624d5b7 JD |
1681 | } |
1682 | stream->tracefile_size_current += len; | |
309167d2 JD |
1683 | if (index) { |
1684 | index->offset = htobe64(stream->out_fd_offset); | |
1685 | } | |
f02e1e8a DG |
1686 | } |
1687 | ||
d02b8372 DG |
1688 | /* |
1689 | * This call guarantee that len or less is returned. It's impossible to | |
1690 | * receive a ret value that is bigger than len. | |
1691 | */ | |
1692 | ret = lttng_write(outfd, mmap_base + mmap_offset, len); | |
1693 | DBG("Consumer mmap write() ret %zd (len %lu)", ret, len); | |
1694 | if (ret < 0 || ((size_t) ret != len)) { | |
1695 | /* | |
1696 | * Report error to caller if nothing was written else at least send the | |
1697 | * amount written. | |
1698 | */ | |
1699 | if (ret < 0) { | |
994ab360 | 1700 | ret = -errno; |
f02e1e8a | 1701 | } |
994ab360 | 1702 | relayd_hang_up = 1; |
f02e1e8a | 1703 | |
d02b8372 | 1704 | /* Socket operation failed. We consider the relayd dead */ |
994ab360 | 1705 | if (errno == EPIPE || errno == EINVAL || errno == EBADF) { |
d02b8372 DG |
1706 | /* |
1707 | * This is possible if the fd is closed on the other side | |
1708 | * (outfd) or any write problem. It can be verbose a bit for a | |
1709 | * normal execution if for instance the relayd is stopped | |
1710 | * abruptly. This can happen so set this to a DBG statement. | |
1711 | */ | |
1712 | DBG("Consumer mmap write detected relayd hang up"); | |
994ab360 DG |
1713 | } else { |
1714 | /* Unhandled error, print it and stop function right now. */ | |
1715 | PERROR("Error in write mmap (ret %zd != len %lu)", ret, len); | |
f02e1e8a | 1716 | } |
994ab360 | 1717 | goto write_error; |
d02b8372 DG |
1718 | } |
1719 | stream->output_written += ret; | |
d02b8372 DG |
1720 | |
1721 | /* This call is useless on a socket so better save a syscall. */ | |
1722 | if (!relayd) { | |
1723 | /* This won't block, but will start writeout asynchronously */ | |
1724 | lttng_sync_file_range(outfd, stream->out_fd_offset, len, | |
1725 | SYNC_FILE_RANGE_WRITE); | |
1726 | stream->out_fd_offset += len; | |
f5dbe415 | 1727 | lttng_consumer_sync_trace_file(stream, orig_offset); |
f02e1e8a | 1728 | } |
f02e1e8a | 1729 | |
8994307f DG |
1730 | write_error: |
1731 | /* | |
1732 | * This is a special case that the relayd has closed its socket. Let's | |
1733 | * cleanup the relayd object and all associated streams. | |
1734 | */ | |
1735 | if (relayd && relayd_hang_up) { | |
11413bb9 JR |
1736 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
1737 | lttng_consumer_cleanup_relayd(relayd); | |
8994307f DG |
1738 | } |
1739 | ||
f02e1e8a DG |
1740 | end: |
1741 | /* Unlock only if ctrl socket used */ | |
1742 | if (relayd && stream->metadata_flag) { | |
1743 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1744 | } | |
1745 | ||
1746 | rcu_read_unlock(); | |
994ab360 | 1747 | return ret; |
3bd1e081 MD |
1748 | } |
1749 | ||
1750 | /* | |
1751 | * Splice the data from the ring buffer to the tracefile. | |
1752 | * | |
79d4ffb7 DG |
1753 | * It must be called with the stream lock held. |
1754 | * | |
3bd1e081 MD |
1755 | * Returns the number of bytes spliced. |
1756 | */ | |
4078b776 | 1757 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 | 1758 | struct lttng_consumer_local_data *ctx, |
1d4dfdef | 1759 | struct lttng_consumer_stream *stream, unsigned long len, |
309167d2 | 1760 | unsigned long padding, |
50adc264 | 1761 | struct ctf_packet_index *index) |
3bd1e081 | 1762 | { |
f02e1e8a DG |
1763 | ssize_t ret = 0, written = 0, ret_splice = 0; |
1764 | loff_t offset = 0; | |
1765 | off_t orig_offset = stream->out_fd_offset; | |
1766 | int fd = stream->wait_fd; | |
1767 | /* Default is on the disk */ | |
1768 | int outfd = stream->out_fd; | |
f02e1e8a | 1769 | struct consumer_relayd_sock_pair *relayd = NULL; |
fb3a43a9 | 1770 | int *splice_pipe; |
8994307f | 1771 | unsigned int relayd_hang_up = 0; |
f02e1e8a | 1772 | |
3bd1e081 MD |
1773 | switch (consumer_data.type) { |
1774 | case LTTNG_CONSUMER_KERNEL: | |
f02e1e8a | 1775 | break; |
7753dea8 MD |
1776 | case LTTNG_CONSUMER32_UST: |
1777 | case LTTNG_CONSUMER64_UST: | |
f02e1e8a | 1778 | /* Not supported for user space tracing */ |
3bd1e081 MD |
1779 | return -ENOSYS; |
1780 | default: | |
1781 | ERR("Unknown consumer_data type"); | |
1782 | assert(0); | |
3bd1e081 MD |
1783 | } |
1784 | ||
f02e1e8a DG |
1785 | /* RCU lock for the relayd pointer */ |
1786 | rcu_read_lock(); | |
1787 | ||
1788 | /* Flag that the current stream if set for network streaming. */ | |
da009f2c | 1789 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1790 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1791 | if (relayd == NULL) { | |
ad0b0d23 | 1792 | written = -ret; |
f02e1e8a DG |
1793 | goto end; |
1794 | } | |
1795 | } | |
a2361a61 | 1796 | splice_pipe = stream->splice_pipe; |
fb3a43a9 | 1797 | |
f02e1e8a | 1798 | /* Write metadata stream id before payload */ |
1d4dfdef | 1799 | if (relayd) { |
ad0b0d23 | 1800 | unsigned long total_len = len; |
f02e1e8a | 1801 | |
1d4dfdef DG |
1802 | if (stream->metadata_flag) { |
1803 | /* | |
1804 | * Lock the control socket for the complete duration of the function | |
1805 | * since from this point on we will use the socket. | |
1806 | */ | |
1807 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
1808 | ||
93ec662e JD |
1809 | if (stream->reset_metadata_flag) { |
1810 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1811 | stream->relayd_stream_id, | |
1812 | stream->metadata_version); | |
1813 | if (ret < 0) { | |
1814 | relayd_hang_up = 1; | |
1815 | goto write_error; | |
1816 | } | |
1817 | stream->reset_metadata_flag = 0; | |
1818 | } | |
239f61af | 1819 | ret = write_relayd_metadata_id(splice_pipe[1], stream, |
1d4dfdef DG |
1820 | padding); |
1821 | if (ret < 0) { | |
1822 | written = ret; | |
ad0b0d23 DG |
1823 | relayd_hang_up = 1; |
1824 | goto write_error; | |
1d4dfdef DG |
1825 | } |
1826 | ||
1827 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); | |
1828 | } | |
1829 | ||
1830 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); | |
ad0b0d23 DG |
1831 | if (ret < 0) { |
1832 | written = ret; | |
1833 | relayd_hang_up = 1; | |
1834 | goto write_error; | |
f02e1e8a | 1835 | } |
ad0b0d23 DG |
1836 | /* Use the returned socket. */ |
1837 | outfd = ret; | |
1d4dfdef DG |
1838 | } else { |
1839 | /* No streaming, we have to set the len with the full padding */ | |
1840 | len += padding; | |
1624d5b7 | 1841 | |
93ec662e JD |
1842 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1843 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1844 | if (ret < 0) { | |
1845 | ERR("Reset metadata file"); | |
1846 | goto end; | |
1847 | } | |
1848 | stream->reset_metadata_flag = 0; | |
1849 | } | |
1624d5b7 JD |
1850 | /* |
1851 | * Check if we need to change the tracefile before writing the packet. | |
1852 | */ | |
1853 | if (stream->chan->tracefile_size > 0 && | |
1854 | (stream->tracefile_size_current + len) > | |
1855 | stream->chan->tracefile_size) { | |
fe4477ee JD |
1856 | ret = utils_rotate_stream_file(stream->chan->pathname, |
1857 | stream->name, stream->chan->tracefile_size, | |
1858 | stream->chan->tracefile_count, stream->uid, stream->gid, | |
309167d2 JD |
1859 | stream->out_fd, &(stream->tracefile_count_current), |
1860 | &stream->out_fd); | |
1624d5b7 | 1861 | if (ret < 0) { |
ad0b0d23 | 1862 | written = ret; |
1624d5b7 JD |
1863 | ERR("Rotating output file"); |
1864 | goto end; | |
1865 | } | |
309167d2 JD |
1866 | outfd = stream->out_fd; |
1867 | ||
f8f3885c MD |
1868 | if (stream->index_file) { |
1869 | lttng_index_file_put(stream->index_file); | |
1870 | stream->index_file = lttng_index_file_create(stream->chan->pathname, | |
309167d2 JD |
1871 | stream->name, stream->uid, stream->gid, |
1872 | stream->chan->tracefile_size, | |
f8f3885c MD |
1873 | stream->tracefile_count_current, |
1874 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
1875 | if (!stream->index_file) { | |
309167d2 JD |
1876 | goto end; |
1877 | } | |
309167d2 JD |
1878 | } |
1879 | ||
a6976990 DG |
1880 | /* Reset current size because we just perform a rotation. */ |
1881 | stream->tracefile_size_current = 0; | |
a1ae300f JD |
1882 | stream->out_fd_offset = 0; |
1883 | orig_offset = 0; | |
1624d5b7 JD |
1884 | } |
1885 | stream->tracefile_size_current += len; | |
309167d2 | 1886 | index->offset = htobe64(stream->out_fd_offset); |
f02e1e8a DG |
1887 | } |
1888 | ||
1889 | while (len > 0) { | |
1d4dfdef DG |
1890 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
1891 | (unsigned long)offset, len, fd, splice_pipe[1]); | |
fb3a43a9 | 1892 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
f02e1e8a DG |
1893 | SPLICE_F_MOVE | SPLICE_F_MORE); |
1894 | DBG("splice chan to pipe, ret %zd", ret_splice); | |
1895 | if (ret_splice < 0) { | |
d02b8372 | 1896 | ret = errno; |
ad0b0d23 | 1897 | written = -ret; |
d02b8372 | 1898 | PERROR("Error in relay splice"); |
f02e1e8a DG |
1899 | goto splice_error; |
1900 | } | |
1901 | ||
1902 | /* Handle stream on the relayd if the output is on the network */ | |
ad0b0d23 DG |
1903 | if (relayd && stream->metadata_flag) { |
1904 | size_t metadata_payload_size = | |
1905 | sizeof(struct lttcomm_relayd_metadata_payload); | |
1906 | ||
1907 | /* Update counter to fit the spliced data */ | |
1908 | ret_splice += metadata_payload_size; | |
1909 | len += metadata_payload_size; | |
1910 | /* | |
1911 | * We do this so the return value can match the len passed as | |
1912 | * argument to this function. | |
1913 | */ | |
1914 | written -= metadata_payload_size; | |
f02e1e8a DG |
1915 | } |
1916 | ||
1917 | /* Splice data out */ | |
fb3a43a9 | 1918 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
f02e1e8a | 1919 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
a2361a61 JD |
1920 | DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", |
1921 | outfd, ret_splice); | |
f02e1e8a | 1922 | if (ret_splice < 0) { |
d02b8372 | 1923 | ret = errno; |
ad0b0d23 DG |
1924 | written = -ret; |
1925 | relayd_hang_up = 1; | |
1926 | goto write_error; | |
f02e1e8a | 1927 | } else if (ret_splice > len) { |
d02b8372 DG |
1928 | /* |
1929 | * We don't expect this code path to be executed but you never know | |
1930 | * so this is an extra protection agains a buggy splice(). | |
1931 | */ | |
f02e1e8a | 1932 | ret = errno; |
ad0b0d23 | 1933 | written += ret_splice; |
d02b8372 DG |
1934 | PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice, |
1935 | len); | |
f02e1e8a | 1936 | goto splice_error; |
d02b8372 DG |
1937 | } else { |
1938 | /* All good, update current len and continue. */ | |
1939 | len -= ret_splice; | |
f02e1e8a | 1940 | } |
f02e1e8a DG |
1941 | |
1942 | /* This call is useless on a socket so better save a syscall. */ | |
1943 | if (!relayd) { | |
1944 | /* This won't block, but will start writeout asynchronously */ | |
1945 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, | |
1946 | SYNC_FILE_RANGE_WRITE); | |
1947 | stream->out_fd_offset += ret_splice; | |
1948 | } | |
e5d1a9b3 | 1949 | stream->output_written += ret_splice; |
f02e1e8a DG |
1950 | written += ret_splice; |
1951 | } | |
f5dbe415 JG |
1952 | if (!relayd) { |
1953 | lttng_consumer_sync_trace_file(stream, orig_offset); | |
1954 | } | |
f02e1e8a DG |
1955 | goto end; |
1956 | ||
8994307f DG |
1957 | write_error: |
1958 | /* | |
1959 | * This is a special case that the relayd has closed its socket. Let's | |
1960 | * cleanup the relayd object and all associated streams. | |
1961 | */ | |
1962 | if (relayd && relayd_hang_up) { | |
11413bb9 JR |
1963 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
1964 | lttng_consumer_cleanup_relayd(relayd); | |
8994307f DG |
1965 | /* Skip splice error so the consumer does not fail */ |
1966 | goto end; | |
1967 | } | |
1968 | ||
f02e1e8a DG |
1969 | splice_error: |
1970 | /* send the appropriate error description to sessiond */ | |
1971 | switch (ret) { | |
f02e1e8a | 1972 | case EINVAL: |
f73fabfd | 1973 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
f02e1e8a DG |
1974 | break; |
1975 | case ENOMEM: | |
f73fabfd | 1976 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
f02e1e8a DG |
1977 | break; |
1978 | case ESPIPE: | |
f73fabfd | 1979 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
f02e1e8a DG |
1980 | break; |
1981 | } | |
1982 | ||
1983 | end: | |
1984 | if (relayd && stream->metadata_flag) { | |
1985 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1986 | } | |
1987 | ||
1988 | rcu_read_unlock(); | |
1989 | return written; | |
3bd1e081 MD |
1990 | } |
1991 | ||
15055ce5 JD |
1992 | /* |
1993 | * Sample the snapshot positions for a specific fd | |
1994 | * | |
1995 | * Returns 0 on success, < 0 on error | |
1996 | */ | |
1997 | int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream *stream) | |
1998 | { | |
1999 | switch (consumer_data.type) { | |
2000 | case LTTNG_CONSUMER_KERNEL: | |
2001 | return lttng_kconsumer_sample_snapshot_positions(stream); | |
2002 | case LTTNG_CONSUMER32_UST: | |
2003 | case LTTNG_CONSUMER64_UST: | |
2004 | return lttng_ustconsumer_sample_snapshot_positions(stream); | |
2005 | default: | |
2006 | ERR("Unknown consumer_data type"); | |
2007 | assert(0); | |
2008 | return -ENOSYS; | |
2009 | } | |
2010 | } | |
3bd1e081 MD |
2011 | /* |
2012 | * Take a snapshot for a specific fd | |
2013 | * | |
2014 | * Returns 0 on success, < 0 on error | |
2015 | */ | |
ffe60014 | 2016 | int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
2017 | { |
2018 | switch (consumer_data.type) { | |
2019 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 2020 | return lttng_kconsumer_take_snapshot(stream); |
7753dea8 MD |
2021 | case LTTNG_CONSUMER32_UST: |
2022 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 2023 | return lttng_ustconsumer_take_snapshot(stream); |
3bd1e081 MD |
2024 | default: |
2025 | ERR("Unknown consumer_data type"); | |
2026 | assert(0); | |
2027 | return -ENOSYS; | |
2028 | } | |
3bd1e081 MD |
2029 | } |
2030 | ||
2031 | /* | |
2032 | * Get the produced position | |
2033 | * | |
2034 | * Returns 0 on success, < 0 on error | |
2035 | */ | |
ffe60014 | 2036 | int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
2037 | unsigned long *pos) |
2038 | { | |
2039 | switch (consumer_data.type) { | |
2040 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 2041 | return lttng_kconsumer_get_produced_snapshot(stream, pos); |
7753dea8 MD |
2042 | case LTTNG_CONSUMER32_UST: |
2043 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 2044 | return lttng_ustconsumer_get_produced_snapshot(stream, pos); |
3bd1e081 MD |
2045 | default: |
2046 | ERR("Unknown consumer_data type"); | |
2047 | assert(0); | |
2048 | return -ENOSYS; | |
2049 | } | |
2050 | } | |
2051 | ||
15055ce5 JD |
2052 | /* |
2053 | * Get the consumed position (free-running counter position in bytes). | |
2054 | * | |
2055 | * Returns 0 on success, < 0 on error | |
2056 | */ | |
2057 | int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
2058 | unsigned long *pos) | |
2059 | { | |
2060 | switch (consumer_data.type) { | |
2061 | case LTTNG_CONSUMER_KERNEL: | |
2062 | return lttng_kconsumer_get_consumed_snapshot(stream, pos); | |
2063 | case LTTNG_CONSUMER32_UST: | |
2064 | case LTTNG_CONSUMER64_UST: | |
2065 | return lttng_ustconsumer_get_consumed_snapshot(stream, pos); | |
2066 | default: | |
2067 | ERR("Unknown consumer_data type"); | |
2068 | assert(0); | |
2069 | return -ENOSYS; | |
2070 | } | |
2071 | } | |
2072 | ||
3bd1e081 MD |
2073 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
2074 | int sock, struct pollfd *consumer_sockpoll) | |
2075 | { | |
2076 | switch (consumer_data.type) { | |
2077 | case LTTNG_CONSUMER_KERNEL: | |
2078 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
7753dea8 MD |
2079 | case LTTNG_CONSUMER32_UST: |
2080 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
2081 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
2082 | default: | |
2083 | ERR("Unknown consumer_data type"); | |
2084 | assert(0); | |
2085 | return -ENOSYS; | |
2086 | } | |
2087 | } | |
2088 | ||
6d574024 | 2089 | void lttng_consumer_close_all_metadata(void) |
d88aee68 DG |
2090 | { |
2091 | switch (consumer_data.type) { | |
2092 | case LTTNG_CONSUMER_KERNEL: | |
2093 | /* | |
2094 | * The Kernel consumer has a different metadata scheme so we don't | |
2095 | * close anything because the stream will be closed by the session | |
2096 | * daemon. | |
2097 | */ | |
2098 | break; | |
2099 | case LTTNG_CONSUMER32_UST: | |
2100 | case LTTNG_CONSUMER64_UST: | |
2101 | /* | |
2102 | * Close all metadata streams. The metadata hash table is passed and | |
2103 | * this call iterates over it by closing all wakeup fd. This is safe | |
2104 | * because at this point we are sure that the metadata producer is | |
2105 | * either dead or blocked. | |
2106 | */ | |
6d574024 | 2107 | lttng_ustconsumer_close_all_metadata(metadata_ht); |
d88aee68 DG |
2108 | break; |
2109 | default: | |
2110 | ERR("Unknown consumer_data type"); | |
2111 | assert(0); | |
2112 | } | |
2113 | } | |
2114 | ||
fb3a43a9 DG |
2115 | /* |
2116 | * Clean up a metadata stream and free its memory. | |
2117 | */ | |
e316aad5 DG |
2118 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
2119 | struct lttng_ht *ht) | |
fb3a43a9 | 2120 | { |
e316aad5 | 2121 | struct lttng_consumer_channel *free_chan = NULL; |
fb3a43a9 DG |
2122 | |
2123 | assert(stream); | |
2124 | /* | |
2125 | * This call should NEVER receive regular stream. It must always be | |
2126 | * metadata stream and this is crucial for data structure synchronization. | |
2127 | */ | |
2128 | assert(stream->metadata_flag); | |
2129 | ||
e316aad5 DG |
2130 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
2131 | ||
74251bb8 | 2132 | pthread_mutex_lock(&consumer_data.lock); |
fb549e7a | 2133 | pthread_mutex_lock(&stream->chan->lock); |
3dad2c0f | 2134 | pthread_mutex_lock(&stream->lock); |
081424af JG |
2135 | if (stream->chan->metadata_cache) { |
2136 | /* Only applicable to userspace consumers. */ | |
2137 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); | |
2138 | } | |
8994307f | 2139 | |
6d574024 DG |
2140 | /* Remove any reference to that stream. */ |
2141 | consumer_stream_delete(stream, ht); | |
ca22feea | 2142 | |
6d574024 DG |
2143 | /* Close down everything including the relayd if one. */ |
2144 | consumer_stream_close(stream); | |
2145 | /* Destroy tracer buffers of the stream. */ | |
2146 | consumer_stream_destroy_buffers(stream); | |
fb3a43a9 DG |
2147 | |
2148 | /* Atomically decrement channel refcount since other threads can use it. */ | |
f2ad556d | 2149 | if (!uatomic_sub_return(&stream->chan->refcount, 1) |
ffe60014 | 2150 | && !uatomic_read(&stream->chan->nb_init_stream_left)) { |
c30aaa51 | 2151 | /* Go for channel deletion! */ |
e316aad5 | 2152 | free_chan = stream->chan; |
fb3a43a9 DG |
2153 | } |
2154 | ||
73811ecc DG |
2155 | /* |
2156 | * Nullify the stream reference so it is not used after deletion. The | |
6d574024 DG |
2157 | * channel lock MUST be acquired before being able to check for a NULL |
2158 | * pointer value. | |
73811ecc DG |
2159 | */ |
2160 | stream->chan->metadata_stream = NULL; | |
2161 | ||
081424af JG |
2162 | if (stream->chan->metadata_cache) { |
2163 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); | |
2164 | } | |
3dad2c0f | 2165 | pthread_mutex_unlock(&stream->lock); |
fb549e7a | 2166 | pthread_mutex_unlock(&stream->chan->lock); |
74251bb8 | 2167 | pthread_mutex_unlock(&consumer_data.lock); |
e316aad5 DG |
2168 | |
2169 | if (free_chan) { | |
2170 | consumer_del_channel(free_chan); | |
2171 | } | |
2172 | ||
6d574024 | 2173 | consumer_stream_free(stream); |
fb3a43a9 DG |
2174 | } |
2175 | ||
2176 | /* | |
2177 | * Action done with the metadata stream when adding it to the consumer internal | |
2178 | * data structures to handle it. | |
2179 | */ | |
66d583dc | 2180 | void consumer_add_metadata_stream(struct lttng_consumer_stream *stream) |
fb3a43a9 | 2181 | { |
5ab66908 | 2182 | struct lttng_ht *ht = metadata_ht; |
76082088 | 2183 | struct lttng_ht_iter iter; |
d88aee68 | 2184 | struct lttng_ht_node_u64 *node; |
fb3a43a9 | 2185 | |
e316aad5 DG |
2186 | assert(stream); |
2187 | assert(ht); | |
2188 | ||
d88aee68 | 2189 | DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key); |
e316aad5 DG |
2190 | |
2191 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 2192 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 2193 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 2194 | pthread_mutex_lock(&stream->lock); |
e316aad5 | 2195 | |
e316aad5 DG |
2196 | /* |
2197 | * From here, refcounts are updated so be _careful_ when returning an error | |
2198 | * after this point. | |
2199 | */ | |
2200 | ||
fb3a43a9 | 2201 | rcu_read_lock(); |
76082088 DG |
2202 | |
2203 | /* | |
2204 | * Lookup the stream just to make sure it does not exist in our internal | |
2205 | * state. This should NEVER happen. | |
2206 | */ | |
d88aee68 DG |
2207 | lttng_ht_lookup(ht, &stream->key, &iter); |
2208 | node = lttng_ht_iter_get_node_u64(&iter); | |
76082088 DG |
2209 | assert(!node); |
2210 | ||
e316aad5 | 2211 | /* |
ffe60014 DG |
2212 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
2213 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
2214 | * causes the count to become 0 also causes a stream to be added. The |
2215 | * channel deletion will thus be triggered by the following removal of this | |
2216 | * stream. | |
2217 | */ | |
ffe60014 | 2218 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
2219 | /* Increment refcount before decrementing nb_init_stream_left */ |
2220 | cmm_smp_wmb(); | |
ffe60014 | 2221 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
2222 | } |
2223 | ||
d88aee68 | 2224 | lttng_ht_add_unique_u64(ht, &stream->node); |
ca22feea | 2225 | |
446156b4 | 2226 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
d8ef542d MD |
2227 | &stream->node_channel_id); |
2228 | ||
ca22feea DG |
2229 | /* |
2230 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
2231 | * the key since the HT does not use it and we allow to add redundant keys | |
2232 | * into this table. | |
2233 | */ | |
d88aee68 | 2234 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 2235 | |
fb3a43a9 | 2236 | rcu_read_unlock(); |
e316aad5 | 2237 | |
2e818a6a | 2238 | pthread_mutex_unlock(&stream->lock); |
a9838785 | 2239 | pthread_mutex_unlock(&stream->chan->lock); |
ec6ea7d0 | 2240 | pthread_mutex_unlock(&stream->chan->timer_lock); |
e316aad5 | 2241 | pthread_mutex_unlock(&consumer_data.lock); |
fb3a43a9 DG |
2242 | } |
2243 | ||
8994307f DG |
2244 | /* |
2245 | * Delete data stream that are flagged for deletion (endpoint_status). | |
2246 | */ | |
2247 | static void validate_endpoint_status_data_stream(void) | |
2248 | { | |
2249 | struct lttng_ht_iter iter; | |
2250 | struct lttng_consumer_stream *stream; | |
2251 | ||
2252 | DBG("Consumer delete flagged data stream"); | |
2253 | ||
2254 | rcu_read_lock(); | |
2255 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
2256 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2257 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2258 | continue; |
2259 | } | |
2260 | /* Delete it right now */ | |
2261 | consumer_del_stream(stream, data_ht); | |
2262 | } | |
2263 | rcu_read_unlock(); | |
2264 | } | |
2265 | ||
2266 | /* | |
2267 | * Delete metadata stream that are flagged for deletion (endpoint_status). | |
2268 | */ | |
2269 | static void validate_endpoint_status_metadata_stream( | |
2270 | struct lttng_poll_event *pollset) | |
2271 | { | |
2272 | struct lttng_ht_iter iter; | |
2273 | struct lttng_consumer_stream *stream; | |
2274 | ||
2275 | DBG("Consumer delete flagged metadata stream"); | |
2276 | ||
2277 | assert(pollset); | |
2278 | ||
2279 | rcu_read_lock(); | |
2280 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
2281 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2282 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2283 | continue; |
2284 | } | |
2285 | /* | |
2286 | * Remove from pollset so the metadata thread can continue without | |
2287 | * blocking on a deleted stream. | |
2288 | */ | |
2289 | lttng_poll_del(pollset, stream->wait_fd); | |
2290 | ||
2291 | /* Delete it right now */ | |
2292 | consumer_del_metadata_stream(stream, metadata_ht); | |
2293 | } | |
2294 | rcu_read_unlock(); | |
2295 | } | |
2296 | ||
d73bf3d7 JD |
2297 | /* |
2298 | * Perform operations that need to be done after a stream has | |
2299 | * rotated and released the stream lock. | |
2300 | * | |
2301 | * Multiple rotations cannot occur simultaneously, so we know the state of the | |
2302 | * "rotated" stream flag cannot change. | |
2303 | * | |
2304 | * This MUST be called WITHOUT the stream lock held. | |
2305 | */ | |
2306 | static | |
2307 | int consumer_post_rotation(struct lttng_consumer_stream *stream, | |
2308 | struct lttng_consumer_local_data *ctx) | |
2309 | { | |
2310 | int ret = 0; | |
2311 | ||
2312 | pthread_mutex_lock(&stream->chan->lock); | |
2313 | ||
2314 | switch (consumer_data.type) { | |
2315 | case LTTNG_CONSUMER_KERNEL: | |
2316 | break; | |
2317 | case LTTNG_CONSUMER32_UST: | |
2318 | case LTTNG_CONSUMER64_UST: | |
2319 | /* | |
2320 | * The ust_metadata_pushed counter has been reset to 0, so now | |
2321 | * we can wakeup the metadata thread so it dumps the metadata | |
2322 | * cache to the new file. | |
2323 | */ | |
2324 | if (stream->metadata_flag) { | |
2325 | consumer_metadata_wakeup_pipe(stream->chan); | |
2326 | } | |
2327 | break; | |
2328 | default: | |
2329 | ERR("Unknown consumer_data type"); | |
2330 | abort(); | |
2331 | } | |
2332 | ||
d73bf3d7 | 2333 | pthread_mutex_unlock(&stream->chan->lock); |
d73bf3d7 JD |
2334 | return ret; |
2335 | } | |
2336 | ||
fb3a43a9 DG |
2337 | /* |
2338 | * Thread polls on metadata file descriptor and write them on disk or on the | |
2339 | * network. | |
2340 | */ | |
7d980def | 2341 | void *consumer_thread_metadata_poll(void *data) |
fb3a43a9 | 2342 | { |
1fc79fb4 | 2343 | int ret, i, pollfd, err = -1; |
fb3a43a9 | 2344 | uint32_t revents, nb_fd; |
e316aad5 | 2345 | struct lttng_consumer_stream *stream = NULL; |
fb3a43a9 | 2346 | struct lttng_ht_iter iter; |
d88aee68 | 2347 | struct lttng_ht_node_u64 *node; |
fb3a43a9 DG |
2348 | struct lttng_poll_event events; |
2349 | struct lttng_consumer_local_data *ctx = data; | |
2350 | ssize_t len; | |
2351 | ||
2352 | rcu_register_thread(); | |
2353 | ||
1fc79fb4 MD |
2354 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA); |
2355 | ||
2d57de81 MD |
2356 | if (testpoint(consumerd_thread_metadata)) { |
2357 | goto error_testpoint; | |
2358 | } | |
2359 | ||
9ce5646a MD |
2360 | health_code_update(); |
2361 | ||
fb3a43a9 DG |
2362 | DBG("Thread metadata poll started"); |
2363 | ||
fb3a43a9 DG |
2364 | /* Size is set to 1 for the consumer_metadata pipe */ |
2365 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2366 | if (ret < 0) { | |
2367 | ERR("Poll set creation failed"); | |
d8ef542d | 2368 | goto end_poll; |
fb3a43a9 DG |
2369 | } |
2370 | ||
13886d2d DG |
2371 | ret = lttng_poll_add(&events, |
2372 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN); | |
fb3a43a9 DG |
2373 | if (ret < 0) { |
2374 | goto end; | |
2375 | } | |
2376 | ||
2377 | /* Main loop */ | |
2378 | DBG("Metadata main loop started"); | |
2379 | ||
2380 | while (1) { | |
fb3a43a9 | 2381 | restart: |
7fa2082e | 2382 | health_code_update(); |
9ce5646a | 2383 | health_poll_entry(); |
7fa2082e | 2384 | DBG("Metadata poll wait"); |
fb3a43a9 | 2385 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2386 | DBG("Metadata poll return from wait with %d fd(s)", |
2387 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2388 | health_poll_exit(); |
40063ead | 2389 | DBG("Metadata event caught in thread"); |
fb3a43a9 DG |
2390 | if (ret < 0) { |
2391 | if (errno == EINTR) { | |
40063ead | 2392 | ERR("Poll EINTR caught"); |
fb3a43a9 DG |
2393 | goto restart; |
2394 | } | |
d9607cd7 MD |
2395 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2396 | err = 0; /* All is OK */ | |
2397 | } | |
2398 | goto end; | |
fb3a43a9 DG |
2399 | } |
2400 | ||
0d9c5d77 DG |
2401 | nb_fd = ret; |
2402 | ||
e316aad5 | 2403 | /* From here, the event is a metadata wait fd */ |
fb3a43a9 | 2404 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2405 | health_code_update(); |
2406 | ||
fb3a43a9 DG |
2407 | revents = LTTNG_POLL_GETEV(&events, i); |
2408 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2409 | ||
13886d2d | 2410 | if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) { |
03e43155 | 2411 | if (revents & LPOLLIN) { |
13886d2d DG |
2412 | ssize_t pipe_len; |
2413 | ||
2414 | pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe, | |
2415 | &stream, sizeof(stream)); | |
6cd525e8 | 2416 | if (pipe_len < sizeof(stream)) { |
03e43155 MD |
2417 | if (pipe_len < 0) { |
2418 | PERROR("read metadata stream"); | |
2419 | } | |
fb3a43a9 | 2420 | /* |
03e43155 MD |
2421 | * Remove the pipe from the poll set and continue the loop |
2422 | * since their might be data to consume. | |
fb3a43a9 | 2423 | */ |
03e43155 MD |
2424 | lttng_poll_del(&events, |
2425 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2426 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
fb3a43a9 DG |
2427 | continue; |
2428 | } | |
2429 | ||
8994307f DG |
2430 | /* A NULL stream means that the state has changed. */ |
2431 | if (stream == NULL) { | |
2432 | /* Check for deleted streams. */ | |
2433 | validate_endpoint_status_metadata_stream(&events); | |
3714380f | 2434 | goto restart; |
8994307f DG |
2435 | } |
2436 | ||
fb3a43a9 DG |
2437 | DBG("Adding metadata stream %d to poll set", |
2438 | stream->wait_fd); | |
2439 | ||
fb3a43a9 DG |
2440 | /* Add metadata stream to the global poll events list */ |
2441 | lttng_poll_add(&events, stream->wait_fd, | |
6d574024 | 2442 | LPOLLIN | LPOLLPRI | LPOLLHUP); |
03e43155 MD |
2443 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
2444 | DBG("Metadata thread pipe hung up"); | |
2445 | /* | |
2446 | * Remove the pipe from the poll set and continue the loop | |
2447 | * since their might be data to consume. | |
2448 | */ | |
2449 | lttng_poll_del(&events, | |
2450 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2451 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
2452 | continue; | |
2453 | } else { | |
2454 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2455 | goto end; | |
fb3a43a9 DG |
2456 | } |
2457 | ||
e316aad5 | 2458 | /* Handle other stream */ |
fb3a43a9 DG |
2459 | continue; |
2460 | } | |
2461 | ||
d09e1200 | 2462 | rcu_read_lock(); |
d88aee68 DG |
2463 | { |
2464 | uint64_t tmp_id = (uint64_t) pollfd; | |
2465 | ||
2466 | lttng_ht_lookup(metadata_ht, &tmp_id, &iter); | |
2467 | } | |
2468 | node = lttng_ht_iter_get_node_u64(&iter); | |
e316aad5 | 2469 | assert(node); |
fb3a43a9 DG |
2470 | |
2471 | stream = caa_container_of(node, struct lttng_consumer_stream, | |
58b1f425 | 2472 | node); |
fb3a43a9 | 2473 | |
03e43155 MD |
2474 | if (revents & (LPOLLIN | LPOLLPRI)) { |
2475 | /* Get the data out of the metadata file descriptor */ | |
2476 | DBG("Metadata available on fd %d", pollfd); | |
2477 | assert(stream->wait_fd == pollfd); | |
2478 | ||
2479 | do { | |
2480 | health_code_update(); | |
2481 | ||
2482 | len = ctx->on_buffer_ready(stream, ctx); | |
2483 | /* | |
2484 | * We don't check the return value here since if we get | |
83f4233d | 2485 | * a negative len, it means an error occurred thus we |
03e43155 MD |
2486 | * simply remove it from the poll set and free the |
2487 | * stream. | |
2488 | */ | |
2489 | } while (len > 0); | |
2490 | ||
2491 | /* It's ok to have an unavailable sub-buffer */ | |
2492 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { | |
2493 | /* Clean up stream from consumer and free it. */ | |
2494 | lttng_poll_del(&events, stream->wait_fd); | |
2495 | consumer_del_metadata_stream(stream, metadata_ht); | |
2496 | } | |
2497 | } else if (revents & (LPOLLERR | LPOLLHUP)) { | |
e316aad5 | 2498 | DBG("Metadata fd %d is hup|err.", pollfd); |
fb3a43a9 DG |
2499 | if (!stream->hangup_flush_done |
2500 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2501 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2502 | DBG("Attempting to flush and consume the UST buffers"); | |
2503 | lttng_ustconsumer_on_stream_hangup(stream); | |
2504 | ||
2505 | /* We just flushed the stream now read it. */ | |
4bb94b75 | 2506 | do { |
9ce5646a MD |
2507 | health_code_update(); |
2508 | ||
4bb94b75 DG |
2509 | len = ctx->on_buffer_ready(stream, ctx); |
2510 | /* | |
2511 | * We don't check the return value here since if we get | |
83f4233d | 2512 | * a negative len, it means an error occurred thus we |
4bb94b75 DG |
2513 | * simply remove it from the poll set and free the |
2514 | * stream. | |
2515 | */ | |
2516 | } while (len > 0); | |
fb3a43a9 DG |
2517 | } |
2518 | ||
fb3a43a9 | 2519 | lttng_poll_del(&events, stream->wait_fd); |
e316aad5 DG |
2520 | /* |
2521 | * This call update the channel states, closes file descriptors | |
2522 | * and securely free the stream. | |
2523 | */ | |
2524 | consumer_del_metadata_stream(stream, metadata_ht); | |
03e43155 MD |
2525 | } else { |
2526 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
6f2f1a70 | 2527 | rcu_read_unlock(); |
03e43155 | 2528 | goto end; |
fb3a43a9 | 2529 | } |
e316aad5 | 2530 | /* Release RCU lock for the stream looked up */ |
d09e1200 | 2531 | rcu_read_unlock(); |
fb3a43a9 DG |
2532 | } |
2533 | } | |
2534 | ||
1fc79fb4 MD |
2535 | /* All is OK */ |
2536 | err = 0; | |
fb3a43a9 DG |
2537 | end: |
2538 | DBG("Metadata poll thread exiting"); | |
fb3a43a9 | 2539 | |
d8ef542d MD |
2540 | lttng_poll_clean(&events); |
2541 | end_poll: | |
2d57de81 | 2542 | error_testpoint: |
1fc79fb4 MD |
2543 | if (err) { |
2544 | health_error(); | |
2545 | ERR("Health error occurred in %s", __func__); | |
2546 | } | |
2547 | health_unregister(health_consumerd); | |
fb3a43a9 DG |
2548 | rcu_unregister_thread(); |
2549 | return NULL; | |
2550 | } | |
2551 | ||
3bd1e081 | 2552 | /* |
e4421fec | 2553 | * This thread polls the fds in the set to consume the data and write |
3bd1e081 MD |
2554 | * it to tracefile if necessary. |
2555 | */ | |
7d980def | 2556 | void *consumer_thread_data_poll(void *data) |
3bd1e081 | 2557 | { |
1fc79fb4 | 2558 | int num_rdy, num_hup, high_prio, ret, i, err = -1; |
3bd1e081 MD |
2559 | struct pollfd *pollfd = NULL; |
2560 | /* local view of the streams */ | |
c869f647 | 2561 | struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL; |
3bd1e081 | 2562 | /* local view of consumer_data.fds_count */ |
8bdcc002 JG |
2563 | int nb_fd = 0; |
2564 | /* 2 for the consumer_data_pipe and wake up pipe */ | |
2565 | const int nb_pipes_fd = 2; | |
9a2fcf78 JD |
2566 | /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */ |
2567 | int nb_inactive_fd = 0; | |
3bd1e081 | 2568 | struct lttng_consumer_local_data *ctx = data; |
00e2e675 | 2569 | ssize_t len; |
3bd1e081 | 2570 | |
e7b994a3 DG |
2571 | rcu_register_thread(); |
2572 | ||
1fc79fb4 MD |
2573 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA); |
2574 | ||
2d57de81 MD |
2575 | if (testpoint(consumerd_thread_data)) { |
2576 | goto error_testpoint; | |
2577 | } | |
2578 | ||
9ce5646a MD |
2579 | health_code_update(); |
2580 | ||
4df6c8cb MD |
2581 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream *)); |
2582 | if (local_stream == NULL) { | |
2583 | PERROR("local_stream malloc"); | |
2584 | goto end; | |
2585 | } | |
3bd1e081 MD |
2586 | |
2587 | while (1) { | |
9ce5646a MD |
2588 | health_code_update(); |
2589 | ||
3bd1e081 MD |
2590 | high_prio = 0; |
2591 | num_hup = 0; | |
2592 | ||
2593 | /* | |
e4421fec | 2594 | * the fds set has been updated, we need to update our |
3bd1e081 MD |
2595 | * local array as well |
2596 | */ | |
2597 | pthread_mutex_lock(&consumer_data.lock); | |
2598 | if (consumer_data.need_update) { | |
0e428499 DG |
2599 | free(pollfd); |
2600 | pollfd = NULL; | |
2601 | ||
2602 | free(local_stream); | |
2603 | local_stream = NULL; | |
3bd1e081 | 2604 | |
8bdcc002 | 2605 | /* Allocate for all fds */ |
261de637 | 2606 | pollfd = zmalloc((consumer_data.stream_count + nb_pipes_fd) * sizeof(struct pollfd)); |
3bd1e081 | 2607 | if (pollfd == NULL) { |
7a57cf92 | 2608 | PERROR("pollfd malloc"); |
3bd1e081 MD |
2609 | pthread_mutex_unlock(&consumer_data.lock); |
2610 | goto end; | |
2611 | } | |
2612 | ||
261de637 | 2613 | local_stream = zmalloc((consumer_data.stream_count + nb_pipes_fd) * |
747f8642 | 2614 | sizeof(struct lttng_consumer_stream *)); |
3bd1e081 | 2615 | if (local_stream == NULL) { |
7a57cf92 | 2616 | PERROR("local_stream malloc"); |
3bd1e081 MD |
2617 | pthread_mutex_unlock(&consumer_data.lock); |
2618 | goto end; | |
2619 | } | |
ffe60014 | 2620 | ret = update_poll_array(ctx, &pollfd, local_stream, |
9a2fcf78 | 2621 | data_ht, &nb_inactive_fd); |
3bd1e081 MD |
2622 | if (ret < 0) { |
2623 | ERR("Error in allocating pollfd or local_outfds"); | |
f73fabfd | 2624 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2625 | pthread_mutex_unlock(&consumer_data.lock); |
2626 | goto end; | |
2627 | } | |
2628 | nb_fd = ret; | |
2629 | consumer_data.need_update = 0; | |
2630 | } | |
2631 | pthread_mutex_unlock(&consumer_data.lock); | |
2632 | ||
4078b776 | 2633 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
9a2fcf78 JD |
2634 | if (nb_fd == 0 && nb_inactive_fd == 0 && |
2635 | CMM_LOAD_SHARED(consumer_quit) == 1) { | |
1fc79fb4 | 2636 | err = 0; /* All is OK */ |
4078b776 MD |
2637 | goto end; |
2638 | } | |
3bd1e081 | 2639 | /* poll on the array of fds */ |
88f2b785 | 2640 | restart: |
261de637 | 2641 | DBG("polling on %d fd", nb_fd + nb_pipes_fd); |
cf0bcb51 JG |
2642 | if (testpoint(consumerd_thread_data_poll)) { |
2643 | goto end; | |
2644 | } | |
9ce5646a | 2645 | health_poll_entry(); |
261de637 | 2646 | num_rdy = poll(pollfd, nb_fd + nb_pipes_fd, -1); |
9ce5646a | 2647 | health_poll_exit(); |
3bd1e081 MD |
2648 | DBG("poll num_rdy : %d", num_rdy); |
2649 | if (num_rdy == -1) { | |
88f2b785 MD |
2650 | /* |
2651 | * Restart interrupted system call. | |
2652 | */ | |
2653 | if (errno == EINTR) { | |
2654 | goto restart; | |
2655 | } | |
7a57cf92 | 2656 | PERROR("Poll error"); |
f73fabfd | 2657 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2658 | goto end; |
2659 | } else if (num_rdy == 0) { | |
2660 | DBG("Polling thread timed out"); | |
2661 | goto end; | |
2662 | } | |
2663 | ||
80957876 JG |
2664 | if (caa_unlikely(data_consumption_paused)) { |
2665 | DBG("Data consumption paused, sleeping..."); | |
2666 | sleep(1); | |
2667 | goto restart; | |
2668 | } | |
2669 | ||
3bd1e081 | 2670 | /* |
50f8ae69 | 2671 | * If the consumer_data_pipe triggered poll go directly to the |
00e2e675 DG |
2672 | * beginning of the loop to update the array. We want to prioritize |
2673 | * array update over low-priority reads. | |
3bd1e081 | 2674 | */ |
509bb1cf | 2675 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
ab30f567 | 2676 | ssize_t pipe_readlen; |
04fdd819 | 2677 | |
50f8ae69 | 2678 | DBG("consumer_data_pipe wake up"); |
acdb9057 DG |
2679 | pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe, |
2680 | &new_stream, sizeof(new_stream)); | |
6cd525e8 MD |
2681 | if (pipe_readlen < sizeof(new_stream)) { |
2682 | PERROR("Consumer data pipe"); | |
23f5f35d DG |
2683 | /* Continue so we can at least handle the current stream(s). */ |
2684 | continue; | |
2685 | } | |
c869f647 DG |
2686 | |
2687 | /* | |
2688 | * If the stream is NULL, just ignore it. It's also possible that | |
2689 | * the sessiond poll thread changed the consumer_quit state and is | |
2690 | * waking us up to test it. | |
2691 | */ | |
2692 | if (new_stream == NULL) { | |
8994307f | 2693 | validate_endpoint_status_data_stream(); |
c869f647 DG |
2694 | continue; |
2695 | } | |
2696 | ||
c869f647 | 2697 | /* Continue to update the local streams and handle prio ones */ |
3bd1e081 MD |
2698 | continue; |
2699 | } | |
2700 | ||
02b3d176 DG |
2701 | /* Handle wakeup pipe. */ |
2702 | if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) { | |
2703 | char dummy; | |
2704 | ssize_t pipe_readlen; | |
2705 | ||
2706 | pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy, | |
2707 | sizeof(dummy)); | |
2708 | if (pipe_readlen < 0) { | |
2709 | PERROR("Consumer data wakeup pipe"); | |
2710 | } | |
2711 | /* We've been awakened to handle stream(s). */ | |
2712 | ctx->has_wakeup = 0; | |
2713 | } | |
2714 | ||
3bd1e081 MD |
2715 | /* Take care of high priority channels first. */ |
2716 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2717 | health_code_update(); |
2718 | ||
9617607b DG |
2719 | if (local_stream[i] == NULL) { |
2720 | continue; | |
2721 | } | |
fb3a43a9 | 2722 | if (pollfd[i].revents & POLLPRI) { |
d41f73b7 MD |
2723 | DBG("Urgent read on fd %d", pollfd[i].fd); |
2724 | high_prio = 1; | |
4078b776 | 2725 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
d41f73b7 | 2726 | /* it's ok to have an unavailable sub-buffer */ |
b64403e3 | 2727 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2728 | /* Clean the stream and free it. */ |
2729 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2730 | local_stream[i] = NULL; |
4078b776 MD |
2731 | } else if (len > 0) { |
2732 | local_stream[i]->data_read = 1; | |
d41f73b7 | 2733 | } |
3bd1e081 MD |
2734 | } |
2735 | } | |
2736 | ||
4078b776 MD |
2737 | /* |
2738 | * If we read high prio channel in this loop, try again | |
2739 | * for more high prio data. | |
2740 | */ | |
2741 | if (high_prio) { | |
3bd1e081 MD |
2742 | continue; |
2743 | } | |
2744 | ||
2745 | /* Take care of low priority channels. */ | |
4078b776 | 2746 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2747 | health_code_update(); |
2748 | ||
9617607b DG |
2749 | if (local_stream[i] == NULL) { |
2750 | continue; | |
2751 | } | |
4078b776 | 2752 | if ((pollfd[i].revents & POLLIN) || |
02b3d176 DG |
2753 | local_stream[i]->hangup_flush_done || |
2754 | local_stream[i]->has_data) { | |
4078b776 MD |
2755 | DBG("Normal read on fd %d", pollfd[i].fd); |
2756 | len = ctx->on_buffer_ready(local_stream[i], ctx); | |
2757 | /* it's ok to have an unavailable sub-buffer */ | |
b64403e3 | 2758 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2759 | /* Clean the stream and free it. */ |
2760 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2761 | local_stream[i] = NULL; |
4078b776 MD |
2762 | } else if (len > 0) { |
2763 | local_stream[i]->data_read = 1; | |
2764 | } | |
2765 | } | |
2766 | } | |
2767 | ||
2768 | /* Handle hangup and errors */ | |
2769 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2770 | health_code_update(); |
2771 | ||
9617607b DG |
2772 | if (local_stream[i] == NULL) { |
2773 | continue; | |
2774 | } | |
4078b776 MD |
2775 | if (!local_stream[i]->hangup_flush_done |
2776 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) | |
2777 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2778 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2779 | DBG("fd %d is hup|err|nval. Attempting flush and read.", | |
9617607b | 2780 | pollfd[i].fd); |
4078b776 MD |
2781 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
2782 | /* Attempt read again, for the data we just flushed. */ | |
2783 | local_stream[i]->data_read = 1; | |
2784 | } | |
2785 | /* | |
2786 | * If the poll flag is HUP/ERR/NVAL and we have | |
2787 | * read no data in this pass, we can remove the | |
2788 | * stream from its hash table. | |
2789 | */ | |
2790 | if ((pollfd[i].revents & POLLHUP)) { | |
2791 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); | |
2792 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2793 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2794 | local_stream[i] = NULL; |
4078b776 MD |
2795 | num_hup++; |
2796 | } | |
2797 | } else if (pollfd[i].revents & POLLERR) { | |
2798 | ERR("Error returned in polling fd %d.", pollfd[i].fd); | |
2799 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2800 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2801 | local_stream[i] = NULL; |
4078b776 MD |
2802 | num_hup++; |
2803 | } | |
2804 | } else if (pollfd[i].revents & POLLNVAL) { | |
2805 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); | |
2806 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2807 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2808 | local_stream[i] = NULL; |
4078b776 | 2809 | num_hup++; |
3bd1e081 MD |
2810 | } |
2811 | } | |
9617607b DG |
2812 | if (local_stream[i] != NULL) { |
2813 | local_stream[i]->data_read = 0; | |
2814 | } | |
3bd1e081 MD |
2815 | } |
2816 | } | |
1fc79fb4 MD |
2817 | /* All is OK */ |
2818 | err = 0; | |
3bd1e081 MD |
2819 | end: |
2820 | DBG("polling thread exiting"); | |
0e428499 DG |
2821 | free(pollfd); |
2822 | free(local_stream); | |
fb3a43a9 DG |
2823 | |
2824 | /* | |
2825 | * Close the write side of the pipe so epoll_wait() in | |
7d980def DG |
2826 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
2827 | * read side of the pipe. If we close them both, epoll_wait strangely does | |
2828 | * not return and could create a endless wait period if the pipe is the | |
2829 | * only tracked fd in the poll set. The thread will take care of closing | |
2830 | * the read side. | |
fb3a43a9 | 2831 | */ |
13886d2d | 2832 | (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe); |
fb3a43a9 | 2833 | |
2d57de81 | 2834 | error_testpoint: |
1fc79fb4 MD |
2835 | if (err) { |
2836 | health_error(); | |
2837 | ERR("Health error occurred in %s", __func__); | |
2838 | } | |
2839 | health_unregister(health_consumerd); | |
2840 | ||
e7b994a3 | 2841 | rcu_unregister_thread(); |
3bd1e081 MD |
2842 | return NULL; |
2843 | } | |
2844 | ||
d8ef542d MD |
2845 | /* |
2846 | * Close wake-up end of each stream belonging to the channel. This will | |
2847 | * allow the poll() on the stream read-side to detect when the | |
2848 | * write-side (application) finally closes them. | |
2849 | */ | |
2850 | static | |
2851 | void consumer_close_channel_streams(struct lttng_consumer_channel *channel) | |
2852 | { | |
2853 | struct lttng_ht *ht; | |
2854 | struct lttng_consumer_stream *stream; | |
2855 | struct lttng_ht_iter iter; | |
2856 | ||
2857 | ht = consumer_data.stream_per_chan_id_ht; | |
2858 | ||
2859 | rcu_read_lock(); | |
2860 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
2861 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
2862 | ht->match_fct, &channel->key, | |
2863 | &iter.iter, stream, node_channel_id.node) { | |
f2ad556d MD |
2864 | /* |
2865 | * Protect against teardown with mutex. | |
2866 | */ | |
2867 | pthread_mutex_lock(&stream->lock); | |
2868 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
2869 | goto next; | |
2870 | } | |
d8ef542d MD |
2871 | switch (consumer_data.type) { |
2872 | case LTTNG_CONSUMER_KERNEL: | |
2873 | break; | |
2874 | case LTTNG_CONSUMER32_UST: | |
2875 | case LTTNG_CONSUMER64_UST: | |
b4a650f3 DG |
2876 | if (stream->metadata_flag) { |
2877 | /* Safe and protected by the stream lock. */ | |
2878 | lttng_ustconsumer_close_metadata(stream->chan); | |
2879 | } else { | |
2880 | /* | |
2881 | * Note: a mutex is taken internally within | |
2882 | * liblttng-ust-ctl to protect timer wakeup_fd | |
2883 | * use from concurrent close. | |
2884 | */ | |
2885 | lttng_ustconsumer_close_stream_wakeup(stream); | |
2886 | } | |
d8ef542d MD |
2887 | break; |
2888 | default: | |
2889 | ERR("Unknown consumer_data type"); | |
2890 | assert(0); | |
2891 | } | |
f2ad556d MD |
2892 | next: |
2893 | pthread_mutex_unlock(&stream->lock); | |
d8ef542d MD |
2894 | } |
2895 | rcu_read_unlock(); | |
2896 | } | |
2897 | ||
2898 | static void destroy_channel_ht(struct lttng_ht *ht) | |
2899 | { | |
2900 | struct lttng_ht_iter iter; | |
2901 | struct lttng_consumer_channel *channel; | |
2902 | int ret; | |
2903 | ||
2904 | if (ht == NULL) { | |
2905 | return; | |
2906 | } | |
2907 | ||
2908 | rcu_read_lock(); | |
2909 | cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) { | |
2910 | ret = lttng_ht_del(ht, &iter); | |
2911 | assert(ret != 0); | |
2912 | } | |
2913 | rcu_read_unlock(); | |
2914 | ||
2915 | lttng_ht_destroy(ht); | |
2916 | } | |
2917 | ||
2918 | /* | |
2919 | * This thread polls the channel fds to detect when they are being | |
2920 | * closed. It closes all related streams if the channel is detected as | |
2921 | * closed. It is currently only used as a shim layer for UST because the | |
2922 | * consumerd needs to keep the per-stream wakeup end of pipes open for | |
2923 | * periodical flush. | |
2924 | */ | |
2925 | void *consumer_thread_channel_poll(void *data) | |
2926 | { | |
1fc79fb4 | 2927 | int ret, i, pollfd, err = -1; |
d8ef542d MD |
2928 | uint32_t revents, nb_fd; |
2929 | struct lttng_consumer_channel *chan = NULL; | |
2930 | struct lttng_ht_iter iter; | |
2931 | struct lttng_ht_node_u64 *node; | |
2932 | struct lttng_poll_event events; | |
2933 | struct lttng_consumer_local_data *ctx = data; | |
2934 | struct lttng_ht *channel_ht; | |
2935 | ||
2936 | rcu_register_thread(); | |
2937 | ||
1fc79fb4 MD |
2938 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL); |
2939 | ||
2d57de81 MD |
2940 | if (testpoint(consumerd_thread_channel)) { |
2941 | goto error_testpoint; | |
2942 | } | |
2943 | ||
9ce5646a MD |
2944 | health_code_update(); |
2945 | ||
d8ef542d MD |
2946 | channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2947 | if (!channel_ht) { | |
2948 | /* ENOMEM at this point. Better to bail out. */ | |
2949 | goto end_ht; | |
2950 | } | |
2951 | ||
2952 | DBG("Thread channel poll started"); | |
2953 | ||
2954 | /* Size is set to 1 for the consumer_channel pipe */ | |
2955 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2956 | if (ret < 0) { | |
2957 | ERR("Poll set creation failed"); | |
2958 | goto end_poll; | |
2959 | } | |
2960 | ||
2961 | ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN); | |
2962 | if (ret < 0) { | |
2963 | goto end; | |
2964 | } | |
2965 | ||
2966 | /* Main loop */ | |
2967 | DBG("Channel main loop started"); | |
2968 | ||
2969 | while (1) { | |
d8ef542d | 2970 | restart: |
7fa2082e MD |
2971 | health_code_update(); |
2972 | DBG("Channel poll wait"); | |
9ce5646a | 2973 | health_poll_entry(); |
d8ef542d | 2974 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2975 | DBG("Channel poll return from wait with %d fd(s)", |
2976 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2977 | health_poll_exit(); |
40063ead | 2978 | DBG("Channel event caught in thread"); |
d8ef542d MD |
2979 | if (ret < 0) { |
2980 | if (errno == EINTR) { | |
40063ead | 2981 | ERR("Poll EINTR caught"); |
d8ef542d MD |
2982 | goto restart; |
2983 | } | |
d9607cd7 MD |
2984 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2985 | err = 0; /* All is OK */ | |
2986 | } | |
d8ef542d MD |
2987 | goto end; |
2988 | } | |
2989 | ||
2990 | nb_fd = ret; | |
2991 | ||
2992 | /* From here, the event is a channel wait fd */ | |
2993 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2994 | health_code_update(); |
2995 | ||
d8ef542d MD |
2996 | revents = LTTNG_POLL_GETEV(&events, i); |
2997 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2998 | ||
d8ef542d | 2999 | if (pollfd == ctx->consumer_channel_pipe[0]) { |
03e43155 | 3000 | if (revents & LPOLLIN) { |
d8ef542d | 3001 | enum consumer_channel_action action; |
a0cbdd2e | 3002 | uint64_t key; |
d8ef542d | 3003 | |
a0cbdd2e | 3004 | ret = read_channel_pipe(ctx, &chan, &key, &action); |
d8ef542d | 3005 | if (ret <= 0) { |
03e43155 MD |
3006 | if (ret < 0) { |
3007 | ERR("Error reading channel pipe"); | |
3008 | } | |
3009 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
d8ef542d MD |
3010 | continue; |
3011 | } | |
3012 | ||
3013 | switch (action) { | |
3014 | case CONSUMER_CHANNEL_ADD: | |
3015 | DBG("Adding channel %d to poll set", | |
3016 | chan->wait_fd); | |
3017 | ||
3018 | lttng_ht_node_init_u64(&chan->wait_fd_node, | |
3019 | chan->wait_fd); | |
c7260a81 | 3020 | rcu_read_lock(); |
d8ef542d MD |
3021 | lttng_ht_add_unique_u64(channel_ht, |
3022 | &chan->wait_fd_node); | |
c7260a81 | 3023 | rcu_read_unlock(); |
d8ef542d MD |
3024 | /* Add channel to the global poll events list */ |
3025 | lttng_poll_add(&events, chan->wait_fd, | |
03e43155 | 3026 | LPOLLERR | LPOLLHUP); |
d8ef542d | 3027 | break; |
a0cbdd2e MD |
3028 | case CONSUMER_CHANNEL_DEL: |
3029 | { | |
b4a650f3 DG |
3030 | /* |
3031 | * This command should never be called if the channel | |
3032 | * has streams monitored by either the data or metadata | |
3033 | * thread. The consumer only notify this thread with a | |
3034 | * channel del. command if it receives a destroy | |
3035 | * channel command from the session daemon that send it | |
3036 | * if a command prior to the GET_CHANNEL failed. | |
3037 | */ | |
3038 | ||
c7260a81 | 3039 | rcu_read_lock(); |
a0cbdd2e MD |
3040 | chan = consumer_find_channel(key); |
3041 | if (!chan) { | |
c7260a81 | 3042 | rcu_read_unlock(); |
a0cbdd2e MD |
3043 | ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key); |
3044 | break; | |
3045 | } | |
3046 | lttng_poll_del(&events, chan->wait_fd); | |
f623cc0b | 3047 | iter.iter.node = &chan->wait_fd_node.node; |
a0cbdd2e MD |
3048 | ret = lttng_ht_del(channel_ht, &iter); |
3049 | assert(ret == 0); | |
a0cbdd2e | 3050 | |
f2a444f1 DG |
3051 | switch (consumer_data.type) { |
3052 | case LTTNG_CONSUMER_KERNEL: | |
3053 | break; | |
3054 | case LTTNG_CONSUMER32_UST: | |
3055 | case LTTNG_CONSUMER64_UST: | |
212d67a2 DG |
3056 | health_code_update(); |
3057 | /* Destroy streams that might have been left in the stream list. */ | |
3058 | clean_channel_stream_list(chan); | |
f2a444f1 DG |
3059 | break; |
3060 | default: | |
3061 | ERR("Unknown consumer_data type"); | |
3062 | assert(0); | |
3063 | } | |
3064 | ||
a0cbdd2e MD |
3065 | /* |
3066 | * Release our own refcount. Force channel deletion even if | |
3067 | * streams were not initialized. | |
3068 | */ | |
3069 | if (!uatomic_sub_return(&chan->refcount, 1)) { | |
3070 | consumer_del_channel(chan); | |
3071 | } | |
c7260a81 | 3072 | rcu_read_unlock(); |
a0cbdd2e MD |
3073 | goto restart; |
3074 | } | |
d8ef542d MD |
3075 | case CONSUMER_CHANNEL_QUIT: |
3076 | /* | |
3077 | * Remove the pipe from the poll set and continue the loop | |
3078 | * since their might be data to consume. | |
3079 | */ | |
3080 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
3081 | continue; | |
3082 | default: | |
3083 | ERR("Unknown action"); | |
3084 | break; | |
3085 | } | |
03e43155 MD |
3086 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
3087 | DBG("Channel thread pipe hung up"); | |
3088 | /* | |
3089 | * Remove the pipe from the poll set and continue the loop | |
3090 | * since their might be data to consume. | |
3091 | */ | |
3092 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
3093 | continue; | |
3094 | } else { | |
3095 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3096 | goto end; | |
d8ef542d MD |
3097 | } |
3098 | ||
3099 | /* Handle other stream */ | |
3100 | continue; | |
3101 | } | |
3102 | ||
3103 | rcu_read_lock(); | |
3104 | { | |
3105 | uint64_t tmp_id = (uint64_t) pollfd; | |
3106 | ||
3107 | lttng_ht_lookup(channel_ht, &tmp_id, &iter); | |
3108 | } | |
3109 | node = lttng_ht_iter_get_node_u64(&iter); | |
3110 | assert(node); | |
3111 | ||
3112 | chan = caa_container_of(node, struct lttng_consumer_channel, | |
3113 | wait_fd_node); | |
3114 | ||
3115 | /* Check for error event */ | |
3116 | if (revents & (LPOLLERR | LPOLLHUP)) { | |
3117 | DBG("Channel fd %d is hup|err.", pollfd); | |
3118 | ||
3119 | lttng_poll_del(&events, chan->wait_fd); | |
3120 | ret = lttng_ht_del(channel_ht, &iter); | |
3121 | assert(ret == 0); | |
b4a650f3 DG |
3122 | |
3123 | /* | |
3124 | * This will close the wait fd for each stream associated to | |
3125 | * this channel AND monitored by the data/metadata thread thus | |
3126 | * will be clean by the right thread. | |
3127 | */ | |
d8ef542d | 3128 | consumer_close_channel_streams(chan); |
f2ad556d MD |
3129 | |
3130 | /* Release our own refcount */ | |
3131 | if (!uatomic_sub_return(&chan->refcount, 1) | |
3132 | && !uatomic_read(&chan->nb_init_stream_left)) { | |
3133 | consumer_del_channel(chan); | |
3134 | } | |
03e43155 MD |
3135 | } else { |
3136 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3137 | rcu_read_unlock(); | |
3138 | goto end; | |
d8ef542d MD |
3139 | } |
3140 | ||
3141 | /* Release RCU lock for the channel looked up */ | |
3142 | rcu_read_unlock(); | |
3143 | } | |
3144 | } | |
3145 | ||
1fc79fb4 MD |
3146 | /* All is OK */ |
3147 | err = 0; | |
d8ef542d MD |
3148 | end: |
3149 | lttng_poll_clean(&events); | |
3150 | end_poll: | |
3151 | destroy_channel_ht(channel_ht); | |
3152 | end_ht: | |
2d57de81 | 3153 | error_testpoint: |
d8ef542d | 3154 | DBG("Channel poll thread exiting"); |
1fc79fb4 MD |
3155 | if (err) { |
3156 | health_error(); | |
3157 | ERR("Health error occurred in %s", __func__); | |
3158 | } | |
3159 | health_unregister(health_consumerd); | |
d8ef542d MD |
3160 | rcu_unregister_thread(); |
3161 | return NULL; | |
3162 | } | |
3163 | ||
331744e3 JD |
3164 | static int set_metadata_socket(struct lttng_consumer_local_data *ctx, |
3165 | struct pollfd *sockpoll, int client_socket) | |
3166 | { | |
3167 | int ret; | |
3168 | ||
3169 | assert(ctx); | |
3170 | assert(sockpoll); | |
3171 | ||
84382d49 MD |
3172 | ret = lttng_consumer_poll_socket(sockpoll); |
3173 | if (ret) { | |
331744e3 JD |
3174 | goto error; |
3175 | } | |
3176 | DBG("Metadata connection on client_socket"); | |
3177 | ||
3178 | /* Blocking call, waiting for transmission */ | |
3179 | ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket); | |
3180 | if (ctx->consumer_metadata_socket < 0) { | |
3181 | WARN("On accept metadata"); | |
3182 | ret = -1; | |
3183 | goto error; | |
3184 | } | |
3185 | ret = 0; | |
3186 | ||
3187 | error: | |
3188 | return ret; | |
3189 | } | |
3190 | ||
3bd1e081 MD |
3191 | /* |
3192 | * This thread listens on the consumerd socket and receives the file | |
3193 | * descriptors from the session daemon. | |
3194 | */ | |
7d980def | 3195 | void *consumer_thread_sessiond_poll(void *data) |
3bd1e081 | 3196 | { |
1fc79fb4 | 3197 | int sock = -1, client_socket, ret, err = -1; |
3bd1e081 MD |
3198 | /* |
3199 | * structure to poll for incoming data on communication socket avoids | |
3200 | * making blocking sockets. | |
3201 | */ | |
3202 | struct pollfd consumer_sockpoll[2]; | |
3203 | struct lttng_consumer_local_data *ctx = data; | |
3204 | ||
e7b994a3 DG |
3205 | rcu_register_thread(); |
3206 | ||
1fc79fb4 MD |
3207 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND); |
3208 | ||
2d57de81 MD |
3209 | if (testpoint(consumerd_thread_sessiond)) { |
3210 | goto error_testpoint; | |
3211 | } | |
3212 | ||
9ce5646a MD |
3213 | health_code_update(); |
3214 | ||
3bd1e081 MD |
3215 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
3216 | unlink(ctx->consumer_command_sock_path); | |
3217 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); | |
3218 | if (client_socket < 0) { | |
3219 | ERR("Cannot create command socket"); | |
3220 | goto end; | |
3221 | } | |
3222 | ||
3223 | ret = lttcomm_listen_unix_sock(client_socket); | |
3224 | if (ret < 0) { | |
3225 | goto end; | |
3226 | } | |
3227 | ||
32258573 | 3228 | DBG("Sending ready command to lttng-sessiond"); |
f73fabfd | 3229 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
3bd1e081 MD |
3230 | /* return < 0 on error, but == 0 is not fatal */ |
3231 | if (ret < 0) { | |
32258573 | 3232 | ERR("Error sending ready command to lttng-sessiond"); |
3bd1e081 MD |
3233 | goto end; |
3234 | } | |
3235 | ||
3bd1e081 MD |
3236 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
3237 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; | |
3238 | consumer_sockpoll[0].events = POLLIN | POLLPRI; | |
3239 | consumer_sockpoll[1].fd = client_socket; | |
3240 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3241 | ||
84382d49 MD |
3242 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3243 | if (ret) { | |
3244 | if (ret > 0) { | |
3245 | /* should exit */ | |
3246 | err = 0; | |
3247 | } | |
3bd1e081 MD |
3248 | goto end; |
3249 | } | |
3250 | DBG("Connection on client_socket"); | |
3251 | ||
3252 | /* Blocking call, waiting for transmission */ | |
3253 | sock = lttcomm_accept_unix_sock(client_socket); | |
534d2592 | 3254 | if (sock < 0) { |
3bd1e081 MD |
3255 | WARN("On accept"); |
3256 | goto end; | |
3257 | } | |
3bd1e081 | 3258 | |
331744e3 JD |
3259 | /* |
3260 | * Setup metadata socket which is the second socket connection on the | |
3261 | * command unix socket. | |
3262 | */ | |
3263 | ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket); | |
84382d49 MD |
3264 | if (ret) { |
3265 | if (ret > 0) { | |
3266 | /* should exit */ | |
3267 | err = 0; | |
3268 | } | |
331744e3 JD |
3269 | goto end; |
3270 | } | |
3271 | ||
d96f09c6 DG |
3272 | /* This socket is not useful anymore. */ |
3273 | ret = close(client_socket); | |
3274 | if (ret < 0) { | |
3275 | PERROR("close client_socket"); | |
3276 | } | |
3277 | client_socket = -1; | |
3278 | ||
3bd1e081 MD |
3279 | /* update the polling structure to poll on the established socket */ |
3280 | consumer_sockpoll[1].fd = sock; | |
3281 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3282 | ||
3283 | while (1) { | |
9ce5646a MD |
3284 | health_code_update(); |
3285 | ||
3286 | health_poll_entry(); | |
3287 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
3288 | health_poll_exit(); | |
84382d49 MD |
3289 | if (ret) { |
3290 | if (ret > 0) { | |
3291 | /* should exit */ | |
3292 | err = 0; | |
3293 | } | |
3bd1e081 MD |
3294 | goto end; |
3295 | } | |
3296 | DBG("Incoming command on sock"); | |
3297 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
4cbc1a04 DG |
3298 | if (ret <= 0) { |
3299 | /* | |
3300 | * This could simply be a session daemon quitting. Don't output | |
3301 | * ERR() here. | |
3302 | */ | |
3303 | DBG("Communication interrupted on command socket"); | |
41ba6035 | 3304 | err = 0; |
3bd1e081 MD |
3305 | goto end; |
3306 | } | |
10211f5c | 3307 | if (CMM_LOAD_SHARED(consumer_quit)) { |
3bd1e081 | 3308 | DBG("consumer_thread_receive_fds received quit from signal"); |
1fc79fb4 | 3309 | err = 0; /* All is OK */ |
3bd1e081 MD |
3310 | goto end; |
3311 | } | |
ffe60014 | 3312 | DBG("received command on sock"); |
3bd1e081 | 3313 | } |
1fc79fb4 MD |
3314 | /* All is OK */ |
3315 | err = 0; | |
3316 | ||
3bd1e081 | 3317 | end: |
ffe60014 | 3318 | DBG("Consumer thread sessiond poll exiting"); |
3bd1e081 | 3319 | |
d88aee68 DG |
3320 | /* |
3321 | * Close metadata streams since the producer is the session daemon which | |
3322 | * just died. | |
3323 | * | |
3324 | * NOTE: for now, this only applies to the UST tracer. | |
3325 | */ | |
6d574024 | 3326 | lttng_consumer_close_all_metadata(); |
d88aee68 | 3327 | |
3bd1e081 MD |
3328 | /* |
3329 | * when all fds have hung up, the polling thread | |
3330 | * can exit cleanly | |
3331 | */ | |
10211f5c | 3332 | CMM_STORE_SHARED(consumer_quit, 1); |
3bd1e081 | 3333 | |
04fdd819 | 3334 | /* |
c869f647 | 3335 | * Notify the data poll thread to poll back again and test the |
8994307f | 3336 | * consumer_quit state that we just set so to quit gracefully. |
04fdd819 | 3337 | */ |
acdb9057 | 3338 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
c869f647 | 3339 | |
a0cbdd2e | 3340 | notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT); |
d8ef542d | 3341 | |
5c635c72 MD |
3342 | notify_health_quit_pipe(health_quit_pipe); |
3343 | ||
d96f09c6 DG |
3344 | /* Cleaning up possibly open sockets. */ |
3345 | if (sock >= 0) { | |
3346 | ret = close(sock); | |
3347 | if (ret < 0) { | |
3348 | PERROR("close sock sessiond poll"); | |
3349 | } | |
3350 | } | |
3351 | if (client_socket >= 0) { | |
38476d24 | 3352 | ret = close(client_socket); |
d96f09c6 DG |
3353 | if (ret < 0) { |
3354 | PERROR("close client_socket sessiond poll"); | |
3355 | } | |
3356 | } | |
3357 | ||
2d57de81 | 3358 | error_testpoint: |
1fc79fb4 MD |
3359 | if (err) { |
3360 | health_error(); | |
3361 | ERR("Health error occurred in %s", __func__); | |
3362 | } | |
3363 | health_unregister(health_consumerd); | |
3364 | ||
e7b994a3 | 3365 | rcu_unregister_thread(); |
3bd1e081 MD |
3366 | return NULL; |
3367 | } | |
d41f73b7 | 3368 | |
4078b776 | 3369 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
3370 | struct lttng_consumer_local_data *ctx) |
3371 | { | |
74251bb8 | 3372 | ssize_t ret; |
02d02e31 JD |
3373 | int rotate_ret; |
3374 | bool rotated = false; | |
74251bb8 DG |
3375 | |
3376 | pthread_mutex_lock(&stream->lock); | |
94d49140 JD |
3377 | if (stream->metadata_flag) { |
3378 | pthread_mutex_lock(&stream->metadata_rdv_lock); | |
3379 | } | |
74251bb8 | 3380 | |
d41f73b7 MD |
3381 | switch (consumer_data.type) { |
3382 | case LTTNG_CONSUMER_KERNEL: | |
02d02e31 | 3383 | ret = lttng_kconsumer_read_subbuffer(stream, ctx, &rotated); |
74251bb8 | 3384 | break; |
7753dea8 MD |
3385 | case LTTNG_CONSUMER32_UST: |
3386 | case LTTNG_CONSUMER64_UST: | |
02d02e31 | 3387 | ret = lttng_ustconsumer_read_subbuffer(stream, ctx, &rotated); |
74251bb8 | 3388 | break; |
d41f73b7 MD |
3389 | default: |
3390 | ERR("Unknown consumer_data type"); | |
3391 | assert(0); | |
74251bb8 DG |
3392 | ret = -ENOSYS; |
3393 | break; | |
d41f73b7 | 3394 | } |
74251bb8 | 3395 | |
94d49140 JD |
3396 | if (stream->metadata_flag) { |
3397 | pthread_cond_broadcast(&stream->metadata_rdv); | |
3398 | pthread_mutex_unlock(&stream->metadata_rdv_lock); | |
3399 | } | |
74251bb8 | 3400 | pthread_mutex_unlock(&stream->lock); |
02d02e31 JD |
3401 | if (rotated) { |
3402 | rotate_ret = consumer_post_rotation(stream, ctx); | |
3403 | if (rotate_ret < 0) { | |
3404 | ERR("Failed after a rotation"); | |
3405 | ret = -1; | |
3406 | } | |
3407 | } | |
3408 | ||
74251bb8 | 3409 | return ret; |
d41f73b7 MD |
3410 | } |
3411 | ||
3412 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
3413 | { | |
3414 | switch (consumer_data.type) { | |
3415 | case LTTNG_CONSUMER_KERNEL: | |
3416 | return lttng_kconsumer_on_recv_stream(stream); | |
7753dea8 MD |
3417 | case LTTNG_CONSUMER32_UST: |
3418 | case LTTNG_CONSUMER64_UST: | |
d41f73b7 MD |
3419 | return lttng_ustconsumer_on_recv_stream(stream); |
3420 | default: | |
3421 | ERR("Unknown consumer_data type"); | |
3422 | assert(0); | |
3423 | return -ENOSYS; | |
3424 | } | |
3425 | } | |
e4421fec DG |
3426 | |
3427 | /* | |
3428 | * Allocate and set consumer data hash tables. | |
3429 | */ | |
282dadbc | 3430 | int lttng_consumer_init(void) |
e4421fec | 3431 | { |
d88aee68 | 3432 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3433 | if (!consumer_data.channel_ht) { |
3434 | goto error; | |
3435 | } | |
3436 | ||
0fc67d9b JG |
3437 | consumer_data.channels_by_session_id_ht = |
3438 | lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3439 | if (!consumer_data.channels_by_session_id_ht) { | |
3440 | goto error; | |
3441 | } | |
3442 | ||
d88aee68 | 3443 | consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3444 | if (!consumer_data.relayd_ht) { |
3445 | goto error; | |
3446 | } | |
3447 | ||
d88aee68 | 3448 | consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3449 | if (!consumer_data.stream_list_ht) { |
3450 | goto error; | |
3451 | } | |
3452 | ||
d8ef542d | 3453 | consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3454 | if (!consumer_data.stream_per_chan_id_ht) { |
3455 | goto error; | |
3456 | } | |
3457 | ||
3458 | data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3459 | if (!data_ht) { | |
3460 | goto error; | |
3461 | } | |
3462 | ||
3463 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3464 | if (!metadata_ht) { | |
3465 | goto error; | |
3466 | } | |
3467 | ||
a335e585 JG |
3468 | consumer_data.chunk_registry = lttng_trace_chunk_registry_create(); |
3469 | if (!consumer_data.chunk_registry) { | |
3470 | goto error; | |
3471 | } | |
3472 | ||
282dadbc MD |
3473 | return 0; |
3474 | ||
3475 | error: | |
3476 | return -1; | |
e4421fec | 3477 | } |
7735ef9e DG |
3478 | |
3479 | /* | |
3480 | * Process the ADD_RELAYD command receive by a consumer. | |
3481 | * | |
3482 | * This will create a relayd socket pair and add it to the relayd hash table. | |
3483 | * The caller MUST acquire a RCU read side lock before calling it. | |
3484 | */ | |
2527bf85 | 3485 | void consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type, |
7735ef9e | 3486 | struct lttng_consumer_local_data *ctx, int sock, |
6151a90f | 3487 | struct pollfd *consumer_sockpoll, |
d3e2ba59 JD |
3488 | struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id, |
3489 | uint64_t relayd_session_id) | |
7735ef9e | 3490 | { |
cd2b09ed | 3491 | int fd = -1, ret = -1, relayd_created = 0; |
0c759fc9 | 3492 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
d4298c99 | 3493 | struct consumer_relayd_sock_pair *relayd = NULL; |
7735ef9e | 3494 | |
6151a90f JD |
3495 | assert(ctx); |
3496 | assert(relayd_sock); | |
3497 | ||
da009f2c | 3498 | DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx); |
7735ef9e DG |
3499 | |
3500 | /* Get relayd reference if exists. */ | |
3501 | relayd = consumer_find_relayd(net_seq_idx); | |
3502 | if (relayd == NULL) { | |
da009f2c | 3503 | assert(sock_type == LTTNG_STREAM_CONTROL); |
7735ef9e DG |
3504 | /* Not found. Allocate one. */ |
3505 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); | |
3506 | if (relayd == NULL) { | |
618a6a28 MD |
3507 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
3508 | goto error; | |
0d08d75e | 3509 | } else { |
30319bcb | 3510 | relayd->sessiond_session_id = sessiond_id; |
0d08d75e | 3511 | relayd_created = 1; |
7735ef9e | 3512 | } |
0d08d75e DG |
3513 | |
3514 | /* | |
3515 | * This code path MUST continue to the consumer send status message to | |
3516 | * we can notify the session daemon and continue our work without | |
3517 | * killing everything. | |
3518 | */ | |
da009f2c MD |
3519 | } else { |
3520 | /* | |
3521 | * relayd key should never be found for control socket. | |
3522 | */ | |
3523 | assert(sock_type != LTTNG_STREAM_CONTROL); | |
0d08d75e DG |
3524 | } |
3525 | ||
3526 | /* First send a status message before receiving the fds. */ | |
0c759fc9 | 3527 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
618a6a28 | 3528 | if (ret < 0) { |
0d08d75e | 3529 | /* Somehow, the session daemon is not responding anymore. */ |
618a6a28 MD |
3530 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
3531 | goto error_nosignal; | |
7735ef9e DG |
3532 | } |
3533 | ||
3534 | /* Poll on consumer socket. */ | |
84382d49 MD |
3535 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3536 | if (ret) { | |
3537 | /* Needing to exit in the middle of a command: error. */ | |
0d08d75e | 3538 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
618a6a28 | 3539 | goto error_nosignal; |
7735ef9e DG |
3540 | } |
3541 | ||
3542 | /* Get relayd socket from session daemon */ | |
3543 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); | |
3544 | if (ret != sizeof(fd)) { | |
4028eeb9 | 3545 | fd = -1; /* Just in case it gets set with an invalid value. */ |
0d08d75e DG |
3546 | |
3547 | /* | |
3548 | * Failing to receive FDs might indicate a major problem such as | |
3549 | * reaching a fd limit during the receive where the kernel returns a | |
3550 | * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we | |
3551 | * don't take any chances and stop everything. | |
3552 | * | |
3553 | * XXX: Feature request #558 will fix that and avoid this possible | |
3554 | * issue when reaching the fd limit. | |
3555 | */ | |
3556 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); | |
618a6a28 | 3557 | ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD; |
f50f23d9 DG |
3558 | goto error; |
3559 | } | |
3560 | ||
7735ef9e DG |
3561 | /* Copy socket information and received FD */ |
3562 | switch (sock_type) { | |
3563 | case LTTNG_STREAM_CONTROL: | |
3564 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3565 | lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock); |
3566 | ret = lttcomm_create_sock(&relayd->control_sock.sock); | |
4028eeb9 | 3567 | /* Handle create_sock error. */ |
f66c074c | 3568 | if (ret < 0) { |
618a6a28 | 3569 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3570 | goto error; |
f66c074c | 3571 | } |
da009f2c MD |
3572 | /* |
3573 | * Close the socket created internally by | |
3574 | * lttcomm_create_sock, so we can replace it by the one | |
3575 | * received from sessiond. | |
3576 | */ | |
3577 | if (close(relayd->control_sock.sock.fd)) { | |
3578 | PERROR("close"); | |
3579 | } | |
7735ef9e DG |
3580 | |
3581 | /* Assign new file descriptor */ | |
6151a90f JD |
3582 | relayd->control_sock.sock.fd = fd; |
3583 | /* Assign version values. */ | |
3584 | relayd->control_sock.major = relayd_sock->major; | |
3585 | relayd->control_sock.minor = relayd_sock->minor; | |
c5b6f4f0 | 3586 | |
d3e2ba59 | 3587 | relayd->relayd_session_id = relayd_session_id; |
c5b6f4f0 | 3588 | |
7735ef9e DG |
3589 | break; |
3590 | case LTTNG_STREAM_DATA: | |
3591 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3592 | lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock); |
3593 | ret = lttcomm_create_sock(&relayd->data_sock.sock); | |
4028eeb9 | 3594 | /* Handle create_sock error. */ |
f66c074c | 3595 | if (ret < 0) { |
618a6a28 | 3596 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3597 | goto error; |
f66c074c | 3598 | } |
da009f2c MD |
3599 | /* |
3600 | * Close the socket created internally by | |
3601 | * lttcomm_create_sock, so we can replace it by the one | |
3602 | * received from sessiond. | |
3603 | */ | |
3604 | if (close(relayd->data_sock.sock.fd)) { | |
3605 | PERROR("close"); | |
3606 | } | |
7735ef9e DG |
3607 | |
3608 | /* Assign new file descriptor */ | |
6151a90f JD |
3609 | relayd->data_sock.sock.fd = fd; |
3610 | /* Assign version values. */ | |
3611 | relayd->data_sock.major = relayd_sock->major; | |
3612 | relayd->data_sock.minor = relayd_sock->minor; | |
7735ef9e DG |
3613 | break; |
3614 | default: | |
3615 | ERR("Unknown relayd socket type (%d)", sock_type); | |
618a6a28 | 3616 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
7735ef9e DG |
3617 | goto error; |
3618 | } | |
3619 | ||
d88aee68 | 3620 | DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)", |
7735ef9e DG |
3621 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
3622 | relayd->net_seq_idx, fd); | |
e303e76b FD |
3623 | /* |
3624 | * We gave the ownership of the fd to the relayd structure. Set the | |
3625 | * fd to -1 so we don't call close() on it in the error path below. | |
3626 | */ | |
3627 | fd = -1; | |
7735ef9e | 3628 | |
618a6a28 MD |
3629 | /* We successfully added the socket. Send status back. */ |
3630 | ret = consumer_send_status_msg(sock, ret_code); | |
3631 | if (ret < 0) { | |
3632 | /* Somehow, the session daemon is not responding anymore. */ | |
3633 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3634 | goto error_nosignal; | |
3635 | } | |
3636 | ||
7735ef9e DG |
3637 | /* |
3638 | * Add relayd socket pair to consumer data hashtable. If object already | |
3639 | * exists or on error, the function gracefully returns. | |
3640 | */ | |
11413bb9 | 3641 | relayd->ctx = ctx; |
d09e1200 | 3642 | add_relayd(relayd); |
7735ef9e DG |
3643 | |
3644 | /* All good! */ | |
2527bf85 | 3645 | return; |
7735ef9e DG |
3646 | |
3647 | error: | |
618a6a28 MD |
3648 | if (consumer_send_status_msg(sock, ret_code) < 0) { |
3649 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3650 | } | |
3651 | ||
3652 | error_nosignal: | |
4028eeb9 DG |
3653 | /* Close received socket if valid. */ |
3654 | if (fd >= 0) { | |
3655 | if (close(fd)) { | |
3656 | PERROR("close received socket"); | |
3657 | } | |
3658 | } | |
cd2b09ed DG |
3659 | |
3660 | if (relayd_created) { | |
cd2b09ed DG |
3661 | free(relayd); |
3662 | } | |
7735ef9e | 3663 | } |
ca22feea | 3664 | |
f7079f67 DG |
3665 | /* |
3666 | * Search for a relayd associated to the session id and return the reference. | |
3667 | * | |
3668 | * A rcu read side lock MUST be acquire before calling this function and locked | |
3669 | * until the relayd object is no longer necessary. | |
3670 | */ | |
3671 | static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id) | |
3672 | { | |
3673 | struct lttng_ht_iter iter; | |
f7079f67 | 3674 | struct consumer_relayd_sock_pair *relayd = NULL; |
f7079f67 DG |
3675 | |
3676 | /* Iterate over all relayd since they are indexed by net_seq_idx. */ | |
3677 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
3678 | node.node) { | |
18261bd1 DG |
3679 | /* |
3680 | * Check by sessiond id which is unique here where the relayd session | |
3681 | * id might not be when having multiple relayd. | |
3682 | */ | |
3683 | if (relayd->sessiond_session_id == id) { | |
f7079f67 | 3684 | /* Found the relayd. There can be only one per id. */ |
18261bd1 | 3685 | goto found; |
f7079f67 DG |
3686 | } |
3687 | } | |
3688 | ||
18261bd1 DG |
3689 | return NULL; |
3690 | ||
3691 | found: | |
f7079f67 DG |
3692 | return relayd; |
3693 | } | |
3694 | ||
ca22feea DG |
3695 | /* |
3696 | * Check if for a given session id there is still data needed to be extract | |
3697 | * from the buffers. | |
3698 | * | |
6d805429 | 3699 | * Return 1 if data is pending or else 0 meaning ready to be read. |
ca22feea | 3700 | */ |
6d805429 | 3701 | int consumer_data_pending(uint64_t id) |
ca22feea DG |
3702 | { |
3703 | int ret; | |
3704 | struct lttng_ht_iter iter; | |
3705 | struct lttng_ht *ht; | |
3706 | struct lttng_consumer_stream *stream; | |
f7079f67 | 3707 | struct consumer_relayd_sock_pair *relayd = NULL; |
6d805429 | 3708 | int (*data_pending)(struct lttng_consumer_stream *); |
ca22feea | 3709 | |
6d805429 | 3710 | DBG("Consumer data pending command on session id %" PRIu64, id); |
ca22feea | 3711 | |
6f6eda74 | 3712 | rcu_read_lock(); |
ca22feea DG |
3713 | pthread_mutex_lock(&consumer_data.lock); |
3714 | ||
3715 | switch (consumer_data.type) { | |
3716 | case LTTNG_CONSUMER_KERNEL: | |
6d805429 | 3717 | data_pending = lttng_kconsumer_data_pending; |
ca22feea DG |
3718 | break; |
3719 | case LTTNG_CONSUMER32_UST: | |
3720 | case LTTNG_CONSUMER64_UST: | |
6d805429 | 3721 | data_pending = lttng_ustconsumer_data_pending; |
ca22feea DG |
3722 | break; |
3723 | default: | |
3724 | ERR("Unknown consumer data type"); | |
3725 | assert(0); | |
3726 | } | |
3727 | ||
3728 | /* Ease our life a bit */ | |
3729 | ht = consumer_data.stream_list_ht; | |
3730 | ||
c8f59ee5 | 3731 | cds_lfht_for_each_entry_duplicate(ht->ht, |
d88aee68 DG |
3732 | ht->hash_fct(&id, lttng_ht_seed), |
3733 | ht->match_fct, &id, | |
ca22feea | 3734 | &iter.iter, stream, node_session_id.node) { |
7c87fb16 | 3735 | pthread_mutex_lock(&stream->lock); |
ca22feea | 3736 | |
4e9a4686 DG |
3737 | /* |
3738 | * A removed node from the hash table indicates that the stream has | |
3739 | * been deleted thus having a guarantee that the buffers are closed | |
3740 | * on the consumer side. However, data can still be transmitted | |
3741 | * over the network so don't skip the relayd check. | |
3742 | */ | |
3743 | ret = cds_lfht_is_node_deleted(&stream->node.node); | |
3744 | if (!ret) { | |
3745 | /* Check the stream if there is data in the buffers. */ | |
6d805429 DG |
3746 | ret = data_pending(stream); |
3747 | if (ret == 1) { | |
4e9a4686 | 3748 | pthread_mutex_unlock(&stream->lock); |
f7079f67 | 3749 | goto data_pending; |
4e9a4686 DG |
3750 | } |
3751 | } | |
3752 | ||
78f18a75 JR |
3753 | pthread_mutex_unlock(&stream->lock); |
3754 | } | |
3755 | ||
3756 | relayd = find_relayd_by_session_id(id); | |
3757 | if (relayd) { | |
3758 | unsigned int is_data_inflight = 0; | |
3759 | ||
3760 | /* Send init command for data pending. */ | |
3761 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
3762 | ret = relayd_begin_data_pending(&relayd->control_sock, | |
3763 | relayd->relayd_session_id); | |
3764 | if (ret < 0) { | |
3765 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
3766 | /* Communication error thus the relayd so no data pending. */ | |
3767 | goto data_not_pending; | |
3768 | } | |
3769 | ||
3770 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
3771 | ht->hash_fct(&id, lttng_ht_seed), | |
3772 | ht->match_fct, &id, | |
3773 | &iter.iter, stream, node_session_id.node) { | |
c8f59ee5 | 3774 | if (stream->metadata_flag) { |
ad7051c0 DG |
3775 | ret = relayd_quiescent_control(&relayd->control_sock, |
3776 | stream->relayd_stream_id); | |
c8f59ee5 | 3777 | } else { |
6d805429 | 3778 | ret = relayd_data_pending(&relayd->control_sock, |
39df6d9f DG |
3779 | stream->relayd_stream_id, |
3780 | stream->next_net_seq_num - 1); | |
c8f59ee5 | 3781 | } |
78f18a75 JR |
3782 | |
3783 | if (ret == 1) { | |
3784 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
3785 | goto data_pending; | |
3786 | } else if (ret < 0) { | |
11413bb9 JR |
3787 | ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
3788 | lttng_consumer_cleanup_relayd(relayd); | |
3789 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
11413bb9 JR |
3790 | goto data_not_pending; |
3791 | } | |
c8f59ee5 | 3792 | } |
f7079f67 | 3793 | |
78f18a75 | 3794 | /* Send end command for data pending. */ |
f7079f67 DG |
3795 | ret = relayd_end_data_pending(&relayd->control_sock, |
3796 | relayd->relayd_session_id, &is_data_inflight); | |
3797 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
bdd88757 | 3798 | if (ret < 0) { |
11413bb9 JR |
3799 | ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
3800 | lttng_consumer_cleanup_relayd(relayd); | |
f7079f67 DG |
3801 | goto data_not_pending; |
3802 | } | |
bdd88757 DG |
3803 | if (is_data_inflight) { |
3804 | goto data_pending; | |
3805 | } | |
f7079f67 DG |
3806 | } |
3807 | ||
ca22feea | 3808 | /* |
f7079f67 DG |
3809 | * Finding _no_ node in the hash table and no inflight data means that the |
3810 | * stream(s) have been removed thus data is guaranteed to be available for | |
3811 | * analysis from the trace files. | |
ca22feea DG |
3812 | */ |
3813 | ||
f7079f67 | 3814 | data_not_pending: |
ca22feea DG |
3815 | /* Data is available to be read by a viewer. */ |
3816 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 3817 | rcu_read_unlock(); |
6d805429 | 3818 | return 0; |
ca22feea | 3819 | |
f7079f67 | 3820 | data_pending: |
ca22feea DG |
3821 | /* Data is still being extracted from buffers. */ |
3822 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 3823 | rcu_read_unlock(); |
6d805429 | 3824 | return 1; |
ca22feea | 3825 | } |
f50f23d9 DG |
3826 | |
3827 | /* | |
3828 | * Send a ret code status message to the sessiond daemon. | |
3829 | * | |
3830 | * Return the sendmsg() return value. | |
3831 | */ | |
3832 | int consumer_send_status_msg(int sock, int ret_code) | |
3833 | { | |
3834 | struct lttcomm_consumer_status_msg msg; | |
3835 | ||
53efb85a | 3836 | memset(&msg, 0, sizeof(msg)); |
f50f23d9 DG |
3837 | msg.ret_code = ret_code; |
3838 | ||
3839 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
3840 | } | |
ffe60014 DG |
3841 | |
3842 | /* | |
3843 | * Send a channel status message to the sessiond daemon. | |
3844 | * | |
3845 | * Return the sendmsg() return value. | |
3846 | */ | |
3847 | int consumer_send_status_channel(int sock, | |
3848 | struct lttng_consumer_channel *channel) | |
3849 | { | |
3850 | struct lttcomm_consumer_status_channel msg; | |
3851 | ||
3852 | assert(sock >= 0); | |
3853 | ||
53efb85a | 3854 | memset(&msg, 0, sizeof(msg)); |
ffe60014 | 3855 | if (!channel) { |
0c759fc9 | 3856 | msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; |
ffe60014 | 3857 | } else { |
0c759fc9 | 3858 | msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
ffe60014 DG |
3859 | msg.key = channel->key; |
3860 | msg.stream_count = channel->streams.count; | |
3861 | } | |
3862 | ||
3863 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
3864 | } | |
5c786ded | 3865 | |
d07ceecd MD |
3866 | unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos, |
3867 | unsigned long produced_pos, uint64_t nb_packets_per_stream, | |
3868 | uint64_t max_sb_size) | |
5c786ded | 3869 | { |
d07ceecd | 3870 | unsigned long start_pos; |
5c786ded | 3871 | |
d07ceecd MD |
3872 | if (!nb_packets_per_stream) { |
3873 | return consumed_pos; /* Grab everything */ | |
3874 | } | |
3875 | start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size); | |
3876 | start_pos -= max_sb_size * nb_packets_per_stream; | |
3877 | if ((long) (start_pos - consumed_pos) < 0) { | |
3878 | return consumed_pos; /* Grab everything */ | |
3879 | } | |
3880 | return start_pos; | |
5c786ded | 3881 | } |
a1ae2ea5 | 3882 | |
b99a8d42 JD |
3883 | static |
3884 | int consumer_flush_buffer(struct lttng_consumer_stream *stream, int producer_active) | |
3885 | { | |
3886 | int ret = 0; | |
3887 | ||
3888 | switch (consumer_data.type) { | |
3889 | case LTTNG_CONSUMER_KERNEL: | |
3890 | ret = kernctl_buffer_flush(stream->wait_fd); | |
3891 | if (ret < 0) { | |
3892 | ERR("Failed to flush kernel stream"); | |
3893 | goto end; | |
3894 | } | |
3895 | break; | |
3896 | case LTTNG_CONSUMER32_UST: | |
3897 | case LTTNG_CONSUMER64_UST: | |
3898 | lttng_ustctl_flush_buffer(stream, producer_active); | |
3899 | break; | |
3900 | default: | |
3901 | ERR("Unknown consumer_data type"); | |
3902 | abort(); | |
3903 | } | |
3904 | ||
3905 | end: | |
3906 | return ret; | |
3907 | } | |
3908 | ||
3909 | /* | |
3910 | * Sample the rotate position for all the streams of a channel. If a stream | |
3911 | * is already at the rotate position (produced == consumed), we flag it as | |
3912 | * ready for rotation. The rotation of ready streams occurs after we have | |
3913 | * replied to the session daemon that we have finished sampling the positions. | |
e96d66b4 | 3914 | * Must be called with RCU read-side lock held to ensure existence of channel. |
b99a8d42 JD |
3915 | * |
3916 | * Returns 0 on success, < 0 on error | |
3917 | */ | |
e96d66b4 MD |
3918 | int lttng_consumer_rotate_channel(struct lttng_consumer_channel *channel, |
3919 | uint64_t key, const char *path, uint64_t relayd_id, | |
3920 | uint32_t metadata, uint64_t new_chunk_id, | |
b99a8d42 JD |
3921 | struct lttng_consumer_local_data *ctx) |
3922 | { | |
3923 | int ret; | |
b99a8d42 JD |
3924 | struct lttng_consumer_stream *stream; |
3925 | struct lttng_ht_iter iter; | |
3926 | struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht; | |
3927 | ||
3928 | DBG("Consumer sample rotate position for channel %" PRIu64, key); | |
3929 | ||
3930 | rcu_read_lock(); | |
3931 | ||
b99a8d42 JD |
3932 | pthread_mutex_lock(&channel->lock); |
3933 | channel->current_chunk_id = new_chunk_id; | |
3934 | ||
3935 | ret = lttng_strncpy(channel->pathname, path, sizeof(channel->pathname)); | |
3936 | if (ret) { | |
3937 | ERR("Failed to copy new path to channel during channel rotation"); | |
3938 | ret = -1; | |
3939 | goto end_unlock_channel; | |
3940 | } | |
3941 | ||
3942 | if (relayd_id == -1ULL) { | |
3943 | /* | |
3944 | * The domain path (/ust or /kernel) has been created before, we | |
3945 | * now need to create the last part of the path: the application/user | |
3946 | * specific section (uid/1000/64-bit). | |
3947 | */ | |
3948 | ret = utils_mkdir_recursive(channel->pathname, S_IRWXU | S_IRWXG, | |
3949 | channel->uid, channel->gid); | |
3950 | if (ret < 0) { | |
3951 | ERR("Failed to create trace directory at %s during rotation", | |
3952 | channel->pathname); | |
3953 | ret = -1; | |
3954 | goto end_unlock_channel; | |
3955 | } | |
3956 | } | |
3957 | ||
3958 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
3959 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
3960 | ht->match_fct, &channel->key, &iter.iter, | |
3961 | stream, node_channel_id.node) { | |
3962 | unsigned long consumed_pos; | |
3963 | ||
3964 | health_code_update(); | |
3965 | ||
3966 | /* | |
3967 | * Lock stream because we are about to change its state. | |
3968 | */ | |
3969 | pthread_mutex_lock(&stream->lock); | |
3970 | ||
3971 | ret = lttng_strncpy(stream->channel_read_only_attributes.path, | |
3972 | channel->pathname, | |
3973 | sizeof(stream->channel_read_only_attributes.path)); | |
3974 | if (ret) { | |
3975 | ERR("Failed to sample channel path name during channel rotation"); | |
3976 | goto end_unlock_stream; | |
3977 | } | |
3978 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
3979 | if (ret < 0) { | |
3980 | ERR("Failed to sample snapshot position during channel rotation"); | |
3981 | goto end_unlock_stream; | |
3982 | } | |
3983 | ||
3984 | ret = lttng_consumer_get_produced_snapshot(stream, | |
3985 | &stream->rotate_position); | |
3986 | if (ret < 0) { | |
3987 | ERR("Failed to sample produced position during channel rotation"); | |
3988 | goto end_unlock_stream; | |
3989 | } | |
3990 | ||
3991 | lttng_consumer_get_consumed_snapshot(stream, | |
3992 | &consumed_pos); | |
3993 | if (consumed_pos == stream->rotate_position) { | |
3994 | stream->rotate_ready = true; | |
3995 | } | |
b99a8d42 JD |
3996 | |
3997 | ret = consumer_flush_buffer(stream, 1); | |
3998 | if (ret < 0) { | |
3999 | ERR("Failed to flush stream %" PRIu64 " during channel rotation", | |
4000 | stream->key); | |
4001 | goto end_unlock_stream; | |
4002 | } | |
4003 | ||
4004 | pthread_mutex_unlock(&stream->lock); | |
4005 | } | |
4006 | pthread_mutex_unlock(&channel->lock); | |
4007 | ||
4008 | ret = 0; | |
4009 | goto end; | |
4010 | ||
4011 | end_unlock_stream: | |
4012 | pthread_mutex_unlock(&stream->lock); | |
4013 | end_unlock_channel: | |
4014 | pthread_mutex_unlock(&channel->lock); | |
4015 | end: | |
4016 | rcu_read_unlock(); | |
4017 | return ret; | |
4018 | } | |
4019 | ||
02d02e31 JD |
4020 | /* |
4021 | * Check if a stream is ready to be rotated after extracting it. | |
4022 | * | |
4023 | * Return 1 if it is ready for rotation, 0 if it is not, a negative value on | |
4024 | * error. Stream lock must be held. | |
4025 | */ | |
4026 | int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream *stream) | |
4027 | { | |
4028 | int ret; | |
4029 | unsigned long consumed_pos; | |
4030 | ||
4031 | if (!stream->rotate_position && !stream->rotate_ready) { | |
4032 | ret = 0; | |
4033 | goto end; | |
4034 | } | |
4035 | ||
4036 | if (stream->rotate_ready) { | |
4037 | ret = 1; | |
4038 | goto end; | |
4039 | } | |
4040 | ||
4041 | /* | |
4042 | * If we don't have the rotate_ready flag, check the consumed position | |
4043 | * to determine if we need to rotate. | |
4044 | */ | |
4045 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
4046 | if (ret < 0) { | |
4047 | ERR("Taking snapshot positions"); | |
4048 | goto end; | |
4049 | } | |
4050 | ||
4051 | ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos); | |
4052 | if (ret < 0) { | |
4053 | ERR("Consumed snapshot position"); | |
4054 | goto end; | |
4055 | } | |
4056 | ||
4057 | /* Rotate position not reached yet (with check for overflow). */ | |
4058 | if ((long) (consumed_pos - stream->rotate_position) < 0) { | |
4059 | ret = 0; | |
4060 | goto end; | |
4061 | } | |
4062 | ret = 1; | |
4063 | ||
4064 | end: | |
4065 | return ret; | |
4066 | } | |
4067 | ||
d73bf3d7 JD |
4068 | /* |
4069 | * Reset the state for a stream after a rotation occurred. | |
4070 | */ | |
4071 | void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream *stream) | |
4072 | { | |
4073 | stream->rotate_position = 0; | |
4074 | stream->rotate_ready = false; | |
4075 | } | |
4076 | ||
4077 | /* | |
4078 | * Perform the rotation a local stream file. | |
4079 | */ | |
4080 | int rotate_local_stream(struct lttng_consumer_local_data *ctx, | |
4081 | struct lttng_consumer_stream *stream) | |
4082 | { | |
4083 | int ret; | |
4084 | ||
4085 | DBG("Rotate local stream: stream key %" PRIu64 ", channel key %" PRIu64 " at path %s", | |
4086 | stream->key, | |
4087 | stream->chan->key, | |
4088 | stream->channel_read_only_attributes.path); | |
4089 | ||
4090 | ret = close(stream->out_fd); | |
4091 | if (ret < 0) { | |
4092 | PERROR("Closing trace file (fd %d), stream %" PRIu64, | |
4093 | stream->out_fd, stream->key); | |
4094 | assert(0); | |
4095 | goto error; | |
4096 | } | |
4097 | ||
4098 | ret = utils_create_stream_file( | |
4099 | stream->channel_read_only_attributes.path, | |
4100 | stream->name, | |
4101 | stream->channel_read_only_attributes.tracefile_size, | |
4102 | stream->tracefile_count_current, | |
4103 | stream->uid, stream->gid, NULL); | |
4104 | if (ret < 0) { | |
4105 | ERR("Rotate create stream file"); | |
4106 | goto error; | |
4107 | } | |
4108 | stream->out_fd = ret; | |
4109 | stream->tracefile_size_current = 0; | |
4110 | ||
4111 | if (!stream->metadata_flag) { | |
4112 | struct lttng_index_file *index_file; | |
4113 | ||
4114 | lttng_index_file_put(stream->index_file); | |
4115 | ||
4116 | index_file = lttng_index_file_create( | |
4117 | stream->channel_read_only_attributes.path, | |
4118 | stream->name, stream->uid, stream->gid, | |
4119 | stream->channel_read_only_attributes.tracefile_size, | |
4120 | stream->tracefile_count_current, | |
4121 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
4122 | if (!index_file) { | |
4123 | ERR("Create index file during rotation"); | |
4124 | goto error; | |
4125 | } | |
4126 | stream->index_file = index_file; | |
4127 | stream->out_fd_offset = 0; | |
4128 | } | |
4129 | ||
4130 | ret = 0; | |
4131 | goto end; | |
4132 | ||
4133 | error: | |
4134 | ret = -1; | |
4135 | end: | |
4136 | return ret; | |
4137 | ||
4138 | } | |
4139 | ||
4140 | /* | |
4141 | * Perform the rotation a stream file on the relay. | |
4142 | */ | |
4143 | int rotate_relay_stream(struct lttng_consumer_local_data *ctx, | |
4144 | struct lttng_consumer_stream *stream) | |
4145 | { | |
4146 | int ret; | |
4147 | struct consumer_relayd_sock_pair *relayd; | |
4148 | ||
4149 | DBG("Rotate relay stream"); | |
4150 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
4151 | if (!relayd) { | |
4152 | ERR("Failed to find relayd"); | |
4153 | ret = -1; | |
4154 | goto end; | |
4155 | } | |
4156 | ||
4157 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
4158 | ret = relayd_rotate_stream(&relayd->control_sock, | |
4159 | stream->relayd_stream_id, | |
4160 | stream->channel_read_only_attributes.path, | |
4161 | stream->chan->current_chunk_id, | |
4162 | stream->last_sequence_number); | |
4163 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
768f6a48 JR |
4164 | if (ret < 0) { |
4165 | ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); | |
4166 | lttng_consumer_cleanup_relayd(relayd); | |
4167 | } | |
d73bf3d7 JD |
4168 | if (ret) { |
4169 | ERR("Rotate relay stream"); | |
4170 | } | |
4171 | ||
4172 | end: | |
4173 | return ret; | |
4174 | } | |
4175 | ||
4176 | /* | |
4177 | * Performs the stream rotation for the rotate session feature if needed. | |
4178 | * It must be called with the stream lock held. | |
4179 | * | |
4180 | * Return 0 on success, a negative number of error. | |
4181 | */ | |
4182 | int lttng_consumer_rotate_stream(struct lttng_consumer_local_data *ctx, | |
02d02e31 | 4183 | struct lttng_consumer_stream *stream, bool *rotated) |
d73bf3d7 JD |
4184 | { |
4185 | int ret; | |
4186 | ||
4187 | DBG("Consumer rotate stream %" PRIu64, stream->key); | |
4188 | ||
4189 | if (stream->net_seq_idx != (uint64_t) -1ULL) { | |
4190 | ret = rotate_relay_stream(ctx, stream); | |
4191 | } else { | |
4192 | ret = rotate_local_stream(ctx, stream); | |
4193 | } | |
82528808 | 4194 | stream->trace_archive_id++; |
d73bf3d7 | 4195 | if (ret < 0) { |
82528808 | 4196 | ERR("Failed to rotate stream, ret = %i", ret); |
d73bf3d7 JD |
4197 | goto error; |
4198 | } | |
4199 | ||
4200 | if (stream->metadata_flag) { | |
4201 | switch (consumer_data.type) { | |
4202 | case LTTNG_CONSUMER_KERNEL: | |
4203 | /* | |
4204 | * Reset the position of what has been read from the metadata | |
4205 | * cache to 0 so we can dump it again. | |
4206 | */ | |
4207 | ret = kernctl_metadata_cache_dump(stream->wait_fd); | |
4208 | if (ret < 0) { | |
4209 | ERR("Failed to dump the kernel metadata cache after rotation"); | |
4210 | goto error; | |
4211 | } | |
4212 | break; | |
4213 | case LTTNG_CONSUMER32_UST: | |
4214 | case LTTNG_CONSUMER64_UST: | |
4215 | /* | |
4216 | * Reset the position pushed from the metadata cache so it | |
4217 | * will write from the beginning on the next push. | |
4218 | */ | |
4219 | stream->ust_metadata_pushed = 0; | |
4220 | break; | |
4221 | default: | |
4222 | ERR("Unknown consumer_data type"); | |
4223 | abort(); | |
4224 | } | |
4225 | } | |
4226 | lttng_consumer_reset_stream_rotate_state(stream); | |
4227 | ||
02d02e31 JD |
4228 | if (rotated) { |
4229 | *rotated = true; | |
4230 | } | |
4231 | ||
d73bf3d7 JD |
4232 | ret = 0; |
4233 | ||
4234 | error: | |
4235 | return ret; | |
4236 | } | |
4237 | ||
b99a8d42 JD |
4238 | /* |
4239 | * Rotate all the ready streams now. | |
4240 | * | |
4241 | * This is especially important for low throughput streams that have already | |
4242 | * been consumed, we cannot wait for their next packet to perform the | |
4243 | * rotation. | |
e96d66b4 MD |
4244 | * Need to be called with RCU read-side lock held to ensure existence of |
4245 | * channel. | |
b99a8d42 JD |
4246 | * |
4247 | * Returns 0 on success, < 0 on error | |
4248 | */ | |
e96d66b4 MD |
4249 | int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel *channel, |
4250 | uint64_t key, struct lttng_consumer_local_data *ctx) | |
b99a8d42 JD |
4251 | { |
4252 | int ret; | |
b99a8d42 JD |
4253 | struct lttng_consumer_stream *stream; |
4254 | struct lttng_ht_iter iter; | |
4255 | struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht; | |
4256 | ||
4257 | rcu_read_lock(); | |
4258 | ||
4259 | DBG("Consumer rotate ready streams in channel %" PRIu64, key); | |
4260 | ||
b99a8d42 JD |
4261 | cds_lfht_for_each_entry_duplicate(ht->ht, |
4262 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
4263 | ht->match_fct, &channel->key, &iter.iter, | |
4264 | stream, node_channel_id.node) { | |
4265 | health_code_update(); | |
4266 | ||
4267 | pthread_mutex_lock(&stream->lock); | |
4268 | ||
4269 | if (!stream->rotate_ready) { | |
4270 | pthread_mutex_unlock(&stream->lock); | |
4271 | continue; | |
4272 | } | |
4273 | DBG("Consumer rotate ready stream %" PRIu64, stream->key); | |
4274 | ||
4275 | ret = lttng_consumer_rotate_stream(ctx, stream, NULL); | |
4276 | pthread_mutex_unlock(&stream->lock); | |
4277 | if (ret) { | |
4278 | goto end; | |
4279 | } | |
4280 | ||
4281 | ret = consumer_post_rotation(stream, ctx); | |
4282 | if (ret) { | |
4283 | goto end; | |
4284 | } | |
4285 | } | |
4286 | ||
4287 | ret = 0; | |
4288 | ||
4289 | end: | |
4290 | rcu_read_unlock(); | |
4291 | return ret; | |
4292 | } | |
4293 | ||
00fb02ac JD |
4294 | static |
4295 | int rotate_rename_local(const char *old_path, const char *new_path, | |
4296 | uid_t uid, gid_t gid) | |
4297 | { | |
4298 | int ret; | |
4299 | ||
4300 | assert(old_path); | |
4301 | assert(new_path); | |
4302 | ||
4303 | ret = utils_mkdir_recursive(new_path, S_IRWXU | S_IRWXG, uid, gid); | |
4304 | if (ret < 0) { | |
4305 | ERR("Create directory on rotate"); | |
4306 | goto end; | |
4307 | } | |
4308 | ||
4309 | ret = rename(old_path, new_path); | |
4310 | if (ret < 0 && errno != ENOENT) { | |
4311 | PERROR("Rename completed rotation chunk"); | |
4312 | goto end; | |
4313 | } | |
4314 | ||
4315 | ret = 0; | |
4316 | end: | |
4317 | return ret; | |
4318 | } | |
4319 | ||
4320 | static | |
4321 | int rotate_rename_relay(const char *old_path, const char *new_path, | |
4322 | uint64_t relayd_id) | |
4323 | { | |
4324 | int ret; | |
4325 | struct consumer_relayd_sock_pair *relayd; | |
4326 | ||
4327 | relayd = consumer_find_relayd(relayd_id); | |
4328 | if (!relayd) { | |
4329 | ERR("Failed to find relayd while running rotate_rename_relay command"); | |
4330 | ret = -1; | |
4331 | goto end; | |
4332 | } | |
4333 | ||
4334 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
4335 | ret = relayd_rotate_rename(&relayd->control_sock, old_path, new_path); | |
768f6a48 JR |
4336 | if (ret < 0) { |
4337 | ERR("Relayd rotate rename failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); | |
4338 | lttng_consumer_cleanup_relayd(relayd); | |
4339 | } | |
00fb02ac JD |
4340 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
4341 | end: | |
4342 | return ret; | |
4343 | } | |
4344 | ||
4345 | int lttng_consumer_rotate_rename(const char *old_path, const char *new_path, | |
4346 | uid_t uid, gid_t gid, uint64_t relayd_id) | |
4347 | { | |
4348 | if (relayd_id != -1ULL) { | |
4349 | return rotate_rename_relay(old_path, new_path, relayd_id); | |
4350 | } else { | |
4351 | return rotate_rename_local(old_path, new_path, uid, gid); | |
4352 | } | |
4353 | } | |
4354 | ||
82528808 JG |
4355 | /* Stream lock must be acquired by the caller. */ |
4356 | static | |
4357 | bool check_stream_rotation_pending(const struct lttng_consumer_stream *stream, | |
4358 | uint64_t session_id, uint64_t chunk_id) | |
4359 | { | |
4360 | bool pending = false; | |
4361 | ||
4362 | if (stream->session_id != session_id) { | |
4363 | /* Skip. */ | |
4364 | goto end; | |
4365 | } | |
4366 | ||
4367 | /* | |
4368 | * If the stream's archive_id belongs to the chunk being rotated (or an | |
4369 | * even older one), it means that the consumer has not consumed all the | |
4370 | * buffers that belong to the chunk being rotated. Therefore, the | |
4371 | * rotation is considered as ongoing/pending. | |
4372 | */ | |
4373 | pending = stream->trace_archive_id <= chunk_id; | |
4374 | end: | |
4375 | return pending; | |
4376 | } | |
4377 | ||
4378 | /* RCU read lock must be acquired by the caller. */ | |
4379 | int lttng_consumer_check_rotation_pending_local(uint64_t session_id, | |
4380 | uint64_t chunk_id) | |
4381 | { | |
4382 | struct lttng_ht_iter iter; | |
4383 | struct lttng_consumer_stream *stream; | |
4384 | bool rotation_pending = false; | |
4385 | ||
4386 | /* Start with the metadata streams... */ | |
4387 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
4388 | pthread_mutex_lock(&stream->lock); | |
4389 | rotation_pending = check_stream_rotation_pending(stream, | |
4390 | session_id, chunk_id); | |
4391 | pthread_mutex_unlock(&stream->lock); | |
4392 | if (rotation_pending) { | |
4393 | goto end; | |
4394 | } | |
4395 | } | |
4396 | ||
4397 | /* ... followed by the data streams. */ | |
4398 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
4399 | pthread_mutex_lock(&stream->lock); | |
4400 | rotation_pending = check_stream_rotation_pending(stream, | |
4401 | session_id, chunk_id); | |
4402 | pthread_mutex_unlock(&stream->lock); | |
4403 | if (rotation_pending) { | |
4404 | goto end; | |
4405 | } | |
4406 | } | |
4407 | ||
4408 | end: | |
4409 | return !!rotation_pending; | |
4410 | } | |
4411 | ||
4412 | int lttng_consumer_check_rotation_pending_relay(uint64_t session_id, | |
d88744a4 JD |
4413 | uint64_t relayd_id, uint64_t chunk_id) |
4414 | { | |
4415 | int ret; | |
4416 | struct consumer_relayd_sock_pair *relayd; | |
4417 | ||
4418 | relayd = consumer_find_relayd(relayd_id); | |
4419 | if (!relayd) { | |
82528808 | 4420 | ERR("Failed to find relayd id %" PRIu64, relayd_id); |
d88744a4 JD |
4421 | ret = -1; |
4422 | goto end; | |
4423 | } | |
4424 | ||
4425 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
4426 | ret = relayd_rotate_pending(&relayd->control_sock, chunk_id); | |
768f6a48 JR |
4427 | if (ret < 0) { |
4428 | ERR("Relayd rotate pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); | |
4429 | lttng_consumer_cleanup_relayd(relayd); | |
4430 | } | |
d88744a4 JD |
4431 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
4432 | ||
4433 | end: | |
4434 | return ret; | |
4435 | } | |
4436 | ||
a1ae2ea5 JD |
4437 | static |
4438 | int mkdir_local(const char *path, uid_t uid, gid_t gid) | |
4439 | { | |
4440 | int ret; | |
4441 | ||
4442 | ret = utils_mkdir_recursive(path, S_IRWXU | S_IRWXG, uid, gid); | |
4443 | if (ret < 0) { | |
4444 | /* utils_mkdir_recursive logs an error. */ | |
4445 | goto end; | |
4446 | } | |
4447 | ||
4448 | ret = 0; | |
4449 | end: | |
4450 | return ret; | |
4451 | } | |
4452 | ||
4453 | static | |
4454 | int mkdir_relay(const char *path, uint64_t relayd_id) | |
4455 | { | |
4456 | int ret; | |
4457 | struct consumer_relayd_sock_pair *relayd; | |
4458 | ||
4459 | relayd = consumer_find_relayd(relayd_id); | |
4460 | if (!relayd) { | |
4461 | ERR("Failed to find relayd"); | |
4462 | ret = -1; | |
4463 | goto end; | |
4464 | } | |
4465 | ||
4466 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
4467 | ret = relayd_mkdir(&relayd->control_sock, path); | |
768f6a48 JR |
4468 | if (ret < 0) { |
4469 | ERR("Relayd mkdir failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); | |
4470 | lttng_consumer_cleanup_relayd(relayd); | |
4471 | } | |
a1ae2ea5 JD |
4472 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
4473 | ||
4474 | end: | |
4475 | return ret; | |
4476 | ||
4477 | } | |
4478 | ||
4479 | int lttng_consumer_mkdir(const char *path, uid_t uid, gid_t gid, | |
4480 | uint64_t relayd_id) | |
4481 | { | |
4482 | if (relayd_id != -1ULL) { | |
4483 | return mkdir_relay(path, relayd_id); | |
4484 | } else { | |
4485 | return mkdir_local(path, uid, gid); | |
4486 | } | |
4487 | } | |
fc181d72 JG |
4488 | |
4489 | enum lttcomm_return_code lttng_consumer_init_command( | |
4490 | struct lttng_consumer_local_data *ctx, | |
4491 | const lttng_uuid sessiond_uuid) | |
4492 | { | |
4493 | enum lttcomm_return_code ret; | |
1fb0b36e | 4494 | char uuid_str[UUID_STR_LEN]; |
fc181d72 JG |
4495 | |
4496 | if (ctx->sessiond_uuid.is_set) { | |
4497 | ret = LTTCOMM_CONSUMERD_ALREADY_SET; | |
4498 | goto end; | |
4499 | } | |
4500 | ||
4501 | ctx->sessiond_uuid.is_set = true; | |
4502 | memcpy(ctx->sessiond_uuid.value, sessiond_uuid, sizeof(lttng_uuid)); | |
4503 | ret = LTTCOMM_CONSUMERD_SUCCESS; | |
1fb0b36e JG |
4504 | lttng_uuid_to_str(sessiond_uuid, uuid_str); |
4505 | DBG("Received session daemon UUID: %s", uuid_str); | |
fc181d72 JG |
4506 | end: |
4507 | return ret; | |
4508 | } |