test-coroutine.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <glib.h>
  14. #include "qemu-coroutine.h"
  15. /*
  16. * Check that qemu_in_coroutine() works
  17. */
  18. static void coroutine_fn verify_in_coroutine(void *opaque)
  19. {
  20. g_assert(qemu_in_coroutine());
  21. }
  22. static void test_in_coroutine(void)
  23. {
  24. Coroutine *coroutine;
  25. g_assert(!qemu_in_coroutine());
  26. coroutine = qemu_coroutine_create(verify_in_coroutine);
  27. qemu_coroutine_enter(coroutine, NULL);
  28. }
  29. /*
  30. * Check that qemu_coroutine_self() works
  31. */
  32. static void coroutine_fn verify_self(void *opaque)
  33. {
  34. g_assert(qemu_coroutine_self() == opaque);
  35. }
  36. static void test_self(void)
  37. {
  38. Coroutine *coroutine;
  39. coroutine = qemu_coroutine_create(verify_self);
  40. qemu_coroutine_enter(coroutine, coroutine);
  41. }
  42. /*
  43. * Check that coroutines may nest multiple levels
  44. */
  45. typedef struct {
  46. unsigned int n_enter; /* num coroutines entered */
  47. unsigned int n_return; /* num coroutines returned */
  48. unsigned int max; /* maximum level of nesting */
  49. } NestData;
  50. static void coroutine_fn nest(void *opaque)
  51. {
  52. NestData *nd = opaque;
  53. nd->n_enter++;
  54. if (nd->n_enter < nd->max) {
  55. Coroutine *child;
  56. child = qemu_coroutine_create(nest);
  57. qemu_coroutine_enter(child, nd);
  58. }
  59. nd->n_return++;
  60. }
  61. static void test_nesting(void)
  62. {
  63. Coroutine *root;
  64. NestData nd = {
  65. .n_enter = 0,
  66. .n_return = 0,
  67. .max = 128,
  68. };
  69. root = qemu_coroutine_create(nest);
  70. qemu_coroutine_enter(root, &nd);
  71. /* Must enter and return from max nesting level */
  72. g_assert_cmpint(nd.n_enter, ==, nd.max);
  73. g_assert_cmpint(nd.n_return, ==, nd.max);
  74. }
  75. /*
  76. * Check that yield/enter transfer control correctly
  77. */
  78. static void coroutine_fn yield_5_times(void *opaque)
  79. {
  80. bool *done = opaque;
  81. int i;
  82. for (i = 0; i < 5; i++) {
  83. qemu_coroutine_yield();
  84. }
  85. *done = true;
  86. }
  87. static void test_yield(void)
  88. {
  89. Coroutine *coroutine;
  90. bool done = false;
  91. int i = -1; /* one extra time to return from coroutine */
  92. coroutine = qemu_coroutine_create(yield_5_times);
  93. while (!done) {
  94. qemu_coroutine_enter(coroutine, &done);
  95. i++;
  96. }
  97. g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
  98. }
  99. /*
  100. * Check that creation, enter, and return work
  101. */
  102. static void coroutine_fn set_and_exit(void *opaque)
  103. {
  104. bool *done = opaque;
  105. *done = true;
  106. }
  107. static void test_lifecycle(void)
  108. {
  109. Coroutine *coroutine;
  110. bool done = false;
  111. /* Create, enter, and return from coroutine */
  112. coroutine = qemu_coroutine_create(set_and_exit);
  113. qemu_coroutine_enter(coroutine, &done);
  114. g_assert(done); /* expect done to be true (first time) */
  115. /* Repeat to check that no state affects this test */
  116. done = false;
  117. coroutine = qemu_coroutine_create(set_and_exit);
  118. qemu_coroutine_enter(coroutine, &done);
  119. g_assert(done); /* expect done to be true (second time) */
  120. }
  121. /*
  122. * Lifecycle benchmark
  123. */
  124. static void coroutine_fn empty_coroutine(void *opaque)
  125. {
  126. /* Do nothing */
  127. }
  128. static void perf_lifecycle(void)
  129. {
  130. Coroutine *coroutine;
  131. unsigned int i, max;
  132. double duration;
  133. max = 1000000;
  134. g_test_timer_start();
  135. for (i = 0; i < max; i++) {
  136. coroutine = qemu_coroutine_create(empty_coroutine);
  137. qemu_coroutine_enter(coroutine, NULL);
  138. }
  139. duration = g_test_timer_elapsed();
  140. g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. g_test_init(&argc, &argv, NULL);
  145. g_test_add_func("/basic/lifecycle", test_lifecycle);
  146. g_test_add_func("/basic/yield", test_yield);
  147. g_test_add_func("/basic/nesting", test_nesting);
  148. g_test_add_func("/basic/self", test_self);
  149. g_test_add_func("/basic/in_coroutine", test_in_coroutine);
  150. if (g_test_perf()) {
  151. g_test_add_func("/perf/lifecycle", perf_lifecycle);
  152. }
  153. return g_test_run();
  154. }