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