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