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