main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (C) 2016 Red Hat, Inc.
  3. * Author: Michael S. Tsirkin <mst@redhat.com>
  4. * This work is licensed under the terms of the GNU GPL, version 2.
  5. *
  6. * Command line processing and common functions for ring benchmarking.
  7. */
  8. #define _GNU_SOURCE
  9. #include <getopt.h>
  10. #include <pthread.h>
  11. #include <assert.h>
  12. #include <sched.h>
  13. #include "main.h"
  14. #include <sys/eventfd.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <limits.h>
  19. int runcycles = 10000000;
  20. int max_outstanding = INT_MAX;
  21. int batch = 1;
  22. bool do_sleep = false;
  23. bool do_relax = false;
  24. bool do_exit = true;
  25. unsigned ring_size = 256;
  26. static int kickfd = -1;
  27. static int callfd = -1;
  28. void notify(int fd)
  29. {
  30. unsigned long long v = 1;
  31. int r;
  32. vmexit();
  33. r = write(fd, &v, sizeof v);
  34. assert(r == sizeof v);
  35. vmentry();
  36. }
  37. void wait_for_notify(int fd)
  38. {
  39. unsigned long long v = 1;
  40. int r;
  41. vmexit();
  42. r = read(fd, &v, sizeof v);
  43. assert(r == sizeof v);
  44. vmentry();
  45. }
  46. void kick(void)
  47. {
  48. notify(kickfd);
  49. }
  50. void wait_for_kick(void)
  51. {
  52. wait_for_notify(kickfd);
  53. }
  54. void call(void)
  55. {
  56. notify(callfd);
  57. }
  58. void wait_for_call(void)
  59. {
  60. wait_for_notify(callfd);
  61. }
  62. void set_affinity(const char *arg)
  63. {
  64. cpu_set_t cpuset;
  65. int ret;
  66. pthread_t self;
  67. long int cpu;
  68. char *endptr;
  69. if (!arg)
  70. return;
  71. cpu = strtol(arg, &endptr, 0);
  72. assert(!*endptr);
  73. assert(cpu >= 0 || cpu < CPU_SETSIZE);
  74. self = pthread_self();
  75. CPU_ZERO(&cpuset);
  76. CPU_SET(cpu, &cpuset);
  77. ret = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset);
  78. assert(!ret);
  79. }
  80. static void run_guest(void)
  81. {
  82. int completed_before;
  83. int completed = 0;
  84. int started = 0;
  85. int bufs = runcycles;
  86. int spurious = 0;
  87. int r;
  88. unsigned len;
  89. void *buf;
  90. int tokick = batch;
  91. for (;;) {
  92. if (do_sleep)
  93. disable_call();
  94. completed_before = completed;
  95. do {
  96. if (started < bufs &&
  97. started - completed < max_outstanding) {
  98. r = add_inbuf(0, NULL, "Hello, world!");
  99. if (__builtin_expect(r == 0, true)) {
  100. ++started;
  101. if (!--tokick) {
  102. tokick = batch;
  103. if (do_sleep)
  104. kick_available();
  105. }
  106. }
  107. } else
  108. r = -1;
  109. /* Flush out completed bufs if any */
  110. if (get_buf(&len, &buf)) {
  111. ++completed;
  112. if (__builtin_expect(completed == bufs, false))
  113. return;
  114. r = 0;
  115. }
  116. } while (r == 0);
  117. if (completed == completed_before)
  118. ++spurious;
  119. assert(completed <= bufs);
  120. assert(started <= bufs);
  121. if (do_sleep) {
  122. if (enable_call())
  123. wait_for_call();
  124. } else {
  125. poll_used();
  126. }
  127. }
  128. }
  129. static void run_host(void)
  130. {
  131. int completed_before;
  132. int completed = 0;
  133. int spurious = 0;
  134. int bufs = runcycles;
  135. unsigned len;
  136. void *buf;
  137. for (;;) {
  138. if (do_sleep) {
  139. if (enable_kick())
  140. wait_for_kick();
  141. } else {
  142. poll_avail();
  143. }
  144. if (do_sleep)
  145. disable_kick();
  146. completed_before = completed;
  147. while (__builtin_expect(use_buf(&len, &buf), true)) {
  148. if (do_sleep)
  149. call_used();
  150. ++completed;
  151. if (__builtin_expect(completed == bufs, false))
  152. return;
  153. }
  154. if (completed == completed_before)
  155. ++spurious;
  156. assert(completed <= bufs);
  157. if (completed == bufs)
  158. break;
  159. }
  160. }
  161. void *start_guest(void *arg)
  162. {
  163. set_affinity(arg);
  164. run_guest();
  165. pthread_exit(NULL);
  166. }
  167. void *start_host(void *arg)
  168. {
  169. set_affinity(arg);
  170. run_host();
  171. pthread_exit(NULL);
  172. }
  173. static const char optstring[] = "";
  174. static const struct option longopts[] = {
  175. {
  176. .name = "help",
  177. .has_arg = no_argument,
  178. .val = 'h',
  179. },
  180. {
  181. .name = "host-affinity",
  182. .has_arg = required_argument,
  183. .val = 'H',
  184. },
  185. {
  186. .name = "guest-affinity",
  187. .has_arg = required_argument,
  188. .val = 'G',
  189. },
  190. {
  191. .name = "ring-size",
  192. .has_arg = required_argument,
  193. .val = 'R',
  194. },
  195. {
  196. .name = "run-cycles",
  197. .has_arg = required_argument,
  198. .val = 'C',
  199. },
  200. {
  201. .name = "outstanding",
  202. .has_arg = required_argument,
  203. .val = 'o',
  204. },
  205. {
  206. .name = "batch",
  207. .has_arg = required_argument,
  208. .val = 'b',
  209. },
  210. {
  211. .name = "sleep",
  212. .has_arg = no_argument,
  213. .val = 's',
  214. },
  215. {
  216. .name = "relax",
  217. .has_arg = no_argument,
  218. .val = 'x',
  219. },
  220. {
  221. .name = "exit",
  222. .has_arg = no_argument,
  223. .val = 'e',
  224. },
  225. {
  226. }
  227. };
  228. static void help(void)
  229. {
  230. fprintf(stderr, "Usage: <test> [--help]"
  231. " [--host-affinity H]"
  232. " [--guest-affinity G]"
  233. " [--ring-size R (default: %d)]"
  234. " [--run-cycles C (default: %d)]"
  235. " [--batch b]"
  236. " [--outstanding o]"
  237. " [--sleep]"
  238. " [--relax]"
  239. " [--exit]"
  240. "\n",
  241. ring_size,
  242. runcycles);
  243. }
  244. int main(int argc, char **argv)
  245. {
  246. int ret;
  247. pthread_t host, guest;
  248. void *tret;
  249. char *host_arg = NULL;
  250. char *guest_arg = NULL;
  251. char *endptr;
  252. long int c;
  253. kickfd = eventfd(0, 0);
  254. assert(kickfd >= 0);
  255. callfd = eventfd(0, 0);
  256. assert(callfd >= 0);
  257. for (;;) {
  258. int o = getopt_long(argc, argv, optstring, longopts, NULL);
  259. switch (o) {
  260. case -1:
  261. goto done;
  262. case '?':
  263. help();
  264. exit(2);
  265. case 'H':
  266. host_arg = optarg;
  267. break;
  268. case 'G':
  269. guest_arg = optarg;
  270. break;
  271. case 'R':
  272. ring_size = strtol(optarg, &endptr, 0);
  273. assert(ring_size && !(ring_size & (ring_size - 1)));
  274. assert(!*endptr);
  275. break;
  276. case 'C':
  277. c = strtol(optarg, &endptr, 0);
  278. assert(!*endptr);
  279. assert(c > 0 && c < INT_MAX);
  280. runcycles = c;
  281. break;
  282. case 'o':
  283. c = strtol(optarg, &endptr, 0);
  284. assert(!*endptr);
  285. assert(c > 0 && c < INT_MAX);
  286. max_outstanding = c;
  287. break;
  288. case 'b':
  289. c = strtol(optarg, &endptr, 0);
  290. assert(!*endptr);
  291. assert(c > 0 && c < INT_MAX);
  292. batch = c;
  293. break;
  294. case 's':
  295. do_sleep = true;
  296. break;
  297. case 'x':
  298. do_relax = true;
  299. break;
  300. case 'e':
  301. do_exit = true;
  302. break;
  303. default:
  304. help();
  305. exit(4);
  306. break;
  307. }
  308. }
  309. /* does nothing here, used to make sure all smp APIs compile */
  310. smp_acquire();
  311. smp_release();
  312. smp_mb();
  313. done:
  314. if (batch > max_outstanding)
  315. batch = max_outstanding;
  316. if (optind < argc) {
  317. help();
  318. exit(4);
  319. }
  320. alloc_ring();
  321. ret = pthread_create(&host, NULL, start_host, host_arg);
  322. assert(!ret);
  323. ret = pthread_create(&guest, NULL, start_guest, guest_arg);
  324. assert(!ret);
  325. ret = pthread_join(guest, &tret);
  326. assert(!ret);
  327. ret = pthread_join(host, &tret);
  328. assert(!ret);
  329. return 0;
  330. }