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