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