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