test_workqueue.c 9.6 KB

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