gnunet-consensus-profiler.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file consensus/gnunet-consensus-profiler.c
  18. * @brief profiling tool for gnunet-consensus
  19. * @author Florian Dold
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_time_lib.h"
  24. #include "gnunet_consensus_service.h"
  25. #include "gnunet_testbed_service.h"
  26. static unsigned int num_peers = 2;
  27. static unsigned int replication = 1;
  28. static unsigned int num_values = 5;
  29. static struct GNUNET_TIME_Relative conclude_timeout;
  30. static struct GNUNET_TIME_Relative consensus_delay;
  31. static struct GNUNET_CONSENSUS_Handle **consensus_handles;
  32. static struct GNUNET_TESTBED_Operation **testbed_operations;
  33. static unsigned int num_connected_handles;
  34. static struct GNUNET_TESTBED_Peer **peers;
  35. static struct GNUNET_PeerIdentity *peer_ids;
  36. static unsigned int num_retrieved_peer_ids;
  37. static struct GNUNET_HashCode session_id;
  38. static unsigned int peers_done = 0;
  39. static int dist_static;
  40. static unsigned *results_for_peer;
  41. /**
  42. * The profiler will write statistics
  43. * for all peers to the file with this name.
  44. */
  45. static char *statistics_filename;
  46. /**
  47. * The profiler will write statistics
  48. * for all peers to this file.
  49. */
  50. static FILE *statistics_file;
  51. static int verbose;
  52. /**
  53. * Start time for all consensuses.
  54. */
  55. static struct GNUNET_TIME_Absolute start;
  56. /**
  57. * Deadline for all consensuses.
  58. */
  59. static struct GNUNET_TIME_Absolute deadline;
  60. /**
  61. * Signature of the event handler function called by the
  62. * respective event controller.
  63. *
  64. * @param cls closure
  65. * @param event information about the event
  66. */
  67. static void
  68. controller_cb (void *cls,
  69. const struct GNUNET_TESTBED_EventInformation *event)
  70. {
  71. GNUNET_assert (0);
  72. }
  73. static void
  74. statistics_done_cb (void *cls,
  75. struct
  76. GNUNET_TESTBED_Operation
  77. *op,
  78. const char *emsg)
  79. {
  80. GNUNET_assert (NULL == emsg);
  81. GNUNET_TESTBED_operation_done (op);
  82. if (NULL != statistics_file)
  83. fclose (statistics_file);
  84. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got statistics, shutting down\n");
  85. GNUNET_SCHEDULER_shutdown ();
  86. }
  87. /**
  88. * Callback function to process statistic values from all peers.
  89. *
  90. * @param cls closure
  91. * @param peer the peer the statistic belong to
  92. * @param subsystem name of subsystem that created the statistic
  93. * @param name the name of the datum
  94. * @param value the current value
  95. * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
  96. * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  97. */
  98. static int
  99. statistics_cb (void *cls,
  100. const struct GNUNET_TESTBED_Peer *peer,
  101. const char *subsystem,
  102. const char *name,
  103. uint64_t value,
  104. int is_persistent)
  105. {
  106. if (NULL != statistics_file)
  107. {
  108. fprintf (statistics_file, "P%u\t%s\t%s\t%lu\n", GNUNET_TESTBED_get_index (
  109. peer), subsystem, name, (unsigned long) value);
  110. }
  111. return GNUNET_OK;
  112. }
  113. static void
  114. destroy (void *cls)
  115. {
  116. struct GNUNET_CONSENSUS_Handle *consensus = cls;
  117. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  118. "destroying consensus\n");
  119. GNUNET_CONSENSUS_destroy (consensus);
  120. peers_done++;
  121. if (peers_done == num_peers)
  122. {
  123. unsigned int i;
  124. for (i = 0; i < num_peers; i++)
  125. GNUNET_TESTBED_operation_done (testbed_operations[i]);
  126. for (i = 0; i < num_peers; i++)
  127. printf ("P%u got %u of %u elements\n",
  128. i,
  129. results_for_peer[i],
  130. num_values);
  131. if (NULL != statistics_filename)
  132. statistics_file = fopen (statistics_filename, "w");
  133. GNUNET_TESTBED_get_statistics (num_peers, peers, NULL, NULL,
  134. statistics_cb,
  135. statistics_done_cb,
  136. NULL);
  137. }
  138. }
  139. /**
  140. * Called when a conclusion was successful.
  141. *
  142. * @param cls closure, the consensus handle
  143. * @return #GNUNET_YES if more consensus groups should be offered,
  144. * #GNUNET_NO if not
  145. */
  146. static void
  147. conclude_cb (void *cls)
  148. {
  149. struct GNUNET_CONSENSUS_Handle **chp = cls;
  150. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  151. "consensus %d done\n",
  152. (int) (chp - consensus_handles));
  153. GNUNET_SCHEDULER_add_now (destroy, *chp);
  154. }
  155. static void
  156. generate_indices (int *indices)
  157. {
  158. int j;
  159. j = 0;
  160. while (j < replication)
  161. {
  162. int n;
  163. int k;
  164. int repeat;
  165. n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
  166. repeat = GNUNET_NO;
  167. for (k = 0; k < j; k++)
  168. if (indices[k] == n)
  169. {
  170. repeat = GNUNET_YES;
  171. break;
  172. }
  173. if (GNUNET_NO == repeat)
  174. indices[j++] = n;
  175. }
  176. }
  177. static void
  178. do_consensus ()
  179. {
  180. int unique_indices[replication];
  181. unsigned int i;
  182. unsigned int j;
  183. struct GNUNET_HashCode val;
  184. struct GNUNET_SET_Element element;
  185. if (dist_static)
  186. {
  187. for (i = 0; i < num_values; i++)
  188. {
  189. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
  190. element.data = &val;
  191. element.size = sizeof(val);
  192. for (j = 0; j < replication; j++)
  193. {
  194. GNUNET_CONSENSUS_insert (consensus_handles[j],
  195. &element,
  196. NULL, NULL);
  197. }
  198. }
  199. }
  200. else
  201. {
  202. for (i = 0; i < num_values; i++)
  203. {
  204. generate_indices (unique_indices);
  205. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
  206. element.data = &val;
  207. element.size = sizeof(val);
  208. for (j = 0; j < replication; j++)
  209. {
  210. int cid;
  211. cid = unique_indices[j];
  212. GNUNET_CONSENSUS_insert (consensus_handles[cid],
  213. &element,
  214. NULL, NULL);
  215. }
  216. }
  217. }
  218. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  219. "all elements inserted, calling conclude\n");
  220. for (i = 0; i < num_peers; i++)
  221. GNUNET_CONSENSUS_conclude (consensus_handles[i],
  222. conclude_cb, &consensus_handles[i]);
  223. }
  224. /**
  225. * Callback to be called when a service connect operation is completed
  226. *
  227. * @param cls the callback closure from functions generating an operation
  228. * @param op the operation that has been finished
  229. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  230. * @param emsg error message in case the operation has failed; will be NULL if
  231. * operation has executed successfully.
  232. */
  233. static void
  234. connect_complete (void *cls,
  235. struct GNUNET_TESTBED_Operation *op,
  236. void *ca_result,
  237. const char *emsg)
  238. {
  239. if (NULL != emsg)
  240. {
  241. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  242. "testbed connect emsg: %s\n",
  243. emsg);
  244. GNUNET_assert (0);
  245. }
  246. num_connected_handles++;
  247. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  248. "connect complete\n");
  249. if (num_connected_handles == num_peers)
  250. {
  251. do_consensus ();
  252. }
  253. }
  254. static void
  255. new_element_cb (void *cls,
  256. const struct GNUNET_SET_Element *element)
  257. {
  258. struct GNUNET_CONSENSUS_Handle **chp = cls;
  259. int idx = chp - consensus_handles;
  260. GNUNET_assert (NULL != cls);
  261. results_for_peer[idx]++;
  262. GNUNET_assert (sizeof(struct GNUNET_HashCode) == element->size);
  263. if (GNUNET_YES == verbose)
  264. {
  265. printf ("P%d received %s\n",
  266. idx,
  267. GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
  268. }
  269. }
  270. /**
  271. * Adapter function called to establish a connection to
  272. * a service.
  273. *
  274. * @param cls closure
  275. * @param cfg configuration of the peer to connect to; will be available until
  276. * GNUNET_TESTBED_operation_done() is called on the operation returned
  277. * from GNUNET_TESTBED_service_connect()
  278. * @return service handle to return in 'op_result', NULL on error
  279. */
  280. static void *
  281. connect_adapter (void *cls,
  282. const struct GNUNET_CONFIGURATION_Handle *cfg)
  283. {
  284. struct GNUNET_CONSENSUS_Handle **chp = cls;
  285. struct GNUNET_CONSENSUS_Handle *consensus;
  286. chp = (struct GNUNET_CONSENSUS_Handle **) cls;
  287. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  288. "connect adapter, %d peers\n",
  289. num_peers);
  290. consensus = GNUNET_CONSENSUS_create (cfg,
  291. num_peers, peer_ids,
  292. &session_id,
  293. start,
  294. deadline,
  295. &new_element_cb, chp);
  296. *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
  297. return consensus;
  298. }
  299. /**
  300. * Adapter function called to destroy a connection to
  301. * a service.
  302. *
  303. * @param cls closure
  304. * @param op_result service handle returned from the connect adapter
  305. */
  306. static void
  307. disconnect_adapter (void *cls, void *op_result)
  308. {
  309. /* FIXME: what to do here? */
  310. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  311. "disconnect adapter called\n");
  312. }
  313. /**
  314. * Callback to be called when the requested peer information is available
  315. *
  316. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  317. * @param op the operation this callback corresponds to
  318. * @param pinfo the result; will be NULL if the operation has failed
  319. * @param emsg error message if the operation has failed; will be NULL if the
  320. * operation is successfull
  321. */
  322. static void
  323. peer_info_cb (void *cb_cls,
  324. struct GNUNET_TESTBED_Operation *op,
  325. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  326. const char *emsg)
  327. {
  328. struct GNUNET_PeerIdentity *p;
  329. int i;
  330. GNUNET_assert (NULL == emsg);
  331. p = (struct GNUNET_PeerIdentity *) cb_cls;
  332. if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
  333. {
  334. *p = *pinfo->result.id;
  335. num_retrieved_peer_ids++;
  336. if (num_retrieved_peer_ids == num_peers)
  337. for (i = 0; i < num_peers; i++)
  338. testbed_operations[i] =
  339. GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus",
  340. connect_complete, NULL,
  341. connect_adapter, disconnect_adapter,
  342. &consensus_handles[i]);
  343. }
  344. else
  345. {
  346. GNUNET_assert (0);
  347. }
  348. GNUNET_TESTBED_operation_done (op);
  349. }
  350. /**
  351. * Signature of a main function for a testcase.
  352. *
  353. * @param cls closure
  354. * @param h the run handle
  355. * @param num_peers number of peers in 'peers'
  356. * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
  357. * GNUNET_TESTBED_test_run()).
  358. * @param links_succeeded the number of overlay link connection attempts that
  359. * succeeded
  360. * @param links_failed the number of overlay link connection attempts that
  361. * failed
  362. */
  363. static void
  364. test_master (void *cls,
  365. struct GNUNET_TESTBED_RunHandle *h,
  366. unsigned int num_peers,
  367. struct GNUNET_TESTBED_Peer **started_peers,
  368. unsigned int links_succeeded,
  369. unsigned int links_failed)
  370. {
  371. int i;
  372. GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
  373. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
  374. peers = started_peers;
  375. peer_ids = GNUNET_malloc (num_peers * sizeof(struct GNUNET_PeerIdentity));
  376. results_for_peer = GNUNET_malloc (num_peers * sizeof(unsigned int));
  377. consensus_handles = GNUNET_malloc (num_peers * sizeof(struct
  378. ConsensusHandle *));
  379. testbed_operations = GNUNET_malloc (num_peers * sizeof(struct
  380. ConsensusHandle *));
  381. for (i = 0; i < num_peers; i++)
  382. GNUNET_TESTBED_peer_get_information (peers[i],
  383. GNUNET_TESTBED_PIT_IDENTITY,
  384. peer_info_cb,
  385. &peer_ids[i]);
  386. }
  387. static void
  388. run (void *cls, char *const *args, const char *cfgfile,
  389. const struct GNUNET_CONFIGURATION_Handle *cfg)
  390. {
  391. static char *session_str = "gnunet-consensus/test";
  392. char *topology;
  393. int topology_cmp_result;
  394. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
  395. "OVERLAY_TOPOLOGY",
  396. &topology))
  397. {
  398. fprintf (stderr,
  399. "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
  400. "seems like you passed the wrong configuration file\n");
  401. return;
  402. }
  403. topology_cmp_result = strcasecmp (topology, "NONE");
  404. GNUNET_free (topology);
  405. if (0 == topology_cmp_result)
  406. {
  407. fprintf (stderr,
  408. "'OVERLAY_TOPOLOGY' set to 'NONE', "
  409. "seems like you passed the wrong configuration file\n");
  410. return;
  411. }
  412. if (num_peers < replication)
  413. {
  414. fprintf (stderr, "k must be <=n\n");
  415. return;
  416. }
  417. start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
  418. consensus_delay);
  419. deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
  420. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  421. "running gnunet-consensus\n");
  422. GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
  423. (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
  424. cfgfile,
  425. num_peers,
  426. 0,
  427. controller_cb,
  428. NULL,
  429. test_master,
  430. NULL);
  431. }
  432. int
  433. main (int argc, char **argv)
  434. {
  435. struct GNUNET_GETOPT_CommandLineOption options[] = {
  436. GNUNET_GETOPT_option_uint ('n',
  437. "num-peers",
  438. NULL,
  439. gettext_noop ("number of peers in consensus"),
  440. &num_peers),
  441. GNUNET_GETOPT_option_uint ('k',
  442. "value-replication",
  443. NULL,
  444. gettext_noop (
  445. "how many peers (random selection without replacement) receive one value?"),
  446. &replication),
  447. GNUNET_GETOPT_option_uint ('x',
  448. "num-values",
  449. NULL,
  450. gettext_noop ("number of values"),
  451. &num_values),
  452. GNUNET_GETOPT_option_relative_time ('t',
  453. "timeout",
  454. NULL,
  455. gettext_noop ("consensus timeout"),
  456. &conclude_timeout),
  457. GNUNET_GETOPT_option_relative_time ('d',
  458. "delay",
  459. NULL,
  460. gettext_noop (
  461. "delay until consensus starts"),
  462. &consensus_delay),
  463. GNUNET_GETOPT_option_filename ('s',
  464. "statistics",
  465. "FILENAME",
  466. gettext_noop ("write statistics to file"),
  467. &statistics_filename),
  468. GNUNET_GETOPT_option_flag ('S',
  469. "dist-static",
  470. gettext_noop (
  471. "distribute elements to a static subset of good peers"),
  472. &dist_static),
  473. GNUNET_GETOPT_option_flag ('V',
  474. "verbose",
  475. gettext_noop (
  476. "be more verbose (print received values)"),
  477. &verbose),
  478. GNUNET_GETOPT_OPTION_END
  479. };
  480. conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
  481. GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
  482. "help",
  483. options, &run, NULL, GNUNET_YES);
  484. return 0;
  485. }