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