test_workqueue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, 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_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
  58. (void)serial;
  59. #endif
  60. }
  61. static int
  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 int
  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 int
  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 void *
  106. new_state(void *arg)
  107. {
  108. state_t *st;
  109. (void)arg;
  110. st = tor_malloc(sizeof(*st));
  111. /* Every thread gets its own keys. not a problem for benchmarking */
  112. st->rsa = crypto_pk_new();
  113. if (crypto_pk_generate_key_with_bits(st->rsa, 1024) < 0) {
  114. puts("keygen failed");
  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 int
  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) != NULL;
  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) != NULL;
  169. }
  170. }
  171. static int shutting_down = 0;
  172. static int n_shutdowns_done = 0;
  173. static void
  174. shutdown_reply(void *arg)
  175. {
  176. (void)arg;
  177. tor_assert(shutting_down);
  178. ++n_shutdowns_done;
  179. if (n_shutdowns_done == opt_n_threads) {
  180. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  181. }
  182. }
  183. static void
  184. replysock_readable_cb(tor_socket_t sock, short what, void *arg)
  185. {
  186. threadpool_t *tp = arg;
  187. replyqueue_t *rq = threadpool_get_replyqueue(tp);
  188. int old_r = n_received;
  189. (void) sock;
  190. (void) what;
  191. replyqueue_process(rq);
  192. if (old_r == n_received)
  193. return;
  194. if (opt_verbose)
  195. printf("%d / %d\n", n_received, n_sent);
  196. #ifdef TRACK_RESPONSES
  197. tor_mutex_acquire(&bitmap_mutex);
  198. for (i = 0; i < opt_n_items; ++i) {
  199. if (bitarray_is_set(received, i))
  200. putc('o', stdout);
  201. else if (bitarray_is_set(handled, i))
  202. putc('!', stdout);
  203. else
  204. putc('.', stdout);
  205. }
  206. puts("");
  207. tor_mutex_release(&bitmap_mutex);
  208. #endif
  209. if (n_sent - n_received < opt_n_lowwater) {
  210. while (n_sent < n_received + opt_n_inflight && n_sent < opt_n_items) {
  211. if (! add_work(tp)) {
  212. puts("Couldn't add work.");
  213. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  214. }
  215. }
  216. }
  217. if (shutting_down == 0 && n_received == n_sent && n_sent >= opt_n_items) {
  218. shutting_down = 1;
  219. threadpool_queue_for_all(tp, NULL, workqueue_do_shutdown, shutdown_reply, NULL);
  220. }
  221. }
  222. static void
  223. help(void)
  224. {
  225. puts(
  226. "Options:\n"
  227. " -N <items> Run this many items of work\n"
  228. " -T <threads> Use this many threads\n"
  229. " -I <inflight> Have no more than this many requests queued at once\n"
  230. " -L <lowwater> Add items whenever fewer than this many are pending\n"
  231. " -R <ratio> Make one out of this many items be a slow (RSA) one\n"
  232. " --no-{eventfd2,eventfd,pipe2,pipe,socketpair}\n"
  233. " Disable one of the alert_socket backends.");
  234. }
  235. int
  236. main(int argc, char **argv)
  237. {
  238. replyqueue_t *rq;
  239. threadpool_t *tp;
  240. int i;
  241. tor_libevent_cfg evcfg;
  242. struct event *ev;
  243. uint32_t as_flags = 0;
  244. for (i = 1; i < argc; ++i) {
  245. if (!strcmp(argv[i], "-v")) {
  246. opt_verbose = 1;
  247. } else if (!strcmp(argv[i], "-T") && i+1<argc) {
  248. opt_n_threads = atoi(argv[++i]);
  249. } else if (!strcmp(argv[i], "-N") && i+1<argc) {
  250. opt_n_items = atoi(argv[++i]);
  251. } else if (!strcmp(argv[i], "-I") && i+1<argc) {
  252. opt_n_inflight = atoi(argv[++i]);
  253. } else if (!strcmp(argv[i], "-L") && i+1<argc) {
  254. opt_n_lowwater = atoi(argv[++i]);
  255. } else if (!strcmp(argv[i], "-R") && i+1<argc) {
  256. opt_ratio_rsa = atoi(argv[++i]);
  257. } else if (!strcmp(argv[i], "--no-eventfd2")) {
  258. as_flags |= ASOCKS_NOEVENTFD2;
  259. } else if (!strcmp(argv[i], "--no-eventfd")) {
  260. as_flags |= ASOCKS_NOEVENTFD;
  261. } else if (!strcmp(argv[i], "--no-pipe2")) {
  262. as_flags |= ASOCKS_NOPIPE2;
  263. } else if (!strcmp(argv[i], "--no-pipe")) {
  264. as_flags |= ASOCKS_NOPIPE;
  265. } else if (!strcmp(argv[i], "--no-socketpair")) {
  266. as_flags |= ASOCKS_NOSOCKETPAIR;
  267. } else if (!strcmp(argv[i], "-h")) {
  268. help();
  269. return 0;
  270. } else {
  271. help();
  272. return 1;
  273. }
  274. }
  275. if (opt_n_threads < 1 ||
  276. opt_n_items < 1 || opt_n_inflight < 1 || opt_n_lowwater < 0 ||
  277. opt_ratio_rsa < 0) {
  278. help();
  279. return 1;
  280. }
  281. init_logging(1);
  282. crypto_global_init(1, NULL, NULL);
  283. crypto_seed_rng(1);
  284. rq = replyqueue_new(as_flags);
  285. tor_assert(rq);
  286. tp = threadpool_new(opt_n_threads,
  287. rq, new_state, free_state, NULL);
  288. tor_assert(tp);
  289. crypto_seed_weak_rng(&weak_rng);
  290. memset(&evcfg, 0, sizeof(evcfg));
  291. tor_libevent_initialize(&evcfg);
  292. ev = tor_event_new(tor_libevent_get_base(),
  293. replyqueue_get_socket(rq), EV_READ|EV_PERSIST,
  294. replysock_readable_cb, tp);
  295. event_add(ev, NULL);
  296. #ifdef TRACK_RESPONSES
  297. handled = bitarray_init_zero(opt_n_items);
  298. received = bitarray_init_zero(opt_n_items);
  299. tor_mutex_init(&bitmap_mutex);
  300. handled_len = opt_n_items;
  301. #endif
  302. for (i = 0; i < opt_n_inflight; ++i) {
  303. if (! add_work(tp)) {
  304. puts("Couldn't add work.");
  305. return 1;
  306. }
  307. }
  308. {
  309. struct timeval limit = { 30, 0 };
  310. tor_event_base_loopexit(tor_libevent_get_base(), &limit);
  311. }
  312. event_base_loop(tor_libevent_get_base(), 0);
  313. if (n_sent != opt_n_items || n_received != n_sent ||
  314. n_shutdowns_done != opt_n_threads) {
  315. puts("FAIL");
  316. return 1;
  317. } else {
  318. puts("OK");
  319. return 0;
  320. }
  321. }