gnunet-testbed-profiler.c 8.0 KB

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