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