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