test_workqueue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "or.h"
  6. #include "compat_threads.h"
  7. #include "onion.h"
  8. #include "workqueue.h"
  9. #include "crypto_curve25519.h"
  10. #include "crypto_rand.h"
  11. #include "compat_libevent.h"
  12. #include <stdio.h>
  13. #define MAX_INFLIGHT (1<<16)
  14. static int opt_verbose = 0;
  15. static int opt_n_threads = 8;
  16. static int opt_n_items = 10000;
  17. static int opt_n_inflight = 1000;
  18. static int opt_n_lowwater = 250;
  19. static int opt_n_cancel = 0;
  20. static int opt_ratio_rsa = 5;
  21. #ifdef TRACK_RESPONSES
  22. tor_mutex_t bitmap_mutex;
  23. int handled_len;
  24. bitarray_t *handled;
  25. #endif
  26. typedef struct state_s {
  27. int magic;
  28. int n_handled;
  29. crypto_pk_t *rsa;
  30. curve25519_secret_key_t ecdh;
  31. int is_shutdown;
  32. } state_t;
  33. typedef struct rsa_work_s {
  34. int serial;
  35. uint8_t msg[128];
  36. uint8_t msglen;
  37. } rsa_work_t;
  38. typedef struct ecdh_work_s {
  39. int serial;
  40. union {
  41. curve25519_public_key_t pk;
  42. uint8_t msg[32];
  43. } u;
  44. } ecdh_work_t;
  45. static void
  46. mark_handled(int serial)
  47. {
  48. #ifdef TRACK_RESPONSES
  49. tor_mutex_acquire(&bitmap_mutex);
  50. tor_assert(serial < handled_len);
  51. tor_assert(! bitarray_is_set(handled, serial));
  52. bitarray_set(handled, serial);
  53. tor_mutex_release(&bitmap_mutex);
  54. #else /* !(defined(TRACK_RESPONSES)) */
  55. (void)serial;
  56. #endif /* defined(TRACK_RESPONSES) */
  57. }
  58. static workqueue_reply_t
  59. workqueue_do_rsa(void *state, void *work)
  60. {
  61. rsa_work_t *rw = work;
  62. state_t *st = state;
  63. crypto_pk_t *rsa = st->rsa;
  64. uint8_t sig[256];
  65. int len;
  66. tor_assert(st->magic == 13371337);
  67. len = crypto_pk_private_sign(rsa, (char*)sig, 256,
  68. (char*)rw->msg, rw->msglen);
  69. if (len < 0) {
  70. rw->msglen = 0;
  71. return WQ_RPL_ERROR;
  72. }
  73. memset(rw->msg, 0, sizeof(rw->msg));
  74. rw->msglen = len;
  75. memcpy(rw->msg, sig, len);
  76. ++st->n_handled;
  77. mark_handled(rw->serial);
  78. return WQ_RPL_REPLY;
  79. }
  80. static workqueue_reply_t
  81. workqueue_do_shutdown(void *state, void *work)
  82. {
  83. (void)state;
  84. (void)work;
  85. crypto_pk_free(((state_t*)state)->rsa);
  86. tor_free(state);
  87. return WQ_RPL_SHUTDOWN;
  88. }
  89. static workqueue_reply_t
  90. workqueue_do_ecdh(void *state, void *work)
  91. {
  92. ecdh_work_t *ew = work;
  93. uint8_t output[CURVE25519_OUTPUT_LEN];
  94. state_t *st = state;
  95. tor_assert(st->magic == 13371337);
  96. curve25519_handshake(output, &st->ecdh, &ew->u.pk);
  97. memcpy(ew->u.msg, output, CURVE25519_OUTPUT_LEN);
  98. ++st->n_handled;
  99. mark_handled(ew->serial);
  100. return WQ_RPL_REPLY;
  101. }
  102. static workqueue_reply_t
  103. workqueue_shutdown_error(void *state, void *work)
  104. {
  105. (void)state;
  106. (void)work;
  107. return WQ_RPL_REPLY;
  108. }
  109. static void *
  110. new_state(void *arg)
  111. {
  112. state_t *st;
  113. (void)arg;
  114. st = tor_malloc(sizeof(*st));
  115. /* Every thread gets its own keys. not a problem for benchmarking */
  116. st->rsa = crypto_pk_new();
  117. if (crypto_pk_generate_key_with_bits(st->rsa, 1024) < 0) {
  118. crypto_pk_free(st->rsa);
  119. tor_free(st);
  120. return NULL;
  121. }
  122. curve25519_secret_key_generate(&st->ecdh, 0);
  123. st->magic = 13371337;
  124. return st;
  125. }
  126. static void
  127. free_state(void *arg)
  128. {
  129. state_t *st = arg;
  130. crypto_pk_free(st->rsa);
  131. tor_free(st);
  132. }
  133. static tor_weak_rng_t weak_rng;
  134. static int n_sent = 0;
  135. static int rsa_sent = 0;
  136. static int ecdh_sent = 0;
  137. static int n_received_previously = 0;
  138. static int n_received = 0;
  139. static int no_shutdown = 0;
  140. #ifdef TRACK_RESPONSES
  141. bitarray_t *received;
  142. #endif
  143. static void
  144. handle_reply(void *arg)
  145. {
  146. #ifdef TRACK_RESPONSES
  147. rsa_work_t *rw = arg; /* Naughty cast, but only looking at serial. */
  148. tor_assert(! bitarray_is_set(received, rw->serial));
  149. bitarray_set(received,rw->serial);
  150. #endif
  151. tor_free(arg);
  152. ++n_received;
  153. }
  154. /* This should never get called. */
  155. static void
  156. handle_reply_shutdown(void *arg)
  157. {
  158. (void)arg;
  159. no_shutdown = 1;
  160. }
  161. static workqueue_entry_t *
  162. add_work(threadpool_t *tp)
  163. {
  164. int add_rsa =
  165. opt_ratio_rsa == 0 ||
  166. tor_weak_random_range(&weak_rng, opt_ratio_rsa) == 0;
  167. if (add_rsa) {
  168. rsa_work_t *w = tor_malloc_zero(sizeof(*w));
  169. w->serial = n_sent++;
  170. crypto_rand((char*)w->msg, 20);
  171. w->msglen = 20;
  172. ++rsa_sent;
  173. return threadpool_queue_work_priority(tp,
  174. WQ_PRI_MED,
  175. workqueue_do_rsa, handle_reply, w);
  176. } else {
  177. ecdh_work_t *w = tor_malloc_zero(sizeof(*w));
  178. w->serial = n_sent++;
  179. /* Not strictly right, but this is just for benchmarks. */
  180. crypto_rand((char*)w->u.pk.public_key, 32);
  181. ++ecdh_sent;
  182. return threadpool_queue_work(tp, workqueue_do_ecdh, handle_reply, w);
  183. }
  184. }
  185. static int n_failed_cancel = 0;
  186. static int n_successful_cancel = 0;
  187. static int
  188. add_n_work_items(threadpool_t *tp, int n)
  189. {
  190. int n_queued = 0;
  191. int n_try_cancel = 0, i;
  192. workqueue_entry_t **to_cancel;
  193. workqueue_entry_t *ent;
  194. // We'll choose randomly which entries to cancel.
  195. to_cancel = tor_calloc(opt_n_cancel, sizeof(workqueue_entry_t*));
  196. while (n_queued++ < n) {
  197. ent = add_work(tp);
  198. if (! ent) {
  199. puts("Z");
  200. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), NULL);
  201. return -1;
  202. }
  203. if (n_try_cancel < opt_n_cancel) {
  204. to_cancel[n_try_cancel++] = ent;
  205. } else {
  206. int p = tor_weak_random_range(&weak_rng, n_queued);
  207. if (p < n_try_cancel) {
  208. to_cancel[p] = ent;
  209. }
  210. }
  211. }
  212. for (i = 0; i < n_try_cancel; ++i) {
  213. void *work = workqueue_entry_cancel(to_cancel[i]);
  214. if (! work) {
  215. n_failed_cancel++;
  216. } else {
  217. n_successful_cancel++;
  218. tor_free(work);
  219. }
  220. }
  221. tor_free(to_cancel);
  222. return 0;
  223. }
  224. static int shutting_down = 0;
  225. static void
  226. replysock_readable_cb(threadpool_t *tp)
  227. {
  228. if (n_received_previously == n_received)
  229. return;
  230. n_received_previously = n_received;
  231. if (opt_verbose) {
  232. printf("%d / %d", n_received, n_sent);
  233. if (opt_n_cancel)
  234. printf(" (%d cancelled, %d uncancellable)",
  235. n_successful_cancel, n_failed_cancel);
  236. puts("");
  237. }
  238. #ifdef TRACK_RESPONSES
  239. tor_mutex_acquire(&bitmap_mutex);
  240. for (i = 0; i < opt_n_items; ++i) {
  241. if (bitarray_is_set(received, i))
  242. putc('o', stdout);
  243. else if (bitarray_is_set(handled, i))
  244. putc('!', stdout);
  245. else
  246. putc('.', stdout);
  247. }
  248. puts("");
  249. tor_mutex_release(&bitmap_mutex);
  250. #endif /* defined(TRACK_RESPONSES) */
  251. if (n_sent - (n_received+n_successful_cancel) < opt_n_lowwater) {
  252. int n_to_send = n_received + opt_n_inflight - n_sent;
  253. if (n_to_send > opt_n_items - n_sent)
  254. n_to_send = opt_n_items - n_sent;
  255. add_n_work_items(tp, n_to_send);
  256. }
  257. if (shutting_down == 0 &&
  258. n_received+n_successful_cancel == n_sent &&
  259. n_sent >= opt_n_items) {
  260. shutting_down = 1;
  261. threadpool_queue_update(tp, NULL,
  262. workqueue_do_shutdown, NULL, NULL);
  263. // Anything we add after starting the shutdown must not be executed.
  264. threadpool_queue_work(tp, workqueue_shutdown_error,
  265. handle_reply_shutdown, NULL);
  266. {
  267. struct timeval limit = { 2, 0 };
  268. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
  269. }
  270. }
  271. }
  272. static void
  273. help(void)
  274. {
  275. puts(
  276. "Options:\n"
  277. " -h Display this information\n"
  278. " -v Be verbose\n"
  279. " -N <items> Run this many items of work\n"
  280. " -T <threads> Use this many threads\n"
  281. " -I <inflight> Have no more than this many requests queued at once\n"
  282. " -L <lowwater> Add items whenever fewer than this many are pending\n"
  283. " -C <cancel> Try to cancel N items of every batch that we add\n"
  284. " -R <ratio> Make one out of this many items be a slow (RSA) one\n"
  285. " --no-{eventfd2,eventfd,pipe2,pipe,socketpair}\n"
  286. " Disable one of the alert_socket backends.");
  287. }
  288. int
  289. main(int argc, char **argv)
  290. {
  291. replyqueue_t *rq;
  292. threadpool_t *tp;
  293. int i;
  294. tor_libevent_cfg evcfg;
  295. uint32_t as_flags = 0;
  296. for (i = 1; i < argc; ++i) {
  297. if (!strcmp(argv[i], "-v")) {
  298. opt_verbose = 1;
  299. } else if (!strcmp(argv[i], "-T") && i+1<argc) {
  300. opt_n_threads = atoi(argv[++i]);
  301. } else if (!strcmp(argv[i], "-N") && i+1<argc) {
  302. opt_n_items = atoi(argv[++i]);
  303. } else if (!strcmp(argv[i], "-I") && i+1<argc) {
  304. opt_n_inflight = atoi(argv[++i]);
  305. } else if (!strcmp(argv[i], "-L") && i+1<argc) {
  306. opt_n_lowwater = atoi(argv[++i]);
  307. } else if (!strcmp(argv[i], "-R") && i+1<argc) {
  308. opt_ratio_rsa = atoi(argv[++i]);
  309. } else if (!strcmp(argv[i], "-C") && i+1<argc) {
  310. opt_n_cancel = atoi(argv[++i]);
  311. } else if (!strcmp(argv[i], "--no-eventfd2")) {
  312. as_flags |= ASOCKS_NOEVENTFD2;
  313. } else if (!strcmp(argv[i], "--no-eventfd")) {
  314. as_flags |= ASOCKS_NOEVENTFD;
  315. } else if (!strcmp(argv[i], "--no-pipe2")) {
  316. as_flags |= ASOCKS_NOPIPE2;
  317. } else if (!strcmp(argv[i], "--no-pipe")) {
  318. as_flags |= ASOCKS_NOPIPE;
  319. } else if (!strcmp(argv[i], "--no-socketpair")) {
  320. as_flags |= ASOCKS_NOSOCKETPAIR;
  321. } else if (!strcmp(argv[i], "-h")) {
  322. help();
  323. return 0;
  324. } else {
  325. help();
  326. return 1;
  327. }
  328. }
  329. if (opt_n_threads < 1 ||
  330. opt_n_items < 1 || opt_n_inflight < 1 || opt_n_lowwater < 0 ||
  331. opt_n_cancel > opt_n_inflight || opt_n_inflight > MAX_INFLIGHT ||
  332. opt_ratio_rsa < 0) {
  333. help();
  334. return 1;
  335. }
  336. if (opt_n_inflight > opt_n_items) {
  337. opt_n_inflight = opt_n_items;
  338. }
  339. init_logging(1);
  340. network_init();
  341. if (crypto_global_init(1, NULL, NULL) < 0) {
  342. printf("Couldn't initialize crypto subsystem; exiting.\n");
  343. return 1;
  344. }
  345. if (crypto_seed_rng() < 0) {
  346. printf("Couldn't seed RNG; exiting.\n");
  347. return 1;
  348. }
  349. rq = replyqueue_new(as_flags);
  350. if (as_flags && rq == NULL)
  351. return 77; // 77 means "skipped".
  352. tor_assert(rq);
  353. tp = threadpool_new(opt_n_threads,
  354. rq, new_state, free_state, NULL);
  355. tor_assert(tp);
  356. crypto_seed_weak_rng(&weak_rng);
  357. memset(&evcfg, 0, sizeof(evcfg));
  358. tor_libevent_initialize(&evcfg);
  359. {
  360. int r = threadpool_register_reply_event(tp,
  361. replysock_readable_cb);
  362. tor_assert(r == 0);
  363. }
  364. #ifdef TRACK_RESPONSES
  365. handled = bitarray_init_zero(opt_n_items);
  366. received = bitarray_init_zero(opt_n_items);
  367. tor_mutex_init(&bitmap_mutex);
  368. handled_len = opt_n_items;
  369. #endif /* defined(TRACK_RESPONSES) */
  370. for (i = 0; i < opt_n_inflight; ++i) {
  371. if (! add_work(tp)) {
  372. puts("Couldn't add work.");
  373. return 1;
  374. }
  375. }
  376. {
  377. struct timeval limit = { 180, 0 };
  378. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
  379. }
  380. tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
  381. if (n_sent != opt_n_items || n_received+n_successful_cancel != n_sent) {
  382. printf("%d vs %d\n", n_sent, opt_n_items);
  383. printf("%d+%d vs %d\n", n_received, n_successful_cancel, n_sent);
  384. puts("FAIL");
  385. return 1;
  386. } else if (no_shutdown) {
  387. puts("Accepted work after shutdown\n");
  388. puts("FAIL");
  389. } else {
  390. puts("OK");
  391. return 0;
  392. }
  393. }