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