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