test-aio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * AioContext tests
  3. *
  4. * Copyright Red Hat, Inc. 2012
  5. *
  6. * Authors:
  7. * Paolo Bonzini <pbonzini@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "block/aio.h"
  14. #include "qapi/error.h"
  15. #include "qemu/timer.h"
  16. #include "qemu/sockets.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/coroutine.h"
  19. #include "qemu/main-loop.h"
  20. static AioContext *ctx;
  21. typedef struct {
  22. EventNotifier e;
  23. int n;
  24. int active;
  25. bool auto_set;
  26. } EventNotifierTestData;
  27. /* Wait until event notifier becomes inactive */
  28. static void wait_until_inactive(EventNotifierTestData *data)
  29. {
  30. while (data->active > 0) {
  31. aio_poll(ctx, true);
  32. }
  33. }
  34. /* Simple callbacks for testing. */
  35. typedef struct {
  36. QEMUBH *bh;
  37. int n;
  38. int max;
  39. } BHTestData;
  40. typedef struct {
  41. QEMUTimer timer;
  42. QEMUClockType clock_type;
  43. int n;
  44. int max;
  45. int64_t ns;
  46. AioContext *ctx;
  47. } TimerTestData;
  48. static void bh_test_cb(void *opaque)
  49. {
  50. BHTestData *data = opaque;
  51. if (++data->n < data->max) {
  52. qemu_bh_schedule(data->bh);
  53. }
  54. }
  55. static void timer_test_cb(void *opaque)
  56. {
  57. TimerTestData *data = opaque;
  58. if (++data->n < data->max) {
  59. timer_mod(&data->timer,
  60. qemu_clock_get_ns(data->clock_type) + data->ns);
  61. }
  62. }
  63. static void dummy_io_handler_read(EventNotifier *e)
  64. {
  65. }
  66. static void bh_delete_cb(void *opaque)
  67. {
  68. BHTestData *data = opaque;
  69. if (++data->n < data->max) {
  70. qemu_bh_schedule(data->bh);
  71. } else {
  72. qemu_bh_delete(data->bh);
  73. data->bh = NULL;
  74. }
  75. }
  76. static void event_ready_cb(EventNotifier *e)
  77. {
  78. EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
  79. g_assert(event_notifier_test_and_clear(e));
  80. data->n++;
  81. if (data->active > 0) {
  82. data->active--;
  83. }
  84. if (data->auto_set && data->active) {
  85. event_notifier_set(e);
  86. }
  87. }
  88. /* Tests using aio_*. */
  89. typedef struct {
  90. QemuMutex start_lock;
  91. EventNotifier notifier;
  92. bool thread_acquired;
  93. } AcquireTestData;
  94. static void *test_acquire_thread(void *opaque)
  95. {
  96. AcquireTestData *data = opaque;
  97. /* Wait for other thread to let us start */
  98. qemu_mutex_lock(&data->start_lock);
  99. qemu_mutex_unlock(&data->start_lock);
  100. /* event_notifier_set might be called either before or after
  101. * the main thread's call to poll(). The test case's outcome
  102. * should be the same in either case.
  103. */
  104. event_notifier_set(&data->notifier);
  105. aio_context_acquire(ctx);
  106. aio_context_release(ctx);
  107. data->thread_acquired = true; /* success, we got here */
  108. return NULL;
  109. }
  110. static void set_event_notifier(AioContext *ctx, EventNotifier *notifier,
  111. EventNotifierHandler *handler)
  112. {
  113. aio_set_event_notifier(ctx, notifier, false, handler, NULL);
  114. }
  115. static void dummy_notifier_read(EventNotifier *n)
  116. {
  117. event_notifier_test_and_clear(n);
  118. }
  119. static void test_acquire(void)
  120. {
  121. QemuThread thread;
  122. AcquireTestData data;
  123. /* Dummy event notifier ensures aio_poll() will block */
  124. event_notifier_init(&data.notifier, false);
  125. set_event_notifier(ctx, &data.notifier, dummy_notifier_read);
  126. g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
  127. qemu_mutex_init(&data.start_lock);
  128. qemu_mutex_lock(&data.start_lock);
  129. data.thread_acquired = false;
  130. qemu_thread_create(&thread, "test_acquire_thread",
  131. test_acquire_thread,
  132. &data, QEMU_THREAD_JOINABLE);
  133. /* Block in aio_poll(), let other thread kick us and acquire context */
  134. aio_context_acquire(ctx);
  135. qemu_mutex_unlock(&data.start_lock); /* let the thread run */
  136. g_assert(aio_poll(ctx, true));
  137. g_assert(!data.thread_acquired);
  138. aio_context_release(ctx);
  139. qemu_thread_join(&thread);
  140. set_event_notifier(ctx, &data.notifier, NULL);
  141. event_notifier_cleanup(&data.notifier);
  142. g_assert(data.thread_acquired);
  143. }
  144. static void test_bh_schedule(void)
  145. {
  146. BHTestData data = { .n = 0 };
  147. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  148. qemu_bh_schedule(data.bh);
  149. g_assert_cmpint(data.n, ==, 0);
  150. g_assert(aio_poll(ctx, true));
  151. g_assert_cmpint(data.n, ==, 1);
  152. g_assert(!aio_poll(ctx, false));
  153. g_assert_cmpint(data.n, ==, 1);
  154. qemu_bh_delete(data.bh);
  155. }
  156. static void test_bh_schedule10(void)
  157. {
  158. BHTestData data = { .n = 0, .max = 10 };
  159. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  160. qemu_bh_schedule(data.bh);
  161. g_assert_cmpint(data.n, ==, 0);
  162. g_assert(aio_poll(ctx, false));
  163. g_assert_cmpint(data.n, ==, 1);
  164. g_assert(aio_poll(ctx, true));
  165. g_assert_cmpint(data.n, ==, 2);
  166. while (data.n < 10) {
  167. aio_poll(ctx, true);
  168. }
  169. g_assert_cmpint(data.n, ==, 10);
  170. g_assert(!aio_poll(ctx, false));
  171. g_assert_cmpint(data.n, ==, 10);
  172. qemu_bh_delete(data.bh);
  173. }
  174. static void test_bh_cancel(void)
  175. {
  176. BHTestData data = { .n = 0 };
  177. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  178. qemu_bh_schedule(data.bh);
  179. g_assert_cmpint(data.n, ==, 0);
  180. qemu_bh_cancel(data.bh);
  181. g_assert_cmpint(data.n, ==, 0);
  182. g_assert(!aio_poll(ctx, false));
  183. g_assert_cmpint(data.n, ==, 0);
  184. qemu_bh_delete(data.bh);
  185. }
  186. static void test_bh_delete(void)
  187. {
  188. BHTestData data = { .n = 0 };
  189. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  190. qemu_bh_schedule(data.bh);
  191. g_assert_cmpint(data.n, ==, 0);
  192. qemu_bh_delete(data.bh);
  193. g_assert_cmpint(data.n, ==, 0);
  194. g_assert(!aio_poll(ctx, false));
  195. g_assert_cmpint(data.n, ==, 0);
  196. }
  197. static void test_bh_delete_from_cb(void)
  198. {
  199. BHTestData data1 = { .n = 0, .max = 1 };
  200. data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
  201. qemu_bh_schedule(data1.bh);
  202. g_assert_cmpint(data1.n, ==, 0);
  203. while (data1.n < data1.max) {
  204. aio_poll(ctx, true);
  205. }
  206. g_assert_cmpint(data1.n, ==, data1.max);
  207. g_assert(data1.bh == NULL);
  208. g_assert(!aio_poll(ctx, false));
  209. }
  210. static void test_bh_delete_from_cb_many(void)
  211. {
  212. BHTestData data1 = { .n = 0, .max = 1 };
  213. BHTestData data2 = { .n = 0, .max = 3 };
  214. BHTestData data3 = { .n = 0, .max = 2 };
  215. BHTestData data4 = { .n = 0, .max = 4 };
  216. data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
  217. data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
  218. data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
  219. data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
  220. qemu_bh_schedule(data1.bh);
  221. qemu_bh_schedule(data2.bh);
  222. qemu_bh_schedule(data3.bh);
  223. qemu_bh_schedule(data4.bh);
  224. g_assert_cmpint(data1.n, ==, 0);
  225. g_assert_cmpint(data2.n, ==, 0);
  226. g_assert_cmpint(data3.n, ==, 0);
  227. g_assert_cmpint(data4.n, ==, 0);
  228. g_assert(aio_poll(ctx, false));
  229. g_assert_cmpint(data1.n, ==, 1);
  230. g_assert_cmpint(data2.n, ==, 1);
  231. g_assert_cmpint(data3.n, ==, 1);
  232. g_assert_cmpint(data4.n, ==, 1);
  233. g_assert(data1.bh == NULL);
  234. while (data1.n < data1.max ||
  235. data2.n < data2.max ||
  236. data3.n < data3.max ||
  237. data4.n < data4.max) {
  238. aio_poll(ctx, true);
  239. }
  240. g_assert_cmpint(data1.n, ==, data1.max);
  241. g_assert_cmpint(data2.n, ==, data2.max);
  242. g_assert_cmpint(data3.n, ==, data3.max);
  243. g_assert_cmpint(data4.n, ==, data4.max);
  244. g_assert(data1.bh == NULL);
  245. g_assert(data2.bh == NULL);
  246. g_assert(data3.bh == NULL);
  247. g_assert(data4.bh == NULL);
  248. }
  249. static void test_bh_flush(void)
  250. {
  251. BHTestData data = { .n = 0 };
  252. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  253. qemu_bh_schedule(data.bh);
  254. g_assert_cmpint(data.n, ==, 0);
  255. g_assert(aio_poll(ctx, true));
  256. g_assert_cmpint(data.n, ==, 1);
  257. g_assert(!aio_poll(ctx, false));
  258. g_assert_cmpint(data.n, ==, 1);
  259. qemu_bh_delete(data.bh);
  260. }
  261. static void test_set_event_notifier(void)
  262. {
  263. EventNotifierTestData data = { .n = 0, .active = 0 };
  264. event_notifier_init(&data.e, false);
  265. set_event_notifier(ctx, &data.e, event_ready_cb);
  266. g_assert(!aio_poll(ctx, false));
  267. g_assert_cmpint(data.n, ==, 0);
  268. set_event_notifier(ctx, &data.e, NULL);
  269. g_assert(!aio_poll(ctx, false));
  270. g_assert_cmpint(data.n, ==, 0);
  271. event_notifier_cleanup(&data.e);
  272. }
  273. static void test_wait_event_notifier(void)
  274. {
  275. EventNotifierTestData data = { .n = 0, .active = 1 };
  276. event_notifier_init(&data.e, false);
  277. set_event_notifier(ctx, &data.e, event_ready_cb);
  278. while (aio_poll(ctx, false));
  279. g_assert_cmpint(data.n, ==, 0);
  280. g_assert_cmpint(data.active, ==, 1);
  281. event_notifier_set(&data.e);
  282. g_assert(aio_poll(ctx, false));
  283. g_assert_cmpint(data.n, ==, 1);
  284. g_assert_cmpint(data.active, ==, 0);
  285. g_assert(!aio_poll(ctx, false));
  286. g_assert_cmpint(data.n, ==, 1);
  287. g_assert_cmpint(data.active, ==, 0);
  288. set_event_notifier(ctx, &data.e, NULL);
  289. g_assert(!aio_poll(ctx, false));
  290. g_assert_cmpint(data.n, ==, 1);
  291. event_notifier_cleanup(&data.e);
  292. }
  293. static void test_flush_event_notifier(void)
  294. {
  295. EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
  296. event_notifier_init(&data.e, false);
  297. set_event_notifier(ctx, &data.e, event_ready_cb);
  298. while (aio_poll(ctx, false));
  299. g_assert_cmpint(data.n, ==, 0);
  300. g_assert_cmpint(data.active, ==, 10);
  301. event_notifier_set(&data.e);
  302. g_assert(aio_poll(ctx, false));
  303. g_assert_cmpint(data.n, ==, 1);
  304. g_assert_cmpint(data.active, ==, 9);
  305. g_assert(aio_poll(ctx, false));
  306. wait_until_inactive(&data);
  307. g_assert_cmpint(data.n, ==, 10);
  308. g_assert_cmpint(data.active, ==, 0);
  309. g_assert(!aio_poll(ctx, false));
  310. set_event_notifier(ctx, &data.e, NULL);
  311. g_assert(!aio_poll(ctx, false));
  312. event_notifier_cleanup(&data.e);
  313. }
  314. static void test_aio_external_client(void)
  315. {
  316. int i, j;
  317. for (i = 1; i < 3; i++) {
  318. EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
  319. event_notifier_init(&data.e, false);
  320. aio_set_event_notifier(ctx, &data.e, true, event_ready_cb, NULL);
  321. event_notifier_set(&data.e);
  322. for (j = 0; j < i; j++) {
  323. aio_disable_external(ctx);
  324. }
  325. for (j = 0; j < i; j++) {
  326. assert(!aio_poll(ctx, false));
  327. assert(event_notifier_test_and_clear(&data.e));
  328. event_notifier_set(&data.e);
  329. aio_enable_external(ctx);
  330. }
  331. assert(aio_poll(ctx, false));
  332. set_event_notifier(ctx, &data.e, NULL);
  333. event_notifier_cleanup(&data.e);
  334. }
  335. }
  336. static void test_wait_event_notifier_noflush(void)
  337. {
  338. EventNotifierTestData data = { .n = 0 };
  339. EventNotifierTestData dummy = { .n = 0, .active = 1 };
  340. event_notifier_init(&data.e, false);
  341. set_event_notifier(ctx, &data.e, event_ready_cb);
  342. g_assert(!aio_poll(ctx, false));
  343. g_assert_cmpint(data.n, ==, 0);
  344. /* Until there is an active descriptor, aio_poll may or may not call
  345. * event_ready_cb. Still, it must not block. */
  346. event_notifier_set(&data.e);
  347. g_assert(aio_poll(ctx, true));
  348. data.n = 0;
  349. /* An active event notifier forces aio_poll to look at EventNotifiers. */
  350. event_notifier_init(&dummy.e, false);
  351. set_event_notifier(ctx, &dummy.e, event_ready_cb);
  352. event_notifier_set(&data.e);
  353. g_assert(aio_poll(ctx, false));
  354. g_assert_cmpint(data.n, ==, 1);
  355. g_assert(!aio_poll(ctx, false));
  356. g_assert_cmpint(data.n, ==, 1);
  357. event_notifier_set(&data.e);
  358. g_assert(aio_poll(ctx, false));
  359. g_assert_cmpint(data.n, ==, 2);
  360. g_assert(!aio_poll(ctx, false));
  361. g_assert_cmpint(data.n, ==, 2);
  362. event_notifier_set(&dummy.e);
  363. wait_until_inactive(&dummy);
  364. g_assert_cmpint(data.n, ==, 2);
  365. g_assert_cmpint(dummy.n, ==, 1);
  366. g_assert_cmpint(dummy.active, ==, 0);
  367. set_event_notifier(ctx, &dummy.e, NULL);
  368. event_notifier_cleanup(&dummy.e);
  369. set_event_notifier(ctx, &data.e, NULL);
  370. g_assert(!aio_poll(ctx, false));
  371. g_assert_cmpint(data.n, ==, 2);
  372. event_notifier_cleanup(&data.e);
  373. }
  374. static void test_timer_schedule(void)
  375. {
  376. TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
  377. .max = 2,
  378. .clock_type = QEMU_CLOCK_REALTIME };
  379. EventNotifier e;
  380. /* aio_poll will not block to wait for timers to complete unless it has
  381. * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
  382. */
  383. event_notifier_init(&e, false);
  384. set_event_notifier(ctx, &e, dummy_io_handler_read);
  385. aio_poll(ctx, false);
  386. aio_timer_init(ctx, &data.timer, data.clock_type,
  387. SCALE_NS, timer_test_cb, &data);
  388. timer_mod(&data.timer,
  389. qemu_clock_get_ns(data.clock_type) +
  390. data.ns);
  391. g_assert_cmpint(data.n, ==, 0);
  392. /* timer_mod may well cause an event notifer to have gone off,
  393. * so clear that
  394. */
  395. do {} while (aio_poll(ctx, false));
  396. g_assert(!aio_poll(ctx, false));
  397. g_assert_cmpint(data.n, ==, 0);
  398. g_usleep(1 * G_USEC_PER_SEC);
  399. g_assert_cmpint(data.n, ==, 0);
  400. g_assert(aio_poll(ctx, false));
  401. g_assert_cmpint(data.n, ==, 1);
  402. /* timer_mod called by our callback */
  403. do {} while (aio_poll(ctx, false));
  404. g_assert(!aio_poll(ctx, false));
  405. g_assert_cmpint(data.n, ==, 1);
  406. g_assert(aio_poll(ctx, true));
  407. g_assert_cmpint(data.n, ==, 2);
  408. /* As max is now 2, an event notifier should not have gone off */
  409. g_assert(!aio_poll(ctx, false));
  410. g_assert_cmpint(data.n, ==, 2);
  411. set_event_notifier(ctx, &e, NULL);
  412. event_notifier_cleanup(&e);
  413. timer_del(&data.timer);
  414. }
  415. /* Now the same tests, using the context as a GSource. They are
  416. * very similar to the ones above, with g_main_context_iteration
  417. * replacing aio_poll. However:
  418. * - sometimes both the AioContext and the glib main loop wake
  419. * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
  420. * are replaced by "while (g_main_context_iteration(NULL, false));".
  421. * - there is no exact replacement for a blocking wait.
  422. * "while (g_main_context_iteration(NULL, true)" seems to work,
  423. * but it is not documented _why_ it works. For these tests a
  424. * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
  425. * works well, and that's what I am using.
  426. */
  427. static void test_source_flush(void)
  428. {
  429. g_assert(!g_main_context_iteration(NULL, false));
  430. aio_notify(ctx);
  431. while (g_main_context_iteration(NULL, false));
  432. g_assert(!g_main_context_iteration(NULL, false));
  433. }
  434. static void test_source_bh_schedule(void)
  435. {
  436. BHTestData data = { .n = 0 };
  437. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  438. qemu_bh_schedule(data.bh);
  439. g_assert_cmpint(data.n, ==, 0);
  440. g_assert(g_main_context_iteration(NULL, true));
  441. g_assert_cmpint(data.n, ==, 1);
  442. g_assert(!g_main_context_iteration(NULL, false));
  443. g_assert_cmpint(data.n, ==, 1);
  444. qemu_bh_delete(data.bh);
  445. }
  446. static void test_source_bh_schedule10(void)
  447. {
  448. BHTestData data = { .n = 0, .max = 10 };
  449. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  450. qemu_bh_schedule(data.bh);
  451. g_assert_cmpint(data.n, ==, 0);
  452. g_assert(g_main_context_iteration(NULL, false));
  453. g_assert_cmpint(data.n, ==, 1);
  454. g_assert(g_main_context_iteration(NULL, true));
  455. g_assert_cmpint(data.n, ==, 2);
  456. while (g_main_context_iteration(NULL, false));
  457. g_assert_cmpint(data.n, ==, 10);
  458. g_assert(!g_main_context_iteration(NULL, false));
  459. g_assert_cmpint(data.n, ==, 10);
  460. qemu_bh_delete(data.bh);
  461. }
  462. static void test_source_bh_cancel(void)
  463. {
  464. BHTestData data = { .n = 0 };
  465. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  466. qemu_bh_schedule(data.bh);
  467. g_assert_cmpint(data.n, ==, 0);
  468. qemu_bh_cancel(data.bh);
  469. g_assert_cmpint(data.n, ==, 0);
  470. while (g_main_context_iteration(NULL, false));
  471. g_assert_cmpint(data.n, ==, 0);
  472. qemu_bh_delete(data.bh);
  473. }
  474. static void test_source_bh_delete(void)
  475. {
  476. BHTestData data = { .n = 0 };
  477. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  478. qemu_bh_schedule(data.bh);
  479. g_assert_cmpint(data.n, ==, 0);
  480. qemu_bh_delete(data.bh);
  481. g_assert_cmpint(data.n, ==, 0);
  482. while (g_main_context_iteration(NULL, false));
  483. g_assert_cmpint(data.n, ==, 0);
  484. }
  485. static void test_source_bh_delete_from_cb(void)
  486. {
  487. BHTestData data1 = { .n = 0, .max = 1 };
  488. data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
  489. qemu_bh_schedule(data1.bh);
  490. g_assert_cmpint(data1.n, ==, 0);
  491. g_main_context_iteration(NULL, true);
  492. g_assert_cmpint(data1.n, ==, data1.max);
  493. g_assert(data1.bh == NULL);
  494. g_assert(!g_main_context_iteration(NULL, false));
  495. }
  496. static void test_source_bh_delete_from_cb_many(void)
  497. {
  498. BHTestData data1 = { .n = 0, .max = 1 };
  499. BHTestData data2 = { .n = 0, .max = 3 };
  500. BHTestData data3 = { .n = 0, .max = 2 };
  501. BHTestData data4 = { .n = 0, .max = 4 };
  502. data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
  503. data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
  504. data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
  505. data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
  506. qemu_bh_schedule(data1.bh);
  507. qemu_bh_schedule(data2.bh);
  508. qemu_bh_schedule(data3.bh);
  509. qemu_bh_schedule(data4.bh);
  510. g_assert_cmpint(data1.n, ==, 0);
  511. g_assert_cmpint(data2.n, ==, 0);
  512. g_assert_cmpint(data3.n, ==, 0);
  513. g_assert_cmpint(data4.n, ==, 0);
  514. g_assert(g_main_context_iteration(NULL, false));
  515. g_assert_cmpint(data1.n, ==, 1);
  516. g_assert_cmpint(data2.n, ==, 1);
  517. g_assert_cmpint(data3.n, ==, 1);
  518. g_assert_cmpint(data4.n, ==, 1);
  519. g_assert(data1.bh == NULL);
  520. while (g_main_context_iteration(NULL, false));
  521. g_assert_cmpint(data1.n, ==, data1.max);
  522. g_assert_cmpint(data2.n, ==, data2.max);
  523. g_assert_cmpint(data3.n, ==, data3.max);
  524. g_assert_cmpint(data4.n, ==, data4.max);
  525. g_assert(data1.bh == NULL);
  526. g_assert(data2.bh == NULL);
  527. g_assert(data3.bh == NULL);
  528. g_assert(data4.bh == NULL);
  529. }
  530. static void test_source_bh_flush(void)
  531. {
  532. BHTestData data = { .n = 0 };
  533. data.bh = aio_bh_new(ctx, bh_test_cb, &data);
  534. qemu_bh_schedule(data.bh);
  535. g_assert_cmpint(data.n, ==, 0);
  536. g_assert(g_main_context_iteration(NULL, true));
  537. g_assert_cmpint(data.n, ==, 1);
  538. g_assert(!g_main_context_iteration(NULL, false));
  539. g_assert_cmpint(data.n, ==, 1);
  540. qemu_bh_delete(data.bh);
  541. }
  542. static void test_source_set_event_notifier(void)
  543. {
  544. EventNotifierTestData data = { .n = 0, .active = 0 };
  545. event_notifier_init(&data.e, false);
  546. set_event_notifier(ctx, &data.e, event_ready_cb);
  547. while (g_main_context_iteration(NULL, false));
  548. g_assert_cmpint(data.n, ==, 0);
  549. set_event_notifier(ctx, &data.e, NULL);
  550. while (g_main_context_iteration(NULL, false));
  551. g_assert_cmpint(data.n, ==, 0);
  552. event_notifier_cleanup(&data.e);
  553. }
  554. static void test_source_wait_event_notifier(void)
  555. {
  556. EventNotifierTestData data = { .n = 0, .active = 1 };
  557. event_notifier_init(&data.e, false);
  558. set_event_notifier(ctx, &data.e, event_ready_cb);
  559. while (g_main_context_iteration(NULL, false));
  560. g_assert_cmpint(data.n, ==, 0);
  561. g_assert_cmpint(data.active, ==, 1);
  562. event_notifier_set(&data.e);
  563. g_assert(g_main_context_iteration(NULL, false));
  564. g_assert_cmpint(data.n, ==, 1);
  565. g_assert_cmpint(data.active, ==, 0);
  566. while (g_main_context_iteration(NULL, false));
  567. g_assert_cmpint(data.n, ==, 1);
  568. g_assert_cmpint(data.active, ==, 0);
  569. set_event_notifier(ctx, &data.e, NULL);
  570. while (g_main_context_iteration(NULL, false));
  571. g_assert_cmpint(data.n, ==, 1);
  572. event_notifier_cleanup(&data.e);
  573. }
  574. static void test_source_flush_event_notifier(void)
  575. {
  576. EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
  577. event_notifier_init(&data.e, false);
  578. set_event_notifier(ctx, &data.e, event_ready_cb);
  579. while (g_main_context_iteration(NULL, false));
  580. g_assert_cmpint(data.n, ==, 0);
  581. g_assert_cmpint(data.active, ==, 10);
  582. event_notifier_set(&data.e);
  583. g_assert(g_main_context_iteration(NULL, false));
  584. g_assert_cmpint(data.n, ==, 1);
  585. g_assert_cmpint(data.active, ==, 9);
  586. g_assert(g_main_context_iteration(NULL, false));
  587. while (g_main_context_iteration(NULL, false));
  588. g_assert_cmpint(data.n, ==, 10);
  589. g_assert_cmpint(data.active, ==, 0);
  590. g_assert(!g_main_context_iteration(NULL, false));
  591. set_event_notifier(ctx, &data.e, NULL);
  592. while (g_main_context_iteration(NULL, false));
  593. event_notifier_cleanup(&data.e);
  594. }
  595. static void test_source_wait_event_notifier_noflush(void)
  596. {
  597. EventNotifierTestData data = { .n = 0 };
  598. EventNotifierTestData dummy = { .n = 0, .active = 1 };
  599. event_notifier_init(&data.e, false);
  600. set_event_notifier(ctx, &data.e, event_ready_cb);
  601. while (g_main_context_iteration(NULL, false));
  602. g_assert_cmpint(data.n, ==, 0);
  603. /* Until there is an active descriptor, glib may or may not call
  604. * event_ready_cb. Still, it must not block. */
  605. event_notifier_set(&data.e);
  606. g_main_context_iteration(NULL, true);
  607. data.n = 0;
  608. /* An active event notifier forces aio_poll to look at EventNotifiers. */
  609. event_notifier_init(&dummy.e, false);
  610. set_event_notifier(ctx, &dummy.e, event_ready_cb);
  611. event_notifier_set(&data.e);
  612. g_assert(g_main_context_iteration(NULL, false));
  613. g_assert_cmpint(data.n, ==, 1);
  614. g_assert(!g_main_context_iteration(NULL, false));
  615. g_assert_cmpint(data.n, ==, 1);
  616. event_notifier_set(&data.e);
  617. g_assert(g_main_context_iteration(NULL, false));
  618. g_assert_cmpint(data.n, ==, 2);
  619. g_assert(!g_main_context_iteration(NULL, false));
  620. g_assert_cmpint(data.n, ==, 2);
  621. event_notifier_set(&dummy.e);
  622. while (g_main_context_iteration(NULL, false));
  623. g_assert_cmpint(data.n, ==, 2);
  624. g_assert_cmpint(dummy.n, ==, 1);
  625. g_assert_cmpint(dummy.active, ==, 0);
  626. set_event_notifier(ctx, &dummy.e, NULL);
  627. event_notifier_cleanup(&dummy.e);
  628. set_event_notifier(ctx, &data.e, NULL);
  629. while (g_main_context_iteration(NULL, false));
  630. g_assert_cmpint(data.n, ==, 2);
  631. event_notifier_cleanup(&data.e);
  632. }
  633. static void test_source_timer_schedule(void)
  634. {
  635. TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
  636. .max = 2,
  637. .clock_type = QEMU_CLOCK_REALTIME };
  638. EventNotifier e;
  639. int64_t expiry;
  640. /* aio_poll will not block to wait for timers to complete unless it has
  641. * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
  642. */
  643. event_notifier_init(&e, false);
  644. set_event_notifier(ctx, &e, dummy_io_handler_read);
  645. do {} while (g_main_context_iteration(NULL, false));
  646. aio_timer_init(ctx, &data.timer, data.clock_type,
  647. SCALE_NS, timer_test_cb, &data);
  648. expiry = qemu_clock_get_ns(data.clock_type) +
  649. data.ns;
  650. timer_mod(&data.timer, expiry);
  651. g_assert_cmpint(data.n, ==, 0);
  652. g_usleep(1 * G_USEC_PER_SEC);
  653. g_assert_cmpint(data.n, ==, 0);
  654. g_assert(g_main_context_iteration(NULL, true));
  655. g_assert_cmpint(data.n, ==, 1);
  656. expiry += data.ns;
  657. while (data.n < 2) {
  658. g_main_context_iteration(NULL, true);
  659. }
  660. g_assert_cmpint(data.n, ==, 2);
  661. g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
  662. set_event_notifier(ctx, &e, NULL);
  663. event_notifier_cleanup(&e);
  664. timer_del(&data.timer);
  665. }
  666. /*
  667. * Check that aio_co_enter() can chain many times
  668. *
  669. * Two coroutines should be able to invoke each other via aio_co_enter() many
  670. * times without hitting a limit like stack exhaustion. In other words, the
  671. * calls should be chained instead of nested.
  672. */
  673. typedef struct {
  674. Coroutine *other;
  675. unsigned i;
  676. unsigned max;
  677. } ChainData;
  678. static void coroutine_fn chain(void *opaque)
  679. {
  680. ChainData *data = opaque;
  681. for (data->i = 0; data->i < data->max; data->i++) {
  682. /* Queue up the other coroutine... */
  683. aio_co_enter(ctx, data->other);
  684. /* ...and give control to it */
  685. qemu_coroutine_yield();
  686. }
  687. }
  688. static void test_queue_chaining(void)
  689. {
  690. /* This number of iterations hit stack exhaustion in the past: */
  691. ChainData data_a = { .max = 25000 };
  692. ChainData data_b = { .max = 25000 };
  693. data_b.other = qemu_coroutine_create(chain, &data_a);
  694. data_a.other = qemu_coroutine_create(chain, &data_b);
  695. qemu_coroutine_enter(data_b.other);
  696. g_assert_cmpint(data_a.i, ==, data_a.max);
  697. g_assert_cmpint(data_b.i, ==, data_b.max - 1);
  698. /* Allow the second coroutine to terminate */
  699. qemu_coroutine_enter(data_a.other);
  700. g_assert_cmpint(data_b.i, ==, data_b.max);
  701. }
  702. /* End of tests. */
  703. int main(int argc, char **argv)
  704. {
  705. qemu_init_main_loop(&error_fatal);
  706. ctx = qemu_get_aio_context();
  707. while (g_main_context_iteration(NULL, false));
  708. g_test_init(&argc, &argv, NULL);
  709. g_test_add_func("/aio/acquire", test_acquire);
  710. g_test_add_func("/aio/bh/schedule", test_bh_schedule);
  711. g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
  712. g_test_add_func("/aio/bh/cancel", test_bh_cancel);
  713. g_test_add_func("/aio/bh/delete", test_bh_delete);
  714. g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
  715. g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
  716. g_test_add_func("/aio/bh/flush", test_bh_flush);
  717. g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
  718. g_test_add_func("/aio/event/wait", test_wait_event_notifier);
  719. g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
  720. g_test_add_func("/aio/event/flush", test_flush_event_notifier);
  721. g_test_add_func("/aio/external-client", test_aio_external_client);
  722. g_test_add_func("/aio/timer/schedule", test_timer_schedule);
  723. g_test_add_func("/aio/coroutine/queue-chaining", test_queue_chaining);
  724. g_test_add_func("/aio-gsource/flush", test_source_flush);
  725. g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
  726. g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
  727. g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
  728. g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
  729. g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
  730. g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
  731. g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
  732. g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
  733. g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
  734. g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
  735. g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
  736. g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
  737. return g_test_run();
  738. }