test_workqueue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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)
  148. {
  149. #ifdef TRACK_RESPONSES
  150. rsa_work_t *rw = arg; /* Naughty cast, but only looking at serial. */
  151. tor_assert(! bitarray_is_set(received, rw->serial));
  152. bitarray_set(received,rw->serial);
  153. #endif
  154. tor_free(arg);
  155. ++n_received;
  156. }
  157. /* This should never get called. */
  158. static void
  159. handle_reply_shutdown(void *arg)
  160. {
  161. (void)arg;
  162. no_shutdown = 1;
  163. }
  164. static workqueue_entry_t *
  165. add_work(threadpool_t *tp)
  166. {
  167. int add_rsa =
  168. opt_ratio_rsa == 0 ||
  169. tor_weak_random_range(&weak_rng, opt_ratio_rsa) == 0;
  170. if (add_rsa) {
  171. rsa_work_t *w = tor_malloc_zero(sizeof(*w));
  172. w->serial = n_sent++;
  173. crypto_rand((char*)w->msg, 20);
  174. w->msglen = 20;
  175. ++rsa_sent;
  176. return threadpool_queue_work_priority(tp,
  177. WQ_PRI_MED,
  178. workqueue_do_rsa, handle_reply, w);
  179. } else {
  180. ecdh_work_t *w = tor_malloc_zero(sizeof(*w));
  181. w->serial = n_sent++;
  182. /* Not strictly right, but this is just for benchmarks. */
  183. crypto_rand((char*)w->u.pk.public_key, 32);
  184. ++ecdh_sent;
  185. return threadpool_queue_work(tp, workqueue_do_ecdh, handle_reply, w);
  186. }
  187. }
  188. static int n_failed_cancel = 0;
  189. static int n_successful_cancel = 0;
  190. static int
  191. add_n_work_items(threadpool_t *tp, int n)
  192. {
  193. int n_queued = 0;
  194. int n_try_cancel = 0, i;
  195. workqueue_entry_t **to_cancel;
  196. workqueue_entry_t *ent;
  197. // We'll choose randomly which entries to cancel.
  198. to_cancel = tor_calloc(opt_n_cancel, sizeof(workqueue_entry_t*));
  199. while (n_queued++ < n) {
  200. ent = add_work(tp);
  201. if (! ent) {
  202. puts("Z");
  203. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), NULL);
  204. return -1;
  205. }
  206. if (n_try_cancel < opt_n_cancel) {
  207. to_cancel[n_try_cancel++] = ent;
  208. } else {
  209. int p = tor_weak_random_range(&weak_rng, n_queued);
  210. if (p < n_try_cancel) {
  211. to_cancel[p] = ent;
  212. }
  213. }
  214. }
  215. for (i = 0; i < n_try_cancel; ++i) {
  216. void *work = workqueue_entry_cancel(to_cancel[i]);
  217. if (! work) {
  218. n_failed_cancel++;
  219. } else {
  220. n_successful_cancel++;
  221. tor_free(work);
  222. }
  223. }
  224. tor_free(to_cancel);
  225. return 0;
  226. }
  227. static int shutting_down = 0;
  228. static void
  229. replysock_readable_cb(threadpool_t *tp)
  230. {
  231. if (n_received_previously == n_received)
  232. return;
  233. n_received_previously = n_received;
  234. if (opt_verbose) {
  235. printf("%d / %d", n_received, n_sent);
  236. if (opt_n_cancel)
  237. printf(" (%d cancelled, %d uncancellable)",
  238. n_successful_cancel, n_failed_cancel);
  239. puts("");
  240. }
  241. #ifdef TRACK_RESPONSES
  242. tor_mutex_acquire(&bitmap_mutex);
  243. for (i = 0; i < opt_n_items; ++i) {
  244. if (bitarray_is_set(received, i))
  245. putc('o', stdout);
  246. else if (bitarray_is_set(handled, i))
  247. putc('!', stdout);
  248. else
  249. putc('.', stdout);
  250. }
  251. puts("");
  252. tor_mutex_release(&bitmap_mutex);
  253. #endif /* defined(TRACK_RESPONSES) */
  254. if (n_sent - (n_received+n_successful_cancel) < opt_n_lowwater) {
  255. int n_to_send = n_received + opt_n_inflight - n_sent;
  256. if (n_to_send > opt_n_items - n_sent)
  257. n_to_send = opt_n_items - n_sent;
  258. add_n_work_items(tp, n_to_send);
  259. }
  260. if (shutting_down == 0 &&
  261. n_received+n_successful_cancel == n_sent &&
  262. n_sent >= opt_n_items) {
  263. shutting_down = 1;
  264. threadpool_queue_update(tp, NULL,
  265. workqueue_do_shutdown, NULL, NULL);
  266. // Anything we add after starting the shutdown must not be executed.
  267. threadpool_queue_work(tp, workqueue_shutdown_error,
  268. handle_reply_shutdown, NULL);
  269. {
  270. struct timeval limit = { 2, 0 };
  271. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
  272. }
  273. }
  274. }
  275. static void
  276. help(void)
  277. {
  278. puts(
  279. "Options:\n"
  280. " -h Display this information\n"
  281. " -v Be verbose\n"
  282. " -N <items> Run this many items of work\n"
  283. " -T <threads> Use this many threads\n"
  284. " -I <inflight> Have no more than this many requests queued at once\n"
  285. " -L <lowwater> Add items whenever fewer than this many are pending\n"
  286. " -C <cancel> Try to cancel N items of every batch that we add\n"
  287. " -R <ratio> Make one out of this many items be a slow (RSA) one\n"
  288. " --no-{eventfd2,eventfd,pipe2,pipe,socketpair}\n"
  289. " Disable one of the alert_socket backends.");
  290. }
  291. int
  292. main(int argc, char **argv)
  293. {
  294. replyqueue_t *rq;
  295. threadpool_t *tp;
  296. int i;
  297. tor_libevent_cfg evcfg;
  298. uint32_t as_flags = 0;
  299. for (i = 1; i < argc; ++i) {
  300. if (!strcmp(argv[i], "-v")) {
  301. opt_verbose = 1;
  302. } else if (!strcmp(argv[i], "-T") && i+1<argc) {
  303. opt_n_threads = atoi(argv[++i]);
  304. } else if (!strcmp(argv[i], "-N") && i+1<argc) {
  305. opt_n_items = atoi(argv[++i]);
  306. } else if (!strcmp(argv[i], "-I") && i+1<argc) {
  307. opt_n_inflight = atoi(argv[++i]);
  308. } else if (!strcmp(argv[i], "-L") && i+1<argc) {
  309. opt_n_lowwater = atoi(argv[++i]);
  310. } else if (!strcmp(argv[i], "-R") && i+1<argc) {
  311. opt_ratio_rsa = atoi(argv[++i]);
  312. } else if (!strcmp(argv[i], "-C") && i+1<argc) {
  313. opt_n_cancel = atoi(argv[++i]);
  314. } else if (!strcmp(argv[i], "--no-eventfd2")) {
  315. as_flags |= ASOCKS_NOEVENTFD2;
  316. } else if (!strcmp(argv[i], "--no-eventfd")) {
  317. as_flags |= ASOCKS_NOEVENTFD;
  318. } else if (!strcmp(argv[i], "--no-pipe2")) {
  319. as_flags |= ASOCKS_NOPIPE2;
  320. } else if (!strcmp(argv[i], "--no-pipe")) {
  321. as_flags |= ASOCKS_NOPIPE;
  322. } else if (!strcmp(argv[i], "--no-socketpair")) {
  323. as_flags |= ASOCKS_NOSOCKETPAIR;
  324. } else if (!strcmp(argv[i], "-h")) {
  325. help();
  326. return 0;
  327. } else {
  328. help();
  329. return 1;
  330. }
  331. }
  332. if (opt_n_threads < 1 ||
  333. opt_n_items < 1 || opt_n_inflight < 1 || opt_n_lowwater < 0 ||
  334. opt_n_cancel > opt_n_inflight || opt_n_inflight > MAX_INFLIGHT ||
  335. opt_ratio_rsa < 0) {
  336. help();
  337. return 1;
  338. }
  339. if (opt_n_inflight > opt_n_items) {
  340. opt_n_inflight = opt_n_items;
  341. }
  342. init_logging(1);
  343. network_init();
  344. if (crypto_global_init(1, NULL, NULL) < 0) {
  345. printf("Couldn't initialize crypto subsystem; exiting.\n");
  346. return 1;
  347. }
  348. if (crypto_seed_rng() < 0) {
  349. printf("Couldn't seed RNG; exiting.\n");
  350. return 1;
  351. }
  352. rq = replyqueue_new(as_flags);
  353. if (as_flags && rq == NULL)
  354. return 77; // 77 means "skipped".
  355. tor_assert(rq);
  356. tp = threadpool_new(opt_n_threads,
  357. rq, new_state, free_state, NULL);
  358. tor_assert(tp);
  359. crypto_seed_weak_rng(&weak_rng);
  360. memset(&evcfg, 0, sizeof(evcfg));
  361. tor_libevent_initialize(&evcfg);
  362. {
  363. int r = threadpool_register_reply_event(tp,
  364. replysock_readable_cb);
  365. tor_assert(r == 0);
  366. }
  367. #ifdef TRACK_RESPONSES
  368. handled = bitarray_init_zero(opt_n_items);
  369. received = bitarray_init_zero(opt_n_items);
  370. tor_mutex_init(&bitmap_mutex);
  371. handled_len = opt_n_items;
  372. #endif /* defined(TRACK_RESPONSES) */
  373. for (i = 0; i < opt_n_inflight; ++i) {
  374. if (! add_work(tp)) {
  375. puts("Couldn't add work.");
  376. return 1;
  377. }
  378. }
  379. {
  380. struct timeval limit = { 180, 0 };
  381. tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
  382. }
  383. tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
  384. if (n_sent != opt_n_items || n_received+n_successful_cancel != n_sent) {
  385. printf("%d vs %d\n", n_sent, opt_n_items);
  386. printf("%d+%d vs %d\n", n_received, n_successful_cancel, n_sent);
  387. puts("FAIL");
  388. return 1;
  389. } else if (no_shutdown) {
  390. puts("Accepted work after shutdown\n");
  391. puts("FAIL");
  392. } else {
  393. puts("OK");
  394. return 0;
  395. }
  396. }