gnunet-ats-sim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-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 ats-tests/gnunet-ats-sim.c
  18. * @brief ats traffic simulator: this tool uses the ats-test library to setup a
  19. * topology and generate traffic between these peers. The traffic description
  20. * is loaded from a experiment description file
  21. * @author Christian Grothoff
  22. * @author Matthias Wachs
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_testbed_service.h"
  27. #include "gnunet_ats_service.h"
  28. #include "gnunet_core_service.h"
  29. #include "ats-testing.h"
  30. #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  31. static struct BenchmarkPeer *masters_p;
  32. static struct BenchmarkPeer *slaves_p;
  33. /**
  34. * cmd option -e: experiment file
  35. */
  36. static char *opt_exp_file;
  37. /**
  38. * cmd option -l: enable logging
  39. */
  40. static int opt_log;
  41. /**
  42. * cmd option -p: enable plots
  43. */
  44. static int opt_plot;
  45. /**
  46. * cmd option -v: verbose logs
  47. */
  48. static int opt_verbose;
  49. static struct GNUNET_SCHEDULER_Task *timeout_task;
  50. static struct Experiment *e;
  51. static struct LoggingHandle *l;
  52. static void
  53. evaluate (struct GNUNET_TIME_Relative duration_total)
  54. {
  55. int c_m;
  56. int c_s;
  57. unsigned int duration;
  58. struct BenchmarkPeer *mp;
  59. struct BenchmarkPartner *p;
  60. unsigned int b_sent_sec;
  61. double kb_sent_percent;
  62. unsigned int b_recv_sec;
  63. double kb_recv_percent;
  64. unsigned int rtt;
  65. duration = (duration_total.rel_value_us / (1000 * 1000));
  66. if (0 == duration)
  67. duration = 1;
  68. for (c_m = 0; c_m < e->num_masters; c_m++)
  69. {
  70. mp = &masters_p[c_m];
  71. fprintf (stderr,
  72. _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
  73. mp->no, mp->total_bytes_sent / 1024,
  74. duration,
  75. (mp->total_bytes_sent / 1024) / duration,
  76. mp->total_bytes_received / 1024,
  77. duration,
  78. (mp->total_bytes_received / 1024) / duration);
  79. for (c_s = 0; c_s < e->num_slaves; c_s++)
  80. {
  81. p = &mp->partners[c_s];
  82. b_sent_sec = 0;
  83. b_recv_sec = 0;
  84. kb_sent_percent = 0.0;
  85. kb_recv_percent = 0.0;
  86. rtt = 0;
  87. if (duration > 0)
  88. {
  89. b_sent_sec = p->bytes_sent / duration;
  90. b_recv_sec = p->bytes_received / duration;
  91. }
  92. if (mp->total_bytes_sent > 0)
  93. kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
  94. if (mp->total_bytes_received > 0)
  95. kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
  96. if (1000 * p->messages_sent > 0)
  97. rtt = p->total_app_rtt / (1000 * p->messages_sent);
  98. fprintf (stderr,
  99. "%c Master [%u] -> Slave [%u]: sent %u Bips (%.2f %%), received %u Bips (%.2f %%)\n",
  100. (mp->pref_partner == p->dest) ? '*' : ' ',
  101. mp->no, p->dest->no,
  102. b_sent_sec, kb_sent_percent,
  103. b_recv_sec, kb_recv_percent);
  104. fprintf (stderr,
  105. "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
  106. (mp->pref_partner == p->dest) ? '*' : ' ',
  107. mp->no, p->dest->no, rtt);
  108. }
  109. }
  110. }
  111. static void
  112. do_shutdown (void *cls)
  113. {
  114. fprintf (stderr, "Shutdown\n");
  115. if (NULL != timeout_task)
  116. {
  117. GNUNET_SCHEDULER_cancel (timeout_task);
  118. timeout_task = NULL;
  119. }
  120. if (NULL != l)
  121. {
  122. GNUNET_ATS_TEST_logging_stop (l);
  123. GNUNET_ATS_TEST_logging_clean_up (l);
  124. l = NULL;
  125. }
  126. /* Stop traffic generation */
  127. GNUNET_ATS_TEST_generate_traffic_stop_all();
  128. /* Stop all preference generations */
  129. GNUNET_ATS_TEST_generate_preferences_stop_all ();
  130. if (NULL != e)
  131. {
  132. GNUNET_ATS_TEST_experimentation_stop (e);
  133. e = NULL;
  134. }
  135. GNUNET_ATS_TEST_shutdown_topology ();
  136. }
  137. static void
  138. do_timeout (void *cls)
  139. {
  140. timeout_task = NULL;
  141. GNUNET_SCHEDULER_shutdown ();
  142. }
  143. static void
  144. log_request__cb (void *cls,
  145. const struct GNUNET_HELLO_Address *address,
  146. int address_active,
  147. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  148. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  149. const struct GNUNET_ATS_Properties *ats)
  150. {
  151. if (NULL != l)
  152. {
  153. //GNUNET_break (0);
  154. //GNUNET_ATS_TEST_logging_now (l);
  155. }
  156. }
  157. static void
  158. experiment_done_cb (struct Experiment *e,
  159. struct GNUNET_TIME_Relative duration,
  160. int success)
  161. {
  162. if (GNUNET_OK == success)
  163. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  164. "Experiment done successful in %s\n",
  165. GNUNET_STRINGS_relative_time_to_string (duration,
  166. GNUNET_YES));
  167. else
  168. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
  169. /* Stop logging */
  170. GNUNET_ATS_TEST_logging_stop (l);
  171. /* Stop traffic generation */
  172. GNUNET_ATS_TEST_generate_traffic_stop_all();
  173. /* Stop all preference generations */
  174. GNUNET_ATS_TEST_generate_preferences_stop_all ();
  175. evaluate (duration);
  176. if (opt_log)
  177. GNUNET_ATS_TEST_logging_write_to_file(l, opt_exp_file, opt_plot);
  178. GNUNET_SCHEDULER_shutdown ();
  179. }
  180. static void
  181. episode_done_cb (struct Episode *ep)
  182. {
  183. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  184. "Episode %u done\n",
  185. ep->id);
  186. }
  187. static void
  188. topology_setup_done (void *cls,
  189. struct BenchmarkPeer *masters,
  190. struct BenchmarkPeer *slaves)
  191. {
  192. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  193. "Topology setup complete!\n");
  194. masters_p = masters;
  195. slaves_p = slaves;
  196. l = GNUNET_ATS_TEST_logging_start (e->log_freq,
  197. e->name,
  198. masters_p,
  199. e->num_masters, e->num_slaves,
  200. opt_verbose);
  201. GNUNET_ATS_TEST_experimentation_run (e,
  202. &episode_done_cb,
  203. &experiment_done_cb);
  204. /*
  205. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  206. GNUNET_ATS_TEST_TG_CONSTANT, 1, 1, GNUNET_TIME_UNIT_SECONDS,
  207. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  208. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  209. */
  210. /*
  211. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  212. GNUNET_ATS_TEST_TG_LINEAR, 1, 50,
  213. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
  214. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  215. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  216. */
  217. /*
  218. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  219. GNUNET_ATS_TEST_TG_RANDOM, 1, 50,
  220. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
  221. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  222. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  223. */
  224. /*
  225. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  226. GNUNET_ATS_TEST_TG_SINUS, 10, 5,
  227. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
  228. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  229. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  230. */
  231. #if 0
  232. int c_m;
  233. int c_s;
  234. for (c_m = 0; c_m < e->num_masters; c_m++)
  235. {
  236. for (c_s = 0; c_s < e->num_slaves; c_s++)
  237. {
  238. /* Generate maximum traffic to all peers */
  239. /* Example: Generate traffic with constant 10,000 Bytes/s */
  240. GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
  241. &masters[c_m].partners[c_s],
  242. GNUNET_ATS_TEST_TG_CONSTANT,
  243. 10000,
  244. GNUNET_TIME_UNIT_FOREVER_REL);
  245. /* Example: Generate traffic with an increasing rate from 1000 to 2000
  246. * Bytes/s with in a minute */
  247. GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
  248. &masters[c_m].partners[c_s],
  249. GNUNET_ATS_TEST_TG_LINEAR,
  250. 1000,
  251. 2000,
  252. GNUNET_TIME_UNIT_MINUTES,
  253. GNUNET_TIME_UNIT_FOREVER_REL);
  254. /* Example: Generate traffic with a random rate between 1000 to 2000
  255. * Bytes/s */
  256. GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
  257. &masters[c_m].partners[c_s],
  258. GNUNET_ATS_TEST_TG_RANDOM,
  259. 1000,
  260. 2000,
  261. GNUNET_TIME_UNIT_FOREVER_REL,
  262. GNUNET_TIME_UNIT_FOREVER_REL);
  263. /* Example: Generate traffic with a sinus form, a base rate of
  264. * 1000 Bytes/s, an amplitude of (max-base), and a period of 1 minute */
  265. GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
  266. &masters[c_m].partners[c_s],
  267. GNUNET_ATS_TEST_TG_SINUS,
  268. 1000,
  269. 2000,
  270. GNUNET_TIME_UNIT_MINUTES,
  271. GNUNET_TIME_UNIT_FOREVER_REL);
  272. }
  273. }
  274. #endif
  275. timeout_task
  276. = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
  277. e->max_duration),
  278. &do_timeout,
  279. NULL);
  280. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  281. }
  282. static void
  283. parse_args (int argc, char *argv[])
  284. {
  285. int c;
  286. opt_exp_file = NULL;
  287. opt_log = GNUNET_NO;
  288. opt_plot = GNUNET_NO;
  289. for (c = 0; c < argc; c++)
  290. {
  291. if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
  292. {
  293. GNUNET_free_non_null (opt_exp_file);
  294. opt_exp_file = GNUNET_strdup ( argv[c + 1]);
  295. }
  296. if (0 == strcmp (argv[c], "-l"))
  297. {
  298. opt_log = GNUNET_YES;
  299. }
  300. if (0 == strcmp (argv[c], "-p"))
  301. {
  302. opt_plot = GNUNET_YES;
  303. }
  304. if (0 == strcmp (argv[c], "-v"))
  305. {
  306. opt_verbose = GNUNET_YES;
  307. }
  308. }
  309. }
  310. int
  311. main (int argc, char *argv[])
  312. {
  313. GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
  314. parse_args (argc, argv);
  315. if (NULL == opt_exp_file )
  316. {
  317. fprintf (stderr, "No experiment given...\n");
  318. return 1;
  319. }
  320. fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file );
  321. e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
  322. if (NULL == e)
  323. {
  324. fprintf (stderr, "Invalid experiment\n");
  325. return 1;
  326. }
  327. if (0 == e->num_episodes)
  328. {
  329. fprintf (stderr, "No episodes included\n");
  330. return 1;
  331. }
  332. /* Setup a topology with */
  333. GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
  334. e->num_slaves,
  335. e->num_masters,
  336. GNUNET_NO,
  337. &topology_setup_done,
  338. NULL,
  339. &log_request__cb);
  340. GNUNET_free (opt_exp_file);
  341. return 0;
  342. }
  343. /* end of file gnunet-ats-sim.c */