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