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