xref: /freebsd/sys/contrib/openzfs/module/zfs/bqueue.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2014, 2018 by Delphix. All rights reserved.
14  */
15 
16 #include	<sys/bqueue.h>
17 #include	<sys/zfs_context.h>
18 
19 static inline bqueue_node_t *
obj2node(bqueue_t * q,void * data)20 obj2node(bqueue_t *q, void *data)
21 {
22 	return ((bqueue_node_t *)((char *)data + q->bq_node_offset));
23 }
24 
25 /*
26  * Initialize a blocking queue  The maximum capacity of the queue is set to
27  * size.  Types that are stored in a bqueue must contain a bqueue_node_t, and
28  * node_offset must be its offset from the start of the struct. fill_fraction
29  * is a performance tuning value; when the queue is full, any threads
30  * attempting to enqueue records will block.  They will block until they're
31  * signaled, which will occur when the queue is at least 1/fill_fraction
32  * empty.  Similar behavior occurs on dequeue; if the queue is empty, threads
33  * block.  They will be signalled when the queue has 1/fill_fraction full.
34  * As a result, you must call bqueue_enqueue_flush() when you enqueue your
35  * final record on a thread, in case the dequeuing threads are currently
36  * blocked and that enqueue does not cause them to be woken. Alternatively,
37  * this behavior can be disabled (causing signaling to happen immediately) by
38  * setting fill_fraction to any value larger than size. Return 0 on success,
39  * or -1 on failure.
40  *
41  * Note: The caller must ensure that for a given bqueue_t, there's only a
42  * single call to bqueue_enqueue() running at a time (e.g. by calling only
43  * from a single thread, or with locking around the call). Similarly, the
44  * caller must ensure that there's only a single call to bqueue_dequeue()
45  * running at a time. However, the one call to bqueue_enqueue() may be
46  * invoked concurrently with the one call to bqueue_dequeue().
47  */
48 int
bqueue_init(bqueue_t * q,uint_t fill_fraction,size_t size,size_t node_offset)49 bqueue_init(bqueue_t *q, uint_t fill_fraction, size_t size, size_t node_offset)
50 {
51 	if (fill_fraction == 0) {
52 		return (-1);
53 	}
54 	list_create(&q->bq_list, node_offset + sizeof (bqueue_node_t),
55 	    node_offset + offsetof(bqueue_node_t, bqn_node));
56 	list_create(&q->bq_dequeuing_list, node_offset + sizeof (bqueue_node_t),
57 	    node_offset + offsetof(bqueue_node_t, bqn_node));
58 	list_create(&q->bq_enqueuing_list, node_offset + sizeof (bqueue_node_t),
59 	    node_offset + offsetof(bqueue_node_t, bqn_node));
60 	cv_init(&q->bq_add_cv, NULL, CV_DEFAULT, NULL);
61 	cv_init(&q->bq_pop_cv, NULL, CV_DEFAULT, NULL);
62 	mutex_init(&q->bq_lock, NULL, MUTEX_DEFAULT, NULL);
63 	q->bq_node_offset = node_offset;
64 	q->bq_size = 0;
65 	q->bq_dequeuing_size = 0;
66 	q->bq_enqueuing_size = 0;
67 	q->bq_maxsize = size;
68 	q->bq_fill_fraction = fill_fraction;
69 	return (0);
70 }
71 
72 /*
73  * Destroy a blocking queue.  This function asserts that there are no
74  * elements in the queue, and no one is blocked on the condition
75  * variables.
76  */
77 void
bqueue_destroy(bqueue_t * q)78 bqueue_destroy(bqueue_t *q)
79 {
80 	mutex_enter(&q->bq_lock);
81 	ASSERT0(q->bq_size);
82 	ASSERT0(q->bq_dequeuing_size);
83 	ASSERT0(q->bq_enqueuing_size);
84 	cv_destroy(&q->bq_add_cv);
85 	cv_destroy(&q->bq_pop_cv);
86 	list_destroy(&q->bq_list);
87 	list_destroy(&q->bq_dequeuing_list);
88 	list_destroy(&q->bq_enqueuing_list);
89 	mutex_exit(&q->bq_lock);
90 	mutex_destroy(&q->bq_lock);
91 }
92 
93 static void
bqueue_enqueue_impl(bqueue_t * q,void * data,size_t item_size,boolean_t flush)94 bqueue_enqueue_impl(bqueue_t *q, void *data, size_t item_size, boolean_t flush)
95 {
96 	ASSERT3U(item_size, >, 0);
97 	ASSERT3U(item_size, <=, q->bq_maxsize);
98 
99 	obj2node(q, data)->bqn_size = item_size;
100 	q->bq_enqueuing_size += item_size;
101 	list_insert_tail(&q->bq_enqueuing_list, data);
102 
103 	if (flush ||
104 	    q->bq_enqueuing_size >= q->bq_maxsize / q->bq_fill_fraction) {
105 		/* Append the enquing list to the shared list. */
106 		mutex_enter(&q->bq_lock);
107 		while (q->bq_size > q->bq_maxsize) {
108 			cv_wait_sig(&q->bq_add_cv, &q->bq_lock);
109 		}
110 		q->bq_size += q->bq_enqueuing_size;
111 		list_move_tail(&q->bq_list, &q->bq_enqueuing_list);
112 		q->bq_enqueuing_size = 0;
113 		cv_broadcast(&q->bq_pop_cv);
114 		mutex_exit(&q->bq_lock);
115 	}
116 }
117 
118 /*
119  * Add data to q, consuming size units of capacity.  If there is insufficient
120  * capacity to consume size units, block until capacity exists.  Asserts size is
121  * > 0.
122  */
123 void
bqueue_enqueue(bqueue_t * q,void * data,size_t item_size)124 bqueue_enqueue(bqueue_t *q, void *data, size_t item_size)
125 {
126 	bqueue_enqueue_impl(q, data, item_size, B_FALSE);
127 }
128 
129 /*
130  * Enqueue an entry, and then flush the queue.  This forces the popping threads
131  * to wake up, even if we're below the fill fraction.  We have this in a single
132  * function, rather than having a separate call, because it prevents race
133  * conditions between the enqueuing thread and the dequeuing thread, where the
134  * enqueueing thread will wake up the dequeuing thread, that thread will
135  * destroy the condvar before the enqueuing thread is done.
136  */
137 void
bqueue_enqueue_flush(bqueue_t * q,void * data,size_t item_size)138 bqueue_enqueue_flush(bqueue_t *q, void *data, size_t item_size)
139 {
140 	bqueue_enqueue_impl(q, data, item_size, B_TRUE);
141 }
142 
143 /*
144  * Take the first element off of q.  If there are no elements on the queue, wait
145  * until one is put there.  Return the removed element.
146  */
147 void *
bqueue_dequeue(bqueue_t * q)148 bqueue_dequeue(bqueue_t *q)
149 {
150 	void *ret = list_remove_head(&q->bq_dequeuing_list);
151 	if (ret == NULL) {
152 		/*
153 		 * Dequeuing list is empty.  Wait for there to be something on
154 		 * the shared list, then move the entire shared list to the
155 		 * dequeuing list.
156 		 */
157 		mutex_enter(&q->bq_lock);
158 		while (q->bq_size == 0) {
159 			cv_wait_sig(&q->bq_pop_cv, &q->bq_lock);
160 		}
161 		ASSERT0(q->bq_dequeuing_size);
162 		ASSERT(list_is_empty(&q->bq_dequeuing_list));
163 		list_move_tail(&q->bq_dequeuing_list, &q->bq_list);
164 		q->bq_dequeuing_size = q->bq_size;
165 		q->bq_size = 0;
166 		cv_broadcast(&q->bq_add_cv);
167 		mutex_exit(&q->bq_lock);
168 		ret = list_remove_head(&q->bq_dequeuing_list);
169 	}
170 	q->bq_dequeuing_size -= obj2node(q, ret)->bqn_size;
171 	return (ret);
172 }
173