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