test_experimentation_clique_run.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. This file is part of GNUnet
  3. (C) 2008--2013 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 src/experimentation/test_experimentation_clique_run.c
  19. * @brief test case to run experiments with experimentation daemons in a clique
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_testbed_service.h"
  26. /**
  27. * Number of peers we want to start
  28. */
  29. #define NUM_PEERS 2
  30. #define NUM_ISSUER 1
  31. #define NUM_EXPERIMENTS 2
  32. #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (5 * NUM_PEERS) + 20)
  33. /**
  34. * Array of peers
  35. */
  36. static struct GNUNET_TESTBED_Peer **peers;
  37. /**
  38. * Operation handle
  39. */
  40. static struct GNUNET_TESTBED_Operation *op;
  41. /**
  42. * Shutdown task
  43. */
  44. static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
  45. /**
  46. * Testing result
  47. */
  48. static int result;
  49. /**
  50. * Counter for counting overlay connections
  51. */
  52. static unsigned int overlay_connects;
  53. /**
  54. * Information we track for a peer in the testbed.
  55. */
  56. struct ExperimentationPeer
  57. {
  58. /**
  59. * Handle with testbed.
  60. */
  61. struct GNUNET_TESTBED_Peer *daemon;
  62. /**
  63. * Testbed operation to connect to statistics service
  64. */
  65. struct GNUNET_TESTBED_Operation *stat_op;
  66. /**
  67. * Handle to the statistics service
  68. */
  69. struct GNUNET_STATISTICS_Handle *sh;
  70. unsigned int active_nodes;
  71. unsigned int requested_nodes;
  72. unsigned int inactive_nodes;
  73. unsigned int issuer;
  74. unsigned int experiments_active;
  75. unsigned int experiments_outbound_running;
  76. unsigned int experiments_inbound_running;
  77. };
  78. struct ExperimentationPeer bp_slaves[NUM_PEERS];
  79. /**
  80. * Shutdown nicely
  81. *
  82. * @param cls NULL
  83. * @param tc the task context
  84. */
  85. static void
  86. do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  87. {
  88. unsigned int peer;
  89. shutdown_task = GNUNET_SCHEDULER_NO_TASK;
  90. for (peer = 0; peer < NUM_PEERS; peer++)
  91. {
  92. if (NULL != bp_slaves[peer].stat_op)
  93. GNUNET_TESTBED_operation_done (bp_slaves[peer].stat_op);
  94. bp_slaves[peer].stat_op = NULL;
  95. }
  96. if (NULL != op)
  97. {
  98. GNUNET_TESTBED_operation_done (op);
  99. op = NULL;
  100. }
  101. GNUNET_SCHEDULER_shutdown ();
  102. }
  103. /**
  104. * Controller event callback
  105. *
  106. * @param cls NULL
  107. * @param event the controller event
  108. */
  109. static void
  110. controller_event_cb (void *cls,
  111. const struct GNUNET_TESTBED_EventInformation *event)
  112. {
  113. switch (event->type)
  114. {
  115. case GNUNET_TESTBED_ET_CONNECT:
  116. overlay_connects++;
  117. if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
  118. {
  119. result = GNUNET_OK;
  120. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
  121. if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
  122. {
  123. GNUNET_SCHEDULER_cancel (shutdown_task);
  124. }
  125. shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
  126. }
  127. break;
  128. case GNUNET_TESTBED_ET_OPERATION_FINISHED:
  129. break;
  130. default:
  131. GNUNET_break (0);
  132. result = GNUNET_SYSERR;
  133. GNUNET_SCHEDULER_cancel (shutdown_task);
  134. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  135. }
  136. }
  137. static void
  138. check_end ()
  139. {
  140. static int last_in_experiments_value = 0;
  141. static int last_out_experiments_value = 0;
  142. unsigned int peer;
  143. unsigned int t_running_outbound_experiments = 0;
  144. unsigned int t_running_inbound_experiments = 0;
  145. for (peer = 0; peer < NUM_PEERS; peer++)
  146. {
  147. t_running_outbound_experiments += bp_slaves[peer].experiments_outbound_running;
  148. t_running_inbound_experiments += bp_slaves[peer].experiments_inbound_running;
  149. }
  150. //fprintf (stderr, "%u %u \n", t_running_outbound_experiments, t_running_inbound_experiments);
  151. if (last_in_experiments_value < t_running_inbound_experiments)
  152. fprintf (stderr, ".");
  153. last_in_experiments_value = t_running_inbound_experiments;
  154. if (last_out_experiments_value < t_running_outbound_experiments)
  155. fprintf (stderr, ".");
  156. last_out_experiments_value = t_running_outbound_experiments;
  157. if ((t_running_inbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)) &&
  158. (t_running_outbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)))
  159. {
  160. fprintf (stderr, "\n");
  161. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "All %u peers are running experiments\n", NUM_PEERS);
  162. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  163. }
  164. }
  165. /**
  166. * Callback function to process statistic values.
  167. *
  168. * @param cls struct StatsContext
  169. * @param subsystem name of subsystem that created the statistic
  170. * @param name the name of the datum
  171. * @param value the current value
  172. * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
  173. * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
  174. */
  175. static int
  176. stat_iterator (void *cls, const char *subsystem, const char *name,
  177. uint64_t value, int is_persistent)
  178. {
  179. struct ExperimentationPeer *peer = cls;
  180. if (0 == strcmp (name, "# experiments active"))
  181. {
  182. peer->experiments_active = value;
  183. }
  184. if (0 == strcmp (name, "# experiments outbound running"))
  185. {
  186. peer->experiments_outbound_running = value;
  187. }
  188. if (0 == strcmp (name, "# experiments inbound running"))
  189. {
  190. peer->experiments_inbound_running = value;
  191. }
  192. check_end ();
  193. return GNUNET_OK;
  194. }
  195. /**
  196. * Called after successfully opening a connection to a peer's statistics
  197. * service; we register statistics monitoring here.
  198. *
  199. * @param cls the callback closure from functions generating an operation
  200. * @param op the operation that has been finished
  201. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  202. * @param emsg error message in case the operation has failed; will be NULL if
  203. * operation has executed successfully.
  204. */
  205. static void
  206. stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
  207. void *ca_result, const char *emsg )
  208. {
  209. //struct GNUNET_STATISTICS_Handle *sh = ca_result;
  210. struct ExperimentationPeer *peer = cls;
  211. if (NULL != emsg)
  212. {
  213. GNUNET_break (0);
  214. return;
  215. }
  216. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
  217. (peer->sh, "experimentation", "# experiments active",
  218. stat_iterator, peer));
  219. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
  220. (peer->sh, "experimentation", "# experiments outbound running",
  221. stat_iterator, peer));
  222. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
  223. (peer->sh, "experimentation", "# experiments inbound running",
  224. stat_iterator, peer));
  225. }
  226. /**
  227. * Called to open a connection to the peer's statistics
  228. *
  229. * @param cls peer context
  230. * @param cfg configuration of the peer to connect to; will be available until
  231. * GNUNET_TESTBED_operation_done() is called on the operation returned
  232. * from GNUNET_TESTBED_service_connect()
  233. * @return service handle to return in 'op_result', NULL on error
  234. */
  235. static void *
  236. stat_connect_adapter (void *cls,
  237. const struct GNUNET_CONFIGURATION_Handle *cfg)
  238. {
  239. struct ExperimentationPeer *peer = cls;
  240. peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
  241. if (NULL == peer->sh)
  242. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
  243. return peer->sh;
  244. }
  245. /**
  246. * Called to disconnect from peer's statistics service
  247. *
  248. * @param cls peer context
  249. * @param op_result service handle returned from the connect adapter
  250. */
  251. static void
  252. stat_disconnect_adapter (void *cls, void *op_result)
  253. {
  254. struct ExperimentationPeer *peer = cls;
  255. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
  256. (peer->sh, "experimentation", "# experiments active",
  257. stat_iterator, peer));
  258. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
  259. (peer->sh, "experimentation", "# experiments outbound running",
  260. stat_iterator, peer));
  261. GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
  262. (peer->sh, "experimentation", "# experiments inbound running",
  263. stat_iterator, peer));
  264. GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
  265. peer->sh = NULL;
  266. }
  267. /**
  268. * Signature of a main function for a testcase.
  269. *
  270. * @param cls closure
  271. * @param h the run handle
  272. * @param num_peers number of peers in 'peers'
  273. * @param peers_ handle to peers run in the testbed
  274. * @param links_succeeded the number of overlay link connection attempts that
  275. * succeeded
  276. * @param links_failed the number of overlay link connection attempts that
  277. * failed
  278. */
  279. static void
  280. test_master (void *cls,
  281. struct GNUNET_TESTBED_RunHandle *h,
  282. unsigned int num_peers,
  283. struct GNUNET_TESTBED_Peer **peers_,
  284. unsigned int links_succeeded,
  285. unsigned int links_failed)
  286. {
  287. unsigned int peer;
  288. GNUNET_assert (NULL == cls);
  289. GNUNET_assert (NUM_PEERS == num_peers);
  290. GNUNET_assert (NULL != peers_);
  291. for (peer = 0; peer < num_peers; peer++)
  292. {
  293. GNUNET_assert (NULL != peers_[peer]);
  294. /* Connect to peer's statistic service */
  295. bp_slaves[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
  296. peers_[peer], "statistics",
  297. &stat_comp_cb, &bp_slaves[peer],
  298. &stat_connect_adapter,
  299. &stat_disconnect_adapter,
  300. &bp_slaves[peer]);
  301. }
  302. peers = peers_;
  303. overlay_connects = 0;
  304. op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
  305. NULL,
  306. NULL,
  307. GNUNET_TESTBED_TOPOLOGY_CLIQUE,
  308. /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
  309. /* NUM_PEERS, */
  310. GNUNET_TESTBED_TOPOLOGY_OPTION_END);
  311. GNUNET_assert (NULL != op);
  312. shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
  313. }
  314. /**
  315. * Main function
  316. */
  317. int
  318. main (int argc, char **argv)
  319. {
  320. uint64_t event_mask;
  321. result = GNUNET_SYSERR;
  322. event_mask = 0;
  323. event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
  324. event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  325. (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_run",
  326. "test_experimentation_clique.conf", NUM_PEERS,
  327. event_mask, &controller_event_cb, NULL,
  328. &test_master, NULL);
  329. if (GNUNET_OK != result)
  330. return 1;
  331. return 0;
  332. }
  333. /* end of test_experimentation_clique_run.c */