]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-coroutine.c
iotests: add filter_qmp_generated_node_ids()
[mirror_qemu.git] / util / qemu-coroutine.c
CommitLineData
00dccaf1
KW
1/*
2 * QEMU coroutines
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Kevin Wolf <kwolf@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
aafd7584 15#include "qemu/osdep.h"
00dccaf1 16#include "trace.h"
b84c4586 17#include "qemu/thread.h"
4d68e86b 18#include "qemu/atomic.h"
10817bf0 19#include "qemu/coroutine_int.h"
ac387a08 20#include "qemu/coroutine-tls.h"
0c330a73 21#include "block/aio.h"
00dccaf1 22
9ec7a59b
KW
23/**
24 * The minimal batch size is always 64, coroutines from the release_pool are
25 * reused as soon as there are 64 coroutines in it. The maximum pool size starts
26 * with 64 and is increased on demand so that coroutines are not deleted even if
27 * they are not immediately reused.
28 */
40239784 29enum {
9ec7a59b
KW
30 POOL_MIN_BATCH_SIZE = 64,
31 POOL_INITIAL_MAX_SIZE = 64,
40239784
PB
32};
33
34/** Free list to speed up creation */
4d68e86b 35static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
9ec7a59b 36static unsigned int pool_max_size = POOL_INITIAL_MAX_SIZE;
4d68e86b 37static unsigned int release_pool_size;
ac387a08
SH
38
39typedef QSLIST_HEAD(, Coroutine) CoroutineQSList;
40QEMU_DEFINE_STATIC_CO_TLS(CoroutineQSList, alloc_pool);
41QEMU_DEFINE_STATIC_CO_TLS(unsigned int, alloc_pool_size);
42QEMU_DEFINE_STATIC_CO_TLS(Notifier, coroutine_pool_cleanup_notifier);
4d68e86b
PB
43
44static void coroutine_pool_cleanup(Notifier *n, void *value)
45{
46 Coroutine *co;
47 Coroutine *tmp;
ac387a08 48 CoroutineQSList *alloc_pool = get_ptr_alloc_pool();
4d68e86b 49
ac387a08
SH
50 QSLIST_FOREACH_SAFE(co, alloc_pool, pool_next, tmp) {
51 QSLIST_REMOVE_HEAD(alloc_pool, pool_next);
4d68e86b
PB
52 qemu_coroutine_delete(co);
53 }
54}
40239784 55
0b8b8753 56Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque)
00dccaf1 57{
70c60c08 58 Coroutine *co = NULL;
40239784 59
230f6e06 60 if (IS_ENABLED(CONFIG_COROUTINE_POOL)) {
ac387a08
SH
61 CoroutineQSList *alloc_pool = get_ptr_alloc_pool();
62
63 co = QSLIST_FIRST(alloc_pool);
4d68e86b 64 if (!co) {
9ec7a59b 65 if (release_pool_size > POOL_MIN_BATCH_SIZE) {
4d68e86b 66 /* Slow path; a good place to register the destructor, too. */
ac387a08
SH
67 Notifier *notifier = get_ptr_coroutine_pool_cleanup_notifier();
68 if (!notifier->notify) {
69 notifier->notify = coroutine_pool_cleanup;
70 qemu_thread_atexit_add(notifier);
4d68e86b
PB
71 }
72
73 /* This is not exact; there could be a little skew between
74 * release_pool_size and the actual size of release_pool. But
75 * it is just a heuristic, it does not need to be perfect.
76 */
ac387a08
SH
77 set_alloc_pool_size(qatomic_xchg(&release_pool_size, 0));
78 QSLIST_MOVE_ATOMIC(alloc_pool, &release_pool);
79 co = QSLIST_FIRST(alloc_pool);
4d68e86b
PB
80 }
81 }
70c60c08 82 if (co) {
ac387a08
SH
83 QSLIST_REMOVE_HEAD(alloc_pool, pool_next);
84 set_alloc_pool_size(get_alloc_pool_size() - 1);
70c60c08 85 }
b84c4586 86 }
b84c4586
SH
87
88 if (!co) {
40239784
PB
89 co = qemu_coroutine_new();
90 }
91
00dccaf1 92 co->entry = entry;
0b8b8753 93 co->entry_arg = opaque;
7d9c8581 94 QSIMPLEQ_INIT(&co->co_queue_wakeup);
00dccaf1
KW
95 return co;
96}
97
40239784
PB
98static void coroutine_delete(Coroutine *co)
99{
4d68e86b
PB
100 co->caller = NULL;
101
230f6e06 102 if (IS_ENABLED(CONFIG_COROUTINE_POOL)) {
9ec7a59b 103 if (release_pool_size < qatomic_read(&pool_max_size) * 2) {
4d68e86b 104 QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
d73415a3 105 qatomic_inc(&release_pool_size);
70c60c08
SH
106 return;
107 }
9ec7a59b 108 if (get_alloc_pool_size() < qatomic_read(&pool_max_size)) {
ac387a08
SH
109 QSLIST_INSERT_HEAD(get_ptr_alloc_pool(), co, pool_next);
110 set_alloc_pool_size(get_alloc_pool_size() + 1);
51a2219b
PL
111 return;
112 }
40239784
PB
113 }
114
115 qemu_coroutine_delete(co);
116}
117
ba9e75ce 118void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)
00dccaf1 119{
c40a2545
SH
120 QSIMPLEQ_HEAD(, Coroutine) pending = QSIMPLEQ_HEAD_INITIALIZER(pending);
121 Coroutine *from = qemu_coroutine_self();
6133b39f 122
c40a2545 123 QSIMPLEQ_INSERT_TAIL(&pending, co, co_queue_next);
00dccaf1 124
c40a2545
SH
125 /* Run co and any queued coroutines */
126 while (!QSIMPLEQ_EMPTY(&pending)) {
127 Coroutine *to = QSIMPLEQ_FIRST(&pending);
128 CoroutineAction ret;
6133b39f 129
512c90c9
PB
130 /*
131 * Read to before to->scheduled; pairs with qatomic_cmpxchg in
132 * qemu_co_sleep(), aio_co_schedule() etc.
133 */
134 smp_read_barrier_depends();
135
136 const char *scheduled = qatomic_read(&to->scheduled);
00dccaf1 137
c40a2545 138 QSIMPLEQ_REMOVE_HEAD(&pending, co_queue_next);
0c330a73 139
c40a2545 140 trace_qemu_aio_coroutine_enter(ctx, from, to, to->entry_arg);
0c330a73 141
c40a2545
SH
142 /* if the Coroutine has already been scheduled, entering it again will
143 * cause us to enter it twice, potentially even after the coroutine has
144 * been deleted */
145 if (scheduled) {
146 fprintf(stderr,
147 "%s: Co-routine was already scheduled in '%s'\n",
148 __func__, scheduled);
149 abort();
150 }
cd12bb56 151
c40a2545
SH
152 if (to->caller) {
153 fprintf(stderr, "Co-routine re-entered recursively\n");
154 abort();
155 }
528f449f 156
c40a2545
SH
157 to->caller = from;
158 to->ctx = ctx;
159
160 /* Store to->ctx before anything that stores to. Matches
161 * barrier in aio_co_wake and qemu_co_mutex_wake.
162 */
163 smp_wmb();
164
165 ret = qemu_coroutine_switch(from, to, COROUTINE_ENTER);
166
167 /* Queued coroutines are run depth-first; previously pending coroutines
168 * run after those queued more recently.
169 */
170 QSIMPLEQ_PREPEND(&pending, &to->co_queue_wakeup);
171
172 switch (ret) {
173 case COROUTINE_YIELD:
174 break;
175 case COROUTINE_TERMINATE:
176 assert(!to->locks_held);
177 trace_qemu_coroutine_terminate(to);
178 coroutine_delete(to);
179 break;
180 default:
181 abort();
182 }
cd12bb56 183 }
00dccaf1
KW
184}
185
ba9e75ce
FZ
186void qemu_coroutine_enter(Coroutine *co)
187{
188 qemu_aio_coroutine_enter(qemu_get_current_aio_context(), co);
189}
190
536fca7f
KW
191void qemu_coroutine_enter_if_inactive(Coroutine *co)
192{
193 if (!qemu_coroutine_entered(co)) {
194 qemu_coroutine_enter(co);
195 }
196}
197
00dccaf1
KW
198void coroutine_fn qemu_coroutine_yield(void)
199{
200 Coroutine *self = qemu_coroutine_self();
201 Coroutine *to = self->caller;
202
203 trace_qemu_coroutine_yield(self, to);
204
205 if (!to) {
206 fprintf(stderr, "Co-routine is yielding to no one\n");
207 abort();
208 }
209
210 self->caller = NULL;
315a1309 211 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
00dccaf1 212}
f643e469
SH
213
214bool qemu_coroutine_entered(Coroutine *co)
215{
216 return co->caller;
217}
aa1361d5 218
a248b856 219AioContext *qemu_coroutine_get_aio_context(Coroutine *co)
aa1361d5
KW
220{
221 return co->ctx;
222}
4c41c69e 223
98e3ab35 224void qemu_coroutine_inc_pool_size(unsigned int additional_pool_size)
4c41c69e 225{
9ec7a59b 226 qatomic_add(&pool_max_size, additional_pool_size);
4c41c69e
HN
227}
228
98e3ab35 229void qemu_coroutine_dec_pool_size(unsigned int removing_pool_size)
4c41c69e 230{
9ec7a59b 231 qatomic_sub(&pool_max_size, removing_pool_size);
4c41c69e 232}