test_workqueue.c 11 KB

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