Commit | Line | Data |
---|---|---|
3bd1e081 | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> | |
3bd1e081 | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
3bd1e081 | 7 | * |
3bd1e081 MD |
8 | */ |
9 | ||
bdc8d1bb | 10 | #include "common/index/ctf-index.h" |
6c1c0768 | 11 | #define _LGPL_SOURCE |
3bd1e081 | 12 | #include <assert.h> |
3bd1e081 MD |
13 | #include <poll.h> |
14 | #include <pthread.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <sys/mman.h> | |
18 | #include <sys/socket.h> | |
19 | #include <sys/types.h> | |
20 | #include <unistd.h> | |
77c7c900 | 21 | #include <inttypes.h> |
331744e3 | 22 | #include <signal.h> |
3bd1e081 | 23 | |
51a9e1c7 | 24 | #include <bin/lttng-consumerd/health-consumerd.h> |
990570ed | 25 | #include <common/common.h> |
fb3a43a9 | 26 | #include <common/utils.h> |
d2956687 | 27 | #include <common/time.h> |
fb3a43a9 | 28 | #include <common/compat/poll.h> |
f263b7fd | 29 | #include <common/compat/endian.h> |
309167d2 | 30 | #include <common/index/index.h> |
10a8a223 | 31 | #include <common/kernel-ctl/kernel-ctl.h> |
00e2e675 | 32 | #include <common/sessiond-comm/relayd.h> |
10a8a223 DG |
33 | #include <common/sessiond-comm/sessiond-comm.h> |
34 | #include <common/kernel-consumer/kernel-consumer.h> | |
00e2e675 | 35 | #include <common/relayd/relayd.h> |
10a8a223 | 36 | #include <common/ust-consumer/ust-consumer.h> |
c8fea79c JR |
37 | #include <common/consumer/consumer-timer.h> |
38 | #include <common/consumer/consumer.h> | |
39 | #include <common/consumer/consumer-stream.h> | |
40 | #include <common/consumer/consumer-testpoint.h> | |
41 | #include <common/align.h> | |
5feafd41 | 42 | #include <common/consumer/consumer-metadata-cache.h> |
d2956687 JG |
43 | #include <common/trace-chunk.h> |
44 | #include <common/trace-chunk-registry.h> | |
45 | #include <common/string-utils/format.h> | |
c35f9726 | 46 | #include <common/dynamic-array.h> |
3bd1e081 MD |
47 | |
48 | struct lttng_consumer_global_data consumer_data = { | |
3bd1e081 MD |
49 | .stream_count = 0, |
50 | .need_update = 1, | |
51 | .type = LTTNG_CONSUMER_UNKNOWN, | |
52 | }; | |
53 | ||
d8ef542d MD |
54 | enum consumer_channel_action { |
55 | CONSUMER_CHANNEL_ADD, | |
a0cbdd2e | 56 | CONSUMER_CHANNEL_DEL, |
d8ef542d MD |
57 | CONSUMER_CHANNEL_QUIT, |
58 | }; | |
59 | ||
60 | struct consumer_channel_msg { | |
61 | enum consumer_channel_action action; | |
a0cbdd2e MD |
62 | struct lttng_consumer_channel *chan; /* add */ |
63 | uint64_t key; /* del */ | |
d8ef542d MD |
64 | }; |
65 | ||
fc0c52e9 JG |
66 | enum open_packet_status { |
67 | OPEN_PACKET_STATUS_OPENED, | |
68 | OPEN_PACKET_STATUS_NO_SPACE, | |
69 | OPEN_PACKET_STATUS_ERROR, | |
70 | }; | |
71 | ||
80957876 | 72 | /* Flag used to temporarily pause data consumption from testpoints. */ |
cf0bcb51 JG |
73 | int data_consumption_paused; |
74 | ||
3bd1e081 MD |
75 | /* |
76 | * Flag to inform the polling thread to quit when all fd hung up. Updated by | |
77 | * the consumer_thread_receive_fds when it notices that all fds has hung up. | |
78 | * Also updated by the signal handler (consumer_should_exit()). Read by the | |
79 | * polling threads. | |
80 | */ | |
10211f5c | 81 | int consumer_quit; |
3bd1e081 | 82 | |
43c34bc3 | 83 | /* |
43c34bc3 DG |
84 | * Global hash table containing respectively metadata and data streams. The |
85 | * stream element in this ht should only be updated by the metadata poll thread | |
86 | * for the metadata and the data poll thread for the data. | |
87 | */ | |
40dc48e0 DG |
88 | static struct lttng_ht *metadata_ht; |
89 | static struct lttng_ht *data_ht; | |
43c34bc3 | 90 | |
5da88b0f MD |
91 | static const char *get_consumer_domain(void) |
92 | { | |
93 | switch (consumer_data.type) { | |
94 | case LTTNG_CONSUMER_KERNEL: | |
95 | return DEFAULT_KERNEL_TRACE_DIR; | |
96 | case LTTNG_CONSUMER64_UST: | |
97 | /* Fall-through. */ | |
98 | case LTTNG_CONSUMER32_UST: | |
99 | return DEFAULT_UST_TRACE_DIR; | |
100 | default: | |
101 | abort(); | |
102 | } | |
103 | } | |
104 | ||
acdb9057 DG |
105 | /* |
106 | * Notify a thread lttng pipe to poll back again. This usually means that some | |
107 | * global state has changed so we just send back the thread in a poll wait | |
108 | * call. | |
109 | */ | |
110 | static void notify_thread_lttng_pipe(struct lttng_pipe *pipe) | |
111 | { | |
112 | struct lttng_consumer_stream *null_stream = NULL; | |
113 | ||
114 | assert(pipe); | |
115 | ||
116 | (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream)); | |
117 | } | |
118 | ||
5c635c72 MD |
119 | static void notify_health_quit_pipe(int *pipe) |
120 | { | |
6cd525e8 | 121 | ssize_t ret; |
5c635c72 | 122 | |
6cd525e8 MD |
123 | ret = lttng_write(pipe[1], "4", 1); |
124 | if (ret < 1) { | |
5c635c72 MD |
125 | PERROR("write consumer health quit"); |
126 | } | |
127 | } | |
128 | ||
d8ef542d MD |
129 | static void notify_channel_pipe(struct lttng_consumer_local_data *ctx, |
130 | struct lttng_consumer_channel *chan, | |
a0cbdd2e | 131 | uint64_t key, |
d8ef542d MD |
132 | enum consumer_channel_action action) |
133 | { | |
134 | struct consumer_channel_msg msg; | |
6cd525e8 | 135 | ssize_t ret; |
d8ef542d | 136 | |
e56251fc DG |
137 | memset(&msg, 0, sizeof(msg)); |
138 | ||
d8ef542d MD |
139 | msg.action = action; |
140 | msg.chan = chan; | |
f21dae48 | 141 | msg.key = key; |
6cd525e8 MD |
142 | ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg)); |
143 | if (ret < sizeof(msg)) { | |
144 | PERROR("notify_channel_pipe write error"); | |
145 | } | |
d8ef542d MD |
146 | } |
147 | ||
a0cbdd2e MD |
148 | void notify_thread_del_channel(struct lttng_consumer_local_data *ctx, |
149 | uint64_t key) | |
150 | { | |
151 | notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL); | |
152 | } | |
153 | ||
d8ef542d MD |
154 | static int read_channel_pipe(struct lttng_consumer_local_data *ctx, |
155 | struct lttng_consumer_channel **chan, | |
a0cbdd2e | 156 | uint64_t *key, |
d8ef542d MD |
157 | enum consumer_channel_action *action) |
158 | { | |
159 | struct consumer_channel_msg msg; | |
6cd525e8 | 160 | ssize_t ret; |
d8ef542d | 161 | |
6cd525e8 MD |
162 | ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg)); |
163 | if (ret < sizeof(msg)) { | |
164 | ret = -1; | |
165 | goto error; | |
d8ef542d | 166 | } |
6cd525e8 MD |
167 | *action = msg.action; |
168 | *chan = msg.chan; | |
169 | *key = msg.key; | |
170 | error: | |
171 | return (int) ret; | |
d8ef542d MD |
172 | } |
173 | ||
212d67a2 DG |
174 | /* |
175 | * Cleanup the stream list of a channel. Those streams are not yet globally | |
176 | * visible | |
177 | */ | |
178 | static void clean_channel_stream_list(struct lttng_consumer_channel *channel) | |
179 | { | |
180 | struct lttng_consumer_stream *stream, *stmp; | |
181 | ||
182 | assert(channel); | |
183 | ||
184 | /* Delete streams that might have been left in the stream list. */ | |
185 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
186 | send_node) { | |
187 | cds_list_del(&stream->send_node); | |
188 | /* | |
189 | * Once a stream is added to this list, the buffers were created so we | |
190 | * have a guarantee that this call will succeed. Setting the monitor | |
191 | * mode to 0 so we don't lock nor try to delete the stream from the | |
192 | * global hash table. | |
193 | */ | |
194 | stream->monitor = 0; | |
195 | consumer_stream_destroy(stream, NULL); | |
196 | } | |
197 | } | |
198 | ||
3bd1e081 MD |
199 | /* |
200 | * Find a stream. The consumer_data.lock must be locked during this | |
201 | * call. | |
202 | */ | |
d88aee68 | 203 | static struct lttng_consumer_stream *find_stream(uint64_t key, |
8389e4f8 | 204 | struct lttng_ht *ht) |
3bd1e081 | 205 | { |
e4421fec | 206 | struct lttng_ht_iter iter; |
d88aee68 | 207 | struct lttng_ht_node_u64 *node; |
e4421fec | 208 | struct lttng_consumer_stream *stream = NULL; |
3bd1e081 | 209 | |
8389e4f8 DG |
210 | assert(ht); |
211 | ||
d88aee68 DG |
212 | /* -1ULL keys are lookup failures */ |
213 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 214 | return NULL; |
7a57cf92 | 215 | } |
e4421fec | 216 | |
6065ceec DG |
217 | rcu_read_lock(); |
218 | ||
d88aee68 DG |
219 | lttng_ht_lookup(ht, &key, &iter); |
220 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
221 | if (node != NULL) { |
222 | stream = caa_container_of(node, struct lttng_consumer_stream, node); | |
3bd1e081 | 223 | } |
e4421fec | 224 | |
6065ceec DG |
225 | rcu_read_unlock(); |
226 | ||
e4421fec | 227 | return stream; |
3bd1e081 MD |
228 | } |
229 | ||
da009f2c | 230 | static void steal_stream_key(uint64_t key, struct lttng_ht *ht) |
7ad0a0cb MD |
231 | { |
232 | struct lttng_consumer_stream *stream; | |
233 | ||
04253271 | 234 | rcu_read_lock(); |
ffe60014 | 235 | stream = find_stream(key, ht); |
04253271 | 236 | if (stream) { |
da009f2c | 237 | stream->key = (uint64_t) -1ULL; |
04253271 MD |
238 | /* |
239 | * We don't want the lookup to match, but we still need | |
240 | * to iterate on this stream when iterating over the hash table. Just | |
241 | * change the node key. | |
242 | */ | |
da009f2c | 243 | stream->node.key = (uint64_t) -1ULL; |
04253271 MD |
244 | } |
245 | rcu_read_unlock(); | |
7ad0a0cb MD |
246 | } |
247 | ||
d56db448 DG |
248 | /* |
249 | * Return a channel object for the given key. | |
250 | * | |
251 | * RCU read side lock MUST be acquired before calling this function and | |
252 | * protects the channel ptr. | |
253 | */ | |
d88aee68 | 254 | struct lttng_consumer_channel *consumer_find_channel(uint64_t key) |
3bd1e081 | 255 | { |
e4421fec | 256 | struct lttng_ht_iter iter; |
d88aee68 | 257 | struct lttng_ht_node_u64 *node; |
e4421fec | 258 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 | 259 | |
d88aee68 DG |
260 | /* -1ULL keys are lookup failures */ |
261 | if (key == (uint64_t) -1ULL) { | |
7ad0a0cb | 262 | return NULL; |
7a57cf92 | 263 | } |
e4421fec | 264 | |
d88aee68 DG |
265 | lttng_ht_lookup(consumer_data.channel_ht, &key, &iter); |
266 | node = lttng_ht_iter_get_node_u64(&iter); | |
e4421fec DG |
267 | if (node != NULL) { |
268 | channel = caa_container_of(node, struct lttng_consumer_channel, node); | |
3bd1e081 | 269 | } |
e4421fec DG |
270 | |
271 | return channel; | |
3bd1e081 MD |
272 | } |
273 | ||
b5a6470f DG |
274 | /* |
275 | * There is a possibility that the consumer does not have enough time between | |
276 | * the close of the channel on the session daemon and the cleanup in here thus | |
277 | * once we have a channel add with an existing key, we know for sure that this | |
278 | * channel will eventually get cleaned up by all streams being closed. | |
279 | * | |
280 | * This function just nullifies the already existing channel key. | |
281 | */ | |
282 | static void steal_channel_key(uint64_t key) | |
283 | { | |
284 | struct lttng_consumer_channel *channel; | |
285 | ||
286 | rcu_read_lock(); | |
287 | channel = consumer_find_channel(key); | |
288 | if (channel) { | |
289 | channel->key = (uint64_t) -1ULL; | |
290 | /* | |
291 | * We don't want the lookup to match, but we still need to iterate on | |
292 | * this channel when iterating over the hash table. Just change the | |
293 | * node key. | |
294 | */ | |
295 | channel->node.key = (uint64_t) -1ULL; | |
296 | } | |
297 | rcu_read_unlock(); | |
298 | } | |
299 | ||
ffe60014 | 300 | static void free_channel_rcu(struct rcu_head *head) |
702b1ea4 | 301 | { |
d88aee68 DG |
302 | struct lttng_ht_node_u64 *node = |
303 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
ffe60014 DG |
304 | struct lttng_consumer_channel *channel = |
305 | caa_container_of(node, struct lttng_consumer_channel, node); | |
702b1ea4 | 306 | |
b83e03c4 MD |
307 | switch (consumer_data.type) { |
308 | case LTTNG_CONSUMER_KERNEL: | |
309 | break; | |
310 | case LTTNG_CONSUMER32_UST: | |
311 | case LTTNG_CONSUMER64_UST: | |
312 | lttng_ustconsumer_free_channel(channel); | |
313 | break; | |
314 | default: | |
315 | ERR("Unknown consumer_data type"); | |
316 | abort(); | |
317 | } | |
ffe60014 | 318 | free(channel); |
702b1ea4 MD |
319 | } |
320 | ||
00e2e675 DG |
321 | /* |
322 | * RCU protected relayd socket pair free. | |
323 | */ | |
ffe60014 | 324 | static void free_relayd_rcu(struct rcu_head *head) |
00e2e675 | 325 | { |
d88aee68 DG |
326 | struct lttng_ht_node_u64 *node = |
327 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
00e2e675 DG |
328 | struct consumer_relayd_sock_pair *relayd = |
329 | caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
330 | ||
8994307f DG |
331 | /* |
332 | * Close all sockets. This is done in the call RCU since we don't want the | |
333 | * socket fds to be reassigned thus potentially creating bad state of the | |
334 | * relayd object. | |
335 | * | |
336 | * We do not have to lock the control socket mutex here since at this stage | |
337 | * there is no one referencing to this relayd object. | |
338 | */ | |
339 | (void) relayd_close(&relayd->control_sock); | |
340 | (void) relayd_close(&relayd->data_sock); | |
341 | ||
3a84e2f3 | 342 | pthread_mutex_destroy(&relayd->ctrl_sock_mutex); |
00e2e675 DG |
343 | free(relayd); |
344 | } | |
345 | ||
346 | /* | |
347 | * Destroy and free relayd socket pair object. | |
00e2e675 | 348 | */ |
51230d70 | 349 | void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
350 | { |
351 | int ret; | |
352 | struct lttng_ht_iter iter; | |
353 | ||
173af62f DG |
354 | if (relayd == NULL) { |
355 | return; | |
356 | } | |
357 | ||
00e2e675 DG |
358 | DBG("Consumer destroy and close relayd socket pair"); |
359 | ||
360 | iter.iter.node = &relayd->node.node; | |
361 | ret = lttng_ht_del(consumer_data.relayd_ht, &iter); | |
173af62f | 362 | if (ret != 0) { |
8994307f | 363 | /* We assume the relayd is being or is destroyed */ |
173af62f DG |
364 | return; |
365 | } | |
00e2e675 | 366 | |
00e2e675 | 367 | /* RCU free() call */ |
ffe60014 DG |
368 | call_rcu(&relayd->node.head, free_relayd_rcu); |
369 | } | |
370 | ||
371 | /* | |
372 | * Remove a channel from the global list protected by a mutex. This function is | |
373 | * also responsible for freeing its data structures. | |
374 | */ | |
375 | void consumer_del_channel(struct lttng_consumer_channel *channel) | |
376 | { | |
ffe60014 DG |
377 | struct lttng_ht_iter iter; |
378 | ||
d88aee68 | 379 | DBG("Consumer delete channel key %" PRIu64, channel->key); |
ffe60014 DG |
380 | |
381 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 382 | pthread_mutex_lock(&channel->lock); |
ffe60014 | 383 | |
212d67a2 DG |
384 | /* Destroy streams that might have been left in the stream list. */ |
385 | clean_channel_stream_list(channel); | |
51e762e5 | 386 | |
d3e2ba59 JD |
387 | if (channel->live_timer_enabled == 1) { |
388 | consumer_timer_live_stop(channel); | |
389 | } | |
e9404c27 JG |
390 | if (channel->monitor_timer_enabled == 1) { |
391 | consumer_timer_monitor_stop(channel); | |
392 | } | |
d3e2ba59 | 393 | |
ffe60014 DG |
394 | switch (consumer_data.type) { |
395 | case LTTNG_CONSUMER_KERNEL: | |
396 | break; | |
397 | case LTTNG_CONSUMER32_UST: | |
398 | case LTTNG_CONSUMER64_UST: | |
399 | lttng_ustconsumer_del_channel(channel); | |
400 | break; | |
401 | default: | |
402 | ERR("Unknown consumer_data type"); | |
403 | assert(0); | |
404 | goto end; | |
405 | } | |
406 | ||
d2956687 JG |
407 | lttng_trace_chunk_put(channel->trace_chunk); |
408 | channel->trace_chunk = NULL; | |
5c3892a6 | 409 | |
d2956687 JG |
410 | if (channel->is_published) { |
411 | int ret; | |
412 | ||
413 | rcu_read_lock(); | |
414 | iter.iter.node = &channel->node.node; | |
415 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); | |
416 | assert(!ret); | |
ffe60014 | 417 | |
d2956687 JG |
418 | iter.iter.node = &channel->channels_by_session_id_ht_node.node; |
419 | ret = lttng_ht_del(consumer_data.channels_by_session_id_ht, | |
420 | &iter); | |
421 | assert(!ret); | |
422 | rcu_read_unlock(); | |
423 | } | |
424 | ||
b6921a17 JG |
425 | channel->is_deleted = true; |
426 | call_rcu(&channel->node.head, free_channel_rcu); | |
ffe60014 | 427 | end: |
a9838785 | 428 | pthread_mutex_unlock(&channel->lock); |
ffe60014 | 429 | pthread_mutex_unlock(&consumer_data.lock); |
00e2e675 DG |
430 | } |
431 | ||
228b5bf7 DG |
432 | /* |
433 | * Iterate over the relayd hash table and destroy each element. Finally, | |
434 | * destroy the whole hash table. | |
435 | */ | |
436 | static void cleanup_relayd_ht(void) | |
437 | { | |
438 | struct lttng_ht_iter iter; | |
439 | struct consumer_relayd_sock_pair *relayd; | |
440 | ||
441 | rcu_read_lock(); | |
442 | ||
443 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
444 | node.node) { | |
51230d70 | 445 | consumer_destroy_relayd(relayd); |
228b5bf7 DG |
446 | } |
447 | ||
228b5bf7 | 448 | rcu_read_unlock(); |
36b588ed MD |
449 | |
450 | lttng_ht_destroy(consumer_data.relayd_ht); | |
228b5bf7 DG |
451 | } |
452 | ||
8994307f DG |
453 | /* |
454 | * Update the end point status of all streams having the given network sequence | |
455 | * index (relayd index). | |
456 | * | |
457 | * It's atomically set without having the stream mutex locked which is fine | |
458 | * because we handle the write/read race with a pipe wakeup for each thread. | |
459 | */ | |
da009f2c | 460 | static void update_endpoint_status_by_netidx(uint64_t net_seq_idx, |
8994307f DG |
461 | enum consumer_endpoint_status status) |
462 | { | |
463 | struct lttng_ht_iter iter; | |
464 | struct lttng_consumer_stream *stream; | |
465 | ||
da009f2c | 466 | DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx); |
8994307f DG |
467 | |
468 | rcu_read_lock(); | |
469 | ||
470 | /* Let's begin with metadata */ | |
471 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
472 | if (stream->net_seq_idx == net_seq_idx) { | |
473 | uatomic_set(&stream->endpoint_status, status); | |
474 | DBG("Delete flag set to metadata stream %d", stream->wait_fd); | |
475 | } | |
476 | } | |
477 | ||
478 | /* Follow up by the data streams */ | |
479 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
480 | if (stream->net_seq_idx == net_seq_idx) { | |
481 | uatomic_set(&stream->endpoint_status, status); | |
482 | DBG("Delete flag set to data stream %d", stream->wait_fd); | |
483 | } | |
484 | } | |
485 | rcu_read_unlock(); | |
486 | } | |
487 | ||
488 | /* | |
489 | * Cleanup a relayd object by flagging every associated streams for deletion, | |
490 | * destroying the object meaning removing it from the relayd hash table, | |
491 | * closing the sockets and freeing the memory in a RCU call. | |
492 | * | |
493 | * If a local data context is available, notify the threads that the streams' | |
494 | * state have changed. | |
495 | */ | |
9276e5c8 | 496 | void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd) |
8994307f | 497 | { |
da009f2c | 498 | uint64_t netidx; |
8994307f DG |
499 | |
500 | assert(relayd); | |
501 | ||
9276e5c8 | 502 | DBG("Cleaning up relayd object ID %"PRIu64, relayd->net_seq_idx); |
9617607b | 503 | |
8994307f DG |
504 | /* Save the net sequence index before destroying the object */ |
505 | netidx = relayd->net_seq_idx; | |
506 | ||
507 | /* | |
508 | * Delete the relayd from the relayd hash table, close the sockets and free | |
509 | * the object in a RCU call. | |
510 | */ | |
51230d70 | 511 | consumer_destroy_relayd(relayd); |
8994307f DG |
512 | |
513 | /* Set inactive endpoint to all streams */ | |
514 | update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE); | |
515 | ||
516 | /* | |
517 | * With a local data context, notify the threads that the streams' state | |
518 | * have changed. The write() action on the pipe acts as an "implicit" | |
519 | * memory barrier ordering the updates of the end point status from the | |
520 | * read of this status which happens AFTER receiving this notify. | |
521 | */ | |
9276e5c8 JR |
522 | notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe); |
523 | notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe); | |
8994307f DG |
524 | } |
525 | ||
a6ba4fe1 DG |
526 | /* |
527 | * Flag a relayd socket pair for destruction. Destroy it if the refcount | |
528 | * reaches zero. | |
529 | * | |
530 | * RCU read side lock MUST be aquired before calling this function. | |
531 | */ | |
532 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) | |
533 | { | |
534 | assert(relayd); | |
535 | ||
536 | /* Set destroy flag for this object */ | |
537 | uatomic_set(&relayd->destroy_flag, 1); | |
538 | ||
539 | /* Destroy the relayd if refcount is 0 */ | |
540 | if (uatomic_read(&relayd->refcount) == 0) { | |
51230d70 | 541 | consumer_destroy_relayd(relayd); |
a6ba4fe1 DG |
542 | } |
543 | } | |
544 | ||
3bd1e081 | 545 | /* |
1d1a276c DG |
546 | * Completly destroy stream from every visiable data structure and the given |
547 | * hash table if one. | |
548 | * | |
549 | * One this call returns, the stream object is not longer usable nor visible. | |
3bd1e081 | 550 | */ |
e316aad5 DG |
551 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
552 | struct lttng_ht *ht) | |
3bd1e081 | 553 | { |
1d1a276c | 554 | consumer_stream_destroy(stream, ht); |
3bd1e081 MD |
555 | } |
556 | ||
5ab66908 MD |
557 | /* |
558 | * XXX naming of del vs destroy is all mixed up. | |
559 | */ | |
560 | void consumer_del_stream_for_data(struct lttng_consumer_stream *stream) | |
561 | { | |
562 | consumer_stream_destroy(stream, data_ht); | |
563 | } | |
564 | ||
565 | void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream) | |
566 | { | |
567 | consumer_stream_destroy(stream, metadata_ht); | |
568 | } | |
569 | ||
d9a2e16e JD |
570 | void consumer_stream_update_channel_attributes( |
571 | struct lttng_consumer_stream *stream, | |
572 | struct lttng_consumer_channel *channel) | |
573 | { | |
574 | stream->channel_read_only_attributes.tracefile_size = | |
575 | channel->tracefile_size; | |
d9a2e16e JD |
576 | } |
577 | ||
3bd1e081 MD |
578 | /* |
579 | * Add a stream to the global list protected by a mutex. | |
580 | */ | |
66d583dc | 581 | void consumer_add_data_stream(struct lttng_consumer_stream *stream) |
3bd1e081 | 582 | { |
5ab66908 | 583 | struct lttng_ht *ht = data_ht; |
3bd1e081 | 584 | |
e316aad5 | 585 | assert(stream); |
43c34bc3 | 586 | assert(ht); |
c77fc10a | 587 | |
d88aee68 | 588 | DBG3("Adding consumer stream %" PRIu64, stream->key); |
e316aad5 DG |
589 | |
590 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 591 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 592 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 593 | pthread_mutex_lock(&stream->lock); |
b0b335c8 | 594 | rcu_read_lock(); |
e316aad5 | 595 | |
43c34bc3 | 596 | /* Steal stream identifier to avoid having streams with the same key */ |
ffe60014 | 597 | steal_stream_key(stream->key, ht); |
43c34bc3 | 598 | |
d88aee68 | 599 | lttng_ht_add_unique_u64(ht, &stream->node); |
00e2e675 | 600 | |
d8ef542d MD |
601 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
602 | &stream->node_channel_id); | |
603 | ||
ca22feea DG |
604 | /* |
605 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
606 | * the key since the HT does not use it and we allow to add redundant keys | |
607 | * into this table. | |
608 | */ | |
d88aee68 | 609 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 610 | |
e316aad5 | 611 | /* |
ffe60014 DG |
612 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
613 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
614 | * causes the count to become 0 also causes a stream to be added. The |
615 | * channel deletion will thus be triggered by the following removal of this | |
616 | * stream. | |
617 | */ | |
ffe60014 | 618 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
619 | /* Increment refcount before decrementing nb_init_stream_left */ |
620 | cmm_smp_wmb(); | |
ffe60014 | 621 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
622 | } |
623 | ||
624 | /* Update consumer data once the node is inserted. */ | |
3bd1e081 MD |
625 | consumer_data.stream_count++; |
626 | consumer_data.need_update = 1; | |
627 | ||
e316aad5 | 628 | rcu_read_unlock(); |
2e818a6a | 629 | pthread_mutex_unlock(&stream->lock); |
ec6ea7d0 | 630 | pthread_mutex_unlock(&stream->chan->timer_lock); |
a9838785 | 631 | pthread_mutex_unlock(&stream->chan->lock); |
3bd1e081 | 632 | pthread_mutex_unlock(&consumer_data.lock); |
3bd1e081 MD |
633 | } |
634 | ||
00e2e675 | 635 | /* |
3f8e211f DG |
636 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
637 | * be acquired before calling this. | |
00e2e675 | 638 | */ |
d09e1200 | 639 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
00e2e675 DG |
640 | { |
641 | int ret = 0; | |
d88aee68 | 642 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
643 | struct lttng_ht_iter iter; |
644 | ||
ffe60014 | 645 | assert(relayd); |
00e2e675 | 646 | |
00e2e675 | 647 | lttng_ht_lookup(consumer_data.relayd_ht, |
d88aee68 DG |
648 | &relayd->net_seq_idx, &iter); |
649 | node = lttng_ht_iter_get_node_u64(&iter); | |
00e2e675 | 650 | if (node != NULL) { |
00e2e675 DG |
651 | goto end; |
652 | } | |
d88aee68 | 653 | lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node); |
00e2e675 | 654 | |
00e2e675 DG |
655 | end: |
656 | return ret; | |
657 | } | |
658 | ||
659 | /* | |
660 | * Allocate and return a consumer relayd socket. | |
661 | */ | |
027a694f | 662 | static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
da009f2c | 663 | uint64_t net_seq_idx) |
00e2e675 DG |
664 | { |
665 | struct consumer_relayd_sock_pair *obj = NULL; | |
666 | ||
da009f2c MD |
667 | /* net sequence index of -1 is a failure */ |
668 | if (net_seq_idx == (uint64_t) -1ULL) { | |
00e2e675 DG |
669 | goto error; |
670 | } | |
671 | ||
672 | obj = zmalloc(sizeof(struct consumer_relayd_sock_pair)); | |
673 | if (obj == NULL) { | |
674 | PERROR("zmalloc relayd sock"); | |
675 | goto error; | |
676 | } | |
677 | ||
678 | obj->net_seq_idx = net_seq_idx; | |
679 | obj->refcount = 0; | |
173af62f | 680 | obj->destroy_flag = 0; |
f96e4545 MD |
681 | obj->control_sock.sock.fd = -1; |
682 | obj->data_sock.sock.fd = -1; | |
d88aee68 | 683 | lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx); |
00e2e675 DG |
684 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
685 | ||
686 | error: | |
687 | return obj; | |
688 | } | |
689 | ||
690 | /* | |
691 | * Find a relayd socket pair in the global consumer data. | |
692 | * | |
693 | * Return the object if found else NULL. | |
b0b335c8 MD |
694 | * RCU read-side lock must be held across this call and while using the |
695 | * returned object. | |
00e2e675 | 696 | */ |
d88aee68 | 697 | struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key) |
00e2e675 DG |
698 | { |
699 | struct lttng_ht_iter iter; | |
d88aee68 | 700 | struct lttng_ht_node_u64 *node; |
00e2e675 DG |
701 | struct consumer_relayd_sock_pair *relayd = NULL; |
702 | ||
703 | /* Negative keys are lookup failures */ | |
d88aee68 | 704 | if (key == (uint64_t) -1ULL) { |
00e2e675 DG |
705 | goto error; |
706 | } | |
707 | ||
d88aee68 | 708 | lttng_ht_lookup(consumer_data.relayd_ht, &key, |
00e2e675 | 709 | &iter); |
d88aee68 | 710 | node = lttng_ht_iter_get_node_u64(&iter); |
00e2e675 DG |
711 | if (node != NULL) { |
712 | relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node); | |
713 | } | |
714 | ||
00e2e675 DG |
715 | error: |
716 | return relayd; | |
717 | } | |
718 | ||
10a50311 JD |
719 | /* |
720 | * Find a relayd and send the stream | |
721 | * | |
722 | * Returns 0 on success, < 0 on error | |
723 | */ | |
724 | int consumer_send_relayd_stream(struct lttng_consumer_stream *stream, | |
725 | char *path) | |
726 | { | |
727 | int ret = 0; | |
728 | struct consumer_relayd_sock_pair *relayd; | |
729 | ||
730 | assert(stream); | |
731 | assert(stream->net_seq_idx != -1ULL); | |
732 | assert(path); | |
733 | ||
734 | /* The stream is not metadata. Get relayd reference if exists. */ | |
735 | rcu_read_lock(); | |
736 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
737 | if (relayd != NULL) { | |
738 | /* Add stream on the relayd */ | |
739 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
740 | ret = relayd_add_stream(&relayd->control_sock, stream->name, | |
5da88b0f | 741 | get_consumer_domain(), path, &stream->relayd_stream_id, |
d2956687 JG |
742 | stream->chan->tracefile_size, |
743 | stream->chan->tracefile_count, | |
744 | stream->trace_chunk); | |
10a50311 JD |
745 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
746 | if (ret < 0) { | |
9276e5c8 JR |
747 | ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
748 | lttng_consumer_cleanup_relayd(relayd); | |
10a50311 JD |
749 | goto end; |
750 | } | |
1c20f0e2 | 751 | |
10a50311 | 752 | uatomic_inc(&relayd->refcount); |
d01178b6 | 753 | stream->sent_to_relayd = 1; |
10a50311 JD |
754 | } else { |
755 | ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.", | |
756 | stream->key, stream->net_seq_idx); | |
757 | ret = -1; | |
758 | goto end; | |
759 | } | |
760 | ||
761 | DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64, | |
762 | stream->name, stream->key, stream->net_seq_idx); | |
763 | ||
764 | end: | |
765 | rcu_read_unlock(); | |
766 | return ret; | |
767 | } | |
768 | ||
a4baae1b JD |
769 | /* |
770 | * Find a relayd and send the streams sent message | |
771 | * | |
772 | * Returns 0 on success, < 0 on error | |
773 | */ | |
774 | int consumer_send_relayd_streams_sent(uint64_t net_seq_idx) | |
775 | { | |
776 | int ret = 0; | |
777 | struct consumer_relayd_sock_pair *relayd; | |
778 | ||
779 | assert(net_seq_idx != -1ULL); | |
780 | ||
781 | /* The stream is not metadata. Get relayd reference if exists. */ | |
782 | rcu_read_lock(); | |
783 | relayd = consumer_find_relayd(net_seq_idx); | |
784 | if (relayd != NULL) { | |
785 | /* Add stream on the relayd */ | |
786 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
787 | ret = relayd_streams_sent(&relayd->control_sock); | |
788 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
789 | if (ret < 0) { | |
9276e5c8 JR |
790 | ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
791 | lttng_consumer_cleanup_relayd(relayd); | |
a4baae1b JD |
792 | goto end; |
793 | } | |
794 | } else { | |
795 | ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.", | |
796 | net_seq_idx); | |
797 | ret = -1; | |
798 | goto end; | |
799 | } | |
800 | ||
801 | ret = 0; | |
802 | DBG("All streams sent relayd id %" PRIu64, net_seq_idx); | |
803 | ||
804 | end: | |
805 | rcu_read_unlock(); | |
806 | return ret; | |
807 | } | |
808 | ||
10a50311 JD |
809 | /* |
810 | * Find a relayd and close the stream | |
811 | */ | |
812 | void close_relayd_stream(struct lttng_consumer_stream *stream) | |
813 | { | |
814 | struct consumer_relayd_sock_pair *relayd; | |
815 | ||
816 | /* The stream is not metadata. Get relayd reference if exists. */ | |
817 | rcu_read_lock(); | |
818 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
819 | if (relayd) { | |
820 | consumer_stream_relayd_close(stream, relayd); | |
821 | } | |
822 | rcu_read_unlock(); | |
823 | } | |
824 | ||
00e2e675 DG |
825 | /* |
826 | * Handle stream for relayd transmission if the stream applies for network | |
827 | * streaming where the net sequence index is set. | |
828 | * | |
829 | * Return destination file descriptor or negative value on error. | |
830 | */ | |
6197aea7 | 831 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
1d4dfdef DG |
832 | size_t data_size, unsigned long padding, |
833 | struct consumer_relayd_sock_pair *relayd) | |
00e2e675 DG |
834 | { |
835 | int outfd = -1, ret; | |
00e2e675 DG |
836 | struct lttcomm_relayd_data_hdr data_hdr; |
837 | ||
838 | /* Safety net */ | |
839 | assert(stream); | |
6197aea7 | 840 | assert(relayd); |
00e2e675 DG |
841 | |
842 | /* Reset data header */ | |
843 | memset(&data_hdr, 0, sizeof(data_hdr)); | |
844 | ||
00e2e675 DG |
845 | if (stream->metadata_flag) { |
846 | /* Caller MUST acquire the relayd control socket lock */ | |
847 | ret = relayd_send_metadata(&relayd->control_sock, data_size); | |
848 | if (ret < 0) { | |
849 | goto error; | |
850 | } | |
851 | ||
852 | /* Metadata are always sent on the control socket. */ | |
6151a90f | 853 | outfd = relayd->control_sock.sock.fd; |
00e2e675 DG |
854 | } else { |
855 | /* Set header with stream information */ | |
856 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); | |
857 | data_hdr.data_size = htobe32(data_size); | |
1d4dfdef | 858 | data_hdr.padding_size = htobe32(padding); |
c35f9726 | 859 | |
39df6d9f DG |
860 | /* |
861 | * Note that net_seq_num below is assigned with the *current* value of | |
862 | * next_net_seq_num and only after that the next_net_seq_num will be | |
863 | * increment. This is why when issuing a command on the relayd using | |
864 | * this next value, 1 should always be substracted in order to compare | |
865 | * the last seen sequence number on the relayd side to the last sent. | |
866 | */ | |
3604f373 | 867 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num); |
00e2e675 DG |
868 | /* Other fields are zeroed previously */ |
869 | ||
870 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, | |
871 | sizeof(data_hdr)); | |
872 | if (ret < 0) { | |
873 | goto error; | |
874 | } | |
875 | ||
3604f373 DG |
876 | ++stream->next_net_seq_num; |
877 | ||
00e2e675 | 878 | /* Set to go on data socket */ |
6151a90f | 879 | outfd = relayd->data_sock.sock.fd; |
00e2e675 DG |
880 | } |
881 | ||
882 | error: | |
883 | return outfd; | |
884 | } | |
885 | ||
d2956687 JG |
886 | /* |
887 | * Trigger a dump of the metadata content. Following/during the succesful | |
888 | * completion of this call, the metadata poll thread will start receiving | |
889 | * metadata packets to consume. | |
890 | * | |
891 | * The caller must hold the channel and stream locks. | |
892 | */ | |
893 | static | |
894 | int consumer_metadata_stream_dump(struct lttng_consumer_stream *stream) | |
895 | { | |
896 | int ret; | |
897 | ||
898 | ASSERT_LOCKED(stream->chan->lock); | |
899 | ASSERT_LOCKED(stream->lock); | |
900 | assert(stream->metadata_flag); | |
901 | assert(stream->chan->trace_chunk); | |
902 | ||
903 | switch (consumer_data.type) { | |
904 | case LTTNG_CONSUMER_KERNEL: | |
905 | /* | |
906 | * Reset the position of what has been read from the | |
907 | * metadata cache to 0 so we can dump it again. | |
908 | */ | |
909 | ret = kernctl_metadata_cache_dump(stream->wait_fd); | |
910 | break; | |
911 | case LTTNG_CONSUMER32_UST: | |
912 | case LTTNG_CONSUMER64_UST: | |
913 | /* | |
914 | * Reset the position pushed from the metadata cache so it | |
915 | * will write from the beginning on the next push. | |
916 | */ | |
917 | stream->ust_metadata_pushed = 0; | |
918 | ret = consumer_metadata_wakeup_pipe(stream->chan); | |
919 | break; | |
920 | default: | |
921 | ERR("Unknown consumer_data type"); | |
922 | abort(); | |
923 | } | |
924 | if (ret < 0) { | |
925 | ERR("Failed to dump the metadata cache"); | |
926 | } | |
927 | return ret; | |
928 | } | |
929 | ||
930 | static | |
931 | int lttng_consumer_channel_set_trace_chunk( | |
932 | struct lttng_consumer_channel *channel, | |
933 | struct lttng_trace_chunk *new_trace_chunk) | |
934 | { | |
d2956687 | 935 | pthread_mutex_lock(&channel->lock); |
b6921a17 JG |
936 | if (channel->is_deleted) { |
937 | /* | |
938 | * The channel has been logically deleted and should no longer | |
939 | * be used. It has released its reference to its current trace | |
940 | * chunk and should not acquire a new one. | |
941 | * | |
942 | * Return success as there is nothing for the caller to do. | |
943 | */ | |
944 | goto end; | |
945 | } | |
d2956687 JG |
946 | |
947 | /* | |
948 | * The acquisition of the reference cannot fail (barring | |
949 | * a severe internal error) since a reference to the published | |
950 | * chunk is already held by the caller. | |
951 | */ | |
952 | if (new_trace_chunk) { | |
953 | const bool acquired_reference = lttng_trace_chunk_get( | |
954 | new_trace_chunk); | |
955 | ||
956 | assert(acquired_reference); | |
957 | } | |
958 | ||
959 | lttng_trace_chunk_put(channel->trace_chunk); | |
960 | channel->trace_chunk = new_trace_chunk; | |
d2956687 JG |
961 | end: |
962 | pthread_mutex_unlock(&channel->lock); | |
ce1aa6fe | 963 | return 0; |
d2956687 JG |
964 | } |
965 | ||
3bd1e081 | 966 | /* |
ffe60014 DG |
967 | * Allocate and return a new lttng_consumer_channel object using the given key |
968 | * to initialize the hash table node. | |
969 | * | |
970 | * On error, return NULL. | |
3bd1e081 | 971 | */ |
886224ff | 972 | struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, |
ffe60014 | 973 | uint64_t session_id, |
d2956687 | 974 | const uint64_t *chunk_id, |
ffe60014 DG |
975 | const char *pathname, |
976 | const char *name, | |
57a269f2 | 977 | uint64_t relayd_id, |
1624d5b7 JD |
978 | enum lttng_event_output output, |
979 | uint64_t tracefile_size, | |
2bba9e53 | 980 | uint64_t tracefile_count, |
1950109e | 981 | uint64_t session_id_per_pid, |
ecc48a90 | 982 | unsigned int monitor, |
d7ba1388 | 983 | unsigned int live_timer_interval, |
ee8935c3 | 984 | bool is_in_live_session, |
3d071855 | 985 | const char *root_shm_path, |
d7ba1388 | 986 | const char *shm_path) |
3bd1e081 | 987 | { |
d2956687 JG |
988 | struct lttng_consumer_channel *channel = NULL; |
989 | struct lttng_trace_chunk *trace_chunk = NULL; | |
990 | ||
991 | if (chunk_id) { | |
992 | trace_chunk = lttng_trace_chunk_registry_find_chunk( | |
993 | consumer_data.chunk_registry, session_id, | |
994 | *chunk_id); | |
995 | if (!trace_chunk) { | |
996 | ERR("Failed to find trace chunk reference during creation of channel"); | |
997 | goto end; | |
998 | } | |
999 | } | |
3bd1e081 | 1000 | |
276b26d1 | 1001 | channel = zmalloc(sizeof(*channel)); |
3bd1e081 | 1002 | if (channel == NULL) { |
7a57cf92 | 1003 | PERROR("malloc struct lttng_consumer_channel"); |
3bd1e081 MD |
1004 | goto end; |
1005 | } | |
ffe60014 DG |
1006 | |
1007 | channel->key = key; | |
3bd1e081 | 1008 | channel->refcount = 0; |
ffe60014 | 1009 | channel->session_id = session_id; |
1950109e | 1010 | channel->session_id_per_pid = session_id_per_pid; |
ffe60014 | 1011 | channel->relayd_id = relayd_id; |
1624d5b7 JD |
1012 | channel->tracefile_size = tracefile_size; |
1013 | channel->tracefile_count = tracefile_count; | |
2bba9e53 | 1014 | channel->monitor = monitor; |
ecc48a90 | 1015 | channel->live_timer_interval = live_timer_interval; |
ee8935c3 | 1016 | channel->is_live = is_in_live_session; |
a9838785 | 1017 | pthread_mutex_init(&channel->lock, NULL); |
ec6ea7d0 | 1018 | pthread_mutex_init(&channel->timer_lock, NULL); |
ffe60014 | 1019 | |
0c759fc9 DG |
1020 | switch (output) { |
1021 | case LTTNG_EVENT_SPLICE: | |
1022 | channel->output = CONSUMER_CHANNEL_SPLICE; | |
1023 | break; | |
1024 | case LTTNG_EVENT_MMAP: | |
1025 | channel->output = CONSUMER_CHANNEL_MMAP; | |
1026 | break; | |
1027 | default: | |
1028 | assert(0); | |
1029 | free(channel); | |
1030 | channel = NULL; | |
1031 | goto end; | |
1032 | } | |
1033 | ||
07b86b52 JD |
1034 | /* |
1035 | * In monitor mode, the streams associated with the channel will be put in | |
1036 | * a special list ONLY owned by this channel. So, the refcount is set to 1 | |
1037 | * here meaning that the channel itself has streams that are referenced. | |
1038 | * | |
1039 | * On a channel deletion, once the channel is no longer visible, the | |
1040 | * refcount is decremented and checked for a zero value to delete it. With | |
1041 | * streams in no monitor mode, it will now be safe to destroy the channel. | |
1042 | */ | |
1043 | if (!channel->monitor) { | |
1044 | channel->refcount = 1; | |
1045 | } | |
1046 | ||
ffe60014 DG |
1047 | strncpy(channel->pathname, pathname, sizeof(channel->pathname)); |
1048 | channel->pathname[sizeof(channel->pathname) - 1] = '\0'; | |
1049 | ||
1050 | strncpy(channel->name, name, sizeof(channel->name)); | |
1051 | channel->name[sizeof(channel->name) - 1] = '\0'; | |
1052 | ||
3d071855 MD |
1053 | if (root_shm_path) { |
1054 | strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path)); | |
1055 | channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0'; | |
1056 | } | |
d7ba1388 MD |
1057 | if (shm_path) { |
1058 | strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path)); | |
1059 | channel->shm_path[sizeof(channel->shm_path) - 1] = '\0'; | |
1060 | } | |
1061 | ||
d88aee68 | 1062 | lttng_ht_node_init_u64(&channel->node, channel->key); |
5c3892a6 JG |
1063 | lttng_ht_node_init_u64(&channel->channels_by_session_id_ht_node, |
1064 | channel->session_id); | |
d8ef542d MD |
1065 | |
1066 | channel->wait_fd = -1; | |
ffe60014 DG |
1067 | CDS_INIT_LIST_HEAD(&channel->streams.head); |
1068 | ||
d2956687 JG |
1069 | if (trace_chunk) { |
1070 | int ret = lttng_consumer_channel_set_trace_chunk(channel, | |
1071 | trace_chunk); | |
1072 | if (ret) { | |
1073 | goto error; | |
1074 | } | |
1075 | } | |
1076 | ||
62a7b8ed | 1077 | DBG("Allocated channel (key %" PRIu64 ")", channel->key); |
3bd1e081 | 1078 | |
3bd1e081 | 1079 | end: |
d2956687 | 1080 | lttng_trace_chunk_put(trace_chunk); |
3bd1e081 | 1081 | return channel; |
d2956687 JG |
1082 | error: |
1083 | consumer_del_channel(channel); | |
1084 | channel = NULL; | |
1085 | goto end; | |
3bd1e081 MD |
1086 | } |
1087 | ||
1088 | /* | |
1089 | * Add a channel to the global list protected by a mutex. | |
821fffb2 | 1090 | * |
b5a6470f | 1091 | * Always return 0 indicating success. |
3bd1e081 | 1092 | */ |
d8ef542d MD |
1093 | int consumer_add_channel(struct lttng_consumer_channel *channel, |
1094 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 | 1095 | { |
3bd1e081 | 1096 | pthread_mutex_lock(&consumer_data.lock); |
a9838785 | 1097 | pthread_mutex_lock(&channel->lock); |
ec6ea7d0 | 1098 | pthread_mutex_lock(&channel->timer_lock); |
c77fc10a | 1099 | |
b5a6470f DG |
1100 | /* |
1101 | * This gives us a guarantee that the channel we are about to add to the | |
1102 | * channel hash table will be unique. See this function comment on the why | |
1103 | * we need to steel the channel key at this stage. | |
1104 | */ | |
1105 | steal_channel_key(channel->key); | |
c77fc10a | 1106 | |
b5a6470f | 1107 | rcu_read_lock(); |
d88aee68 | 1108 | lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node); |
5c3892a6 JG |
1109 | lttng_ht_add_u64(consumer_data.channels_by_session_id_ht, |
1110 | &channel->channels_by_session_id_ht_node); | |
6065ceec | 1111 | rcu_read_unlock(); |
d2956687 | 1112 | channel->is_published = true; |
b5a6470f | 1113 | |
ec6ea7d0 | 1114 | pthread_mutex_unlock(&channel->timer_lock); |
a9838785 | 1115 | pthread_mutex_unlock(&channel->lock); |
3bd1e081 | 1116 | pthread_mutex_unlock(&consumer_data.lock); |
702b1ea4 | 1117 | |
b5a6470f | 1118 | if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) { |
a0cbdd2e | 1119 | notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD); |
d8ef542d | 1120 | } |
b5a6470f DG |
1121 | |
1122 | return 0; | |
3bd1e081 MD |
1123 | } |
1124 | ||
1125 | /* | |
1126 | * Allocate the pollfd structure and the local view of the out fds to avoid | |
1127 | * doing a lookup in the linked list and concurrency issues when writing is | |
1128 | * needed. Called with consumer_data.lock held. | |
1129 | * | |
1130 | * Returns the number of fds in the structures. | |
1131 | */ | |
ffe60014 DG |
1132 | static int update_poll_array(struct lttng_consumer_local_data *ctx, |
1133 | struct pollfd **pollfd, struct lttng_consumer_stream **local_stream, | |
9a2fcf78 | 1134 | struct lttng_ht *ht, int *nb_inactive_fd) |
3bd1e081 | 1135 | { |
3bd1e081 | 1136 | int i = 0; |
e4421fec DG |
1137 | struct lttng_ht_iter iter; |
1138 | struct lttng_consumer_stream *stream; | |
3bd1e081 | 1139 | |
ffe60014 DG |
1140 | assert(ctx); |
1141 | assert(ht); | |
1142 | assert(pollfd); | |
1143 | assert(local_stream); | |
1144 | ||
3bd1e081 | 1145 | DBG("Updating poll fd array"); |
9a2fcf78 | 1146 | *nb_inactive_fd = 0; |
481d6c57 | 1147 | rcu_read_lock(); |
43c34bc3 | 1148 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
8994307f DG |
1149 | /* |
1150 | * Only active streams with an active end point can be added to the | |
1151 | * poll set and local stream storage of the thread. | |
1152 | * | |
1153 | * There is a potential race here for endpoint_status to be updated | |
1154 | * just after the check. However, this is OK since the stream(s) will | |
1155 | * be deleted once the thread is notified that the end point state has | |
1156 | * changed where this function will be called back again. | |
9a2fcf78 JD |
1157 | * |
1158 | * We track the number of inactive FDs because they still need to be | |
1159 | * closed by the polling thread after a wakeup on the data_pipe or | |
1160 | * metadata_pipe. | |
8994307f | 1161 | */ |
d2956687 | 1162 | if (stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) { |
9a2fcf78 | 1163 | (*nb_inactive_fd)++; |
3bd1e081 MD |
1164 | continue; |
1165 | } | |
7972aab2 DG |
1166 | /* |
1167 | * This clobbers way too much the debug output. Uncomment that if you | |
1168 | * need it for debugging purposes. | |
7972aab2 | 1169 | */ |
e4421fec | 1170 | (*pollfd)[i].fd = stream->wait_fd; |
3bd1e081 | 1171 | (*pollfd)[i].events = POLLIN | POLLPRI; |
e4421fec | 1172 | local_stream[i] = stream; |
3bd1e081 MD |
1173 | i++; |
1174 | } | |
481d6c57 | 1175 | rcu_read_unlock(); |
3bd1e081 MD |
1176 | |
1177 | /* | |
50f8ae69 | 1178 | * Insert the consumer_data_pipe at the end of the array and don't |
3bd1e081 MD |
1179 | * increment i so nb_fd is the number of real FD. |
1180 | */ | |
acdb9057 | 1181 | (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe); |
509bb1cf | 1182 | (*pollfd)[i].events = POLLIN | POLLPRI; |
02b3d176 DG |
1183 | |
1184 | (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe); | |
1185 | (*pollfd)[i + 1].events = POLLIN | POLLPRI; | |
3bd1e081 MD |
1186 | return i; |
1187 | } | |
1188 | ||
1189 | /* | |
84382d49 MD |
1190 | * Poll on the should_quit pipe and the command socket return -1 on |
1191 | * error, 1 if should exit, 0 if data is available on the command socket | |
3bd1e081 MD |
1192 | */ |
1193 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) | |
1194 | { | |
1195 | int num_rdy; | |
1196 | ||
88f2b785 | 1197 | restart: |
3bd1e081 MD |
1198 | num_rdy = poll(consumer_sockpoll, 2, -1); |
1199 | if (num_rdy == -1) { | |
88f2b785 MD |
1200 | /* |
1201 | * Restart interrupted system call. | |
1202 | */ | |
1203 | if (errno == EINTR) { | |
1204 | goto restart; | |
1205 | } | |
7a57cf92 | 1206 | PERROR("Poll error"); |
84382d49 | 1207 | return -1; |
3bd1e081 | 1208 | } |
509bb1cf | 1209 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
3bd1e081 | 1210 | DBG("consumer_should_quit wake up"); |
84382d49 | 1211 | return 1; |
3bd1e081 MD |
1212 | } |
1213 | return 0; | |
3bd1e081 MD |
1214 | } |
1215 | ||
1216 | /* | |
1217 | * Set the error socket. | |
1218 | */ | |
ffe60014 DG |
1219 | void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx, |
1220 | int sock) | |
3bd1e081 MD |
1221 | { |
1222 | ctx->consumer_error_socket = sock; | |
1223 | } | |
1224 | ||
1225 | /* | |
1226 | * Set the command socket path. | |
1227 | */ | |
3bd1e081 MD |
1228 | void lttng_consumer_set_command_sock_path( |
1229 | struct lttng_consumer_local_data *ctx, char *sock) | |
1230 | { | |
1231 | ctx->consumer_command_sock_path = sock; | |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * Send return code to the session daemon. | |
1236 | * If the socket is not defined, we return 0, it is not a fatal error | |
1237 | */ | |
ffe60014 | 1238 | int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd) |
3bd1e081 MD |
1239 | { |
1240 | if (ctx->consumer_error_socket > 0) { | |
1241 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, | |
1242 | sizeof(enum lttcomm_sessiond_command)); | |
1243 | } | |
1244 | ||
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | /* | |
228b5bf7 DG |
1249 | * Close all the tracefiles and stream fds and MUST be called when all |
1250 | * instances are destroyed i.e. when all threads were joined and are ended. | |
3bd1e081 MD |
1251 | */ |
1252 | void lttng_consumer_cleanup(void) | |
1253 | { | |
e4421fec | 1254 | struct lttng_ht_iter iter; |
ffe60014 | 1255 | struct lttng_consumer_channel *channel; |
e10aec8f | 1256 | unsigned int trace_chunks_left; |
6065ceec DG |
1257 | |
1258 | rcu_read_lock(); | |
3bd1e081 | 1259 | |
ffe60014 DG |
1260 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel, |
1261 | node.node) { | |
702b1ea4 | 1262 | consumer_del_channel(channel); |
3bd1e081 | 1263 | } |
6065ceec DG |
1264 | |
1265 | rcu_read_unlock(); | |
d6ce1df2 | 1266 | |
d6ce1df2 | 1267 | lttng_ht_destroy(consumer_data.channel_ht); |
5c3892a6 | 1268 | lttng_ht_destroy(consumer_data.channels_by_session_id_ht); |
228b5bf7 DG |
1269 | |
1270 | cleanup_relayd_ht(); | |
1271 | ||
d8ef542d MD |
1272 | lttng_ht_destroy(consumer_data.stream_per_chan_id_ht); |
1273 | ||
228b5bf7 DG |
1274 | /* |
1275 | * This HT contains streams that are freed by either the metadata thread or | |
1276 | * the data thread so we do *nothing* on the hash table and simply destroy | |
1277 | * it. | |
1278 | */ | |
1279 | lttng_ht_destroy(consumer_data.stream_list_ht); | |
28cc88f3 | 1280 | |
e10aec8f MD |
1281 | /* |
1282 | * Trace chunks in the registry may still exist if the session | |
1283 | * daemon has encountered an internal error and could not | |
1284 | * tear down its sessions and/or trace chunks properly. | |
1285 | * | |
1286 | * Release the session daemon's implicit reference to any remaining | |
1287 | * trace chunk and print an error if any trace chunk was found. Note | |
1288 | * that there are _no_ legitimate cases for trace chunks to be left, | |
1289 | * it is a leak. However, it can happen following a crash of the | |
1290 | * session daemon and not emptying the registry would cause an assertion | |
1291 | * to hit. | |
1292 | */ | |
1293 | trace_chunks_left = lttng_trace_chunk_registry_put_each_chunk( | |
1294 | consumer_data.chunk_registry); | |
1295 | if (trace_chunks_left) { | |
1296 | ERR("%u trace chunks are leaked by lttng-consumerd. " | |
1297 | "This can be caused by an internal error of the session daemon.", | |
1298 | trace_chunks_left); | |
1299 | } | |
1300 | /* Run all callbacks freeing each chunk. */ | |
1301 | rcu_barrier(); | |
28cc88f3 | 1302 | lttng_trace_chunk_registry_destroy(consumer_data.chunk_registry); |
3bd1e081 MD |
1303 | } |
1304 | ||
1305 | /* | |
1306 | * Called from signal handler. | |
1307 | */ | |
1308 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) | |
1309 | { | |
6cd525e8 MD |
1310 | ssize_t ret; |
1311 | ||
10211f5c | 1312 | CMM_STORE_SHARED(consumer_quit, 1); |
6cd525e8 MD |
1313 | ret = lttng_write(ctx->consumer_should_quit[1], "4", 1); |
1314 | if (ret < 1) { | |
7a57cf92 | 1315 | PERROR("write consumer quit"); |
3bd1e081 | 1316 | } |
ab1027f4 DG |
1317 | |
1318 | DBG("Consumer flag that it should quit"); | |
3bd1e081 MD |
1319 | } |
1320 | ||
5199ffc4 JG |
1321 | |
1322 | /* | |
1323 | * Flush pending writes to trace output disk file. | |
1324 | */ | |
1325 | static | |
00e2e675 DG |
1326 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
1327 | off_t orig_offset) | |
3bd1e081 | 1328 | { |
c7a78aab | 1329 | int ret; |
3bd1e081 MD |
1330 | int outfd = stream->out_fd; |
1331 | ||
1332 | /* | |
1333 | * This does a blocking write-and-wait on any page that belongs to the | |
1334 | * subbuffer prior to the one we just wrote. | |
1335 | * Don't care about error values, as these are just hints and ways to | |
1336 | * limit the amount of page cache used. | |
1337 | */ | |
ffe60014 | 1338 | if (orig_offset < stream->max_sb_size) { |
3bd1e081 MD |
1339 | return; |
1340 | } | |
ffe60014 DG |
1341 | lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size, |
1342 | stream->max_sb_size, | |
3bd1e081 MD |
1343 | SYNC_FILE_RANGE_WAIT_BEFORE |
1344 | | SYNC_FILE_RANGE_WRITE | |
1345 | | SYNC_FILE_RANGE_WAIT_AFTER); | |
1346 | /* | |
1347 | * Give hints to the kernel about how we access the file: | |
1348 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after | |
1349 | * we write it. | |
1350 | * | |
1351 | * We need to call fadvise again after the file grows because the | |
1352 | * kernel does not seem to apply fadvise to non-existing parts of the | |
1353 | * file. | |
1354 | * | |
1355 | * Call fadvise _after_ having waited for the page writeback to | |
1356 | * complete because the dirty page writeback semantic is not well | |
1357 | * defined. So it can be expected to lead to lower throughput in | |
1358 | * streaming. | |
1359 | */ | |
c7a78aab | 1360 | ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size, |
ffe60014 | 1361 | stream->max_sb_size, POSIX_FADV_DONTNEED); |
a0d0e127 | 1362 | if (ret && ret != -ENOSYS) { |
a74a5f4a JG |
1363 | errno = ret; |
1364 | PERROR("posix_fadvise on fd %i", outfd); | |
c7a78aab | 1365 | } |
3bd1e081 MD |
1366 | } |
1367 | ||
1368 | /* | |
1369 | * Initialise the necessary environnement : | |
1370 | * - create a new context | |
1371 | * - create the poll_pipe | |
1372 | * - create the should_quit pipe (for signal handler) | |
1373 | * - create the thread pipe (for splice) | |
1374 | * | |
1375 | * Takes a function pointer as argument, this function is called when data is | |
1376 | * available on a buffer. This function is responsible to do the | |
1377 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the | |
1378 | * buffer configuration and then kernctl_put_next_subbuf at the end. | |
1379 | * | |
1380 | * Returns a pointer to the new context or NULL on error. | |
1381 | */ | |
1382 | struct lttng_consumer_local_data *lttng_consumer_create( | |
1383 | enum lttng_consumer_type type, | |
4078b776 | 1384 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
bdc8d1bb | 1385 | struct lttng_consumer_local_data *ctx, bool locked_by_caller), |
3bd1e081 MD |
1386 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
1387 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
30319bcb | 1388 | int (*update_stream)(uint64_t stream_key, uint32_t state)) |
3bd1e081 | 1389 | { |
d8ef542d | 1390 | int ret; |
3bd1e081 MD |
1391 | struct lttng_consumer_local_data *ctx; |
1392 | ||
1393 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || | |
1394 | consumer_data.type == type); | |
1395 | consumer_data.type = type; | |
1396 | ||
effcf122 | 1397 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
3bd1e081 | 1398 | if (ctx == NULL) { |
7a57cf92 | 1399 | PERROR("allocating context"); |
3bd1e081 MD |
1400 | goto error; |
1401 | } | |
1402 | ||
1403 | ctx->consumer_error_socket = -1; | |
331744e3 | 1404 | ctx->consumer_metadata_socket = -1; |
75d83e50 | 1405 | pthread_mutex_init(&ctx->metadata_socket_lock, NULL); |
3bd1e081 MD |
1406 | /* assign the callbacks */ |
1407 | ctx->on_buffer_ready = buffer_ready; | |
1408 | ctx->on_recv_channel = recv_channel; | |
1409 | ctx->on_recv_stream = recv_stream; | |
1410 | ctx->on_update_stream = update_stream; | |
1411 | ||
acdb9057 DG |
1412 | ctx->consumer_data_pipe = lttng_pipe_open(0); |
1413 | if (!ctx->consumer_data_pipe) { | |
3bd1e081 MD |
1414 | goto error_poll_pipe; |
1415 | } | |
1416 | ||
02b3d176 DG |
1417 | ctx->consumer_wakeup_pipe = lttng_pipe_open(0); |
1418 | if (!ctx->consumer_wakeup_pipe) { | |
1419 | goto error_wakeup_pipe; | |
1420 | } | |
1421 | ||
3bd1e081 MD |
1422 | ret = pipe(ctx->consumer_should_quit); |
1423 | if (ret < 0) { | |
7a57cf92 | 1424 | PERROR("Error creating recv pipe"); |
3bd1e081 MD |
1425 | goto error_quit_pipe; |
1426 | } | |
1427 | ||
d8ef542d MD |
1428 | ret = pipe(ctx->consumer_channel_pipe); |
1429 | if (ret < 0) { | |
1430 | PERROR("Error creating channel pipe"); | |
1431 | goto error_channel_pipe; | |
1432 | } | |
1433 | ||
13886d2d DG |
1434 | ctx->consumer_metadata_pipe = lttng_pipe_open(0); |
1435 | if (!ctx->consumer_metadata_pipe) { | |
fb3a43a9 DG |
1436 | goto error_metadata_pipe; |
1437 | } | |
3bd1e081 | 1438 | |
e9404c27 JG |
1439 | ctx->channel_monitor_pipe = -1; |
1440 | ||
fb3a43a9 | 1441 | return ctx; |
3bd1e081 | 1442 | |
fb3a43a9 | 1443 | error_metadata_pipe: |
d8ef542d MD |
1444 | utils_close_pipe(ctx->consumer_channel_pipe); |
1445 | error_channel_pipe: | |
d8ef542d | 1446 | utils_close_pipe(ctx->consumer_should_quit); |
3bd1e081 | 1447 | error_quit_pipe: |
02b3d176 DG |
1448 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
1449 | error_wakeup_pipe: | |
acdb9057 | 1450 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
3bd1e081 MD |
1451 | error_poll_pipe: |
1452 | free(ctx); | |
1453 | error: | |
1454 | return NULL; | |
1455 | } | |
1456 | ||
282dadbc MD |
1457 | /* |
1458 | * Iterate over all streams of the hashtable and free them properly. | |
1459 | */ | |
1460 | static void destroy_data_stream_ht(struct lttng_ht *ht) | |
1461 | { | |
1462 | struct lttng_ht_iter iter; | |
1463 | struct lttng_consumer_stream *stream; | |
1464 | ||
1465 | if (ht == NULL) { | |
1466 | return; | |
1467 | } | |
1468 | ||
1469 | rcu_read_lock(); | |
1470 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1471 | /* | |
1472 | * Ignore return value since we are currently cleaning up so any error | |
1473 | * can't be handled. | |
1474 | */ | |
1475 | (void) consumer_del_stream(stream, ht); | |
1476 | } | |
1477 | rcu_read_unlock(); | |
1478 | ||
1479 | lttng_ht_destroy(ht); | |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * Iterate over all streams of the metadata hashtable and free them | |
1484 | * properly. | |
1485 | */ | |
1486 | static void destroy_metadata_stream_ht(struct lttng_ht *ht) | |
1487 | { | |
1488 | struct lttng_ht_iter iter; | |
1489 | struct lttng_consumer_stream *stream; | |
1490 | ||
1491 | if (ht == NULL) { | |
1492 | return; | |
1493 | } | |
1494 | ||
1495 | rcu_read_lock(); | |
1496 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { | |
1497 | /* | |
1498 | * Ignore return value since we are currently cleaning up so any error | |
1499 | * can't be handled. | |
1500 | */ | |
1501 | (void) consumer_del_metadata_stream(stream, ht); | |
1502 | } | |
1503 | rcu_read_unlock(); | |
1504 | ||
1505 | lttng_ht_destroy(ht); | |
1506 | } | |
1507 | ||
3bd1e081 MD |
1508 | /* |
1509 | * Close all fds associated with the instance and free the context. | |
1510 | */ | |
1511 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) | |
1512 | { | |
4c462e79 MD |
1513 | int ret; |
1514 | ||
ab1027f4 DG |
1515 | DBG("Consumer destroying it. Closing everything."); |
1516 | ||
4f2e75b9 DG |
1517 | if (!ctx) { |
1518 | return; | |
1519 | } | |
1520 | ||
282dadbc MD |
1521 | destroy_data_stream_ht(data_ht); |
1522 | destroy_metadata_stream_ht(metadata_ht); | |
1523 | ||
4c462e79 MD |
1524 | ret = close(ctx->consumer_error_socket); |
1525 | if (ret) { | |
1526 | PERROR("close"); | |
1527 | } | |
331744e3 JD |
1528 | ret = close(ctx->consumer_metadata_socket); |
1529 | if (ret) { | |
1530 | PERROR("close"); | |
1531 | } | |
d8ef542d | 1532 | utils_close_pipe(ctx->consumer_channel_pipe); |
acdb9057 | 1533 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
13886d2d | 1534 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
02b3d176 | 1535 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
d8ef542d | 1536 | utils_close_pipe(ctx->consumer_should_quit); |
fb3a43a9 | 1537 | |
3bd1e081 MD |
1538 | unlink(ctx->consumer_command_sock_path); |
1539 | free(ctx); | |
1540 | } | |
1541 | ||
6197aea7 DG |
1542 | /* |
1543 | * Write the metadata stream id on the specified file descriptor. | |
1544 | */ | |
1545 | static int write_relayd_metadata_id(int fd, | |
1546 | struct lttng_consumer_stream *stream, | |
239f61af | 1547 | unsigned long padding) |
6197aea7 | 1548 | { |
6cd525e8 | 1549 | ssize_t ret; |
1d4dfdef | 1550 | struct lttcomm_relayd_metadata_payload hdr; |
6197aea7 | 1551 | |
1d4dfdef DG |
1552 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
1553 | hdr.padding_size = htobe32(padding); | |
6cd525e8 MD |
1554 | ret = lttng_write(fd, (void *) &hdr, sizeof(hdr)); |
1555 | if (ret < sizeof(hdr)) { | |
d7b75ec8 | 1556 | /* |
6f04ed72 | 1557 | * This error means that the fd's end is closed so ignore the PERROR |
d7b75ec8 DG |
1558 | * not to clubber the error output since this can happen in a normal |
1559 | * code path. | |
1560 | */ | |
1561 | if (errno != EPIPE) { | |
1562 | PERROR("write metadata stream id"); | |
1563 | } | |
1564 | DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno); | |
534d2592 DG |
1565 | /* |
1566 | * Set ret to a negative value because if ret != sizeof(hdr), we don't | |
1567 | * handle writting the missing part so report that as an error and | |
1568 | * don't lie to the caller. | |
1569 | */ | |
1570 | ret = -1; | |
6197aea7 DG |
1571 | goto end; |
1572 | } | |
1d4dfdef DG |
1573 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
1574 | stream->relayd_stream_id, padding); | |
6197aea7 DG |
1575 | |
1576 | end: | |
6cd525e8 | 1577 | return (int) ret; |
6197aea7 DG |
1578 | } |
1579 | ||
3bd1e081 | 1580 | /* |
09e26845 DG |
1581 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
1582 | * core function for writing trace buffers to either the local filesystem or | |
1583 | * the network. | |
1584 | * | |
d2956687 | 1585 | * It must be called with the stream and the channel lock held. |
79d4ffb7 | 1586 | * |
09e26845 | 1587 | * Careful review MUST be put if any changes occur! |
3bd1e081 MD |
1588 | * |
1589 | * Returns the number of bytes written | |
1590 | */ | |
4078b776 | 1591 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
276cd1d7 | 1592 | struct lttng_consumer_stream *stream, |
a587bd64 | 1593 | const struct lttng_buffer_view *buffer, |
bdc8d1bb | 1594 | unsigned long padding) |
3bd1e081 | 1595 | { |
994ab360 | 1596 | ssize_t ret = 0; |
f02e1e8a DG |
1597 | off_t orig_offset = stream->out_fd_offset; |
1598 | /* Default is on the disk */ | |
1599 | int outfd = stream->out_fd; | |
f02e1e8a | 1600 | struct consumer_relayd_sock_pair *relayd = NULL; |
8994307f | 1601 | unsigned int relayd_hang_up = 0; |
a587bd64 JG |
1602 | const size_t subbuf_content_size = buffer->size - padding; |
1603 | size_t write_len; | |
f02e1e8a DG |
1604 | |
1605 | /* RCU lock for the relayd pointer */ | |
1606 | rcu_read_lock(); | |
7fd975c5 | 1607 | assert(stream->net_seq_idx != (uint64_t) -1ULL || |
948411cd | 1608 | stream->trace_chunk); |
d2956687 | 1609 | |
f02e1e8a | 1610 | /* Flag that the current stream if set for network streaming. */ |
da009f2c | 1611 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1612 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1613 | if (relayd == NULL) { | |
56591bac | 1614 | ret = -EPIPE; |
f02e1e8a DG |
1615 | goto end; |
1616 | } | |
1617 | } | |
1618 | ||
f02e1e8a DG |
1619 | /* Handle stream on the relayd if the output is on the network */ |
1620 | if (relayd) { | |
a587bd64 | 1621 | unsigned long netlen = subbuf_content_size; |
f02e1e8a DG |
1622 | |
1623 | /* | |
1624 | * Lock the control socket for the complete duration of the function | |
1625 | * since from this point on we will use the socket. | |
1626 | */ | |
1627 | if (stream->metadata_flag) { | |
1628 | /* Metadata requires the control socket. */ | |
1629 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
93ec662e JD |
1630 | if (stream->reset_metadata_flag) { |
1631 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1632 | stream->relayd_stream_id, | |
1633 | stream->metadata_version); | |
1634 | if (ret < 0) { | |
1635 | relayd_hang_up = 1; | |
1636 | goto write_error; | |
1637 | } | |
1638 | stream->reset_metadata_flag = 0; | |
1639 | } | |
1d4dfdef | 1640 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
f02e1e8a DG |
1641 | } |
1642 | ||
1d4dfdef | 1643 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
994ab360 DG |
1644 | if (ret < 0) { |
1645 | relayd_hang_up = 1; | |
1646 | goto write_error; | |
1647 | } | |
1648 | /* Use the returned socket. */ | |
1649 | outfd = ret; | |
f02e1e8a | 1650 | |
994ab360 DG |
1651 | /* Write metadata stream id before payload */ |
1652 | if (stream->metadata_flag) { | |
239f61af | 1653 | ret = write_relayd_metadata_id(outfd, stream, padding); |
994ab360 | 1654 | if (ret < 0) { |
8994307f DG |
1655 | relayd_hang_up = 1; |
1656 | goto write_error; | |
1657 | } | |
f02e1e8a | 1658 | } |
1624d5b7 | 1659 | |
a587bd64 JG |
1660 | write_len = subbuf_content_size; |
1661 | } else { | |
1662 | /* No streaming; we have to write the full padding. */ | |
93ec662e JD |
1663 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1664 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1665 | if (ret < 0) { | |
1666 | ERR("Reset metadata file"); | |
1667 | goto end; | |
1668 | } | |
1669 | stream->reset_metadata_flag = 0; | |
1670 | } | |
1671 | ||
1624d5b7 JD |
1672 | /* |
1673 | * Check if we need to change the tracefile before writing the packet. | |
1674 | */ | |
1675 | if (stream->chan->tracefile_size > 0 && | |
a587bd64 | 1676 | (stream->tracefile_size_current + buffer->size) > |
1624d5b7 | 1677 | stream->chan->tracefile_size) { |
d2956687 JG |
1678 | ret = consumer_stream_rotate_output_files(stream); |
1679 | if (ret) { | |
1624d5b7 JD |
1680 | goto end; |
1681 | } | |
309167d2 | 1682 | outfd = stream->out_fd; |
a1ae300f | 1683 | orig_offset = 0; |
1624d5b7 | 1684 | } |
a587bd64 | 1685 | stream->tracefile_size_current += buffer->size; |
a587bd64 | 1686 | write_len = buffer->size; |
f02e1e8a DG |
1687 | } |
1688 | ||
d02b8372 DG |
1689 | /* |
1690 | * This call guarantee that len or less is returned. It's impossible to | |
1691 | * receive a ret value that is bigger than len. | |
1692 | */ | |
a587bd64 | 1693 | ret = lttng_write(outfd, buffer->data, write_len); |
63bacd79 | 1694 | DBG("Consumer mmap write() ret %zd (len %zu)", ret, write_len); |
a587bd64 | 1695 | if (ret < 0 || ((size_t) ret != write_len)) { |
d02b8372 DG |
1696 | /* |
1697 | * Report error to caller if nothing was written else at least send the | |
1698 | * amount written. | |
1699 | */ | |
1700 | if (ret < 0) { | |
994ab360 | 1701 | ret = -errno; |
f02e1e8a | 1702 | } |
994ab360 | 1703 | relayd_hang_up = 1; |
f02e1e8a | 1704 | |
d02b8372 | 1705 | /* Socket operation failed. We consider the relayd dead */ |
fcf0f774 | 1706 | if (errno == EPIPE) { |
d02b8372 DG |
1707 | /* |
1708 | * This is possible if the fd is closed on the other side | |
1709 | * (outfd) or any write problem. It can be verbose a bit for a | |
1710 | * normal execution if for instance the relayd is stopped | |
1711 | * abruptly. This can happen so set this to a DBG statement. | |
1712 | */ | |
1713 | DBG("Consumer mmap write detected relayd hang up"); | |
994ab360 DG |
1714 | } else { |
1715 | /* Unhandled error, print it and stop function right now. */ | |
a587bd64 JG |
1716 | PERROR("Error in write mmap (ret %zd != write_len %zu)", ret, |
1717 | write_len); | |
f02e1e8a | 1718 | } |
994ab360 | 1719 | goto write_error; |
d02b8372 DG |
1720 | } |
1721 | stream->output_written += ret; | |
d02b8372 DG |
1722 | |
1723 | /* This call is useless on a socket so better save a syscall. */ | |
1724 | if (!relayd) { | |
1725 | /* This won't block, but will start writeout asynchronously */ | |
a587bd64 | 1726 | lttng_sync_file_range(outfd, stream->out_fd_offset, write_len, |
d02b8372 | 1727 | SYNC_FILE_RANGE_WRITE); |
a587bd64 | 1728 | stream->out_fd_offset += write_len; |
f5dbe415 | 1729 | lttng_consumer_sync_trace_file(stream, orig_offset); |
f02e1e8a | 1730 | } |
f02e1e8a | 1731 | |
8994307f DG |
1732 | write_error: |
1733 | /* | |
1734 | * This is a special case that the relayd has closed its socket. Let's | |
1735 | * cleanup the relayd object and all associated streams. | |
1736 | */ | |
1737 | if (relayd && relayd_hang_up) { | |
9276e5c8 JR |
1738 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
1739 | lttng_consumer_cleanup_relayd(relayd); | |
8994307f DG |
1740 | } |
1741 | ||
f02e1e8a DG |
1742 | end: |
1743 | /* Unlock only if ctrl socket used */ | |
1744 | if (relayd && stream->metadata_flag) { | |
1745 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1746 | } | |
1747 | ||
1748 | rcu_read_unlock(); | |
994ab360 | 1749 | return ret; |
3bd1e081 MD |
1750 | } |
1751 | ||
1752 | /* | |
1753 | * Splice the data from the ring buffer to the tracefile. | |
1754 | * | |
79d4ffb7 DG |
1755 | * It must be called with the stream lock held. |
1756 | * | |
3bd1e081 MD |
1757 | * Returns the number of bytes spliced. |
1758 | */ | |
4078b776 | 1759 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 | 1760 | struct lttng_consumer_local_data *ctx, |
1d4dfdef | 1761 | struct lttng_consumer_stream *stream, unsigned long len, |
bdc8d1bb | 1762 | unsigned long padding) |
3bd1e081 | 1763 | { |
f02e1e8a DG |
1764 | ssize_t ret = 0, written = 0, ret_splice = 0; |
1765 | loff_t offset = 0; | |
1766 | off_t orig_offset = stream->out_fd_offset; | |
1767 | int fd = stream->wait_fd; | |
1768 | /* Default is on the disk */ | |
1769 | int outfd = stream->out_fd; | |
f02e1e8a | 1770 | struct consumer_relayd_sock_pair *relayd = NULL; |
fb3a43a9 | 1771 | int *splice_pipe; |
8994307f | 1772 | unsigned int relayd_hang_up = 0; |
f02e1e8a | 1773 | |
3bd1e081 MD |
1774 | switch (consumer_data.type) { |
1775 | case LTTNG_CONSUMER_KERNEL: | |
f02e1e8a | 1776 | break; |
7753dea8 MD |
1777 | case LTTNG_CONSUMER32_UST: |
1778 | case LTTNG_CONSUMER64_UST: | |
f02e1e8a | 1779 | /* Not supported for user space tracing */ |
3bd1e081 MD |
1780 | return -ENOSYS; |
1781 | default: | |
1782 | ERR("Unknown consumer_data type"); | |
1783 | assert(0); | |
3bd1e081 MD |
1784 | } |
1785 | ||
f02e1e8a DG |
1786 | /* RCU lock for the relayd pointer */ |
1787 | rcu_read_lock(); | |
1788 | ||
1789 | /* Flag that the current stream if set for network streaming. */ | |
da009f2c | 1790 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
f02e1e8a DG |
1791 | relayd = consumer_find_relayd(stream->net_seq_idx); |
1792 | if (relayd == NULL) { | |
ad0b0d23 | 1793 | written = -ret; |
f02e1e8a DG |
1794 | goto end; |
1795 | } | |
1796 | } | |
a2361a61 | 1797 | splice_pipe = stream->splice_pipe; |
fb3a43a9 | 1798 | |
f02e1e8a | 1799 | /* Write metadata stream id before payload */ |
1d4dfdef | 1800 | if (relayd) { |
ad0b0d23 | 1801 | unsigned long total_len = len; |
f02e1e8a | 1802 | |
1d4dfdef DG |
1803 | if (stream->metadata_flag) { |
1804 | /* | |
1805 | * Lock the control socket for the complete duration of the function | |
1806 | * since from this point on we will use the socket. | |
1807 | */ | |
1808 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
1809 | ||
93ec662e JD |
1810 | if (stream->reset_metadata_flag) { |
1811 | ret = relayd_reset_metadata(&relayd->control_sock, | |
1812 | stream->relayd_stream_id, | |
1813 | stream->metadata_version); | |
1814 | if (ret < 0) { | |
1815 | relayd_hang_up = 1; | |
1816 | goto write_error; | |
1817 | } | |
1818 | stream->reset_metadata_flag = 0; | |
1819 | } | |
239f61af | 1820 | ret = write_relayd_metadata_id(splice_pipe[1], stream, |
1d4dfdef DG |
1821 | padding); |
1822 | if (ret < 0) { | |
1823 | written = ret; | |
ad0b0d23 DG |
1824 | relayd_hang_up = 1; |
1825 | goto write_error; | |
1d4dfdef DG |
1826 | } |
1827 | ||
1828 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); | |
1829 | } | |
1830 | ||
1831 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); | |
ad0b0d23 DG |
1832 | if (ret < 0) { |
1833 | written = ret; | |
1834 | relayd_hang_up = 1; | |
1835 | goto write_error; | |
f02e1e8a | 1836 | } |
ad0b0d23 DG |
1837 | /* Use the returned socket. */ |
1838 | outfd = ret; | |
1d4dfdef DG |
1839 | } else { |
1840 | /* No streaming, we have to set the len with the full padding */ | |
1841 | len += padding; | |
1624d5b7 | 1842 | |
93ec662e JD |
1843 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
1844 | ret = utils_truncate_stream_file(stream->out_fd, 0); | |
1845 | if (ret < 0) { | |
1846 | ERR("Reset metadata file"); | |
1847 | goto end; | |
1848 | } | |
1849 | stream->reset_metadata_flag = 0; | |
1850 | } | |
1624d5b7 JD |
1851 | /* |
1852 | * Check if we need to change the tracefile before writing the packet. | |
1853 | */ | |
1854 | if (stream->chan->tracefile_size > 0 && | |
1855 | (stream->tracefile_size_current + len) > | |
1856 | stream->chan->tracefile_size) { | |
d2956687 | 1857 | ret = consumer_stream_rotate_output_files(stream); |
1624d5b7 | 1858 | if (ret < 0) { |
ad0b0d23 | 1859 | written = ret; |
1624d5b7 JD |
1860 | goto end; |
1861 | } | |
309167d2 | 1862 | outfd = stream->out_fd; |
a1ae300f | 1863 | orig_offset = 0; |
1624d5b7 JD |
1864 | } |
1865 | stream->tracefile_size_current += len; | |
f02e1e8a DG |
1866 | } |
1867 | ||
1868 | while (len > 0) { | |
1d4dfdef DG |
1869 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
1870 | (unsigned long)offset, len, fd, splice_pipe[1]); | |
fb3a43a9 | 1871 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
f02e1e8a DG |
1872 | SPLICE_F_MOVE | SPLICE_F_MORE); |
1873 | DBG("splice chan to pipe, ret %zd", ret_splice); | |
1874 | if (ret_splice < 0) { | |
d02b8372 | 1875 | ret = errno; |
ad0b0d23 | 1876 | written = -ret; |
d02b8372 | 1877 | PERROR("Error in relay splice"); |
f02e1e8a DG |
1878 | goto splice_error; |
1879 | } | |
1880 | ||
1881 | /* Handle stream on the relayd if the output is on the network */ | |
ad0b0d23 DG |
1882 | if (relayd && stream->metadata_flag) { |
1883 | size_t metadata_payload_size = | |
1884 | sizeof(struct lttcomm_relayd_metadata_payload); | |
1885 | ||
1886 | /* Update counter to fit the spliced data */ | |
1887 | ret_splice += metadata_payload_size; | |
1888 | len += metadata_payload_size; | |
1889 | /* | |
1890 | * We do this so the return value can match the len passed as | |
1891 | * argument to this function. | |
1892 | */ | |
1893 | written -= metadata_payload_size; | |
f02e1e8a DG |
1894 | } |
1895 | ||
1896 | /* Splice data out */ | |
fb3a43a9 | 1897 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
f02e1e8a | 1898 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
a2361a61 JD |
1899 | DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", |
1900 | outfd, ret_splice); | |
f02e1e8a | 1901 | if (ret_splice < 0) { |
d02b8372 | 1902 | ret = errno; |
ad0b0d23 DG |
1903 | written = -ret; |
1904 | relayd_hang_up = 1; | |
1905 | goto write_error; | |
f02e1e8a | 1906 | } else if (ret_splice > len) { |
d02b8372 DG |
1907 | /* |
1908 | * We don't expect this code path to be executed but you never know | |
1909 | * so this is an extra protection agains a buggy splice(). | |
1910 | */ | |
f02e1e8a | 1911 | ret = errno; |
ad0b0d23 | 1912 | written += ret_splice; |
d02b8372 DG |
1913 | PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice, |
1914 | len); | |
f02e1e8a | 1915 | goto splice_error; |
d02b8372 DG |
1916 | } else { |
1917 | /* All good, update current len and continue. */ | |
1918 | len -= ret_splice; | |
f02e1e8a | 1919 | } |
f02e1e8a DG |
1920 | |
1921 | /* This call is useless on a socket so better save a syscall. */ | |
1922 | if (!relayd) { | |
1923 | /* This won't block, but will start writeout asynchronously */ | |
1924 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, | |
1925 | SYNC_FILE_RANGE_WRITE); | |
1926 | stream->out_fd_offset += ret_splice; | |
1927 | } | |
e5d1a9b3 | 1928 | stream->output_written += ret_splice; |
f02e1e8a DG |
1929 | written += ret_splice; |
1930 | } | |
f5dbe415 JG |
1931 | if (!relayd) { |
1932 | lttng_consumer_sync_trace_file(stream, orig_offset); | |
1933 | } | |
f02e1e8a DG |
1934 | goto end; |
1935 | ||
8994307f DG |
1936 | write_error: |
1937 | /* | |
1938 | * This is a special case that the relayd has closed its socket. Let's | |
1939 | * cleanup the relayd object and all associated streams. | |
1940 | */ | |
1941 | if (relayd && relayd_hang_up) { | |
9276e5c8 JR |
1942 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
1943 | lttng_consumer_cleanup_relayd(relayd); | |
8994307f DG |
1944 | /* Skip splice error so the consumer does not fail */ |
1945 | goto end; | |
1946 | } | |
1947 | ||
f02e1e8a DG |
1948 | splice_error: |
1949 | /* send the appropriate error description to sessiond */ | |
1950 | switch (ret) { | |
f02e1e8a | 1951 | case EINVAL: |
f73fabfd | 1952 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
f02e1e8a DG |
1953 | break; |
1954 | case ENOMEM: | |
f73fabfd | 1955 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
f02e1e8a DG |
1956 | break; |
1957 | case ESPIPE: | |
f73fabfd | 1958 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
f02e1e8a DG |
1959 | break; |
1960 | } | |
1961 | ||
1962 | end: | |
1963 | if (relayd && stream->metadata_flag) { | |
1964 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
1965 | } | |
1966 | ||
1967 | rcu_read_unlock(); | |
1968 | return written; | |
3bd1e081 MD |
1969 | } |
1970 | ||
15055ce5 JD |
1971 | /* |
1972 | * Sample the snapshot positions for a specific fd | |
1973 | * | |
1974 | * Returns 0 on success, < 0 on error | |
1975 | */ | |
1976 | int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream *stream) | |
1977 | { | |
1978 | switch (consumer_data.type) { | |
1979 | case LTTNG_CONSUMER_KERNEL: | |
1980 | return lttng_kconsumer_sample_snapshot_positions(stream); | |
1981 | case LTTNG_CONSUMER32_UST: | |
1982 | case LTTNG_CONSUMER64_UST: | |
1983 | return lttng_ustconsumer_sample_snapshot_positions(stream); | |
1984 | default: | |
1985 | ERR("Unknown consumer_data type"); | |
1986 | assert(0); | |
1987 | return -ENOSYS; | |
1988 | } | |
1989 | } | |
3bd1e081 MD |
1990 | /* |
1991 | * Take a snapshot for a specific fd | |
1992 | * | |
1993 | * Returns 0 on success, < 0 on error | |
1994 | */ | |
ffe60014 | 1995 | int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
1996 | { |
1997 | switch (consumer_data.type) { | |
1998 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 1999 | return lttng_kconsumer_take_snapshot(stream); |
7753dea8 MD |
2000 | case LTTNG_CONSUMER32_UST: |
2001 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 2002 | return lttng_ustconsumer_take_snapshot(stream); |
3bd1e081 MD |
2003 | default: |
2004 | ERR("Unknown consumer_data type"); | |
2005 | assert(0); | |
2006 | return -ENOSYS; | |
2007 | } | |
3bd1e081 MD |
2008 | } |
2009 | ||
2010 | /* | |
2011 | * Get the produced position | |
2012 | * | |
2013 | * Returns 0 on success, < 0 on error | |
2014 | */ | |
ffe60014 | 2015 | int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
2016 | unsigned long *pos) |
2017 | { | |
2018 | switch (consumer_data.type) { | |
2019 | case LTTNG_CONSUMER_KERNEL: | |
ffe60014 | 2020 | return lttng_kconsumer_get_produced_snapshot(stream, pos); |
7753dea8 MD |
2021 | case LTTNG_CONSUMER32_UST: |
2022 | case LTTNG_CONSUMER64_UST: | |
ffe60014 | 2023 | return lttng_ustconsumer_get_produced_snapshot(stream, pos); |
3bd1e081 MD |
2024 | default: |
2025 | ERR("Unknown consumer_data type"); | |
2026 | assert(0); | |
2027 | return -ENOSYS; | |
2028 | } | |
2029 | } | |
2030 | ||
15055ce5 JD |
2031 | /* |
2032 | * Get the consumed position (free-running counter position in bytes). | |
2033 | * | |
2034 | * Returns 0 on success, < 0 on error | |
2035 | */ | |
2036 | int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
2037 | unsigned long *pos) | |
2038 | { | |
2039 | switch (consumer_data.type) { | |
2040 | case LTTNG_CONSUMER_KERNEL: | |
2041 | return lttng_kconsumer_get_consumed_snapshot(stream, pos); | |
2042 | case LTTNG_CONSUMER32_UST: | |
2043 | case LTTNG_CONSUMER64_UST: | |
2044 | return lttng_ustconsumer_get_consumed_snapshot(stream, pos); | |
2045 | default: | |
2046 | ERR("Unknown consumer_data type"); | |
2047 | assert(0); | |
2048 | return -ENOSYS; | |
2049 | } | |
2050 | } | |
2051 | ||
3bd1e081 MD |
2052 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
2053 | int sock, struct pollfd *consumer_sockpoll) | |
2054 | { | |
2055 | switch (consumer_data.type) { | |
2056 | case LTTNG_CONSUMER_KERNEL: | |
2057 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
7753dea8 MD |
2058 | case LTTNG_CONSUMER32_UST: |
2059 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
2060 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
2061 | default: | |
2062 | ERR("Unknown consumer_data type"); | |
2063 | assert(0); | |
2064 | return -ENOSYS; | |
2065 | } | |
2066 | } | |
2067 | ||
1f8d1c14 | 2068 | static |
6d574024 | 2069 | void lttng_consumer_close_all_metadata(void) |
d88aee68 DG |
2070 | { |
2071 | switch (consumer_data.type) { | |
2072 | case LTTNG_CONSUMER_KERNEL: | |
2073 | /* | |
2074 | * The Kernel consumer has a different metadata scheme so we don't | |
2075 | * close anything because the stream will be closed by the session | |
2076 | * daemon. | |
2077 | */ | |
2078 | break; | |
2079 | case LTTNG_CONSUMER32_UST: | |
2080 | case LTTNG_CONSUMER64_UST: | |
2081 | /* | |
2082 | * Close all metadata streams. The metadata hash table is passed and | |
2083 | * this call iterates over it by closing all wakeup fd. This is safe | |
2084 | * because at this point we are sure that the metadata producer is | |
2085 | * either dead or blocked. | |
2086 | */ | |
6d574024 | 2087 | lttng_ustconsumer_close_all_metadata(metadata_ht); |
d88aee68 DG |
2088 | break; |
2089 | default: | |
2090 | ERR("Unknown consumer_data type"); | |
2091 | assert(0); | |
2092 | } | |
2093 | } | |
2094 | ||
fb3a43a9 DG |
2095 | /* |
2096 | * Clean up a metadata stream and free its memory. | |
2097 | */ | |
e316aad5 DG |
2098 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
2099 | struct lttng_ht *ht) | |
fb3a43a9 | 2100 | { |
a6ef8ee6 JG |
2101 | struct lttng_consumer_channel *channel = NULL; |
2102 | bool free_channel = false; | |
fb3a43a9 DG |
2103 | |
2104 | assert(stream); | |
2105 | /* | |
2106 | * This call should NEVER receive regular stream. It must always be | |
2107 | * metadata stream and this is crucial for data structure synchronization. | |
2108 | */ | |
2109 | assert(stream->metadata_flag); | |
2110 | ||
e316aad5 DG |
2111 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
2112 | ||
74251bb8 | 2113 | pthread_mutex_lock(&consumer_data.lock); |
a6ef8ee6 JG |
2114 | /* |
2115 | * Note that this assumes that a stream's channel is never changed and | |
2116 | * that the stream's lock doesn't need to be taken to sample its | |
2117 | * channel. | |
2118 | */ | |
2119 | channel = stream->chan; | |
2120 | pthread_mutex_lock(&channel->lock); | |
3dad2c0f | 2121 | pthread_mutex_lock(&stream->lock); |
a6ef8ee6 | 2122 | if (channel->metadata_cache) { |
081424af | 2123 | /* Only applicable to userspace consumers. */ |
a6ef8ee6 | 2124 | pthread_mutex_lock(&channel->metadata_cache->lock); |
081424af | 2125 | } |
8994307f | 2126 | |
6d574024 DG |
2127 | /* Remove any reference to that stream. */ |
2128 | consumer_stream_delete(stream, ht); | |
ca22feea | 2129 | |
6d574024 DG |
2130 | /* Close down everything including the relayd if one. */ |
2131 | consumer_stream_close(stream); | |
2132 | /* Destroy tracer buffers of the stream. */ | |
2133 | consumer_stream_destroy_buffers(stream); | |
fb3a43a9 DG |
2134 | |
2135 | /* Atomically decrement channel refcount since other threads can use it. */ | |
a6ef8ee6 JG |
2136 | if (!uatomic_sub_return(&channel->refcount, 1) |
2137 | && !uatomic_read(&channel->nb_init_stream_left)) { | |
c30aaa51 | 2138 | /* Go for channel deletion! */ |
a6ef8ee6 | 2139 | free_channel = true; |
fb3a43a9 | 2140 | } |
a6ef8ee6 | 2141 | stream->chan = NULL; |
fb3a43a9 | 2142 | |
73811ecc DG |
2143 | /* |
2144 | * Nullify the stream reference so it is not used after deletion. The | |
6d574024 DG |
2145 | * channel lock MUST be acquired before being able to check for a NULL |
2146 | * pointer value. | |
73811ecc | 2147 | */ |
a6ef8ee6 | 2148 | channel->metadata_stream = NULL; |
73811ecc | 2149 | |
a6ef8ee6 JG |
2150 | if (channel->metadata_cache) { |
2151 | pthread_mutex_unlock(&channel->metadata_cache->lock); | |
081424af | 2152 | } |
3dad2c0f | 2153 | pthread_mutex_unlock(&stream->lock); |
a6ef8ee6 | 2154 | pthread_mutex_unlock(&channel->lock); |
74251bb8 | 2155 | pthread_mutex_unlock(&consumer_data.lock); |
e316aad5 | 2156 | |
a6ef8ee6 JG |
2157 | if (free_channel) { |
2158 | consumer_del_channel(channel); | |
e316aad5 DG |
2159 | } |
2160 | ||
d2956687 JG |
2161 | lttng_trace_chunk_put(stream->trace_chunk); |
2162 | stream->trace_chunk = NULL; | |
6d574024 | 2163 | consumer_stream_free(stream); |
fb3a43a9 DG |
2164 | } |
2165 | ||
2166 | /* | |
2167 | * Action done with the metadata stream when adding it to the consumer internal | |
2168 | * data structures to handle it. | |
2169 | */ | |
66d583dc | 2170 | void consumer_add_metadata_stream(struct lttng_consumer_stream *stream) |
fb3a43a9 | 2171 | { |
5ab66908 | 2172 | struct lttng_ht *ht = metadata_ht; |
76082088 | 2173 | struct lttng_ht_iter iter; |
d88aee68 | 2174 | struct lttng_ht_node_u64 *node; |
fb3a43a9 | 2175 | |
e316aad5 DG |
2176 | assert(stream); |
2177 | assert(ht); | |
2178 | ||
d88aee68 | 2179 | DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key); |
e316aad5 DG |
2180 | |
2181 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 2182 | pthread_mutex_lock(&stream->chan->lock); |
ec6ea7d0 | 2183 | pthread_mutex_lock(&stream->chan->timer_lock); |
2e818a6a | 2184 | pthread_mutex_lock(&stream->lock); |
e316aad5 | 2185 | |
e316aad5 DG |
2186 | /* |
2187 | * From here, refcounts are updated so be _careful_ when returning an error | |
2188 | * after this point. | |
2189 | */ | |
2190 | ||
fb3a43a9 | 2191 | rcu_read_lock(); |
76082088 DG |
2192 | |
2193 | /* | |
2194 | * Lookup the stream just to make sure it does not exist in our internal | |
2195 | * state. This should NEVER happen. | |
2196 | */ | |
d88aee68 DG |
2197 | lttng_ht_lookup(ht, &stream->key, &iter); |
2198 | node = lttng_ht_iter_get_node_u64(&iter); | |
76082088 DG |
2199 | assert(!node); |
2200 | ||
e316aad5 | 2201 | /* |
ffe60014 DG |
2202 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
2203 | * in terms of destroying the associated channel, because the action that | |
e316aad5 DG |
2204 | * causes the count to become 0 also causes a stream to be added. The |
2205 | * channel deletion will thus be triggered by the following removal of this | |
2206 | * stream. | |
2207 | */ | |
ffe60014 | 2208 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
f2ad556d MD |
2209 | /* Increment refcount before decrementing nb_init_stream_left */ |
2210 | cmm_smp_wmb(); | |
ffe60014 | 2211 | uatomic_dec(&stream->chan->nb_init_stream_left); |
e316aad5 DG |
2212 | } |
2213 | ||
d88aee68 | 2214 | lttng_ht_add_unique_u64(ht, &stream->node); |
ca22feea | 2215 | |
446156b4 | 2216 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
d8ef542d MD |
2217 | &stream->node_channel_id); |
2218 | ||
ca22feea DG |
2219 | /* |
2220 | * Add stream to the stream_list_ht of the consumer data. No need to steal | |
2221 | * the key since the HT does not use it and we allow to add redundant keys | |
2222 | * into this table. | |
2223 | */ | |
d88aee68 | 2224 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
ca22feea | 2225 | |
fb3a43a9 | 2226 | rcu_read_unlock(); |
e316aad5 | 2227 | |
2e818a6a | 2228 | pthread_mutex_unlock(&stream->lock); |
a9838785 | 2229 | pthread_mutex_unlock(&stream->chan->lock); |
ec6ea7d0 | 2230 | pthread_mutex_unlock(&stream->chan->timer_lock); |
e316aad5 | 2231 | pthread_mutex_unlock(&consumer_data.lock); |
fb3a43a9 DG |
2232 | } |
2233 | ||
8994307f DG |
2234 | /* |
2235 | * Delete data stream that are flagged for deletion (endpoint_status). | |
2236 | */ | |
2237 | static void validate_endpoint_status_data_stream(void) | |
2238 | { | |
2239 | struct lttng_ht_iter iter; | |
2240 | struct lttng_consumer_stream *stream; | |
2241 | ||
2242 | DBG("Consumer delete flagged data stream"); | |
2243 | ||
2244 | rcu_read_lock(); | |
2245 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { | |
2246 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2247 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2248 | continue; |
2249 | } | |
2250 | /* Delete it right now */ | |
2251 | consumer_del_stream(stream, data_ht); | |
2252 | } | |
2253 | rcu_read_unlock(); | |
2254 | } | |
2255 | ||
2256 | /* | |
2257 | * Delete metadata stream that are flagged for deletion (endpoint_status). | |
2258 | */ | |
2259 | static void validate_endpoint_status_metadata_stream( | |
2260 | struct lttng_poll_event *pollset) | |
2261 | { | |
2262 | struct lttng_ht_iter iter; | |
2263 | struct lttng_consumer_stream *stream; | |
2264 | ||
2265 | DBG("Consumer delete flagged metadata stream"); | |
2266 | ||
2267 | assert(pollset); | |
2268 | ||
2269 | rcu_read_lock(); | |
2270 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { | |
2271 | /* Validate delete flag of the stream */ | |
79d4ffb7 | 2272 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
8994307f DG |
2273 | continue; |
2274 | } | |
2275 | /* | |
2276 | * Remove from pollset so the metadata thread can continue without | |
2277 | * blocking on a deleted stream. | |
2278 | */ | |
2279 | lttng_poll_del(pollset, stream->wait_fd); | |
2280 | ||
2281 | /* Delete it right now */ | |
2282 | consumer_del_metadata_stream(stream, metadata_ht); | |
2283 | } | |
2284 | rcu_read_unlock(); | |
2285 | } | |
2286 | ||
fb3a43a9 DG |
2287 | /* |
2288 | * Thread polls on metadata file descriptor and write them on disk or on the | |
2289 | * network. | |
2290 | */ | |
7d980def | 2291 | void *consumer_thread_metadata_poll(void *data) |
fb3a43a9 | 2292 | { |
1fc79fb4 | 2293 | int ret, i, pollfd, err = -1; |
fb3a43a9 | 2294 | uint32_t revents, nb_fd; |
e316aad5 | 2295 | struct lttng_consumer_stream *stream = NULL; |
fb3a43a9 | 2296 | struct lttng_ht_iter iter; |
d88aee68 | 2297 | struct lttng_ht_node_u64 *node; |
fb3a43a9 DG |
2298 | struct lttng_poll_event events; |
2299 | struct lttng_consumer_local_data *ctx = data; | |
2300 | ssize_t len; | |
2301 | ||
2302 | rcu_register_thread(); | |
2303 | ||
1fc79fb4 MD |
2304 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA); |
2305 | ||
2d57de81 MD |
2306 | if (testpoint(consumerd_thread_metadata)) { |
2307 | goto error_testpoint; | |
2308 | } | |
2309 | ||
9ce5646a MD |
2310 | health_code_update(); |
2311 | ||
fb3a43a9 DG |
2312 | DBG("Thread metadata poll started"); |
2313 | ||
fb3a43a9 DG |
2314 | /* Size is set to 1 for the consumer_metadata pipe */ |
2315 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2316 | if (ret < 0) { | |
2317 | ERR("Poll set creation failed"); | |
d8ef542d | 2318 | goto end_poll; |
fb3a43a9 DG |
2319 | } |
2320 | ||
13886d2d DG |
2321 | ret = lttng_poll_add(&events, |
2322 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN); | |
fb3a43a9 DG |
2323 | if (ret < 0) { |
2324 | goto end; | |
2325 | } | |
2326 | ||
2327 | /* Main loop */ | |
2328 | DBG("Metadata main loop started"); | |
2329 | ||
2330 | while (1) { | |
fb3a43a9 | 2331 | restart: |
7fa2082e | 2332 | health_code_update(); |
9ce5646a | 2333 | health_poll_entry(); |
7fa2082e | 2334 | DBG("Metadata poll wait"); |
fb3a43a9 | 2335 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2336 | DBG("Metadata poll return from wait with %d fd(s)", |
2337 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2338 | health_poll_exit(); |
40063ead | 2339 | DBG("Metadata event caught in thread"); |
fb3a43a9 DG |
2340 | if (ret < 0) { |
2341 | if (errno == EINTR) { | |
40063ead | 2342 | ERR("Poll EINTR caught"); |
fb3a43a9 DG |
2343 | goto restart; |
2344 | } | |
d9607cd7 MD |
2345 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2346 | err = 0; /* All is OK */ | |
2347 | } | |
2348 | goto end; | |
fb3a43a9 DG |
2349 | } |
2350 | ||
0d9c5d77 DG |
2351 | nb_fd = ret; |
2352 | ||
e316aad5 | 2353 | /* From here, the event is a metadata wait fd */ |
fb3a43a9 | 2354 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2355 | health_code_update(); |
2356 | ||
fb3a43a9 DG |
2357 | revents = LTTNG_POLL_GETEV(&events, i); |
2358 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2359 | ||
13886d2d | 2360 | if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) { |
03e43155 | 2361 | if (revents & LPOLLIN) { |
13886d2d DG |
2362 | ssize_t pipe_len; |
2363 | ||
2364 | pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe, | |
2365 | &stream, sizeof(stream)); | |
6cd525e8 | 2366 | if (pipe_len < sizeof(stream)) { |
03e43155 MD |
2367 | if (pipe_len < 0) { |
2368 | PERROR("read metadata stream"); | |
2369 | } | |
fb3a43a9 | 2370 | /* |
03e43155 MD |
2371 | * Remove the pipe from the poll set and continue the loop |
2372 | * since their might be data to consume. | |
fb3a43a9 | 2373 | */ |
03e43155 MD |
2374 | lttng_poll_del(&events, |
2375 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2376 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
fb3a43a9 DG |
2377 | continue; |
2378 | } | |
2379 | ||
8994307f DG |
2380 | /* A NULL stream means that the state has changed. */ |
2381 | if (stream == NULL) { | |
2382 | /* Check for deleted streams. */ | |
2383 | validate_endpoint_status_metadata_stream(&events); | |
3714380f | 2384 | goto restart; |
8994307f DG |
2385 | } |
2386 | ||
fb3a43a9 DG |
2387 | DBG("Adding metadata stream %d to poll set", |
2388 | stream->wait_fd); | |
2389 | ||
fb3a43a9 DG |
2390 | /* Add metadata stream to the global poll events list */ |
2391 | lttng_poll_add(&events, stream->wait_fd, | |
6d574024 | 2392 | LPOLLIN | LPOLLPRI | LPOLLHUP); |
03e43155 MD |
2393 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
2394 | DBG("Metadata thread pipe hung up"); | |
2395 | /* | |
2396 | * Remove the pipe from the poll set and continue the loop | |
2397 | * since their might be data to consume. | |
2398 | */ | |
2399 | lttng_poll_del(&events, | |
2400 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); | |
2401 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); | |
2402 | continue; | |
2403 | } else { | |
2404 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2405 | goto end; | |
fb3a43a9 DG |
2406 | } |
2407 | ||
e316aad5 | 2408 | /* Handle other stream */ |
fb3a43a9 DG |
2409 | continue; |
2410 | } | |
2411 | ||
d09e1200 | 2412 | rcu_read_lock(); |
d88aee68 DG |
2413 | { |
2414 | uint64_t tmp_id = (uint64_t) pollfd; | |
2415 | ||
2416 | lttng_ht_lookup(metadata_ht, &tmp_id, &iter); | |
2417 | } | |
2418 | node = lttng_ht_iter_get_node_u64(&iter); | |
e316aad5 | 2419 | assert(node); |
fb3a43a9 DG |
2420 | |
2421 | stream = caa_container_of(node, struct lttng_consumer_stream, | |
58b1f425 | 2422 | node); |
fb3a43a9 | 2423 | |
03e43155 MD |
2424 | if (revents & (LPOLLIN | LPOLLPRI)) { |
2425 | /* Get the data out of the metadata file descriptor */ | |
2426 | DBG("Metadata available on fd %d", pollfd); | |
2427 | assert(stream->wait_fd == pollfd); | |
2428 | ||
2429 | do { | |
2430 | health_code_update(); | |
2431 | ||
bdc8d1bb | 2432 | len = ctx->on_buffer_ready(stream, ctx, false); |
03e43155 MD |
2433 | /* |
2434 | * We don't check the return value here since if we get | |
83f4233d | 2435 | * a negative len, it means an error occurred thus we |
03e43155 MD |
2436 | * simply remove it from the poll set and free the |
2437 | * stream. | |
2438 | */ | |
2439 | } while (len > 0); | |
2440 | ||
2441 | /* It's ok to have an unavailable sub-buffer */ | |
2442 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { | |
2443 | /* Clean up stream from consumer and free it. */ | |
2444 | lttng_poll_del(&events, stream->wait_fd); | |
2445 | consumer_del_metadata_stream(stream, metadata_ht); | |
2446 | } | |
2447 | } else if (revents & (LPOLLERR | LPOLLHUP)) { | |
e316aad5 | 2448 | DBG("Metadata fd %d is hup|err.", pollfd); |
fb3a43a9 DG |
2449 | if (!stream->hangup_flush_done |
2450 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2451 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2452 | DBG("Attempting to flush and consume the UST buffers"); | |
2453 | lttng_ustconsumer_on_stream_hangup(stream); | |
2454 | ||
2455 | /* We just flushed the stream now read it. */ | |
4bb94b75 | 2456 | do { |
9ce5646a MD |
2457 | health_code_update(); |
2458 | ||
bdc8d1bb | 2459 | len = ctx->on_buffer_ready(stream, ctx, false); |
4bb94b75 DG |
2460 | /* |
2461 | * We don't check the return value here since if we get | |
83f4233d | 2462 | * a negative len, it means an error occurred thus we |
4bb94b75 DG |
2463 | * simply remove it from the poll set and free the |
2464 | * stream. | |
2465 | */ | |
2466 | } while (len > 0); | |
fb3a43a9 DG |
2467 | } |
2468 | ||
fb3a43a9 | 2469 | lttng_poll_del(&events, stream->wait_fd); |
e316aad5 DG |
2470 | /* |
2471 | * This call update the channel states, closes file descriptors | |
2472 | * and securely free the stream. | |
2473 | */ | |
2474 | consumer_del_metadata_stream(stream, metadata_ht); | |
03e43155 MD |
2475 | } else { |
2476 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
6f2f1a70 | 2477 | rcu_read_unlock(); |
03e43155 | 2478 | goto end; |
fb3a43a9 | 2479 | } |
e316aad5 | 2480 | /* Release RCU lock for the stream looked up */ |
d09e1200 | 2481 | rcu_read_unlock(); |
fb3a43a9 DG |
2482 | } |
2483 | } | |
2484 | ||
1fc79fb4 MD |
2485 | /* All is OK */ |
2486 | err = 0; | |
fb3a43a9 DG |
2487 | end: |
2488 | DBG("Metadata poll thread exiting"); | |
fb3a43a9 | 2489 | |
d8ef542d MD |
2490 | lttng_poll_clean(&events); |
2491 | end_poll: | |
2d57de81 | 2492 | error_testpoint: |
1fc79fb4 MD |
2493 | if (err) { |
2494 | health_error(); | |
2495 | ERR("Health error occurred in %s", __func__); | |
2496 | } | |
2497 | health_unregister(health_consumerd); | |
fb3a43a9 DG |
2498 | rcu_unregister_thread(); |
2499 | return NULL; | |
2500 | } | |
2501 | ||
3bd1e081 | 2502 | /* |
e4421fec | 2503 | * This thread polls the fds in the set to consume the data and write |
3bd1e081 MD |
2504 | * it to tracefile if necessary. |
2505 | */ | |
7d980def | 2506 | void *consumer_thread_data_poll(void *data) |
3bd1e081 | 2507 | { |
1fc79fb4 | 2508 | int num_rdy, num_hup, high_prio, ret, i, err = -1; |
3bd1e081 MD |
2509 | struct pollfd *pollfd = NULL; |
2510 | /* local view of the streams */ | |
c869f647 | 2511 | struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL; |
3bd1e081 | 2512 | /* local view of consumer_data.fds_count */ |
8bdcc002 JG |
2513 | int nb_fd = 0; |
2514 | /* 2 for the consumer_data_pipe and wake up pipe */ | |
2515 | const int nb_pipes_fd = 2; | |
9a2fcf78 JD |
2516 | /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */ |
2517 | int nb_inactive_fd = 0; | |
3bd1e081 | 2518 | struct lttng_consumer_local_data *ctx = data; |
00e2e675 | 2519 | ssize_t len; |
3bd1e081 | 2520 | |
e7b994a3 DG |
2521 | rcu_register_thread(); |
2522 | ||
1fc79fb4 MD |
2523 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA); |
2524 | ||
2d57de81 MD |
2525 | if (testpoint(consumerd_thread_data)) { |
2526 | goto error_testpoint; | |
2527 | } | |
2528 | ||
9ce5646a MD |
2529 | health_code_update(); |
2530 | ||
4df6c8cb MD |
2531 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream *)); |
2532 | if (local_stream == NULL) { | |
2533 | PERROR("local_stream malloc"); | |
2534 | goto end; | |
2535 | } | |
3bd1e081 MD |
2536 | |
2537 | while (1) { | |
9ce5646a MD |
2538 | health_code_update(); |
2539 | ||
3bd1e081 MD |
2540 | high_prio = 0; |
2541 | num_hup = 0; | |
2542 | ||
2543 | /* | |
e4421fec | 2544 | * the fds set has been updated, we need to update our |
3bd1e081 MD |
2545 | * local array as well |
2546 | */ | |
2547 | pthread_mutex_lock(&consumer_data.lock); | |
2548 | if (consumer_data.need_update) { | |
0e428499 DG |
2549 | free(pollfd); |
2550 | pollfd = NULL; | |
2551 | ||
2552 | free(local_stream); | |
2553 | local_stream = NULL; | |
3bd1e081 | 2554 | |
8bdcc002 | 2555 | /* Allocate for all fds */ |
261de637 | 2556 | pollfd = zmalloc((consumer_data.stream_count + nb_pipes_fd) * sizeof(struct pollfd)); |
3bd1e081 | 2557 | if (pollfd == NULL) { |
7a57cf92 | 2558 | PERROR("pollfd malloc"); |
3bd1e081 MD |
2559 | pthread_mutex_unlock(&consumer_data.lock); |
2560 | goto end; | |
2561 | } | |
2562 | ||
261de637 | 2563 | local_stream = zmalloc((consumer_data.stream_count + nb_pipes_fd) * |
747f8642 | 2564 | sizeof(struct lttng_consumer_stream *)); |
3bd1e081 | 2565 | if (local_stream == NULL) { |
7a57cf92 | 2566 | PERROR("local_stream malloc"); |
3bd1e081 MD |
2567 | pthread_mutex_unlock(&consumer_data.lock); |
2568 | goto end; | |
2569 | } | |
ffe60014 | 2570 | ret = update_poll_array(ctx, &pollfd, local_stream, |
9a2fcf78 | 2571 | data_ht, &nb_inactive_fd); |
3bd1e081 MD |
2572 | if (ret < 0) { |
2573 | ERR("Error in allocating pollfd or local_outfds"); | |
f73fabfd | 2574 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2575 | pthread_mutex_unlock(&consumer_data.lock); |
2576 | goto end; | |
2577 | } | |
2578 | nb_fd = ret; | |
2579 | consumer_data.need_update = 0; | |
2580 | } | |
2581 | pthread_mutex_unlock(&consumer_data.lock); | |
2582 | ||
4078b776 | 2583 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
9a2fcf78 JD |
2584 | if (nb_fd == 0 && nb_inactive_fd == 0 && |
2585 | CMM_LOAD_SHARED(consumer_quit) == 1) { | |
1fc79fb4 | 2586 | err = 0; /* All is OK */ |
4078b776 MD |
2587 | goto end; |
2588 | } | |
3bd1e081 | 2589 | /* poll on the array of fds */ |
88f2b785 | 2590 | restart: |
261de637 | 2591 | DBG("polling on %d fd", nb_fd + nb_pipes_fd); |
cf0bcb51 JG |
2592 | if (testpoint(consumerd_thread_data_poll)) { |
2593 | goto end; | |
2594 | } | |
9ce5646a | 2595 | health_poll_entry(); |
261de637 | 2596 | num_rdy = poll(pollfd, nb_fd + nb_pipes_fd, -1); |
9ce5646a | 2597 | health_poll_exit(); |
3bd1e081 MD |
2598 | DBG("poll num_rdy : %d", num_rdy); |
2599 | if (num_rdy == -1) { | |
88f2b785 MD |
2600 | /* |
2601 | * Restart interrupted system call. | |
2602 | */ | |
2603 | if (errno == EINTR) { | |
2604 | goto restart; | |
2605 | } | |
7a57cf92 | 2606 | PERROR("Poll error"); |
f73fabfd | 2607 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
3bd1e081 MD |
2608 | goto end; |
2609 | } else if (num_rdy == 0) { | |
2610 | DBG("Polling thread timed out"); | |
2611 | goto end; | |
2612 | } | |
2613 | ||
80957876 JG |
2614 | if (caa_unlikely(data_consumption_paused)) { |
2615 | DBG("Data consumption paused, sleeping..."); | |
2616 | sleep(1); | |
2617 | goto restart; | |
2618 | } | |
2619 | ||
3bd1e081 | 2620 | /* |
50f8ae69 | 2621 | * If the consumer_data_pipe triggered poll go directly to the |
00e2e675 DG |
2622 | * beginning of the loop to update the array. We want to prioritize |
2623 | * array update over low-priority reads. | |
3bd1e081 | 2624 | */ |
509bb1cf | 2625 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
ab30f567 | 2626 | ssize_t pipe_readlen; |
04fdd819 | 2627 | |
50f8ae69 | 2628 | DBG("consumer_data_pipe wake up"); |
acdb9057 DG |
2629 | pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe, |
2630 | &new_stream, sizeof(new_stream)); | |
6cd525e8 MD |
2631 | if (pipe_readlen < sizeof(new_stream)) { |
2632 | PERROR("Consumer data pipe"); | |
23f5f35d DG |
2633 | /* Continue so we can at least handle the current stream(s). */ |
2634 | continue; | |
2635 | } | |
c869f647 DG |
2636 | |
2637 | /* | |
2638 | * If the stream is NULL, just ignore it. It's also possible that | |
2639 | * the sessiond poll thread changed the consumer_quit state and is | |
2640 | * waking us up to test it. | |
2641 | */ | |
2642 | if (new_stream == NULL) { | |
8994307f | 2643 | validate_endpoint_status_data_stream(); |
c869f647 DG |
2644 | continue; |
2645 | } | |
2646 | ||
c869f647 | 2647 | /* Continue to update the local streams and handle prio ones */ |
3bd1e081 MD |
2648 | continue; |
2649 | } | |
2650 | ||
02b3d176 DG |
2651 | /* Handle wakeup pipe. */ |
2652 | if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) { | |
2653 | char dummy; | |
2654 | ssize_t pipe_readlen; | |
2655 | ||
2656 | pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy, | |
2657 | sizeof(dummy)); | |
2658 | if (pipe_readlen < 0) { | |
2659 | PERROR("Consumer data wakeup pipe"); | |
2660 | } | |
2661 | /* We've been awakened to handle stream(s). */ | |
2662 | ctx->has_wakeup = 0; | |
2663 | } | |
2664 | ||
3bd1e081 MD |
2665 | /* Take care of high priority channels first. */ |
2666 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2667 | health_code_update(); |
2668 | ||
9617607b DG |
2669 | if (local_stream[i] == NULL) { |
2670 | continue; | |
2671 | } | |
fb3a43a9 | 2672 | if (pollfd[i].revents & POLLPRI) { |
d41f73b7 MD |
2673 | DBG("Urgent read on fd %d", pollfd[i].fd); |
2674 | high_prio = 1; | |
bdc8d1bb | 2675 | len = ctx->on_buffer_ready(local_stream[i], ctx, false); |
d41f73b7 | 2676 | /* it's ok to have an unavailable sub-buffer */ |
b64403e3 | 2677 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2678 | /* Clean the stream and free it. */ |
2679 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2680 | local_stream[i] = NULL; |
4078b776 MD |
2681 | } else if (len > 0) { |
2682 | local_stream[i]->data_read = 1; | |
d41f73b7 | 2683 | } |
3bd1e081 MD |
2684 | } |
2685 | } | |
2686 | ||
4078b776 MD |
2687 | /* |
2688 | * If we read high prio channel in this loop, try again | |
2689 | * for more high prio data. | |
2690 | */ | |
2691 | if (high_prio) { | |
3bd1e081 MD |
2692 | continue; |
2693 | } | |
2694 | ||
2695 | /* Take care of low priority channels. */ | |
4078b776 | 2696 | for (i = 0; i < nb_fd; i++) { |
9ce5646a MD |
2697 | health_code_update(); |
2698 | ||
9617607b DG |
2699 | if (local_stream[i] == NULL) { |
2700 | continue; | |
2701 | } | |
4078b776 | 2702 | if ((pollfd[i].revents & POLLIN) || |
02b3d176 DG |
2703 | local_stream[i]->hangup_flush_done || |
2704 | local_stream[i]->has_data) { | |
4078b776 | 2705 | DBG("Normal read on fd %d", pollfd[i].fd); |
bdc8d1bb | 2706 | len = ctx->on_buffer_ready(local_stream[i], ctx, false); |
4078b776 | 2707 | /* it's ok to have an unavailable sub-buffer */ |
b64403e3 | 2708 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
ab1027f4 DG |
2709 | /* Clean the stream and free it. */ |
2710 | consumer_del_stream(local_stream[i], data_ht); | |
9617607b | 2711 | local_stream[i] = NULL; |
4078b776 MD |
2712 | } else if (len > 0) { |
2713 | local_stream[i]->data_read = 1; | |
2714 | } | |
2715 | } | |
2716 | } | |
2717 | ||
2718 | /* Handle hangup and errors */ | |
2719 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2720 | health_code_update(); |
2721 | ||
9617607b DG |
2722 | if (local_stream[i] == NULL) { |
2723 | continue; | |
2724 | } | |
4078b776 MD |
2725 | if (!local_stream[i]->hangup_flush_done |
2726 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) | |
2727 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
2728 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
2729 | DBG("fd %d is hup|err|nval. Attempting flush and read.", | |
9617607b | 2730 | pollfd[i].fd); |
4078b776 MD |
2731 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
2732 | /* Attempt read again, for the data we just flushed. */ | |
2733 | local_stream[i]->data_read = 1; | |
2734 | } | |
2735 | /* | |
2736 | * If the poll flag is HUP/ERR/NVAL and we have | |
2737 | * read no data in this pass, we can remove the | |
2738 | * stream from its hash table. | |
2739 | */ | |
2740 | if ((pollfd[i].revents & POLLHUP)) { | |
2741 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); | |
2742 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2743 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2744 | local_stream[i] = NULL; |
4078b776 MD |
2745 | num_hup++; |
2746 | } | |
2747 | } else if (pollfd[i].revents & POLLERR) { | |
2748 | ERR("Error returned in polling fd %d.", pollfd[i].fd); | |
2749 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2750 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2751 | local_stream[i] = NULL; |
4078b776 MD |
2752 | num_hup++; |
2753 | } | |
2754 | } else if (pollfd[i].revents & POLLNVAL) { | |
2755 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); | |
2756 | if (!local_stream[i]->data_read) { | |
43c34bc3 | 2757 | consumer_del_stream(local_stream[i], data_ht); |
9617607b | 2758 | local_stream[i] = NULL; |
4078b776 | 2759 | num_hup++; |
3bd1e081 MD |
2760 | } |
2761 | } | |
9617607b DG |
2762 | if (local_stream[i] != NULL) { |
2763 | local_stream[i]->data_read = 0; | |
2764 | } | |
3bd1e081 MD |
2765 | } |
2766 | } | |
1fc79fb4 MD |
2767 | /* All is OK */ |
2768 | err = 0; | |
3bd1e081 MD |
2769 | end: |
2770 | DBG("polling thread exiting"); | |
0e428499 DG |
2771 | free(pollfd); |
2772 | free(local_stream); | |
fb3a43a9 DG |
2773 | |
2774 | /* | |
2775 | * Close the write side of the pipe so epoll_wait() in | |
7d980def DG |
2776 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
2777 | * read side of the pipe. If we close them both, epoll_wait strangely does | |
2778 | * not return and could create a endless wait period if the pipe is the | |
2779 | * only tracked fd in the poll set. The thread will take care of closing | |
2780 | * the read side. | |
fb3a43a9 | 2781 | */ |
13886d2d | 2782 | (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe); |
fb3a43a9 | 2783 | |
2d57de81 | 2784 | error_testpoint: |
1fc79fb4 MD |
2785 | if (err) { |
2786 | health_error(); | |
2787 | ERR("Health error occurred in %s", __func__); | |
2788 | } | |
2789 | health_unregister(health_consumerd); | |
2790 | ||
e7b994a3 | 2791 | rcu_unregister_thread(); |
3bd1e081 MD |
2792 | return NULL; |
2793 | } | |
2794 | ||
d8ef542d MD |
2795 | /* |
2796 | * Close wake-up end of each stream belonging to the channel. This will | |
2797 | * allow the poll() on the stream read-side to detect when the | |
2798 | * write-side (application) finally closes them. | |
2799 | */ | |
2800 | static | |
2801 | void consumer_close_channel_streams(struct lttng_consumer_channel *channel) | |
2802 | { | |
2803 | struct lttng_ht *ht; | |
2804 | struct lttng_consumer_stream *stream; | |
2805 | struct lttng_ht_iter iter; | |
2806 | ||
2807 | ht = consumer_data.stream_per_chan_id_ht; | |
2808 | ||
2809 | rcu_read_lock(); | |
2810 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
2811 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
2812 | ht->match_fct, &channel->key, | |
2813 | &iter.iter, stream, node_channel_id.node) { | |
f2ad556d MD |
2814 | /* |
2815 | * Protect against teardown with mutex. | |
2816 | */ | |
2817 | pthread_mutex_lock(&stream->lock); | |
2818 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
2819 | goto next; | |
2820 | } | |
d8ef542d MD |
2821 | switch (consumer_data.type) { |
2822 | case LTTNG_CONSUMER_KERNEL: | |
2823 | break; | |
2824 | case LTTNG_CONSUMER32_UST: | |
2825 | case LTTNG_CONSUMER64_UST: | |
b4a650f3 DG |
2826 | if (stream->metadata_flag) { |
2827 | /* Safe and protected by the stream lock. */ | |
2828 | lttng_ustconsumer_close_metadata(stream->chan); | |
2829 | } else { | |
2830 | /* | |
2831 | * Note: a mutex is taken internally within | |
2832 | * liblttng-ust-ctl to protect timer wakeup_fd | |
2833 | * use from concurrent close. | |
2834 | */ | |
2835 | lttng_ustconsumer_close_stream_wakeup(stream); | |
2836 | } | |
d8ef542d MD |
2837 | break; |
2838 | default: | |
2839 | ERR("Unknown consumer_data type"); | |
2840 | assert(0); | |
2841 | } | |
f2ad556d MD |
2842 | next: |
2843 | pthread_mutex_unlock(&stream->lock); | |
d8ef542d MD |
2844 | } |
2845 | rcu_read_unlock(); | |
2846 | } | |
2847 | ||
2848 | static void destroy_channel_ht(struct lttng_ht *ht) | |
2849 | { | |
2850 | struct lttng_ht_iter iter; | |
2851 | struct lttng_consumer_channel *channel; | |
2852 | int ret; | |
2853 | ||
2854 | if (ht == NULL) { | |
2855 | return; | |
2856 | } | |
2857 | ||
2858 | rcu_read_lock(); | |
2859 | cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) { | |
2860 | ret = lttng_ht_del(ht, &iter); | |
2861 | assert(ret != 0); | |
2862 | } | |
2863 | rcu_read_unlock(); | |
2864 | ||
2865 | lttng_ht_destroy(ht); | |
2866 | } | |
2867 | ||
2868 | /* | |
2869 | * This thread polls the channel fds to detect when they are being | |
2870 | * closed. It closes all related streams if the channel is detected as | |
2871 | * closed. It is currently only used as a shim layer for UST because the | |
2872 | * consumerd needs to keep the per-stream wakeup end of pipes open for | |
2873 | * periodical flush. | |
2874 | */ | |
2875 | void *consumer_thread_channel_poll(void *data) | |
2876 | { | |
1fc79fb4 | 2877 | int ret, i, pollfd, err = -1; |
d8ef542d MD |
2878 | uint32_t revents, nb_fd; |
2879 | struct lttng_consumer_channel *chan = NULL; | |
2880 | struct lttng_ht_iter iter; | |
2881 | struct lttng_ht_node_u64 *node; | |
2882 | struct lttng_poll_event events; | |
2883 | struct lttng_consumer_local_data *ctx = data; | |
2884 | struct lttng_ht *channel_ht; | |
2885 | ||
2886 | rcu_register_thread(); | |
2887 | ||
1fc79fb4 MD |
2888 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL); |
2889 | ||
2d57de81 MD |
2890 | if (testpoint(consumerd_thread_channel)) { |
2891 | goto error_testpoint; | |
2892 | } | |
2893 | ||
9ce5646a MD |
2894 | health_code_update(); |
2895 | ||
d8ef542d MD |
2896 | channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2897 | if (!channel_ht) { | |
2898 | /* ENOMEM at this point. Better to bail out. */ | |
2899 | goto end_ht; | |
2900 | } | |
2901 | ||
2902 | DBG("Thread channel poll started"); | |
2903 | ||
2904 | /* Size is set to 1 for the consumer_channel pipe */ | |
2905 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
2906 | if (ret < 0) { | |
2907 | ERR("Poll set creation failed"); | |
2908 | goto end_poll; | |
2909 | } | |
2910 | ||
2911 | ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN); | |
2912 | if (ret < 0) { | |
2913 | goto end; | |
2914 | } | |
2915 | ||
2916 | /* Main loop */ | |
2917 | DBG("Channel main loop started"); | |
2918 | ||
2919 | while (1) { | |
d8ef542d | 2920 | restart: |
7fa2082e MD |
2921 | health_code_update(); |
2922 | DBG("Channel poll wait"); | |
9ce5646a | 2923 | health_poll_entry(); |
d8ef542d | 2924 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
2925 | DBG("Channel poll return from wait with %d fd(s)", |
2926 | LTTNG_POLL_GETNB(&events)); | |
9ce5646a | 2927 | health_poll_exit(); |
40063ead | 2928 | DBG("Channel event caught in thread"); |
d8ef542d MD |
2929 | if (ret < 0) { |
2930 | if (errno == EINTR) { | |
40063ead | 2931 | ERR("Poll EINTR caught"); |
d8ef542d MD |
2932 | goto restart; |
2933 | } | |
d9607cd7 MD |
2934 | if (LTTNG_POLL_GETNB(&events) == 0) { |
2935 | err = 0; /* All is OK */ | |
2936 | } | |
d8ef542d MD |
2937 | goto end; |
2938 | } | |
2939 | ||
2940 | nb_fd = ret; | |
2941 | ||
2942 | /* From here, the event is a channel wait fd */ | |
2943 | for (i = 0; i < nb_fd; i++) { | |
9ce5646a MD |
2944 | health_code_update(); |
2945 | ||
d8ef542d MD |
2946 | revents = LTTNG_POLL_GETEV(&events, i); |
2947 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2948 | ||
d8ef542d | 2949 | if (pollfd == ctx->consumer_channel_pipe[0]) { |
03e43155 | 2950 | if (revents & LPOLLIN) { |
d8ef542d | 2951 | enum consumer_channel_action action; |
a0cbdd2e | 2952 | uint64_t key; |
d8ef542d | 2953 | |
a0cbdd2e | 2954 | ret = read_channel_pipe(ctx, &chan, &key, &action); |
d8ef542d | 2955 | if (ret <= 0) { |
03e43155 MD |
2956 | if (ret < 0) { |
2957 | ERR("Error reading channel pipe"); | |
2958 | } | |
2959 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
d8ef542d MD |
2960 | continue; |
2961 | } | |
2962 | ||
2963 | switch (action) { | |
2964 | case CONSUMER_CHANNEL_ADD: | |
2965 | DBG("Adding channel %d to poll set", | |
2966 | chan->wait_fd); | |
2967 | ||
2968 | lttng_ht_node_init_u64(&chan->wait_fd_node, | |
2969 | chan->wait_fd); | |
c7260a81 | 2970 | rcu_read_lock(); |
d8ef542d MD |
2971 | lttng_ht_add_unique_u64(channel_ht, |
2972 | &chan->wait_fd_node); | |
c7260a81 | 2973 | rcu_read_unlock(); |
d8ef542d MD |
2974 | /* Add channel to the global poll events list */ |
2975 | lttng_poll_add(&events, chan->wait_fd, | |
03e43155 | 2976 | LPOLLERR | LPOLLHUP); |
d8ef542d | 2977 | break; |
a0cbdd2e MD |
2978 | case CONSUMER_CHANNEL_DEL: |
2979 | { | |
b4a650f3 DG |
2980 | /* |
2981 | * This command should never be called if the channel | |
2982 | * has streams monitored by either the data or metadata | |
2983 | * thread. The consumer only notify this thread with a | |
2984 | * channel del. command if it receives a destroy | |
2985 | * channel command from the session daemon that send it | |
2986 | * if a command prior to the GET_CHANNEL failed. | |
2987 | */ | |
2988 | ||
c7260a81 | 2989 | rcu_read_lock(); |
a0cbdd2e MD |
2990 | chan = consumer_find_channel(key); |
2991 | if (!chan) { | |
c7260a81 | 2992 | rcu_read_unlock(); |
a0cbdd2e MD |
2993 | ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key); |
2994 | break; | |
2995 | } | |
2996 | lttng_poll_del(&events, chan->wait_fd); | |
f623cc0b | 2997 | iter.iter.node = &chan->wait_fd_node.node; |
a0cbdd2e MD |
2998 | ret = lttng_ht_del(channel_ht, &iter); |
2999 | assert(ret == 0); | |
a0cbdd2e | 3000 | |
f2a444f1 DG |
3001 | switch (consumer_data.type) { |
3002 | case LTTNG_CONSUMER_KERNEL: | |
3003 | break; | |
3004 | case LTTNG_CONSUMER32_UST: | |
3005 | case LTTNG_CONSUMER64_UST: | |
212d67a2 DG |
3006 | health_code_update(); |
3007 | /* Destroy streams that might have been left in the stream list. */ | |
3008 | clean_channel_stream_list(chan); | |
f2a444f1 DG |
3009 | break; |
3010 | default: | |
3011 | ERR("Unknown consumer_data type"); | |
3012 | assert(0); | |
3013 | } | |
3014 | ||
a0cbdd2e MD |
3015 | /* |
3016 | * Release our own refcount. Force channel deletion even if | |
3017 | * streams were not initialized. | |
3018 | */ | |
3019 | if (!uatomic_sub_return(&chan->refcount, 1)) { | |
3020 | consumer_del_channel(chan); | |
3021 | } | |
c7260a81 | 3022 | rcu_read_unlock(); |
a0cbdd2e MD |
3023 | goto restart; |
3024 | } | |
d8ef542d MD |
3025 | case CONSUMER_CHANNEL_QUIT: |
3026 | /* | |
3027 | * Remove the pipe from the poll set and continue the loop | |
3028 | * since their might be data to consume. | |
3029 | */ | |
3030 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
3031 | continue; | |
3032 | default: | |
3033 | ERR("Unknown action"); | |
3034 | break; | |
3035 | } | |
03e43155 MD |
3036 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
3037 | DBG("Channel thread pipe hung up"); | |
3038 | /* | |
3039 | * Remove the pipe from the poll set and continue the loop | |
3040 | * since their might be data to consume. | |
3041 | */ | |
3042 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); | |
3043 | continue; | |
3044 | } else { | |
3045 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3046 | goto end; | |
d8ef542d MD |
3047 | } |
3048 | ||
3049 | /* Handle other stream */ | |
3050 | continue; | |
3051 | } | |
3052 | ||
3053 | rcu_read_lock(); | |
3054 | { | |
3055 | uint64_t tmp_id = (uint64_t) pollfd; | |
3056 | ||
3057 | lttng_ht_lookup(channel_ht, &tmp_id, &iter); | |
3058 | } | |
3059 | node = lttng_ht_iter_get_node_u64(&iter); | |
3060 | assert(node); | |
3061 | ||
3062 | chan = caa_container_of(node, struct lttng_consumer_channel, | |
3063 | wait_fd_node); | |
3064 | ||
3065 | /* Check for error event */ | |
3066 | if (revents & (LPOLLERR | LPOLLHUP)) { | |
3067 | DBG("Channel fd %d is hup|err.", pollfd); | |
3068 | ||
3069 | lttng_poll_del(&events, chan->wait_fd); | |
3070 | ret = lttng_ht_del(channel_ht, &iter); | |
3071 | assert(ret == 0); | |
b4a650f3 DG |
3072 | |
3073 | /* | |
3074 | * This will close the wait fd for each stream associated to | |
3075 | * this channel AND monitored by the data/metadata thread thus | |
3076 | * will be clean by the right thread. | |
3077 | */ | |
d8ef542d | 3078 | consumer_close_channel_streams(chan); |
f2ad556d MD |
3079 | |
3080 | /* Release our own refcount */ | |
3081 | if (!uatomic_sub_return(&chan->refcount, 1) | |
3082 | && !uatomic_read(&chan->nb_init_stream_left)) { | |
3083 | consumer_del_channel(chan); | |
3084 | } | |
03e43155 MD |
3085 | } else { |
3086 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3087 | rcu_read_unlock(); | |
3088 | goto end; | |
d8ef542d MD |
3089 | } |
3090 | ||
3091 | /* Release RCU lock for the channel looked up */ | |
3092 | rcu_read_unlock(); | |
3093 | } | |
3094 | } | |
3095 | ||
1fc79fb4 MD |
3096 | /* All is OK */ |
3097 | err = 0; | |
d8ef542d MD |
3098 | end: |
3099 | lttng_poll_clean(&events); | |
3100 | end_poll: | |
3101 | destroy_channel_ht(channel_ht); | |
3102 | end_ht: | |
2d57de81 | 3103 | error_testpoint: |
d8ef542d | 3104 | DBG("Channel poll thread exiting"); |
1fc79fb4 MD |
3105 | if (err) { |
3106 | health_error(); | |
3107 | ERR("Health error occurred in %s", __func__); | |
3108 | } | |
3109 | health_unregister(health_consumerd); | |
d8ef542d MD |
3110 | rcu_unregister_thread(); |
3111 | return NULL; | |
3112 | } | |
3113 | ||
331744e3 JD |
3114 | static int set_metadata_socket(struct lttng_consumer_local_data *ctx, |
3115 | struct pollfd *sockpoll, int client_socket) | |
3116 | { | |
3117 | int ret; | |
3118 | ||
3119 | assert(ctx); | |
3120 | assert(sockpoll); | |
3121 | ||
84382d49 MD |
3122 | ret = lttng_consumer_poll_socket(sockpoll); |
3123 | if (ret) { | |
331744e3 JD |
3124 | goto error; |
3125 | } | |
3126 | DBG("Metadata connection on client_socket"); | |
3127 | ||
3128 | /* Blocking call, waiting for transmission */ | |
3129 | ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket); | |
3130 | if (ctx->consumer_metadata_socket < 0) { | |
3131 | WARN("On accept metadata"); | |
3132 | ret = -1; | |
3133 | goto error; | |
3134 | } | |
3135 | ret = 0; | |
3136 | ||
3137 | error: | |
3138 | return ret; | |
3139 | } | |
3140 | ||
3bd1e081 MD |
3141 | /* |
3142 | * This thread listens on the consumerd socket and receives the file | |
3143 | * descriptors from the session daemon. | |
3144 | */ | |
7d980def | 3145 | void *consumer_thread_sessiond_poll(void *data) |
3bd1e081 | 3146 | { |
1fc79fb4 | 3147 | int sock = -1, client_socket, ret, err = -1; |
3bd1e081 MD |
3148 | /* |
3149 | * structure to poll for incoming data on communication socket avoids | |
3150 | * making blocking sockets. | |
3151 | */ | |
3152 | struct pollfd consumer_sockpoll[2]; | |
3153 | struct lttng_consumer_local_data *ctx = data; | |
3154 | ||
e7b994a3 DG |
3155 | rcu_register_thread(); |
3156 | ||
1fc79fb4 MD |
3157 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND); |
3158 | ||
2d57de81 MD |
3159 | if (testpoint(consumerd_thread_sessiond)) { |
3160 | goto error_testpoint; | |
3161 | } | |
3162 | ||
9ce5646a MD |
3163 | health_code_update(); |
3164 | ||
3bd1e081 MD |
3165 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
3166 | unlink(ctx->consumer_command_sock_path); | |
3167 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); | |
3168 | if (client_socket < 0) { | |
3169 | ERR("Cannot create command socket"); | |
3170 | goto end; | |
3171 | } | |
3172 | ||
3173 | ret = lttcomm_listen_unix_sock(client_socket); | |
3174 | if (ret < 0) { | |
3175 | goto end; | |
3176 | } | |
3177 | ||
32258573 | 3178 | DBG("Sending ready command to lttng-sessiond"); |
f73fabfd | 3179 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
3bd1e081 MD |
3180 | /* return < 0 on error, but == 0 is not fatal */ |
3181 | if (ret < 0) { | |
32258573 | 3182 | ERR("Error sending ready command to lttng-sessiond"); |
3bd1e081 MD |
3183 | goto end; |
3184 | } | |
3185 | ||
3bd1e081 MD |
3186 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
3187 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; | |
3188 | consumer_sockpoll[0].events = POLLIN | POLLPRI; | |
3189 | consumer_sockpoll[1].fd = client_socket; | |
3190 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3191 | ||
84382d49 MD |
3192 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3193 | if (ret) { | |
3194 | if (ret > 0) { | |
3195 | /* should exit */ | |
3196 | err = 0; | |
3197 | } | |
3bd1e081 MD |
3198 | goto end; |
3199 | } | |
3200 | DBG("Connection on client_socket"); | |
3201 | ||
3202 | /* Blocking call, waiting for transmission */ | |
3203 | sock = lttcomm_accept_unix_sock(client_socket); | |
534d2592 | 3204 | if (sock < 0) { |
3bd1e081 MD |
3205 | WARN("On accept"); |
3206 | goto end; | |
3207 | } | |
3bd1e081 | 3208 | |
331744e3 JD |
3209 | /* |
3210 | * Setup metadata socket which is the second socket connection on the | |
3211 | * command unix socket. | |
3212 | */ | |
3213 | ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket); | |
84382d49 MD |
3214 | if (ret) { |
3215 | if (ret > 0) { | |
3216 | /* should exit */ | |
3217 | err = 0; | |
3218 | } | |
331744e3 JD |
3219 | goto end; |
3220 | } | |
3221 | ||
d96f09c6 DG |
3222 | /* This socket is not useful anymore. */ |
3223 | ret = close(client_socket); | |
3224 | if (ret < 0) { | |
3225 | PERROR("close client_socket"); | |
3226 | } | |
3227 | client_socket = -1; | |
3228 | ||
3bd1e081 MD |
3229 | /* update the polling structure to poll on the established socket */ |
3230 | consumer_sockpoll[1].fd = sock; | |
3231 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
3232 | ||
3233 | while (1) { | |
9ce5646a MD |
3234 | health_code_update(); |
3235 | ||
3236 | health_poll_entry(); | |
3237 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
3238 | health_poll_exit(); | |
84382d49 MD |
3239 | if (ret) { |
3240 | if (ret > 0) { | |
3241 | /* should exit */ | |
3242 | err = 0; | |
3243 | } | |
3bd1e081 MD |
3244 | goto end; |
3245 | } | |
3246 | DBG("Incoming command on sock"); | |
3247 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
4cbc1a04 DG |
3248 | if (ret <= 0) { |
3249 | /* | |
3250 | * This could simply be a session daemon quitting. Don't output | |
3251 | * ERR() here. | |
3252 | */ | |
3253 | DBG("Communication interrupted on command socket"); | |
41ba6035 | 3254 | err = 0; |
3bd1e081 MD |
3255 | goto end; |
3256 | } | |
10211f5c | 3257 | if (CMM_LOAD_SHARED(consumer_quit)) { |
3bd1e081 | 3258 | DBG("consumer_thread_receive_fds received quit from signal"); |
1fc79fb4 | 3259 | err = 0; /* All is OK */ |
3bd1e081 MD |
3260 | goto end; |
3261 | } | |
ffe60014 | 3262 | DBG("received command on sock"); |
3bd1e081 | 3263 | } |
1fc79fb4 MD |
3264 | /* All is OK */ |
3265 | err = 0; | |
3266 | ||
3bd1e081 | 3267 | end: |
ffe60014 | 3268 | DBG("Consumer thread sessiond poll exiting"); |
3bd1e081 | 3269 | |
d88aee68 DG |
3270 | /* |
3271 | * Close metadata streams since the producer is the session daemon which | |
3272 | * just died. | |
3273 | * | |
3274 | * NOTE: for now, this only applies to the UST tracer. | |
3275 | */ | |
6d574024 | 3276 | lttng_consumer_close_all_metadata(); |
d88aee68 | 3277 | |
3bd1e081 MD |
3278 | /* |
3279 | * when all fds have hung up, the polling thread | |
3280 | * can exit cleanly | |
3281 | */ | |
10211f5c | 3282 | CMM_STORE_SHARED(consumer_quit, 1); |
3bd1e081 | 3283 | |
04fdd819 | 3284 | /* |
c869f647 | 3285 | * Notify the data poll thread to poll back again and test the |
8994307f | 3286 | * consumer_quit state that we just set so to quit gracefully. |
04fdd819 | 3287 | */ |
acdb9057 | 3288 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
c869f647 | 3289 | |
a0cbdd2e | 3290 | notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT); |
d8ef542d | 3291 | |
5c635c72 MD |
3292 | notify_health_quit_pipe(health_quit_pipe); |
3293 | ||
d96f09c6 DG |
3294 | /* Cleaning up possibly open sockets. */ |
3295 | if (sock >= 0) { | |
3296 | ret = close(sock); | |
3297 | if (ret < 0) { | |
3298 | PERROR("close sock sessiond poll"); | |
3299 | } | |
3300 | } | |
3301 | if (client_socket >= 0) { | |
38476d24 | 3302 | ret = close(client_socket); |
d96f09c6 DG |
3303 | if (ret < 0) { |
3304 | PERROR("close client_socket sessiond poll"); | |
3305 | } | |
3306 | } | |
3307 | ||
2d57de81 | 3308 | error_testpoint: |
1fc79fb4 MD |
3309 | if (err) { |
3310 | health_error(); | |
3311 | ERR("Health error occurred in %s", __func__); | |
3312 | } | |
3313 | health_unregister(health_consumerd); | |
3314 | ||
e7b994a3 | 3315 | rcu_unregister_thread(); |
3bd1e081 MD |
3316 | return NULL; |
3317 | } | |
d41f73b7 | 3318 | |
fc0c52e9 JG |
3319 | static |
3320 | int consumer_flush_buffer(struct lttng_consumer_stream *stream, | |
3321 | int producer_active) | |
3322 | { | |
3323 | int ret = 0; | |
3324 | ||
3325 | switch (consumer_data.type) { | |
3326 | case LTTNG_CONSUMER_KERNEL: | |
3327 | if (producer_active) { | |
3328 | ret = kernctl_buffer_flush(stream->wait_fd); | |
3329 | if (ret < 0) { | |
3330 | ERR("Failed to flush kernel stream"); | |
3331 | goto end; | |
3332 | } | |
3333 | } else { | |
3334 | ret = kernctl_buffer_flush_empty(stream->wait_fd); | |
3335 | if (ret < 0) { | |
3336 | /* | |
3337 | * Doing a buffer flush which does not take into | |
3338 | * account empty packets. This is not perfect, | |
3339 | * but required as a fall-back when | |
3340 | * "flush_empty" is not implemented by | |
3341 | * lttng-modules. | |
3342 | */ | |
3343 | ret = kernctl_buffer_flush(stream->wait_fd); | |
3344 | if (ret < 0) { | |
3345 | ERR("Failed to flush kernel stream"); | |
3346 | goto end; | |
3347 | } | |
3348 | } | |
3349 | } | |
3350 | break; | |
3351 | case LTTNG_CONSUMER32_UST: | |
3352 | case LTTNG_CONSUMER64_UST: | |
3353 | lttng_ustconsumer_flush_buffer(stream, producer_active); | |
3354 | break; | |
3355 | default: | |
3356 | ERR("Unknown consumer_data type"); | |
3357 | abort(); | |
3358 | } | |
3359 | ||
3360 | end: | |
3361 | return ret; | |
3362 | } | |
3363 | ||
3364 | static enum open_packet_status open_packet(struct lttng_consumer_stream *stream) | |
3365 | { | |
3366 | int ret; | |
3367 | enum open_packet_status status; | |
3368 | unsigned long produced_pos_before, produced_pos_after; | |
3369 | ||
3370 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
3371 | if (ret < 0) { | |
3372 | ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64 | |
3373 | ", channel name = %s, session id = %" PRIu64, | |
3374 | stream->key, stream->chan->name, | |
3375 | stream->chan->session_id); | |
3376 | status = OPEN_PACKET_STATUS_ERROR; | |
3377 | goto end; | |
3378 | } | |
3379 | ||
3380 | ret = lttng_consumer_get_produced_snapshot( | |
3381 | stream, &produced_pos_before); | |
3382 | if (ret < 0) { | |
3383 | ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64 | |
3384 | ", channel name = %s, session id = %" PRIu64, | |
3385 | stream->key, stream->chan->name, | |
3386 | stream->chan->session_id); | |
3387 | status = OPEN_PACKET_STATUS_ERROR; | |
3388 | goto end; | |
3389 | } | |
3390 | ||
3391 | ret = consumer_flush_buffer(stream, 0); | |
3392 | if (ret) { | |
3393 | ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64 | |
3394 | ", channel name = %s, session id = %" PRIu64, | |
3395 | stream->key, stream->chan->name, | |
3396 | stream->chan->session_id); | |
3397 | status = OPEN_PACKET_STATUS_ERROR; | |
3398 | goto end; | |
3399 | } | |
3400 | ||
3401 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
3402 | if (ret < 0) { | |
3403 | ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64 | |
3404 | ", channel name = %s, session id = %" PRIu64, | |
3405 | stream->key, stream->chan->name, | |
3406 | stream->chan->session_id); | |
3407 | status = OPEN_PACKET_STATUS_ERROR; | |
3408 | goto end; | |
3409 | } | |
3410 | ||
3411 | ret = lttng_consumer_get_produced_snapshot(stream, &produced_pos_after); | |
3412 | if (ret < 0) { | |
3413 | ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64 | |
3414 | ", channel name = %s, session id = %" PRIu64, | |
3415 | stream->key, stream->chan->name, | |
3416 | stream->chan->session_id); | |
3417 | status = OPEN_PACKET_STATUS_ERROR; | |
3418 | goto end; | |
3419 | } | |
3420 | ||
3421 | /* | |
3422 | * Determine if the flush had an effect by comparing the produced | |
3423 | * positons before and after the flush. | |
3424 | */ | |
3425 | status = produced_pos_before != produced_pos_after ? | |
3426 | OPEN_PACKET_STATUS_OPENED : | |
3427 | OPEN_PACKET_STATUS_NO_SPACE; | |
1223361a JG |
3428 | if (status == OPEN_PACKET_STATUS_OPENED) { |
3429 | stream->opened_packet_in_current_trace_chunk = true; | |
3430 | } | |
fc0c52e9 JG |
3431 | end: |
3432 | return status; | |
3433 | } | |
3434 | ||
3435 | static bool stream_is_rotating_to_null_chunk( | |
3436 | const struct lttng_consumer_stream *stream) | |
3437 | { | |
3438 | bool rotating_to_null_chunk = false; | |
3439 | ||
3440 | if (stream->rotate_position == -1ULL) { | |
3441 | /* No rotation ongoing. */ | |
3442 | goto end; | |
3443 | } | |
3444 | ||
3445 | if (stream->trace_chunk == stream->chan->trace_chunk || | |
3446 | !stream->chan->trace_chunk) { | |
3447 | rotating_to_null_chunk = true; | |
3448 | } | |
3449 | end: | |
3450 | return rotating_to_null_chunk; | |
3451 | } | |
3452 | ||
4078b776 | 3453 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
bdc8d1bb JG |
3454 | struct lttng_consumer_local_data *ctx, |
3455 | bool locked_by_caller) | |
d41f73b7 | 3456 | { |
9a63f11d | 3457 | ssize_t ret, written_bytes = 0; |
9573993e | 3458 | int rotation_ret; |
bdc8d1bb | 3459 | struct stream_subbuffer subbuffer = {}; |
74251bb8 | 3460 | |
bdc8d1bb JG |
3461 | if (!locked_by_caller) { |
3462 | stream->read_subbuffer_ops.lock(stream); | |
3463 | } | |
3464 | ||
3465 | if (stream->read_subbuffer_ops.on_wake_up) { | |
3466 | ret = stream->read_subbuffer_ops.on_wake_up(stream); | |
3467 | if (ret) { | |
3468 | goto end; | |
3469 | } | |
94d49140 | 3470 | } |
74251bb8 | 3471 | |
9573993e JG |
3472 | /* |
3473 | * If the stream was flagged to be ready for rotation before we extract | |
3474 | * the next packet, rotate it now. | |
3475 | */ | |
3476 | if (stream->rotate_ready) { | |
3477 | DBG("Rotate stream before consuming data"); | |
3478 | ret = lttng_consumer_rotate_stream(ctx, stream); | |
3479 | if (ret < 0) { | |
3480 | ERR("Stream rotation error before consuming data"); | |
3481 | goto end; | |
3482 | } | |
3483 | } | |
3484 | ||
bdc8d1bb JG |
3485 | ret = stream->read_subbuffer_ops.get_next_subbuffer(stream, &subbuffer); |
3486 | if (ret) { | |
3487 | if (ret == -ENODATA) { | |
3488 | /* Not an error. */ | |
3489 | ret = 0; | |
fa2f196b | 3490 | goto sleep_stream; |
bdc8d1bb JG |
3491 | } |
3492 | goto end; | |
d41f73b7 | 3493 | } |
74251bb8 | 3494 | |
bdc8d1bb JG |
3495 | ret = stream->read_subbuffer_ops.pre_consume_subbuffer( |
3496 | stream, &subbuffer); | |
3497 | if (ret) { | |
3498 | goto error_put_subbuf; | |
3499 | } | |
3500 | ||
3501 | written_bytes = stream->read_subbuffer_ops.consume_subbuffer( | |
3502 | ctx, stream, &subbuffer); | |
56c86e0a FD |
3503 | if (written_bytes <= 0) { |
3504 | ERR("Error consuming subbuffer: (%zd)", written_bytes); | |
3505 | ret = (int) written_bytes; | |
3506 | goto error_put_subbuf; | |
bdc8d1bb JG |
3507 | } |
3508 | ||
3509 | ret = stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer); | |
3510 | if (ret) { | |
9573993e JG |
3511 | goto end; |
3512 | } | |
3513 | ||
bdc8d1bb JG |
3514 | if (stream->read_subbuffer_ops.post_consume) { |
3515 | ret = stream->read_subbuffer_ops.post_consume(stream, &subbuffer, ctx); | |
3516 | if (ret) { | |
3517 | goto end; | |
3518 | } | |
3519 | } | |
3520 | ||
9573993e JG |
3521 | /* |
3522 | * After extracting the packet, we check if the stream is now ready to | |
3523 | * be rotated and perform the action immediately. | |
3524 | * | |
3525 | * Don't overwrite `ret` as callers expect the number of bytes | |
3526 | * consumed to be returned on success. | |
3527 | */ | |
3528 | rotation_ret = lttng_consumer_stream_is_rotate_ready(stream); | |
3529 | if (rotation_ret == 1) { | |
3530 | rotation_ret = lttng_consumer_rotate_stream(ctx, stream); | |
3531 | if (rotation_ret < 0) { | |
3532 | ret = rotation_ret; | |
3533 | ERR("Stream rotation error after consuming data"); | |
3534 | goto end; | |
3535 | } | |
3536 | } else if (rotation_ret < 0) { | |
3537 | ret = rotation_ret; | |
3538 | ERR("Failed to check if stream was ready to rotate after consuming data"); | |
3539 | goto end; | |
3540 | } | |
3541 | ||
fc0c52e9 JG |
3542 | /* |
3543 | * TODO roll into a post_consume op as this doesn't apply to metadata | |
3544 | * streams. | |
3545 | */ | |
3546 | if (!stream->opened_packet_in_current_trace_chunk && | |
3547 | stream->trace_chunk && !stream->metadata_flag && | |
3548 | !stream_is_rotating_to_null_chunk(stream)) { | |
3549 | const enum open_packet_status status = open_packet(stream); | |
3550 | ||
3551 | switch (status) { | |
3552 | case OPEN_PACKET_STATUS_OPENED: | |
3553 | DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64 | |
3554 | ", channel name = %s, session id = %" PRIu64, | |
3555 | stream->key, stream->chan->name, | |
3556 | stream->chan->session_id); | |
fc0c52e9 JG |
3557 | break; |
3558 | case OPEN_PACKET_STATUS_NO_SPACE: | |
3559 | /* | |
3560 | * Can't open a packet as there is no space left. | |
3561 | * This means that new events were produced, resulting | |
1223361a | 3562 | * in a packet being opened, which is what we wanted |
fc0c52e9 JG |
3563 | * anyhow. |
3564 | */ | |
3565 | DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64 | |
3566 | ", channel name = %s, session id = %" PRIu64, | |
3567 | stream->key, stream->chan->name, | |
3568 | stream->chan->session_id); | |
3569 | stream->opened_packet_in_current_trace_chunk = true; | |
3570 | break; | |
3571 | case OPEN_PACKET_STATUS_ERROR: | |
3572 | /* Logged by callee. */ | |
3573 | ret = -1; | |
3574 | goto end; | |
3575 | default: | |
3576 | abort(); | |
3577 | } | |
fc0c52e9 JG |
3578 | } |
3579 | ||
8ac94c7d | 3580 | sleep_stream: |
bdc8d1bb JG |
3581 | if (stream->read_subbuffer_ops.on_sleep) { |
3582 | stream->read_subbuffer_ops.on_sleep(stream, ctx); | |
3583 | } | |
3584 | ||
3585 | ret = written_bytes; | |
9573993e | 3586 | end: |
bdc8d1bb JG |
3587 | if (!locked_by_caller) { |
3588 | stream->read_subbuffer_ops.unlock(stream); | |
94d49140 | 3589 | } |
bdc8d1bb | 3590 | |
74251bb8 | 3591 | return ret; |
bdc8d1bb JG |
3592 | error_put_subbuf: |
3593 | (void) stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer); | |
3594 | goto end; | |
d41f73b7 MD |
3595 | } |
3596 | ||
3597 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
3598 | { | |
3599 | switch (consumer_data.type) { | |
3600 | case LTTNG_CONSUMER_KERNEL: | |
3601 | return lttng_kconsumer_on_recv_stream(stream); | |
7753dea8 MD |
3602 | case LTTNG_CONSUMER32_UST: |
3603 | case LTTNG_CONSUMER64_UST: | |
d41f73b7 MD |
3604 | return lttng_ustconsumer_on_recv_stream(stream); |
3605 | default: | |
3606 | ERR("Unknown consumer_data type"); | |
3607 | assert(0); | |
3608 | return -ENOSYS; | |
3609 | } | |
3610 | } | |
e4421fec DG |
3611 | |
3612 | /* | |
3613 | * Allocate and set consumer data hash tables. | |
3614 | */ | |
282dadbc | 3615 | int lttng_consumer_init(void) |
e4421fec | 3616 | { |
d88aee68 | 3617 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3618 | if (!consumer_data.channel_ht) { |
3619 | goto error; | |
3620 | } | |
3621 | ||
5c3892a6 JG |
3622 | consumer_data.channels_by_session_id_ht = |
3623 | lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3624 | if (!consumer_data.channels_by_session_id_ht) { | |
3625 | goto error; | |
3626 | } | |
3627 | ||
d88aee68 | 3628 | consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3629 | if (!consumer_data.relayd_ht) { |
3630 | goto error; | |
3631 | } | |
3632 | ||
d88aee68 | 3633 | consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3634 | if (!consumer_data.stream_list_ht) { |
3635 | goto error; | |
3636 | } | |
3637 | ||
d8ef542d | 3638 | consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
282dadbc MD |
3639 | if (!consumer_data.stream_per_chan_id_ht) { |
3640 | goto error; | |
3641 | } | |
3642 | ||
3643 | data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3644 | if (!data_ht) { | |
3645 | goto error; | |
3646 | } | |
3647 | ||
3648 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
3649 | if (!metadata_ht) { | |
3650 | goto error; | |
3651 | } | |
3652 | ||
28cc88f3 JG |
3653 | consumer_data.chunk_registry = lttng_trace_chunk_registry_create(); |
3654 | if (!consumer_data.chunk_registry) { | |
3655 | goto error; | |
3656 | } | |
3657 | ||
282dadbc MD |
3658 | return 0; |
3659 | ||
3660 | error: | |
3661 | return -1; | |
e4421fec | 3662 | } |
7735ef9e DG |
3663 | |
3664 | /* | |
3665 | * Process the ADD_RELAYD command receive by a consumer. | |
3666 | * | |
3667 | * This will create a relayd socket pair and add it to the relayd hash table. | |
3668 | * The caller MUST acquire a RCU read side lock before calling it. | |
3669 | */ | |
2527bf85 | 3670 | void consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type, |
7735ef9e | 3671 | struct lttng_consumer_local_data *ctx, int sock, |
6151a90f | 3672 | struct pollfd *consumer_sockpoll, |
d3e2ba59 JD |
3673 | struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id, |
3674 | uint64_t relayd_session_id) | |
7735ef9e | 3675 | { |
cd2b09ed | 3676 | int fd = -1, ret = -1, relayd_created = 0; |
0c759fc9 | 3677 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
d4298c99 | 3678 | struct consumer_relayd_sock_pair *relayd = NULL; |
7735ef9e | 3679 | |
6151a90f JD |
3680 | assert(ctx); |
3681 | assert(relayd_sock); | |
3682 | ||
da009f2c | 3683 | DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx); |
7735ef9e DG |
3684 | |
3685 | /* Get relayd reference if exists. */ | |
3686 | relayd = consumer_find_relayd(net_seq_idx); | |
3687 | if (relayd == NULL) { | |
da009f2c | 3688 | assert(sock_type == LTTNG_STREAM_CONTROL); |
7735ef9e DG |
3689 | /* Not found. Allocate one. */ |
3690 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); | |
3691 | if (relayd == NULL) { | |
618a6a28 MD |
3692 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
3693 | goto error; | |
0d08d75e | 3694 | } else { |
30319bcb | 3695 | relayd->sessiond_session_id = sessiond_id; |
0d08d75e | 3696 | relayd_created = 1; |
7735ef9e | 3697 | } |
0d08d75e DG |
3698 | |
3699 | /* | |
3700 | * This code path MUST continue to the consumer send status message to | |
3701 | * we can notify the session daemon and continue our work without | |
3702 | * killing everything. | |
3703 | */ | |
da009f2c MD |
3704 | } else { |
3705 | /* | |
3706 | * relayd key should never be found for control socket. | |
3707 | */ | |
3708 | assert(sock_type != LTTNG_STREAM_CONTROL); | |
0d08d75e DG |
3709 | } |
3710 | ||
3711 | /* First send a status message before receiving the fds. */ | |
0c759fc9 | 3712 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
618a6a28 | 3713 | if (ret < 0) { |
0d08d75e | 3714 | /* Somehow, the session daemon is not responding anymore. */ |
618a6a28 MD |
3715 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
3716 | goto error_nosignal; | |
7735ef9e DG |
3717 | } |
3718 | ||
3719 | /* Poll on consumer socket. */ | |
84382d49 MD |
3720 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
3721 | if (ret) { | |
3722 | /* Needing to exit in the middle of a command: error. */ | |
0d08d75e | 3723 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
618a6a28 | 3724 | goto error_nosignal; |
7735ef9e DG |
3725 | } |
3726 | ||
3727 | /* Get relayd socket from session daemon */ | |
3728 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); | |
3729 | if (ret != sizeof(fd)) { | |
4028eeb9 | 3730 | fd = -1; /* Just in case it gets set with an invalid value. */ |
0d08d75e DG |
3731 | |
3732 | /* | |
3733 | * Failing to receive FDs might indicate a major problem such as | |
3734 | * reaching a fd limit during the receive where the kernel returns a | |
3735 | * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we | |
3736 | * don't take any chances and stop everything. | |
3737 | * | |
3738 | * XXX: Feature request #558 will fix that and avoid this possible | |
3739 | * issue when reaching the fd limit. | |
3740 | */ | |
3741 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); | |
618a6a28 | 3742 | ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD; |
f50f23d9 DG |
3743 | goto error; |
3744 | } | |
3745 | ||
7735ef9e DG |
3746 | /* Copy socket information and received FD */ |
3747 | switch (sock_type) { | |
3748 | case LTTNG_STREAM_CONTROL: | |
3749 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3750 | lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock); |
3751 | ret = lttcomm_create_sock(&relayd->control_sock.sock); | |
4028eeb9 | 3752 | /* Handle create_sock error. */ |
f66c074c | 3753 | if (ret < 0) { |
618a6a28 | 3754 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3755 | goto error; |
f66c074c | 3756 | } |
da009f2c MD |
3757 | /* |
3758 | * Close the socket created internally by | |
3759 | * lttcomm_create_sock, so we can replace it by the one | |
3760 | * received from sessiond. | |
3761 | */ | |
3762 | if (close(relayd->control_sock.sock.fd)) { | |
3763 | PERROR("close"); | |
3764 | } | |
7735ef9e DG |
3765 | |
3766 | /* Assign new file descriptor */ | |
6151a90f JD |
3767 | relayd->control_sock.sock.fd = fd; |
3768 | /* Assign version values. */ | |
3769 | relayd->control_sock.major = relayd_sock->major; | |
3770 | relayd->control_sock.minor = relayd_sock->minor; | |
c5b6f4f0 | 3771 | |
d3e2ba59 | 3772 | relayd->relayd_session_id = relayd_session_id; |
c5b6f4f0 | 3773 | |
7735ef9e DG |
3774 | break; |
3775 | case LTTNG_STREAM_DATA: | |
3776 | /* Copy received lttcomm socket */ | |
6151a90f JD |
3777 | lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock); |
3778 | ret = lttcomm_create_sock(&relayd->data_sock.sock); | |
4028eeb9 | 3779 | /* Handle create_sock error. */ |
f66c074c | 3780 | if (ret < 0) { |
618a6a28 | 3781 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
4028eeb9 | 3782 | goto error; |
f66c074c | 3783 | } |
da009f2c MD |
3784 | /* |
3785 | * Close the socket created internally by | |
3786 | * lttcomm_create_sock, so we can replace it by the one | |
3787 | * received from sessiond. | |
3788 | */ | |
3789 | if (close(relayd->data_sock.sock.fd)) { | |
3790 | PERROR("close"); | |
3791 | } | |
7735ef9e DG |
3792 | |
3793 | /* Assign new file descriptor */ | |
6151a90f JD |
3794 | relayd->data_sock.sock.fd = fd; |
3795 | /* Assign version values. */ | |
3796 | relayd->data_sock.major = relayd_sock->major; | |
3797 | relayd->data_sock.minor = relayd_sock->minor; | |
7735ef9e DG |
3798 | break; |
3799 | default: | |
3800 | ERR("Unknown relayd socket type (%d)", sock_type); | |
618a6a28 | 3801 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
7735ef9e DG |
3802 | goto error; |
3803 | } | |
3804 | ||
d88aee68 | 3805 | DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)", |
7735ef9e DG |
3806 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
3807 | relayd->net_seq_idx, fd); | |
39d9954c FD |
3808 | /* |
3809 | * We gave the ownership of the fd to the relayd structure. Set the | |
3810 | * fd to -1 so we don't call close() on it in the error path below. | |
3811 | */ | |
3812 | fd = -1; | |
7735ef9e | 3813 | |
618a6a28 MD |
3814 | /* We successfully added the socket. Send status back. */ |
3815 | ret = consumer_send_status_msg(sock, ret_code); | |
3816 | if (ret < 0) { | |
3817 | /* Somehow, the session daemon is not responding anymore. */ | |
3818 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3819 | goto error_nosignal; | |
3820 | } | |
3821 | ||
7735ef9e DG |
3822 | /* |
3823 | * Add relayd socket pair to consumer data hashtable. If object already | |
3824 | * exists or on error, the function gracefully returns. | |
3825 | */ | |
9276e5c8 | 3826 | relayd->ctx = ctx; |
d09e1200 | 3827 | add_relayd(relayd); |
7735ef9e DG |
3828 | |
3829 | /* All good! */ | |
2527bf85 | 3830 | return; |
7735ef9e DG |
3831 | |
3832 | error: | |
618a6a28 MD |
3833 | if (consumer_send_status_msg(sock, ret_code) < 0) { |
3834 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); | |
3835 | } | |
3836 | ||
3837 | error_nosignal: | |
4028eeb9 DG |
3838 | /* Close received socket if valid. */ |
3839 | if (fd >= 0) { | |
3840 | if (close(fd)) { | |
3841 | PERROR("close received socket"); | |
3842 | } | |
3843 | } | |
cd2b09ed DG |
3844 | |
3845 | if (relayd_created) { | |
cd2b09ed DG |
3846 | free(relayd); |
3847 | } | |
7735ef9e | 3848 | } |
ca22feea | 3849 | |
f7079f67 DG |
3850 | /* |
3851 | * Search for a relayd associated to the session id and return the reference. | |
3852 | * | |
3853 | * A rcu read side lock MUST be acquire before calling this function and locked | |
3854 | * until the relayd object is no longer necessary. | |
3855 | */ | |
3856 | static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id) | |
3857 | { | |
3858 | struct lttng_ht_iter iter; | |
f7079f67 | 3859 | struct consumer_relayd_sock_pair *relayd = NULL; |
f7079f67 DG |
3860 | |
3861 | /* Iterate over all relayd since they are indexed by net_seq_idx. */ | |
3862 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, | |
3863 | node.node) { | |
18261bd1 DG |
3864 | /* |
3865 | * Check by sessiond id which is unique here where the relayd session | |
3866 | * id might not be when having multiple relayd. | |
3867 | */ | |
3868 | if (relayd->sessiond_session_id == id) { | |
f7079f67 | 3869 | /* Found the relayd. There can be only one per id. */ |
18261bd1 | 3870 | goto found; |
f7079f67 DG |
3871 | } |
3872 | } | |
3873 | ||
18261bd1 DG |
3874 | return NULL; |
3875 | ||
3876 | found: | |
f7079f67 DG |
3877 | return relayd; |
3878 | } | |
3879 | ||
ca22feea DG |
3880 | /* |
3881 | * Check if for a given session id there is still data needed to be extract | |
3882 | * from the buffers. | |
3883 | * | |
6d805429 | 3884 | * Return 1 if data is pending or else 0 meaning ready to be read. |
ca22feea | 3885 | */ |
6d805429 | 3886 | int consumer_data_pending(uint64_t id) |
ca22feea DG |
3887 | { |
3888 | int ret; | |
3889 | struct lttng_ht_iter iter; | |
3890 | struct lttng_ht *ht; | |
3891 | struct lttng_consumer_stream *stream; | |
f7079f67 | 3892 | struct consumer_relayd_sock_pair *relayd = NULL; |
6d805429 | 3893 | int (*data_pending)(struct lttng_consumer_stream *); |
ca22feea | 3894 | |
6d805429 | 3895 | DBG("Consumer data pending command on session id %" PRIu64, id); |
ca22feea | 3896 | |
6f6eda74 | 3897 | rcu_read_lock(); |
ca22feea DG |
3898 | pthread_mutex_lock(&consumer_data.lock); |
3899 | ||
3900 | switch (consumer_data.type) { | |
3901 | case LTTNG_CONSUMER_KERNEL: | |
6d805429 | 3902 | data_pending = lttng_kconsumer_data_pending; |
ca22feea DG |
3903 | break; |
3904 | case LTTNG_CONSUMER32_UST: | |
3905 | case LTTNG_CONSUMER64_UST: | |
6d805429 | 3906 | data_pending = lttng_ustconsumer_data_pending; |
ca22feea DG |
3907 | break; |
3908 | default: | |
3909 | ERR("Unknown consumer data type"); | |
3910 | assert(0); | |
3911 | } | |
3912 | ||
3913 | /* Ease our life a bit */ | |
3914 | ht = consumer_data.stream_list_ht; | |
3915 | ||
c8f59ee5 | 3916 | cds_lfht_for_each_entry_duplicate(ht->ht, |
d88aee68 DG |
3917 | ht->hash_fct(&id, lttng_ht_seed), |
3918 | ht->match_fct, &id, | |
ca22feea | 3919 | &iter.iter, stream, node_session_id.node) { |
bb586a6e | 3920 | pthread_mutex_lock(&stream->lock); |
ca22feea | 3921 | |
4e9a4686 DG |
3922 | /* |
3923 | * A removed node from the hash table indicates that the stream has | |
3924 | * been deleted thus having a guarantee that the buffers are closed | |
3925 | * on the consumer side. However, data can still be transmitted | |
3926 | * over the network so don't skip the relayd check. | |
3927 | */ | |
3928 | ret = cds_lfht_is_node_deleted(&stream->node.node); | |
3929 | if (!ret) { | |
3930 | /* Check the stream if there is data in the buffers. */ | |
6d805429 DG |
3931 | ret = data_pending(stream); |
3932 | if (ret == 1) { | |
4e9a4686 | 3933 | pthread_mutex_unlock(&stream->lock); |
f7079f67 | 3934 | goto data_pending; |
4e9a4686 DG |
3935 | } |
3936 | } | |
3937 | ||
d9f0c7c7 JR |
3938 | pthread_mutex_unlock(&stream->lock); |
3939 | } | |
3940 | ||
3941 | relayd = find_relayd_by_session_id(id); | |
3942 | if (relayd) { | |
3943 | unsigned int is_data_inflight = 0; | |
3944 | ||
3945 | /* Send init command for data pending. */ | |
3946 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
3947 | ret = relayd_begin_data_pending(&relayd->control_sock, | |
3948 | relayd->relayd_session_id); | |
3949 | if (ret < 0) { | |
3950 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
3951 | /* Communication error thus the relayd so no data pending. */ | |
3952 | goto data_not_pending; | |
3953 | } | |
3954 | ||
3955 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
3956 | ht->hash_fct(&id, lttng_ht_seed), | |
3957 | ht->match_fct, &id, | |
3958 | &iter.iter, stream, node_session_id.node) { | |
c8f59ee5 | 3959 | if (stream->metadata_flag) { |
ad7051c0 DG |
3960 | ret = relayd_quiescent_control(&relayd->control_sock, |
3961 | stream->relayd_stream_id); | |
c8f59ee5 | 3962 | } else { |
6d805429 | 3963 | ret = relayd_data_pending(&relayd->control_sock, |
39df6d9f DG |
3964 | stream->relayd_stream_id, |
3965 | stream->next_net_seq_num - 1); | |
c8f59ee5 | 3966 | } |
d9f0c7c7 JR |
3967 | |
3968 | if (ret == 1) { | |
3969 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
3970 | goto data_pending; | |
3971 | } else if (ret < 0) { | |
9276e5c8 JR |
3972 | ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
3973 | lttng_consumer_cleanup_relayd(relayd); | |
3974 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
9276e5c8 JR |
3975 | goto data_not_pending; |
3976 | } | |
c8f59ee5 | 3977 | } |
f7079f67 | 3978 | |
d9f0c7c7 | 3979 | /* Send end command for data pending. */ |
f7079f67 DG |
3980 | ret = relayd_end_data_pending(&relayd->control_sock, |
3981 | relayd->relayd_session_id, &is_data_inflight); | |
3982 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
bdd88757 | 3983 | if (ret < 0) { |
9276e5c8 JR |
3984 | ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
3985 | lttng_consumer_cleanup_relayd(relayd); | |
f7079f67 DG |
3986 | goto data_not_pending; |
3987 | } | |
bdd88757 DG |
3988 | if (is_data_inflight) { |
3989 | goto data_pending; | |
3990 | } | |
f7079f67 DG |
3991 | } |
3992 | ||
ca22feea | 3993 | /* |
f7079f67 DG |
3994 | * Finding _no_ node in the hash table and no inflight data means that the |
3995 | * stream(s) have been removed thus data is guaranteed to be available for | |
3996 | * analysis from the trace files. | |
ca22feea DG |
3997 | */ |
3998 | ||
f7079f67 | 3999 | data_not_pending: |
ca22feea DG |
4000 | /* Data is available to be read by a viewer. */ |
4001 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 4002 | rcu_read_unlock(); |
6d805429 | 4003 | return 0; |
ca22feea | 4004 | |
f7079f67 | 4005 | data_pending: |
ca22feea DG |
4006 | /* Data is still being extracted from buffers. */ |
4007 | pthread_mutex_unlock(&consumer_data.lock); | |
c8f59ee5 | 4008 | rcu_read_unlock(); |
6d805429 | 4009 | return 1; |
ca22feea | 4010 | } |
f50f23d9 DG |
4011 | |
4012 | /* | |
4013 | * Send a ret code status message to the sessiond daemon. | |
4014 | * | |
4015 | * Return the sendmsg() return value. | |
4016 | */ | |
4017 | int consumer_send_status_msg(int sock, int ret_code) | |
4018 | { | |
4019 | struct lttcomm_consumer_status_msg msg; | |
4020 | ||
53efb85a | 4021 | memset(&msg, 0, sizeof(msg)); |
f50f23d9 DG |
4022 | msg.ret_code = ret_code; |
4023 | ||
4024 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
4025 | } | |
ffe60014 DG |
4026 | |
4027 | /* | |
4028 | * Send a channel status message to the sessiond daemon. | |
4029 | * | |
4030 | * Return the sendmsg() return value. | |
4031 | */ | |
4032 | int consumer_send_status_channel(int sock, | |
4033 | struct lttng_consumer_channel *channel) | |
4034 | { | |
4035 | struct lttcomm_consumer_status_channel msg; | |
4036 | ||
4037 | assert(sock >= 0); | |
4038 | ||
53efb85a | 4039 | memset(&msg, 0, sizeof(msg)); |
ffe60014 | 4040 | if (!channel) { |
0c759fc9 | 4041 | msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; |
ffe60014 | 4042 | } else { |
0c759fc9 | 4043 | msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
ffe60014 DG |
4044 | msg.key = channel->key; |
4045 | msg.stream_count = channel->streams.count; | |
4046 | } | |
4047 | ||
4048 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
4049 | } | |
5c786ded | 4050 | |
d07ceecd MD |
4051 | unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos, |
4052 | unsigned long produced_pos, uint64_t nb_packets_per_stream, | |
4053 | uint64_t max_sb_size) | |
5c786ded | 4054 | { |
d07ceecd | 4055 | unsigned long start_pos; |
5c786ded | 4056 | |
d07ceecd MD |
4057 | if (!nb_packets_per_stream) { |
4058 | return consumed_pos; /* Grab everything */ | |
4059 | } | |
4060 | start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size); | |
4061 | start_pos -= max_sb_size * nb_packets_per_stream; | |
4062 | if ((long) (start_pos - consumed_pos) < 0) { | |
4063 | return consumed_pos; /* Grab everything */ | |
4064 | } | |
4065 | return start_pos; | |
5c786ded | 4066 | } |
a1ae2ea5 | 4067 | |
b99a8d42 JD |
4068 | /* |
4069 | * Sample the rotate position for all the streams of a channel. If a stream | |
4070 | * is already at the rotate position (produced == consumed), we flag it as | |
4071 | * ready for rotation. The rotation of ready streams occurs after we have | |
4072 | * replied to the session daemon that we have finished sampling the positions. | |
92b7a7f8 | 4073 | * Must be called with RCU read-side lock held to ensure existence of channel. |
b99a8d42 JD |
4074 | * |
4075 | * Returns 0 on success, < 0 on error | |
4076 | */ | |
92b7a7f8 | 4077 | int lttng_consumer_rotate_channel(struct lttng_consumer_channel *channel, |
d2956687 | 4078 | uint64_t key, uint64_t relayd_id, uint32_t metadata, |
b99a8d42 JD |
4079 | struct lttng_consumer_local_data *ctx) |
4080 | { | |
4081 | int ret; | |
b99a8d42 JD |
4082 | struct lttng_consumer_stream *stream; |
4083 | struct lttng_ht_iter iter; | |
4084 | struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht; | |
c35f9726 JG |
4085 | struct lttng_dynamic_array stream_rotation_positions; |
4086 | uint64_t next_chunk_id, stream_count = 0; | |
4087 | enum lttng_trace_chunk_status chunk_status; | |
4088 | const bool is_local_trace = relayd_id == -1ULL; | |
4089 | struct consumer_relayd_sock_pair *relayd = NULL; | |
4090 | bool rotating_to_new_chunk = true; | |
e537f276 JG |
4091 | /* Array of `struct lttng_consumer_stream *` */ |
4092 | struct lttng_dynamic_pointer_array streams_packet_to_open; | |
4093 | size_t stream_idx; | |
b99a8d42 JD |
4094 | |
4095 | DBG("Consumer sample rotate position for channel %" PRIu64, key); | |
4096 | ||
c35f9726 JG |
4097 | lttng_dynamic_array_init(&stream_rotation_positions, |
4098 | sizeof(struct relayd_stream_rotation_position), NULL); | |
e537f276 | 4099 | lttng_dynamic_pointer_array_init(&streams_packet_to_open, NULL); |
c35f9726 | 4100 | |
b99a8d42 JD |
4101 | rcu_read_lock(); |
4102 | ||
b99a8d42 | 4103 | pthread_mutex_lock(&channel->lock); |
c35f9726 JG |
4104 | assert(channel->trace_chunk); |
4105 | chunk_status = lttng_trace_chunk_get_id(channel->trace_chunk, | |
4106 | &next_chunk_id); | |
4107 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4108 | ret = -1; | |
4109 | goto end_unlock_channel; | |
4110 | } | |
b99a8d42 JD |
4111 | |
4112 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
4113 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
4114 | ht->match_fct, &channel->key, &iter.iter, | |
4115 | stream, node_channel_id.node) { | |
a40a503f | 4116 | unsigned long produced_pos = 0, consumed_pos = 0; |
b99a8d42 JD |
4117 | |
4118 | health_code_update(); | |
4119 | ||
4120 | /* | |
4121 | * Lock stream because we are about to change its state. | |
4122 | */ | |
4123 | pthread_mutex_lock(&stream->lock); | |
4124 | ||
c35f9726 JG |
4125 | if (stream->trace_chunk == stream->chan->trace_chunk) { |
4126 | rotating_to_new_chunk = false; | |
4127 | } | |
4128 | ||
a40a503f | 4129 | /* |
a9dde553 MD |
4130 | * Do not flush an empty packet when rotating from a NULL trace |
4131 | * chunk. The stream has no means to output data, and the prior | |
4132 | * rotation which rotated to NULL performed that side-effect already. | |
a40a503f | 4133 | */ |
a9dde553 MD |
4134 | if (stream->trace_chunk) { |
4135 | /* | |
4136 | * For metadata stream, do an active flush, which does not | |
4137 | * produce empty packets. For data streams, empty-flush; | |
4138 | * ensures we have at least one packet in each stream per trace | |
4139 | * chunk, even if no data was produced. | |
4140 | */ | |
4141 | ret = consumer_flush_buffer(stream, stream->metadata_flag ? 1 : 0); | |
4142 | if (ret < 0) { | |
4143 | ERR("Failed to flush stream %" PRIu64 " during channel rotation", | |
4144 | stream->key); | |
4145 | goto end_unlock_stream; | |
4146 | } | |
b99a8d42 JD |
4147 | } |
4148 | ||
a40a503f MD |
4149 | ret = lttng_consumer_take_snapshot(stream); |
4150 | if (ret < 0 && ret != -ENODATA && ret != -EAGAIN) { | |
4151 | ERR("Failed to sample snapshot position during channel rotation"); | |
b99a8d42 JD |
4152 | goto end_unlock_stream; |
4153 | } | |
a40a503f MD |
4154 | if (!ret) { |
4155 | ret = lttng_consumer_get_produced_snapshot(stream, | |
4156 | &produced_pos); | |
4157 | if (ret < 0) { | |
4158 | ERR("Failed to sample produced position during channel rotation"); | |
4159 | goto end_unlock_stream; | |
4160 | } | |
b99a8d42 | 4161 | |
a40a503f MD |
4162 | ret = lttng_consumer_get_consumed_snapshot(stream, |
4163 | &consumed_pos); | |
4164 | if (ret < 0) { | |
4165 | ERR("Failed to sample consumed position during channel rotation"); | |
4166 | goto end_unlock_stream; | |
4167 | } | |
4168 | } | |
4169 | /* | |
4170 | * Align produced position on the start-of-packet boundary of the first | |
4171 | * packet going into the next trace chunk. | |
4172 | */ | |
4173 | produced_pos = ALIGN_FLOOR(produced_pos, stream->max_sb_size); | |
4174 | if (consumed_pos == produced_pos) { | |
f8528c7a MD |
4175 | DBG("Set rotate ready for stream %" PRIu64 " produced = %lu consumed = %lu", |
4176 | stream->key, produced_pos, consumed_pos); | |
b99a8d42 | 4177 | stream->rotate_ready = true; |
f8528c7a MD |
4178 | } else { |
4179 | DBG("Different consumed and produced positions " | |
4180 | "for stream %" PRIu64 " produced = %lu consumed = %lu", | |
4181 | stream->key, produced_pos, consumed_pos); | |
b99a8d42 | 4182 | } |
633d0182 | 4183 | /* |
a40a503f MD |
4184 | * The rotation position is based on the packet_seq_num of the |
4185 | * packet following the last packet that was consumed for this | |
4186 | * stream, incremented by the offset between produced and | |
4187 | * consumed positions. This rotation position is a lower bound | |
4188 | * (inclusive) at which the next trace chunk starts. Since it | |
4189 | * is a lower bound, it is OK if the packet_seq_num does not | |
4190 | * correspond exactly to the same packet identified by the | |
4191 | * consumed_pos, which can happen in overwrite mode. | |
633d0182 | 4192 | */ |
a40a503f MD |
4193 | if (stream->sequence_number_unavailable) { |
4194 | /* | |
4195 | * Rotation should never be performed on a session which | |
4196 | * interacts with a pre-2.8 lttng-modules, which does | |
4197 | * not implement packet sequence number. | |
4198 | */ | |
4199 | ERR("Failure to rotate stream %" PRIu64 ": sequence number unavailable", | |
b99a8d42 | 4200 | stream->key); |
a40a503f | 4201 | ret = -1; |
b99a8d42 JD |
4202 | goto end_unlock_stream; |
4203 | } | |
a40a503f MD |
4204 | stream->rotate_position = stream->last_sequence_number + 1 + |
4205 | ((produced_pos - consumed_pos) / stream->max_sb_size); | |
f8528c7a MD |
4206 | DBG("Set rotation position for stream %" PRIu64 " at position %" PRIu64, |
4207 | stream->key, stream->rotate_position); | |
b99a8d42 | 4208 | |
c35f9726 | 4209 | if (!is_local_trace) { |
633d0182 JG |
4210 | /* |
4211 | * The relay daemon control protocol expects a rotation | |
4212 | * position as "the sequence number of the first packet | |
a40a503f | 4213 | * _after_ the current trace chunk". |
633d0182 | 4214 | */ |
c35f9726 JG |
4215 | const struct relayd_stream_rotation_position position = { |
4216 | .stream_id = stream->relayd_stream_id, | |
a40a503f | 4217 | .rotate_at_seq_num = stream->rotate_position, |
c35f9726 JG |
4218 | }; |
4219 | ||
4220 | ret = lttng_dynamic_array_add_element( | |
4221 | &stream_rotation_positions, | |
4222 | &position); | |
4223 | if (ret) { | |
4224 | ERR("Failed to allocate stream rotation position"); | |
4225 | goto end_unlock_stream; | |
4226 | } | |
4227 | stream_count++; | |
4228 | } | |
fc0c52e9 JG |
4229 | |
4230 | stream->opened_packet_in_current_trace_chunk = false; | |
4231 | ||
4232 | if (rotating_to_new_chunk && !stream->metadata_flag) { | |
4233 | /* | |
4234 | * Attempt to flush an empty packet as close to the | |
4235 | * rotation point as possible. In the event where a | |
4236 | * stream remains inactive after the rotation point, | |
4237 | * this ensures that the new trace chunk has a | |
4238 | * beginning timestamp set at the begining of the | |
4239 | * trace chunk instead of only creating an empty | |
4240 | * packet when the trace chunk is stopped. | |
4241 | * | |
4242 | * This indicates to the viewers that the stream | |
4243 | * was being recorded, but more importantly it | |
4244 | * allows viewers to determine a useable trace | |
4245 | * intersection. | |
4246 | * | |
4247 | * This presents a problem in the case where the | |
4248 | * ring-buffer is completely full. | |
4249 | * | |
4250 | * Consider the following scenario: | |
4251 | * - The consumption of data is slow (slow network, | |
4252 | * for instance), | |
4253 | * - The ring buffer is full, | |
4254 | * - A rotation is initiated, | |
4255 | * - The flush below does nothing (no space left to | |
4256 | * open a new packet), | |
4257 | * - The other streams rotate very soon, and new | |
4258 | * data is produced in the new chunk, | |
4259 | * - This stream completes its rotation long after the | |
4260 | * rotation was initiated | |
4261 | * - The session is stopped before any event can be | |
4262 | * produced in this stream's buffers. | |
4263 | * | |
4264 | * The resulting trace chunk will have a single packet | |
4265 | * temporaly at the end of the trace chunk for this | |
4266 | * stream making the stream intersection more narrow | |
4267 | * than it should be. | |
4268 | * | |
4269 | * To work-around this, an empty flush is performed | |
4270 | * after the first consumption of a packet during a | |
4271 | * rotation if open_packet fails. The idea is that | |
4272 | * consuming a packet frees enough space to switch | |
4273 | * packets in this scenario and allows the tracer to | |
4274 | * "stamp" the beginning of the new trace chunk at the | |
4275 | * earliest possible point. | |
e537f276 JG |
4276 | * |
4277 | * The packet open is performed after the channel | |
4278 | * rotation to ensure that no attempt to open a packet | |
4279 | * is performed in a stream that has no active trace | |
4280 | * chunk. | |
fc0c52e9 | 4281 | */ |
e537f276 JG |
4282 | ret = lttng_dynamic_pointer_array_add_pointer( |
4283 | &streams_packet_to_open, stream); | |
4284 | if (ret) { | |
4285 | PERROR("Failed to add a stream pointer to array of streams in which to open a packet"); | |
fc0c52e9 JG |
4286 | ret = -1; |
4287 | goto end_unlock_stream; | |
fc0c52e9 JG |
4288 | } |
4289 | } | |
4290 | ||
b99a8d42 JD |
4291 | pthread_mutex_unlock(&stream->lock); |
4292 | } | |
c35f9726 | 4293 | stream = NULL; |
b99a8d42 | 4294 | |
e537f276 JG |
4295 | if (!is_local_trace) { |
4296 | relayd = consumer_find_relayd(relayd_id); | |
4297 | if (!relayd) { | |
4298 | ERR("Failed to find relayd %" PRIu64, relayd_id); | |
4299 | ret = -1; | |
4300 | goto end_unlock_channel; | |
4301 | } | |
c35f9726 | 4302 | |
e537f276 JG |
4303 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
4304 | ret = relayd_rotate_streams(&relayd->control_sock, stream_count, | |
4305 | rotating_to_new_chunk ? &next_chunk_id : NULL, | |
4306 | (const struct relayd_stream_rotation_position *) | |
4307 | stream_rotation_positions.buffer | |
4308 | .data); | |
4309 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
4310 | if (ret < 0) { | |
4311 | ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64, | |
4312 | relayd->net_seq_idx); | |
4313 | lttng_consumer_cleanup_relayd(relayd); | |
4314 | goto end_unlock_channel; | |
4315 | } | |
c35f9726 JG |
4316 | } |
4317 | ||
e537f276 JG |
4318 | for (stream_idx = 0; |
4319 | stream_idx < lttng_dynamic_pointer_array_get_count( | |
4320 | &streams_packet_to_open); | |
4321 | stream_idx++) { | |
4322 | enum open_packet_status status; | |
4323 | ||
4324 | stream = lttng_dynamic_pointer_array_get_pointer( | |
4325 | &streams_packet_to_open, stream_idx); | |
4326 | ||
4327 | pthread_mutex_lock(&stream->lock); | |
4328 | status = open_packet(stream); | |
4329 | pthread_mutex_unlock(&stream->lock); | |
4330 | switch (status) { | |
4331 | case OPEN_PACKET_STATUS_OPENED: | |
4332 | DBG("Opened a packet after a rotation: stream id = %" PRIu64 | |
4333 | ", channel name = %s, session id = %" PRIu64, | |
4334 | stream->key, stream->chan->name, | |
4335 | stream->chan->session_id); | |
4336 | break; | |
4337 | case OPEN_PACKET_STATUS_NO_SPACE: | |
4338 | /* | |
4339 | * Can't open a packet as there is no space left | |
4340 | * in the buffer. A new packet will be opened | |
4341 | * once one has been consumed. | |
4342 | */ | |
4343 | DBG("No space left to open a packet after a rotation: stream id = %" PRIu64 | |
4344 | ", channel name = %s, session id = %" PRIu64, | |
4345 | stream->key, stream->chan->name, | |
4346 | stream->chan->session_id); | |
4347 | break; | |
4348 | case OPEN_PACKET_STATUS_ERROR: | |
4349 | /* Logged by callee. */ | |
4350 | ret = -1; | |
5e285896 | 4351 | goto end_unlock_channel; |
e537f276 JG |
4352 | default: |
4353 | abort(); | |
4354 | } | |
c35f9726 JG |
4355 | } |
4356 | ||
e537f276 | 4357 | pthread_mutex_unlock(&channel->lock); |
b99a8d42 JD |
4358 | ret = 0; |
4359 | goto end; | |
4360 | ||
4361 | end_unlock_stream: | |
4362 | pthread_mutex_unlock(&stream->lock); | |
c35f9726 | 4363 | end_unlock_channel: |
b99a8d42 JD |
4364 | pthread_mutex_unlock(&channel->lock); |
4365 | end: | |
4366 | rcu_read_unlock(); | |
c35f9726 | 4367 | lttng_dynamic_array_reset(&stream_rotation_positions); |
e537f276 | 4368 | lttng_dynamic_pointer_array_reset(&streams_packet_to_open); |
b99a8d42 JD |
4369 | return ret; |
4370 | } | |
4371 | ||
5f3aff8b MD |
4372 | static |
4373 | int consumer_clear_buffer(struct lttng_consumer_stream *stream) | |
4374 | { | |
4375 | int ret = 0; | |
4376 | unsigned long consumed_pos_before, consumed_pos_after; | |
4377 | ||
4378 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
4379 | if (ret < 0) { | |
4380 | ERR("Taking snapshot positions"); | |
4381 | goto end; | |
4382 | } | |
4383 | ||
4384 | ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_before); | |
4385 | if (ret < 0) { | |
4386 | ERR("Consumed snapshot position"); | |
4387 | goto end; | |
4388 | } | |
4389 | ||
4390 | switch (consumer_data.type) { | |
4391 | case LTTNG_CONSUMER_KERNEL: | |
4392 | ret = kernctl_buffer_clear(stream->wait_fd); | |
4393 | if (ret < 0) { | |
95c4203e | 4394 | ERR("Failed to clear kernel stream (ret = %d)", ret); |
5f3aff8b MD |
4395 | goto end; |
4396 | } | |
4397 | break; | |
4398 | case LTTNG_CONSUMER32_UST: | |
4399 | case LTTNG_CONSUMER64_UST: | |
4400 | lttng_ustconsumer_clear_buffer(stream); | |
4401 | break; | |
4402 | default: | |
4403 | ERR("Unknown consumer_data type"); | |
4404 | abort(); | |
4405 | } | |
4406 | ||
4407 | ret = lttng_consumer_sample_snapshot_positions(stream); | |
4408 | if (ret < 0) { | |
4409 | ERR("Taking snapshot positions"); | |
4410 | goto end; | |
4411 | } | |
4412 | ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_after); | |
4413 | if (ret < 0) { | |
4414 | ERR("Consumed snapshot position"); | |
4415 | goto end; | |
4416 | } | |
4417 | DBG("clear: before: %lu after: %lu", consumed_pos_before, consumed_pos_after); | |
4418 | end: | |
4419 | return ret; | |
4420 | } | |
4421 | ||
4422 | static | |
4423 | int consumer_clear_stream(struct lttng_consumer_stream *stream) | |
4424 | { | |
4425 | int ret; | |
4426 | ||
4427 | ret = consumer_flush_buffer(stream, 1); | |
4428 | if (ret < 0) { | |
4429 | ERR("Failed to flush stream %" PRIu64 " during channel clear", | |
4430 | stream->key); | |
4431 | ret = LTTCOMM_CONSUMERD_FATAL; | |
4432 | goto error; | |
4433 | } | |
4434 | ||
4435 | ret = consumer_clear_buffer(stream); | |
4436 | if (ret < 0) { | |
4437 | ERR("Failed to clear stream %" PRIu64 " during channel clear", | |
4438 | stream->key); | |
4439 | ret = LTTCOMM_CONSUMERD_FATAL; | |
4440 | goto error; | |
4441 | } | |
4442 | ||
4443 | ret = LTTCOMM_CONSUMERD_SUCCESS; | |
4444 | error: | |
4445 | return ret; | |
4446 | } | |
4447 | ||
4448 | static | |
4449 | int consumer_clear_unmonitored_channel(struct lttng_consumer_channel *channel) | |
4450 | { | |
4451 | int ret; | |
4452 | struct lttng_consumer_stream *stream; | |
4453 | ||
4454 | rcu_read_lock(); | |
4455 | pthread_mutex_lock(&channel->lock); | |
4456 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
4457 | health_code_update(); | |
4458 | pthread_mutex_lock(&stream->lock); | |
4459 | ret = consumer_clear_stream(stream); | |
4460 | if (ret) { | |
4461 | goto error_unlock; | |
4462 | } | |
4463 | pthread_mutex_unlock(&stream->lock); | |
4464 | } | |
4465 | pthread_mutex_unlock(&channel->lock); | |
4466 | rcu_read_unlock(); | |
4467 | return 0; | |
4468 | ||
4469 | error_unlock: | |
4470 | pthread_mutex_unlock(&stream->lock); | |
4471 | pthread_mutex_unlock(&channel->lock); | |
4472 | rcu_read_unlock(); | |
5f3aff8b MD |
4473 | return ret; |
4474 | } | |
4475 | ||
02d02e31 JD |
4476 | /* |
4477 | * Check if a stream is ready to be rotated after extracting it. | |
4478 | * | |
4479 | * Return 1 if it is ready for rotation, 0 if it is not, a negative value on | |
4480 | * error. Stream lock must be held. | |
4481 | */ | |
4482 | int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream *stream) | |
4483 | { | |
f8528c7a MD |
4484 | DBG("Check is rotate ready for stream %" PRIu64 |
4485 | " ready %u rotate_position %" PRIu64 | |
4486 | " last_sequence_number %" PRIu64, | |
4487 | stream->key, stream->rotate_ready, | |
4488 | stream->rotate_position, stream->last_sequence_number); | |
02d02e31 | 4489 | if (stream->rotate_ready) { |
a40a503f | 4490 | return 1; |
02d02e31 JD |
4491 | } |
4492 | ||
4493 | /* | |
a40a503f MD |
4494 | * If packet seq num is unavailable, it means we are interacting |
4495 | * with a pre-2.8 lttng-modules which does not implement the | |
4496 | * sequence number. Rotation should never be used by sessiond in this | |
4497 | * scenario. | |
02d02e31 | 4498 | */ |
a40a503f MD |
4499 | if (stream->sequence_number_unavailable) { |
4500 | ERR("Internal error: rotation used on stream %" PRIu64 | |
4501 | " with unavailable sequence number", | |
4502 | stream->key); | |
4503 | return -1; | |
02d02e31 JD |
4504 | } |
4505 | ||
a40a503f MD |
4506 | if (stream->rotate_position == -1ULL || |
4507 | stream->last_sequence_number == -1ULL) { | |
4508 | return 0; | |
02d02e31 JD |
4509 | } |
4510 | ||
a40a503f MD |
4511 | /* |
4512 | * Rotate position not reached yet. The stream rotate position is | |
4513 | * the position of the next packet belonging to the next trace chunk, | |
4514 | * but consumerd considers rotation ready when reaching the last | |
4515 | * packet of the current chunk, hence the "rotate_position - 1". | |
4516 | */ | |
f8528c7a MD |
4517 | |
4518 | DBG("Check is rotate ready for stream %" PRIu64 | |
4519 | " last_sequence_number %" PRIu64 | |
4520 | " rotate_position %" PRIu64, | |
4521 | stream->key, stream->last_sequence_number, | |
4522 | stream->rotate_position); | |
a40a503f MD |
4523 | if (stream->last_sequence_number >= stream->rotate_position - 1) { |
4524 | return 1; | |
02d02e31 | 4525 | } |
02d02e31 | 4526 | |
a40a503f | 4527 | return 0; |
02d02e31 JD |
4528 | } |
4529 | ||
d73bf3d7 JD |
4530 | /* |
4531 | * Reset the state for a stream after a rotation occurred. | |
4532 | */ | |
4533 | void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream *stream) | |
4534 | { | |
f8528c7a MD |
4535 | DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64, |
4536 | stream->key); | |
a40a503f | 4537 | stream->rotate_position = -1ULL; |
d73bf3d7 JD |
4538 | stream->rotate_ready = false; |
4539 | } | |
4540 | ||
4541 | /* | |
4542 | * Perform the rotation a local stream file. | |
4543 | */ | |
d2956687 | 4544 | static |
d73bf3d7 JD |
4545 | int rotate_local_stream(struct lttng_consumer_local_data *ctx, |
4546 | struct lttng_consumer_stream *stream) | |
4547 | { | |
d2956687 | 4548 | int ret = 0; |
d73bf3d7 | 4549 | |
d2956687 | 4550 | DBG("Rotate local stream: stream key %" PRIu64 ", channel key %" PRIu64, |
d73bf3d7 | 4551 | stream->key, |
d2956687 | 4552 | stream->chan->key); |
d73bf3d7 | 4553 | stream->tracefile_size_current = 0; |
d2956687 | 4554 | stream->tracefile_count_current = 0; |
d73bf3d7 | 4555 | |
d2956687 JG |
4556 | if (stream->out_fd >= 0) { |
4557 | ret = close(stream->out_fd); | |
4558 | if (ret) { | |
4559 | PERROR("Failed to close stream out_fd of channel \"%s\"", | |
4560 | stream->chan->name); | |
4561 | } | |
4562 | stream->out_fd = -1; | |
4563 | } | |
d73bf3d7 | 4564 | |
d2956687 | 4565 | if (stream->index_file) { |
d73bf3d7 | 4566 | lttng_index_file_put(stream->index_file); |
d2956687 | 4567 | stream->index_file = NULL; |
d73bf3d7 JD |
4568 | } |
4569 | ||
d2956687 JG |
4570 | if (!stream->trace_chunk) { |
4571 | goto end; | |
4572 | } | |
d73bf3d7 | 4573 | |
d2956687 | 4574 | ret = consumer_stream_create_output_files(stream, true); |
d73bf3d7 JD |
4575 | end: |
4576 | return ret; | |
d73bf3d7 JD |
4577 | } |
4578 | ||
d73bf3d7 JD |
4579 | /* |
4580 | * Performs the stream rotation for the rotate session feature if needed. | |
d2956687 | 4581 | * It must be called with the channel and stream locks held. |
d73bf3d7 JD |
4582 | * |
4583 | * Return 0 on success, a negative number of error. | |
4584 | */ | |
4585 | int lttng_consumer_rotate_stream(struct lttng_consumer_local_data *ctx, | |
d2956687 | 4586 | struct lttng_consumer_stream *stream) |
d73bf3d7 JD |
4587 | { |
4588 | int ret; | |
4589 | ||
4590 | DBG("Consumer rotate stream %" PRIu64, stream->key); | |
4591 | ||
d2956687 JG |
4592 | /* |
4593 | * Update the stream's 'current' chunk to the session's (channel) | |
4594 | * now-current chunk. | |
4595 | */ | |
4596 | lttng_trace_chunk_put(stream->trace_chunk); | |
4597 | if (stream->chan->trace_chunk == stream->trace_chunk) { | |
4598 | /* | |
4599 | * A channel can be rotated and not have a "next" chunk | |
4600 | * to transition to. In that case, the channel's "current chunk" | |
4601 | * has not been closed yet, but it has not been updated to | |
4602 | * a "next" trace chunk either. Hence, the stream, like its | |
4603 | * parent channel, becomes part of no chunk and can't output | |
4604 | * anything until a new trace chunk is created. | |
4605 | */ | |
4606 | stream->trace_chunk = NULL; | |
4607 | } else if (stream->chan->trace_chunk && | |
4608 | !lttng_trace_chunk_get(stream->chan->trace_chunk)) { | |
4609 | ERR("Failed to acquire a reference to channel's trace chunk during stream rotation"); | |
4610 | ret = -1; | |
4611 | goto error; | |
4612 | } else { | |
4613 | /* | |
4614 | * Update the stream's trace chunk to its parent channel's | |
4615 | * current trace chunk. | |
4616 | */ | |
4617 | stream->trace_chunk = stream->chan->trace_chunk; | |
4618 | } | |
4619 | ||
c35f9726 | 4620 | if (stream->net_seq_idx == (uint64_t) -1ULL) { |
d73bf3d7 | 4621 | ret = rotate_local_stream(ctx, stream); |
c35f9726 JG |
4622 | if (ret < 0) { |
4623 | ERR("Failed to rotate stream, ret = %i", ret); | |
4624 | goto error; | |
4625 | } | |
d73bf3d7 JD |
4626 | } |
4627 | ||
d2956687 JG |
4628 | if (stream->metadata_flag && stream->trace_chunk) { |
4629 | /* | |
4630 | * If the stream has transitioned to a new trace | |
4631 | * chunk, the metadata should be re-dumped to the | |
4632 | * newest chunk. | |
4633 | * | |
4634 | * However, it is possible for a stream to transition to | |
4635 | * a "no-chunk" state. This can happen if a rotation | |
4636 | * occurs on an inactive session. In such cases, the metadata | |
4637 | * regeneration will happen when the next trace chunk is | |
4638 | * created. | |
4639 | */ | |
4640 | ret = consumer_metadata_stream_dump(stream); | |
4641 | if (ret) { | |
4642 | goto error; | |
d73bf3d7 JD |
4643 | } |
4644 | } | |
4645 | lttng_consumer_reset_stream_rotate_state(stream); | |
4646 | ||
4647 | ret = 0; | |
4648 | ||
4649 | error: | |
4650 | return ret; | |
4651 | } | |
4652 | ||
b99a8d42 JD |
4653 | /* |
4654 | * Rotate all the ready streams now. | |
4655 | * | |
4656 | * This is especially important for low throughput streams that have already | |
4657 | * been consumed, we cannot wait for their next packet to perform the | |
4658 | * rotation. | |
92b7a7f8 MD |
4659 | * Need to be called with RCU read-side lock held to ensure existence of |
4660 | * channel. | |
b99a8d42 JD |
4661 | * |
4662 | * Returns 0 on success, < 0 on error | |
4663 | */ | |
92b7a7f8 MD |
4664 | int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel *channel, |
4665 | uint64_t key, struct lttng_consumer_local_data *ctx) | |
b99a8d42 JD |
4666 | { |
4667 | int ret; | |
b99a8d42 JD |
4668 | struct lttng_consumer_stream *stream; |
4669 | struct lttng_ht_iter iter; | |
4670 | struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht; | |
4671 | ||
4672 | rcu_read_lock(); | |
4673 | ||
4674 | DBG("Consumer rotate ready streams in channel %" PRIu64, key); | |
4675 | ||
b99a8d42 JD |
4676 | cds_lfht_for_each_entry_duplicate(ht->ht, |
4677 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
4678 | ht->match_fct, &channel->key, &iter.iter, | |
4679 | stream, node_channel_id.node) { | |
4680 | health_code_update(); | |
4681 | ||
d2956687 | 4682 | pthread_mutex_lock(&stream->chan->lock); |
b99a8d42 JD |
4683 | pthread_mutex_lock(&stream->lock); |
4684 | ||
4685 | if (!stream->rotate_ready) { | |
4686 | pthread_mutex_unlock(&stream->lock); | |
d2956687 | 4687 | pthread_mutex_unlock(&stream->chan->lock); |
b99a8d42 JD |
4688 | continue; |
4689 | } | |
4690 | DBG("Consumer rotate ready stream %" PRIu64, stream->key); | |
4691 | ||
d2956687 | 4692 | ret = lttng_consumer_rotate_stream(ctx, stream); |
b99a8d42 | 4693 | pthread_mutex_unlock(&stream->lock); |
d2956687 | 4694 | pthread_mutex_unlock(&stream->chan->lock); |
b99a8d42 JD |
4695 | if (ret) { |
4696 | goto end; | |
4697 | } | |
4698 | } | |
4699 | ||
4700 | ret = 0; | |
4701 | ||
4702 | end: | |
4703 | rcu_read_unlock(); | |
4704 | return ret; | |
4705 | } | |
4706 | ||
d2956687 JG |
4707 | enum lttcomm_return_code lttng_consumer_init_command( |
4708 | struct lttng_consumer_local_data *ctx, | |
4709 | const lttng_uuid sessiond_uuid) | |
00fb02ac | 4710 | { |
d2956687 | 4711 | enum lttcomm_return_code ret; |
c70636a7 | 4712 | char uuid_str[LTTNG_UUID_STR_LEN]; |
00fb02ac | 4713 | |
d2956687 JG |
4714 | if (ctx->sessiond_uuid.is_set) { |
4715 | ret = LTTCOMM_CONSUMERD_ALREADY_SET; | |
00fb02ac JD |
4716 | goto end; |
4717 | } | |
4718 | ||
d2956687 JG |
4719 | ctx->sessiond_uuid.is_set = true; |
4720 | memcpy(ctx->sessiond_uuid.value, sessiond_uuid, sizeof(lttng_uuid)); | |
4721 | ret = LTTCOMM_CONSUMERD_SUCCESS; | |
4722 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
4723 | DBG("Received session daemon UUID: %s", uuid_str); | |
00fb02ac JD |
4724 | end: |
4725 | return ret; | |
4726 | } | |
4727 | ||
d2956687 JG |
4728 | enum lttcomm_return_code lttng_consumer_create_trace_chunk( |
4729 | const uint64_t *relayd_id, uint64_t session_id, | |
4730 | uint64_t chunk_id, | |
4731 | time_t chunk_creation_timestamp, | |
4732 | const char *chunk_override_name, | |
4733 | const struct lttng_credentials *credentials, | |
4734 | struct lttng_directory_handle *chunk_directory_handle) | |
00fb02ac JD |
4735 | { |
4736 | int ret; | |
d2956687 | 4737 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
7ea24db3 | 4738 | struct lttng_trace_chunk *created_chunk = NULL, *published_chunk = NULL; |
d2956687 JG |
4739 | enum lttng_trace_chunk_status chunk_status; |
4740 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; | |
4741 | char creation_timestamp_buffer[ISO8601_STR_LEN]; | |
4742 | const char *relayd_id_str = "(none)"; | |
4743 | const char *creation_timestamp_str; | |
4744 | struct lttng_ht_iter iter; | |
4745 | struct lttng_consumer_channel *channel; | |
92816cc3 | 4746 | |
d2956687 JG |
4747 | if (relayd_id) { |
4748 | /* Only used for logging purposes. */ | |
4749 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), | |
4750 | "%" PRIu64, *relayd_id); | |
4751 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { | |
4752 | relayd_id_str = relayd_id_buffer; | |
4753 | } else { | |
4754 | relayd_id_str = "(formatting error)"; | |
4755 | } | |
4756 | } | |
4757 | ||
4758 | /* Local protocol error. */ | |
4759 | assert(chunk_creation_timestamp); | |
4760 | ret = time_to_iso8601_str(chunk_creation_timestamp, | |
4761 | creation_timestamp_buffer, | |
4762 | sizeof(creation_timestamp_buffer)); | |
4763 | creation_timestamp_str = !ret ? creation_timestamp_buffer : | |
4764 | "(formatting error)"; | |
4765 | ||
4766 | DBG("Consumer create trace chunk command: relay_id = %s" | |
4767 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 | |
4768 | ", chunk_override_name = %s" | |
4769 | ", chunk_creation_timestamp = %s", | |
4770 | relayd_id_str, session_id, chunk_id, | |
4771 | chunk_override_name ? : "(none)", | |
4772 | creation_timestamp_str); | |
92816cc3 JG |
4773 | |
4774 | /* | |
d2956687 JG |
4775 | * The trace chunk registry, as used by the consumer daemon, implicitly |
4776 | * owns the trace chunks. This is only needed in the consumer since | |
4777 | * the consumer has no notion of a session beyond session IDs being | |
4778 | * used to identify other objects. | |
4779 | * | |
4780 | * The lttng_trace_chunk_registry_publish() call below provides a | |
4781 | * reference which is not released; it implicitly becomes the session | |
4782 | * daemon's reference to the chunk in the consumer daemon. | |
4783 | * | |
4784 | * The lifetime of trace chunks in the consumer daemon is managed by | |
4785 | * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK | |
4786 | * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands. | |
92816cc3 | 4787 | */ |
d2956687 | 4788 | created_chunk = lttng_trace_chunk_create(chunk_id, |
a7ceb342 | 4789 | chunk_creation_timestamp, NULL); |
d2956687 JG |
4790 | if (!created_chunk) { |
4791 | ERR("Failed to create trace chunk"); | |
4792 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4793 | goto error; |
d2956687 | 4794 | } |
92816cc3 | 4795 | |
d2956687 JG |
4796 | if (chunk_override_name) { |
4797 | chunk_status = lttng_trace_chunk_override_name(created_chunk, | |
4798 | chunk_override_name); | |
4799 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4800 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4801 | goto error; |
92816cc3 JG |
4802 | } |
4803 | } | |
4804 | ||
d2956687 JG |
4805 | if (chunk_directory_handle) { |
4806 | chunk_status = lttng_trace_chunk_set_credentials(created_chunk, | |
4807 | credentials); | |
4808 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4809 | ERR("Failed to set trace chunk credentials"); | |
4810 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4811 | goto error; |
d2956687 JG |
4812 | } |
4813 | /* | |
4814 | * The consumer daemon has no ownership of the chunk output | |
4815 | * directory. | |
4816 | */ | |
4817 | chunk_status = lttng_trace_chunk_set_as_user(created_chunk, | |
4818 | chunk_directory_handle); | |
cbf53d23 | 4819 | chunk_directory_handle = NULL; |
d2956687 JG |
4820 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
4821 | ERR("Failed to set trace chunk's directory handle"); | |
4822 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4823 | goto error; |
92816cc3 JG |
4824 | } |
4825 | } | |
4826 | ||
d2956687 JG |
4827 | published_chunk = lttng_trace_chunk_registry_publish_chunk( |
4828 | consumer_data.chunk_registry, session_id, | |
4829 | created_chunk); | |
4830 | lttng_trace_chunk_put(created_chunk); | |
4831 | created_chunk = NULL; | |
4832 | if (!published_chunk) { | |
4833 | ERR("Failed to publish trace chunk"); | |
4834 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4835 | goto error; |
d88744a4 JD |
4836 | } |
4837 | ||
d2956687 JG |
4838 | rcu_read_lock(); |
4839 | cds_lfht_for_each_entry_duplicate(consumer_data.channels_by_session_id_ht->ht, | |
4840 | consumer_data.channels_by_session_id_ht->hash_fct( | |
4841 | &session_id, lttng_ht_seed), | |
4842 | consumer_data.channels_by_session_id_ht->match_fct, | |
4843 | &session_id, &iter.iter, channel, | |
4844 | channels_by_session_id_ht_node.node) { | |
4845 | ret = lttng_consumer_channel_set_trace_chunk(channel, | |
4846 | published_chunk); | |
4847 | if (ret) { | |
4848 | /* | |
4849 | * Roll-back the creation of this chunk. | |
4850 | * | |
4851 | * This is important since the session daemon will | |
4852 | * assume that the creation of this chunk failed and | |
4853 | * will never ask for it to be closed, resulting | |
4854 | * in a leak and an inconsistent state for some | |
4855 | * channels. | |
4856 | */ | |
4857 | enum lttcomm_return_code close_ret; | |
ecd1a12f | 4858 | char path[LTTNG_PATH_MAX]; |
d2956687 JG |
4859 | |
4860 | DBG("Failed to set new trace chunk on existing channels, rolling back"); | |
4861 | close_ret = lttng_consumer_close_trace_chunk(relayd_id, | |
4862 | session_id, chunk_id, | |
ecd1a12f MD |
4863 | chunk_creation_timestamp, NULL, |
4864 | path); | |
d2956687 JG |
4865 | if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) { |
4866 | ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
4867 | session_id, chunk_id); | |
4868 | } | |
a1ae2ea5 | 4869 | |
d2956687 JG |
4870 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
4871 | break; | |
4872 | } | |
a1ae2ea5 JD |
4873 | } |
4874 | ||
e5add6d0 JG |
4875 | if (relayd_id) { |
4876 | struct consumer_relayd_sock_pair *relayd; | |
4877 | ||
4878 | relayd = consumer_find_relayd(*relayd_id); | |
4879 | if (relayd) { | |
4880 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
4881 | ret = relayd_create_trace_chunk( | |
4882 | &relayd->control_sock, published_chunk); | |
4883 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
4884 | } else { | |
4885 | ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, *relayd_id); | |
4886 | } | |
4887 | ||
4888 | if (!relayd || ret) { | |
4889 | enum lttcomm_return_code close_ret; | |
ecd1a12f | 4890 | char path[LTTNG_PATH_MAX]; |
e5add6d0 JG |
4891 | |
4892 | close_ret = lttng_consumer_close_trace_chunk(relayd_id, | |
4893 | session_id, | |
4894 | chunk_id, | |
bbc4768c | 4895 | chunk_creation_timestamp, |
ecd1a12f | 4896 | NULL, path); |
e5add6d0 JG |
4897 | if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) { |
4898 | ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
4899 | session_id, | |
4900 | chunk_id); | |
4901 | } | |
4902 | ||
4903 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; | |
7ea24db3 | 4904 | goto error_unlock; |
e5add6d0 JG |
4905 | } |
4906 | } | |
7ea24db3 | 4907 | error_unlock: |
e5add6d0 | 4908 | rcu_read_unlock(); |
7ea24db3 | 4909 | error: |
d2956687 JG |
4910 | /* Release the reference returned by the "publish" operation. */ |
4911 | lttng_trace_chunk_put(published_chunk); | |
9bb5f1f8 | 4912 | lttng_trace_chunk_put(created_chunk); |
d2956687 | 4913 | return ret_code; |
a1ae2ea5 JD |
4914 | } |
4915 | ||
d2956687 JG |
4916 | enum lttcomm_return_code lttng_consumer_close_trace_chunk( |
4917 | const uint64_t *relayd_id, uint64_t session_id, | |
bbc4768c | 4918 | uint64_t chunk_id, time_t chunk_close_timestamp, |
ecd1a12f MD |
4919 | const enum lttng_trace_chunk_command_type *close_command, |
4920 | char *path) | |
a1ae2ea5 | 4921 | { |
d2956687 JG |
4922 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
4923 | struct lttng_trace_chunk *chunk; | |
4924 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; | |
4925 | const char *relayd_id_str = "(none)"; | |
bbc4768c | 4926 | const char *close_command_name = "none"; |
d2956687 JG |
4927 | struct lttng_ht_iter iter; |
4928 | struct lttng_consumer_channel *channel; | |
4929 | enum lttng_trace_chunk_status chunk_status; | |
a1ae2ea5 | 4930 | |
d2956687 JG |
4931 | if (relayd_id) { |
4932 | int ret; | |
4933 | ||
4934 | /* Only used for logging purposes. */ | |
4935 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), | |
4936 | "%" PRIu64, *relayd_id); | |
4937 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { | |
4938 | relayd_id_str = relayd_id_buffer; | |
4939 | } else { | |
4940 | relayd_id_str = "(formatting error)"; | |
4941 | } | |
bbc4768c JG |
4942 | } |
4943 | if (close_command) { | |
4944 | close_command_name = lttng_trace_chunk_command_type_get_name( | |
4945 | *close_command); | |
4946 | } | |
d2956687 JG |
4947 | |
4948 | DBG("Consumer close trace chunk command: relayd_id = %s" | |
bbc4768c JG |
4949 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 |
4950 | ", close command = %s", | |
4951 | relayd_id_str, session_id, chunk_id, | |
4952 | close_command_name); | |
4953 | ||
d2956687 | 4954 | chunk = lttng_trace_chunk_registry_find_chunk( |
bbc4768c JG |
4955 | consumer_data.chunk_registry, session_id, chunk_id); |
4956 | if (!chunk) { | |
d2956687 JG |
4957 | ERR("Failed to find chunk: session_id = %" PRIu64 |
4958 | ", chunk_id = %" PRIu64, | |
4959 | session_id, chunk_id); | |
4960 | ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; | |
a1ae2ea5 JD |
4961 | goto end; |
4962 | } | |
4963 | ||
d2956687 JG |
4964 | chunk_status = lttng_trace_chunk_set_close_timestamp(chunk, |
4965 | chunk_close_timestamp); | |
4966 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4967 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; | |
4968 | goto end; | |
45f1d9a1 | 4969 | } |
bbc4768c JG |
4970 | |
4971 | if (close_command) { | |
4972 | chunk_status = lttng_trace_chunk_set_close_command( | |
4973 | chunk, *close_command); | |
4974 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4975 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; | |
4976 | goto end; | |
4977 | } | |
4978 | } | |
a1ae2ea5 | 4979 | |
d2956687 JG |
4980 | /* |
4981 | * chunk is now invalid to access as we no longer hold a reference to | |
4982 | * it; it is only kept around to compare it (by address) to the | |
4983 | * current chunk found in the session's channels. | |
4984 | */ | |
4985 | rcu_read_lock(); | |
4986 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, | |
4987 | channel, node.node) { | |
4988 | int ret; | |
a1ae2ea5 | 4989 | |
d2956687 JG |
4990 | /* |
4991 | * Only change the channel's chunk to NULL if it still | |
4992 | * references the chunk being closed. The channel may | |
4993 | * reference a newer channel in the case of a session | |
4994 | * rotation. When a session rotation occurs, the "next" | |
4995 | * chunk is created before the "current" chunk is closed. | |
4996 | */ | |
4997 | if (channel->trace_chunk != chunk) { | |
4998 | continue; | |
4999 | } | |
5000 | ret = lttng_consumer_channel_set_trace_chunk(channel, NULL); | |
5001 | if (ret) { | |
5002 | /* | |
5003 | * Attempt to close the chunk on as many channels as | |
5004 | * possible. | |
5005 | */ | |
5006 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; | |
5007 | } | |
a1ae2ea5 | 5008 | } |
bbc4768c JG |
5009 | |
5010 | if (relayd_id) { | |
5011 | int ret; | |
5012 | struct consumer_relayd_sock_pair *relayd; | |
5013 | ||
5014 | relayd = consumer_find_relayd(*relayd_id); | |
5015 | if (relayd) { | |
5016 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
5017 | ret = relayd_close_trace_chunk( | |
ecd1a12f MD |
5018 | &relayd->control_sock, chunk, |
5019 | path); | |
bbc4768c JG |
5020 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
5021 | } else { | |
5022 | ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, | |
5023 | *relayd_id); | |
5024 | } | |
5025 | ||
5026 | if (!relayd || ret) { | |
5027 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; | |
5028 | goto error_unlock; | |
5029 | } | |
5030 | } | |
5031 | error_unlock: | |
d2956687 JG |
5032 | rcu_read_unlock(); |
5033 | end: | |
bbc4768c JG |
5034 | /* |
5035 | * Release the reference returned by the "find" operation and | |
5036 | * the session daemon's implicit reference to the chunk. | |
5037 | */ | |
5038 | lttng_trace_chunk_put(chunk); | |
5039 | lttng_trace_chunk_put(chunk); | |
5040 | ||
d2956687 | 5041 | return ret_code; |
a1ae2ea5 | 5042 | } |
3654ed19 | 5043 | |
d2956687 JG |
5044 | enum lttcomm_return_code lttng_consumer_trace_chunk_exists( |
5045 | const uint64_t *relayd_id, uint64_t session_id, | |
5046 | uint64_t chunk_id) | |
3654ed19 | 5047 | { |
c35f9726 | 5048 | int ret; |
d2956687 | 5049 | enum lttcomm_return_code ret_code; |
d2956687 JG |
5050 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; |
5051 | const char *relayd_id_str = "(none)"; | |
c35f9726 JG |
5052 | const bool is_local_trace = !relayd_id; |
5053 | struct consumer_relayd_sock_pair *relayd = NULL; | |
6b584c2e | 5054 | bool chunk_exists_local, chunk_exists_remote; |
d2956687 JG |
5055 | |
5056 | if (relayd_id) { | |
5057 | int ret; | |
5058 | ||
5059 | /* Only used for logging purposes. */ | |
5060 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), | |
5061 | "%" PRIu64, *relayd_id); | |
5062 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { | |
5063 | relayd_id_str = relayd_id_buffer; | |
5064 | } else { | |
5065 | relayd_id_str = "(formatting error)"; | |
5066 | } | |
5067 | } | |
5068 | ||
5069 | DBG("Consumer trace chunk exists command: relayd_id = %s" | |
d2956687 | 5070 | ", chunk_id = %" PRIu64, relayd_id_str, |
c35f9726 | 5071 | chunk_id); |
6b584c2e | 5072 | ret = lttng_trace_chunk_registry_chunk_exists( |
d2956687 | 5073 | consumer_data.chunk_registry, session_id, |
6b584c2e JG |
5074 | chunk_id, &chunk_exists_local); |
5075 | if (ret) { | |
5076 | /* Internal error. */ | |
5077 | ERR("Failed to query the existence of a trace chunk"); | |
5078 | ret_code = LTTCOMM_CONSUMERD_FATAL; | |
13e3b280 | 5079 | goto end; |
6b584c2e JG |
5080 | } |
5081 | DBG("Trace chunk %s locally", | |
5082 | chunk_exists_local ? "exists" : "does not exist"); | |
5083 | if (chunk_exists_local) { | |
c35f9726 | 5084 | ret_code = LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL; |
c35f9726 JG |
5085 | goto end; |
5086 | } else if (is_local_trace) { | |
5087 | ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; | |
5088 | goto end; | |
5089 | } | |
5090 | ||
5091 | rcu_read_lock(); | |
5092 | relayd = consumer_find_relayd(*relayd_id); | |
5093 | if (!relayd) { | |
5094 | ERR("Failed to find relayd %" PRIu64, *relayd_id); | |
5095 | ret_code = LTTCOMM_CONSUMERD_INVALID_PARAMETERS; | |
5096 | goto end_rcu_unlock; | |
5097 | } | |
5098 | DBG("Looking up existence of trace chunk on relay daemon"); | |
5099 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
5100 | ret = relayd_trace_chunk_exists(&relayd->control_sock, chunk_id, | |
5101 | &chunk_exists_remote); | |
5102 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
5103 | if (ret < 0) { | |
5104 | ERR("Failed to look-up the existence of trace chunk on relay daemon"); | |
5105 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; | |
5106 | goto end_rcu_unlock; | |
5107 | } | |
5108 | ||
5109 | ret_code = chunk_exists_remote ? | |
5110 | LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE : | |
d2956687 | 5111 | LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; |
c35f9726 JG |
5112 | DBG("Trace chunk %s on relay daemon", |
5113 | chunk_exists_remote ? "exists" : "does not exist"); | |
d2956687 | 5114 | |
c35f9726 JG |
5115 | end_rcu_unlock: |
5116 | rcu_read_unlock(); | |
5117 | end: | |
d2956687 | 5118 | return ret_code; |
3654ed19 | 5119 | } |
5f3aff8b MD |
5120 | |
5121 | static | |
5122 | int consumer_clear_monitored_channel(struct lttng_consumer_channel *channel) | |
5123 | { | |
5124 | struct lttng_ht *ht; | |
5125 | struct lttng_consumer_stream *stream; | |
5126 | struct lttng_ht_iter iter; | |
5127 | int ret; | |
5128 | ||
5129 | ht = consumer_data.stream_per_chan_id_ht; | |
5130 | ||
5131 | rcu_read_lock(); | |
5132 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
5133 | ht->hash_fct(&channel->key, lttng_ht_seed), | |
5134 | ht->match_fct, &channel->key, | |
5135 | &iter.iter, stream, node_channel_id.node) { | |
5136 | /* | |
5137 | * Protect against teardown with mutex. | |
5138 | */ | |
5139 | pthread_mutex_lock(&stream->lock); | |
5140 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
5141 | goto next; | |
5142 | } | |
5143 | ret = consumer_clear_stream(stream); | |
5144 | if (ret) { | |
5145 | goto error_unlock; | |
5146 | } | |
5147 | next: | |
5148 | pthread_mutex_unlock(&stream->lock); | |
5149 | } | |
5150 | rcu_read_unlock(); | |
5151 | return LTTCOMM_CONSUMERD_SUCCESS; | |
5152 | ||
5153 | error_unlock: | |
5154 | pthread_mutex_unlock(&stream->lock); | |
5155 | rcu_read_unlock(); | |
5156 | return ret; | |
5157 | } | |
5158 | ||
5159 | int lttng_consumer_clear_channel(struct lttng_consumer_channel *channel) | |
5160 | { | |
5161 | int ret; | |
5162 | ||
5163 | DBG("Consumer clear channel %" PRIu64, channel->key); | |
5164 | ||
5165 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
5166 | /* | |
5167 | * Nothing to do for the metadata channel/stream. | |
5168 | * Snapshot mechanism already take care of the metadata | |
5169 | * handling/generation, and monitored channels only need to | |
5170 | * have their data stream cleared.. | |
5171 | */ | |
5172 | ret = LTTCOMM_CONSUMERD_SUCCESS; | |
5173 | goto end; | |
5174 | } | |
5175 | ||
5176 | if (!channel->monitor) { | |
5177 | ret = consumer_clear_unmonitored_channel(channel); | |
5178 | } else { | |
5179 | ret = consumer_clear_monitored_channel(channel); | |
5180 | } | |
5181 | end: | |
5182 | return ret; | |
5183 | } | |
1223361a JG |
5184 | |
5185 | enum lttcomm_return_code lttng_consumer_open_channel_packets( | |
5186 | struct lttng_consumer_channel *channel) | |
5187 | { | |
5188 | struct lttng_consumer_stream *stream; | |
5189 | enum lttcomm_return_code ret = LTTCOMM_CONSUMERD_SUCCESS; | |
5190 | ||
5191 | if (channel->metadata_stream) { | |
5192 | ERR("Open channel packets command attempted on a metadata channel"); | |
5193 | ret = LTTCOMM_CONSUMERD_INVALID_PARAMETERS; | |
5194 | goto end; | |
5195 | } | |
5196 | ||
5197 | rcu_read_lock(); | |
5198 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
5199 | enum open_packet_status status; | |
5200 | ||
5201 | pthread_mutex_lock(&stream->lock); | |
5202 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
5203 | goto next; | |
5204 | } | |
5205 | ||
5206 | status = open_packet(stream); | |
5207 | switch (status) { | |
5208 | case OPEN_PACKET_STATUS_OPENED: | |
5209 | DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64 | |
5210 | ", channel name = %s, session id = %" PRIu64, | |
5211 | stream->key, stream->chan->name, | |
5212 | stream->chan->session_id); | |
5213 | stream->opened_packet_in_current_trace_chunk = true; | |
5214 | break; | |
5215 | case OPEN_PACKET_STATUS_NO_SPACE: | |
5216 | DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64 | |
5217 | ", channel name = %s, session id = %" PRIu64, | |
5218 | stream->key, stream->chan->name, | |
5219 | stream->chan->session_id); | |
5220 | break; | |
5221 | case OPEN_PACKET_STATUS_ERROR: | |
5222 | /* | |
5223 | * Only unexpected internal errors can lead to this | |
5224 | * failing. Report an unknown error. | |
5225 | */ | |
5226 | ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64 | |
5227 | ", channel id = %" PRIu64 | |
5228 | ", channel name = %s" | |
5229 | ", session id = %" PRIu64, | |
5230 | stream->key, channel->key, | |
5231 | channel->name, channel->session_id); | |
5232 | ret = LTTCOMM_CONSUMERD_UNKNOWN_ERROR; | |
5233 | goto error_unlock; | |
5234 | default: | |
5235 | abort(); | |
5236 | } | |
5237 | ||
5238 | next: | |
5239 | pthread_mutex_unlock(&stream->lock); | |
5240 | } | |
5241 | ||
5242 | end_rcu_unlock: | |
5243 | rcu_read_unlock(); | |
5244 | end: | |
5245 | return ret; | |
5246 | ||
5247 | error_unlock: | |
5248 | pthread_mutex_unlock(&stream->lock); | |
5249 | goto end_rcu_unlock; | |
5250 | } |