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