main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. void poll_used(void)
  81. {
  82. while (used_empty())
  83. busy_wait();
  84. }
  85. static void __attribute__((__flatten__)) run_guest(void)
  86. {
  87. int completed_before;
  88. int completed = 0;
  89. int started = 0;
  90. int bufs = runcycles;
  91. int spurious = 0;
  92. int r;
  93. unsigned len;
  94. void *buf;
  95. int tokick = batch;
  96. for (;;) {
  97. if (do_sleep)
  98. disable_call();
  99. completed_before = completed;
  100. do {
  101. if (started < bufs &&
  102. started - completed < max_outstanding) {
  103. r = add_inbuf(0, "Buffer\n", "Hello, world!");
  104. if (__builtin_expect(r == 0, true)) {
  105. ++started;
  106. if (!--tokick) {
  107. tokick = batch;
  108. if (do_sleep)
  109. kick_available();
  110. }
  111. }
  112. } else
  113. r = -1;
  114. /* Flush out completed bufs if any */
  115. if (get_buf(&len, &buf)) {
  116. ++completed;
  117. if (__builtin_expect(completed == bufs, false))
  118. return;
  119. r = 0;
  120. }
  121. } while (r == 0);
  122. if (completed == completed_before)
  123. ++spurious;
  124. assert(completed <= bufs);
  125. assert(started <= bufs);
  126. if (do_sleep) {
  127. if (used_empty() && enable_call())
  128. wait_for_call();
  129. } else {
  130. poll_used();
  131. }
  132. }
  133. }
  134. void poll_avail(void)
  135. {
  136. while (avail_empty())
  137. busy_wait();
  138. }
  139. static void __attribute__((__flatten__)) run_host(void)
  140. {
  141. int completed_before;
  142. int completed = 0;
  143. int spurious = 0;
  144. int bufs = runcycles;
  145. unsigned len;
  146. void *buf;
  147. for (;;) {
  148. if (do_sleep) {
  149. if (avail_empty() && enable_kick())
  150. wait_for_kick();
  151. } else {
  152. poll_avail();
  153. }
  154. if (do_sleep)
  155. disable_kick();
  156. completed_before = completed;
  157. while (__builtin_expect(use_buf(&len, &buf), true)) {
  158. if (do_sleep)
  159. call_used();
  160. ++completed;
  161. if (__builtin_expect(completed == bufs, false))
  162. return;
  163. }
  164. if (completed == completed_before)
  165. ++spurious;
  166. assert(completed <= bufs);
  167. if (completed == bufs)
  168. break;
  169. }
  170. }
  171. void *start_guest(void *arg)
  172. {
  173. set_affinity(arg);
  174. run_guest();
  175. pthread_exit(NULL);
  176. }
  177. void *start_host(void *arg)
  178. {
  179. set_affinity(arg);
  180. run_host();
  181. pthread_exit(NULL);
  182. }
  183. static const char optstring[] = "";
  184. static const struct option longopts[] = {
  185. {
  186. .name = "help",
  187. .has_arg = no_argument,
  188. .val = 'h',
  189. },
  190. {
  191. .name = "host-affinity",
  192. .has_arg = required_argument,
  193. .val = 'H',
  194. },
  195. {
  196. .name = "guest-affinity",
  197. .has_arg = required_argument,
  198. .val = 'G',
  199. },
  200. {
  201. .name = "ring-size",
  202. .has_arg = required_argument,
  203. .val = 'R',
  204. },
  205. {
  206. .name = "run-cycles",
  207. .has_arg = required_argument,
  208. .val = 'C',
  209. },
  210. {
  211. .name = "outstanding",
  212. .has_arg = required_argument,
  213. .val = 'o',
  214. },
  215. {
  216. .name = "batch",
  217. .has_arg = required_argument,
  218. .val = 'b',
  219. },
  220. {
  221. .name = "sleep",
  222. .has_arg = no_argument,
  223. .val = 's',
  224. },
  225. {
  226. .name = "relax",
  227. .has_arg = no_argument,
  228. .val = 'x',
  229. },
  230. {
  231. .name = "exit",
  232. .has_arg = no_argument,
  233. .val = 'e',
  234. },
  235. {
  236. }
  237. };
  238. static void help(void)
  239. {
  240. fprintf(stderr, "Usage: <test> [--help]"
  241. " [--host-affinity H]"
  242. " [--guest-affinity G]"
  243. " [--ring-size R (default: %d)]"
  244. " [--run-cycles C (default: %d)]"
  245. " [--batch b]"
  246. " [--outstanding o]"
  247. " [--sleep]"
  248. " [--relax]"
  249. " [--exit]"
  250. "\n",
  251. ring_size,
  252. runcycles);
  253. }
  254. int main(int argc, char **argv)
  255. {
  256. int ret;
  257. pthread_t host, guest;
  258. void *tret;
  259. char *host_arg = NULL;
  260. char *guest_arg = NULL;
  261. char *endptr;
  262. long int c;
  263. kickfd = eventfd(0, 0);
  264. assert(kickfd >= 0);
  265. callfd = eventfd(0, 0);
  266. assert(callfd >= 0);
  267. for (;;) {
  268. int o = getopt_long(argc, argv, optstring, longopts, NULL);
  269. switch (o) {
  270. case -1:
  271. goto done;
  272. case '?':
  273. help();
  274. exit(2);
  275. case 'H':
  276. host_arg = optarg;
  277. break;
  278. case 'G':
  279. guest_arg = optarg;
  280. break;
  281. case 'R':
  282. ring_size = strtol(optarg, &endptr, 0);
  283. assert(ring_size && !(ring_size & (ring_size - 1)));
  284. assert(!*endptr);
  285. break;
  286. case 'C':
  287. c = strtol(optarg, &endptr, 0);
  288. assert(!*endptr);
  289. assert(c > 0 && c < INT_MAX);
  290. runcycles = c;
  291. break;
  292. case 'o':
  293. c = strtol(optarg, &endptr, 0);
  294. assert(!*endptr);
  295. assert(c > 0 && c < INT_MAX);
  296. max_outstanding = c;
  297. break;
  298. case 'b':
  299. c = strtol(optarg, &endptr, 0);
  300. assert(!*endptr);
  301. assert(c > 0 && c < INT_MAX);
  302. batch = c;
  303. break;
  304. case 's':
  305. do_sleep = true;
  306. break;
  307. case 'x':
  308. do_relax = true;
  309. break;
  310. case 'e':
  311. do_exit = true;
  312. break;
  313. default:
  314. help();
  315. exit(4);
  316. break;
  317. }
  318. }
  319. /* does nothing here, used to make sure all smp APIs compile */
  320. smp_acquire();
  321. smp_release();
  322. smp_mb();
  323. done:
  324. if (batch > max_outstanding)
  325. batch = max_outstanding;
  326. if (optind < argc) {
  327. help();
  328. exit(4);
  329. }
  330. alloc_ring();
  331. ret = pthread_create(&host, NULL, start_host, host_arg);
  332. assert(!ret);
  333. ret = pthread_create(&guest, NULL, start_guest, guest_arg);
  334. assert(!ret);
  335. ret = pthread_join(guest, &tret);
  336. assert(!ret);
  337. ret = pthread_join(host, &tret);
  338. assert(!ret);
  339. return 0;
  340. }