test_workqueue.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. } state_t;
  35. typedef struct rsa_work_s {
  36. int serial;
  37. uint8_t msg[128];
  38. uint8_t msglen;
  39. } rsa_work_t;
  40. typedef struct ecdh_work_s {
  41. int serial;
  42. union {
  43. curve25519_public_key_t pk;
  44. uint8_t msg[32];
  45. } u;
  46. } ecdh_work_t;
  47. static void
  48. mark_handled(int serial)
  49. {
  50. #ifdef TRACK_RESPONSES
  51. tor_mutex_acquire(&bitmap_mutex);
  52. tor_assert(serial < handled_len);
  53. tor_assert(! bitarray_is_set(handled, serial));
  54. bitarray_set(handled, serial);
  55. tor_mutex_release(&bitmap_mutex);
  56. #else
  57. (void)serial;
  58. #endif
  59. }
  60. static int
  61. workqueue_do_rsa(void *state, void *work)
  62. {
  63. rsa_work_t *rw = work;
  64. state_t *st = state;
  65. crypto_pk_t *rsa = st->rsa;
  66. uint8_t sig[256];
  67. int len;
  68. tor_assert(st->magic == 13371337);
  69. len = crypto_pk_private_sign(rsa, (char*)sig, 256,
  70. (char*)rw->msg, rw->msglen);
  71. if (len < 0) {
  72. rw->msglen = 0;
  73. return WQ_RPL_ERROR;
  74. }
  75. memset(rw->msg, 0, sizeof(rw->msg));
  76. rw->msglen = len;
  77. memcpy(rw->msg, sig, len);
  78. ++st->n_handled;
  79. mark_handled(rw->serial);
  80. return WQ_RPL_REPLY;
  81. }
  82. #if 0
  83. static int
  84. workqueue_do_shutdown(void *state, void *work)
  85. {
  86. (void)state;
  87. (void)work;
  88. (void)cmd;
  89. crypto_pk_free(((state_t*)state)->rsa);
  90. tor_free(state);
  91. return WQ_RPL_SHUTDOWN;
  92. }
  93. #endif
  94. static int
  95. workqueue_do_ecdh(void *state, void *work)
  96. {
  97. ecdh_work_t *ew = work;
  98. uint8_t output[CURVE25519_OUTPUT_LEN];
  99. state_t *st = state;
  100. tor_assert(st->magic == 13371337);
  101. curve25519_handshake(output, &st->ecdh, &ew->u.pk);
  102. memcpy(ew->u.msg, output, CURVE25519_OUTPUT_LEN);
  103. ++st->n_handled;
  104. mark_handled(ew->serial);
  105. return WQ_RPL_REPLY;
  106. }
  107. static void *
  108. new_state(void *arg)
  109. {
  110. state_t *st;
  111. (void)arg;
  112. st = tor_malloc(sizeof(*st));
  113. /* Every thread gets its own keys. not a problem for benchmarking */
  114. st->rsa = crypto_pk_new();
  115. if (crypto_pk_generate_key_with_bits(st->rsa, 1024) < 0) {
  116. puts("keygen failed");
  117. crypto_pk_free(st->rsa);
  118. tor_free(st);
  119. return NULL;
  120. }
  121. curve25519_secret_key_generate(&st->ecdh, 0);
  122. st->magic = 13371337;
  123. return st;
  124. }
  125. static void
  126. free_state(void *arg)
  127. {
  128. state_t *st = arg;
  129. crypto_pk_free(st->rsa);
  130. tor_free(st);
  131. }
  132. static tor_weak_rng_t weak_rng;
  133. static int n_sent = 0;
  134. static int rsa_sent = 0;
  135. static int ecdh_sent = 0;
  136. static int n_received = 0;
  137. #ifdef TRACK_RESPONSES
  138. bitarray_t *received;
  139. #endif
  140. static void
  141. handle_reply(void *arg)
  142. {
  143. #ifdef TRACK_RESPONSES
  144. rsa_work_t *rw = arg; /* Naughty cast, but only looking at serial. */
  145. tor_assert(! bitarray_is_set(received, rw->serial));
  146. bitarray_set(received,rw->serial);
  147. #endif
  148. tor_free(arg);
  149. ++n_received;
  150. }
  151. static int
  152. add_work(threadpool_t *tp)
  153. {
  154. int add_rsa =
  155. opt_ratio_rsa == 0 ||
  156. tor_weak_random_range(&weak_rng, opt_ratio_rsa) == 0;
  157. if (add_rsa) {
  158. rsa_work_t *w = tor_malloc_zero(sizeof(*w));
  159. w->serial = n_sent++;
  160. crypto_rand((char*)w->msg, 20);
  161. w->msglen = 20;
  162. ++rsa_sent;
  163. return threadpool_queue_work(tp, workqueue_do_rsa, handle_reply, w) != NULL;
  164. } else {
  165. ecdh_work_t *w = tor_malloc_zero(sizeof(*w));
  166. w->serial = n_sent++;
  167. /* Not strictly right, but this is just for benchmarks. */
  168. crypto_rand((char*)w->u.pk.public_key, 32);
  169. ++ecdh_sent;
  170. return threadpool_queue_work(tp, workqueue_do_ecdh, handle_reply, w) != NULL;
  171. }
  172. }
  173. static void
  174. replysock_readable_cb(tor_socket_t sock, short what, void *arg)
  175. {
  176. threadpool_t *tp = arg;
  177. replyqueue_t *rq = threadpool_get_replyqueue(tp);
  178. int old_r = n_received;
  179. (void) sock;
  180. (void) what;
  181. replyqueue_process(rq);
  182. if (old_r == n_received)
  183. return;
  184. if (opt_verbose)
  185. printf("%d / %d\n", n_received, n_sent);
  186. #ifdef TRACK_RESPONSES
  187. tor_mutex_acquire(&bitmap_mutex);
  188. for (i = 0; i < opt_n_items; ++i) {
  189. if (bitarray_is_set(received, i))
  190. putc('o', stdout);
  191. else if (bitarray_is_set(handled, i))
  192. putc('!', stdout);
  193. else
  194. putc('.', stdout);
  195. }
  196. puts("");
  197. tor_mutex_release(&bitmap_mutex);
  198. #endif
  199. if (n_sent - n_received < opt_n_lowwater) {
  200. while (n_sent < n_received + opt_n_inflight && n_sent < opt_n_items) {
  201. if (! add_work(tp)) {
  202. puts("Couldn't add work.");
  203. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  204. }
  205. }
  206. }
  207. if (n_received == n_sent && n_sent >= opt_n_items) {
  208. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  209. }
  210. }
  211. static void
  212. help(void)
  213. {
  214. puts(
  215. "Options:\n"
  216. " -N <items> Run this many items of work\n"
  217. " -T <threads> Use this many threads\n"
  218. " -I <inflight> Have no more than this many requests queued at once\n"
  219. " -L <lowwater> Add items whenever fewer than this many are pending.\n"
  220. " -R <ratio> Make one out of this many items be a slow (RSA) one");
  221. }
  222. int
  223. main(int argc, char **argv)
  224. {
  225. replyqueue_t *rq;
  226. threadpool_t *tp;
  227. int i;
  228. tor_libevent_cfg evcfg;
  229. struct event *ev;
  230. for (i = 1; i < argc; ++i) {
  231. if (!strcmp(argv[i], "-v")) {
  232. opt_verbose = 1;
  233. } else if (!strcmp(argv[i], "-T") && i+1<argc) {
  234. opt_n_threads = atoi(argv[++i]);
  235. } else if (!strcmp(argv[i], "-N") && i+1<argc) {
  236. opt_n_items = atoi(argv[++i]);
  237. } else if (!strcmp(argv[i], "-I") && i+1<argc) {
  238. opt_n_inflight = atoi(argv[++i]);
  239. } else if (!strcmp(argv[i], "-L") && i+1<argc) {
  240. opt_n_lowwater = atoi(argv[++i]);
  241. } else if (!strcmp(argv[i], "-R") && i+1<argc) {
  242. opt_ratio_rsa = atoi(argv[++i]);
  243. } else if (!strcmp(argv[i], "-h")) {
  244. help();
  245. return 0;
  246. } else {
  247. help();
  248. return 1;
  249. }
  250. }
  251. if (opt_n_threads < 1 ||
  252. opt_n_items < 1 || opt_n_inflight < 1 || opt_n_lowwater < 0 ||
  253. opt_ratio_rsa < 0) {
  254. help();
  255. return 1;
  256. }
  257. init_logging(1);
  258. crypto_global_init(1, NULL, NULL);
  259. crypto_seed_rng(1);
  260. rq = replyqueue_new();
  261. tor_assert(rq);
  262. tp = threadpool_new(opt_n_threads,
  263. rq, new_state, free_state, NULL);
  264. tor_assert(tp);
  265. crypto_seed_weak_rng(&weak_rng);
  266. memset(&evcfg, 0, sizeof(evcfg));
  267. tor_libevent_initialize(&evcfg);
  268. ev = tor_event_new(tor_libevent_get_base(),
  269. replyqueue_get_socket(rq), EV_READ|EV_PERSIST,
  270. replysock_readable_cb, tp);
  271. event_add(ev, NULL);
  272. #ifdef TRACK_RESPONSES
  273. handled = bitarray_init_zero(opt_n_items);
  274. received = bitarray_init_zero(opt_n_items);
  275. tor_mutex_init(&bitmap_mutex);
  276. handled_len = opt_n_items;
  277. #endif
  278. for (i = 0; i < opt_n_inflight; ++i) {
  279. if (! add_work(tp)) {
  280. puts("Couldn't add work.");
  281. return 1;
  282. }
  283. }
  284. {
  285. struct timeval limit = { 30, 0 };
  286. tor_event_base_loopexit(tor_libevent_get_base(), &limit);
  287. }
  288. event_base_loop(tor_libevent_get_base(), 0);
  289. if (n_sent != opt_n_items || n_received != n_sent) {
  290. puts("FAIL");
  291. return 1;
  292. } else {
  293. puts("OK");
  294. return 0;
  295. }
  296. }