gnunet-ats-sim.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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, \
  31. 10)
  32. static struct BenchmarkPeer *masters_p;
  33. static struct BenchmarkPeer *slaves_p;
  34. /**
  35. * cmd option -e: experiment file
  36. */
  37. static char *opt_exp_file;
  38. /**
  39. * cmd option -l: enable logging
  40. */
  41. static int opt_log;
  42. /**
  43. * cmd option -p: enable plots
  44. */
  45. static int opt_plot;
  46. /**
  47. * cmd option -v: verbose logs
  48. */
  49. static int opt_verbose;
  50. static struct GNUNET_SCHEDULER_Task *timeout_task;
  51. static struct Experiment *e;
  52. static struct LoggingHandle *l;
  53. static void
  54. evaluate (struct GNUNET_TIME_Relative duration_total)
  55. {
  56. int c_m;
  57. int c_s;
  58. unsigned int duration;
  59. struct BenchmarkPeer *mp;
  60. struct BenchmarkPartner *p;
  61. unsigned int b_sent_sec;
  62. double kb_sent_percent;
  63. unsigned int b_recv_sec;
  64. double kb_recv_percent;
  65. unsigned int rtt;
  66. duration = (duration_total.rel_value_us / (1000 * 1000));
  67. if (0 == duration)
  68. duration = 1;
  69. for (c_m = 0; c_m < e->num_masters; c_m++)
  70. {
  71. mp = &masters_p[c_m];
  72. fprintf (stderr,
  73. _ (
  74. "Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
  75. mp->no, mp->total_bytes_sent / 1024,
  76. duration,
  77. (mp->total_bytes_sent / 1024) / duration,
  78. mp->total_bytes_received / 1024,
  79. duration,
  80. (mp->total_bytes_received / 1024) / duration);
  81. for (c_s = 0; c_s < e->num_slaves; c_s++)
  82. {
  83. p = &mp->partners[c_s];
  84. b_sent_sec = 0;
  85. b_recv_sec = 0;
  86. kb_sent_percent = 0.0;
  87. kb_recv_percent = 0.0;
  88. rtt = 0;
  89. if (duration > 0)
  90. {
  91. b_sent_sec = p->bytes_sent / duration;
  92. b_recv_sec = p->bytes_received / duration;
  93. }
  94. if (mp->total_bytes_sent > 0)
  95. kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
  96. if (mp->total_bytes_received > 0)
  97. kb_recv_percent = ((double) p->bytes_received * 100)
  98. / mp->total_bytes_received;
  99. if (1000 * p->messages_sent > 0)
  100. rtt = p->total_app_rtt / (1000 * p->messages_sent);
  101. fprintf (stderr,
  102. "%c Master [%u] -> Slave [%u]: sent %u Bips (%.2f %%), received %u Bips (%.2f %%)\n",
  103. (mp->pref_partner == p->dest) ? '*' : ' ',
  104. mp->no, p->dest->no,
  105. b_sent_sec, kb_sent_percent,
  106. b_recv_sec, kb_recv_percent);
  107. fprintf (stderr,
  108. "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
  109. (mp->pref_partner == p->dest) ? '*' : ' ',
  110. mp->no, p->dest->no, rtt);
  111. }
  112. }
  113. }
  114. static void
  115. do_shutdown (void *cls)
  116. {
  117. fprintf (stderr, "Shutdown\n");
  118. if (NULL != timeout_task)
  119. {
  120. GNUNET_SCHEDULER_cancel (timeout_task);
  121. timeout_task = NULL;
  122. }
  123. if (NULL != l)
  124. {
  125. GNUNET_ATS_TEST_logging_stop (l);
  126. GNUNET_ATS_TEST_logging_clean_up (l);
  127. l = NULL;
  128. }
  129. /* Stop traffic generation */
  130. GNUNET_ATS_TEST_generate_traffic_stop_all ();
  131. /* Stop all preference generations */
  132. GNUNET_ATS_TEST_generate_preferences_stop_all ();
  133. if (NULL != e)
  134. {
  135. GNUNET_ATS_TEST_experimentation_stop (e);
  136. e = NULL;
  137. }
  138. GNUNET_ATS_TEST_shutdown_topology ();
  139. }
  140. static void
  141. do_timeout (void *cls)
  142. {
  143. timeout_task = NULL;
  144. GNUNET_SCHEDULER_shutdown ();
  145. }
  146. static void
  147. log_request__cb (void *cls,
  148. const struct GNUNET_HELLO_Address *address,
  149. int address_active,
  150. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  151. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  152. const struct GNUNET_ATS_Properties *ats)
  153. {
  154. if (NULL != l)
  155. {
  156. // GNUNET_break (0);
  157. // GNUNET_ATS_TEST_logging_now (l);
  158. }
  159. }
  160. static void
  161. experiment_done_cb (struct Experiment *e,
  162. struct GNUNET_TIME_Relative duration,
  163. int success)
  164. {
  165. if (GNUNET_OK == success)
  166. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  167. "Experiment done successful in %s\n",
  168. GNUNET_STRINGS_relative_time_to_string (duration,
  169. GNUNET_YES));
  170. else
  171. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
  172. /* Stop logging */
  173. GNUNET_ATS_TEST_logging_stop (l);
  174. /* Stop traffic generation */
  175. GNUNET_ATS_TEST_generate_traffic_stop_all ();
  176. /* Stop all preference generations */
  177. GNUNET_ATS_TEST_generate_preferences_stop_all ();
  178. evaluate (duration);
  179. if (opt_log)
  180. GNUNET_ATS_TEST_logging_write_to_file (l, opt_exp_file, opt_plot);
  181. GNUNET_SCHEDULER_shutdown ();
  182. }
  183. static void
  184. episode_done_cb (struct Episode *ep)
  185. {
  186. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  187. "Episode %u done\n",
  188. ep->id);
  189. }
  190. static void
  191. topology_setup_done (void *cls,
  192. struct BenchmarkPeer *masters,
  193. struct BenchmarkPeer *slaves)
  194. {
  195. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  196. "Topology setup complete!\n");
  197. masters_p = masters;
  198. slaves_p = slaves;
  199. l = GNUNET_ATS_TEST_logging_start (e->log_freq,
  200. e->name,
  201. masters_p,
  202. e->num_masters, e->num_slaves,
  203. opt_verbose);
  204. GNUNET_ATS_TEST_experimentation_run (e,
  205. &episode_done_cb,
  206. &experiment_done_cb);
  207. /*
  208. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  209. GNUNET_ATS_TEST_TG_CONSTANT, 1, 1, GNUNET_TIME_UNIT_SECONDS,
  210. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  211. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  212. *//*
  213. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  214. GNUNET_ATS_TEST_TG_LINEAR, 1, 50,
  215. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
  216. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  217. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  218. *//*
  219. GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
  220. GNUNET_ATS_TEST_TG_RANDOM, 1, 50,
  221. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
  222. GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
  223. GNUNET_ATS_PREFERENCE_BANDWIDTH);
  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 (
  277. GNUNET_TIME_UNIT_MINUTES,
  278. e->max_duration),
  279. &do_timeout,
  280. NULL);
  281. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  282. }
  283. static void
  284. parse_args (int argc, char *argv[])
  285. {
  286. int c;
  287. opt_exp_file = NULL;
  288. opt_log = GNUNET_NO;
  289. opt_plot = GNUNET_NO;
  290. for (c = 0; c < argc; c++)
  291. {
  292. if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
  293. {
  294. GNUNET_free (opt_exp_file);
  295. opt_exp_file = GNUNET_strdup (argv[c + 1]);
  296. }
  297. if (0 == strcmp (argv[c], "-l"))
  298. {
  299. opt_log = GNUNET_YES;
  300. }
  301. if (0 == strcmp (argv[c], "-p"))
  302. {
  303. opt_plot = GNUNET_YES;
  304. }
  305. if (0 == strcmp (argv[c], "-v"))
  306. {
  307. opt_verbose = GNUNET_YES;
  308. }
  309. }
  310. }
  311. int
  312. main (int argc, char *argv[])
  313. {
  314. GNUNET_log_setup ("gnunet-ats-sim", "INFO", NULL);
  315. parse_args (argc, argv);
  316. if (NULL == opt_exp_file)
  317. {
  318. fprintf (stderr, "No experiment given...\n");
  319. return 1;
  320. }
  321. fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file);
  322. e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
  323. if (NULL == e)
  324. {
  325. fprintf (stderr, "Invalid experiment\n");
  326. return 1;
  327. }
  328. if (0 == e->num_episodes)
  329. {
  330. fprintf (stderr, "No episodes included\n");
  331. return 1;
  332. }
  333. /* Setup a topology with */
  334. GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
  335. e->num_slaves,
  336. e->num_masters,
  337. GNUNET_NO,
  338. &topology_setup_done,
  339. NULL,
  340. &log_request__cb);
  341. GNUNET_free (opt_exp_file);
  342. return 0;
  343. }
  344. /* end of file gnunet-ats-sim.c */