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