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