gnunet-testbed-profiler.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008--2013 GNUnet e.V.
  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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file testbed/gnunet-testbed-profiler.c
  19. * @brief Profiling driver for the testbed.
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testbed_service.h"
  25. #include "testbed_api_hosts.h"
  26. /**
  27. * Generic loggins shorthand
  28. */
  29. #define LOG(kind,...) \
  30. GNUNET_log (kind, __VA_ARGS__)
  31. /**
  32. * Handle to global configuration
  33. */
  34. struct GNUNET_CONFIGURATION_Handle *cfg;
  35. /**
  36. * Peer linking - topology operation
  37. */
  38. struct GNUNET_TESTBED_Operation *topology_op;
  39. /**
  40. * Name of the file with the hosts to run the test over (configuration option).
  41. * It will be NULL if ENABLE_LL is set
  42. */
  43. static char *hosts_file;
  44. /**
  45. * Abort task identifier
  46. */
  47. static struct GNUNET_SCHEDULER_Task *abort_task;
  48. /**
  49. * Global event mask for all testbed events
  50. */
  51. uint64_t event_mask;
  52. /**
  53. * Number of peers to be started by the profiler
  54. */
  55. static unsigned int num_peers;
  56. /**
  57. * Number of timeout failures to tolerate
  58. */
  59. static unsigned int num_cont_fails;
  60. /**
  61. * Continuous failures during overlay connect operations
  62. */
  63. static unsigned int cont_fails;
  64. /**
  65. * Links which are successfully established
  66. */
  67. static unsigned int established_links;
  68. /**
  69. * Links which are not successfully established
  70. */
  71. static unsigned int failed_links;
  72. /**
  73. * Global testing status
  74. */
  75. static int result;
  76. /**
  77. * Are we running non interactively
  78. */
  79. static int noninteractive;
  80. /**
  81. * Shutdown nicely
  82. *
  83. * @param cls NULL
  84. */
  85. static void
  86. do_shutdown (void *cls)
  87. {
  88. if (NULL != abort_task)
  89. {
  90. GNUNET_SCHEDULER_cancel (abort_task);
  91. abort_task = NULL;
  92. }
  93. if (NULL != cfg)
  94. {
  95. GNUNET_CONFIGURATION_destroy (cfg);
  96. cfg = NULL;
  97. }
  98. }
  99. /**
  100. * abort task to run on test timed out
  101. *
  102. * @param cls NULL
  103. */
  104. static void
  105. do_abort (void *cls)
  106. {
  107. abort_task = NULL;
  108. LOG (GNUNET_ERROR_TYPE_WARNING,
  109. "Aborting\n");
  110. result = GNUNET_SYSERR;
  111. GNUNET_SCHEDULER_shutdown ();
  112. }
  113. /**
  114. * Function to print summary about how many overlay links we have made and how
  115. * many failed
  116. */
  117. static void
  118. print_overlay_links_summary ()
  119. {
  120. static int printed_already;
  121. if (GNUNET_YES == printed_already)
  122. return;
  123. printed_already = GNUNET_YES;
  124. printf ("%u links succeeded\n", established_links);
  125. printf ("%u links failed due to timeouts\n", failed_links);
  126. }
  127. /**
  128. * Controller event callback
  129. *
  130. * @param cls NULL
  131. * @param event the controller event
  132. */
  133. static void
  134. controller_event_cb (void *cls,
  135. const struct GNUNET_TESTBED_EventInformation *event)
  136. {
  137. switch (event->type)
  138. {
  139. case GNUNET_TESTBED_ET_OPERATION_FINISHED:
  140. /* Control reaches here when a peer linking operation fails */
  141. if (NULL != event->details.operation_finished.emsg)
  142. {
  143. printf ("F");
  144. fflush (stdout);
  145. failed_links++;
  146. if (++cont_fails > num_cont_fails)
  147. {
  148. printf ("\nAborting due to very high failure rate\n");
  149. print_overlay_links_summary ();
  150. if (NULL != abort_task)
  151. GNUNET_SCHEDULER_cancel (abort_task);
  152. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
  153. return;
  154. }
  155. }
  156. break;
  157. case GNUNET_TESTBED_ET_CONNECT:
  158. {
  159. if (0 != cont_fails)
  160. cont_fails--;
  161. if (0 == established_links)
  162. printf ("Establishing links. Please wait\n");
  163. printf (".");
  164. fflush (stdout);
  165. established_links++;
  166. }
  167. break;
  168. default:
  169. GNUNET_break (0);
  170. }
  171. }
  172. /**
  173. * Signature of a main function for a testcase.
  174. *
  175. * @param cls closure
  176. * @param h the run handle
  177. * @param num_peers number of peers in 'peers'
  178. * @param peers handle to peers run in the testbed
  179. * @param links_succeeded the number of overlay link connection attempts that
  180. * succeeded
  181. * @param links_failed the number of overlay link
  182. */
  183. static void
  184. test_run (void *cls,
  185. struct GNUNET_TESTBED_RunHandle *h,
  186. unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
  187. unsigned int links_succeeded,
  188. unsigned int links_failed)
  189. {
  190. result = GNUNET_OK;
  191. fprintf (stdout, "\n");
  192. print_overlay_links_summary ();
  193. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  194. if (noninteractive)
  195. {
  196. GNUNET_SCHEDULER_cancel (abort_task);
  197. abort_task = NULL;
  198. return;
  199. }
  200. #if (!ENABLE_SUPERMUC)
  201. fprintf (stdout, "Testbed running, waiting for keystroke to shut down\n");
  202. fflush (stdout);
  203. (void) getc (stdin);
  204. #endif
  205. fprintf (stdout, "Shutting down. Please wait\n");
  206. fflush (stdout);
  207. GNUNET_SCHEDULER_shutdown ();
  208. }
  209. /**
  210. * Main function that will be run by the scheduler.
  211. *
  212. * @param cls closure
  213. * @param args remaining command-line arguments
  214. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  215. * @param config configuration
  216. */
  217. static void
  218. run (void *cls, char *const *args, const char *cfgfile,
  219. const struct GNUNET_CONFIGURATION_Handle *config)
  220. {
  221. if (0 == num_peers)
  222. {
  223. LOG (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
  224. num_peers);
  225. return;
  226. }
  227. cfg = GNUNET_CONFIGURATION_dup (config);
  228. event_mask = 0;
  229. event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
  230. event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  231. GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, controller_event_cb,
  232. NULL, &test_run, NULL);
  233. abort_task =
  234. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_abort,
  235. NULL);
  236. }
  237. /**
  238. * Main function.
  239. *
  240. * @return 0 on success
  241. */
  242. int
  243. main (int argc, char *const *argv)
  244. {
  245. struct GNUNET_GETOPT_CommandLineOption options[] = {
  246. GNUNET_GETOPT_option_uint ('p',
  247. "num-peers",
  248. "COUNT",
  249. gettext_noop ("create COUNT number of peers"),
  250. &num_peers),
  251. GNUNET_GETOPT_option_uint ('e',
  252. "num-errors",
  253. "COUNT",
  254. gettext_noop ("tolerate COUNT number of continious timeout failures"),
  255. &num_cont_fails),
  256. GNUNET_GETOPT_option_flag ('n',
  257. "non-interactive",
  258. gettext_noop ("run profiler in non-interactive mode where upon "
  259. "testbed setup the profiler does not wait for a "
  260. "keystroke but continues to run until a termination "
  261. "signal is received"),
  262. &noninteractive),
  263. #if !ENABLE_SUPERMUC
  264. GNUNET_GETOPT_option_string ('H',
  265. "hosts",
  266. "FILENAME",
  267. gettext_noop ("name of the file with the login information for the testbed"),
  268. &hosts_file),
  269. #endif
  270. GNUNET_GETOPT_OPTION_END
  271. };
  272. const char *binaryHelp = "gnunet-testbed-profiler [OPTIONS]";
  273. int ret;
  274. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  275. return 2;
  276. result = GNUNET_SYSERR;
  277. ret =
  278. GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler", binaryHelp,
  279. options, &run, NULL);
  280. GNUNET_free ((void *) argv);
  281. if (GNUNET_OK != ret)
  282. return ret;
  283. if (GNUNET_OK != result)
  284. return 1;
  285. return 0;
  286. }