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