perf_gnunet_service_fs_p2p_trust.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010, 2011 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 fs/perf_gnunet_service_fs_p2p_trust.c
  19. * @brief profile P2P routing trust mechanism. Creates
  20. * a clique of NUM_DAEMONS (i.e. 3) where two
  21. * peers share (seed) different files and download
  22. * them from each other while all the other peers
  23. * just "leach" those files. Ideally, the seeders
  24. * "learn" that they contribute (to each other),
  25. * and give the other seeder higher priority;
  26. * naturally, this only happens nicely for larger
  27. * files; finally, once the seeders are done, the
  28. * leachers should see fast download rates as well.
  29. * @author Christian Grothoff
  30. *
  31. * Sample output:
  32. * - 10 MB, 3 peers, with delays:
  33. * Download speed of type `seeder 1' was 757 KiB/s
  34. * Download speed of type `seeder 2' was 613 KiB/s
  35. * Download speed of type `leach` was 539 KiB/s
  36. *
  37. * - 10 MB, 3 peers, without delays:
  38. * Download speed of type `seeder 1' was 1784 KiB/s
  39. * Download speed of type `seeder 2' was 1604 KiB/s
  40. * Download speed of type `leach` was 1384 KiB/s
  41. */
  42. #include "platform.h"
  43. #include "fs_test_lib.h"
  44. #include "gnunet_testing_lib.h"
  45. #define VERBOSE GNUNET_NO
  46. /**
  47. * File-size we use for testing.
  48. */
  49. #define FILESIZE (1024 * 1024 * 10)
  50. /**
  51. * How long until we give up on transmitting the message?
  52. */
  53. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
  54. /**
  55. * Number of daemons in clique, must be at least 3 (!).
  56. */
  57. #define NUM_DAEMONS 3
  58. /**
  59. * Seed for first file on offer.
  60. */
  61. #define SEED1 42
  62. /**
  63. * Seed for second file on offer.
  64. */
  65. #define SEED2 43
  66. static struct GNUNET_FS_TestDaemon *daemons[NUM_DAEMONS];
  67. static int ok;
  68. static struct GNUNET_TIME_Absolute start_time;
  69. static const char *progname;
  70. static struct GNUNET_FS_Uri *uri1;
  71. static struct GNUNET_FS_Uri *uri2;
  72. static void
  73. do_stop (void *cls,
  74. const struct GNUNET_SCHEDULER_TaskContext *tc)
  75. {
  76. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS,
  77. daemons);
  78. }
  79. /**
  80. * Master context for 'stat_run'.
  81. */
  82. struct StatMaster
  83. {
  84. struct GNUNET_STATISTICS_Handle *stat;
  85. unsigned int daemon;
  86. unsigned int value;
  87. };
  88. struct StatValues
  89. {
  90. const char *subsystem;
  91. const char *name;
  92. };
  93. /**
  94. * Statistics we print out.
  95. */
  96. static struct StatValues stats[] =
  97. {
  98. { "fs", "# artificial delays introduced (ms)"},
  99. { "fs", "# queries forwarded"},
  100. { "fs", "# replies received and matched"},
  101. { "fs", "# results found locally"},
  102. { "fs", "# requests forwarded due to high load"},
  103. { "fs", "# requests done for free (low load)"},
  104. { "fs", "# requests dropped, priority insufficient"},
  105. { "fs", "# requests done for a price (normal load)"},
  106. { "fs", "# requests dropped by datastore (queue length limit)"},
  107. { "fs", "# P2P searches received"},
  108. { "fs", "# P2P searches discarded (queue length bound)"},
  109. { "fs", "# replies received for local clients"},
  110. { "fs", "# queries retransmitted to same target"},
  111. { "core", "# bytes decrypted"},
  112. { "core", "# bytes encrypted"},
  113. { "core", "# discarded CORE_SEND requests"},
  114. { "core", "# discarded lower priority CORE_SEND requests"},
  115. { "transport", "# bytes received via TCP"},
  116. { "transport", "# bytes transmitted via TCP"},
  117. { "datacache", "# bytes stored"},
  118. { NULL, NULL}
  119. };
  120. /**
  121. * Callback function to process statistic values.
  122. *
  123. * @param cls closure
  124. * @param subsystem name of subsystem that created the statistic
  125. * @param name the name of the datum
  126. * @param value the current value
  127. * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
  128. * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
  129. */
  130. static int
  131. print_stat (void *cls,
  132. const char *subsystem,
  133. const char *name,
  134. uint64_t value,
  135. int is_persistent)
  136. {
  137. struct StatMaster *sm = cls;
  138. fprintf (stderr,
  139. "Peer %2u: %12s/%50s = %12llu\n",
  140. sm->daemon,
  141. subsystem,
  142. name,
  143. (unsigned long long) value);
  144. return GNUNET_OK;
  145. }
  146. /**
  147. * Function that gathers stats from all daemons.
  148. */
  149. static void
  150. stat_run (void *cls,
  151. const struct GNUNET_SCHEDULER_TaskContext *tc);
  152. /**
  153. * Function called when GET operation on stats is done.
  154. */
  155. static void
  156. get_done (void *cls,
  157. int success)
  158. {
  159. struct StatMaster *sm = cls;
  160. GNUNET_break (GNUNET_OK == success);
  161. sm->value++;
  162. GNUNET_SCHEDULER_add_now (&stat_run, sm);
  163. }
  164. /**
  165. * Function that gathers stats from all daemons.
  166. */
  167. static void
  168. stat_run (void *cls,
  169. const struct GNUNET_SCHEDULER_TaskContext *tc)
  170. {
  171. struct StatMaster *sm = cls;
  172. if (stats[sm->value].name != NULL)
  173. {
  174. GNUNET_STATISTICS_get (sm->stat,
  175. #if 0
  176. NULL, NULL,
  177. #else
  178. stats[sm->value].subsystem,
  179. stats[sm->value].name,
  180. #endif
  181. GNUNET_TIME_UNIT_FOREVER_REL,
  182. &get_done,
  183. &print_stat, sm);
  184. return;
  185. }
  186. GNUNET_STATISTICS_destroy (sm->stat, GNUNET_NO);
  187. sm->value = 0;
  188. sm->daemon++;
  189. if (sm->daemon == NUM_DAEMONS)
  190. {
  191. GNUNET_free (sm);
  192. GNUNET_SCHEDULER_add_now (&do_stop, NULL);
  193. return;
  194. }
  195. sm->stat = GNUNET_STATISTICS_create ("<driver>",
  196. GNUNET_FS_TEST_get_configuration (daemons,
  197. sm->daemon));
  198. GNUNET_SCHEDULER_add_now (&stat_run, sm);
  199. }
  200. static void
  201. do_report (void *cls,
  202. const struct GNUNET_SCHEDULER_TaskContext *tc)
  203. {
  204. static int download_counter;
  205. const char *type = cls;
  206. struct GNUNET_TIME_Relative del;
  207. char *fancy;
  208. struct StatMaster *sm;
  209. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
  210. {
  211. del = GNUNET_TIME_absolute_get_duration (start_time);
  212. if (del.rel_value == 0)
  213. del.rel_value = 1;
  214. fancy = GNUNET_STRINGS_byte_size_fancy (((unsigned long long)FILESIZE) * 1000LL / del.rel_value);
  215. fprintf (stderr,
  216. "Download speed of type `%s' was %s/s\n",
  217. type,
  218. fancy);
  219. GNUNET_free (fancy);
  220. if (NUM_DAEMONS != ++download_counter)
  221. return; /* more downloads to come */
  222. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  223. "Finished all downloads, shutting down\n",
  224. (unsigned long long) FILESIZE);
  225. sm = GNUNET_malloc (sizeof (struct StatMaster));
  226. sm->stat = GNUNET_STATISTICS_create ("<driver>",
  227. GNUNET_FS_TEST_get_configuration (daemons,
  228. sm->daemon));
  229. GNUNET_SCHEDULER_add_now (&stat_run, sm);
  230. }
  231. else
  232. {
  233. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  234. "Timeout during download for type `%s', shutting down with error\n",
  235. type);
  236. ok = 1;
  237. GNUNET_SCHEDULER_add_now (&do_stop, NULL);
  238. }
  239. }
  240. static void
  241. do_downloads (void *cls,
  242. const struct GNUNET_FS_Uri *u2)
  243. {
  244. int anonymity;
  245. unsigned int i;
  246. if (NULL == u2)
  247. {
  248. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS,
  249. daemons);
  250. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  251. "Timeout during upload attempt, shutting down with error\n");
  252. ok = 1;
  253. return;
  254. }
  255. uri2 = GNUNET_FS_uri_dup (u2);
  256. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  257. "Downloading %llu bytes\n",
  258. (unsigned long long) FILESIZE);
  259. start_time = GNUNET_TIME_absolute_get ();
  260. if (NULL != strstr (progname, "dht"))
  261. anonymity = 0;
  262. else
  263. anonymity = 1;
  264. /* (semi) leach-download(s); not true leaches since
  265. these peers do participate in sharing, they just
  266. don't have to offer anything *initially*. */
  267. for (i=0;i<NUM_DAEMONS-2;i++)
  268. GNUNET_FS_TEST_download (daemons[i],
  269. TIMEOUT,
  270. anonymity,
  271. 0 == (i%2) ? SEED1 : SEED2,
  272. 0 == (i%2) ? uri1 : uri2,
  273. VERBOSE,
  274. &do_report, "leach");
  275. /* mutual downloads of (primary) sharing peers */
  276. GNUNET_FS_TEST_download (daemons[NUM_DAEMONS-2],
  277. TIMEOUT,
  278. anonymity, SEED1, uri1,
  279. VERBOSE,
  280. &do_report, "seeder 2");
  281. GNUNET_FS_TEST_download (daemons[NUM_DAEMONS-1],
  282. TIMEOUT,
  283. anonymity, SEED2, uri2,
  284. VERBOSE,
  285. &do_report, "seeder 1");
  286. }
  287. static void
  288. do_publish2 (void *cls,
  289. const struct GNUNET_FS_Uri *u1)
  290. {
  291. int do_index;
  292. int anonymity;
  293. if (NULL == u1)
  294. {
  295. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS,
  296. daemons);
  297. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  298. "Timeout during upload attempt, shutting down with error\n");
  299. ok = 1;
  300. return;
  301. }
  302. uri1 = GNUNET_FS_uri_dup (u1);
  303. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  304. "Publishing %llu bytes\n",
  305. (unsigned long long) FILESIZE);
  306. if (NULL != strstr (progname, "index"))
  307. do_index = GNUNET_YES;
  308. else
  309. do_index = GNUNET_NO;
  310. if (NULL != strstr (progname, "dht"))
  311. anonymity = 0;
  312. else
  313. anonymity = 1;
  314. GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS-2],
  315. TIMEOUT,
  316. anonymity,
  317. do_index, FILESIZE, SEED2,
  318. VERBOSE,
  319. &do_downloads, NULL);
  320. }
  321. static void
  322. do_publish1 (void *cls,
  323. const char *emsg)
  324. {
  325. int do_index;
  326. int anonymity;
  327. if (NULL != emsg)
  328. {
  329. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS,
  330. daemons);
  331. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  332. "Error trying to connect: %s\n",
  333. emsg);
  334. ok = 1;
  335. return;
  336. }
  337. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  338. "Publishing %llu bytes\n",
  339. (unsigned long long) FILESIZE);
  340. if (NULL != strstr (progname, "index"))
  341. do_index = GNUNET_YES;
  342. else
  343. do_index = GNUNET_NO;
  344. if (NULL != strstr (progname, "dht"))
  345. anonymity = 0;
  346. else
  347. anonymity = 1;
  348. GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS-1],
  349. TIMEOUT,
  350. anonymity,
  351. do_index, FILESIZE, SEED1,
  352. VERBOSE,
  353. &do_publish2, NULL);
  354. }
  355. static void
  356. do_connect (void *cls,
  357. const struct GNUNET_SCHEDULER_TaskContext *tc)
  358. {
  359. struct GNUNET_TESTING_PeerGroup *pg;
  360. GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
  361. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  362. "Daemons started, will now try to connect them\n");
  363. pg = GNUNET_FS_TEST_get_group (daemons);
  364. GNUNET_TESTING_create_topology (pg,
  365. GNUNET_TESTING_TOPOLOGY_CLIQUE,
  366. GNUNET_TESTING_TOPOLOGY_NONE,
  367. NULL);
  368. GNUNET_TESTING_connect_topology (pg,
  369. GNUNET_TESTING_TOPOLOGY_CLIQUE,
  370. GNUNET_TESTING_TOPOLOGY_OPTION_NONE,
  371. 0.0,
  372. TIMEOUT,
  373. NUM_DAEMONS,
  374. &do_publish1,
  375. NULL);
  376. }
  377. static void
  378. run (void *cls,
  379. char *const *args,
  380. const char *cfgfile,
  381. const struct GNUNET_CONFIGURATION_Handle *cfg)
  382. {
  383. GNUNET_FS_TEST_daemons_start ("fs_test_lib_data.conf",
  384. TIMEOUT,
  385. NUM_DAEMONS,
  386. daemons,
  387. &do_connect,
  388. NULL);
  389. }
  390. int
  391. main (int argc, char *argv[])
  392. {
  393. char *const argvx[] = {
  394. "perf-gnunet-service-fs-p2p",
  395. "-c",
  396. "fs_test_lib_data.conf",
  397. #if VERBOSE
  398. "-L", "DEBUG",
  399. #endif
  400. NULL
  401. };
  402. struct GNUNET_GETOPT_CommandLineOption options[] = {
  403. GNUNET_GETOPT_OPTION_END
  404. };
  405. progname = argv[0];
  406. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
  407. GNUNET_log_setup ("perf_gnunet_service_fs_p2p_trust",
  408. #if VERBOSE
  409. "DEBUG",
  410. #else
  411. "WARNING",
  412. #endif
  413. NULL);
  414. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
  415. argvx, "perf-gnunet-service-fs-p2p-trust",
  416. "nohelp", options, &run, NULL);
  417. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
  418. return ok;
  419. }
  420. /* end of perf_gnunet_service_fs_p2p_trust.c */