bench_workqueue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifdef TRACK_RESPONSES
  19. tor_mutex_t bitmap_mutex;
  20. int handled_len;
  21. bitarray_t *handled;
  22. #endif
  23. #define N_ITEMS 10000
  24. #define N_INFLIGHT 1000
  25. #define RELAUNCH_AT 250
  26. typedef struct state_s {
  27. int magic;
  28. int n_handled;
  29. crypto_pk_t *rsa;
  30. curve25519_secret_key_t ecdh;
  31. } state_t;
  32. typedef struct rsa_work_s {
  33. int serial;
  34. uint8_t msg[128];
  35. uint8_t msglen;
  36. } rsa_work_t;
  37. typedef struct ecdh_work_s {
  38. int serial;
  39. union {
  40. curve25519_public_key_t pk;
  41. uint8_t msg[32];
  42. } u;
  43. } ecdh_work_t;
  44. static void
  45. mark_handled(int serial)
  46. {
  47. #ifdef TRACK_RESPONSES
  48. tor_mutex_acquire(&bitmap_mutex);
  49. tor_assert(serial < handled_len);
  50. tor_assert(! bitarray_is_set(handled, serial));
  51. bitarray_set(handled, serial);
  52. tor_mutex_release(&bitmap_mutex);
  53. #else
  54. (void)serial;
  55. #endif
  56. }
  57. static int
  58. workqueue_do_rsa(void *state, void *work)
  59. {
  60. rsa_work_t *rw = work;
  61. state_t *st = state;
  62. crypto_pk_t *rsa = st->rsa;
  63. uint8_t sig[256];
  64. int len;
  65. tor_assert(st->magic == 13371337);
  66. len = crypto_pk_private_sign(rsa, (char*)sig, 256,
  67. (char*)rw->msg, rw->msglen);
  68. if (len < 0) {
  69. rw->msglen = 0;
  70. return WQ_RPL_ERROR;
  71. }
  72. memset(rw->msg, 0, sizeof(rw->msg));
  73. rw->msglen = len;
  74. memcpy(rw->msg, sig, len);
  75. ++st->n_handled;
  76. mark_handled(rw->serial);
  77. return WQ_RPL_REPLY;
  78. }
  79. #if 0
  80. static int
  81. workqueue_do_shutdown(void *state, void *work)
  82. {
  83. (void)state;
  84. (void)work;
  85. (void)cmd;
  86. crypto_pk_free(((state_t*)state)->rsa);
  87. tor_free(state);
  88. return WQ_RPL_SHUTDOWN;
  89. }
  90. #endif
  91. static int
  92. workqueue_do_ecdh(void *state, void *work)
  93. {
  94. ecdh_work_t *ew = work;
  95. uint8_t output[CURVE25519_OUTPUT_LEN];
  96. state_t *st = state;
  97. tor_assert(st->magic == 13371337);
  98. curve25519_handshake(output, &st->ecdh, &ew->u.pk);
  99. memcpy(ew->u.msg, output, CURVE25519_OUTPUT_LEN);
  100. ++st->n_handled;
  101. mark_handled(ew->serial);
  102. return WQ_RPL_REPLY;
  103. }
  104. static void *
  105. new_state(void *arg)
  106. {
  107. state_t *st;
  108. (void)arg;
  109. st = tor_malloc(sizeof(*st));
  110. /* Every thread gets its own keys. not a problem for benchmarking */
  111. st->rsa = crypto_pk_new();
  112. if (crypto_pk_generate_key_with_bits(st->rsa, 1024) < 0) {
  113. puts("keygen failed");
  114. crypto_pk_free(st->rsa);
  115. tor_free(st);
  116. return NULL;
  117. }
  118. curve25519_secret_key_generate(&st->ecdh, 0);
  119. st->magic = 13371337;
  120. return st;
  121. }
  122. static void
  123. free_state(void *arg)
  124. {
  125. state_t *st = arg;
  126. crypto_pk_free(st->rsa);
  127. tor_free(st);
  128. }
  129. static tor_weak_rng_t weak_rng;
  130. static int n_sent = 0;
  131. static int rsa_sent = 0;
  132. static int ecdh_sent = 0;
  133. static int n_received = 0;
  134. #ifdef TRACK_RESPONSES
  135. bitarray_t *received;
  136. #endif
  137. static void
  138. handle_reply(void *arg)
  139. {
  140. #ifdef TRACK_RESPONSES
  141. rsa_work_t *rw = arg; /* Naughty cast, but only looking at serial. */
  142. tor_assert(! bitarray_is_set(received, rw->serial));
  143. bitarray_set(received,rw->serial);
  144. #endif
  145. tor_free(arg);
  146. ++n_received;
  147. }
  148. static int
  149. add_work(threadpool_t *tp)
  150. {
  151. int add_rsa = tor_weak_random_range(&weak_rng, 5) == 0;
  152. if (add_rsa) {
  153. rsa_work_t *w = tor_malloc_zero(sizeof(*w));
  154. w->serial = n_sent++;
  155. crypto_rand((char*)w->msg, 20);
  156. w->msglen = 20;
  157. ++rsa_sent;
  158. return threadpool_queue_work(tp, workqueue_do_rsa, handle_reply, w) != NULL;
  159. } else {
  160. ecdh_work_t *w = tor_malloc_zero(sizeof(*w));
  161. w->serial = n_sent++;
  162. /* Not strictly right, but this is just for benchmarks. */
  163. crypto_rand((char*)w->u.pk.public_key, 32);
  164. ++ecdh_sent;
  165. return threadpool_queue_work(tp, workqueue_do_ecdh, handle_reply, w) != NULL;
  166. }
  167. }
  168. static void
  169. replysock_readable_cb(tor_socket_t sock, short what, void *arg)
  170. {
  171. threadpool_t *tp = arg;
  172. replyqueue_t *rq = threadpool_get_replyqueue(tp);
  173. int old_r = n_received;
  174. (void) sock;
  175. (void) what;
  176. replyqueue_process(rq);
  177. if (old_r == n_received)
  178. return;
  179. printf("%d / %d\n", n_received, n_sent);
  180. #ifdef TRACK_RESPONSES
  181. tor_mutex_acquire(&bitmap_mutex);
  182. for (i = 0; i < N_ITEMS; ++i) {
  183. if (bitarray_is_set(received, i))
  184. putc('o', stdout);
  185. else if (bitarray_is_set(handled, i))
  186. putc('!', stdout);
  187. else
  188. putc('.', stdout);
  189. }
  190. puts("");
  191. tor_mutex_release(&bitmap_mutex);
  192. #endif
  193. if (n_sent - n_received < RELAUNCH_AT) {
  194. while (n_sent < n_received + N_INFLIGHT && n_sent < N_ITEMS) {
  195. if (! add_work(tp)) {
  196. puts("Couldn't add work.");
  197. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  198. }
  199. }
  200. }
  201. if (n_received == n_sent && n_sent >= N_ITEMS) {
  202. tor_event_base_loopexit(tor_libevent_get_base(), NULL);
  203. }
  204. }
  205. int
  206. main(int argc, char **argv)
  207. {
  208. replyqueue_t *rq;
  209. threadpool_t *tp;
  210. int i;
  211. tor_libevent_cfg evcfg;
  212. struct event *ev;
  213. (void)argc;
  214. (void)argv;
  215. init_logging(1);
  216. crypto_global_init(1, NULL, NULL);
  217. crypto_seed_rng(1);
  218. rq = replyqueue_new();
  219. tor_assert(rq);
  220. tp = threadpool_new(16,
  221. rq, new_state, free_state, NULL);
  222. tor_assert(tp);
  223. crypto_seed_weak_rng(&weak_rng);
  224. memset(&evcfg, 0, sizeof(evcfg));
  225. tor_libevent_initialize(&evcfg);
  226. ev = tor_event_new(tor_libevent_get_base(),
  227. replyqueue_get_socket(rq), EV_READ|EV_PERSIST,
  228. replysock_readable_cb, tp);
  229. event_add(ev, NULL);
  230. #ifdef TRACK_RESPONSES
  231. handled = bitarray_init_zero(N_ITEMS);
  232. received = bitarray_init_zero(N_ITEMS);
  233. tor_mutex_init(&bitmap_mutex);
  234. handled_len = N_ITEMS;
  235. #endif
  236. for (i = 0; i < N_INFLIGHT; ++i) {
  237. if (! add_work(tp)) {
  238. puts("Couldn't add work.");
  239. return 1;
  240. }
  241. }
  242. event_base_loop(tor_libevent_get_base(), 0);
  243. return 0;
  244. }