2
0

test-rcu-list.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * rcuq_test.c
  3. *
  4. * usage: rcuq_test <readers> <duration>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * Copyright (c) 2013 Mike D. Day, IBM Corporation.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/atomic.h"
  24. #include "qemu/rcu.h"
  25. #include "qemu/thread.h"
  26. #include "qemu/rcu_queue.h"
  27. /*
  28. * Test variables.
  29. */
  30. static QemuMutex counts_mutex;
  31. static long long n_reads = 0LL;
  32. static long long n_updates = 0LL;
  33. static int64_t n_reclaims;
  34. static int64_t n_nodes_removed;
  35. static long long n_nodes = 0LL;
  36. static int g_test_in_charge = 0;
  37. static int nthreadsrunning;
  38. #define GOFLAG_INIT 0
  39. #define GOFLAG_RUN 1
  40. #define GOFLAG_STOP 2
  41. static int goflag = GOFLAG_INIT;
  42. #define RCU_READ_RUN 1000
  43. #define RCU_UPDATE_RUN 10
  44. #define NR_THREADS 100
  45. #define RCU_Q_LEN 100
  46. static QemuThread threads[NR_THREADS];
  47. static struct rcu_reader_data *data[NR_THREADS];
  48. static int n_threads;
  49. static int select_random_el(int max)
  50. {
  51. return (rand() % max);
  52. }
  53. static void create_thread(void *(*func)(void *))
  54. {
  55. if (n_threads >= NR_THREADS) {
  56. fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
  57. exit(-1);
  58. }
  59. qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
  60. QEMU_THREAD_JOINABLE);
  61. n_threads++;
  62. }
  63. static void wait_all_threads(void)
  64. {
  65. int i;
  66. for (i = 0; i < n_threads; i++) {
  67. qemu_thread_join(&threads[i]);
  68. }
  69. n_threads = 0;
  70. }
  71. #ifndef TEST_LIST_TYPE
  72. #define TEST_LIST_TYPE 1
  73. #endif
  74. struct list_element {
  75. #if TEST_LIST_TYPE == 1
  76. QLIST_ENTRY(list_element) entry;
  77. #elif TEST_LIST_TYPE == 2
  78. QSIMPLEQ_ENTRY(list_element) entry;
  79. #elif TEST_LIST_TYPE == 3
  80. QTAILQ_ENTRY(list_element) entry;
  81. #else
  82. #error Invalid TEST_LIST_TYPE
  83. #endif
  84. struct rcu_head rcu;
  85. };
  86. static void reclaim_list_el(struct rcu_head *prcu)
  87. {
  88. struct list_element *el = container_of(prcu, struct list_element, rcu);
  89. g_free(el);
  90. /* Accessed only from call_rcu thread. */
  91. atomic_set_i64(&n_reclaims, n_reclaims + 1);
  92. }
  93. #if TEST_LIST_TYPE == 1
  94. static QLIST_HEAD(, list_element) Q_list_head;
  95. #define TEST_NAME "qlist"
  96. #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
  97. #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
  98. #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
  99. #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
  100. #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
  101. #elif TEST_LIST_TYPE == 2
  102. static QSIMPLEQ_HEAD(, list_element) Q_list_head =
  103. QSIMPLEQ_HEAD_INITIALIZER(Q_list_head);
  104. #define TEST_NAME "qsimpleq"
  105. #define TEST_LIST_REMOVE_RCU(el, f) \
  106. QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
  107. #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
  108. QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
  109. #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
  110. #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
  111. #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
  112. #elif TEST_LIST_TYPE == 3
  113. static QTAILQ_HEAD(, list_element) Q_list_head;
  114. #define TEST_NAME "qtailq"
  115. #define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f)
  116. #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
  117. QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
  118. #define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU
  119. #define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU
  120. #define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU
  121. #else
  122. #error Invalid TEST_LIST_TYPE
  123. #endif
  124. static void *rcu_q_reader(void *arg)
  125. {
  126. long long n_reads_local = 0;
  127. struct list_element *el;
  128. rcu_register_thread();
  129. *(struct rcu_reader_data **)arg = &rcu_reader;
  130. atomic_inc(&nthreadsrunning);
  131. while (atomic_read(&goflag) == GOFLAG_INIT) {
  132. g_usleep(1000);
  133. }
  134. while (atomic_read(&goflag) == GOFLAG_RUN) {
  135. rcu_read_lock();
  136. TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
  137. n_reads_local++;
  138. if (atomic_read(&goflag) == GOFLAG_STOP) {
  139. break;
  140. }
  141. }
  142. rcu_read_unlock();
  143. g_usleep(100);
  144. }
  145. qemu_mutex_lock(&counts_mutex);
  146. n_reads += n_reads_local;
  147. qemu_mutex_unlock(&counts_mutex);
  148. rcu_unregister_thread();
  149. return NULL;
  150. }
  151. static void *rcu_q_updater(void *arg)
  152. {
  153. int j, target_el;
  154. long long n_nodes_local = 0;
  155. long long n_updates_local = 0;
  156. long long n_removed_local = 0;
  157. struct list_element *el, *prev_el;
  158. *(struct rcu_reader_data **)arg = &rcu_reader;
  159. atomic_inc(&nthreadsrunning);
  160. while (atomic_read(&goflag) == GOFLAG_INIT) {
  161. g_usleep(1000);
  162. }
  163. while (atomic_read(&goflag) == GOFLAG_RUN) {
  164. target_el = select_random_el(RCU_Q_LEN);
  165. j = 0;
  166. /* FOREACH_RCU could work here but let's use both macros */
  167. TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
  168. j++;
  169. if (target_el == j) {
  170. TEST_LIST_REMOVE_RCU(prev_el, entry);
  171. /* may be more than one updater in the future */
  172. call_rcu1(&prev_el->rcu, reclaim_list_el);
  173. n_removed_local++;
  174. break;
  175. }
  176. }
  177. if (atomic_read(&goflag) == GOFLAG_STOP) {
  178. break;
  179. }
  180. target_el = select_random_el(RCU_Q_LEN);
  181. j = 0;
  182. TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
  183. j++;
  184. if (target_el == j) {
  185. struct list_element *new_el = g_new(struct list_element, 1);
  186. n_nodes += n_nodes_local;
  187. TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
  188. break;
  189. }
  190. }
  191. n_updates_local += 2;
  192. synchronize_rcu();
  193. }
  194. synchronize_rcu();
  195. qemu_mutex_lock(&counts_mutex);
  196. n_nodes += n_nodes_local;
  197. n_updates += n_updates_local;
  198. atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
  199. qemu_mutex_unlock(&counts_mutex);
  200. return NULL;
  201. }
  202. static void rcu_qtest_init(void)
  203. {
  204. struct list_element *new_el;
  205. int i;
  206. nthreadsrunning = 0;
  207. srand(time(0));
  208. for (i = 0; i < RCU_Q_LEN; i++) {
  209. new_el = g_new(struct list_element, 1);
  210. TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
  211. }
  212. qemu_mutex_lock(&counts_mutex);
  213. n_nodes += RCU_Q_LEN;
  214. qemu_mutex_unlock(&counts_mutex);
  215. }
  216. static void rcu_qtest_run(int duration, int nreaders)
  217. {
  218. int nthreads = nreaders + 1;
  219. while (atomic_read(&nthreadsrunning) < nthreads) {
  220. g_usleep(1000);
  221. }
  222. atomic_set(&goflag, GOFLAG_RUN);
  223. sleep(duration);
  224. atomic_set(&goflag, GOFLAG_STOP);
  225. wait_all_threads();
  226. }
  227. static void rcu_qtest(const char *test, int duration, int nreaders)
  228. {
  229. int i;
  230. long long n_removed_local = 0;
  231. struct list_element *el, *prev_el;
  232. rcu_qtest_init();
  233. for (i = 0; i < nreaders; i++) {
  234. create_thread(rcu_q_reader);
  235. }
  236. create_thread(rcu_q_updater);
  237. rcu_qtest_run(duration, nreaders);
  238. TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
  239. TEST_LIST_REMOVE_RCU(prev_el, entry);
  240. call_rcu1(&prev_el->rcu, reclaim_list_el);
  241. n_removed_local++;
  242. }
  243. qemu_mutex_lock(&counts_mutex);
  244. atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
  245. qemu_mutex_unlock(&counts_mutex);
  246. synchronize_rcu();
  247. while (atomic_read_i64(&n_nodes_removed) > atomic_read_i64(&n_reclaims)) {
  248. g_usleep(100);
  249. synchronize_rcu();
  250. }
  251. if (g_test_in_charge) {
  252. g_assert_cmpint(atomic_read_i64(&n_nodes_removed), ==,
  253. atomic_read_i64(&n_reclaims));
  254. } else {
  255. printf("%s: %d readers; 1 updater; nodes read: " \
  256. "%lld, nodes removed: %"PRIi64"; nodes reclaimed: %"PRIi64"\n",
  257. test, nthreadsrunning - 1, n_reads,
  258. atomic_read_i64(&n_nodes_removed), atomic_read_i64(&n_reclaims));
  259. exit(0);
  260. }
  261. }
  262. static void usage(int argc, char *argv[])
  263. {
  264. fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
  265. exit(-1);
  266. }
  267. static int gtest_seconds;
  268. static void gtest_rcuq_one(void)
  269. {
  270. rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
  271. }
  272. static void gtest_rcuq_few(void)
  273. {
  274. rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
  275. }
  276. static void gtest_rcuq_many(void)
  277. {
  278. rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
  279. }
  280. int main(int argc, char *argv[])
  281. {
  282. int duration = 0, readers = 0;
  283. qemu_mutex_init(&counts_mutex);
  284. if (argc >= 2) {
  285. if (argv[1][0] == '-') {
  286. g_test_init(&argc, &argv, NULL);
  287. if (g_test_quick()) {
  288. gtest_seconds = 4;
  289. } else {
  290. gtest_seconds = 20;
  291. }
  292. g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one);
  293. g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few);
  294. g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many);
  295. g_test_in_charge = 1;
  296. return g_test_run();
  297. }
  298. duration = strtoul(argv[1], NULL, 0);
  299. }
  300. if (argc >= 3) {
  301. readers = strtoul(argv[2], NULL, 0);
  302. }
  303. if (duration && readers) {
  304. rcu_qtest(argv[0], duration, readers);
  305. return 0;
  306. }
  307. usage(argc, argv);
  308. return -1;
  309. }