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