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