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