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