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