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