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