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 DG |
35 | #include <common/utils.h> |
36 | #include <common/compat/poll.h> | |
f263b7fd | 37 | #include <common/compat/endian.h> |
309167d2 | 38 | #include <common/index/index.h> |
10a8a223 | 39 | #include <common/kernel-ctl/kernel-ctl.h> |
00e2e675 | 40 | #include <common/sessiond-comm/relayd.h> |
10a8a223 DG |
41 | #include <common/sessiond-comm/sessiond-comm.h> |
42 | #include <common/kernel-consumer/kernel-consumer.h> | |
00e2e675 | 43 | #include <common/relayd/relayd.h> |
10a8a223 | 44 | #include <common/ust-consumer/ust-consumer.h> |
c8fea79c JR |
45 | #include <common/consumer/consumer-timer.h> |
46 | #include <common/consumer/consumer.h> | |
47 | #include <common/consumer/consumer-stream.h> | |
48 | #include <common/consumer/consumer-testpoint.h> | |
49 | #include <common/align.h> | |
5feafd41 | 50 | #include <common/consumer/consumer-metadata-cache.h> |
3bd1e081 MD |
51 | |
52 | struct lttng_consumer_global_data consumer_data = { | |
3bd1e081 MD |
53 | .stream_count = 0, |
54 | .need_update = 1, | |
55 | .type = LTTNG_CONSUMER_UNKNOWN, | |
56 | }; | |
57 | ||
d8ef542d MD |
58 | enum consumer_channel_action { |
59 | CONSUMER_CHANNEL_ADD, | |
a0cbdd2e | 60 | CONSUMER_CHANNEL_DEL, |
d8ef542d MD |
61 | CONSUMER_CHANNEL_QUIT, |
62 | }; | |
63 | ||
64 | struct consumer_channel_msg { | |
65 | enum consumer_channel_action action; | |
a0cbdd2e MD |
66 | struct lttng_consumer_channel *chan; /* add */ |
67 | uint64_t key; /* del */ | |
d8ef542d MD |
68 | }; |
69 | ||
19246bd6 | 70 | /* Flag used to temporarily pause data consumption from testpoints. */ |
cf0bcb51 JG |
71 | int data_consumption_paused; |
72 | ||
3bd1e081 MD |
73 | /* |
74 | * Flag to inform the polling thread to quit when all fd hung up. Updated by | |
75 | * the consumer_thread_receive_fds when it notices that all fds has hung up. | |
76 | * Also updated by the signal handler (consumer_should_exit()). Read by the | |
77 | * polling threads. | |
78 | */ | |
f7389fbb | 79 | int consumer_quit; |
3bd1e081 | 80 | |
43c34bc3 | 81 | /* |
43c34bc3 DG |
82 | * Global hash table containing respectively metadata and data streams. The |
83 | * stream element in this ht should only be updated by the metadata poll thread | |
84 | * for the metadata and the data poll thread for the data. | |
85 | */ | |
40dc48e0 DG |
86 | static struct lttng_ht *metadata_ht; |
87 | static struct lttng_ht *data_ht; | |
43c34bc3 | 88 | |
acdb9057 DG |
89 | /* |
90 | * Notify a thread lttng pipe to poll back again. This usually means that some | |
91 | * global state has changed so we just send back the thread in a poll wait | |
92 | * call. | |
93 | */ | |
94 | static void notify_thread_lttng_pipe(struct lttng_pipe *pipe) | |
95 | { | |
96 | struct lttng_consumer_stream *null_stream = NULL; | |
97 | ||
98 | assert(pipe); | |
99 | ||
100 | (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream)); | |
101 | } | |
102 | ||
5c635c72 MD |
103 | static void notify_health_quit_pipe(int *pipe) |
104 | { | |
6cd525e8 | 105 | ssize_t ret; |
5c635c72 | 106 | |
6cd525e8 MD |
107 | ret = lttng_write(pipe[1], "4", 1); |
108 | if (ret < 1) { | |
5c635c72 MD |
109 | PERROR("write consumer health quit"); |
110 | } | |
111 | } | |
112 | ||
d8ef542d MD |
113 | static void notify_channel_pipe(struct lttng_consumer_local_data *ctx, |
114 | struct lttng_consumer_channel *chan, | |
a0cbdd2e | 115 | uint64_t key, |
d8ef542d MD |
116 | enum consumer_channel_action action) |
117 | { | |
118 | struct consumer_channel_msg msg; | |
6cd525e8 | 119 | ssize_t ret; |
d8ef542d | 120 | |
e56251fc DG |
121 | memset(&msg, 0, sizeof(msg)); |
122 | ||
d8ef542d MD |
123 | msg.action = action; |
124 | msg.chan = chan; | |
f21dae48 | 125 | msg.key = key; |
6cd525e8 MD |
126 | ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg)); |
127 | if (ret < sizeof(msg)) { | |
128 | PERROR("notify_channel_pipe write error"); | |
129 | } | |
d8ef542d MD |
130 | } |
131 | ||
a0cbdd2e MD |
132 | void notify_thread_del_channel(struct lttng_consumer_local_data *ctx, |
133 | uint64_t key) | |
134 | { | |
135 | notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL); | |
136 | } | |
137 | ||
d8ef542d MD |
138 | static int read_channel_pipe(struct lttng_consumer_local_data *ctx, |
139 | struct lttng_consumer_channel **chan, | |
a0cbdd2e | 140 | uint64_t *key, |
d8ef542d MD |
141 | enum consumer_channel_action *action) |
142 | { | |
143 | struct consumer_channel_msg msg; | |
6cd525e8 | 144 | ssize_t ret; |
d8ef542d | 145 | |
6cd525e8 MD |
146 | ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg)); |
147 | if (ret < sizeof(msg)) { | |
148 | ret = -1; | |
149 | goto error; | |
d8ef542d | 150 | } |
6cd525e8 MD |
151 | *action = msg.action; |
152 | *chan = msg.chan; | |
153 | *key = msg.key; | |
154 | error: | |
155 | return (int) ret; | |
d8ef542d MD |
156 | } |
157 | ||
212d67a2 DG |
158 | /* |
159 | * Cleanup the stream list of a channel. Those streams are not yet globally | |
160 | * visible | |
161 | */ | |
162 | static void clean_channel_stream_list(struct lttng_consumer_channel *channel) | |
163 | { | |
164 | struct lttng_consumer_stream *stream, *stmp; | |
165 | ||
166 | assert(channel); | |
167 | ||
168 | /* Delete streams that might have been left in the stream list. */ | |
169 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
170 | send_node) { | |
171 | cds_list_del(&stream->send_node); | |
172 | /* | |
173 | * Once a stream is added to this list, the buffers were created so we | |
174 | * have a guarantee that this call will succeed. Setting the monitor | |
175 | * mode to 0 so we don't lock nor try to delete the stream from the | |
176 | * global hash table. | |
177 | */ | |
178 | stream->monitor = 0; | |
179 | consumer_stream_destroy(stream, NULL); | |
180 | } | |
181 | } | |
182 | ||
3bd1e081 MD |
183 | /* |
184 | * Find a stream. The consumer_data.lock must be locked during this | |
185 | * call. | |
186 | */ | |
d88aee68 | 187 | static struct lttng_consumer_stream *find_stream(uint64_t key, |
8389e4f8 | 188 | struct lttng_ht *ht) |
3bd1e081 | 189 | { |
e4421fec | 190 | struct lttng_ht_iter iter; |
d88aee68 | 191 | struct lttng_ht_node_u64 *node; |
e4421fec | 192 | struct lttng_consumer_stream *stream = NULL; |
3bd1e081 | 193 | |
8389e4f8 DG |
194 | assert(ht); |
195 | ||
d88aee68 DG |
196 | /* -1ULL keys are lookup failures */ |
197 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 198 | return NULL; |
7a57cf92 | 199 | } |
e4421fec | 200 | |
6065ceec DG |
201 | rcu_read_lock(); |
202 | ||
d88aee68 DG |
203 | lttng_ht_lookup(ht, &key, &iter); |
204 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
205 | if (node != NULL) { |
206 | stream = caa_container_of(node, struct lttng_consumer_stream, node); | |
3bd1e081 | 207 | } |
e4421fec | 208 | |
6065ceec DG |
209 | rcu_read_unlock(); |
210 | ||
e4421fec | 211 | return stream; |
3bd1e081 MD |
212 | } |
213 | ||
da009f2c | 214 | static void steal_stream_key(uint64_t key, struct lttng_ht *ht) |
7ad0a0cb MD |
215 | { |
216 | struct lttng_consumer_stream *stream; | |
217 | ||
04253271 | 218 | rcu_read_lock(); |
ffe60014 | 219 | stream = find_stream(key, ht); |
04253271 | 220 | if (stream) { |
da009f2c | 221 | stream->key = (uint64_t) -1ULL; |
04253271 MD |
222 | /* |
223 | * We don't want the lookup to match, but we still need | |
224 | * to iterate on this stream when iterating over the hash table. Just | |
225 | * change the node key. | |
226 | */ | |
da009f2c | 227 | stream->node.key = (uint64_t) -1ULL; |
04253271 MD |
228 | } |
229 | rcu_read_unlock(); | |
7ad0a0cb MD |
230 | } |
231 | ||
d56db448 DG |
232 | /* |
233 | * Return a channel object for the given key. | |
234 | * | |
235 | * RCU read side lock MUST be acquired before calling this function and | |
236 | * protects the channel ptr. | |
237 | */ | |
d88aee68 | 238 | struct lttng_consumer_channel *consumer_find_channel(uint64_t key) |
3bd1e081 | 239 | { |
e4421fec | 240 | struct lttng_ht_iter iter; |
d88aee68 | 241 | struct lttng_ht_node_u64 *node; |
e4421fec | 242 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 | 243 | |
d88aee68 DG |
244 | /* -1ULL keys are lookup failures */ |
245 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 246 | return NULL; |
7a57cf92 | 247 | } |
e4421fec | 248 | |
d88aee68 DG |
249 | lttng_ht_lookup(consumer_data.channel_ht, &key, &iter); |
250 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
251 | if (node != NULL) { |
252 | channel = caa_container_of(node, struct lttng_consumer_channel, node); | |
3bd1e081 | 253 | } |
e4421fec DG |
254 | |
255 | return channel; | |
3bd1e081 MD |
256 | } |
257 | ||
b5a6470f DG |
258 | /* |
259 | * There is a possibility that the consumer does not have enough time between | |
260 | * the close of the channel on the session daemon and the cleanup in here thus | |
261 | * once we have a channel add with an existing key, we know for sure that this | |
262 | * channel will eventually get cleaned up by all streams being closed. | |
263 | * | |
264 | * This function just nullifies the already existing channel key. | |
265 | */ | |
266 | static void steal_channel_key(uint64_t key) | |
267 | { | |
268 | struct lttng_consumer_channel *channel; | |
269 | ||
270 | rcu_read_lock(); | |
271 | channel = consumer_find_channel(key); | |
272 | if (channel) { | |
273 | channel->key = (uint64_t) -1ULL; | |
274 | /* | |
275 | * We don't want the lookup to match, but we still need to iterate on | |
276 | * this channel when iterating over the hash table. Just change the | |
277 | * node key. | |
278 | */ | |
279 | channel->node.key = (uint64_t) -1ULL; | |
280 | } | |
281 | rcu_read_unlock(); | |
282 | } | |
283 | ||
ffe60014 | 284 | static void free_channel_rcu(struct rcu_head *head) |
702b1ea4 | 285 | { |
d88aee68 DG |
286 | struct lttng_ht_node_u64 *node = |
287 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
ffe60014 DG |
288 | struct lttng_consumer_channel *channel = |
289 | caa_container_of(node, struct lttng_consumer_channel, node); | |
702b1ea4 | 290 | |
b83e03c4 MD |
291 | switch (consumer_data.type) { |
292 | case LTTNG_CONSUMER_KERNEL: | |
293 | break; | |
294 | case LTTNG_CONSUMER32_UST: | |
295 | case LTTNG_CONSUMER64_UST: | |
296 | lttng_ustconsumer_free_channel(channel); | |
297 | break; | |
298 | default: | |
299 | ERR("Unknown consumer_data type"); | |
300 | abort(); | |
301 | } | |
ffe60014 | 302 | free(channel); |
702b1ea4 MD |
303 | } |
304 | ||
00e2e675 DG |
305 | /* |
306 | * RCU protected relayd socket pair free. | |
307 | */ | |
ffe60014 | 308 | static void free_relayd_rcu(struct rcu_head *head) |
00e2e675 | 309 | { |
d88aee68 DG |
310 | struct lttng_ht_node_u64 *node = |
311 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
00e2e675 DG |
312 | struct consumer_relayd_sock_pair *relayd = |
313 | caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
314 | ||
8994307f DG |
315 | /* |
316 | * Close all sockets. This is done in the call RCU since we don't want the | |
317 | * socket fds to be reassigned thus potentially creating bad state of the | |
318 | * relayd object. | |
319 | * | |
320 | * We do not have to lock the control socket mutex here since at this stage | |
321 | * there is no one referencing to this relayd object. | |
322 | */ | |
323 | (void) relayd_close(&relayd->control_sock); | |
324 | (void) relayd_close(&relayd->data_sock); | |
325 | ||
00e2e675 DG |
326 | free(relayd); |
327 | } | |
328 | ||
329 | /* | |
330 | * Destroy and free relayd socket pair object. | |
00e2e675 | 331 | */ |
51230d70 | 332 | void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
333 | { |
334 | int ret; | |
335 | struct lttng_ht_iter iter; | |
336 | ||
173af62f DG |
337 | if (relayd == NULL) { |
338 | return; | |
339 | } | |
340 | ||
00e2e675 DG |
341 | DBG("Consumer destroy and close relayd socket pair"); |
342 | ||
343 | iter.iter.node = &relayd->node.node; | |
344 | ret = lttng_ht_del(consumer_data.relayd_ht, &iter); | |
173af62f | 345 | if (ret != 0) { |
8994307f | 346 | /* We assume the relayd is being or is destroyed */ |
173af62f DG |
347 | return; |
348 | } | |
00e2e675 | 349 | |
00e2e675 | 350 | /* RCU free() call */ |
ffe60014 DG |
351 | call_rcu(&relayd->node.head, free_relayd_rcu); |
352 | } | |
353 | ||
354 | /* | |
355 | * Remove a channel from the global list protected by a mutex. This function is | |
356 | * also responsible for freeing its data structures. | |
357 | */ | |
358 | void consumer_del_channel(struct lttng_consumer_channel *channel) | |
359 | { | |
360 | int ret; | |
361 | struct lttng_ht_iter iter; | |
362 | ||
d88aee68 | 363 | DBG("Consumer delete channel key %" PRIu64, channel->key); |
ffe60014 DG |
364 | |
365 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 366 | pthread_mutex_lock(&channel->lock); |
ffe60014 | 367 | |
212d67a2 DG |
368 | /* Destroy streams that might have been left in the stream list. */ |
369 | clean_channel_stream_list(channel); | |
51e762e5 | 370 | |
d3e2ba59 JD |
371 | if (channel->live_timer_enabled == 1) { |
372 | consumer_timer_live_stop(channel); | |
373 | } | |
e9404c27 JG |
374 | if (channel->monitor_timer_enabled == 1) { |
375 | consumer_timer_monitor_stop(channel); | |
376 | } | |
d3e2ba59 | 377 | |
ffe60014 DG |
378 | switch (consumer_data.type) { |
379 | case LTTNG_CONSUMER_KERNEL: | |
380 | break; | |
381 | case LTTNG_CONSUMER32_UST: | |
382 | case LTTNG_CONSUMER64_UST: | |
383 | lttng_ustconsumer_del_channel(channel); | |
384 | break; | |
385 | default: | |
386 | ERR("Unknown consumer_data type"); | |
387 | assert(0); | |
388 | goto end; | |
389 | } | |
390 | ||
391 | rcu_read_lock(); | |
392 | iter.iter.node = &channel->node.node; | |
393 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); | |
394 | assert(!ret); | |
395 | rcu_read_unlock(); | |
396 | ||
397 | call_rcu(&channel->node.head, free_channel_rcu); | |
398 | end: | |
a9838785 | 399 | pthread_mutex_unlock(&channel->lock); |
ffe60014 | 400 | pthread_mutex_unlock(&consumer_data.lock); |
00e2e675 DG |
401 | } |
402 | ||
228b5bf7 DG |
403 | /* |
404 | * Iterate over the relayd hash table and destroy each element. Finally, | |
405 | * destroy the whole hash table. | |
406 | */ | |
407 | static void cleanup_relayd_ht(void) | |
408 | { | |
409 | struct lttng_ht_iter iter; | |
410 | struct consumer_relayd_sock_pair *relayd; | |
411 | ||
412 | rcu_read_lock(); | |
413 | ||
414 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
415 | node.node) { | |
51230d70 | 416 | consumer_destroy_relayd(relayd); |
228b5bf7 DG |
417 | } |
418 | ||
228b5bf7 | 419 | rcu_read_unlock(); |
36b588ed MD |
420 | |
421 | lttng_ht_destroy(consumer_data.relayd_ht); | |
228b5bf7 DG |
422 | } |
423 | ||
8994307f DG |
424 | /* |
425 | * Update the end point status of all streams having the given network sequence | |
426 | * index (relayd index). | |
427 | * | |
428 | * It's atomically set without having the stream mutex locked which is fine | |
429 | * because we handle the write/read race with a pipe wakeup for each thread. | |
430 | */ | |
da009f2c | 431 | static void update_endpoint_status_by_netidx(uint64_t net_seq_idx, |
8994307f DG |
432 | enum consumer_endpoint_status status) |
433 | { | |
434 | struct lttng_ht_iter iter; | |
435 | struct lttng_consumer_stream *stream; | |
436 | ||
da009f2c | 437 | DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx); |
8994307f DG |
438 | |
439 | rcu_read_lock(); | |
440 | ||
441 | /* Let's begin with metadata */ | |
442 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
443 | if (stream->net_seq_idx == net_seq_idx) { | |
444 | uatomic_set(&stream->endpoint_status, status); | |
445 | DBG("Delete flag set to metadata stream %d", stream->wait_fd); | |
446 | } | |
447 | } | |
448 | ||
449 | /* Follow up by the data streams */ | |
450 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
451 | if (stream->net_seq_idx == net_seq_idx) { | |
452 | uatomic_set(&stream->endpoint_status, status); | |
453 | DBG("Delete flag set to data stream %d", stream->wait_fd); | |
454 | } | |
455 | } | |
456 | rcu_read_unlock(); | |
457 | } | |
458 | ||
459 | /* | |
460 | * Cleanup a relayd object by flagging every associated streams for deletion, | |
461 | * destroying the object meaning removing it from the relayd hash table, | |
462 | * closing the sockets and freeing the memory in a RCU call. | |
463 | * | |
464 | * If a local data context is available, notify the threads that the streams' | |
465 | * state have changed. | |
466 | */ | |
467 | static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd, | |
468 | struct lttng_consumer_local_data *ctx) | |
469 | { | |
da009f2c | 470 | uint64_t netidx; |
8994307f DG |
471 | |
472 | assert(relayd); | |
473 | ||
9617607b DG |
474 | DBG("Cleaning up relayd sockets"); |
475 | ||
8994307f DG |
476 | /* Save the net sequence index before destroying the object */ |
477 | netidx = relayd->net_seq_idx; | |
478 | ||
479 | /* | |
480 | * Delete the relayd from the relayd hash table, close the sockets and free | |
481 | * the object in a RCU call. | |
482 | */ | |
51230d70 | 483 | consumer_destroy_relayd(relayd); |
8994307f DG |
484 | |
485 | /* Set inactive endpoint to all streams */ | |
486 | update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE); | |
487 | ||
488 | /* | |
489 | * With a local data context, notify the threads that the streams' state | |
490 | * have changed. The write() action on the pipe acts as an "implicit" | |
491 | * memory barrier ordering the updates of the end point status from the | |
492 | * read of this status which happens AFTER receiving this notify. | |
493 | */ | |
494 | if (ctx) { | |
acdb9057 | 495 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
13886d2d | 496 | notify_thread_lttng_pipe(ctx->consumer_metadata_pipe); |
8994307f DG |
497 | } |
498 | } | |
499 | ||
a6ba4fe1 DG |
500 | /* |
501 | * Flag a relayd socket pair for destruction. Destroy it if the refcount | |
502 | * reaches zero. | |
503 | * | |
504 | * RCU read side lock MUST be aquired before calling this function. | |
505 | */ | |
506 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) | |
507 | { | |
508 | assert(relayd); | |
509 | ||
510 | /* Set destroy flag for this object */ | |
511 | uatomic_set(&relayd->destroy_flag, 1); | |
512 | ||
513 | /* Destroy the relayd if refcount is 0 */ | |
514 | if (uatomic_read(&relayd->refcount) == 0) { | |
51230d70 | 515 | consumer_destroy_relayd(relayd); |
a6ba4fe1 DG |
516 | } |
517 | } | |
518 | ||
3bd1e081 | 519 | /* |
1d1a276c DG |
520 | * Completly destroy stream from every visiable data structure and the given |
521 | * hash table if one. | |
522 | * | |
523 | * One this call returns, the stream object is not longer usable nor visible. | |
3bd1e081 | 524 | */ |
e316aad5 DG |
525 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
526 | struct lttng_ht *ht) | |
3bd1e081 | 527 | { |
1d1a276c | 528 | consumer_stream_destroy(stream, ht); |
3bd1e081 MD |
529 | } |
530 | ||
5ab66908 MD |
531 | /* |
532 | * XXX naming of del vs destroy is all mixed up. | |
533 | */ | |
534 | void consumer_del_stream_for_data(struct lttng_consumer_stream *stream) | |
535 | { | |
536 | consumer_stream_destroy(stream, data_ht); | |
537 | } | |
538 | ||
539 | void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream) | |
540 | { | |
541 | consumer_stream_destroy(stream, metadata_ht); | |
542 | } | |
543 | ||
d88aee68 DG |
544 | struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key, |
545 | uint64_t stream_key, | |
3bd1e081 | 546 | enum lttng_consumer_stream_state state, |
ffe60014 | 547 | const char *channel_name, |
6df2e2c9 | 548 | uid_t uid, |
00e2e675 | 549 | gid_t gid, |
57a269f2 | 550 | uint64_t relayd_id, |
53632229 | 551 | uint64_t session_id, |
ffe60014 DG |
552 | int cpu, |
553 | int *alloc_ret, | |
4891ece8 DG |
554 | enum consumer_channel_type type, |
555 | unsigned int monitor) | |
3bd1e081 | 556 | { |
ffe60014 | 557 | int ret; |
3bd1e081 | 558 | struct lttng_consumer_stream *stream; |
3bd1e081 | 559 | |
effcf122 | 560 | stream = zmalloc(sizeof(*stream)); |
3bd1e081 | 561 | if (stream == NULL) { |
7a57cf92 | 562 | PERROR("malloc struct lttng_consumer_stream"); |
ffe60014 | 563 | ret = -ENOMEM; |
7a57cf92 | 564 | goto end; |
3bd1e081 | 565 | } |
7a57cf92 | 566 | |
d56db448 DG |
567 | rcu_read_lock(); |
568 | ||
3bd1e081 | 569 | stream->key = stream_key; |
3bd1e081 MD |
570 | stream->out_fd = -1; |
571 | stream->out_fd_offset = 0; | |
e5d1a9b3 | 572 | stream->output_written = 0; |
3bd1e081 | 573 | stream->state = state; |
6df2e2c9 MD |
574 | stream->uid = uid; |
575 | stream->gid = gid; | |
ffe60014 | 576 | stream->net_seq_idx = relayd_id; |
53632229 | 577 | stream->session_id = session_id; |
4891ece8 | 578 | stream->monitor = monitor; |
774d490c | 579 | stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE; |
f8f3885c | 580 | stream->index_file = NULL; |
fb83fe64 | 581 | stream->last_sequence_number = -1ULL; |
53632229 | 582 | pthread_mutex_init(&stream->lock, NULL); |
c585821b | 583 | pthread_mutex_init(&stream->metadata_timer_lock, NULL); |
58b1f425 | 584 | |
ffe60014 DG |
585 | /* If channel is the metadata, flag this stream as metadata. */ |
586 | if (type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
587 | stream->metadata_flag = 1; | |
588 | /* Metadata is flat out. */ | |
589 | strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name)); | |
94d49140 JD |
590 | /* Live rendez-vous point. */ |
591 | pthread_cond_init(&stream->metadata_rdv, NULL); | |
592 | pthread_mutex_init(&stream->metadata_rdv_lock, NULL); | |
58b1f425 | 593 | } else { |
ffe60014 DG |
594 | /* Format stream name to <channel_name>_<cpu_number> */ |
595 | ret = snprintf(stream->name, sizeof(stream->name), "%s_%d", | |
596 | channel_name, cpu); | |
597 | if (ret < 0) { | |
598 | PERROR("snprintf stream name"); | |
599 | goto error; | |
600 | } | |
58b1f425 | 601 | } |
c30aaa51 | 602 | |
ffe60014 | 603 | /* Key is always the wait_fd for streams. */ |
d88aee68 | 604 | lttng_ht_node_init_u64(&stream->node, stream->key); |
ffe60014 | 605 | |
d8ef542d MD |
606 | /* Init node per channel id key */ |
607 | lttng_ht_node_init_u64(&stream->node_channel_id, channel_key); | |
608 | ||
53632229 | 609 | /* Init session id node with the stream session id */ |
d88aee68 | 610 | lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id); |
53632229 | 611 | |
07b86b52 JD |
612 | DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64 |
613 | " relayd_id %" PRIu64 ", session_id %" PRIu64, | |
614 | stream->name, stream->key, channel_key, | |
615 | stream->net_seq_idx, stream->session_id); | |
d56db448 DG |
616 | |
617 | rcu_read_unlock(); | |
3bd1e081 | 618 | return stream; |
c80048c6 MD |
619 | |
620 | error: | |
d56db448 | 621 | rcu_read_unlock(); |
c80048c6 | 622 | free(stream); |
7a57cf92 | 623 | end: |
ffe60014 DG |
624 | if (alloc_ret) { |
625 | *alloc_ret = ret; | |
626 | } | |
c80048c6 | 627 | return NULL; |
3bd1e081 MD |
628 | } |
629 | ||
630 | /* | |
631 | * Add a stream to the global list protected by a mutex. | |
632 | */ | |
5ab66908 | 633 | int consumer_add_data_stream(struct lttng_consumer_stream *stream) |
3bd1e081 | 634 | { |
5ab66908 | 635 | struct lttng_ht *ht = data_ht; |
3bd1e081 MD |
636 | int ret = 0; |
637 | ||
e316aad5 | 638 | assert(stream); |
43c34bc3 | 639 | assert(ht); |
c77fc10a | 640 | |
d88aee68 | 641 | DBG3("Adding consumer stream %" PRIu64, stream->key); |
e316aad5 DG |
642 | |
643 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 644 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 645 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 646 | pthread_mutex_lock(&stream->lock); |
b0b335c8 | 647 | rcu_read_lock(); |
e316aad5 | 648 | |
43c34bc3 | 649 | /* Steal stream identifier to avoid having streams with the same key */ |
ffe60014 | 650 | steal_stream_key(stream->key, ht); |
43c34bc3 | 651 | |
d88aee68 | 652 | lttng_ht_add_unique_u64(ht, &stream->node); |
00e2e675 | 653 | |
d8ef542d MD |
654 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
655 | &stream->node_channel_id); | |
656 | ||
ca22feea DG |
657 | /* |
658 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
659 | * the key since the HT does not use it and we allow to add redundant keys | |
660 | * into this table. | |
661 | */ | |
d88aee68 | 662 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 663 | |
e316aad5 | 664 | /* |
ffe60014 DG |
665 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
666 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
667 | * causes the count to become 0 also causes a stream to be added. The |
668 | * channel deletion will thus be triggered by the following removal of this | |
669 | * stream. | |
670 | */ | |
ffe60014 | 671 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
672 | /* Increment refcount before decrementing nb_init_stream_left */ |
673 | cmm_smp_wmb(); | |
ffe60014 | 674 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
675 | } |
676 | ||
677 | /* Update consumer data once the node is inserted. */ | |
3bd1e081 MD |
678 | consumer_data.stream_count++; |
679 | consumer_data.need_update = 1; | |
680 | ||
e316aad5 | 681 | rcu_read_unlock(); |
2e818a6a | 682 | pthread_mutex_unlock(&stream->lock); |
ec6ea7d0 | 683 | pthread_mutex_unlock(&stream->chan->timer_lock); |
a9838785 | 684 | pthread_mutex_unlock(&stream->chan->lock); |
3bd1e081 | 685 | pthread_mutex_unlock(&consumer_data.lock); |
702b1ea4 | 686 | |
3bd1e081 MD |
687 | return ret; |
688 | } | |
689 | ||
5ab66908 MD |
690 | void consumer_del_data_stream(struct lttng_consumer_stream *stream) |
691 | { | |
692 | consumer_del_stream(stream, data_ht); | |
693 | } | |
694 | ||
00e2e675 | 695 | /* |
3f8e211f DG |
696 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
697 | * be acquired before calling this. | |
00e2e675 | 698 | */ |
d09e1200 | 699 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
700 | { |
701 | int ret = 0; | |
d88aee68 | 702 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
703 | struct lttng_ht_iter iter; |
704 | ||
ffe60014 | 705 | assert(relayd); |
00e2e675 | 706 | |
00e2e675 | 707 | lttng_ht_lookup(consumer_data.relayd_ht, |
d88aee68 DG |
708 | &relayd->net_seq_idx, &iter); |
709 | node = lttng_ht_iter_get_node_u64(&iter); | |
00e2e675 | 710 | if (node != NULL) { |
00e2e675 DG |
711 | goto end; |
712 | } | |
d88aee68 | 713 | lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node); |
00e2e675 | 714 | |
00e2e675 DG |
715 | end: |
716 | return ret; | |
717 | } | |
718 | ||
719 | /* | |
720 | * Allocate and return a consumer relayd socket. | |
721 | */ | |
027a694f | 722 | static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
da009f2c | 723 | uint64_t net_seq_idx) |
00e2e675 DG |
724 | { |
725 | struct consumer_relayd_sock_pair *obj = NULL; | |
726 | ||
da009f2c MD |
727 | /* net sequence index of -1 is a failure */ |
728 | if (net_seq_idx == (uint64_t) -1ULL) { | |
00e2e675 DG |
729 | goto error; |
730 | } | |
731 | ||
732 | obj = zmalloc(sizeof(struct consumer_relayd_sock_pair)); | |
733 | if (obj == NULL) { | |
734 | PERROR("zmalloc relayd sock"); | |
735 | goto error; | |
736 | } | |
737 | ||
738 | obj->net_seq_idx = net_seq_idx; | |
739 | obj->refcount = 0; | |
173af62f | 740 | obj->destroy_flag = 0; |
f96e4545 MD |
741 | obj->control_sock.sock.fd = -1; |
742 | obj->data_sock.sock.fd = -1; | |
d88aee68 | 743 | lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx); |
00e2e675 DG |
744 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
745 | ||
746 | error: | |
747 | return obj; | |
748 | } | |
749 | ||
750 | /* | |
751 | * Find a relayd socket pair in the global consumer data. | |
752 | * | |
753 | * Return the object if found else NULL. | |
b0b335c8 MD |
754 | * RCU read-side lock must be held across this call and while using the |
755 | * returned object. | |
00e2e675 | 756 | */ |
d88aee68 | 757 | struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key) |
00e2e675 DG |
758 | { |
759 | struct lttng_ht_iter iter; | |
d88aee68 | 760 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
761 | struct consumer_relayd_sock_pair *relayd = NULL; |
762 | ||
763 | /* Negative keys are lookup failures */ | |
d88aee68 | 764 | if (key == (uint64_t) -1ULL) { |
00e2e675 DG |
765 | goto error; |
766 | } | |
767 | ||
d88aee68 | 768 | lttng_ht_lookup(consumer_data.relayd_ht, &key, |
00e2e675 | 769 | &iter); |
d88aee68 | 770 | node = lttng_ht_iter_get_node_u64(&iter); |
00e2e675 DG |
771 | if (node != NULL) { |
772 | relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
773 | } | |
774 | ||
00e2e675 DG |
775 | error: |
776 | return relayd; | |
777 | } | |
778 | ||
10a50311 JD |
779 | /* |
780 | * Find a relayd and send the stream | |
781 | * | |
782 | * Returns 0 on success, < 0 on error | |
783 | */ | |
784 | int consumer_send_relayd_stream(struct lttng_consumer_stream *stream, | |
785 | char *path) | |
786 | { | |
787 | int ret = 0; | |
788 | struct consumer_relayd_sock_pair *relayd; | |
789 | ||
790 | assert(stream); | |
791 | assert(stream->net_seq_idx != -1ULL); | |
792 | assert(path); | |
793 | ||
794 | /* The stream is not metadata. Get relayd reference if exists. */ | |
795 | rcu_read_lock(); | |
796 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
797 | if (relayd != NULL) { | |
798 | /* Add stream on the relayd */ | |
799 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
800 | ret = relayd_add_stream(&relayd->control_sock, stream->name, | |
801 | path, &stream->relayd_stream_id, | |
802 | stream->chan->tracefile_size, stream->chan->tracefile_count); | |
803 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
804 | if (ret < 0) { | |
805 | goto end; | |
806 | } | |
1c20f0e2 | 807 | |
10a50311 | 808 | uatomic_inc(&relayd->refcount); |
d01178b6 | 809 | stream->sent_to_relayd = 1; |
10a50311 JD |
810 | } else { |
811 | ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.", | |
812 | stream->key, stream->net_seq_idx); | |
813 | ret = -1; | |
814 | goto end; | |
815 | } | |
816 | ||
817 | DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64, | |
818 | stream->name, stream->key, stream->net_seq_idx); | |
819 | ||
820 | end: | |
821 | rcu_read_unlock(); | |
822 | return ret; | |
823 | } | |
824 | ||
a4baae1b JD |
825 | /* |
826 | * Find a relayd and send the streams sent message | |
827 | * | |
828 | * Returns 0 on success, < 0 on error | |
829 | */ | |
830 | int consumer_send_relayd_streams_sent(uint64_t net_seq_idx) | |
831 | { | |
832 | int ret = 0; | |
833 | struct consumer_relayd_sock_pair *relayd; | |
834 | ||
835 | assert(net_seq_idx != -1ULL); | |
836 | ||
837 | /* The stream is not metadata. Get relayd reference if exists. */ | |
838 | rcu_read_lock(); | |
839 | relayd = consumer_find_relayd(net_seq_idx); | |
840 | if (relayd != NULL) { | |
841 | /* Add stream on the relayd */ | |
842 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
843 | ret = relayd_streams_sent(&relayd->control_sock); | |
844 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
845 | if (ret < 0) { | |
846 | goto end; | |
847 | } | |
848 | } else { | |
849 | ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.", | |
850 | net_seq_idx); | |
851 | ret = -1; | |
852 | goto end; | |
853 | } | |
854 | ||
855 | ret = 0; | |
856 | DBG("All streams sent relayd id %" PRIu64, net_seq_idx); | |
857 | ||
858 | end: | |
859 | rcu_read_unlock(); | |
860 | return ret; | |
861 | } | |
862 | ||
10a50311 JD |
863 | /* |
864 | * Find a relayd and close the stream | |
865 | */ | |
866 | void close_relayd_stream(struct lttng_consumer_stream *stream) | |
867 | { | |
868 | struct consumer_relayd_sock_pair *relayd; | |
869 | ||
870 | /* The stream is not metadata. Get relayd reference if exists. */ | |
871 | rcu_read_lock(); | |
872 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
873 | if (relayd) { | |
874 | consumer_stream_relayd_close(stream, relayd); | |
875 | } | |
876 | rcu_read_unlock(); | |
877 | } | |
878 | ||
00e2e675 DG |
879 | /* |
880 | * Handle stream for relayd transmission if the stream applies for network | |
881 | * streaming where the net sequence index is set. | |
882 | * | |
883 | * Return destination file descriptor or negative value on error. | |
884 | */ | |
6197aea7 | 885 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
1d4dfdef DG |
886 | size_t data_size, unsigned long padding, |
887 | struct consumer_relayd_sock_pair *relayd) | |
00e2e675 DG |
888 | { |
889 | int outfd = -1, ret; | |
00e2e675 DG |
890 | struct lttcomm_relayd_data_hdr data_hdr; |
891 | ||
892 | /* Safety net */ | |
893 | assert(stream); | |
6197aea7 | 894 | assert(relayd); |
00e2e675 DG |
895 | |
896 | /* Reset data header */ | |
897 | memset(&data_hdr, 0, sizeof(data_hdr)); | |
898 | ||
00e2e675 DG |
899 | if (stream->metadata_flag) { |
900 | /* Caller MUST acquire the relayd control socket lock */ | |
901 | ret = relayd_send_metadata(&relayd->control_sock, data_size); | |
902 | if (ret < 0) { | |
903 | goto error; | |
904 | } | |
905 | ||
906 | /* Metadata are always sent on the control socket. */ | |
6151a90f | 907 | outfd = relayd->control_sock.sock.fd; |
00e2e675 DG |
908 | } else { |
909 | /* Set header with stream information */ | |
910 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); | |
911 | data_hdr.data_size = htobe32(data_size); | |
1d4dfdef | 912 | data_hdr.padding_size = htobe32(padding); |
39df6d9f DG |
913 | /* |
914 | * Note that net_seq_num below is assigned with the *current* value of | |
915 | * next_net_seq_num and only after that the next_net_seq_num will be | |
916 | * increment. This is why when issuing a command on the relayd using | |
917 | * this next value, 1 should always be substracted in order to compare | |
918 | * the last seen sequence number on the relayd side to the last sent. | |
919 | */ | |
3604f373 | 920 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num); |
00e2e675 DG |
921 | /* Other fields are zeroed previously */ |
922 | ||
923 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, | |
924 | sizeof(data_hdr)); | |
925 | if (ret < 0) { | |
926 | goto error; | |
927 | } | |
928 | ||
3604f373 DG |
929 | ++stream->next_net_seq_num; |
930 | ||
00e2e675 | 931 | /* Set to go on data socket */ |
6151a90f | 932 | outfd = relayd->data_sock.sock.fd; |
00e2e675 DG |
933 | } |
934 | ||
935 | error: | |
936 | return outfd; | |
937 | } | |
938 | ||
3bd1e081 | 939 | /* |
ffe60014 DG |
940 | * Allocate and return a new lttng_consumer_channel object using the given key |
941 | * to initialize the hash table node. | |
942 | * | |
943 | * On error, return NULL. | |
3bd1e081 | 944 | */ |
886224ff | 945 | struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, |
ffe60014 DG |
946 | uint64_t session_id, |
947 | const char *pathname, | |
948 | const char *name, | |
949 | uid_t uid, | |
950 | gid_t gid, | |
57a269f2 | 951 | uint64_t relayd_id, |
1624d5b7 JD |
952 | enum lttng_event_output output, |
953 | uint64_t tracefile_size, | |
2bba9e53 | 954 | uint64_t tracefile_count, |
1950109e | 955 | uint64_t session_id_per_pid, |
ecc48a90 | 956 | unsigned int monitor, |
d7ba1388 | 957 | unsigned int live_timer_interval, |
3d071855 | 958 | const char *root_shm_path, |
d7ba1388 | 959 | const char *shm_path) |
3bd1e081 MD |
960 | { |
961 | struct lttng_consumer_channel *channel; | |
3bd1e081 | 962 | |
276b26d1 | 963 | channel = zmalloc(sizeof(*channel)); |
3bd1e081 | 964 | if (channel == NULL) { |
7a57cf92 | 965 | PERROR("malloc struct lttng_consumer_channel"); |
3bd1e081 MD |
966 | goto end; |
967 | } | |
ffe60014 DG |
968 | |
969 | channel->key = key; | |
3bd1e081 | 970 | channel->refcount = 0; |
ffe60014 | 971 | channel->session_id = session_id; |
1950109e | 972 | channel->session_id_per_pid = session_id_per_pid; |
ffe60014 DG |
973 | channel->uid = uid; |
974 | channel->gid = gid; | |
975 | channel->relayd_id = relayd_id; | |
1624d5b7 JD |
976 | channel->tracefile_size = tracefile_size; |
977 | channel->tracefile_count = tracefile_count; | |
2bba9e53 | 978 | channel->monitor = monitor; |
ecc48a90 | 979 | channel->live_timer_interval = live_timer_interval; |
a9838785 | 980 | pthread_mutex_init(&channel->lock, NULL); |
ec6ea7d0 | 981 | pthread_mutex_init(&channel->timer_lock, NULL); |
ffe60014 | 982 | |
0c759fc9 DG |
983 | switch (output) { |
984 | case LTTNG_EVENT_SPLICE: | |
985 | channel->output = CONSUMER_CHANNEL_SPLICE; | |
986 | break; | |
987 | case LTTNG_EVENT_MMAP: | |
988 | channel->output = CONSUMER_CHANNEL_MMAP; | |
989 | break; | |
990 | default: | |
991 | assert(0); | |
992 | free(channel); | |
993 | channel = NULL; | |
994 | goto end; | |
995 | } | |
996 | ||
07b86b52 JD |
997 | /* |
998 | * In monitor mode, the streams associated with the channel will be put in | |
999 | * a special list ONLY owned by this channel. So, the refcount is set to 1 | |
1000 | * here meaning that the channel itself has streams that are referenced. | |
1001 | * | |
1002 | * On a channel deletion, once the channel is no longer visible, the | |
1003 | * refcount is decremented and checked for a zero value to delete it. With | |
1004 | * streams in no monitor mode, it will now be safe to destroy the channel. | |
1005 | */ | |
1006 | if (!channel->monitor) { | |
1007 | channel->refcount = 1; | |
1008 | } | |
1009 | ||
ffe60014 DG |
1010 | strncpy(channel->pathname, pathname, sizeof(channel->pathname)); |
1011 | channel->pathname[sizeof(channel->pathname) - 1] = '\0'; | |
1012 | ||
1013 | strncpy(channel->name, name, sizeof(channel->name)); | |
1014 | channel->name[sizeof(channel->name) - 1] = '\0'; | |
1015 | ||
3d071855 MD |
1016 | if (root_shm_path) { |
1017 | strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path)); | |
1018 | channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0'; | |
1019 | } | |
d7ba1388 MD |
1020 | if (shm_path) { |
1021 | strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path)); | |
1022 | channel->shm_path[sizeof(channel->shm_path) - 1] = '\0'; | |
1023 | } | |
1024 | ||
d88aee68 | 1025 | lttng_ht_node_init_u64(&channel->node, channel->key); |
d8ef542d MD |
1026 | |
1027 | channel->wait_fd = -1; | |
1028 | ||
ffe60014 DG |
1029 | CDS_INIT_LIST_HEAD(&channel->streams.head); |
1030 | ||
62a7b8ed | 1031 | DBG("Allocated channel (key %" PRIu64 ")", channel->key); |
3bd1e081 | 1032 | |
3bd1e081 MD |
1033 | end: |
1034 | return channel; | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * Add a channel to the global list protected by a mutex. | |
821fffb2 | 1039 | * |
b5a6470f | 1040 | * Always return 0 indicating success. |
3bd1e081 | 1041 | */ |
d8ef542d MD |
1042 | int consumer_add_channel(struct lttng_consumer_channel *channel, |
1043 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 | 1044 | { |
3bd1e081 | 1045 | pthread_mutex_lock(&consumer_data.lock); |
a9838785 | 1046 | pthread_mutex_lock(&channel->lock); |
ec6ea7d0 | 1047 | pthread_mutex_lock(&channel->timer_lock); |
c77fc10a | 1048 | |
b5a6470f DG |
1049 | /* |
1050 | * This gives us a guarantee that the channel we are about to add to the | |
1051 | * channel hash table will be unique. See this function comment on the why | |
1052 | * we need to steel the channel key at this stage. | |
1053 | */ | |
1054 | steal_channel_key(channel->key); | |
c77fc10a | 1055 | |
b5a6470f | 1056 | rcu_read_lock(); |
d88aee68 | 1057 | lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node); |
6065ceec | 1058 | rcu_read_unlock(); |
b5a6470f | 1059 | |
ec6ea7d0 | 1060 | pthread_mutex_unlock(&channel->timer_lock); |
a9838785 | 1061 | pthread_mutex_unlock(&channel->lock); |
3bd1e081 | 1062 | pthread_mutex_unlock(&consumer_data.lock); |
702b1ea4 | 1063 | |
b5a6470f | 1064 | if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) { |
a0cbdd2e | 1065 | notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD); |
d8ef542d | 1066 | } |
b5a6470f DG |
1067 | |
1068 | return 0; | |
3bd1e081 MD |
1069 | } |
1070 | ||
1071 | /* | |
1072 | * Allocate the pollfd structure and the local view of the out fds to avoid | |
1073 | * doing a lookup in the linked list and concurrency issues when writing is | |
1074 | * needed. Called with consumer_data.lock held. | |
1075 | * | |
1076 | * Returns the number of fds in the structures. | |
1077 | */ | |
ffe60014 DG |
1078 | static int update_poll_array(struct lttng_consumer_local_data *ctx, |
1079 | struct pollfd **pollfd, struct lttng_consumer_stream **local_stream, | |
2afa177e | 1080 | struct lttng_ht *ht, int *nb_inactive_fd) |
3bd1e081 | 1081 | { |
3bd1e081 | 1082 | int i = 0; |
e4421fec DG |
1083 | struct lttng_ht_iter iter; |
1084 | struct lttng_consumer_stream *stream; | |
3bd1e081 | 1085 | |
ffe60014 DG |
1086 | assert(ctx); |
1087 | assert(ht); | |
1088 | assert(pollfd); | |
1089 | assert(local_stream); | |
1090 | ||
3bd1e081 | 1091 | DBG("Updating poll fd array"); |
2afa177e | 1092 | *nb_inactive_fd = 0; |
481d6c57 | 1093 | rcu_read_lock(); |
43c34bc3 | 1094 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
8994307f DG |
1095 | /* |
1096 | * Only active streams with an active end point can be added to the | |
1097 | * poll set and local stream storage of the thread. | |
1098 | * | |
1099 | * There is a potential race here for endpoint_status to be updated | |
1100 | * just after the check. However, this is OK since the stream(s) will | |
1101 | * be deleted once the thread is notified that the end point state has | |
1102 | * changed where this function will be called back again. | |
2afa177e JD |
1103 | * |
1104 | * We track the number of inactive FDs because they still need to be | |
1105 | * closed by the polling thread after a wakeup on the data_pipe or | |
1106 | * metadata_pipe. | |
8994307f DG |
1107 | */ |
1108 | if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM || | |
79d4ffb7 | 1109 | stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) { |
2afa177e | 1110 | (*nb_inactive_fd)++; |
3bd1e081 MD |
1111 | continue; |
1112 | } | |
7972aab2 DG |
1113 | /* |
1114 | * This clobbers way too much the debug output. Uncomment that if you | |
1115 | * need it for debugging purposes. | |
1116 | * | |
1117 | * DBG("Active FD %d", stream->wait_fd); | |
1118 | */ | |
e4421fec | 1119 | (*pollfd)[i].fd = stream->wait_fd; |
3bd1e081 | 1120 | (*pollfd)[i].events = POLLIN | POLLPRI; |
e4421fec | 1121 | local_stream[i] = stream; |
3bd1e081 MD |
1122 | i++; |
1123 | } | |
481d6c57 | 1124 | rcu_read_unlock(); |
3bd1e081 MD |
1125 | |
1126 | /* | |
50f8ae69 | 1127 | * Insert the consumer_data_pipe at the end of the array and don't |
3bd1e081 MD |
1128 | * increment i so nb_fd is the number of real FD. |
1129 | */ | |
acdb9057 | 1130 | (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe); |
509bb1cf | 1131 | (*pollfd)[i].events = POLLIN | POLLPRI; |
02b3d176 DG |
1132 | |
1133 | (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe); | |
1134 | (*pollfd)[i + 1].events = POLLIN | POLLPRI; | |
3bd1e081 MD |
1135 | return i; |
1136 | } | |
1137 | ||
1138 | /* | |
84382d49 MD |
1139 | * Poll on the should_quit pipe and the command socket return -1 on |
1140 | * error, 1 if should exit, 0 if data is available on the command socket | |
3bd1e081 MD |
1141 | */ |
1142 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) | |
1143 | { | |
1144 | int num_rdy; | |
1145 | ||
88f2b785 | 1146 | restart: |
3bd1e081 MD |
1147 | num_rdy = poll(consumer_sockpoll, 2, -1); |
1148 | if (num_rdy == -1) { | |
88f2b785 MD |
1149 | /* |
1150 | * Restart interrupted system call. | |
1151 | */ | |
1152 | if (errno == EINTR) { | |
1153 | goto restart; | |
1154 | } | |
7a57cf92 | 1155 | PERROR("Poll error"); |
84382d49 | 1156 | return -1; |
3bd1e081 | 1157 | } |
509bb1cf | 1158 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
3bd1e081 | 1159 | DBG("consumer_should_quit wake up"); |
84382d49 | 1160 | return 1; |
3bd1e081 MD |
1161 | } |
1162 | return 0; | |
3bd1e081 MD |
1163 | } |
1164 | ||
1165 | /* | |
1166 | * Set the error socket. | |
1167 | */ | |
ffe60014 DG |
1168 | void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx, |
1169 | int sock) | |
3bd1e081 MD |
1170 | { |
1171 | ctx->consumer_error_socket = sock; | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Set the command socket path. | |
1176 | */ | |
3bd1e081 MD |
1177 | void lttng_consumer_set_command_sock_path( |
1178 | struct lttng_consumer_local_data *ctx, char *sock) | |
1179 | { | |
1180 | ctx->consumer_command_sock_path = sock; | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * Send return code to the session daemon. | |
1185 | * If the socket is not defined, we return 0, it is not a fatal error | |
1186 | */ | |
ffe60014 | 1187 | int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd) |
3bd1e081 MD |
1188 | { |
1189 | if (ctx->consumer_error_socket > 0) { | |
1190 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, | |
1191 | sizeof(enum lttcomm_sessiond_command)); | |
1192 | } | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | ||
1197 | /* | |
228b5bf7 DG |
1198 | * Close all the tracefiles and stream fds and MUST be called when all |
1199 | * instances are destroyed i.e. when all threads were joined and are ended. | |
3bd1e081 MD |
1200 | */ |
1201 | void lttng_consumer_cleanup(void) | |
1202 | { | |
e4421fec | 1203 | struct lttng_ht_iter iter; |
ffe60014 | 1204 | struct lttng_consumer_channel *channel; |
6065ceec DG |
1205 | |
1206 | rcu_read_lock(); | |
3bd1e081 | 1207 | |
ffe60014 DG |
1208 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel, |
1209 | node.node) { | |
702b1ea4 | 1210 | consumer_del_channel(channel); |
3bd1e081 | 1211 | } |
6065ceec DG |
1212 | |
1213 | rcu_read_unlock(); | |
d6ce1df2 | 1214 | |
d6ce1df2 | 1215 | lttng_ht_destroy(consumer_data.channel_ht); |
228b5bf7 DG |
1216 | |
1217 | cleanup_relayd_ht(); | |
1218 | ||
d8ef542d MD |
1219 | lttng_ht_destroy(consumer_data.stream_per_chan_id_ht); |
1220 | ||
228b5bf7 DG |
1221 | /* |
1222 | * This HT contains streams that are freed by either the metadata thread or | |
1223 | * the data thread so we do *nothing* on the hash table and simply destroy | |
1224 | * it. | |
1225 | */ | |
1226 | lttng_ht_destroy(consumer_data.stream_list_ht); | |
3bd1e081 MD |
1227 | } |
1228 | ||
1229 | /* | |
1230 | * Called from signal handler. | |
1231 | */ | |
1232 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) | |
1233 | { | |
6cd525e8 MD |
1234 | ssize_t ret; |
1235 | ||
f7389fbb | 1236 | CMM_STORE_SHARED(consumer_quit, 1); |
6cd525e8 MD |
1237 | ret = lttng_write(ctx->consumer_should_quit[1], "4", 1); |
1238 | if (ret < 1) { | |
7a57cf92 | 1239 | PERROR("write consumer quit"); |
3bd1e081 | 1240 | } |
ab1027f4 DG |
1241 | |
1242 | DBG("Consumer flag that it should quit"); | |
3bd1e081 MD |
1243 | } |
1244 | ||
5199ffc4 JG |
1245 | |
1246 | /* | |
1247 | * Flush pending writes to trace output disk file. | |
1248 | */ | |
1249 | static | |
00e2e675 DG |
1250 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
1251 | off_t orig_offset) | |
3bd1e081 | 1252 | { |
c7a78aab | 1253 | int ret; |
3bd1e081 MD |
1254 | int outfd = stream->out_fd; |
1255 | ||
1256 | /* | |
1257 | * This does a blocking write-and-wait on any page that belongs to the | |
1258 | * subbuffer prior to the one we just wrote. | |
1259 | * Don't care about error values, as these are just hints and ways to | |
1260 | * limit the amount of page cache used. | |
1261 | */ | |
ffe60014 | 1262 | if (orig_offset < stream->max_sb_size) { |
3bd1e081 MD |
1263 | return; |
1264 | } | |
ffe60014 DG |
1265 | lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size, |
1266 | stream->max_sb_size, | |
3bd1e081 MD |
1267 | SYNC_FILE_RANGE_WAIT_BEFORE |
1268 | | SYNC_FILE_RANGE_WRITE | |
1269 | | SYNC_FILE_RANGE_WAIT_AFTER); | |
1270 | /* | |
1271 | * Give hints to the kernel about how we access the file: | |
1272 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after | |
1273 | * we write it. | |
1274 | * | |
1275 | * We need to call fadvise again after the file grows because the | |
1276 | * kernel does not seem to apply fadvise to non-existing parts of the | |
1277 | * file. | |
1278 | * | |
1279 | * Call fadvise _after_ having waited for the page writeback to | |
1280 | * complete because the dirty page writeback semantic is not well | |
1281 | * defined. So it can be expected to lead to lower throughput in | |
1282 | * streaming. | |
1283 | */ | |
c7a78aab | 1284 | ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size, |
ffe60014 | 1285 | stream->max_sb_size, POSIX_FADV_DONTNEED); |
a0d0e127 | 1286 | if (ret && ret != -ENOSYS) { |
a74a5f4a JG |
1287 | errno = ret; |
1288 | PERROR("posix_fadvise on fd %i", outfd); | |
c7a78aab | 1289 | } |
3bd1e081 MD |
1290 | } |
1291 | ||
1292 | /* | |
1293 | * Initialise the necessary environnement : | |
1294 | * - create a new context | |
1295 | * - create the poll_pipe | |
1296 | * - create the should_quit pipe (for signal handler) | |
1297 | * - create the thread pipe (for splice) | |
1298 | * | |
1299 | * Takes a function pointer as argument, this function is called when data is | |
1300 | * available on a buffer. This function is responsible to do the | |
1301 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the | |
1302 | * buffer configuration and then kernctl_put_next_subbuf at the end. | |
1303 | * | |
1304 | * Returns a pointer to the new context or NULL on error. | |
1305 | */ | |
1306 | struct lttng_consumer_local_data *lttng_consumer_create( | |
1307 | enum lttng_consumer_type type, | |
4078b776 | 1308 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
d41f73b7 | 1309 | struct lttng_consumer_local_data *ctx), |
3bd1e081 MD |
1310 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
1311 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
30319bcb | 1312 | int (*update_stream)(uint64_t stream_key, uint32_t state)) |
3bd1e081 | 1313 | { |
d8ef542d | 1314 | int ret; |
3bd1e081 MD |
1315 | struct lttng_consumer_local_data *ctx; |
1316 | ||
1317 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || | |
1318 | consumer_data.type == type); | |
1319 | consumer_data.type = type; | |
1320 | ||
effcf122 | 1321 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
3bd1e081 | 1322 | if (ctx == NULL) { |
7a57cf92 | 1323 | PERROR("allocating context"); |
3bd1e081 MD |
1324 | goto error; |
1325 | } | |
1326 | ||
1327 | ctx->consumer_error_socket = -1; | |
331744e3 | 1328 | ctx->consumer_metadata_socket = -1; |
75d83e50 | 1329 | pthread_mutex_init(&ctx->metadata_socket_lock, NULL); |
3bd1e081 MD |
1330 | /* assign the callbacks */ |
1331 | ctx->on_buffer_ready = buffer_ready; | |
1332 | ctx->on_recv_channel = recv_channel; | |
1333 | ctx->on_recv_stream = recv_stream; | |
1334 | ctx->on_update_stream = update_stream; | |
1335 | ||
acdb9057 DG |
1336 | ctx->consumer_data_pipe = lttng_pipe_open(0); |
1337 | if (!ctx->consumer_data_pipe) { | |
3bd1e081 MD |
1338 | goto error_poll_pipe; |
1339 | } | |
1340 | ||
02b3d176 DG |
1341 | ctx->consumer_wakeup_pipe = lttng_pipe_open(0); |
1342 | if (!ctx->consumer_wakeup_pipe) { | |
1343 | goto error_wakeup_pipe; | |
1344 | } | |
1345 | ||
3bd1e081 MD |
1346 | ret = pipe(ctx->consumer_should_quit); |
1347 | if (ret < 0) { | |
7a57cf92 | 1348 | PERROR("Error creating recv pipe"); |
3bd1e081 MD |
1349 | goto error_quit_pipe; |
1350 | } | |
1351 | ||
d8ef542d MD |
1352 | ret = pipe(ctx->consumer_channel_pipe); |
1353 | if (ret < 0) { | |
1354 | PERROR("Error creating channel pipe"); | |
1355 | goto error_channel_pipe; | |
1356 | } | |
1357 | ||
13886d2d DG |
1358 | ctx->consumer_metadata_pipe = lttng_pipe_open(0); |
1359 | if (!ctx->consumer_metadata_pipe) { | |
fb3a43a9 DG |
1360 | goto error_metadata_pipe; |
1361 | } | |
3bd1e081 | 1362 | |
e9404c27 JG |
1363 | ctx->channel_monitor_pipe = -1; |
1364 | ||
fb3a43a9 | 1365 | return ctx; |
3bd1e081 | 1366 | |
fb3a43a9 | 1367 | error_metadata_pipe: |
d8ef542d MD |
1368 | utils_close_pipe(ctx->consumer_channel_pipe); |
1369 | error_channel_pipe: | |
d8ef542d | 1370 | utils_close_pipe(ctx->consumer_should_quit); |
3bd1e081 | 1371 | error_quit_pipe: |
02b3d176 DG |
1372 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
1373 | error_wakeup_pipe: | |
acdb9057 | 1374 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
3bd1e081 MD |
1375 | error_poll_pipe: |
1376 | free(ctx); | |
1377 | error: | |
1378 | return NULL; | |
1379 | } | |
1380 | ||
282dadbc MD |
1381 | /* |
1382 | * Iterate over all streams of the hashtable and free them properly. | |
1383 | */ | |
1384 | static void destroy_data_stream_ht(struct lttng_ht *ht) | |
1385 | { | |
1386 | struct lttng_ht_iter iter; | |
1387 | struct lttng_consumer_stream *stream; | |
1388 | ||
1389 | if (ht == NULL) { | |
1390 | return; | |
1391 | } | |
1392 | ||
1393 | rcu_read_lock(); | |
1394 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1395 | /* | |
1396 | * Ignore return value since we are currently cleaning up so any error | |
1397 | * can't be handled. | |
1398 | */ | |
1399 | (void) consumer_del_stream(stream, ht); | |
1400 | } | |
1401 | rcu_read_unlock(); | |
1402 | ||
1403 | lttng_ht_destroy(ht); | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * Iterate over all streams of the metadata hashtable and free them | |
1408 | * properly. | |
1409 | */ | |
1410 | static void destroy_metadata_stream_ht(struct lttng_ht *ht) | |
1411 | { | |
1412 | struct lttng_ht_iter iter; | |
1413 | struct lttng_consumer_stream *stream; | |
1414 | ||
1415 | if (ht == NULL) { | |
1416 | return; | |
1417 | } | |
1418 | ||
1419 | rcu_read_lock(); | |
1420 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1421 | /* | |
1422 | * Ignore return value since we are currently cleaning up so any error | |
1423 | * can't be handled. | |
1424 | */ | |
1425 | (void) consumer_del_metadata_stream(stream, ht); | |
1426 | } | |
1427 | rcu_read_unlock(); | |
1428 | ||
1429 | lttng_ht_destroy(ht); | |
1430 | } | |
1431 | ||
3bd1e081 MD |
1432 | /* |
1433 | * Close all fds associated with the instance and free the context. | |
1434 | */ | |
1435 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) | |
1436 | { | |
4c462e79 MD |
1437 | int ret; |
1438 | ||
ab1027f4 DG |
1439 | DBG("Consumer destroying it. Closing everything."); |
1440 | ||
4f2e75b9 DG |
1441 | if (!ctx) { |
1442 | return; | |
1443 | } | |
1444 | ||
282dadbc MD |
1445 | destroy_data_stream_ht(data_ht); |
1446 | destroy_metadata_stream_ht(metadata_ht); | |
1447 | ||
4c462e79 MD |
1448 | ret = close(ctx->consumer_error_socket); |
1449 | if (ret) { | |
1450 | PERROR("close"); | |
1451 | } | |
331744e3 JD |
1452 | ret = close(ctx->consumer_metadata_socket); |
1453 | if (ret) { | |
1454 | PERROR("close"); | |
1455 | } | |
d8ef542d | 1456 | utils_close_pipe(ctx->consumer_channel_pipe); |
acdb9057 | 1457 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
13886d2d | 1458 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
02b3d176 | 1459 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
d8ef542d | 1460 | utils_close_pipe(ctx->consumer_should_quit); |
fb3a43a9 | 1461 | |
3bd1e081 MD |
1462 | unlink(ctx->consumer_command_sock_path); |
1463 | free(ctx); | |
1464 | } | |
1465 | ||
6197aea7 DG |
1466 | /* |
1467 | * Write the metadata stream id on the specified file descriptor. | |
1468 | */ | |
1469 | static int write_relayd_metadata_id(int fd, | |
1470 | struct lttng_consumer_stream *stream, | |
ffe60014 | 1471 | struct consumer_relayd_sock_pair *relayd, unsigned long padding) |
6197aea7 | 1472 | { |
6cd525e8 | 1473 | ssize_t ret; |
1d4dfdef | 1474 | struct lttcomm_relayd_metadata_payload hdr; |
6197aea7 | 1475 | |
1d4dfdef DG |
1476 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
1477 | hdr.padding_size = htobe32(padding); | |
6cd525e8 MD |
1478 | ret = lttng_write(fd, (void *) &hdr, sizeof(hdr)); |
1479 | if (ret < sizeof(hdr)) { | |
d7b75ec8 | 1480 | /* |
6f04ed72 | 1481 | * This error means that the fd's end is closed so ignore the PERROR |
d7b75ec8 DG |
1482 | * not to clubber the error output since this can happen in a normal |
1483 | * code path. | |
1484 | */ | |
1485 | if (errno != EPIPE) { | |
1486 | PERROR("write metadata stream id"); | |
1487 | } | |
1488 | DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno); | |
534d2592 DG |
1489 | /* |
1490 | * Set ret to a negative value because if ret != sizeof(hdr), we don't | |
1491 | * handle writting the missing part so report that as an error and | |
1492 | * don't lie to the caller. | |
1493 | */ | |
1494 | ret = -1; | |
6197aea7 DG |
1495 | goto end; |
1496 | } | |
1d4dfdef DG |
1497 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
1498 | stream->relayd_stream_id, padding); | |
6197aea7 DG |
1499 | |
1500 | end: | |
6cd525e8 | 1501 | return (int) ret; |
6197aea7 DG |
1502 | } |
1503 | ||
3bd1e081 | 1504 | /* |
09e26845 DG |
1505 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
1506 | * core function for writing trace buffers to either the local filesystem or | |
1507 | * the network. | |
1508 | * | |
79d4ffb7 DG |
1509 | * It must be called with the stream lock held. |
1510 | * | |
09e26845 | 1511 | * Careful review MUST be put if any changes occur! |
3bd1e081 MD |
1512 | * |
1513 | * Returns the number of bytes written | |
1514 | */ | |
4078b776 | 1515 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
3bd1e081 | 1516 | struct lttng_consumer_local_data *ctx, |
1d4dfdef | 1517 | struct lttng_consumer_stream *stream, unsigned long len, |
309167d2 | 1518 | unsigned long padding, |
50adc264 | 1519 | struct ctf_packet_index *index) |
3bd1e081 | 1520 | { |
f02e1e8a | 1521 | unsigned long mmap_offset; |
ffe60014 | 1522 | void *mmap_base; |
994ab360 | 1523 | ssize_t ret = 0; |
f02e1e8a DG |
1524 | off_t orig_offset = stream->out_fd_offset; |
1525 | /* Default is on the disk */ | |
1526 | int outfd = stream->out_fd; | |
f02e1e8a | 1527 | struct consumer_relayd_sock_pair *relayd = NULL; |
8994307f | 1528 | unsigned int relayd_hang_up = 0; |
f02e1e8a DG |
1529 | |
1530 | /* RCU lock for the relayd pointer */ | |
1531 | rcu_read_lock(); | |
1532 | ||
1533 | /* Flag that the current stream if set for network streaming. */ | |
da009f2c | 1534 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1535 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1536 | if (relayd == NULL) { | |
56591bac | 1537 | ret = -EPIPE; |
f02e1e8a DG |
1538 | goto end; |
1539 | } | |
1540 | } | |
1541 | ||
1542 | /* get the offset inside the fd to mmap */ | |
3bd1e081 MD |
1543 | switch (consumer_data.type) { |
1544 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 1545 | mmap_base = stream->mmap_base; |
f02e1e8a | 1546 | ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset); |
994ab360 | 1547 | if (ret < 0) { |
56591bac | 1548 | PERROR("tracer ctl get_mmap_read_offset"); |
56591bac MD |
1549 | goto end; |
1550 | } | |
f02e1e8a | 1551 | break; |
7753dea8 MD |
1552 | case LTTNG_CONSUMER32_UST: |
1553 | case LTTNG_CONSUMER64_UST: | |
ffe60014 DG |
1554 | mmap_base = lttng_ustctl_get_mmap_base(stream); |
1555 | if (!mmap_base) { | |
1556 | ERR("read mmap get mmap base for stream %s", stream->name); | |
994ab360 | 1557 | ret = -EPERM; |
ffe60014 DG |
1558 | goto end; |
1559 | } | |
1560 | ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset); | |
56591bac MD |
1561 | if (ret != 0) { |
1562 | PERROR("tracer ctl get_mmap_read_offset"); | |
994ab360 | 1563 | ret = -EINVAL; |
56591bac MD |
1564 | goto end; |
1565 | } | |
f02e1e8a | 1566 | break; |
3bd1e081 MD |
1567 | default: |
1568 | ERR("Unknown consumer_data type"); | |
1569 | assert(0); | |
1570 | } | |
b9182dd9 | 1571 | |
f02e1e8a DG |
1572 | /* Handle stream on the relayd if the output is on the network */ |
1573 | if (relayd) { | |
1574 | unsigned long netlen = len; | |
1575 | ||
1576 | /* | |
1577 | * Lock the control socket for the complete duration of the function | |
1578 | * since from this point on we will use the socket. | |
1579 | */ | |
1580 | if (stream->metadata_flag) { | |
1581 | /* Metadata requires the control socket. */ | |
1582 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
93ec662e JD |
1583 | if (stream->reset_metadata_flag) { |
1584 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1585 | stream->relayd_stream_id, | |
1586 | stream->metadata_version); | |
1587 | if (ret < 0) { | |
1588 | relayd_hang_up = 1; | |
1589 | goto write_error; | |
1590 | } | |
1591 | stream->reset_metadata_flag = 0; | |
1592 | } | |
1d4dfdef | 1593 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
f02e1e8a DG |
1594 | } |
1595 | ||
1d4dfdef | 1596 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
994ab360 DG |
1597 | if (ret < 0) { |
1598 | relayd_hang_up = 1; | |
1599 | goto write_error; | |
1600 | } | |
1601 | /* Use the returned socket. */ | |
1602 | outfd = ret; | |
f02e1e8a | 1603 | |
994ab360 DG |
1604 | /* Write metadata stream id before payload */ |
1605 | if (stream->metadata_flag) { | |
1606 | ret = write_relayd_metadata_id(outfd, stream, relayd, padding); | |
1607 | if (ret < 0) { | |
8994307f DG |
1608 | relayd_hang_up = 1; |
1609 | goto write_error; | |
1610 | } | |
f02e1e8a | 1611 | } |
1d4dfdef DG |
1612 | } else { |
1613 | /* No streaming, we have to set the len with the full padding */ | |
1614 | len += padding; | |
1624d5b7 | 1615 | |
93ec662e JD |
1616 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1617 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1618 | if (ret < 0) { | |
1619 | ERR("Reset metadata file"); | |
1620 | goto end; | |
1621 | } | |
1622 | stream->reset_metadata_flag = 0; | |
1623 | } | |
1624 | ||
1624d5b7 JD |
1625 | /* |
1626 | * Check if we need to change the tracefile before writing the packet. | |
1627 | */ | |
1628 | if (stream->chan->tracefile_size > 0 && | |
1629 | (stream->tracefile_size_current + len) > | |
1630 | stream->chan->tracefile_size) { | |
fe4477ee JD |
1631 | ret = utils_rotate_stream_file(stream->chan->pathname, |
1632 | stream->name, stream->chan->tracefile_size, | |
1633 | stream->chan->tracefile_count, stream->uid, stream->gid, | |
309167d2 JD |
1634 | stream->out_fd, &(stream->tracefile_count_current), |
1635 | &stream->out_fd); | |
1624d5b7 JD |
1636 | if (ret < 0) { |
1637 | ERR("Rotating output file"); | |
1638 | goto end; | |
1639 | } | |
309167d2 JD |
1640 | outfd = stream->out_fd; |
1641 | ||
f8f3885c MD |
1642 | if (stream->index_file) { |
1643 | lttng_index_file_put(stream->index_file); | |
1644 | stream->index_file = lttng_index_file_create(stream->chan->pathname, | |
309167d2 JD |
1645 | stream->name, stream->uid, stream->gid, |
1646 | stream->chan->tracefile_size, | |
f8f3885c MD |
1647 | stream->tracefile_count_current, |
1648 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
1649 | if (!stream->index_file) { | |
309167d2 JD |
1650 | goto end; |
1651 | } | |
309167d2 JD |
1652 | } |
1653 | ||
a6976990 DG |
1654 | /* Reset current size because we just perform a rotation. */ |
1655 | stream->tracefile_size_current = 0; | |
a1ae300f JD |
1656 | stream->out_fd_offset = 0; |
1657 | orig_offset = 0; | |
1624d5b7 JD |
1658 | } |
1659 | stream->tracefile_size_current += len; | |
309167d2 JD |
1660 | if (index) { |
1661 | index->offset = htobe64(stream->out_fd_offset); | |
1662 | } | |
f02e1e8a DG |
1663 | } |
1664 | ||
d02b8372 DG |
1665 | /* |
1666 | * This call guarantee that len or less is returned. It's impossible to | |
1667 | * receive a ret value that is bigger than len. | |
1668 | */ | |
1669 | ret = lttng_write(outfd, mmap_base + mmap_offset, len); | |
1670 | DBG("Consumer mmap write() ret %zd (len %lu)", ret, len); | |
1671 | if (ret < 0 || ((size_t) ret != len)) { | |
1672 | /* | |
1673 | * Report error to caller if nothing was written else at least send the | |
1674 | * amount written. | |
1675 | */ | |
1676 | if (ret < 0) { | |
994ab360 | 1677 | ret = -errno; |
f02e1e8a | 1678 | } |
994ab360 | 1679 | relayd_hang_up = 1; |
f02e1e8a | 1680 | |
d02b8372 | 1681 | /* Socket operation failed. We consider the relayd dead */ |
994ab360 | 1682 | if (errno == EPIPE || errno == EINVAL || errno == EBADF) { |
d02b8372 DG |
1683 | /* |
1684 | * This is possible if the fd is closed on the other side | |
1685 | * (outfd) or any write problem. It can be verbose a bit for a | |
1686 | * normal execution if for instance the relayd is stopped | |
1687 | * abruptly. This can happen so set this to a DBG statement. | |
1688 | */ | |
1689 | DBG("Consumer mmap write detected relayd hang up"); | |
994ab360 DG |
1690 | } else { |
1691 | /* Unhandled error, print it and stop function right now. */ | |
1692 | PERROR("Error in write mmap (ret %zd != len %lu)", ret, len); | |
f02e1e8a | 1693 | } |
994ab360 | 1694 | goto write_error; |
d02b8372 DG |
1695 | } |
1696 | stream->output_written += ret; | |
d02b8372 DG |
1697 | |
1698 | /* This call is useless on a socket so better save a syscall. */ | |
1699 | if (!relayd) { | |
1700 | /* This won't block, but will start writeout asynchronously */ | |
1701 | lttng_sync_file_range(outfd, stream->out_fd_offset, len, | |
1702 | SYNC_FILE_RANGE_WRITE); | |
1703 | stream->out_fd_offset += len; | |
f5dbe415 | 1704 | lttng_consumer_sync_trace_file(stream, orig_offset); |
f02e1e8a | 1705 | } |
f02e1e8a | 1706 | |
8994307f DG |
1707 | write_error: |
1708 | /* | |
1709 | * This is a special case that the relayd has closed its socket. Let's | |
1710 | * cleanup the relayd object and all associated streams. | |
1711 | */ | |
1712 | if (relayd && relayd_hang_up) { | |
1713 | cleanup_relayd(relayd, ctx); | |
1714 | } | |
1715 | ||
f02e1e8a DG |
1716 | end: |
1717 | /* Unlock only if ctrl socket used */ | |
1718 | if (relayd && stream->metadata_flag) { | |
1719 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1720 | } | |
1721 | ||
1722 | rcu_read_unlock(); | |
994ab360 | 1723 | return ret; |
3bd1e081 MD |
1724 | } |
1725 | ||
1726 | /* | |
1727 | * Splice the data from the ring buffer to the tracefile. | |
1728 | * | |
79d4ffb7 DG |
1729 | * It must be called with the stream lock held. |
1730 | * | |
3bd1e081 MD |
1731 | * Returns the number of bytes spliced. |
1732 | */ | |
4078b776 | 1733 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 | 1734 | struct lttng_consumer_local_data *ctx, |
1d4dfdef | 1735 | struct lttng_consumer_stream *stream, unsigned long len, |
309167d2 | 1736 | unsigned long padding, |
50adc264 | 1737 | struct ctf_packet_index *index) |
3bd1e081 | 1738 | { |
f02e1e8a DG |
1739 | ssize_t ret = 0, written = 0, ret_splice = 0; |
1740 | loff_t offset = 0; | |
1741 | off_t orig_offset = stream->out_fd_offset; | |
1742 | int fd = stream->wait_fd; | |
1743 | /* Default is on the disk */ | |
1744 | int outfd = stream->out_fd; | |
f02e1e8a | 1745 | struct consumer_relayd_sock_pair *relayd = NULL; |
fb3a43a9 | 1746 | int *splice_pipe; |
8994307f | 1747 | unsigned int relayd_hang_up = 0; |
f02e1e8a | 1748 | |
3bd1e081 MD |
1749 | switch (consumer_data.type) { |
1750 | case LTTNG_CONSUMER_KERNEL: | |
f02e1e8a | 1751 | break; |
7753dea8 MD |
1752 | case LTTNG_CONSUMER32_UST: |
1753 | case LTTNG_CONSUMER64_UST: | |
f02e1e8a | 1754 | /* Not supported for user space tracing */ |
3bd1e081 MD |
1755 | return -ENOSYS; |
1756 | default: | |
1757 | ERR("Unknown consumer_data type"); | |
1758 | assert(0); | |
3bd1e081 MD |
1759 | } |
1760 | ||
f02e1e8a DG |
1761 | /* RCU lock for the relayd pointer */ |
1762 | rcu_read_lock(); | |
1763 | ||
1764 | /* Flag that the current stream if set for network streaming. */ | |
da009f2c | 1765 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1766 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1767 | if (relayd == NULL) { | |
ad0b0d23 | 1768 | written = -ret; |
f02e1e8a DG |
1769 | goto end; |
1770 | } | |
1771 | } | |
a2361a61 | 1772 | splice_pipe = stream->splice_pipe; |
fb3a43a9 | 1773 | |
f02e1e8a | 1774 | /* Write metadata stream id before payload */ |
1d4dfdef | 1775 | if (relayd) { |
ad0b0d23 | 1776 | unsigned long total_len = len; |
f02e1e8a | 1777 | |
1d4dfdef DG |
1778 | if (stream->metadata_flag) { |
1779 | /* | |
1780 | * Lock the control socket for the complete duration of the function | |
1781 | * since from this point on we will use the socket. | |
1782 | */ | |
1783 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
1784 | ||
93ec662e JD |
1785 | if (stream->reset_metadata_flag) { |
1786 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1787 | stream->relayd_stream_id, | |
1788 | stream->metadata_version); | |
1789 | if (ret < 0) { | |
1790 | relayd_hang_up = 1; | |
1791 | goto write_error; | |
1792 | } | |
1793 | stream->reset_metadata_flag = 0; | |
1794 | } | |
1d4dfdef DG |
1795 | ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd, |
1796 | padding); | |
1797 | if (ret < 0) { | |
1798 | written = ret; | |
ad0b0d23 DG |
1799 | relayd_hang_up = 1; |
1800 | goto write_error; | |
1d4dfdef DG |
1801 | } |
1802 | ||
1803 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); | |
1804 | } | |
1805 | ||
1806 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); | |
ad0b0d23 DG |
1807 | if (ret < 0) { |
1808 | written = ret; | |
1809 | relayd_hang_up = 1; | |
1810 | goto write_error; | |
f02e1e8a | 1811 | } |
ad0b0d23 DG |
1812 | /* Use the returned socket. */ |
1813 | outfd = ret; | |
1d4dfdef DG |
1814 | } else { |
1815 | /* No streaming, we have to set the len with the full padding */ | |
1816 | len += padding; | |
1624d5b7 | 1817 | |
93ec662e JD |
1818 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1819 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1820 | if (ret < 0) { | |
1821 | ERR("Reset metadata file"); | |
1822 | goto end; | |
1823 | } | |
1824 | stream->reset_metadata_flag = 0; | |
1825 | } | |
1624d5b7 JD |
1826 | /* |
1827 | * Check if we need to change the tracefile before writing the packet. | |
1828 | */ | |
1829 | if (stream->chan->tracefile_size > 0 && | |
1830 | (stream->tracefile_size_current + len) > | |
1831 | stream->chan->tracefile_size) { | |
fe4477ee JD |
1832 | ret = utils_rotate_stream_file(stream->chan->pathname, |
1833 | stream->name, stream->chan->tracefile_size, | |
1834 | stream->chan->tracefile_count, stream->uid, stream->gid, | |
309167d2 JD |
1835 | stream->out_fd, &(stream->tracefile_count_current), |
1836 | &stream->out_fd); | |
1624d5b7 | 1837 | if (ret < 0) { |
ad0b0d23 | 1838 | written = ret; |
1624d5b7 JD |
1839 | ERR("Rotating output file"); |
1840 | goto end; | |
1841 | } | |
309167d2 JD |
1842 | outfd = stream->out_fd; |
1843 | ||
f8f3885c MD |
1844 | if (stream->index_file) { |
1845 | lttng_index_file_put(stream->index_file); | |
1846 | stream->index_file = lttng_index_file_create(stream->chan->pathname, | |
309167d2 JD |
1847 | stream->name, stream->uid, stream->gid, |
1848 | stream->chan->tracefile_size, | |
f8f3885c MD |
1849 | stream->tracefile_count_current, |
1850 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
1851 | if (!stream->index_file) { | |
309167d2 JD |
1852 | goto end; |
1853 | } | |
309167d2 JD |
1854 | } |
1855 | ||
a6976990 DG |
1856 | /* Reset current size because we just perform a rotation. */ |
1857 | stream->tracefile_size_current = 0; | |
a1ae300f JD |
1858 | stream->out_fd_offset = 0; |
1859 | orig_offset = 0; | |
1624d5b7 JD |
1860 | } |
1861 | stream->tracefile_size_current += len; | |
309167d2 | 1862 | index->offset = htobe64(stream->out_fd_offset); |
f02e1e8a DG |
1863 | } |
1864 | ||
1865 | while (len > 0) { | |
1d4dfdef DG |
1866 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
1867 | (unsigned long)offset, len, fd, splice_pipe[1]); | |
fb3a43a9 | 1868 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
f02e1e8a DG |
1869 | SPLICE_F_MOVE | SPLICE_F_MORE); |
1870 | DBG("splice chan to pipe, ret %zd", ret_splice); | |
1871 | if (ret_splice < 0) { | |
d02b8372 | 1872 | ret = errno; |
ad0b0d23 | 1873 | written = -ret; |
d02b8372 | 1874 | PERROR("Error in relay splice"); |
f02e1e8a DG |
1875 | goto splice_error; |
1876 | } | |
1877 | ||
1878 | /* Handle stream on the relayd if the output is on the network */ | |
ad0b0d23 DG |
1879 | if (relayd && stream->metadata_flag) { |
1880 | size_t metadata_payload_size = | |
1881 | sizeof(struct lttcomm_relayd_metadata_payload); | |
1882 | ||
1883 | /* Update counter to fit the spliced data */ | |
1884 | ret_splice += metadata_payload_size; | |
1885 | len += metadata_payload_size; | |
1886 | /* | |
1887 | * We do this so the return value can match the len passed as | |
1888 | * argument to this function. | |
1889 | */ | |
1890 | written -= metadata_payload_size; | |
f02e1e8a DG |
1891 | } |
1892 | ||
1893 | /* Splice data out */ | |
fb3a43a9 | 1894 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
f02e1e8a | 1895 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
a2361a61 JD |
1896 | DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", |
1897 | outfd, ret_splice); | |
f02e1e8a | 1898 | if (ret_splice < 0) { |
d02b8372 | 1899 | ret = errno; |
ad0b0d23 DG |
1900 | written = -ret; |
1901 | relayd_hang_up = 1; | |
1902 | goto write_error; | |
f02e1e8a | 1903 | } else if (ret_splice > len) { |
d02b8372 DG |
1904 | /* |
1905 | * We don't expect this code path to be executed but you never know | |
1906 | * so this is an extra protection agains a buggy splice(). | |
1907 | */ | |
f02e1e8a | 1908 | ret = errno; |
ad0b0d23 | 1909 | written += ret_splice; |
d02b8372 DG |
1910 | PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice, |
1911 | len); | |
f02e1e8a | 1912 | goto splice_error; |
d02b8372 DG |
1913 | } else { |
1914 | /* All good, update current len and continue. */ | |
1915 | len -= ret_splice; | |
f02e1e8a | 1916 | } |
f02e1e8a DG |
1917 | |
1918 | /* This call is useless on a socket so better save a syscall. */ | |
1919 | if (!relayd) { | |
1920 | /* This won't block, but will start writeout asynchronously */ | |
1921 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, | |
1922 | SYNC_FILE_RANGE_WRITE); | |
1923 | stream->out_fd_offset += ret_splice; | |
1924 | } | |
e5d1a9b3 | 1925 | stream->output_written += ret_splice; |
f02e1e8a DG |
1926 | written += ret_splice; |
1927 | } | |
f5dbe415 JG |
1928 | if (!relayd) { |
1929 | lttng_consumer_sync_trace_file(stream, orig_offset); | |
1930 | } | |
f02e1e8a DG |
1931 | goto end; |
1932 | ||
8994307f DG |
1933 | write_error: |
1934 | /* | |
1935 | * This is a special case that the relayd has closed its socket. Let's | |
1936 | * cleanup the relayd object and all associated streams. | |
1937 | */ | |
1938 | if (relayd && relayd_hang_up) { | |
1939 | cleanup_relayd(relayd, ctx); | |
1940 | /* Skip splice error so the consumer does not fail */ | |
1941 | goto end; | |
1942 | } | |
1943 | ||
f02e1e8a DG |
1944 | splice_error: |
1945 | /* send the appropriate error description to sessiond */ | |
1946 | switch (ret) { | |
f02e1e8a | 1947 | case EINVAL: |
f73fabfd | 1948 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
f02e1e8a DG |
1949 | break; |
1950 | case ENOMEM: | |
f73fabfd | 1951 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
f02e1e8a DG |
1952 | break; |
1953 | case ESPIPE: | |
f73fabfd | 1954 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
f02e1e8a DG |
1955 | break; |
1956 | } | |
1957 | ||
1958 | end: | |
1959 | if (relayd && stream->metadata_flag) { | |
1960 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1961 | } | |
1962 | ||
1963 | rcu_read_unlock(); | |
1964 | return written; | |
3bd1e081 MD |
1965 | } |
1966 | ||
1967 | /* | |
1968 | * Take a snapshot for a specific fd | |
1969 | * | |
1970 | * Returns 0 on success, < 0 on error | |
1971 | */ | |
ffe60014 | 1972 | int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
1973 | { |
1974 | switch (consumer_data.type) { | |
1975 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 1976 | return lttng_kconsumer_take_snapshot(stream); |
7753dea8 MD |
1977 | case LTTNG_CONSUMER32_UST: |
1978 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 1979 | return lttng_ustconsumer_take_snapshot(stream); |
3bd1e081 MD |
1980 | default: |
1981 | ERR("Unknown consumer_data type"); | |
1982 | assert(0); | |
1983 | return -ENOSYS; | |
1984 | } | |
3bd1e081 MD |
1985 | } |
1986 | ||
1987 | /* | |
1988 | * Get the produced position | |
1989 | * | |
1990 | * Returns 0 on success, < 0 on error | |
1991 | */ | |
ffe60014 | 1992 | int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
1993 | unsigned long *pos) |
1994 | { | |
1995 | switch (consumer_data.type) { | |
1996 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 1997 | return lttng_kconsumer_get_produced_snapshot(stream, pos); |
7753dea8 MD |
1998 | case LTTNG_CONSUMER32_UST: |
1999 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 2000 | return lttng_ustconsumer_get_produced_snapshot(stream, pos); |
3bd1e081 MD |
2001 | default: |
2002 | ERR("Unknown consumer_data type"); | |
2003 | assert(0); | |
2004 | return -ENOSYS; | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
2009 | int sock, struct pollfd *consumer_sockpoll) | |
2010 | { | |
2011 | switch (consumer_data.type) { | |
2012 | case LTTNG_CONSUMER_KERNEL: | |
2013 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
7753dea8 MD |
2014 | case LTTNG_CONSUMER32_UST: |
2015 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
2016 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
2017 | default: | |
2018 | ERR("Unknown consumer_data type"); | |
2019 | assert(0); | |
2020 | return -ENOSYS; | |
2021 | } | |
2022 | } | |
2023 | ||
6d574024 | 2024 | void lttng_consumer_close_all_metadata(void) |
d88aee68 DG |
2025 | { |
2026 | switch (consumer_data.type) { | |
2027 | case LTTNG_CONSUMER_KERNEL: | |
2028 | /* | |
2029 | * The Kernel consumer has a different metadata scheme so we don't | |
2030 | * close anything because the stream will be closed by the session | |
2031 | * daemon. | |
2032 | */ | |
2033 | break; | |
2034 | case LTTNG_CONSUMER32_UST: | |
2035 | case LTTNG_CONSUMER64_UST: | |
2036 | /* | |
2037 | * Close all metadata streams. The metadata hash table is passed and | |
2038 | * this call iterates over it by closing all wakeup fd. This is safe | |
2039 | * because at this point we are sure that the metadata producer is | |
2040 | * either dead or blocked. | |
2041 | */ | |
6d574024 | 2042 | lttng_ustconsumer_close_all_metadata(metadata_ht); |
d88aee68 DG |
2043 | break; |
2044 | default: | |
2045 | ERR("Unknown consumer_data type"); | |
2046 | assert(0); | |
2047 | } | |
2048 | } | |
2049 | ||
fb3a43a9 DG |
2050 | /* |
2051 | * Clean up a metadata stream and free its memory. | |
2052 | */ | |
e316aad5 DG |
2053 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
2054 | struct lttng_ht *ht) | |
fb3a43a9 | 2055 | { |
e316aad5 | 2056 | struct lttng_consumer_channel *free_chan = NULL; |
fb3a43a9 DG |
2057 | |
2058 | assert(stream); | |
2059 | /* | |
2060 | * This call should NEVER receive regular stream. It must always be | |
2061 | * metadata stream and this is crucial for data structure synchronization. | |
2062 | */ | |
2063 | assert(stream->metadata_flag); | |
2064 | ||
e316aad5 DG |
2065 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
2066 | ||
74251bb8 | 2067 | pthread_mutex_lock(&consumer_data.lock); |
fb549e7a | 2068 | pthread_mutex_lock(&stream->chan->lock); |
3dad2c0f | 2069 | pthread_mutex_lock(&stream->lock); |
081424af JG |
2070 | if (stream->chan->metadata_cache) { |
2071 | /* Only applicable to userspace consumers. */ | |
2072 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); | |
2073 | } | |
8994307f | 2074 | |
6d574024 DG |
2075 | /* Remove any reference to that stream. */ |
2076 | consumer_stream_delete(stream, ht); | |
ca22feea | 2077 | |
6d574024 DG |
2078 | /* Close down everything including the relayd if one. */ |
2079 | consumer_stream_close(stream); | |
2080 | /* Destroy tracer buffers of the stream. */ | |
2081 | consumer_stream_destroy_buffers(stream); | |
fb3a43a9 DG |
2082 | |
2083 | /* Atomically decrement channel refcount since other threads can use it. */ | |
f2ad556d | 2084 | if (!uatomic_sub_return(&stream->chan->refcount, 1) |
ffe60014 | 2085 | && !uatomic_read(&stream->chan->nb_init_stream_left)) { |
c30aaa51 | 2086 | /* Go for channel deletion! */ |
e316aad5 | 2087 | free_chan = stream->chan; |
fb3a43a9 DG |
2088 | } |
2089 | ||
73811ecc DG |
2090 | /* |
2091 | * Nullify the stream reference so it is not used after deletion. The | |
6d574024 DG |
2092 | * channel lock MUST be acquired before being able to check for a NULL |
2093 | * pointer value. | |
73811ecc DG |
2094 | */ |
2095 | stream->chan->metadata_stream = NULL; | |
2096 | ||
081424af JG |
2097 | if (stream->chan->metadata_cache) { |
2098 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); | |
2099 | } | |
3dad2c0f | 2100 | pthread_mutex_unlock(&stream->lock); |
fb549e7a | 2101 | pthread_mutex_unlock(&stream->chan->lock); |
74251bb8 | 2102 | pthread_mutex_unlock(&consumer_data.lock); |
e316aad5 DG |
2103 | |
2104 | if (free_chan) { | |
2105 | consumer_del_channel(free_chan); | |
2106 | } | |
2107 | ||
6d574024 | 2108 | consumer_stream_free(stream); |
fb3a43a9 DG |
2109 | } |
2110 | ||
2111 | /* | |
2112 | * Action done with the metadata stream when adding it to the consumer internal | |
2113 | * data structures to handle it. | |
2114 | */ | |
5ab66908 | 2115 | int consumer_add_metadata_stream(struct lttng_consumer_stream *stream) |
fb3a43a9 | 2116 | { |
5ab66908 | 2117 | struct lttng_ht *ht = metadata_ht; |
e316aad5 | 2118 | int ret = 0; |
76082088 | 2119 | struct lttng_ht_iter iter; |
d88aee68 | 2120 | struct lttng_ht_node_u64 *node; |
fb3a43a9 | 2121 | |
e316aad5 DG |
2122 | assert(stream); |
2123 | assert(ht); | |
2124 | ||
d88aee68 | 2125 | DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key); |
e316aad5 DG |
2126 | |
2127 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 2128 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 2129 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 2130 | pthread_mutex_lock(&stream->lock); |
e316aad5 | 2131 | |
e316aad5 DG |
2132 | /* |
2133 | * From here, refcounts are updated so be _careful_ when returning an error | |
2134 | * after this point. | |
2135 | */ | |
2136 | ||
fb3a43a9 | 2137 | rcu_read_lock(); |
76082088 DG |
2138 | |
2139 | /* | |
2140 | * Lookup the stream just to make sure it does not exist in our internal | |
2141 | * state. This should NEVER happen. | |
2142 | */ | |
d88aee68 DG |
2143 | lttng_ht_lookup(ht, &stream->key, &iter); |
2144 | node = lttng_ht_iter_get_node_u64(&iter); | |
76082088 DG |
2145 | assert(!node); |
2146 | ||
e316aad5 | 2147 | /* |
ffe60014 DG |
2148 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
2149 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
2150 | * causes the count to become 0 also causes a stream to be added. The |
2151 | * channel deletion will thus be triggered by the following removal of this | |
2152 | * stream. | |
2153 | */ | |
ffe60014 | 2154 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
2155 | /* Increment refcount before decrementing nb_init_stream_left */ |
2156 | cmm_smp_wmb(); | |
ffe60014 | 2157 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
2158 | } |
2159 | ||
d88aee68 | 2160 | lttng_ht_add_unique_u64(ht, &stream->node); |
ca22feea | 2161 | |
d8ef542d MD |
2162 | lttng_ht_add_unique_u64(consumer_data.stream_per_chan_id_ht, |
2163 | &stream->node_channel_id); | |
2164 | ||
ca22feea DG |
2165 | /* |
2166 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
2167 | * the key since the HT does not use it and we allow to add redundant keys | |
2168 | * into this table. | |
2169 | */ | |
d88aee68 | 2170 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 2171 | |
fb3a43a9 | 2172 | rcu_read_unlock(); |
e316aad5 | 2173 | |
2e818a6a | 2174 | pthread_mutex_unlock(&stream->lock); |
a9838785 | 2175 | pthread_mutex_unlock(&stream->chan->lock); |
ec6ea7d0 | 2176 | pthread_mutex_unlock(&stream->chan->timer_lock); |
e316aad5 DG |
2177 | pthread_mutex_unlock(&consumer_data.lock); |
2178 | return ret; | |
fb3a43a9 DG |
2179 | } |
2180 | ||
8994307f DG |
2181 | /* |
2182 | * Delete data stream that are flagged for deletion (endpoint_status). | |
2183 | */ | |
2184 | static void validate_endpoint_status_data_stream(void) | |
2185 | { | |
2186 | struct lttng_ht_iter iter; | |
2187 | struct lttng_consumer_stream *stream; | |
2188 | ||
2189 | DBG("Consumer delete flagged data stream"); | |
2190 | ||
2191 | rcu_read_lock(); | |
2192 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
2193 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2194 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2195 | continue; |
2196 | } | |
2197 | /* Delete it right now */ | |
2198 | consumer_del_stream(stream, data_ht); | |
2199 | } | |
2200 | rcu_read_unlock(); | |
2201 | } | |
2202 | ||
2203 | /* | |
2204 | * Delete metadata stream that are flagged for deletion (endpoint_status). | |
2205 | */ | |
2206 | static void validate_endpoint_status_metadata_stream( | |
2207 | struct lttng_poll_event *pollset) | |
2208 | { | |
2209 | struct lttng_ht_iter iter; | |
2210 | struct lttng_consumer_stream *stream; | |
2211 | ||
2212 | DBG("Consumer delete flagged metadata stream"); | |
2213 | ||
2214 | assert(pollset); | |
2215 | ||
2216 | rcu_read_lock(); | |
2217 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
2218 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2219 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2220 | continue; |
2221 | } | |
2222 | /* | |
2223 | * Remove from pollset so the metadata thread can continue without | |
2224 | * blocking on a deleted stream. | |
2225 | */ | |
2226 | lttng_poll_del(pollset, stream->wait_fd); | |
2227 | ||
2228 | /* Delete it right now */ | |
2229 | consumer_del_metadata_stream(stream, metadata_ht); | |
2230 | } | |
2231 | rcu_read_unlock(); | |
2232 | } | |
2233 | ||
fb3a43a9 DG |
2234 | /* |
2235 | * Thread polls on metadata file descriptor and write them on disk or on the | |
2236 | * network. | |
2237 | */ | |
7d980def | 2238 | void *consumer_thread_metadata_poll(void *data) |
fb3a43a9 | 2239 | { |
1fc79fb4 | 2240 | int ret, i, pollfd, err = -1; |
fb3a43a9 | 2241 | uint32_t revents, nb_fd; |
e316aad5 | 2242 | struct lttng_consumer_stream *stream = NULL; |
fb3a43a9 | 2243 | struct lttng_ht_iter iter; |
d88aee68 | 2244 | struct lttng_ht_node_u64 *node; |
fb3a43a9 DG |
2245 | struct lttng_poll_event events; |
2246 | struct lttng_consumer_local_data *ctx = data; | |
2247 | ssize_t len; | |
2248 | ||
2249 | rcu_register_thread(); | |
2250 | ||
1fc79fb4 MD |
2251 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA); |
2252 | ||
2d57de81 MD |
2253 | if (testpoint(consumerd_thread_metadata)) { |
2254 | goto error_testpoint; | |
2255 | } | |
2256 | ||
9ce5646a MD |
2257 | health_code_update(); |
2258 | ||
fb3a43a9 DG |
2259 | DBG("Thread metadata poll started"); |
2260 | ||
fb3a43a9 DG |
2261 | /* Size is set to 1 for the consumer_metadata pipe */ |
2262 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2263 | if (ret < 0) { | |
2264 | ERR("Poll set creation failed"); | |
d8ef542d | 2265 | goto end_poll; |
fb3a43a9 DG |
2266 | } |
2267 | ||
13886d2d DG |
2268 | ret = lttng_poll_add(&events, |
2269 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN); | |
fb3a43a9 DG |
2270 | if (ret < 0) { |
2271 | goto end; | |
2272 | } | |
2273 | ||
2274 | /* Main loop */ | |
2275 | DBG("Metadata main loop started"); | |
2276 | ||
2277 | while (1) { | |
fb3a43a9 | 2278 | restart: |
7fa2082e | 2279 | health_code_update(); |
9ce5646a | 2280 | health_poll_entry(); |
7fa2082e | 2281 | DBG("Metadata poll wait"); |
fb3a43a9 | 2282 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2283 | DBG("Metadata poll return from wait with %d fd(s)", |
2284 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2285 | health_poll_exit(); |
40063ead | 2286 | DBG("Metadata event caught in thread"); |
fb3a43a9 DG |
2287 | if (ret < 0) { |
2288 | if (errno == EINTR) { | |
40063ead | 2289 | ERR("Poll EINTR caught"); |
fb3a43a9 DG |
2290 | goto restart; |
2291 | } | |
d9607cd7 MD |
2292 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2293 | err = 0; /* All is OK */ | |
2294 | } | |
2295 | goto end; | |
fb3a43a9 DG |
2296 | } |
2297 | ||
0d9c5d77 DG |
2298 | nb_fd = ret; |
2299 | ||
e316aad5 | 2300 | /* From here, the event is a metadata wait fd */ |
fb3a43a9 | 2301 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2302 | health_code_update(); |
2303 | ||
fb3a43a9 DG |
2304 | revents = LTTNG_POLL_GETEV(&events, i); |
2305 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2306 | ||
fd20dac9 MD |
2307 | if (!revents) { |
2308 | /* No activity for this FD (poll implementation). */ | |
2309 | continue; | |
2310 | } | |
2311 | ||
13886d2d | 2312 | if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) { |
03e43155 | 2313 | if (revents & LPOLLIN) { |
13886d2d DG |
2314 | ssize_t pipe_len; |
2315 | ||
2316 | pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe, | |
2317 | &stream, sizeof(stream)); | |
6cd525e8 | 2318 | if (pipe_len < sizeof(stream)) { |
03e43155 MD |
2319 | if (pipe_len < 0) { |
2320 | PERROR("read metadata stream"); | |
2321 | } | |
fb3a43a9 | 2322 | /* |
03e43155 MD |
2323 | * Remove the pipe from the poll set and continue the loop |
2324 | * since their might be data to consume. | |
fb3a43a9 | 2325 | */ |
03e43155 MD |
2326 | lttng_poll_del(&events, |
2327 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2328 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
fb3a43a9 DG |
2329 | continue; |
2330 | } | |
2331 | ||
8994307f DG |
2332 | /* A NULL stream means that the state has changed. */ |
2333 | if (stream == NULL) { | |
2334 | /* Check for deleted streams. */ | |
2335 | validate_endpoint_status_metadata_stream(&events); | |
3714380f | 2336 | goto restart; |
8994307f DG |
2337 | } |
2338 | ||
fb3a43a9 DG |
2339 | DBG("Adding metadata stream %d to poll set", |
2340 | stream->wait_fd); | |
2341 | ||
fb3a43a9 DG |
2342 | /* Add metadata stream to the global poll events list */ |
2343 | lttng_poll_add(&events, stream->wait_fd, | |
6d574024 | 2344 | LPOLLIN | LPOLLPRI | LPOLLHUP); |
03e43155 MD |
2345 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
2346 | DBG("Metadata thread pipe hung up"); | |
2347 | /* | |
2348 | * Remove the pipe from the poll set and continue the loop | |
2349 | * since their might be data to consume. | |
2350 | */ | |
2351 | lttng_poll_del(&events, | |
2352 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2353 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
2354 | continue; | |
2355 | } else { | |
2356 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2357 | goto end; | |
fb3a43a9 DG |
2358 | } |
2359 | ||
e316aad5 | 2360 | /* Handle other stream */ |
fb3a43a9 DG |
2361 | continue; |
2362 | } | |
2363 | ||
d09e1200 | 2364 | rcu_read_lock(); |
d88aee68 DG |
2365 | { |
2366 | uint64_t tmp_id = (uint64_t) pollfd; | |
2367 | ||
2368 | lttng_ht_lookup(metadata_ht, &tmp_id, &iter); | |
2369 | } | |
2370 | node = lttng_ht_iter_get_node_u64(&iter); | |
e316aad5 | 2371 | assert(node); |
fb3a43a9 DG |
2372 | |
2373 | stream = caa_container_of(node, struct lttng_consumer_stream, | |
58b1f425 | 2374 | node); |
fb3a43a9 | 2375 | |
03e43155 MD |
2376 | if (revents & (LPOLLIN | LPOLLPRI)) { |
2377 | /* Get the data out of the metadata file descriptor */ | |
2378 | DBG("Metadata available on fd %d", pollfd); | |
2379 | assert(stream->wait_fd == pollfd); | |
2380 | ||
2381 | do { | |
2382 | health_code_update(); | |
2383 | ||
2384 | len = ctx->on_buffer_ready(stream, ctx); | |
2385 | /* | |
2386 | * We don't check the return value here since if we get | |
83f4233d | 2387 | * a negative len, it means an error occurred thus we |
03e43155 MD |
2388 | * simply remove it from the poll set and free the |
2389 | * stream. | |
2390 | */ | |
2391 | } while (len > 0); | |
2392 | ||
2393 | /* It's ok to have an unavailable sub-buffer */ | |
2394 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { | |
2395 | /* Clean up stream from consumer and free it. */ | |
2396 | lttng_poll_del(&events, stream->wait_fd); | |
2397 | consumer_del_metadata_stream(stream, metadata_ht); | |
2398 | } | |
2399 | } else if (revents & (LPOLLERR | LPOLLHUP)) { | |
e316aad5 | 2400 | DBG("Metadata fd %d is hup|err.", pollfd); |
fb3a43a9 DG |
2401 | if (!stream->hangup_flush_done |
2402 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2403 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2404 | DBG("Attempting to flush and consume the UST buffers"); | |
2405 | lttng_ustconsumer_on_stream_hangup(stream); | |
2406 | ||
2407 | /* We just flushed the stream now read it. */ | |
4bb94b75 | 2408 | do { |
9ce5646a MD |
2409 | health_code_update(); |
2410 | ||
4bb94b75 DG |
2411 | len = ctx->on_buffer_ready(stream, ctx); |
2412 | /* | |
2413 | * We don't check the return value here since if we get | |
83f4233d | 2414 | * a negative len, it means an error occurred thus we |
4bb94b75 DG |
2415 | * simply remove it from the poll set and free the |
2416 | * stream. | |
2417 | */ | |
2418 | } while (len > 0); | |
fb3a43a9 DG |
2419 | } |
2420 | ||
fb3a43a9 | 2421 | lttng_poll_del(&events, stream->wait_fd); |
e316aad5 DG |
2422 | /* |
2423 | * This call update the channel states, closes file descriptors | |
2424 | * and securely free the stream. | |
2425 | */ | |
2426 | consumer_del_metadata_stream(stream, metadata_ht); | |
03e43155 MD |
2427 | } else { |
2428 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
6f2f1a70 | 2429 | rcu_read_unlock(); |
03e43155 | 2430 | goto end; |
fb3a43a9 | 2431 | } |
e316aad5 | 2432 | /* Release RCU lock for the stream looked up */ |
d09e1200 | 2433 | rcu_read_unlock(); |
fb3a43a9 DG |
2434 | } |
2435 | } | |
2436 | ||
1fc79fb4 MD |
2437 | /* All is OK */ |
2438 | err = 0; | |
fb3a43a9 DG |
2439 | end: |
2440 | DBG("Metadata poll thread exiting"); | |
fb3a43a9 | 2441 | |
d8ef542d MD |
2442 | lttng_poll_clean(&events); |
2443 | end_poll: | |
2d57de81 | 2444 | error_testpoint: |
1fc79fb4 MD |
2445 | if (err) { |
2446 | health_error(); | |
2447 | ERR("Health error occurred in %s", __func__); | |
2448 | } | |
2449 | health_unregister(health_consumerd); | |
fb3a43a9 DG |
2450 | rcu_unregister_thread(); |
2451 | return NULL; | |
2452 | } | |
2453 | ||
3bd1e081 | 2454 | /* |
e4421fec | 2455 | * This thread polls the fds in the set to consume the data and write |
3bd1e081 MD |
2456 | * it to tracefile if necessary. |
2457 | */ | |
7d980def | 2458 | void *consumer_thread_data_poll(void *data) |
3bd1e081 | 2459 | { |
1fc79fb4 | 2460 | int num_rdy, num_hup, high_prio, ret, i, err = -1; |
3bd1e081 MD |
2461 | struct pollfd *pollfd = NULL; |
2462 | /* local view of the streams */ | |
c869f647 | 2463 | struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL; |
3bd1e081 MD |
2464 | /* local view of consumer_data.fds_count */ |
2465 | int nb_fd = 0; | |
2afa177e JD |
2466 | /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */ |
2467 | int nb_inactive_fd = 0; | |
3bd1e081 | 2468 | struct lttng_consumer_local_data *ctx = data; |
00e2e675 | 2469 | ssize_t len; |
3bd1e081 | 2470 | |
e7b994a3 DG |
2471 | rcu_register_thread(); |
2472 | ||
1fc79fb4 MD |
2473 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA); |
2474 | ||
2d57de81 MD |
2475 | if (testpoint(consumerd_thread_data)) { |
2476 | goto error_testpoint; | |
2477 | } | |
2478 | ||
9ce5646a MD |
2479 | health_code_update(); |
2480 | ||
4df6c8cb MD |
2481 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream *)); |
2482 | if (local_stream == NULL) { | |
2483 | PERROR("local_stream malloc"); | |
2484 | goto end; | |
2485 | } | |
3bd1e081 MD |
2486 | |
2487 | while (1) { | |
9ce5646a MD |
2488 | health_code_update(); |
2489 | ||
3bd1e081 MD |
2490 | high_prio = 0; |
2491 | num_hup = 0; | |
2492 | ||
2493 | /* | |
e4421fec | 2494 | * the fds set has been updated, we need to update our |
3bd1e081 MD |
2495 | * local array as well |
2496 | */ | |
2497 | pthread_mutex_lock(&consumer_data.lock); | |
2498 | if (consumer_data.need_update) { | |
0e428499 DG |
2499 | free(pollfd); |
2500 | pollfd = NULL; | |
2501 | ||
2502 | free(local_stream); | |
2503 | local_stream = NULL; | |
3bd1e081 | 2504 | |
02b3d176 DG |
2505 | /* |
2506 | * Allocate for all fds +1 for the consumer_data_pipe and +1 for | |
2507 | * wake up pipe. | |
2508 | */ | |
2509 | pollfd = zmalloc((consumer_data.stream_count + 2) * sizeof(struct pollfd)); | |
3bd1e081 | 2510 | if (pollfd == NULL) { |
7a57cf92 | 2511 | PERROR("pollfd malloc"); |
3bd1e081 MD |
2512 | pthread_mutex_unlock(&consumer_data.lock); |
2513 | goto end; | |
2514 | } | |
2515 | ||
02b3d176 | 2516 | local_stream = zmalloc((consumer_data.stream_count + 2) * |
747f8642 | 2517 | sizeof(struct lttng_consumer_stream *)); |
3bd1e081 | 2518 | if (local_stream == NULL) { |
7a57cf92 | 2519 | PERROR("local_stream malloc"); |
3bd1e081 MD |
2520 | pthread_mutex_unlock(&consumer_data.lock); |
2521 | goto end; | |
2522 | } | |
ffe60014 | 2523 | ret = update_poll_array(ctx, &pollfd, local_stream, |
2afa177e | 2524 | data_ht, &nb_inactive_fd); |
3bd1e081 MD |
2525 | if (ret < 0) { |
2526 | ERR("Error in allocating pollfd or local_outfds"); | |
f73fabfd | 2527 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2528 | pthread_mutex_unlock(&consumer_data.lock); |
2529 | goto end; | |
2530 | } | |
2531 | nb_fd = ret; | |
2532 | consumer_data.need_update = 0; | |
2533 | } | |
2534 | pthread_mutex_unlock(&consumer_data.lock); | |
2535 | ||
4078b776 | 2536 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
2afa177e JD |
2537 | if (nb_fd == 0 && nb_inactive_fd == 0 && |
2538 | CMM_LOAD_SHARED(consumer_quit) == 1) { | |
1fc79fb4 | 2539 | err = 0; /* All is OK */ |
4078b776 MD |
2540 | goto end; |
2541 | } | |
3bd1e081 | 2542 | /* poll on the array of fds */ |
88f2b785 | 2543 | restart: |
02b3d176 | 2544 | DBG("polling on %d fd", nb_fd + 2); |
cf0bcb51 JG |
2545 | if (testpoint(consumerd_thread_data_poll)) { |
2546 | goto end; | |
2547 | } | |
9ce5646a | 2548 | health_poll_entry(); |
02b3d176 | 2549 | num_rdy = poll(pollfd, nb_fd + 2, -1); |
9ce5646a | 2550 | health_poll_exit(); |
3bd1e081 MD |
2551 | DBG("poll num_rdy : %d", num_rdy); |
2552 | if (num_rdy == -1) { | |
88f2b785 MD |
2553 | /* |
2554 | * Restart interrupted system call. | |
2555 | */ | |
2556 | if (errno == EINTR) { | |
2557 | goto restart; | |
2558 | } | |
7a57cf92 | 2559 | PERROR("Poll error"); |
f73fabfd | 2560 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2561 | goto end; |
2562 | } else if (num_rdy == 0) { | |
2563 | DBG("Polling thread timed out"); | |
2564 | goto end; | |
2565 | } | |
2566 | ||
19246bd6 JG |
2567 | if (caa_unlikely(data_consumption_paused)) { |
2568 | DBG("Data consumption paused, sleeping..."); | |
2569 | sleep(1); | |
2570 | goto restart; | |
2571 | } | |
2572 | ||
3bd1e081 | 2573 | /* |
50f8ae69 | 2574 | * If the consumer_data_pipe triggered poll go directly to the |
00e2e675 DG |
2575 | * beginning of the loop to update the array. We want to prioritize |
2576 | * array update over low-priority reads. | |
3bd1e081 | 2577 | */ |
509bb1cf | 2578 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
ab30f567 | 2579 | ssize_t pipe_readlen; |
04fdd819 | 2580 | |
50f8ae69 | 2581 | DBG("consumer_data_pipe wake up"); |
acdb9057 DG |
2582 | pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe, |
2583 | &new_stream, sizeof(new_stream)); | |
6cd525e8 MD |
2584 | if (pipe_readlen < sizeof(new_stream)) { |
2585 | PERROR("Consumer data pipe"); | |
23f5f35d DG |
2586 | /* Continue so we can at least handle the current stream(s). */ |
2587 | continue; | |
2588 | } | |
c869f647 DG |
2589 | |
2590 | /* | |
2591 | * If the stream is NULL, just ignore it. It's also possible that | |
2592 | * the sessiond poll thread changed the consumer_quit state and is | |
2593 | * waking us up to test it. | |
2594 | */ | |
2595 | if (new_stream == NULL) { | |
8994307f | 2596 | validate_endpoint_status_data_stream(); |
c869f647 DG |
2597 | continue; |
2598 | } | |
2599 | ||
c869f647 | 2600 | /* Continue to update the local streams and handle prio ones */ |
3bd1e081 MD |
2601 | continue; |
2602 | } | |
2603 | ||
02b3d176 DG |
2604 | /* Handle wakeup pipe. */ |
2605 | if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) { | |
2606 | char dummy; | |
2607 | ssize_t pipe_readlen; | |
2608 | ||
2609 | pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy, | |
2610 | sizeof(dummy)); | |
2611 | if (pipe_readlen < 0) { | |
2612 | PERROR("Consumer data wakeup pipe"); | |
2613 | } | |
2614 | /* We've been awakened to handle stream(s). */ | |
2615 | ctx->has_wakeup = 0; | |
2616 | } | |
2617 | ||
3bd1e081 MD |
2618 | /* Take care of high priority channels first. */ |
2619 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2620 | health_code_update(); |
2621 | ||
9617607b DG |
2622 | if (local_stream[i] == NULL) { |
2623 | continue; | |
2624 | } | |
fb3a43a9 | 2625 | if (pollfd[i].revents & POLLPRI) { |
d41f73b7 MD |
2626 | DBG("Urgent read on fd %d", pollfd[i].fd); |
2627 | high_prio = 1; | |
4078b776 | 2628 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
d41f73b7 | 2629 | /* it's ok to have an unavailable sub-buffer */ |
b64403e3 | 2630 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2631 | /* Clean the stream and free it. */ |
2632 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2633 | local_stream[i] = NULL; |
4078b776 MD |
2634 | } else if (len > 0) { |
2635 | local_stream[i]->data_read = 1; | |
d41f73b7 | 2636 | } |
3bd1e081 MD |
2637 | } |
2638 | } | |
2639 | ||
4078b776 MD |
2640 | /* |
2641 | * If we read high prio channel in this loop, try again | |
2642 | * for more high prio data. | |
2643 | */ | |
2644 | if (high_prio) { | |
3bd1e081 MD |
2645 | continue; |
2646 | } | |
2647 | ||
2648 | /* Take care of low priority channels. */ | |
4078b776 | 2649 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2650 | health_code_update(); |
2651 | ||
9617607b DG |
2652 | if (local_stream[i] == NULL) { |
2653 | continue; | |
2654 | } | |
4078b776 | 2655 | if ((pollfd[i].revents & POLLIN) || |
02b3d176 DG |
2656 | local_stream[i]->hangup_flush_done || |
2657 | local_stream[i]->has_data) { | |
4078b776 MD |
2658 | DBG("Normal read on fd %d", pollfd[i].fd); |
2659 | len = ctx->on_buffer_ready(local_stream[i], ctx); | |
2660 | /* it's ok to have an unavailable sub-buffer */ | |
b64403e3 | 2661 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2662 | /* Clean the stream and free it. */ |
2663 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2664 | local_stream[i] = NULL; |
4078b776 MD |
2665 | } else if (len > 0) { |
2666 | local_stream[i]->data_read = 1; | |
2667 | } | |
2668 | } | |
2669 | } | |
2670 | ||
2671 | /* Handle hangup and errors */ | |
2672 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2673 | health_code_update(); |
2674 | ||
9617607b DG |
2675 | if (local_stream[i] == NULL) { |
2676 | continue; | |
2677 | } | |
4078b776 MD |
2678 | if (!local_stream[i]->hangup_flush_done |
2679 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) | |
2680 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2681 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2682 | DBG("fd %d is hup|err|nval. Attempting flush and read.", | |
9617607b | 2683 | pollfd[i].fd); |
4078b776 MD |
2684 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
2685 | /* Attempt read again, for the data we just flushed. */ | |
2686 | local_stream[i]->data_read = 1; | |
2687 | } | |
2688 | /* | |
2689 | * If the poll flag is HUP/ERR/NVAL and we have | |
2690 | * read no data in this pass, we can remove the | |
2691 | * stream from its hash table. | |
2692 | */ | |
2693 | if ((pollfd[i].revents & POLLHUP)) { | |
2694 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); | |
2695 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2696 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2697 | local_stream[i] = NULL; |
4078b776 MD |
2698 | num_hup++; |
2699 | } | |
2700 | } else if (pollfd[i].revents & POLLERR) { | |
2701 | ERR("Error returned in polling fd %d.", pollfd[i].fd); | |
2702 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2703 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2704 | local_stream[i] = NULL; |
4078b776 MD |
2705 | num_hup++; |
2706 | } | |
2707 | } else if (pollfd[i].revents & POLLNVAL) { | |
2708 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); | |
2709 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2710 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2711 | local_stream[i] = NULL; |
4078b776 | 2712 | num_hup++; |
3bd1e081 MD |
2713 | } |
2714 | } | |
9617607b DG |
2715 | if (local_stream[i] != NULL) { |
2716 | local_stream[i]->data_read = 0; | |
2717 | } | |
3bd1e081 MD |
2718 | } |
2719 | } | |
1fc79fb4 MD |
2720 | /* All is OK */ |
2721 | err = 0; | |
3bd1e081 MD |
2722 | end: |
2723 | DBG("polling thread exiting"); | |
0e428499 DG |
2724 | free(pollfd); |
2725 | free(local_stream); | |
fb3a43a9 DG |
2726 | |
2727 | /* | |
2728 | * Close the write side of the pipe so epoll_wait() in | |
7d980def DG |
2729 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
2730 | * read side of the pipe. If we close them both, epoll_wait strangely does | |
2731 | * not return and could create a endless wait period if the pipe is the | |
2732 | * only tracked fd in the poll set. The thread will take care of closing | |
2733 | * the read side. | |
fb3a43a9 | 2734 | */ |
13886d2d | 2735 | (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe); |
fb3a43a9 | 2736 | |
2d57de81 | 2737 | error_testpoint: |
1fc79fb4 MD |
2738 | if (err) { |
2739 | health_error(); | |
2740 | ERR("Health error occurred in %s", __func__); | |
2741 | } | |
2742 | health_unregister(health_consumerd); | |
2743 | ||
e7b994a3 | 2744 | rcu_unregister_thread(); |
3bd1e081 MD |
2745 | return NULL; |
2746 | } | |
2747 | ||
d8ef542d MD |
2748 | /* |
2749 | * Close wake-up end of each stream belonging to the channel. This will | |
2750 | * allow the poll() on the stream read-side to detect when the | |
2751 | * write-side (application) finally closes them. | |
2752 | */ | |
2753 | static | |
2754 | void consumer_close_channel_streams(struct lttng_consumer_channel *channel) | |
2755 | { | |
2756 | struct lttng_ht *ht; | |
2757 | struct lttng_consumer_stream *stream; | |
2758 | struct lttng_ht_iter iter; | |
2759 | ||
2760 | ht = consumer_data.stream_per_chan_id_ht; | |
2761 | ||
2762 | rcu_read_lock(); | |
2763 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
2764 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
2765 | ht->match_fct, &channel->key, | |
2766 | &iter.iter, stream, node_channel_id.node) { | |
f2ad556d MD |
2767 | /* |
2768 | * Protect against teardown with mutex. | |
2769 | */ | |
2770 | pthread_mutex_lock(&stream->lock); | |
2771 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
2772 | goto next; | |
2773 | } | |
d8ef542d MD |
2774 | switch (consumer_data.type) { |
2775 | case LTTNG_CONSUMER_KERNEL: | |
2776 | break; | |
2777 | case LTTNG_CONSUMER32_UST: | |
2778 | case LTTNG_CONSUMER64_UST: | |
b4a650f3 DG |
2779 | if (stream->metadata_flag) { |
2780 | /* Safe and protected by the stream lock. */ | |
2781 | lttng_ustconsumer_close_metadata(stream->chan); | |
2782 | } else { | |
2783 | /* | |
2784 | * Note: a mutex is taken internally within | |
2785 | * liblttng-ust-ctl to protect timer wakeup_fd | |
2786 | * use from concurrent close. | |
2787 | */ | |
2788 | lttng_ustconsumer_close_stream_wakeup(stream); | |
2789 | } | |
d8ef542d MD |
2790 | break; |
2791 | default: | |
2792 | ERR("Unknown consumer_data type"); | |
2793 | assert(0); | |
2794 | } | |
f2ad556d MD |
2795 | next: |
2796 | pthread_mutex_unlock(&stream->lock); | |
d8ef542d MD |
2797 | } |
2798 | rcu_read_unlock(); | |
2799 | } | |
2800 | ||
2801 | static void destroy_channel_ht(struct lttng_ht *ht) | |
2802 | { | |
2803 | struct lttng_ht_iter iter; | |
2804 | struct lttng_consumer_channel *channel; | |
2805 | int ret; | |
2806 | ||
2807 | if (ht == NULL) { | |
2808 | return; | |
2809 | } | |
2810 | ||
2811 | rcu_read_lock(); | |
2812 | cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) { | |
2813 | ret = lttng_ht_del(ht, &iter); | |
2814 | assert(ret != 0); | |
2815 | } | |
2816 | rcu_read_unlock(); | |
2817 | ||
2818 | lttng_ht_destroy(ht); | |
2819 | } | |
2820 | ||
2821 | /* | |
2822 | * This thread polls the channel fds to detect when they are being | |
2823 | * closed. It closes all related streams if the channel is detected as | |
2824 | * closed. It is currently only used as a shim layer for UST because the | |
2825 | * consumerd needs to keep the per-stream wakeup end of pipes open for | |
2826 | * periodical flush. | |
2827 | */ | |
2828 | void *consumer_thread_channel_poll(void *data) | |
2829 | { | |
1fc79fb4 | 2830 | int ret, i, pollfd, err = -1; |
d8ef542d MD |
2831 | uint32_t revents, nb_fd; |
2832 | struct lttng_consumer_channel *chan = NULL; | |
2833 | struct lttng_ht_iter iter; | |
2834 | struct lttng_ht_node_u64 *node; | |
2835 | struct lttng_poll_event events; | |
2836 | struct lttng_consumer_local_data *ctx = data; | |
2837 | struct lttng_ht *channel_ht; | |
2838 | ||
2839 | rcu_register_thread(); | |
2840 | ||
1fc79fb4 MD |
2841 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL); |
2842 | ||
2d57de81 MD |
2843 | if (testpoint(consumerd_thread_channel)) { |
2844 | goto error_testpoint; | |
2845 | } | |
2846 | ||
9ce5646a MD |
2847 | health_code_update(); |
2848 | ||
d8ef542d MD |
2849 | channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2850 | if (!channel_ht) { | |
2851 | /* ENOMEM at this point. Better to bail out. */ | |
2852 | goto end_ht; | |
2853 | } | |
2854 | ||
2855 | DBG("Thread channel poll started"); | |
2856 | ||
2857 | /* Size is set to 1 for the consumer_channel pipe */ | |
2858 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2859 | if (ret < 0) { | |
2860 | ERR("Poll set creation failed"); | |
2861 | goto end_poll; | |
2862 | } | |
2863 | ||
2864 | ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN); | |
2865 | if (ret < 0) { | |
2866 | goto end; | |
2867 | } | |
2868 | ||
2869 | /* Main loop */ | |
2870 | DBG("Channel main loop started"); | |
2871 | ||
2872 | while (1) { | |
d8ef542d | 2873 | restart: |
7fa2082e MD |
2874 | health_code_update(); |
2875 | DBG("Channel poll wait"); | |
9ce5646a | 2876 | health_poll_entry(); |
d8ef542d | 2877 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2878 | DBG("Channel poll return from wait with %d fd(s)", |
2879 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2880 | health_poll_exit(); |
40063ead | 2881 | DBG("Channel event caught in thread"); |
d8ef542d MD |
2882 | if (ret < 0) { |
2883 | if (errno == EINTR) { | |
40063ead | 2884 | ERR("Poll EINTR caught"); |
d8ef542d MD |
2885 | goto restart; |
2886 | } | |
d9607cd7 MD |
2887 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2888 | err = 0; /* All is OK */ | |
2889 | } | |
d8ef542d MD |
2890 | goto end; |
2891 | } | |
2892 | ||
2893 | nb_fd = ret; | |
2894 | ||
2895 | /* From here, the event is a channel wait fd */ | |
2896 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2897 | health_code_update(); |
2898 | ||
d8ef542d MD |
2899 | revents = LTTNG_POLL_GETEV(&events, i); |
2900 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2901 | ||
d8ef542d | 2902 | if (!revents) { |
fd20dac9 | 2903 | /* No activity for this FD (poll implementation). */ |
d8ef542d MD |
2904 | continue; |
2905 | } | |
fd20dac9 | 2906 | |
d8ef542d | 2907 | if (pollfd == ctx->consumer_channel_pipe[0]) { |
03e43155 | 2908 | if (revents & LPOLLIN) { |
d8ef542d | 2909 | enum consumer_channel_action action; |
a0cbdd2e | 2910 | uint64_t key; |
d8ef542d | 2911 | |
a0cbdd2e | 2912 | ret = read_channel_pipe(ctx, &chan, &key, &action); |
d8ef542d | 2913 | if (ret <= 0) { |
03e43155 MD |
2914 | if (ret < 0) { |
2915 | ERR("Error reading channel pipe"); | |
2916 | } | |
2917 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
d8ef542d MD |
2918 | continue; |
2919 | } | |
2920 | ||
2921 | switch (action) { | |
2922 | case CONSUMER_CHANNEL_ADD: | |
2923 | DBG("Adding channel %d to poll set", | |
2924 | chan->wait_fd); | |
2925 | ||
2926 | lttng_ht_node_init_u64(&chan->wait_fd_node, | |
2927 | chan->wait_fd); | |
c7260a81 | 2928 | rcu_read_lock(); |
d8ef542d MD |
2929 | lttng_ht_add_unique_u64(channel_ht, |
2930 | &chan->wait_fd_node); | |
c7260a81 | 2931 | rcu_read_unlock(); |
d8ef542d MD |
2932 | /* Add channel to the global poll events list */ |
2933 | lttng_poll_add(&events, chan->wait_fd, | |
03e43155 | 2934 | LPOLLERR | LPOLLHUP); |
d8ef542d | 2935 | break; |
a0cbdd2e MD |
2936 | case CONSUMER_CHANNEL_DEL: |
2937 | { | |
b4a650f3 DG |
2938 | /* |
2939 | * This command should never be called if the channel | |
2940 | * has streams monitored by either the data or metadata | |
2941 | * thread. The consumer only notify this thread with a | |
2942 | * channel del. command if it receives a destroy | |
2943 | * channel command from the session daemon that send it | |
2944 | * if a command prior to the GET_CHANNEL failed. | |
2945 | */ | |
2946 | ||
c7260a81 | 2947 | rcu_read_lock(); |
a0cbdd2e MD |
2948 | chan = consumer_find_channel(key); |
2949 | if (!chan) { | |
c7260a81 | 2950 | rcu_read_unlock(); |
a0cbdd2e MD |
2951 | ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key); |
2952 | break; | |
2953 | } | |
2954 | lttng_poll_del(&events, chan->wait_fd); | |
f623cc0b | 2955 | iter.iter.node = &chan->wait_fd_node.node; |
a0cbdd2e MD |
2956 | ret = lttng_ht_del(channel_ht, &iter); |
2957 | assert(ret == 0); | |
a0cbdd2e | 2958 | |
f2a444f1 DG |
2959 | switch (consumer_data.type) { |
2960 | case LTTNG_CONSUMER_KERNEL: | |
2961 | break; | |
2962 | case LTTNG_CONSUMER32_UST: | |
2963 | case LTTNG_CONSUMER64_UST: | |
212d67a2 DG |
2964 | health_code_update(); |
2965 | /* Destroy streams that might have been left in the stream list. */ | |
2966 | clean_channel_stream_list(chan); | |
f2a444f1 DG |
2967 | break; |
2968 | default: | |
2969 | ERR("Unknown consumer_data type"); | |
2970 | assert(0); | |
2971 | } | |
2972 | ||
a0cbdd2e MD |
2973 | /* |
2974 | * Release our own refcount. Force channel deletion even if | |
2975 | * streams were not initialized. | |
2976 | */ | |
2977 | if (!uatomic_sub_return(&chan->refcount, 1)) { | |
2978 | consumer_del_channel(chan); | |
2979 | } | |
c7260a81 | 2980 | rcu_read_unlock(); |
a0cbdd2e MD |
2981 | goto restart; |
2982 | } | |
d8ef542d MD |
2983 | case CONSUMER_CHANNEL_QUIT: |
2984 | /* | |
2985 | * Remove the pipe from the poll set and continue the loop | |
2986 | * since their might be data to consume. | |
2987 | */ | |
2988 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
2989 | continue; | |
2990 | default: | |
2991 | ERR("Unknown action"); | |
2992 | break; | |
2993 | } | |
03e43155 MD |
2994 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
2995 | DBG("Channel thread pipe hung up"); | |
2996 | /* | |
2997 | * Remove the pipe from the poll set and continue the loop | |
2998 | * since their might be data to consume. | |
2999 | */ | |
3000 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
3001 | continue; | |
3002 | } else { | |
3003 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3004 | goto end; | |
d8ef542d MD |
3005 | } |
3006 | ||
3007 | /* Handle other stream */ | |
3008 | continue; | |
3009 | } | |
3010 | ||
3011 | rcu_read_lock(); | |
3012 | { | |
3013 | uint64_t tmp_id = (uint64_t) pollfd; | |
3014 | ||
3015 | lttng_ht_lookup(channel_ht, &tmp_id, &iter); | |
3016 | } | |
3017 | node = lttng_ht_iter_get_node_u64(&iter); | |
3018 | assert(node); | |
3019 | ||
3020 | chan = caa_container_of(node, struct lttng_consumer_channel, | |
3021 | wait_fd_node); | |
3022 | ||
3023 | /* Check for error event */ | |
3024 | if (revents & (LPOLLERR | LPOLLHUP)) { | |
3025 | DBG("Channel fd %d is hup|err.", pollfd); | |
3026 | ||
3027 | lttng_poll_del(&events, chan->wait_fd); | |
3028 | ret = lttng_ht_del(channel_ht, &iter); | |
3029 | assert(ret == 0); | |
b4a650f3 DG |
3030 | |
3031 | /* | |
3032 | * This will close the wait fd for each stream associated to | |
3033 | * this channel AND monitored by the data/metadata thread thus | |
3034 | * will be clean by the right thread. | |
3035 | */ | |
d8ef542d | 3036 | consumer_close_channel_streams(chan); |
f2ad556d MD |
3037 | |
3038 | /* Release our own refcount */ | |
3039 | if (!uatomic_sub_return(&chan->refcount, 1) | |
3040 | && !uatomic_read(&chan->nb_init_stream_left)) { | |
3041 | consumer_del_channel(chan); | |
3042 | } | |
03e43155 MD |
3043 | } else { |
3044 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3045 | rcu_read_unlock(); | |
3046 | goto end; | |
d8ef542d MD |
3047 | } |
3048 | ||
3049 | /* Release RCU lock for the channel looked up */ | |
3050 | rcu_read_unlock(); | |
3051 | } | |
3052 | } | |
3053 | ||
1fc79fb4 MD |
3054 | /* All is OK */ |
3055 | err = 0; | |
d8ef542d MD |
3056 | end: |
3057 | lttng_poll_clean(&events); | |
3058 | end_poll: | |
3059 | destroy_channel_ht(channel_ht); | |
3060 | end_ht: | |
2d57de81 | 3061 | error_testpoint: |
d8ef542d | 3062 | DBG("Channel poll thread exiting"); |
1fc79fb4 MD |
3063 | if (err) { |
3064 | health_error(); | |
3065 | ERR("Health error occurred in %s", __func__); | |
3066 | } | |
3067 | health_unregister(health_consumerd); | |
d8ef542d MD |
3068 | rcu_unregister_thread(); |
3069 | return NULL; | |
3070 | } | |
3071 | ||
331744e3 JD |
3072 | static int set_metadata_socket(struct lttng_consumer_local_data *ctx, |
3073 | struct pollfd *sockpoll, int client_socket) | |
3074 | { | |
3075 | int ret; | |
3076 | ||
3077 | assert(ctx); | |
3078 | assert(sockpoll); | |
3079 | ||
84382d49 MD |
3080 | ret = lttng_consumer_poll_socket(sockpoll); |
3081 | if (ret) { | |
331744e3 JD |
3082 | goto error; |
3083 | } | |
3084 | DBG("Metadata connection on client_socket"); | |
3085 | ||
3086 | /* Blocking call, waiting for transmission */ | |
3087 | ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket); | |
3088 | if (ctx->consumer_metadata_socket < 0) { | |
3089 | WARN("On accept metadata"); | |
3090 | ret = -1; | |
3091 | goto error; | |
3092 | } | |
3093 | ret = 0; | |
3094 | ||
3095 | error: | |
3096 | return ret; | |
3097 | } | |
3098 | ||
3bd1e081 MD |
3099 | /* |
3100 | * This thread listens on the consumerd socket and receives the file | |
3101 | * descriptors from the session daemon. | |
3102 | */ | |
7d980def | 3103 | void *consumer_thread_sessiond_poll(void *data) |
3bd1e081 | 3104 | { |
1fc79fb4 | 3105 | int sock = -1, client_socket, ret, err = -1; |
3bd1e081 MD |
3106 | /* |
3107 | * structure to poll for incoming data on communication socket avoids | |
3108 | * making blocking sockets. | |
3109 | */ | |
3110 | struct pollfd consumer_sockpoll[2]; | |
3111 | struct lttng_consumer_local_data *ctx = data; | |
3112 | ||
e7b994a3 DG |
3113 | rcu_register_thread(); |
3114 | ||
1fc79fb4 MD |
3115 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND); |
3116 | ||
2d57de81 MD |
3117 | if (testpoint(consumerd_thread_sessiond)) { |
3118 | goto error_testpoint; | |
3119 | } | |
3120 | ||
9ce5646a MD |
3121 | health_code_update(); |
3122 | ||
3bd1e081 MD |
3123 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
3124 | unlink(ctx->consumer_command_sock_path); | |
3125 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); | |
3126 | if (client_socket < 0) { | |
3127 | ERR("Cannot create command socket"); | |
3128 | goto end; | |
3129 | } | |
3130 | ||
3131 | ret = lttcomm_listen_unix_sock(client_socket); | |
3132 | if (ret < 0) { | |
3133 | goto end; | |
3134 | } | |
3135 | ||
32258573 | 3136 | DBG("Sending ready command to lttng-sessiond"); |
f73fabfd | 3137 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
3bd1e081 MD |
3138 | /* return < 0 on error, but == 0 is not fatal */ |
3139 | if (ret < 0) { | |
32258573 | 3140 | ERR("Error sending ready command to lttng-sessiond"); |
3bd1e081 MD |
3141 | goto end; |
3142 | } | |
3143 | ||
3bd1e081 MD |
3144 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
3145 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; | |
3146 | consumer_sockpoll[0].events = POLLIN | POLLPRI; | |
3147 | consumer_sockpoll[1].fd = client_socket; | |
3148 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3149 | ||
84382d49 MD |
3150 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3151 | if (ret) { | |
3152 | if (ret > 0) { | |
3153 | /* should exit */ | |
3154 | err = 0; | |
3155 | } | |
3bd1e081 MD |
3156 | goto end; |
3157 | } | |
3158 | DBG("Connection on client_socket"); | |
3159 | ||
3160 | /* Blocking call, waiting for transmission */ | |
3161 | sock = lttcomm_accept_unix_sock(client_socket); | |
534d2592 | 3162 | if (sock < 0) { |
3bd1e081 MD |
3163 | WARN("On accept"); |
3164 | goto end; | |
3165 | } | |
3bd1e081 | 3166 | |
331744e3 JD |
3167 | /* |
3168 | * Setup metadata socket which is the second socket connection on the | |
3169 | * command unix socket. | |
3170 | */ | |
3171 | ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket); | |
84382d49 MD |
3172 | if (ret) { |
3173 | if (ret > 0) { | |
3174 | /* should exit */ | |
3175 | err = 0; | |
3176 | } | |
331744e3 JD |
3177 | goto end; |
3178 | } | |
3179 | ||
d96f09c6 DG |
3180 | /* This socket is not useful anymore. */ |
3181 | ret = close(client_socket); | |
3182 | if (ret < 0) { | |
3183 | PERROR("close client_socket"); | |
3184 | } | |
3185 | client_socket = -1; | |
3186 | ||
3bd1e081 MD |
3187 | /* update the polling structure to poll on the established socket */ |
3188 | consumer_sockpoll[1].fd = sock; | |
3189 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3190 | ||
3191 | while (1) { | |
9ce5646a MD |
3192 | health_code_update(); |
3193 | ||
3194 | health_poll_entry(); | |
3195 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
3196 | health_poll_exit(); | |
84382d49 MD |
3197 | if (ret) { |
3198 | if (ret > 0) { | |
3199 | /* should exit */ | |
3200 | err = 0; | |
3201 | } | |
3bd1e081 MD |
3202 | goto end; |
3203 | } | |
3204 | DBG("Incoming command on sock"); | |
3205 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
4cbc1a04 DG |
3206 | if (ret <= 0) { |
3207 | /* | |
3208 | * This could simply be a session daemon quitting. Don't output | |
3209 | * ERR() here. | |
3210 | */ | |
3211 | DBG("Communication interrupted on command socket"); | |
41ba6035 | 3212 | err = 0; |
3bd1e081 MD |
3213 | goto end; |
3214 | } | |
f7389fbb | 3215 | if (CMM_LOAD_SHARED(consumer_quit)) { |
3bd1e081 | 3216 | DBG("consumer_thread_receive_fds received quit from signal"); |
1fc79fb4 | 3217 | err = 0; /* All is OK */ |
3bd1e081 MD |
3218 | goto end; |
3219 | } | |
ffe60014 | 3220 | DBG("received command on sock"); |
3bd1e081 | 3221 | } |
1fc79fb4 MD |
3222 | /* All is OK */ |
3223 | err = 0; | |
3224 | ||
3bd1e081 | 3225 | end: |
ffe60014 | 3226 | DBG("Consumer thread sessiond poll exiting"); |
3bd1e081 | 3227 | |
d88aee68 DG |
3228 | /* |
3229 | * Close metadata streams since the producer is the session daemon which | |
3230 | * just died. | |
3231 | * | |
3232 | * NOTE: for now, this only applies to the UST tracer. | |
3233 | */ | |
6d574024 | 3234 | lttng_consumer_close_all_metadata(); |
d88aee68 | 3235 | |
3bd1e081 MD |
3236 | /* |
3237 | * when all fds have hung up, the polling thread | |
3238 | * can exit cleanly | |
3239 | */ | |
f7389fbb | 3240 | CMM_STORE_SHARED(consumer_quit, 1); |
3bd1e081 | 3241 | |
04fdd819 | 3242 | /* |
c869f647 | 3243 | * Notify the data poll thread to poll back again and test the |
8994307f | 3244 | * consumer_quit state that we just set so to quit gracefully. |
04fdd819 | 3245 | */ |
acdb9057 | 3246 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
c869f647 | 3247 | |
a0cbdd2e | 3248 | notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT); |
d8ef542d | 3249 | |
5c635c72 MD |
3250 | notify_health_quit_pipe(health_quit_pipe); |
3251 | ||
d96f09c6 DG |
3252 | /* Cleaning up possibly open sockets. */ |
3253 | if (sock >= 0) { | |
3254 | ret = close(sock); | |
3255 | if (ret < 0) { | |
3256 | PERROR("close sock sessiond poll"); | |
3257 | } | |
3258 | } | |
3259 | if (client_socket >= 0) { | |
38476d24 | 3260 | ret = close(client_socket); |
d96f09c6 DG |
3261 | if (ret < 0) { |
3262 | PERROR("close client_socket sessiond poll"); | |
3263 | } | |
3264 | } | |
3265 | ||
2d57de81 | 3266 | error_testpoint: |
1fc79fb4 MD |
3267 | if (err) { |
3268 | health_error(); | |
3269 | ERR("Health error occurred in %s", __func__); | |
3270 | } | |
3271 | health_unregister(health_consumerd); | |
3272 | ||
e7b994a3 | 3273 | rcu_unregister_thread(); |
3bd1e081 MD |
3274 | return NULL; |
3275 | } | |
d41f73b7 | 3276 | |
4078b776 | 3277 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
3278 | struct lttng_consumer_local_data *ctx) |
3279 | { | |
74251bb8 DG |
3280 | ssize_t ret; |
3281 | ||
3282 | pthread_mutex_lock(&stream->lock); | |
94d49140 JD |
3283 | if (stream->metadata_flag) { |
3284 | pthread_mutex_lock(&stream->metadata_rdv_lock); | |
3285 | } | |
74251bb8 | 3286 | |
d41f73b7 MD |
3287 | switch (consumer_data.type) { |
3288 | case LTTNG_CONSUMER_KERNEL: | |
74251bb8 DG |
3289 | ret = lttng_kconsumer_read_subbuffer(stream, ctx); |
3290 | break; | |
7753dea8 MD |
3291 | case LTTNG_CONSUMER32_UST: |
3292 | case LTTNG_CONSUMER64_UST: | |
74251bb8 DG |
3293 | ret = lttng_ustconsumer_read_subbuffer(stream, ctx); |
3294 | break; | |
d41f73b7 MD |
3295 | default: |
3296 | ERR("Unknown consumer_data type"); | |
3297 | assert(0); | |
74251bb8 DG |
3298 | ret = -ENOSYS; |
3299 | break; | |
d41f73b7 | 3300 | } |
74251bb8 | 3301 | |
94d49140 JD |
3302 | if (stream->metadata_flag) { |
3303 | pthread_cond_broadcast(&stream->metadata_rdv); | |
3304 | pthread_mutex_unlock(&stream->metadata_rdv_lock); | |
3305 | } | |
74251bb8 DG |
3306 | pthread_mutex_unlock(&stream->lock); |
3307 | return ret; | |
d41f73b7 MD |
3308 | } |
3309 | ||
3310 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
3311 | { | |
3312 | switch (consumer_data.type) { | |
3313 | case LTTNG_CONSUMER_KERNEL: | |
3314 | return lttng_kconsumer_on_recv_stream(stream); | |
7753dea8 MD |
3315 | case LTTNG_CONSUMER32_UST: |
3316 | case LTTNG_CONSUMER64_UST: | |
d41f73b7 MD |
3317 | return lttng_ustconsumer_on_recv_stream(stream); |
3318 | default: | |
3319 | ERR("Unknown consumer_data type"); | |
3320 | assert(0); | |
3321 | return -ENOSYS; | |
3322 | } | |
3323 | } | |
e4421fec DG |
3324 | |
3325 | /* | |
3326 | * Allocate and set consumer data hash tables. | |
3327 | */ | |
282dadbc | 3328 | int lttng_consumer_init(void) |
e4421fec | 3329 | { |
d88aee68 | 3330 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3331 | if (!consumer_data.channel_ht) { |
3332 | goto error; | |
3333 | } | |
3334 | ||
d88aee68 | 3335 | consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3336 | if (!consumer_data.relayd_ht) { |
3337 | goto error; | |
3338 | } | |
3339 | ||
d88aee68 | 3340 | consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3341 | if (!consumer_data.stream_list_ht) { |
3342 | goto error; | |
3343 | } | |
3344 | ||
d8ef542d | 3345 | consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3346 | if (!consumer_data.stream_per_chan_id_ht) { |
3347 | goto error; | |
3348 | } | |
3349 | ||
3350 | data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3351 | if (!data_ht) { | |
3352 | goto error; | |
3353 | } | |
3354 | ||
3355 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3356 | if (!metadata_ht) { | |
3357 | goto error; | |
3358 | } | |
3359 | ||
3360 | return 0; | |
3361 | ||
3362 | error: | |
3363 | return -1; | |
e4421fec | 3364 | } |
7735ef9e DG |
3365 | |
3366 | /* | |
3367 | * Process the ADD_RELAYD command receive by a consumer. | |
3368 | * | |
3369 | * This will create a relayd socket pair and add it to the relayd hash table. | |
3370 | * The caller MUST acquire a RCU read side lock before calling it. | |
3371 | */ | |
3ab4ee7d | 3372 | void consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type, |
7735ef9e | 3373 | struct lttng_consumer_local_data *ctx, int sock, |
6151a90f | 3374 | struct pollfd *consumer_sockpoll, |
d3e2ba59 JD |
3375 | struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id, |
3376 | uint64_t relayd_session_id) | |
7735ef9e | 3377 | { |
cd2b09ed | 3378 | int fd = -1, ret = -1, relayd_created = 0; |
0c759fc9 | 3379 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
d4298c99 | 3380 | struct consumer_relayd_sock_pair *relayd = NULL; |
7735ef9e | 3381 | |
6151a90f JD |
3382 | assert(ctx); |
3383 | assert(relayd_sock); | |
3384 | ||
da009f2c | 3385 | DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx); |
7735ef9e DG |
3386 | |
3387 | /* Get relayd reference if exists. */ | |
3388 | relayd = consumer_find_relayd(net_seq_idx); | |
3389 | if (relayd == NULL) { | |
da009f2c | 3390 | assert(sock_type == LTTNG_STREAM_CONTROL); |
7735ef9e DG |
3391 | /* Not found. Allocate one. */ |
3392 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); | |
3393 | if (relayd == NULL) { | |
618a6a28 MD |
3394 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
3395 | goto error; | |
0d08d75e | 3396 | } else { |
30319bcb | 3397 | relayd->sessiond_session_id = sessiond_id; |
0d08d75e | 3398 | relayd_created = 1; |
7735ef9e | 3399 | } |
0d08d75e DG |
3400 | |
3401 | /* | |
3402 | * This code path MUST continue to the consumer send status message to | |
3403 | * we can notify the session daemon and continue our work without | |
3404 | * killing everything. | |
3405 | */ | |
da009f2c MD |
3406 | } else { |
3407 | /* | |
3408 | * relayd key should never be found for control socket. | |
3409 | */ | |
3410 | assert(sock_type != LTTNG_STREAM_CONTROL); | |
0d08d75e DG |
3411 | } |
3412 | ||
3413 | /* First send a status message before receiving the fds. */ | |
0c759fc9 | 3414 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
618a6a28 | 3415 | if (ret < 0) { |
0d08d75e | 3416 | /* Somehow, the session daemon is not responding anymore. */ |
618a6a28 MD |
3417 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
3418 | goto error_nosignal; | |
7735ef9e DG |
3419 | } |
3420 | ||
3421 | /* Poll on consumer socket. */ | |
84382d49 MD |
3422 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3423 | if (ret) { | |
3424 | /* Needing to exit in the middle of a command: error. */ | |
0d08d75e | 3425 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
618a6a28 | 3426 | goto error_nosignal; |
7735ef9e DG |
3427 | } |
3428 | ||
3429 | /* Get relayd socket from session daemon */ | |
3430 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); | |
3431 | if (ret != sizeof(fd)) { | |
4028eeb9 | 3432 | fd = -1; /* Just in case it gets set with an invalid value. */ |
0d08d75e DG |
3433 | |
3434 | /* | |
3435 | * Failing to receive FDs might indicate a major problem such as | |
3436 | * reaching a fd limit during the receive where the kernel returns a | |
3437 | * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we | |
3438 | * don't take any chances and stop everything. | |
3439 | * | |
3440 | * XXX: Feature request #558 will fix that and avoid this possible | |
3441 | * issue when reaching the fd limit. | |
3442 | */ | |
3443 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); | |
618a6a28 | 3444 | ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD; |
f50f23d9 DG |
3445 | goto error; |
3446 | } | |
3447 | ||
7735ef9e DG |
3448 | /* Copy socket information and received FD */ |
3449 | switch (sock_type) { | |
3450 | case LTTNG_STREAM_CONTROL: | |
3451 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3452 | lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock); |
3453 | ret = lttcomm_create_sock(&relayd->control_sock.sock); | |
4028eeb9 | 3454 | /* Handle create_sock error. */ |
f66c074c | 3455 | if (ret < 0) { |
618a6a28 | 3456 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3457 | goto error; |
f66c074c | 3458 | } |
da009f2c MD |
3459 | /* |
3460 | * Close the socket created internally by | |
3461 | * lttcomm_create_sock, so we can replace it by the one | |
3462 | * received from sessiond. | |
3463 | */ | |
3464 | if (close(relayd->control_sock.sock.fd)) { | |
3465 | PERROR("close"); | |
3466 | } | |
7735ef9e DG |
3467 | |
3468 | /* Assign new file descriptor */ | |
6151a90f | 3469 | relayd->control_sock.sock.fd = fd; |
4b29f1ce | 3470 | fd = -1; /* For error path */ |
6151a90f JD |
3471 | /* Assign version values. */ |
3472 | relayd->control_sock.major = relayd_sock->major; | |
3473 | relayd->control_sock.minor = relayd_sock->minor; | |
c5b6f4f0 | 3474 | |
d3e2ba59 | 3475 | relayd->relayd_session_id = relayd_session_id; |
c5b6f4f0 | 3476 | |
7735ef9e DG |
3477 | break; |
3478 | case LTTNG_STREAM_DATA: | |
3479 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3480 | lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock); |
3481 | ret = lttcomm_create_sock(&relayd->data_sock.sock); | |
4028eeb9 | 3482 | /* Handle create_sock error. */ |
f66c074c | 3483 | if (ret < 0) { |
618a6a28 | 3484 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3485 | goto error; |
f66c074c | 3486 | } |
da009f2c MD |
3487 | /* |
3488 | * Close the socket created internally by | |
3489 | * lttcomm_create_sock, so we can replace it by the one | |
3490 | * received from sessiond. | |
3491 | */ | |
3492 | if (close(relayd->data_sock.sock.fd)) { | |
3493 | PERROR("close"); | |
3494 | } | |
7735ef9e DG |
3495 | |
3496 | /* Assign new file descriptor */ | |
6151a90f | 3497 | relayd->data_sock.sock.fd = fd; |
4b29f1ce | 3498 | fd = -1; /* for eventual error paths */ |
6151a90f JD |
3499 | /* Assign version values. */ |
3500 | relayd->data_sock.major = relayd_sock->major; | |
3501 | relayd->data_sock.minor = relayd_sock->minor; | |
7735ef9e DG |
3502 | break; |
3503 | default: | |
3504 | ERR("Unknown relayd socket type (%d)", sock_type); | |
618a6a28 | 3505 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
7735ef9e DG |
3506 | goto error; |
3507 | } | |
3508 | ||
d88aee68 | 3509 | DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)", |
7735ef9e DG |
3510 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
3511 | relayd->net_seq_idx, fd); | |
3512 | ||
618a6a28 MD |
3513 | /* We successfully added the socket. Send status back. */ |
3514 | ret = consumer_send_status_msg(sock, ret_code); | |
3515 | if (ret < 0) { | |
3516 | /* Somehow, the session daemon is not responding anymore. */ | |
3517 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3518 | goto error_nosignal; | |
3519 | } | |
3520 | ||
7735ef9e DG |
3521 | /* |
3522 | * Add relayd socket pair to consumer data hashtable. If object already | |
3523 | * exists or on error, the function gracefully returns. | |
3524 | */ | |
d09e1200 | 3525 | add_relayd(relayd); |
7735ef9e DG |
3526 | |
3527 | /* All good! */ | |
3ab4ee7d | 3528 | return; |
7735ef9e DG |
3529 | |
3530 | error: | |
618a6a28 MD |
3531 | if (consumer_send_status_msg(sock, ret_code) < 0) { |
3532 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3533 | } | |
3534 | ||
3535 | error_nosignal: | |
4028eeb9 DG |
3536 | /* Close received socket if valid. */ |
3537 | if (fd >= 0) { | |
3538 | if (close(fd)) { | |
3539 | PERROR("close received socket"); | |
3540 | } | |
3541 | } | |
cd2b09ed DG |
3542 | |
3543 | if (relayd_created) { | |
cd2b09ed DG |
3544 | free(relayd); |
3545 | } | |
7735ef9e | 3546 | } |
ca22feea | 3547 | |
4e9a4686 DG |
3548 | /* |
3549 | * Try to lock the stream mutex. | |
3550 | * | |
3551 | * On success, 1 is returned else 0 indicating that the mutex is NOT lock. | |
3552 | */ | |
3553 | static int stream_try_lock(struct lttng_consumer_stream *stream) | |
3554 | { | |
3555 | int ret; | |
3556 | ||
3557 | assert(stream); | |
3558 | ||
3559 | /* | |
3560 | * Try to lock the stream mutex. On failure, we know that the stream is | |
3561 | * being used else where hence there is data still being extracted. | |
3562 | */ | |
3563 | ret = pthread_mutex_trylock(&stream->lock); | |
3564 | if (ret) { | |
3565 | /* For both EBUSY and EINVAL error, the mutex is NOT locked. */ | |
3566 | ret = 0; | |
3567 | goto end; | |
3568 | } | |
3569 | ||
3570 | ret = 1; | |
3571 | ||
3572 | end: | |
3573 | return ret; | |
3574 | } | |
3575 | ||
f7079f67 DG |
3576 | /* |
3577 | * Search for a relayd associated to the session id and return the reference. | |
3578 | * | |
3579 | * A rcu read side lock MUST be acquire before calling this function and locked | |
3580 | * until the relayd object is no longer necessary. | |
3581 | */ | |
3582 | static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id) | |
3583 | { | |
3584 | struct lttng_ht_iter iter; | |
f7079f67 | 3585 | struct consumer_relayd_sock_pair *relayd = NULL; |
f7079f67 DG |
3586 | |
3587 | /* Iterate over all relayd since they are indexed by net_seq_idx. */ | |
3588 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
3589 | node.node) { | |
18261bd1 DG |
3590 | /* |
3591 | * Check by sessiond id which is unique here where the relayd session | |
3592 | * id might not be when having multiple relayd. | |
3593 | */ | |
3594 | if (relayd->sessiond_session_id == id) { | |
f7079f67 | 3595 | /* Found the relayd. There can be only one per id. */ |
18261bd1 | 3596 | goto found; |
f7079f67 DG |
3597 | } |
3598 | } | |
3599 | ||
18261bd1 DG |
3600 | return NULL; |
3601 | ||
3602 | found: | |
f7079f67 DG |
3603 | return relayd; |
3604 | } | |
3605 | ||
ca22feea DG |
3606 | /* |
3607 | * Check if for a given session id there is still data needed to be extract | |
3608 | * from the buffers. | |
3609 | * | |
6d805429 | 3610 | * Return 1 if data is pending or else 0 meaning ready to be read. |
ca22feea | 3611 | */ |
6d805429 | 3612 | int consumer_data_pending(uint64_t id) |
ca22feea DG |
3613 | { |
3614 | int ret; | |
3615 | struct lttng_ht_iter iter; | |
3616 | struct lttng_ht *ht; | |
3617 | struct lttng_consumer_stream *stream; | |
f7079f67 | 3618 | struct consumer_relayd_sock_pair *relayd = NULL; |
6d805429 | 3619 | int (*data_pending)(struct lttng_consumer_stream *); |
ca22feea | 3620 | |
6d805429 | 3621 | DBG("Consumer data pending command on session id %" PRIu64, id); |
ca22feea | 3622 | |
6f6eda74 | 3623 | rcu_read_lock(); |
ca22feea DG |
3624 | pthread_mutex_lock(&consumer_data.lock); |
3625 | ||
3626 | switch (consumer_data.type) { | |
3627 | case LTTNG_CONSUMER_KERNEL: | |
6d805429 | 3628 | data_pending = lttng_kconsumer_data_pending; |
ca22feea DG |
3629 | break; |
3630 | case LTTNG_CONSUMER32_UST: | |
3631 | case LTTNG_CONSUMER64_UST: | |
6d805429 | 3632 | data_pending = lttng_ustconsumer_data_pending; |
ca22feea DG |
3633 | break; |
3634 | default: | |
3635 | ERR("Unknown consumer data type"); | |
3636 | assert(0); | |
3637 | } | |
3638 | ||
3639 | /* Ease our life a bit */ | |
3640 | ht = consumer_data.stream_list_ht; | |
3641 | ||
f7079f67 DG |
3642 | relayd = find_relayd_by_session_id(id); |
3643 | if (relayd) { | |
3644 | /* Send init command for data pending. */ | |
3645 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
3646 | ret = relayd_begin_data_pending(&relayd->control_sock, | |
3647 | relayd->relayd_session_id); | |
3648 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
3649 | if (ret < 0) { | |
3650 | /* Communication error thus the relayd so no data pending. */ | |
3651 | goto data_not_pending; | |
3652 | } | |
3653 | } | |
3654 | ||
c8f59ee5 | 3655 | cds_lfht_for_each_entry_duplicate(ht->ht, |
d88aee68 DG |
3656 | ht->hash_fct(&id, lttng_ht_seed), |
3657 | ht->match_fct, &id, | |
ca22feea | 3658 | &iter.iter, stream, node_session_id.node) { |
4e9a4686 DG |
3659 | /* If this call fails, the stream is being used hence data pending. */ |
3660 | ret = stream_try_lock(stream); | |
3661 | if (!ret) { | |
f7079f67 | 3662 | goto data_pending; |
ca22feea | 3663 | } |
ca22feea | 3664 | |
4e9a4686 DG |
3665 | /* |
3666 | * A removed node from the hash table indicates that the stream has | |
3667 | * been deleted thus having a guarantee that the buffers are closed | |
3668 | * on the consumer side. However, data can still be transmitted | |
3669 | * over the network so don't skip the relayd check. | |
3670 | */ | |
3671 | ret = cds_lfht_is_node_deleted(&stream->node.node); | |
3672 | if (!ret) { | |
3673 | /* Check the stream if there is data in the buffers. */ | |
6d805429 DG |
3674 | ret = data_pending(stream); |
3675 | if (ret == 1) { | |
4e9a4686 | 3676 | pthread_mutex_unlock(&stream->lock); |
f7079f67 | 3677 | goto data_pending; |
4e9a4686 DG |
3678 | } |
3679 | } | |
3680 | ||
3681 | /* Relayd check */ | |
f7079f67 | 3682 | if (relayd) { |
c8f59ee5 DG |
3683 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
3684 | if (stream->metadata_flag) { | |
ad7051c0 DG |
3685 | ret = relayd_quiescent_control(&relayd->control_sock, |
3686 | stream->relayd_stream_id); | |
c8f59ee5 | 3687 | } else { |
6d805429 | 3688 | ret = relayd_data_pending(&relayd->control_sock, |
39df6d9f DG |
3689 | stream->relayd_stream_id, |
3690 | stream->next_net_seq_num - 1); | |
c8f59ee5 DG |
3691 | } |
3692 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
6d805429 | 3693 | if (ret == 1) { |
4e9a4686 | 3694 | pthread_mutex_unlock(&stream->lock); |
f7079f67 | 3695 | goto data_pending; |
c8f59ee5 DG |
3696 | } |
3697 | } | |
4e9a4686 | 3698 | pthread_mutex_unlock(&stream->lock); |
c8f59ee5 | 3699 | } |
ca22feea | 3700 | |
f7079f67 DG |
3701 | if (relayd) { |
3702 | unsigned int is_data_inflight = 0; | |
3703 | ||
3704 | /* Send init command for data pending. */ | |
3705 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
3706 | ret = relayd_end_data_pending(&relayd->control_sock, | |
3707 | relayd->relayd_session_id, &is_data_inflight); | |
3708 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
bdd88757 | 3709 | if (ret < 0) { |
f7079f67 DG |
3710 | goto data_not_pending; |
3711 | } | |
bdd88757 DG |
3712 | if (is_data_inflight) { |
3713 | goto data_pending; | |
3714 | } | |
f7079f67 DG |
3715 | } |
3716 | ||
ca22feea | 3717 | /* |
f7079f67 DG |
3718 | * Finding _no_ node in the hash table and no inflight data means that the |
3719 | * stream(s) have been removed thus data is guaranteed to be available for | |
3720 | * analysis from the trace files. | |
ca22feea DG |
3721 | */ |
3722 | ||
f7079f67 | 3723 | data_not_pending: |
ca22feea DG |
3724 | /* Data is available to be read by a viewer. */ |
3725 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 3726 | rcu_read_unlock(); |
6d805429 | 3727 | return 0; |
ca22feea | 3728 | |
f7079f67 | 3729 | data_pending: |
ca22feea DG |
3730 | /* Data is still being extracted from buffers. */ |
3731 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 3732 | rcu_read_unlock(); |
6d805429 | 3733 | return 1; |
ca22feea | 3734 | } |
f50f23d9 DG |
3735 | |
3736 | /* | |
3737 | * Send a ret code status message to the sessiond daemon. | |
3738 | * | |
3739 | * Return the sendmsg() return value. | |
3740 | */ | |
3741 | int consumer_send_status_msg(int sock, int ret_code) | |
3742 | { | |
3743 | struct lttcomm_consumer_status_msg msg; | |
3744 | ||
53efb85a | 3745 | memset(&msg, 0, sizeof(msg)); |
f50f23d9 DG |
3746 | msg.ret_code = ret_code; |
3747 | ||
3748 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
3749 | } | |
ffe60014 DG |
3750 | |
3751 | /* | |
3752 | * Send a channel status message to the sessiond daemon. | |
3753 | * | |
3754 | * Return the sendmsg() return value. | |
3755 | */ | |
3756 | int consumer_send_status_channel(int sock, | |
3757 | struct lttng_consumer_channel *channel) | |
3758 | { | |
3759 | struct lttcomm_consumer_status_channel msg; | |
3760 | ||
3761 | assert(sock >= 0); | |
3762 | ||
53efb85a | 3763 | memset(&msg, 0, sizeof(msg)); |
ffe60014 | 3764 | if (!channel) { |
0c759fc9 | 3765 | msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; |
ffe60014 | 3766 | } else { |
0c759fc9 | 3767 | msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
ffe60014 DG |
3768 | msg.key = channel->key; |
3769 | msg.stream_count = channel->streams.count; | |
3770 | } | |
3771 | ||
3772 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
3773 | } | |
5c786ded | 3774 | |
d07ceecd MD |
3775 | unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos, |
3776 | unsigned long produced_pos, uint64_t nb_packets_per_stream, | |
3777 | uint64_t max_sb_size) | |
5c786ded | 3778 | { |
d07ceecd | 3779 | unsigned long start_pos; |
5c786ded | 3780 | |
d07ceecd MD |
3781 | if (!nb_packets_per_stream) { |
3782 | return consumed_pos; /* Grab everything */ | |
3783 | } | |
3784 | start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size); | |
3785 | start_pos -= max_sb_size * nb_packets_per_stream; | |
3786 | if ((long) (start_pos - consumed_pos) < 0) { | |
3787 | return consumed_pos; /* Grab everything */ | |
3788 | } | |
3789 | return start_pos; | |
5c786ded | 3790 | } |