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