perf_gnunet_service_fs_p2p_respect.c 13 KB

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