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