gnunet-consensus-profiler.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 (peer), subsystem, name, (unsigned long) value);
  109. }
  110. return GNUNET_OK;
  111. }
  112. static void
  113. destroy (void *cls)
  114. {
  115. struct GNUNET_CONSENSUS_Handle *consensus = cls;
  116. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  117. "destroying consensus\n");
  118. GNUNET_CONSENSUS_destroy (consensus);
  119. peers_done++;
  120. if (peers_done == num_peers)
  121. {
  122. unsigned int i;
  123. for (i = 0; i < num_peers; i++)
  124. GNUNET_TESTBED_operation_done (testbed_operations[i]);
  125. for (i = 0; i < num_peers; i++)
  126. printf ("P%u got %u of %u elements\n",
  127. i,
  128. results_for_peer[i],
  129. num_values);
  130. if (NULL != statistics_filename)
  131. statistics_file = fopen (statistics_filename, "w");
  132. GNUNET_TESTBED_get_statistics (num_peers, peers, NULL, NULL,
  133. statistics_cb,
  134. statistics_done_cb,
  135. NULL);
  136. }
  137. }
  138. /**
  139. * Called when a conclusion was successful.
  140. *
  141. * @param cls closure, the consensus handle
  142. * @return #GNUNET_YES if more consensus groups should be offered,
  143. * #GNUNET_NO if not
  144. */
  145. static void
  146. conclude_cb (void *cls)
  147. {
  148. struct GNUNET_CONSENSUS_Handle **chp = cls;
  149. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  150. "consensus %d done\n",
  151. (int) (chp - consensus_handles));
  152. GNUNET_SCHEDULER_add_now (destroy, *chp);
  153. }
  154. static void
  155. generate_indices (int *indices)
  156. {
  157. int j;
  158. j = 0;
  159. while (j < replication)
  160. {
  161. int n;
  162. int k;
  163. int repeat;
  164. n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
  165. repeat = GNUNET_NO;
  166. for (k = 0; k < j; k++)
  167. if (indices[k] == n)
  168. {
  169. repeat = GNUNET_YES;
  170. break;
  171. }
  172. if (GNUNET_NO == repeat)
  173. indices[j++] = n;
  174. }
  175. }
  176. static void
  177. do_consensus ()
  178. {
  179. int unique_indices[replication];
  180. unsigned int i;
  181. unsigned int j;
  182. struct GNUNET_HashCode val;
  183. struct GNUNET_SET_Element element;
  184. if (dist_static)
  185. {
  186. for (i = 0; i < num_values; i++)
  187. {
  188. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
  189. element.data = &val;
  190. element.size = sizeof (val);
  191. for (j = 0; j < replication; j++)
  192. {
  193. GNUNET_CONSENSUS_insert (consensus_handles[j],
  194. &element,
  195. NULL, NULL);
  196. }
  197. }
  198. }
  199. else
  200. {
  201. for (i = 0; i < num_values; i++)
  202. {
  203. generate_indices (unique_indices);
  204. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
  205. element.data = &val;
  206. element.size = sizeof (val);
  207. for (j = 0; j < replication; j++)
  208. {
  209. int cid;
  210. cid = unique_indices[j];
  211. GNUNET_CONSENSUS_insert (consensus_handles[cid],
  212. &element,
  213. NULL, NULL);
  214. }
  215. }
  216. }
  217. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  218. "all elements inserted, calling conclude\n");
  219. for (i = 0; i < num_peers; i++)
  220. GNUNET_CONSENSUS_conclude (consensus_handles[i],
  221. conclude_cb, &consensus_handles[i]);
  222. }
  223. /**
  224. * Callback to be called when a service connect operation is completed
  225. *
  226. * @param cls the callback closure from functions generating an operation
  227. * @param op the operation that has been finished
  228. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  229. * @param emsg error message in case the operation has failed; will be NULL if
  230. * operation has executed successfully.
  231. */
  232. static void
  233. connect_complete (void *cls,
  234. struct GNUNET_TESTBED_Operation *op,
  235. void *ca_result,
  236. const char *emsg)
  237. {
  238. if (NULL != emsg)
  239. {
  240. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  241. "testbed connect emsg: %s\n",
  242. emsg);
  243. GNUNET_assert (0);
  244. }
  245. num_connected_handles++;
  246. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  247. "connect complete\n");
  248. if (num_connected_handles == num_peers)
  249. {
  250. do_consensus ();
  251. }
  252. }
  253. static void
  254. new_element_cb (void *cls,
  255. const struct GNUNET_SET_Element *element)
  256. {
  257. struct GNUNET_CONSENSUS_Handle **chp = cls;
  258. int idx = chp - consensus_handles;
  259. GNUNET_assert (NULL != cls);
  260. results_for_peer[idx]++;
  261. GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
  262. if (GNUNET_YES == verbose)
  263. {
  264. printf ("P%d received %s\n",
  265. idx,
  266. GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
  267. }
  268. }
  269. /**
  270. * Adapter function called to establish a connection to
  271. * a service.
  272. *
  273. * @param cls closure
  274. * @param cfg configuration of the peer to connect to; will be available until
  275. * GNUNET_TESTBED_operation_done() is called on the operation returned
  276. * from GNUNET_TESTBED_service_connect()
  277. * @return service handle to return in 'op_result', NULL on error
  278. */
  279. static void *
  280. connect_adapter (void *cls,
  281. const struct GNUNET_CONFIGURATION_Handle *cfg)
  282. {
  283. struct GNUNET_CONSENSUS_Handle **chp = cls;
  284. struct GNUNET_CONSENSUS_Handle *consensus;
  285. chp = (struct GNUNET_CONSENSUS_Handle **) cls;
  286. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  287. "connect adapter, %d peers\n",
  288. num_peers);
  289. consensus = GNUNET_CONSENSUS_create (cfg,
  290. num_peers, peer_ids,
  291. &session_id,
  292. start,
  293. deadline,
  294. &new_element_cb, chp);
  295. *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
  296. return consensus;
  297. }
  298. /**
  299. * Adapter function called to destroy a connection to
  300. * a service.
  301. *
  302. * @param cls closure
  303. * @param op_result service handle returned from the connect adapter
  304. */
  305. static void
  306. disconnect_adapter(void *cls, void *op_result)
  307. {
  308. /* FIXME: what to do here? */
  309. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  310. "disconnect adapter called\n");
  311. }
  312. /**
  313. * Callback to be called when the requested peer information is available
  314. *
  315. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  316. * @param op the operation this callback corresponds to
  317. * @param pinfo the result; will be NULL if the operation has failed
  318. * @param emsg error message if the operation has failed; will be NULL if the
  319. * operation is successfull
  320. */
  321. static void
  322. peer_info_cb (void *cb_cls,
  323. struct GNUNET_TESTBED_Operation *op,
  324. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  325. const char *emsg)
  326. {
  327. struct GNUNET_PeerIdentity *p;
  328. int i;
  329. GNUNET_assert (NULL == emsg);
  330. p = (struct GNUNET_PeerIdentity *) cb_cls;
  331. if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
  332. {
  333. *p = *pinfo->result.id;
  334. num_retrieved_peer_ids++;
  335. if (num_retrieved_peer_ids == num_peers)
  336. for (i = 0; i < num_peers; i++)
  337. testbed_operations[i] =
  338. GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
  339. connect_adapter, disconnect_adapter, &consensus_handles[i]);
  340. }
  341. else
  342. {
  343. GNUNET_assert (0);
  344. }
  345. GNUNET_TESTBED_operation_done (op);
  346. }
  347. /**
  348. * Signature of a main function for a testcase.
  349. *
  350. * @param cls closure
  351. * @param h the run handle
  352. * @param num_peers number of peers in 'peers'
  353. * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
  354. * GNUNET_TESTBED_test_run()).
  355. * @param links_succeeded the number of overlay link connection attempts that
  356. * succeeded
  357. * @param links_failed the number of overlay link connection attempts that
  358. * failed
  359. */
  360. static void
  361. test_master (void *cls,
  362. struct GNUNET_TESTBED_RunHandle *h,
  363. unsigned int num_peers,
  364. struct GNUNET_TESTBED_Peer **started_peers,
  365. unsigned int links_succeeded,
  366. unsigned int links_failed)
  367. {
  368. int i;
  369. GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
  370. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
  371. peers = started_peers;
  372. peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
  373. results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
  374. consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
  375. testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
  376. for (i = 0; i < num_peers; i++)
  377. GNUNET_TESTBED_peer_get_information (peers[i],
  378. GNUNET_TESTBED_PIT_IDENTITY,
  379. peer_info_cb,
  380. &peer_ids[i]);
  381. }
  382. static void
  383. run (void *cls, char *const *args, const char *cfgfile,
  384. const struct GNUNET_CONFIGURATION_Handle *cfg)
  385. {
  386. static char *session_str = "gnunet-consensus/test";
  387. char *topology;
  388. int topology_cmp_result;
  389. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
  390. {
  391. fprintf (stderr,
  392. "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
  393. "seems like you passed the wrong configuration file\n");
  394. return;
  395. }
  396. topology_cmp_result = strcasecmp (topology, "NONE");
  397. GNUNET_free (topology);
  398. if (0 == topology_cmp_result)
  399. {
  400. fprintf (stderr,
  401. "'OVERLAY_TOPOLOGY' set to 'NONE', "
  402. "seems like you passed the wrong configuration file\n");
  403. return;
  404. }
  405. if (num_peers < replication)
  406. {
  407. fprintf (stderr, "k must be <=n\n");
  408. return;
  409. }
  410. start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
  411. deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
  412. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  413. "running gnunet-consensus\n");
  414. GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
  415. (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
  416. cfgfile,
  417. num_peers,
  418. 0,
  419. controller_cb,
  420. NULL,
  421. test_master,
  422. NULL);
  423. }
  424. int
  425. main (int argc, char **argv)
  426. {
  427. struct GNUNET_GETOPT_CommandLineOption options[] = {
  428. GNUNET_GETOPT_option_uint ('n',
  429. "num-peers",
  430. NULL,
  431. gettext_noop ("number of peers in consensus"),
  432. &num_peers),
  433. GNUNET_GETOPT_option_uint ('k',
  434. "value-replication",
  435. NULL,
  436. gettext_noop ("how many peers (random selection without replacement) receive one value?"),
  437. &replication),
  438. GNUNET_GETOPT_option_uint ('x',
  439. "num-values",
  440. NULL,
  441. gettext_noop ("number of values"),
  442. &num_values),
  443. GNUNET_GETOPT_option_relative_time ('t',
  444. "timeout",
  445. NULL,
  446. gettext_noop ("consensus timeout"),
  447. &conclude_timeout),
  448. GNUNET_GETOPT_option_relative_time ('d',
  449. "delay",
  450. NULL,
  451. gettext_noop ("delay until consensus starts"),
  452. &consensus_delay),
  453. GNUNET_GETOPT_option_filename ('s',
  454. "statistics",
  455. "FILENAME",
  456. gettext_noop ("write statistics to file"),
  457. &statistics_filename),
  458. GNUNET_GETOPT_option_flag ('S',
  459. "dist-static",
  460. gettext_noop ("distribute elements to a static subset of good peers"),
  461. &dist_static),
  462. GNUNET_GETOPT_option_flag ('V',
  463. "verbose",
  464. gettext_noop ("be more verbose (print received values)"),
  465. &verbose),
  466. GNUNET_GETOPT_OPTION_END
  467. };
  468. conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
  469. GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
  470. "help",
  471. options, &run, NULL, GNUNET_YES);
  472. return 0;
  473. }