test-coroutine.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Coroutine tests
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.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. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/coroutine.h"
  15. #include "qemu/coroutine_int.h"
  16. #include "qemu/lockable.h"
  17. /*
  18. * Check that qemu_in_coroutine() works
  19. */
  20. static void coroutine_fn verify_in_coroutine(void *opaque)
  21. {
  22. g_assert(qemu_in_coroutine());
  23. }
  24. static void test_in_coroutine(void)
  25. {
  26. Coroutine *coroutine;
  27. g_assert(!qemu_in_coroutine());
  28. coroutine = qemu_coroutine_create(verify_in_coroutine, NULL);
  29. qemu_coroutine_enter(coroutine);
  30. }
  31. /*
  32. * Check that qemu_coroutine_self() works
  33. */
  34. static void coroutine_fn verify_self(void *opaque)
  35. {
  36. Coroutine **p_co = opaque;
  37. g_assert(qemu_coroutine_self() == *p_co);
  38. }
  39. static void test_self(void)
  40. {
  41. Coroutine *coroutine;
  42. coroutine = qemu_coroutine_create(verify_self, &coroutine);
  43. qemu_coroutine_enter(coroutine);
  44. }
  45. /*
  46. * Check that qemu_coroutine_entered() works
  47. */
  48. static void coroutine_fn verify_entered_step_2(void *opaque)
  49. {
  50. Coroutine *caller = (Coroutine *)opaque;
  51. g_assert(qemu_coroutine_entered(caller));
  52. g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
  53. qemu_coroutine_yield();
  54. /* Once more to check it still works after yielding */
  55. g_assert(qemu_coroutine_entered(caller));
  56. g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
  57. }
  58. static void coroutine_fn verify_entered_step_1(void *opaque)
  59. {
  60. Coroutine *self = qemu_coroutine_self();
  61. Coroutine *coroutine;
  62. g_assert(qemu_coroutine_entered(self));
  63. coroutine = qemu_coroutine_create(verify_entered_step_2, self);
  64. g_assert(!qemu_coroutine_entered(coroutine));
  65. qemu_coroutine_enter(coroutine);
  66. g_assert(!qemu_coroutine_entered(coroutine));
  67. qemu_coroutine_enter(coroutine);
  68. }
  69. static void test_entered(void)
  70. {
  71. Coroutine *coroutine;
  72. coroutine = qemu_coroutine_create(verify_entered_step_1, NULL);
  73. g_assert(!qemu_coroutine_entered(coroutine));
  74. qemu_coroutine_enter(coroutine);
  75. }
  76. /*
  77. * Check that coroutines may nest multiple levels
  78. */
  79. typedef struct {
  80. unsigned int n_enter; /* num coroutines entered */
  81. unsigned int n_return; /* num coroutines returned */
  82. unsigned int max; /* maximum level of nesting */
  83. } NestData;
  84. static void coroutine_fn nest(void *opaque)
  85. {
  86. NestData *nd = opaque;
  87. nd->n_enter++;
  88. if (nd->n_enter < nd->max) {
  89. Coroutine *child;
  90. child = qemu_coroutine_create(nest, nd);
  91. qemu_coroutine_enter(child);
  92. }
  93. nd->n_return++;
  94. }
  95. static void test_nesting(void)
  96. {
  97. Coroutine *root;
  98. NestData nd = {
  99. .n_enter = 0,
  100. .n_return = 0,
  101. .max = 128,
  102. };
  103. root = qemu_coroutine_create(nest, &nd);
  104. qemu_coroutine_enter(root);
  105. /* Must enter and return from max nesting level */
  106. g_assert_cmpint(nd.n_enter, ==, nd.max);
  107. g_assert_cmpint(nd.n_return, ==, nd.max);
  108. }
  109. /*
  110. * Check that yield/enter transfer control correctly
  111. */
  112. static void coroutine_fn yield_5_times(void *opaque)
  113. {
  114. bool *done = opaque;
  115. int i;
  116. for (i = 0; i < 5; i++) {
  117. qemu_coroutine_yield();
  118. }
  119. *done = true;
  120. }
  121. static void test_yield(void)
  122. {
  123. Coroutine *coroutine;
  124. bool done = false;
  125. int i = -1; /* one extra time to return from coroutine */
  126. coroutine = qemu_coroutine_create(yield_5_times, &done);
  127. while (!done) {
  128. qemu_coroutine_enter(coroutine);
  129. i++;
  130. }
  131. g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
  132. }
  133. static void coroutine_fn c2_fn(void *opaque)
  134. {
  135. qemu_coroutine_yield();
  136. }
  137. static void coroutine_fn c1_fn(void *opaque)
  138. {
  139. Coroutine *c2 = opaque;
  140. qemu_coroutine_enter(c2);
  141. }
  142. static void test_no_dangling_access(void)
  143. {
  144. Coroutine *c1;
  145. Coroutine *c2;
  146. Coroutine tmp;
  147. c2 = qemu_coroutine_create(c2_fn, NULL);
  148. c1 = qemu_coroutine_create(c1_fn, c2);
  149. qemu_coroutine_enter(c1);
  150. /* c1 shouldn't be used any more now; make sure we segfault if it is */
  151. tmp = *c1;
  152. memset(c1, 0xff, sizeof(Coroutine));
  153. qemu_coroutine_enter(c2);
  154. /* Must restore the coroutine now to avoid corrupted pool */
  155. *c1 = tmp;
  156. }
  157. static bool locked;
  158. static int done;
  159. static void coroutine_fn mutex_fn(void *opaque)
  160. {
  161. CoMutex *m = opaque;
  162. qemu_co_mutex_lock(m);
  163. assert(!locked);
  164. locked = true;
  165. qemu_coroutine_yield();
  166. locked = false;
  167. qemu_co_mutex_unlock(m);
  168. done++;
  169. }
  170. static void coroutine_fn lockable_fn(void *opaque)
  171. {
  172. QemuLockable *x = opaque;
  173. qemu_lockable_lock(x);
  174. assert(!locked);
  175. locked = true;
  176. qemu_coroutine_yield();
  177. locked = false;
  178. qemu_lockable_unlock(x);
  179. done++;
  180. }
  181. static void do_test_co_mutex(CoroutineEntry *entry, void *opaque)
  182. {
  183. Coroutine *c1 = qemu_coroutine_create(entry, opaque);
  184. Coroutine *c2 = qemu_coroutine_create(entry, opaque);
  185. done = 0;
  186. qemu_coroutine_enter(c1);
  187. g_assert(locked);
  188. qemu_coroutine_enter(c2);
  189. /* Unlock queues c2. It is then started automatically when c1 yields or
  190. * terminates.
  191. */
  192. qemu_coroutine_enter(c1);
  193. g_assert_cmpint(done, ==, 1);
  194. g_assert(locked);
  195. qemu_coroutine_enter(c2);
  196. g_assert_cmpint(done, ==, 2);
  197. g_assert(!locked);
  198. }
  199. static void test_co_mutex(void)
  200. {
  201. CoMutex m;
  202. qemu_co_mutex_init(&m);
  203. do_test_co_mutex(mutex_fn, &m);
  204. }
  205. static void test_co_mutex_lockable(void)
  206. {
  207. CoMutex m;
  208. CoMutex *null_pointer = NULL;
  209. qemu_co_mutex_init(&m);
  210. do_test_co_mutex(lockable_fn, QEMU_MAKE_LOCKABLE(&m));
  211. g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
  212. }
  213. /*
  214. * Check that creation, enter, and return work
  215. */
  216. static void coroutine_fn set_and_exit(void *opaque)
  217. {
  218. bool *done = opaque;
  219. *done = true;
  220. }
  221. static void test_lifecycle(void)
  222. {
  223. Coroutine *coroutine;
  224. bool done = false;
  225. /* Create, enter, and return from coroutine */
  226. coroutine = qemu_coroutine_create(set_and_exit, &done);
  227. qemu_coroutine_enter(coroutine);
  228. g_assert(done); /* expect done to be true (first time) */
  229. /* Repeat to check that no state affects this test */
  230. done = false;
  231. coroutine = qemu_coroutine_create(set_and_exit, &done);
  232. qemu_coroutine_enter(coroutine);
  233. g_assert(done); /* expect done to be true (second time) */
  234. }
  235. #define RECORD_SIZE 10 /* Leave some room for expansion */
  236. struct coroutine_position {
  237. int func;
  238. int state;
  239. };
  240. static struct coroutine_position records[RECORD_SIZE];
  241. static unsigned record_pos;
  242. static void record_push(int func, int state)
  243. {
  244. struct coroutine_position *cp = &records[record_pos++];
  245. g_assert_cmpint(record_pos, <, RECORD_SIZE);
  246. cp->func = func;
  247. cp->state = state;
  248. }
  249. static void coroutine_fn co_order_test(void *opaque)
  250. {
  251. record_push(2, 1);
  252. g_assert(qemu_in_coroutine());
  253. qemu_coroutine_yield();
  254. record_push(2, 2);
  255. g_assert(qemu_in_coroutine());
  256. }
  257. static void do_order_test(void)
  258. {
  259. Coroutine *co;
  260. co = qemu_coroutine_create(co_order_test, NULL);
  261. record_push(1, 1);
  262. qemu_coroutine_enter(co);
  263. record_push(1, 2);
  264. g_assert(!qemu_in_coroutine());
  265. qemu_coroutine_enter(co);
  266. record_push(1, 3);
  267. g_assert(!qemu_in_coroutine());
  268. }
  269. static void test_order(void)
  270. {
  271. int i;
  272. const struct coroutine_position expected_pos[] = {
  273. {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
  274. };
  275. do_order_test();
  276. g_assert_cmpint(record_pos, ==, 5);
  277. for (i = 0; i < record_pos; i++) {
  278. g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
  279. g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
  280. }
  281. }
  282. /*
  283. * Lifecycle benchmark
  284. */
  285. static void coroutine_fn empty_coroutine(void *opaque)
  286. {
  287. /* Do nothing */
  288. }
  289. static void perf_lifecycle(void)
  290. {
  291. Coroutine *coroutine;
  292. unsigned int i, max;
  293. double duration;
  294. max = 1000000;
  295. g_test_timer_start();
  296. for (i = 0; i < max; i++) {
  297. coroutine = qemu_coroutine_create(empty_coroutine, NULL);
  298. qemu_coroutine_enter(coroutine);
  299. }
  300. duration = g_test_timer_elapsed();
  301. g_test_message("Lifecycle %u iterations: %f s", max, duration);
  302. }
  303. static void perf_nesting(void)
  304. {
  305. unsigned int i, maxcycles, maxnesting;
  306. double duration;
  307. maxcycles = 10000;
  308. maxnesting = 1000;
  309. Coroutine *root;
  310. g_test_timer_start();
  311. for (i = 0; i < maxcycles; i++) {
  312. NestData nd = {
  313. .n_enter = 0,
  314. .n_return = 0,
  315. .max = maxnesting,
  316. };
  317. root = qemu_coroutine_create(nest, &nd);
  318. qemu_coroutine_enter(root);
  319. }
  320. duration = g_test_timer_elapsed();
  321. g_test_message("Nesting %u iterations of %u depth each: %f s",
  322. maxcycles, maxnesting, duration);
  323. }
  324. /*
  325. * Yield benchmark
  326. */
  327. static void coroutine_fn yield_loop(void *opaque)
  328. {
  329. unsigned int *counter = opaque;
  330. while ((*counter) > 0) {
  331. (*counter)--;
  332. qemu_coroutine_yield();
  333. }
  334. }
  335. static void perf_yield(void)
  336. {
  337. unsigned int i, maxcycles;
  338. double duration;
  339. maxcycles = 100000000;
  340. i = maxcycles;
  341. Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
  342. g_test_timer_start();
  343. while (i > 0) {
  344. qemu_coroutine_enter(coroutine);
  345. }
  346. duration = g_test_timer_elapsed();
  347. g_test_message("Yield %u iterations: %f s", maxcycles, duration);
  348. }
  349. static __attribute__((noinline)) void dummy(unsigned *i)
  350. {
  351. (*i)--;
  352. }
  353. static void perf_baseline(void)
  354. {
  355. unsigned int i, maxcycles;
  356. double duration;
  357. maxcycles = 100000000;
  358. i = maxcycles;
  359. g_test_timer_start();
  360. while (i > 0) {
  361. dummy(&i);
  362. }
  363. duration = g_test_timer_elapsed();
  364. g_test_message("Function call %u iterations: %f s", maxcycles, duration);
  365. }
  366. static __attribute__((noinline)) void perf_cost_func(void *opaque)
  367. {
  368. qemu_coroutine_yield();
  369. }
  370. static void perf_cost(void)
  371. {
  372. const unsigned long maxcycles = 40000000;
  373. unsigned long i = 0;
  374. double duration;
  375. unsigned long ops;
  376. Coroutine *co;
  377. g_test_timer_start();
  378. while (i++ < maxcycles) {
  379. co = qemu_coroutine_create(perf_cost_func, &i);
  380. qemu_coroutine_enter(co);
  381. qemu_coroutine_enter(co);
  382. }
  383. duration = g_test_timer_elapsed();
  384. ops = (long)(maxcycles / (duration * 1000));
  385. g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
  386. "%luns per coroutine",
  387. maxcycles,
  388. duration, ops,
  389. (unsigned long)(1000000000.0 * duration / maxcycles));
  390. }
  391. int main(int argc, char **argv)
  392. {
  393. g_test_init(&argc, &argv, NULL);
  394. /* This test assumes there is a freelist and marks freed coroutine memory
  395. * with a sentinel value. If there is no freelist this would legitimately
  396. * crash, so skip it.
  397. */
  398. if (CONFIG_COROUTINE_POOL) {
  399. g_test_add_func("/basic/no-dangling-access", test_no_dangling_access);
  400. }
  401. g_test_add_func("/basic/lifecycle", test_lifecycle);
  402. g_test_add_func("/basic/yield", test_yield);
  403. g_test_add_func("/basic/nesting", test_nesting);
  404. g_test_add_func("/basic/self", test_self);
  405. g_test_add_func("/basic/entered", test_entered);
  406. g_test_add_func("/basic/in_coroutine", test_in_coroutine);
  407. g_test_add_func("/basic/order", test_order);
  408. g_test_add_func("/locking/co-mutex", test_co_mutex);
  409. g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
  410. if (g_test_perf()) {
  411. g_test_add_func("/perf/lifecycle", perf_lifecycle);
  412. g_test_add_func("/perf/nesting", perf_nesting);
  413. g_test_add_func("/perf/yield", perf_yield);
  414. g_test_add_func("/perf/function-call", perf_baseline);
  415. g_test_add_func("/perf/cost", perf_cost);
  416. }
  417. return g_test_run();
  418. }