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