Fix: consumer data pending for empty streams
[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 /* Just don't waste time if no returned events for the fd */
2201 if (!revents) {
2202 continue;
2203 }
2204
2205 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
2206 if (revents & (LPOLLERR | LPOLLHUP )) {
2207 DBG("Metadata thread pipe hung up");
2208 /*
2209 * Remove the pipe from the poll set and continue the loop
2210 * since their might be data to consume.
2211 */
2212 lttng_poll_del(&events,
2213 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2214 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2215 continue;
2216 } else if (revents & LPOLLIN) {
2217 ssize_t pipe_len;
2218
2219 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2220 &stream, sizeof(stream));
2221 if (pipe_len < 0) {
2222 ERR("read metadata stream, ret: %zd", pipe_len);
2223 /*
2224 * Continue here to handle the rest of the streams.
2225 */
2226 continue;
2227 }
2228
2229 /* A NULL stream means that the state has changed. */
2230 if (stream == NULL) {
2231 /* Check for deleted streams. */
2232 validate_endpoint_status_metadata_stream(&events);
2233 goto restart;
2234 }
2235
2236 DBG("Adding metadata stream %d to poll set",
2237 stream->wait_fd);
2238
2239 /* Add metadata stream to the global poll events list */
2240 lttng_poll_add(&events, stream->wait_fd,
2241 LPOLLIN | LPOLLPRI);
2242 }
2243
2244 /* Handle other stream */
2245 continue;
2246 }
2247
2248 rcu_read_lock();
2249 {
2250 uint64_t tmp_id = (uint64_t) pollfd;
2251
2252 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2253 }
2254 node = lttng_ht_iter_get_node_u64(&iter);
2255 assert(node);
2256
2257 stream = caa_container_of(node, struct lttng_consumer_stream,
2258 node);
2259
2260 /* Check for error event */
2261 if (revents & (LPOLLERR | LPOLLHUP)) {
2262 DBG("Metadata fd %d is hup|err.", pollfd);
2263 if (!stream->hangup_flush_done
2264 && (consumer_data.type == LTTNG_CONSUMER32_UST
2265 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2266 DBG("Attempting to flush and consume the UST buffers");
2267 lttng_ustconsumer_on_stream_hangup(stream);
2268
2269 /* We just flushed the stream now read it. */
2270 do {
2271 len = ctx->on_buffer_ready(stream, ctx);
2272 /*
2273 * We don't check the return value here since if we get
2274 * a negative len, it means an error occured thus we
2275 * simply remove it from the poll set and free the
2276 * stream.
2277 */
2278 } while (len > 0);
2279 }
2280
2281 lttng_poll_del(&events, stream->wait_fd);
2282 /*
2283 * This call update the channel states, closes file descriptors
2284 * and securely free the stream.
2285 */
2286 consumer_del_metadata_stream(stream, metadata_ht);
2287 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2288 /* Get the data out of the metadata file descriptor */
2289 DBG("Metadata available on fd %d", pollfd);
2290 assert(stream->wait_fd == pollfd);
2291
2292 do {
2293 len = ctx->on_buffer_ready(stream, ctx);
2294 /*
2295 * We don't check the return value here since if we get
2296 * a negative len, it means an error occured thus we
2297 * simply remove it from the poll set and free the
2298 * stream.
2299 */
2300 } while (len > 0);
2301
2302 /* It's ok to have an unavailable sub-buffer */
2303 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2304 /* Clean up stream from consumer and free it. */
2305 lttng_poll_del(&events, stream->wait_fd);
2306 consumer_del_metadata_stream(stream, metadata_ht);
2307 }
2308 }
2309
2310 /* Release RCU lock for the stream looked up */
2311 rcu_read_unlock();
2312 }
2313 }
2314
2315 error:
2316 end:
2317 DBG("Metadata poll thread exiting");
2318
2319 lttng_poll_clean(&events);
2320 end_poll:
2321 destroy_stream_ht(metadata_ht);
2322 end_ht:
2323 rcu_unregister_thread();
2324 return NULL;
2325 }
2326
2327 /*
2328 * This thread polls the fds in the set to consume the data and write
2329 * it to tracefile if necessary.
2330 */
2331 void *consumer_thread_data_poll(void *data)
2332 {
2333 int num_rdy, num_hup, high_prio, ret, i;
2334 struct pollfd *pollfd = NULL;
2335 /* local view of the streams */
2336 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2337 /* local view of consumer_data.fds_count */
2338 int nb_fd = 0;
2339 struct lttng_consumer_local_data *ctx = data;
2340 ssize_t len;
2341
2342 rcu_register_thread();
2343
2344 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2345 if (data_ht == NULL) {
2346 /* ENOMEM at this point. Better to bail out. */
2347 goto end;
2348 }
2349
2350 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2351 if (local_stream == NULL) {
2352 PERROR("local_stream malloc");
2353 goto end;
2354 }
2355
2356 while (1) {
2357 high_prio = 0;
2358 num_hup = 0;
2359
2360 /*
2361 * the fds set has been updated, we need to update our
2362 * local array as well
2363 */
2364 pthread_mutex_lock(&consumer_data.lock);
2365 if (consumer_data.need_update) {
2366 free(pollfd);
2367 pollfd = NULL;
2368
2369 free(local_stream);
2370 local_stream = NULL;
2371
2372 /* allocate for all fds + 1 for the consumer_data_pipe */
2373 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
2374 if (pollfd == NULL) {
2375 PERROR("pollfd malloc");
2376 pthread_mutex_unlock(&consumer_data.lock);
2377 goto end;
2378 }
2379
2380 /* allocate for all fds + 1 for the consumer_data_pipe */
2381 local_stream = zmalloc((consumer_data.stream_count + 1) *
2382 sizeof(struct lttng_consumer_stream *));
2383 if (local_stream == NULL) {
2384 PERROR("local_stream malloc");
2385 pthread_mutex_unlock(&consumer_data.lock);
2386 goto end;
2387 }
2388 ret = update_poll_array(ctx, &pollfd, local_stream,
2389 data_ht);
2390 if (ret < 0) {
2391 ERR("Error in allocating pollfd or local_outfds");
2392 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2393 pthread_mutex_unlock(&consumer_data.lock);
2394 goto end;
2395 }
2396 nb_fd = ret;
2397 consumer_data.need_update = 0;
2398 }
2399 pthread_mutex_unlock(&consumer_data.lock);
2400
2401 /* No FDs and consumer_quit, consumer_cleanup the thread */
2402 if (nb_fd == 0 && consumer_quit == 1) {
2403 goto end;
2404 }
2405 /* poll on the array of fds */
2406 restart:
2407 DBG("polling on %d fd", nb_fd + 1);
2408 num_rdy = poll(pollfd, nb_fd + 1, -1);
2409 DBG("poll num_rdy : %d", num_rdy);
2410 if (num_rdy == -1) {
2411 /*
2412 * Restart interrupted system call.
2413 */
2414 if (errno == EINTR) {
2415 goto restart;
2416 }
2417 PERROR("Poll error");
2418 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2419 goto end;
2420 } else if (num_rdy == 0) {
2421 DBG("Polling thread timed out");
2422 goto end;
2423 }
2424
2425 /*
2426 * If the consumer_data_pipe triggered poll go directly to the
2427 * beginning of the loop to update the array. We want to prioritize
2428 * array update over low-priority reads.
2429 */
2430 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2431 ssize_t pipe_readlen;
2432
2433 DBG("consumer_data_pipe wake up");
2434 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2435 &new_stream, sizeof(new_stream));
2436 if (pipe_readlen < 0) {
2437 ERR("Consumer data pipe ret %zd", pipe_readlen);
2438 /* Continue so we can at least handle the current stream(s). */
2439 continue;
2440 }
2441
2442 /*
2443 * If the stream is NULL, just ignore it. It's also possible that
2444 * the sessiond poll thread changed the consumer_quit state and is
2445 * waking us up to test it.
2446 */
2447 if (new_stream == NULL) {
2448 validate_endpoint_status_data_stream();
2449 continue;
2450 }
2451
2452 /* Continue to update the local streams and handle prio ones */
2453 continue;
2454 }
2455
2456 /* Take care of high priority channels first. */
2457 for (i = 0; i < nb_fd; i++) {
2458 if (local_stream[i] == NULL) {
2459 continue;
2460 }
2461 if (pollfd[i].revents & POLLPRI) {
2462 DBG("Urgent read on fd %d", pollfd[i].fd);
2463 high_prio = 1;
2464 len = ctx->on_buffer_ready(local_stream[i], ctx);
2465 /* it's ok to have an unavailable sub-buffer */
2466 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2467 /* Clean the stream and free it. */
2468 consumer_del_stream(local_stream[i], data_ht);
2469 local_stream[i] = NULL;
2470 } else if (len > 0) {
2471 local_stream[i]->data_read = 1;
2472 }
2473 }
2474 }
2475
2476 /*
2477 * If we read high prio channel in this loop, try again
2478 * for more high prio data.
2479 */
2480 if (high_prio) {
2481 continue;
2482 }
2483
2484 /* Take care of low priority channels. */
2485 for (i = 0; i < nb_fd; i++) {
2486 if (local_stream[i] == NULL) {
2487 continue;
2488 }
2489 if ((pollfd[i].revents & POLLIN) ||
2490 local_stream[i]->hangup_flush_done) {
2491 DBG("Normal read on fd %d", pollfd[i].fd);
2492 len = ctx->on_buffer_ready(local_stream[i], ctx);
2493 /* it's ok to have an unavailable sub-buffer */
2494 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2495 /* Clean the stream and free it. */
2496 consumer_del_stream(local_stream[i], data_ht);
2497 local_stream[i] = NULL;
2498 } else if (len > 0) {
2499 local_stream[i]->data_read = 1;
2500 }
2501 }
2502 }
2503
2504 /* Handle hangup and errors */
2505 for (i = 0; i < nb_fd; i++) {
2506 if (local_stream[i] == NULL) {
2507 continue;
2508 }
2509 if (!local_stream[i]->hangup_flush_done
2510 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2511 && (consumer_data.type == LTTNG_CONSUMER32_UST
2512 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2513 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2514 pollfd[i].fd);
2515 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2516 /* Attempt read again, for the data we just flushed. */
2517 local_stream[i]->data_read = 1;
2518 }
2519 /*
2520 * If the poll flag is HUP/ERR/NVAL and we have
2521 * read no data in this pass, we can remove the
2522 * stream from its hash table.
2523 */
2524 if ((pollfd[i].revents & POLLHUP)) {
2525 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2526 if (!local_stream[i]->data_read) {
2527 consumer_del_stream(local_stream[i], data_ht);
2528 local_stream[i] = NULL;
2529 num_hup++;
2530 }
2531 } else if (pollfd[i].revents & POLLERR) {
2532 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2533 if (!local_stream[i]->data_read) {
2534 consumer_del_stream(local_stream[i], data_ht);
2535 local_stream[i] = NULL;
2536 num_hup++;
2537 }
2538 } else if (pollfd[i].revents & POLLNVAL) {
2539 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2540 if (!local_stream[i]->data_read) {
2541 consumer_del_stream(local_stream[i], data_ht);
2542 local_stream[i] = NULL;
2543 num_hup++;
2544 }
2545 }
2546 if (local_stream[i] != NULL) {
2547 local_stream[i]->data_read = 0;
2548 }
2549 }
2550 }
2551 end:
2552 DBG("polling thread exiting");
2553 free(pollfd);
2554 free(local_stream);
2555
2556 /*
2557 * Close the write side of the pipe so epoll_wait() in
2558 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2559 * read side of the pipe. If we close them both, epoll_wait strangely does
2560 * not return and could create a endless wait period if the pipe is the
2561 * only tracked fd in the poll set. The thread will take care of closing
2562 * the read side.
2563 */
2564 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
2565
2566 destroy_data_stream_ht(data_ht);
2567
2568 rcu_unregister_thread();
2569 return NULL;
2570 }
2571
2572 /*
2573 * Close wake-up end of each stream belonging to the channel. This will
2574 * allow the poll() on the stream read-side to detect when the
2575 * write-side (application) finally closes them.
2576 */
2577 static
2578 void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2579 {
2580 struct lttng_ht *ht;
2581 struct lttng_consumer_stream *stream;
2582 struct lttng_ht_iter iter;
2583
2584 ht = consumer_data.stream_per_chan_id_ht;
2585
2586 rcu_read_lock();
2587 cds_lfht_for_each_entry_duplicate(ht->ht,
2588 ht->hash_fct(&channel->key, lttng_ht_seed),
2589 ht->match_fct, &channel->key,
2590 &iter.iter, stream, node_channel_id.node) {
2591 /*
2592 * Protect against teardown with mutex.
2593 */
2594 pthread_mutex_lock(&stream->lock);
2595 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2596 goto next;
2597 }
2598 switch (consumer_data.type) {
2599 case LTTNG_CONSUMER_KERNEL:
2600 break;
2601 case LTTNG_CONSUMER32_UST:
2602 case LTTNG_CONSUMER64_UST:
2603 /*
2604 * Note: a mutex is taken internally within
2605 * liblttng-ust-ctl to protect timer wakeup_fd
2606 * use from concurrent close.
2607 */
2608 lttng_ustconsumer_close_stream_wakeup(stream);
2609 break;
2610 default:
2611 ERR("Unknown consumer_data type");
2612 assert(0);
2613 }
2614 next:
2615 pthread_mutex_unlock(&stream->lock);
2616 }
2617 rcu_read_unlock();
2618 }
2619
2620 static void destroy_channel_ht(struct lttng_ht *ht)
2621 {
2622 struct lttng_ht_iter iter;
2623 struct lttng_consumer_channel *channel;
2624 int ret;
2625
2626 if (ht == NULL) {
2627 return;
2628 }
2629
2630 rcu_read_lock();
2631 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2632 ret = lttng_ht_del(ht, &iter);
2633 assert(ret != 0);
2634 }
2635 rcu_read_unlock();
2636
2637 lttng_ht_destroy(ht);
2638 }
2639
2640 /*
2641 * This thread polls the channel fds to detect when they are being
2642 * closed. It closes all related streams if the channel is detected as
2643 * closed. It is currently only used as a shim layer for UST because the
2644 * consumerd needs to keep the per-stream wakeup end of pipes open for
2645 * periodical flush.
2646 */
2647 void *consumer_thread_channel_poll(void *data)
2648 {
2649 int ret, i, pollfd;
2650 uint32_t revents, nb_fd;
2651 struct lttng_consumer_channel *chan = NULL;
2652 struct lttng_ht_iter iter;
2653 struct lttng_ht_node_u64 *node;
2654 struct lttng_poll_event events;
2655 struct lttng_consumer_local_data *ctx = data;
2656 struct lttng_ht *channel_ht;
2657
2658 rcu_register_thread();
2659
2660 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2661 if (!channel_ht) {
2662 /* ENOMEM at this point. Better to bail out. */
2663 goto end_ht;
2664 }
2665
2666 DBG("Thread channel poll started");
2667
2668 /* Size is set to 1 for the consumer_channel pipe */
2669 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2670 if (ret < 0) {
2671 ERR("Poll set creation failed");
2672 goto end_poll;
2673 }
2674
2675 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2676 if (ret < 0) {
2677 goto end;
2678 }
2679
2680 /* Main loop */
2681 DBG("Channel main loop started");
2682
2683 while (1) {
2684 /* Only the channel pipe is set */
2685 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2686 goto end;
2687 }
2688
2689 restart:
2690 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2691 ret = lttng_poll_wait(&events, -1);
2692 DBG("Channel event catched in thread");
2693 if (ret < 0) {
2694 if (errno == EINTR) {
2695 ERR("Poll EINTR catched");
2696 goto restart;
2697 }
2698 goto end;
2699 }
2700
2701 nb_fd = ret;
2702
2703 /* From here, the event is a channel wait fd */
2704 for (i = 0; i < nb_fd; i++) {
2705 revents = LTTNG_POLL_GETEV(&events, i);
2706 pollfd = LTTNG_POLL_GETFD(&events, i);
2707
2708 /* Just don't waste time if no returned events for the fd */
2709 if (!revents) {
2710 continue;
2711 }
2712 if (pollfd == ctx->consumer_channel_pipe[0]) {
2713 if (revents & (LPOLLERR | LPOLLHUP)) {
2714 DBG("Channel thread pipe hung up");
2715 /*
2716 * Remove the pipe from the poll set and continue the loop
2717 * since their might be data to consume.
2718 */
2719 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2720 continue;
2721 } else if (revents & LPOLLIN) {
2722 enum consumer_channel_action action;
2723 uint64_t key;
2724
2725 ret = read_channel_pipe(ctx, &chan, &key, &action);
2726 if (ret <= 0) {
2727 ERR("Error reading channel pipe");
2728 continue;
2729 }
2730
2731 switch (action) {
2732 case CONSUMER_CHANNEL_ADD:
2733 DBG("Adding channel %d to poll set",
2734 chan->wait_fd);
2735
2736 lttng_ht_node_init_u64(&chan->wait_fd_node,
2737 chan->wait_fd);
2738 rcu_read_lock();
2739 lttng_ht_add_unique_u64(channel_ht,
2740 &chan->wait_fd_node);
2741 rcu_read_unlock();
2742 /* Add channel to the global poll events list */
2743 lttng_poll_add(&events, chan->wait_fd,
2744 LPOLLIN | LPOLLPRI);
2745 break;
2746 case CONSUMER_CHANNEL_DEL:
2747 {
2748 struct lttng_consumer_stream *stream, *stmp;
2749
2750 rcu_read_lock();
2751 chan = consumer_find_channel(key);
2752 if (!chan) {
2753 rcu_read_unlock();
2754 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
2755 break;
2756 }
2757 lttng_poll_del(&events, chan->wait_fd);
2758 iter.iter.node = &chan->wait_fd_node.node;
2759 ret = lttng_ht_del(channel_ht, &iter);
2760 assert(ret == 0);
2761 consumer_close_channel_streams(chan);
2762
2763 switch (consumer_data.type) {
2764 case LTTNG_CONSUMER_KERNEL:
2765 break;
2766 case LTTNG_CONSUMER32_UST:
2767 case LTTNG_CONSUMER64_UST:
2768 /* Delete streams that might have been left in the stream list. */
2769 cds_list_for_each_entry_safe(stream, stmp, &chan->streams.head,
2770 send_node) {
2771 cds_list_del(&stream->send_node);
2772 lttng_ustconsumer_del_stream(stream);
2773 uatomic_sub(&stream->chan->refcount, 1);
2774 assert(&chan->refcount);
2775 free(stream);
2776 }
2777 break;
2778 default:
2779 ERR("Unknown consumer_data type");
2780 assert(0);
2781 }
2782
2783 /*
2784 * Release our own refcount. Force channel deletion even if
2785 * streams were not initialized.
2786 */
2787 if (!uatomic_sub_return(&chan->refcount, 1)) {
2788 consumer_del_channel(chan);
2789 }
2790 rcu_read_unlock();
2791 goto restart;
2792 }
2793 case CONSUMER_CHANNEL_QUIT:
2794 /*
2795 * Remove the pipe from the poll set and continue the loop
2796 * since their might be data to consume.
2797 */
2798 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2799 continue;
2800 default:
2801 ERR("Unknown action");
2802 break;
2803 }
2804 }
2805
2806 /* Handle other stream */
2807 continue;
2808 }
2809
2810 rcu_read_lock();
2811 {
2812 uint64_t tmp_id = (uint64_t) pollfd;
2813
2814 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
2815 }
2816 node = lttng_ht_iter_get_node_u64(&iter);
2817 assert(node);
2818
2819 chan = caa_container_of(node, struct lttng_consumer_channel,
2820 wait_fd_node);
2821
2822 /* Check for error event */
2823 if (revents & (LPOLLERR | LPOLLHUP)) {
2824 DBG("Channel fd %d is hup|err.", pollfd);
2825
2826 lttng_poll_del(&events, chan->wait_fd);
2827 ret = lttng_ht_del(channel_ht, &iter);
2828 assert(ret == 0);
2829 consumer_close_channel_streams(chan);
2830
2831 /* Release our own refcount */
2832 if (!uatomic_sub_return(&chan->refcount, 1)
2833 && !uatomic_read(&chan->nb_init_stream_left)) {
2834 consumer_del_channel(chan);
2835 }
2836 }
2837
2838 /* Release RCU lock for the channel looked up */
2839 rcu_read_unlock();
2840 }
2841 }
2842
2843 end:
2844 lttng_poll_clean(&events);
2845 end_poll:
2846 destroy_channel_ht(channel_ht);
2847 end_ht:
2848 DBG("Channel poll thread exiting");
2849 rcu_unregister_thread();
2850 return NULL;
2851 }
2852
2853 static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
2854 struct pollfd *sockpoll, int client_socket)
2855 {
2856 int ret;
2857
2858 assert(ctx);
2859 assert(sockpoll);
2860
2861 if (lttng_consumer_poll_socket(sockpoll) < 0) {
2862 ret = -1;
2863 goto error;
2864 }
2865 DBG("Metadata connection on client_socket");
2866
2867 /* Blocking call, waiting for transmission */
2868 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
2869 if (ctx->consumer_metadata_socket < 0) {
2870 WARN("On accept metadata");
2871 ret = -1;
2872 goto error;
2873 }
2874 ret = 0;
2875
2876 error:
2877 return ret;
2878 }
2879
2880 /*
2881 * This thread listens on the consumerd socket and receives the file
2882 * descriptors from the session daemon.
2883 */
2884 void *consumer_thread_sessiond_poll(void *data)
2885 {
2886 int sock = -1, client_socket, ret;
2887 /*
2888 * structure to poll for incoming data on communication socket avoids
2889 * making blocking sockets.
2890 */
2891 struct pollfd consumer_sockpoll[2];
2892 struct lttng_consumer_local_data *ctx = data;
2893
2894 rcu_register_thread();
2895
2896 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2897 unlink(ctx->consumer_command_sock_path);
2898 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2899 if (client_socket < 0) {
2900 ERR("Cannot create command socket");
2901 goto end;
2902 }
2903
2904 ret = lttcomm_listen_unix_sock(client_socket);
2905 if (ret < 0) {
2906 goto end;
2907 }
2908
2909 DBG("Sending ready command to lttng-sessiond");
2910 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
2911 /* return < 0 on error, but == 0 is not fatal */
2912 if (ret < 0) {
2913 ERR("Error sending ready command to lttng-sessiond");
2914 goto end;
2915 }
2916
2917 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2918 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2919 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2920 consumer_sockpoll[1].fd = client_socket;
2921 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2922
2923 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2924 goto end;
2925 }
2926 DBG("Connection on client_socket");
2927
2928 /* Blocking call, waiting for transmission */
2929 sock = lttcomm_accept_unix_sock(client_socket);
2930 if (sock < 0) {
2931 WARN("On accept");
2932 goto end;
2933 }
2934
2935 /*
2936 * Setup metadata socket which is the second socket connection on the
2937 * command unix socket.
2938 */
2939 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
2940 if (ret < 0) {
2941 goto end;
2942 }
2943
2944 /* This socket is not useful anymore. */
2945 ret = close(client_socket);
2946 if (ret < 0) {
2947 PERROR("close client_socket");
2948 }
2949 client_socket = -1;
2950
2951 /* update the polling structure to poll on the established socket */
2952 consumer_sockpoll[1].fd = sock;
2953 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2954
2955 while (1) {
2956 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2957 goto end;
2958 }
2959 DBG("Incoming command on sock");
2960 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2961 if (ret == -ENOENT) {
2962 DBG("Received STOP command");
2963 goto end;
2964 }
2965 if (ret <= 0) {
2966 /*
2967 * This could simply be a session daemon quitting. Don't output
2968 * ERR() here.
2969 */
2970 DBG("Communication interrupted on command socket");
2971 goto end;
2972 }
2973 if (consumer_quit) {
2974 DBG("consumer_thread_receive_fds received quit from signal");
2975 goto end;
2976 }
2977 DBG("received command on sock");
2978 }
2979 end:
2980 DBG("Consumer thread sessiond poll exiting");
2981
2982 /*
2983 * Close metadata streams since the producer is the session daemon which
2984 * just died.
2985 *
2986 * NOTE: for now, this only applies to the UST tracer.
2987 */
2988 lttng_consumer_close_metadata();
2989
2990 /*
2991 * when all fds have hung up, the polling thread
2992 * can exit cleanly
2993 */
2994 consumer_quit = 1;
2995
2996 /*
2997 * Notify the data poll thread to poll back again and test the
2998 * consumer_quit state that we just set so to quit gracefully.
2999 */
3000 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
3001
3002 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
3003
3004 /* Cleaning up possibly open sockets. */
3005 if (sock >= 0) {
3006 ret = close(sock);
3007 if (ret < 0) {
3008 PERROR("close sock sessiond poll");
3009 }
3010 }
3011 if (client_socket >= 0) {
3012 ret = close(client_socket);
3013 if (ret < 0) {
3014 PERROR("close client_socket sessiond poll");
3015 }
3016 }
3017
3018 rcu_unregister_thread();
3019 return NULL;
3020 }
3021
3022 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
3023 struct lttng_consumer_local_data *ctx)
3024 {
3025 ssize_t ret;
3026
3027 pthread_mutex_lock(&stream->lock);
3028
3029 switch (consumer_data.type) {
3030 case LTTNG_CONSUMER_KERNEL:
3031 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3032 break;
3033 case LTTNG_CONSUMER32_UST:
3034 case LTTNG_CONSUMER64_UST:
3035 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3036 break;
3037 default:
3038 ERR("Unknown consumer_data type");
3039 assert(0);
3040 ret = -ENOSYS;
3041 break;
3042 }
3043
3044 pthread_mutex_unlock(&stream->lock);
3045 return ret;
3046 }
3047
3048 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3049 {
3050 switch (consumer_data.type) {
3051 case LTTNG_CONSUMER_KERNEL:
3052 return lttng_kconsumer_on_recv_stream(stream);
3053 case LTTNG_CONSUMER32_UST:
3054 case LTTNG_CONSUMER64_UST:
3055 return lttng_ustconsumer_on_recv_stream(stream);
3056 default:
3057 ERR("Unknown consumer_data type");
3058 assert(0);
3059 return -ENOSYS;
3060 }
3061 }
3062
3063 /*
3064 * Allocate and set consumer data hash tables.
3065 */
3066 void lttng_consumer_init(void)
3067 {
3068 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3069 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3070 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3071 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3072 }
3073
3074 /*
3075 * Process the ADD_RELAYD command receive by a consumer.
3076 *
3077 * This will create a relayd socket pair and add it to the relayd hash table.
3078 * The caller MUST acquire a RCU read side lock before calling it.
3079 */
3080 int consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type,
3081 struct lttng_consumer_local_data *ctx, int sock,
3082 struct pollfd *consumer_sockpoll,
3083 struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id)
3084 {
3085 int fd = -1, ret = -1, relayd_created = 0;
3086 enum lttng_error_code ret_code = LTTNG_OK;
3087 struct consumer_relayd_sock_pair *relayd = NULL;
3088
3089 assert(ctx);
3090 assert(relayd_sock);
3091
3092 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx);
3093
3094 /* Get relayd reference if exists. */
3095 relayd = consumer_find_relayd(net_seq_idx);
3096 if (relayd == NULL) {
3097 assert(sock_type == LTTNG_STREAM_CONTROL);
3098 /* Not found. Allocate one. */
3099 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3100 if (relayd == NULL) {
3101 ret = -ENOMEM;
3102 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3103 goto error;
3104 } else {
3105 relayd->sessiond_session_id = sessiond_id;
3106 relayd_created = 1;
3107 }
3108
3109 /*
3110 * This code path MUST continue to the consumer send status message to
3111 * we can notify the session daemon and continue our work without
3112 * killing everything.
3113 */
3114 } else {
3115 /*
3116 * relayd key should never be found for control socket.
3117 */
3118 assert(sock_type != LTTNG_STREAM_CONTROL);
3119 }
3120
3121 /* First send a status message before receiving the fds. */
3122 ret = consumer_send_status_msg(sock, LTTNG_OK);
3123 if (ret < 0) {
3124 /* Somehow, the session daemon is not responding anymore. */
3125 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3126 goto error_nosignal;
3127 }
3128
3129 /* Poll on consumer socket. */
3130 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3131 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3132 ret = -EINTR;
3133 goto error_nosignal;
3134 }
3135
3136 /* Get relayd socket from session daemon */
3137 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3138 if (ret != sizeof(fd)) {
3139 ret = -1;
3140 fd = -1; /* Just in case it gets set with an invalid value. */
3141
3142 /*
3143 * Failing to receive FDs might indicate a major problem such as
3144 * reaching a fd limit during the receive where the kernel returns a
3145 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3146 * don't take any chances and stop everything.
3147 *
3148 * XXX: Feature request #558 will fix that and avoid this possible
3149 * issue when reaching the fd limit.
3150 */
3151 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3152 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
3153 goto error;
3154 }
3155
3156 /* Copy socket information and received FD */
3157 switch (sock_type) {
3158 case LTTNG_STREAM_CONTROL:
3159 /* Copy received lttcomm socket */
3160 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3161 ret = lttcomm_create_sock(&relayd->control_sock.sock);
3162 /* Handle create_sock error. */
3163 if (ret < 0) {
3164 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3165 goto error;
3166 }
3167 /*
3168 * Close the socket created internally by
3169 * lttcomm_create_sock, so we can replace it by the one
3170 * received from sessiond.
3171 */
3172 if (close(relayd->control_sock.sock.fd)) {
3173 PERROR("close");
3174 }
3175
3176 /* Assign new file descriptor */
3177 relayd->control_sock.sock.fd = fd;
3178 fd = -1; /* For error path */
3179 /* Assign version values. */
3180 relayd->control_sock.major = relayd_sock->major;
3181 relayd->control_sock.minor = relayd_sock->minor;
3182
3183 /*
3184 * Create a session on the relayd and store the returned id. Lock the
3185 * control socket mutex if the relayd was NOT created before.
3186 */
3187 if (!relayd_created) {
3188 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3189 }
3190 ret = relayd_create_session(&relayd->control_sock,
3191 &relayd->relayd_session_id);
3192 if (!relayd_created) {
3193 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3194 }
3195 if (ret < 0) {
3196 /*
3197 * Close all sockets of a relayd object. It will be freed if it was
3198 * created at the error code path or else it will be garbage
3199 * collect.
3200 */
3201 (void) relayd_close(&relayd->control_sock);
3202 (void) relayd_close(&relayd->data_sock);
3203 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3204 goto error;
3205 }
3206
3207 break;
3208 case LTTNG_STREAM_DATA:
3209 /* Copy received lttcomm socket */
3210 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3211 ret = lttcomm_create_sock(&relayd->data_sock.sock);
3212 /* Handle create_sock error. */
3213 if (ret < 0) {
3214 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3215 goto error;
3216 }
3217 /*
3218 * Close the socket created internally by
3219 * lttcomm_create_sock, so we can replace it by the one
3220 * received from sessiond.
3221 */
3222 if (close(relayd->data_sock.sock.fd)) {
3223 PERROR("close");
3224 }
3225
3226 /* Assign new file descriptor */
3227 relayd->data_sock.sock.fd = fd;
3228 fd = -1; /* for eventual error paths */
3229 /* Assign version values. */
3230 relayd->data_sock.major = relayd_sock->major;
3231 relayd->data_sock.minor = relayd_sock->minor;
3232 break;
3233 default:
3234 ERR("Unknown relayd socket type (%d)", sock_type);
3235 ret = -1;
3236 ret_code = LTTCOMM_CONSUMERD_FATAL;
3237 goto error;
3238 }
3239
3240 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
3241 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3242 relayd->net_seq_idx, fd);
3243
3244 /* We successfully added the socket. Send status back. */
3245 ret = consumer_send_status_msg(sock, ret_code);
3246 if (ret < 0) {
3247 /* Somehow, the session daemon is not responding anymore. */
3248 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3249 goto error_nosignal;
3250 }
3251
3252 /*
3253 * Add relayd socket pair to consumer data hashtable. If object already
3254 * exists or on error, the function gracefully returns.
3255 */
3256 add_relayd(relayd);
3257
3258 /* All good! */
3259 return 0;
3260
3261 error:
3262 if (consumer_send_status_msg(sock, ret_code) < 0) {
3263 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3264 }
3265
3266 error_nosignal:
3267 /* Close received socket if valid. */
3268 if (fd >= 0) {
3269 if (close(fd)) {
3270 PERROR("close received socket");
3271 }
3272 }
3273
3274 if (relayd_created) {
3275 free(relayd);
3276 }
3277
3278 return ret;
3279 }
3280
3281 /*
3282 * Try to lock the stream mutex.
3283 *
3284 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3285 */
3286 static int stream_try_lock(struct lttng_consumer_stream *stream)
3287 {
3288 int ret;
3289
3290 assert(stream);
3291
3292 /*
3293 * Try to lock the stream mutex. On failure, we know that the stream is
3294 * being used else where hence there is data still being extracted.
3295 */
3296 ret = pthread_mutex_trylock(&stream->lock);
3297 if (ret) {
3298 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3299 ret = 0;
3300 goto end;
3301 }
3302
3303 ret = 1;
3304
3305 end:
3306 return ret;
3307 }
3308
3309 /*
3310 * Search for a relayd associated to the session id and return the reference.
3311 *
3312 * A rcu read side lock MUST be acquire before calling this function and locked
3313 * until the relayd object is no longer necessary.
3314 */
3315 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3316 {
3317 struct lttng_ht_iter iter;
3318 struct consumer_relayd_sock_pair *relayd = NULL;
3319
3320 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3321 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3322 node.node) {
3323 /*
3324 * Check by sessiond id which is unique here where the relayd session
3325 * id might not be when having multiple relayd.
3326 */
3327 if (relayd->sessiond_session_id == id) {
3328 /* Found the relayd. There can be only one per id. */
3329 goto found;
3330 }
3331 }
3332
3333 return NULL;
3334
3335 found:
3336 return relayd;
3337 }
3338
3339 /*
3340 * Check if for a given session id there is still data needed to be extract
3341 * from the buffers.
3342 *
3343 * Return 1 if data is pending or else 0 meaning ready to be read.
3344 */
3345 int consumer_data_pending(uint64_t id)
3346 {
3347 int ret;
3348 struct lttng_ht_iter iter;
3349 struct lttng_ht *ht;
3350 struct lttng_consumer_stream *stream;
3351 struct consumer_relayd_sock_pair *relayd = NULL;
3352 int (*data_pending)(struct lttng_consumer_stream *);
3353
3354 DBG("Consumer data pending command on session id %" PRIu64, id);
3355
3356 rcu_read_lock();
3357 pthread_mutex_lock(&consumer_data.lock);
3358
3359 switch (consumer_data.type) {
3360 case LTTNG_CONSUMER_KERNEL:
3361 data_pending = lttng_kconsumer_data_pending;
3362 break;
3363 case LTTNG_CONSUMER32_UST:
3364 case LTTNG_CONSUMER64_UST:
3365 data_pending = lttng_ustconsumer_data_pending;
3366 break;
3367 default:
3368 ERR("Unknown consumer data type");
3369 assert(0);
3370 }
3371
3372 /* Ease our life a bit */
3373 ht = consumer_data.stream_list_ht;
3374
3375 relayd = find_relayd_by_session_id(id);
3376 if (relayd) {
3377 /* Send init command for data pending. */
3378 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3379 ret = relayd_begin_data_pending(&relayd->control_sock,
3380 relayd->relayd_session_id);
3381 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3382 if (ret < 0) {
3383 /* Communication error thus the relayd so no data pending. */
3384 goto data_not_pending;
3385 }
3386 }
3387
3388 cds_lfht_for_each_entry_duplicate(ht->ht,
3389 ht->hash_fct(&id, lttng_ht_seed),
3390 ht->match_fct, &id,
3391 &iter.iter, stream, node_session_id.node) {
3392 /* If this call fails, the stream is being used hence data pending. */
3393 ret = stream_try_lock(stream);
3394 if (!ret) {
3395 goto data_pending;
3396 }
3397
3398 /*
3399 * A removed node from the hash table indicates that the stream has
3400 * been deleted thus having a guarantee that the buffers are closed
3401 * on the consumer side. However, data can still be transmitted
3402 * over the network so don't skip the relayd check.
3403 */
3404 ret = cds_lfht_is_node_deleted(&stream->node.node);
3405 if (!ret) {
3406 /*
3407 * An empty output file is not valid. We need at least one packet
3408 * generated per stream, even if it contains no event, so it
3409 * contains at least one packet header.
3410 */
3411 if (stream->output_written == 0) {
3412 pthread_mutex_unlock(&stream->lock);
3413 goto data_pending;
3414 }
3415 /* Check the stream if there is data in the buffers. */
3416 ret = data_pending(stream);
3417 if (ret == 1) {
3418 pthread_mutex_unlock(&stream->lock);
3419 goto data_pending;
3420 }
3421 }
3422
3423 /* Relayd check */
3424 if (relayd) {
3425 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3426 if (stream->metadata_flag) {
3427 ret = relayd_quiescent_control(&relayd->control_sock,
3428 stream->relayd_stream_id);
3429 } else {
3430 ret = relayd_data_pending(&relayd->control_sock,
3431 stream->relayd_stream_id,
3432 stream->next_net_seq_num - 1);
3433 }
3434 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3435 if (ret == 1) {
3436 pthread_mutex_unlock(&stream->lock);
3437 goto data_pending;
3438 }
3439 }
3440 pthread_mutex_unlock(&stream->lock);
3441 }
3442
3443 if (relayd) {
3444 unsigned int is_data_inflight = 0;
3445
3446 /* Send init command for data pending. */
3447 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3448 ret = relayd_end_data_pending(&relayd->control_sock,
3449 relayd->relayd_session_id, &is_data_inflight);
3450 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3451 if (ret < 0) {
3452 goto data_not_pending;
3453 }
3454 if (is_data_inflight) {
3455 goto data_pending;
3456 }
3457 }
3458
3459 /*
3460 * Finding _no_ node in the hash table and no inflight data means that the
3461 * stream(s) have been removed thus data is guaranteed to be available for
3462 * analysis from the trace files.
3463 */
3464
3465 data_not_pending:
3466 /* Data is available to be read by a viewer. */
3467 pthread_mutex_unlock(&consumer_data.lock);
3468 rcu_read_unlock();
3469 return 0;
3470
3471 data_pending:
3472 /* Data is still being extracted from buffers. */
3473 pthread_mutex_unlock(&consumer_data.lock);
3474 rcu_read_unlock();
3475 return 1;
3476 }
3477
3478 /*
3479 * Send a ret code status message to the sessiond daemon.
3480 *
3481 * Return the sendmsg() return value.
3482 */
3483 int consumer_send_status_msg(int sock, int ret_code)
3484 {
3485 struct lttcomm_consumer_status_msg msg;
3486
3487 msg.ret_code = ret_code;
3488
3489 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3490 }
3491
3492 /*
3493 * Send a channel status message to the sessiond daemon.
3494 *
3495 * Return the sendmsg() return value.
3496 */
3497 int consumer_send_status_channel(int sock,
3498 struct lttng_consumer_channel *channel)
3499 {
3500 struct lttcomm_consumer_status_channel msg;
3501
3502 assert(sock >= 0);
3503
3504 if (!channel) {
3505 msg.ret_code = -LTTNG_ERR_UST_CHAN_FAIL;
3506 } else {
3507 msg.ret_code = LTTNG_OK;
3508 msg.key = channel->key;
3509 msg.stream_count = channel->streams.count;
3510 }
3511
3512 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3513 }
3514
3515 /*
3516 * Using a maximum stream size with the produced and consumed position of a
3517 * stream, computes the new consumed position to be as close as possible to the
3518 * maximum possible stream size.
3519 *
3520 * If maximum stream size is lower than the possible buffer size (produced -
3521 * consumed), the consumed_pos given is returned untouched else the new value
3522 * is returned.
3523 */
3524 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos,
3525 unsigned long produced_pos, uint64_t max_stream_size)
3526 {
3527 if (max_stream_size && max_stream_size < (produced_pos - consumed_pos)) {
3528 /* Offset from the produced position to get the latest buffers. */
3529 return produced_pos - max_stream_size;
3530 }
3531
3532 return consumed_pos;
3533 }
This page took 0.164512 seconds and 4 git commands to generate.