main.c 6.4 KB

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