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