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