gnunet-consensus-profiler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. This file is part of GNUnet
  3. (C) 2012 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file consensus/gnunet-consensus-profiler.c
  19. * @brief profiling tool for gnunet-consensus
  20. * @author Florian Dold
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_time_lib.h"
  25. #include "gnunet_consensus_service.h"
  26. #include "gnunet_testbed_service.h"
  27. static unsigned int num_peers = 2;
  28. static unsigned int replication = 1;
  29. static unsigned int num_values = 5;
  30. static struct GNUNET_TIME_Relative conclude_timeout;
  31. static struct GNUNET_TIME_Relative consensus_delay;
  32. static struct GNUNET_CONSENSUS_Handle **consensus_handles;
  33. static struct GNUNET_TESTBED_Operation **testbed_operations;
  34. static unsigned int num_connected_handles;
  35. static struct GNUNET_TESTBED_Peer **peers;
  36. static struct GNUNET_PeerIdentity *peer_ids;
  37. static unsigned int num_retrieved_peer_ids;
  38. static struct GNUNET_HashCode session_id;
  39. static unsigned int peers_done = 0;
  40. static unsigned *results_for_peer;
  41. static int verbose;
  42. /**
  43. * Start time for all consensuses.
  44. */
  45. static struct GNUNET_TIME_Absolute start;
  46. /**
  47. * Deadline for all consensuses.
  48. */
  49. static struct GNUNET_TIME_Absolute deadline;
  50. /**
  51. * Signature of the event handler function called by the
  52. * respective event controller.
  53. *
  54. * @param cls closure
  55. * @param event information about the event
  56. */
  57. static void
  58. controller_cb (void *cls,
  59. const struct GNUNET_TESTBED_EventInformation *event)
  60. {
  61. GNUNET_assert (0);
  62. }
  63. static void
  64. destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
  65. {
  66. struct GNUNET_CONSENSUS_Handle *consensus = cls;
  67. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  68. "destroying consensus\n");
  69. GNUNET_CONSENSUS_destroy (consensus);
  70. peers_done++;
  71. if (peers_done == num_peers)
  72. {
  73. unsigned int i;
  74. for (i = 0; i < num_peers; i++)
  75. GNUNET_TESTBED_operation_done (testbed_operations[i]);
  76. for (i = 0; i < num_peers; i++)
  77. printf ("P%u got %u of %u elements\n",
  78. i,
  79. results_for_peer[i],
  80. num_values);
  81. GNUNET_SCHEDULER_shutdown ();
  82. }
  83. }
  84. /**
  85. * Called when a conclusion was successful.
  86. *
  87. * @param cls closure, the consensus handle
  88. * @return #GNUNET_YES if more consensus groups should be offered,
  89. * #GNUNET_NO if not
  90. */
  91. static void
  92. conclude_cb (void *cls)
  93. {
  94. struct GNUNET_CONSENSUS_Handle **chp = cls;
  95. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  96. "consensus %d done\n",
  97. chp - consensus_handles);
  98. GNUNET_SCHEDULER_add_now (destroy, *chp);
  99. }
  100. static void
  101. generate_indices (int *indices)
  102. {
  103. int j;
  104. j = 0;
  105. while (j < replication)
  106. {
  107. int n;
  108. int k;
  109. int repeat;
  110. n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
  111. repeat = GNUNET_NO;
  112. for (k = 0; k < j; k++)
  113. if (indices[k] == n)
  114. {
  115. repeat = GNUNET_YES;
  116. break;
  117. }
  118. if (GNUNET_NO == repeat)
  119. indices[j++] = n;
  120. }
  121. }
  122. static void
  123. do_consensus ()
  124. {
  125. int unique_indices[replication];
  126. unsigned int i;
  127. for (i = 0; i < num_values; i++)
  128. {
  129. unsigned int j;
  130. struct GNUNET_HashCode val;
  131. struct GNUNET_SET_Element element;
  132. generate_indices (unique_indices);
  133. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
  134. element.data = &val;
  135. element.size = sizeof (val);
  136. for (j = 0; j < replication; j++)
  137. {
  138. int cid;
  139. cid = unique_indices[j];
  140. GNUNET_CONSENSUS_insert (consensus_handles[cid],
  141. &element,
  142. NULL, NULL);
  143. }
  144. }
  145. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  146. "all elements inserted, calling conclude\n");
  147. for (i = 0; i < num_peers; i++)
  148. GNUNET_CONSENSUS_conclude (consensus_handles[i],
  149. conclude_cb, &consensus_handles[i]);
  150. }
  151. /**
  152. * Callback to be called when a service connect operation is completed
  153. *
  154. * @param cls the callback closure from functions generating an operation
  155. * @param op the operation that has been finished
  156. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  157. * @param emsg error message in case the operation has failed; will be NULL if
  158. * operation has executed successfully.
  159. */
  160. static void
  161. connect_complete (void *cls,
  162. struct GNUNET_TESTBED_Operation *op,
  163. void *ca_result,
  164. const char *emsg)
  165. {
  166. if (NULL != emsg)
  167. {
  168. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  169. "testbed connect emsg: %s\n",
  170. emsg);
  171. GNUNET_assert (0);
  172. }
  173. num_connected_handles++;
  174. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  175. "connect complete\n");
  176. if (num_connected_handles == num_peers)
  177. {
  178. do_consensus ();
  179. }
  180. }
  181. static void
  182. new_element_cb (void *cls,
  183. const struct GNUNET_SET_Element *element)
  184. {
  185. struct GNUNET_CONSENSUS_Handle **chp = cls;
  186. int idx = chp - consensus_handles;
  187. GNUNET_assert (NULL != cls);
  188. results_for_peer[idx]++;
  189. GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
  190. if (GNUNET_YES == verbose)
  191. {
  192. printf ("P%d received %s\n",
  193. idx,
  194. GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
  195. }
  196. }
  197. /**
  198. * Adapter function called to establish a connection to
  199. * a service.
  200. *
  201. * @param cls closure
  202. * @param cfg configuration of the peer to connect to; will be available until
  203. * GNUNET_TESTBED_operation_done() is called on the operation returned
  204. * from GNUNET_TESTBED_service_connect()
  205. * @return service handle to return in 'op_result', NULL on error
  206. */
  207. static void *
  208. connect_adapter (void *cls,
  209. const struct GNUNET_CONFIGURATION_Handle *cfg)
  210. {
  211. struct GNUNET_CONSENSUS_Handle **chp = cls;
  212. struct GNUNET_CONSENSUS_Handle *consensus;
  213. chp = (struct GNUNET_CONSENSUS_Handle **) cls;
  214. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  215. "connect adapter, %d peers\n",
  216. num_peers);
  217. consensus = GNUNET_CONSENSUS_create (cfg,
  218. num_peers, peer_ids,
  219. &session_id,
  220. start,
  221. deadline,
  222. &new_element_cb, chp);
  223. *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
  224. return consensus;
  225. }
  226. /**
  227. * Adapter function called to destroy a connection to
  228. * a service.
  229. *
  230. * @param cls closure
  231. * @param op_result service handle returned from the connect adapter
  232. */
  233. static void
  234. disconnect_adapter(void *cls, void *op_result)
  235. {
  236. /* FIXME: what to do here? */
  237. }
  238. /**
  239. * Callback to be called when the requested peer information is available
  240. *
  241. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  242. * @param op the operation this callback corresponds to
  243. * @param pinfo the result; will be NULL if the operation has failed
  244. * @param emsg error message if the operation has failed; will be NULL if the
  245. * operation is successfull
  246. */
  247. static void
  248. peer_info_cb (void *cb_cls,
  249. struct GNUNET_TESTBED_Operation *op,
  250. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  251. const char *emsg)
  252. {
  253. struct GNUNET_PeerIdentity *p;
  254. int i;
  255. GNUNET_assert (NULL == emsg);
  256. p = (struct GNUNET_PeerIdentity *) cb_cls;
  257. if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
  258. {
  259. *p = *pinfo->result.id;
  260. num_retrieved_peer_ids++;
  261. if (num_retrieved_peer_ids == num_peers)
  262. for (i = 0; i < num_peers; i++)
  263. testbed_operations[i] =
  264. GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
  265. connect_adapter, disconnect_adapter, &consensus_handles[i]);
  266. }
  267. else
  268. {
  269. GNUNET_assert (0);
  270. }
  271. GNUNET_TESTBED_operation_done (op);
  272. }
  273. /**
  274. * Signature of a main function for a testcase.
  275. *
  276. * @param cls closure
  277. * @param h the run handle
  278. * @param num_peers number of peers in 'peers'
  279. * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
  280. * GNUNET_TESTBED_test_run()).
  281. * @param links_succeeded the number of overlay link connection attempts that
  282. * succeeded
  283. * @param links_failed the number of overlay link connection attempts that
  284. * failed
  285. */
  286. static void
  287. test_master (void *cls,
  288. struct GNUNET_TESTBED_RunHandle *h,
  289. unsigned int num_peers,
  290. struct GNUNET_TESTBED_Peer **started_peers,
  291. unsigned int links_succeeded,
  292. unsigned int links_failed)
  293. {
  294. int i;
  295. GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
  296. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
  297. peers = started_peers;
  298. peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
  299. results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
  300. consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
  301. testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
  302. for (i = 0; i < num_peers; i++)
  303. GNUNET_TESTBED_peer_get_information (peers[i],
  304. GNUNET_TESTBED_PIT_IDENTITY,
  305. peer_info_cb,
  306. &peer_ids[i]);
  307. }
  308. static void
  309. run (void *cls, char *const *args, const char *cfgfile,
  310. const struct GNUNET_CONFIGURATION_Handle *cfg)
  311. {
  312. static char *session_str = "gnunet-consensus/test";
  313. char *topology;
  314. int topology_cmp_result;
  315. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
  316. {
  317. fprintf (stderr,
  318. "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
  319. "seems like you passed the wrong configuration file\n");
  320. return;
  321. }
  322. topology_cmp_result = strcasecmp (topology, "NONE");
  323. GNUNET_free (topology);
  324. if (0 == topology_cmp_result)
  325. {
  326. fprintf (stderr,
  327. "'OVERLAY_TOPOLOGY' set to 'NONE', "
  328. "seems like you passed the wrong configuration file\n");
  329. return;
  330. }
  331. if (num_peers < replication)
  332. {
  333. fprintf (stderr, "k must be <=n\n");
  334. return;
  335. }
  336. start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
  337. deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
  338. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  339. "running gnunet-consensus\n");
  340. GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
  341. (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
  342. cfgfile,
  343. num_peers,
  344. 0,
  345. controller_cb,
  346. NULL,
  347. test_master,
  348. NULL);
  349. }
  350. int
  351. main (int argc, char **argv)
  352. {
  353. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  354. { 'n', "num-peers", NULL,
  355. gettext_noop ("number of peers in consensus"),
  356. GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
  357. { 'k', "value-replication", NULL,
  358. gettext_noop ("how many peers receive one value?"),
  359. GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
  360. { 'x', "num-values", NULL,
  361. gettext_noop ("number of values"),
  362. GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
  363. { 't', "timeout", NULL,
  364. gettext_noop ("consensus timeout"),
  365. GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
  366. { 'd', "delay", NULL,
  367. gettext_noop ("delay until consensus starts"),
  368. GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &consensus_delay },
  369. { 'V', "verbose", NULL,
  370. gettext_noop ("be more verbose (print received values)"),
  371. GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
  372. GNUNET_GETOPT_OPTION_END
  373. };
  374. conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
  375. GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
  376. "help",
  377. options, &run, NULL, GNUNET_YES);
  378. return 0;
  379. }