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