test_ats_solver_convergence.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-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 ats/test_ats_solver_add_address.c
  19. * @brief solver test: add address, request address and wait for suggests, write data to file
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. * @author Fabian Oehlmann
  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 "test_ats_api_common.h"
  29. /**
  30. * Timeout task
  31. */
  32. static struct GNUNET_SCHEDULER_Task * end_task;
  33. /**
  34. * Statistics handle
  35. */
  36. struct GNUNET_STATISTICS_Handle *stats;
  37. /**
  38. * Scheduling handle
  39. */
  40. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  41. /**
  42. * Connectivity handle
  43. */
  44. static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
  45. /**
  46. * Return value
  47. */
  48. static int ret;
  49. /**
  50. * Test address
  51. */
  52. static struct Test_Address test_addr;
  53. /**
  54. * Test peer
  55. */
  56. static struct PeerContext p;
  57. /**
  58. * HELLO address
  59. */
  60. struct GNUNET_HELLO_Address test_hello_address;
  61. /**
  62. * Session
  63. */
  64. static void *test_session;
  65. /**
  66. * Test ats info
  67. */
  68. struct GNUNET_ATS_Information test_ats_info[2];
  69. /**
  70. * Test ats count
  71. */
  72. uint32_t test_ats_count;
  73. /**
  74. * Seconds to run the test
  75. */
  76. static unsigned int seconds;
  77. /**
  78. * When the test starts
  79. */
  80. static struct GNUNET_TIME_Absolute time_start;
  81. /**
  82. * Whether to write a data file
  83. */
  84. static int write_data_file;
  85. /**
  86. * File name
  87. */
  88. static char *data_file_name;
  89. /**
  90. * Run name
  91. */
  92. static char *run_name;
  93. static int
  94. stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
  95. int is_persistent);
  96. static void
  97. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  98. {
  99. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
  100. if (end_task == NULL)
  101. {
  102. GNUNET_SCHEDULER_cancel (end_task);
  103. end_task = NULL;
  104. }
  105. if (NULL != sched_ats)
  106. {
  107. GNUNET_ATS_scheduling_done (sched_ats);
  108. sched_ats = NULL;
  109. }
  110. if (NULL != connect_ats)
  111. {
  112. GNUNET_ATS_connectivity_done (connect_ats);
  113. connect_ats = NULL;
  114. }
  115. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  116. if (NULL != stats)
  117. {
  118. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  119. stats = NULL;
  120. }
  121. /* Close data file */
  122. if (write_data_file)
  123. {
  124. GNUNET_free_non_null(data_file_name);
  125. }
  126. free_test_address (&test_addr);
  127. ret = 0;
  128. }
  129. static void
  130. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  131. {
  132. end (NULL, NULL);
  133. ret = GNUNET_SYSERR;
  134. }
  135. static void
  136. address_suggest_cb (void *cls,
  137. const struct GNUNET_PeerIdentity *peer,
  138. const struct GNUNET_HELLO_Address *address,
  139. struct Session *session,
  140. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  141. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  142. const struct GNUNET_ATS_Information *atsi,
  143. uint32_t ats_count)
  144. {
  145. struct GNUNET_TIME_Relative time_delta;
  146. char *data;
  147. struct GNUNET_DISK_FileHandle *data_file_handle;
  148. GNUNET_assert (NULL != address);
  149. GNUNET_assert (NULL == session);
  150. GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
  151. GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
  152. time_delta = GNUNET_TIME_absolute_get_difference(time_start, GNUNET_TIME_absolute_get());
  153. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  154. "Received suggestion for peer '%s': IN %u kb/s - OUT %u kb/s\n",
  155. GNUNET_i2s (&address->peer),
  156. (unsigned int) ntohl (bandwidth_in.value__)/1024,
  157. (unsigned int) ntohl (bandwidth_out.value__)/1024);
  158. if (write_data_file)
  159. {
  160. GNUNET_asprintf(&data,"%f\tIN %u\tOUT %u\n",
  161. (double) time_delta.rel_value_us / 1000000.,
  162. ntohl(bandwidth_in.value__)/1024,
  163. ntohl(bandwidth_out.value__)/1024);
  164. data_file_handle = GNUNET_DISK_file_open (data_file_name,
  165. GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_APPEND,
  166. GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
  167. if (NULL == data_file_handle)
  168. {
  169. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  170. "Cannot write data to file `%s'\n",
  171. data_file_name);
  172. }
  173. else
  174. {
  175. if (GNUNET_SYSERR == GNUNET_DISK_file_write(data_file_handle, data, strlen(data)))
  176. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  177. "Cannot write data to file `%s'\n",
  178. data_file_name);
  179. if (GNUNET_SYSERR == GNUNET_DISK_file_close (data_file_handle))
  180. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  181. "Cannot close log file '%s'\n",
  182. data_file_name);
  183. }
  184. GNUNET_free(data);
  185. }
  186. }
  187. static int
  188. stat_cb(void *cls, const char *subsystem,
  189. const char *name, uint64_t value,
  190. int is_persistent)
  191. {
  192. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  193. "ATS statistics: `%s' `%s' %llu\n",
  194. subsystem,name,
  195. (unsigned long long) value);
  196. GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
  197. return GNUNET_OK;
  198. }
  199. static void
  200. run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
  201. struct GNUNET_TESTING_Peer *peer)
  202. {
  203. struct GNUNET_DISK_FileHandle *data_file_handle;
  204. stats = GNUNET_STATISTICS_create ("ats", mycfg);
  205. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  206. connect_ats = GNUNET_ATS_connectivity_init (mycfg);
  207. /* Connect to ATS scheduling */
  208. sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
  209. if (sched_ats == NULL)
  210. {
  211. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
  212. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  213. return;
  214. }
  215. /* Create or truncate file */
  216. if (write_data_file)
  217. {
  218. GNUNET_asprintf (&data_file_name, "test_convergence_%s_s%d.data", run_name, seconds);
  219. data_file_handle = GNUNET_DISK_file_open (data_file_name,
  220. GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
  221. GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
  222. if (NULL == data_file_handle)
  223. {
  224. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open data file\n");
  225. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  226. return;
  227. }
  228. if (GNUNET_SYSERR == GNUNET_DISK_file_close (data_file_handle))
  229. {
  230. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file '%s'\n",
  231. data_file_name);
  232. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  233. return;
  234. }
  235. }
  236. /* Set up peer */
  237. memset (&p.id, '1', sizeof (p.id));
  238. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  239. GNUNET_i2s_full(&p.id));
  240. /* Prepare ATS Information */
  241. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  242. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  243. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  244. test_ats_info[1].value = htonl(1);
  245. test_ats_count = 2;
  246. /* Adding address without session */
  247. test_session = NULL;
  248. create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
  249. test_hello_address.peer = p.id;
  250. test_hello_address.transport_name = test_addr.plugin;
  251. test_hello_address.address = test_addr.addr;
  252. test_hello_address.address_length = test_addr.addr_len;
  253. /* Adding address */
  254. GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
  255. time_start = GNUNET_TIME_absolute_get();
  256. end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_relative_get_second_(), seconds), &end, NULL);
  257. }
  258. void
  259. test_run (void *cls, char *const *args,
  260. const char *cfgfile,
  261. const struct GNUNET_CONFIGURATION_Handle *
  262. cfg)
  263. {
  264. char *sep;
  265. char *src_filename = GNUNET_strdup (__FILE__);
  266. char *test_filename = cls;
  267. char *config_file = "none";
  268. char *solver;
  269. ret = 0;
  270. if (NULL == (sep = (strstr (src_filename,".c"))))
  271. {
  272. GNUNET_break (0);
  273. ret = -1;
  274. //return -1;
  275. }
  276. sep[0] = '\0';
  277. if (NULL != (sep = strstr (test_filename, ".exe")))
  278. sep[0] = '\0';
  279. if (NULL == (solver = strstr (test_filename, src_filename)))
  280. {
  281. GNUNET_break (0);
  282. ret = -1;
  283. }
  284. solver += strlen (src_filename) +1;
  285. if (0 == strcmp(solver, "proportional"))
  286. {
  287. config_file = "test_ats_solver_proportional.conf";
  288. }
  289. else if (0 == strcmp(solver, "mlp"))
  290. {
  291. config_file = "test_ats_solver_mlp.conf";
  292. }
  293. else if ((0 == strcmp(solver, "ril")))
  294. {
  295. config_file = "test_ats_solver_ril.conf";
  296. }
  297. else
  298. {
  299. GNUNET_break (0);
  300. GNUNET_free (src_filename);
  301. ret = 1;
  302. }
  303. GNUNET_free (src_filename);
  304. if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
  305. config_file, &run, NULL ))
  306. ret = GNUNET_SYSERR;
  307. }
  308. int
  309. main (int argc, char *argv[])
  310. {
  311. seconds = 5;
  312. run_name = NULL;
  313. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  314. { 's', "seconds", NULL,
  315. gettext_noop ("seconds to run the test"),
  316. 1, &GNUNET_GETOPT_set_uint, &seconds },
  317. { 'd', "data-file", NULL,
  318. gettext_noop ("generate data file"),
  319. 0, &GNUNET_GETOPT_set_one, &write_data_file},
  320. { 'r', "run-name", "NAME",
  321. gettext_noop ("will be part of the data file name"),
  322. 1, &GNUNET_GETOPT_set_string, &run_name},
  323. GNUNET_GETOPT_OPTION_END
  324. };
  325. GNUNET_PROGRAM_run2 (argc, argv, argv[0], NULL, options, &test_run, argv[0], GNUNET_YES);
  326. return ret;
  327. }
  328. /* end of file test_ats_solver_convergence.c */