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