test_workqueue.c 11 KB

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