test_workqueue.c 12 KB

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