gnunet-cadet-profiler.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 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 cadet/gnunet-cadet-profiler.c
  18. *
  19. * @brief Profiler for cadet experiments.
  20. */
  21. #include <stdio.h>
  22. #include "platform.h"
  23. #include "cadet_test_lib.h"
  24. #include "gnunet_cadet_service.h"
  25. #include "gnunet_statistics_service.h"
  26. #define PING 1
  27. #define PONG 2
  28. /**
  29. * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
  30. */
  31. #define PING_PERIOD 500
  32. /**
  33. * How long until we give up on connecting the peers?
  34. */
  35. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  36. /**
  37. * Time to wait for stuff that should be rather fast
  38. */
  39. #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  40. /**
  41. * Total number of rounds.
  42. */
  43. #define number_rounds sizeof(rounds) / sizeof(rounds[0])
  44. /**
  45. * Ratio of peers active. First round always is 1.0.
  46. */
  47. static float rounds[] = { 0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0 };
  48. /**
  49. * Message type for pings.
  50. */
  51. struct CadetPingMessage
  52. {
  53. /**
  54. * Header. Type PING/PONG.
  55. */
  56. struct GNUNET_MessageHeader header;
  57. /**
  58. * Message number.
  59. */
  60. uint32_t counter;
  61. /**
  62. * Time the message was sent.
  63. */
  64. struct GNUNET_TIME_AbsoluteNBO timestamp;
  65. /**
  66. * Round number.
  67. */
  68. uint32_t round_number;
  69. };
  70. /**
  71. * Peer description.
  72. */
  73. struct CadetPeer
  74. {
  75. /**
  76. * Testbed Operation (to get peer id, etc).
  77. */
  78. struct GNUNET_TESTBED_Operation *op;
  79. /**
  80. * Peer ID.
  81. */
  82. struct GNUNET_PeerIdentity id;
  83. /**
  84. * Cadet handle for the root peer
  85. */
  86. struct GNUNET_CADET_Handle *cadet;
  87. /**
  88. * Channel handle for the root peer
  89. */
  90. struct GNUNET_CADET_Channel *ch;
  91. /**
  92. * Channel handle for the dest peer
  93. */
  94. struct GNUNET_CADET_Channel *incoming_ch;
  95. /**
  96. * Channel handle for a warmup channel.
  97. */
  98. struct GNUNET_CADET_Channel *warmup_ch;
  99. /**
  100. * Number of payload packes sent
  101. */
  102. int data_sent;
  103. /**
  104. * Number of payload packets received
  105. */
  106. int data_received;
  107. /**
  108. * Is peer up?
  109. */
  110. int up;
  111. /**
  112. * Destinaton to ping.
  113. */
  114. struct CadetPeer *dest;
  115. /**
  116. * Incoming channel for pings.
  117. */
  118. struct CadetPeer *incoming;
  119. /**
  120. * Task to do the next ping.
  121. */
  122. struct GNUNET_SCHEDULER_Task *ping_task;
  123. /**
  124. * NTR operation for the next ping.
  125. */
  126. struct GNUNET_CADET_TransmitHandle *ping_ntr;
  127. float mean[number_rounds];
  128. float var[number_rounds];
  129. unsigned int pongs[number_rounds];
  130. unsigned int pings[number_rounds];
  131. };
  132. /**
  133. * Duration of each round.
  134. */
  135. static struct GNUNET_TIME_Relative round_time;
  136. /**
  137. * GNUNET_PeerIdentity -> CadetPeer
  138. */
  139. static struct GNUNET_CONTAINER_MultiPeerMap *ids;
  140. /**
  141. * Testbed peer handles.
  142. */
  143. static struct GNUNET_TESTBED_Peer **testbed_handles;
  144. /**
  145. * Testbed Operation (to get stats).
  146. */
  147. static struct GNUNET_TESTBED_Operation *stats_op;
  148. /**
  149. * Operation to get peer ids.
  150. */
  151. static struct CadetPeer *peers;
  152. /**
  153. * Peer ids counter.
  154. */
  155. static unsigned int p_ids;
  156. /**
  157. * Total number of peers.
  158. */
  159. static unsigned long long peers_total;
  160. /**
  161. * Number of currently running peers.
  162. */
  163. static unsigned long long peers_running;
  164. /**
  165. * Number of peers doing pings.
  166. */
  167. static unsigned long long peers_pinging;
  168. /**
  169. * Test context (to shut down).
  170. */
  171. static struct GNUNET_CADET_TEST_Context *test_ctx;
  172. /**
  173. * Task called to disconnect peers, before shutdown.
  174. */
  175. static struct GNUNET_SCHEDULER_Task *disconnect_task;
  176. /**
  177. * Task to perform tests
  178. */
  179. static struct GNUNET_SCHEDULER_Task *test_task;
  180. /**
  181. * Round number.
  182. */
  183. static unsigned int current_round;
  184. /**
  185. * Do preconnect? (Each peer creates a tunnel to one other peer).
  186. */
  187. static int do_warmup;
  188. /**
  189. * Warmup progress.
  190. */
  191. static unsigned int peers_warmup;
  192. /**
  193. * Flag to notify callbacks not to generate any new traffic anymore.
  194. */
  195. static int test_finished;
  196. /**
  197. * Task running each round of the benchmark.
  198. */
  199. static struct GNUNET_SCHEDULER_Task *round_task;
  200. /**
  201. * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
  202. *
  203. * Testcase continues when the root receives confirmation of connected peers,
  204. * on callback function ch.
  205. *
  206. * @param cls Closure (unused).
  207. */
  208. static void
  209. start_test (void *cls);
  210. /**
  211. * Calculate a random delay.
  212. *
  213. * @param max Exclusive maximum, in ms.
  214. *
  215. * @return A time between 0 a max-1 ms.
  216. */
  217. static struct GNUNET_TIME_Relative
  218. delay_ms_rnd (unsigned int max)
  219. {
  220. unsigned int rnd;
  221. rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
  222. return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
  223. }
  224. /**
  225. * Get the index of a peer in the peers array.
  226. *
  227. * @param peer Peer whose index to get.
  228. *
  229. * @return Index of peer in peers.
  230. */
  231. static unsigned int
  232. get_index (struct CadetPeer *peer)
  233. {
  234. return peer - peers;
  235. }
  236. /**
  237. * Show the results of the test (banwidth achieved) and log them to GAUGER
  238. */
  239. static void
  240. show_end_data (void)
  241. {
  242. struct CadetPeer *peer;
  243. unsigned int i;
  244. unsigned int j;
  245. for (i = 0; i < number_rounds; i++)
  246. {
  247. for (j = 0; j < peers_pinging; j++)
  248. {
  249. peer = &peers[j];
  250. fprintf (stdout,
  251. "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n",
  252. i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)),
  253. peer->pings[i], peer->pongs[i]);
  254. }
  255. }
  256. }
  257. /**
  258. * Disconnect from cadet services af all peers, call shutdown.
  259. *
  260. * @param cls Closure (unused).
  261. */
  262. static void
  263. disconnect_cadet_peers (void *cls)
  264. {
  265. long line = (long) cls;
  266. unsigned int i;
  267. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  268. "disconnecting cadet service, called from line %ld\n",
  269. line);
  270. disconnect_task = NULL;
  271. for (i = 0; i < peers_total; i++)
  272. {
  273. if (NULL != peers[i].op)
  274. GNUNET_TESTBED_operation_done (peers[i].op);
  275. if (peers[i].up != GNUNET_YES)
  276. continue;
  277. if (NULL != peers[i].ch)
  278. {
  279. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  280. "%u: channel %p\n", i, peers[i].ch);
  281. GNUNET_CADET_channel_destroy (peers[i].ch);
  282. }
  283. if (NULL != peers[i].warmup_ch)
  284. {
  285. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  286. "%u: warmup channel %p\n",
  287. i, peers[i].warmup_ch);
  288. GNUNET_CADET_channel_destroy (peers[i].warmup_ch);
  289. }
  290. if (NULL != peers[i].incoming_ch)
  291. {
  292. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  293. "%u: incoming channel %p\n",
  294. i, peers[i].incoming_ch);
  295. GNUNET_CADET_channel_destroy (peers[i].incoming_ch);
  296. }
  297. }
  298. GNUNET_CADET_TEST_cleanup (test_ctx);
  299. GNUNET_SCHEDULER_shutdown ();
  300. }
  301. /**
  302. * Shut down peergroup, clean up.
  303. *
  304. * @param cls Closure (unused).
  305. */
  306. static void
  307. shutdown_task (void *cls)
  308. {
  309. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  310. "Ending test.\n");
  311. if (NULL != disconnect_task)
  312. {
  313. GNUNET_SCHEDULER_cancel (disconnect_task);
  314. disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
  315. (void *) __LINE__);
  316. }
  317. if (NULL != round_task)
  318. {
  319. GNUNET_SCHEDULER_cancel (round_task);
  320. round_task = NULL;
  321. }
  322. if (NULL != test_task)
  323. {
  324. GNUNET_SCHEDULER_cancel (test_task);
  325. test_task = NULL;
  326. }
  327. }
  328. /**
  329. * Finish test normally: schedule disconnect and shutdown
  330. *
  331. * @param line Line in the code the abort is requested from (__LINE__).
  332. */
  333. static void
  334. abort_test (long line)
  335. {
  336. if (disconnect_task != NULL)
  337. {
  338. GNUNET_SCHEDULER_cancel (disconnect_task);
  339. disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
  340. (void *) line);
  341. }
  342. }
  343. /**
  344. * Stats callback. Finish the stats testbed operation and when all stats have
  345. * been iterated, shutdown the test.
  346. *
  347. * @param cls closure
  348. * @param op the operation that has been finished
  349. * @param emsg error message in case the operation has failed; will be NULL if
  350. * operation has executed successfully.
  351. */
  352. static void
  353. stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
  354. {
  355. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
  356. GNUNET_TESTBED_operation_done (stats_op);
  357. if (NULL != disconnect_task)
  358. GNUNET_SCHEDULER_cancel (disconnect_task);
  359. disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
  360. (void *) __LINE__);
  361. }
  362. /**
  363. * Process statistic values.
  364. *
  365. * @param cls closure
  366. * @param peer the peer the statistic belong to
  367. * @param subsystem name of subsystem that created the statistic
  368. * @param name the name of the datum
  369. * @param value the current value
  370. * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
  371. * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  372. */
  373. static int
  374. stats_iterator (void *cls,
  375. const struct GNUNET_TESTBED_Peer *peer,
  376. const char *subsystem,
  377. const char *name,
  378. uint64_t value,
  379. int is_persistent)
  380. {
  381. uint32_t i;
  382. i = GNUNET_TESTBED_get_index (peer);
  383. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  384. " STATS %u - %s [%s]: %llu\n",
  385. i, subsystem, name,
  386. (unsigned long long) value);
  387. return GNUNET_OK;
  388. }
  389. /**
  390. * Task check that keepalives were sent and received.
  391. *
  392. * @param cls Closure (NULL).
  393. */
  394. static void
  395. collect_stats (void *cls)
  396. {
  397. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  398. "Start collecting statistics...\n");
  399. stats_op = GNUNET_TESTBED_get_statistics (peers_total,
  400. testbed_handles,
  401. NULL, NULL,
  402. &stats_iterator,
  403. &stats_cont, NULL);
  404. }
  405. /**
  406. * @brief Finish profiler normally. Signal finish and start collecting stats.
  407. *
  408. * @param cls Closure (unused).
  409. */
  410. static void
  411. finish_profiler (void *cls)
  412. {
  413. test_finished = GNUNET_YES;
  414. show_end_data ();
  415. GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
  416. }
  417. /**
  418. * Set the total number of running peers.
  419. *
  420. * @param target Desired number of running peers.
  421. */
  422. static void
  423. adjust_running_peers (unsigned int target)
  424. {
  425. struct GNUNET_TESTBED_Operation *op;
  426. unsigned int delta;
  427. unsigned int run;
  428. unsigned int i;
  429. unsigned int r;
  430. GNUNET_assert (target <= peers_total);
  431. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
  432. if (target > peers_running)
  433. {
  434. delta = target - peers_running;
  435. run = GNUNET_YES;
  436. }
  437. else
  438. {
  439. delta = peers_running - target;
  440. run = GNUNET_NO;
  441. }
  442. for (i = 0; i < delta; i++)
  443. {
  444. do
  445. {
  446. r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  447. peers_total - peers_pinging);
  448. r += peers_pinging;
  449. }
  450. while (peers[r].up == run || NULL != peers[r].incoming);
  451. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
  452. run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
  453. if (NULL != peers[r].ping_task)
  454. {
  455. GNUNET_SCHEDULER_cancel (peers[r].ping_task);
  456. peers[r].ping_task = NULL;
  457. }
  458. if (NULL != peers[r].ping_ntr)
  459. {
  460. GNUNET_CADET_notify_transmit_ready_cancel (peers[r].ping_ntr);
  461. peers[r].ping_ntr = NULL;
  462. }
  463. peers[r].up = run;
  464. if (NULL != peers[r].ch)
  465. GNUNET_CADET_channel_destroy (peers[r].ch);
  466. peers[r].ch = NULL;
  467. if (NULL != peers[r].dest)
  468. {
  469. if (NULL != peers[r].dest->incoming_ch)
  470. GNUNET_CADET_channel_destroy (peers[r].dest->incoming_ch);
  471. peers[r].dest->incoming_ch = NULL;
  472. }
  473. op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
  474. "cadet", NULL, NULL, run);
  475. GNUNET_break (NULL != op);
  476. peers_running += run ? 1 : -1;
  477. GNUNET_assert (peers_running > 0);
  478. }
  479. }
  480. /**
  481. * @brief Move to next round.
  482. *
  483. * @param cls Closure (round #).
  484. */
  485. static void
  486. next_rnd (void *cls)
  487. {
  488. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  489. "ROUND %u\n",
  490. current_round);
  491. if (0.0 == rounds[current_round])
  492. {
  493. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
  494. GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
  495. return;
  496. }
  497. adjust_running_peers (rounds[current_round] * peers_total);
  498. current_round++;
  499. round_task = GNUNET_SCHEDULER_add_delayed (round_time,
  500. &next_rnd,
  501. NULL);
  502. }
  503. /**
  504. * Transmit ping callback.
  505. *
  506. * @param cls Closure (peer for PING, NULL for PONG).
  507. * @param size Size of the transmit buffer.
  508. * @param buf Pointer to the beginning of the buffer.
  509. *
  510. * @return Number of bytes written to buf.
  511. */
  512. static size_t
  513. tmt_rdy_ping (void *cls, size_t size, void *buf);
  514. /**
  515. * Transmit pong callback.
  516. *
  517. * @param cls Closure (copy of PING message, to be freed).
  518. * @param size Size of the buffer we have.
  519. * @param buf Buffer to copy data to.
  520. */
  521. static size_t
  522. tmt_rdy_pong (void *cls, size_t size, void *buf)
  523. {
  524. struct CadetPingMessage *ping = cls;
  525. struct CadetPingMessage *pong;
  526. if ((0 == size) || (NULL == buf))
  527. {
  528. GNUNET_free (ping);
  529. return 0;
  530. }
  531. pong = (struct CadetPingMessage *) buf;
  532. GNUNET_memcpy (pong, ping, sizeof(*ping));
  533. pong->header.type = htons (PONG);
  534. GNUNET_free (ping);
  535. return sizeof(*ping);
  536. }
  537. /**
  538. * @brief Send a ping to destination
  539. *
  540. * @param cls Closure (peer).
  541. */
  542. static void
  543. ping (void *cls)
  544. {
  545. struct CadetPeer *peer = cls;
  546. peer->ping_task = NULL;
  547. if (GNUNET_YES == test_finished)
  548. return;
  549. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  550. "%u -> %u (%u)\n",
  551. get_index (peer),
  552. get_index (peer->dest),
  553. peer->data_sent);
  554. peer->ping_ntr = GNUNET_CADET_notify_transmit_ready (peer->ch, GNUNET_NO,
  555. GNUNET_TIME_UNIT_FOREVER_REL,
  556. sizeof(struct
  557. CadetPingMessage),
  558. &tmt_rdy_ping, peer);
  559. }
  560. /**
  561. * @brief Reply with a pong to origin.
  562. *
  563. * @param cls Closure (peer).
  564. * @param tc Task context.
  565. */
  566. static void
  567. pong (struct GNUNET_CADET_Channel *channel,
  568. const struct CadetPingMessage *ping)
  569. {
  570. struct CadetPingMessage *copy;
  571. copy = GNUNET_new (struct CadetPingMessage);
  572. *copy = *ping;
  573. GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
  574. GNUNET_TIME_UNIT_FOREVER_REL,
  575. sizeof(struct CadetPingMessage),
  576. &tmt_rdy_pong, copy);
  577. }
  578. /**
  579. * Transmit ping callback
  580. *
  581. * @param cls Closure (peer).
  582. * @param size Size of the buffer we have.
  583. * @param buf Buffer to copy data to.
  584. */
  585. static size_t
  586. tmt_rdy_ping (void *cls, size_t size, void *buf)
  587. {
  588. struct CadetPeer *peer = cls;
  589. struct CadetPingMessage *msg = buf;
  590. peer->ping_ntr = NULL;
  591. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  592. "tmt_rdy called, filling buffer\n");
  593. if ((size < sizeof(struct CadetPingMessage)) || (NULL == buf))
  594. {
  595. GNUNET_break (GNUNET_YES == test_finished);
  596. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  597. "size %u, buf %p, data_sent %u, data_received %u\n",
  598. (unsigned int) size,
  599. buf,
  600. peer->data_sent,
  601. peer->data_received);
  602. return 0;
  603. }
  604. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  605. "Sending: msg %d\n",
  606. peer->data_sent);
  607. msg->header.size = htons (size);
  608. msg->header.type = htons (PING);
  609. msg->counter = htonl (peer->data_sent++);
  610. msg->round_number = htonl (current_round);
  611. msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
  612. peer->pings[current_round]++;
  613. peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
  614. &ping, peer);
  615. return sizeof(struct CadetPingMessage);
  616. }
  617. /**
  618. * Function is called whenever a PING message is received.
  619. *
  620. * @param cls closure (peer #, set from GNUNET_CADET_connect)
  621. * @param channel connection to the other end
  622. * @param channel_ctx place to store local state associated with the channel
  623. * @param message the actual message
  624. * @return GNUNET_OK to keep the connection open,
  625. * GNUNET_SYSERR to close it (signal serious error)
  626. */
  627. int
  628. ping_handler (void *cls, struct GNUNET_CADET_Channel *channel,
  629. void **channel_ctx,
  630. const struct GNUNET_MessageHeader *message)
  631. {
  632. long n = (long) cls;
  633. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  634. "%u got PING\n",
  635. (unsigned int) n);
  636. GNUNET_CADET_receive_done (channel);
  637. if (GNUNET_NO == test_finished)
  638. pong (channel, (struct CadetPingMessage *) message);
  639. return GNUNET_OK;
  640. }
  641. /**
  642. * Function is called whenever a PONG message is received.
  643. *
  644. * @param cls closure (peer #, set from GNUNET_CADET_connect)
  645. * @param channel connection to the other end
  646. * @param channel_ctx place to store local state associated with the channel
  647. * @param message the actual message
  648. * @return GNUNET_OK to keep the connection open,
  649. * GNUNET_SYSERR to close it (signal serious error)
  650. */
  651. int
  652. pong_handler (void *cls, struct GNUNET_CADET_Channel *channel,
  653. void **channel_ctx,
  654. const struct GNUNET_MessageHeader *message)
  655. {
  656. long n = (long) cls;
  657. struct CadetPeer *peer;
  658. struct CadetPingMessage *msg;
  659. struct GNUNET_TIME_Absolute send_time;
  660. struct GNUNET_TIME_Relative latency;
  661. unsigned int r /* Ping round */;
  662. float delta;
  663. GNUNET_CADET_receive_done (channel);
  664. peer = &peers[n];
  665. msg = (struct CadetPingMessage *) message;
  666. send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp);
  667. latency = GNUNET_TIME_absolute_get_duration (send_time);
  668. r = ntohl (msg->round_number);
  669. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n",
  670. get_index (peer),
  671. get_index (peer->dest),
  672. (uint32_t) ntohl (msg->counter),
  673. GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
  674. /* Online variance calculation */
  675. peer->pongs[r]++;
  676. delta = latency.rel_value_us - peer->mean[r];
  677. peer->mean[r] = peer->mean[r] + delta / peer->pongs[r];
  678. peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]);
  679. return GNUNET_OK;
  680. }
  681. /**
  682. * Handlers, for diverse services
  683. */
  684. static struct GNUNET_CADET_MessageHandler handlers[] = {
  685. { &ping_handler, PING, sizeof(struct CadetPingMessage) },
  686. { &pong_handler, PONG, sizeof(struct CadetPingMessage) },
  687. { NULL, 0, 0 }
  688. };
  689. /**
  690. * Method called whenever another peer has added us to a channel
  691. * the other peer initiated.
  692. *
  693. * @param cls Closure.
  694. * @param channel New handle to the channel.
  695. * @param initiator Peer that started the channel.
  696. * @param port Port this channel is connected to.
  697. * @param options channel option flags
  698. * @return Initial channel context for the channel
  699. * (can be NULL -- that's not an error).
  700. */
  701. static void *
  702. incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
  703. const struct GNUNET_PeerIdentity *initiator,
  704. const struct GNUNET_HashCode *port,
  705. enum GNUNET_CADET_ChannelOption options)
  706. {
  707. long n = (long) cls;
  708. struct CadetPeer *peer;
  709. peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
  710. GNUNET_assert (NULL != peer);
  711. if (NULL == peers[n].incoming)
  712. {
  713. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  714. "WARMUP %3u: %u <= %u\n",
  715. peers_warmup,
  716. (unsigned int) n,
  717. get_index (peer));
  718. peers_warmup++;
  719. if (peers_warmup < peers_total)
  720. return NULL;
  721. if (NULL != test_task)
  722. {
  723. GNUNET_SCHEDULER_cancel (test_task);
  724. test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  725. &start_test, NULL);
  726. }
  727. return NULL;
  728. }
  729. GNUNET_assert (peer == peers[n].incoming);
  730. GNUNET_assert (peer->dest == &peers[n]);
  731. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  732. "%u <= %u %p\n",
  733. (unsigned int) n,
  734. get_index (peer),
  735. channel);
  736. peers[n].incoming_ch = channel;
  737. return NULL;
  738. }
  739. /**
  740. * Function called whenever an inbound channel is destroyed. Should clean up
  741. * any associated state.
  742. *
  743. * @param cls closure (set from GNUNET_CADET_connect)
  744. * @param channel connection to the other end (henceforth invalid)
  745. * @param channel_ctx place where local state associated
  746. * with the channel is stored
  747. */
  748. static void
  749. channel_cleaner (void *cls,
  750. const struct GNUNET_CADET_Channel *channel,
  751. void *channel_ctx)
  752. {
  753. long n = (long) cls;
  754. struct CadetPeer *peer = &peers[n];
  755. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  756. "Channel %p disconnected at peer %ld\n", channel, n);
  757. if (peer->ch == channel)
  758. peer->ch = NULL;
  759. }
  760. /**
  761. * Select a random peer that has no incoming channel
  762. *
  763. * @param peer ID of the peer connecting. NULL if irrelevant (warmup).
  764. *
  765. * @return Random peer not yet connected to.
  766. */
  767. static struct CadetPeer *
  768. select_random_peer (struct CadetPeer *peer)
  769. {
  770. unsigned int r;
  771. do
  772. {
  773. r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total);
  774. }
  775. while (NULL != peers[r].incoming);
  776. peers[r].incoming = peer;
  777. return &peers[r];
  778. }
  779. /**
  780. * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
  781. *
  782. * Testcase continues when the root receives confirmation of connected peers,
  783. * on callback function ch.
  784. *
  785. * @param cls Closure (unused).
  786. */
  787. static void
  788. start_test (void *cls)
  789. {
  790. unsigned long i;
  791. test_task = NULL;
  792. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
  793. for (i = 0; i < peers_pinging; i++)
  794. {
  795. peers[i].dest = select_random_peer (&peers[i]);
  796. peers[i].ch = GNUNET_CADET_channel_create (peers[i].cadet, NULL,
  797. &peers[i].dest->id,
  798. GC_u2h (1));
  799. if (NULL == peers[i].ch)
  800. {
  801. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i);
  802. GNUNET_CADET_TEST_cleanup (test_ctx);
  803. return;
  804. }
  805. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  806. "%lu => %u %p\n",
  807. i,
  808. get_index (peers[i].dest),
  809. peers[i].ch);
  810. peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
  811. &ping, &peers[i]);
  812. }
  813. peers_running = peers_total;
  814. if (NULL != disconnect_task)
  815. GNUNET_SCHEDULER_cancel (disconnect_task);
  816. disconnect_task =
  817. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (round_time,
  818. number_rounds
  819. + 1),
  820. &disconnect_cadet_peers,
  821. (void *) __LINE__);
  822. round_task = GNUNET_SCHEDULER_add_delayed (round_time,
  823. &next_rnd,
  824. NULL);
  825. }
  826. /**
  827. * Do warmup: create some channels to spread information about the topology.
  828. */
  829. static void
  830. warmup (void)
  831. {
  832. struct CadetPeer *peer;
  833. unsigned int i;
  834. for (i = 0; i < peers_total; i++)
  835. {
  836. peer = select_random_peer (NULL);
  837. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
  838. i, get_index (peer));
  839. peers[i].warmup_ch =
  840. GNUNET_CADET_channel_create (peers[i].cadet, NULL, &peer->id,
  841. GC_u2h (1));
  842. if (NULL == peers[i].warmup_ch)
  843. {
  844. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i);
  845. GNUNET_CADET_TEST_cleanup (test_ctx);
  846. return;
  847. }
  848. }
  849. }
  850. /**
  851. * Callback to be called when the requested peer information is available
  852. *
  853. * @param cls the closure from GNUNET_TESTBED_peer_get_information()
  854. * @param op the operation this callback corresponds to
  855. * @param pinfo the result; will be NULL if the operation has failed
  856. * @param emsg error message if the operation has failed;
  857. * NULL if the operation is successful
  858. */
  859. static void
  860. peer_id_cb (void *cls,
  861. struct GNUNET_TESTBED_Operation *op,
  862. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  863. const char *emsg)
  864. {
  865. long n = (long) cls;
  866. if ((NULL == pinfo) || (NULL != emsg))
  867. {
  868. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
  869. abort_test (__LINE__);
  870. return;
  871. }
  872. peers[n].id = *(pinfo->result.id);
  873. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  874. "%ld id: %s\n",
  875. n,
  876. GNUNET_i2s (&peers[n].id));
  877. GNUNET_break (GNUNET_OK ==
  878. GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
  879. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
  880. GNUNET_TESTBED_operation_done (peers[n].op);
  881. peers[n].op = NULL;
  882. p_ids++;
  883. if (p_ids < peers_total)
  884. return;
  885. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
  886. if (do_warmup)
  887. {
  888. struct GNUNET_TIME_Relative delay;
  889. warmup ();
  890. delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  891. 100 * peers_total);
  892. test_task = GNUNET_SCHEDULER_add_delayed (delay, &start_test, NULL);
  893. return; /* start_test from incoming_channel */
  894. }
  895. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n");
  896. test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  897. &start_test, NULL);
  898. }
  899. /**
  900. * test main: start test when all peers are connected
  901. *
  902. * @param cls Closure.
  903. * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
  904. * @param num_peers Number of peers that are running.
  905. * @param testbed_peers Array of peers.
  906. * @param cadetes Handle to each of the CADETs of the peers.
  907. */
  908. static void
  909. tmain (void *cls,
  910. struct GNUNET_CADET_TEST_Context *ctx,
  911. unsigned int num_peers,
  912. struct GNUNET_TESTBED_Peer **testbed_peers,
  913. struct GNUNET_CADET_Handle **cadetes)
  914. {
  915. unsigned long i;
  916. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  917. "test main\n");
  918. test_ctx = ctx;
  919. GNUNET_assert (peers_total == num_peers);
  920. peers_running = num_peers;
  921. testbed_handles = testbed_peers;
  922. disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
  923. &disconnect_cadet_peers,
  924. (void *) __LINE__);
  925. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  926. for (i = 0; i < peers_total; i++)
  927. {
  928. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  929. "requesting id %ld\n",
  930. i);
  931. peers[i].up = GNUNET_YES;
  932. peers[i].cadet = cadetes[i];
  933. peers[i].op =
  934. GNUNET_TESTBED_peer_get_information (testbed_handles[i],
  935. GNUNET_TESTBED_PIT_IDENTITY,
  936. &peer_id_cb, (void *) i);
  937. }
  938. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "requested peer ids\n");
  939. /* Continues from pi_cb -> do_test */
  940. }
  941. /**
  942. * Main: start profiler.
  943. */
  944. int
  945. main (int argc, char *argv[])
  946. {
  947. static const struct GNUNET_HashCode *ports[2];
  948. const char *config_file;
  949. config_file = ".profiler.conf";
  950. if (4 > argc)
  951. {
  952. fprintf (stderr,
  953. "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n",
  954. argv[0]);
  955. fprintf (stderr,
  956. "example: %s 30s 16 1 Y\n",
  957. argv[0]);
  958. return 1;
  959. }
  960. if (GNUNET_OK !=
  961. GNUNET_STRINGS_fancy_time_to_relative (argv[1],
  962. &round_time))
  963. {
  964. fprintf (stderr,
  965. "%s is not a valid time\n",
  966. argv[1]);
  967. return 1;
  968. }
  969. peers_total = atoll (argv[2]);
  970. if (2 > peers_total)
  971. {
  972. fprintf (stderr,
  973. "%s peers is not valid (> 2)\n",
  974. argv[1]);
  975. return 1;
  976. }
  977. peers = GNUNET_new_array (peers_total,
  978. struct CadetPeer);
  979. peers_pinging = atoll (argv[3]);
  980. if (peers_total < 2 * peers_pinging)
  981. {
  982. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  983. "not enough peers, total should be > 2 * peers_pinging\n");
  984. return 1;
  985. }
  986. do_warmup = (5 > argc || argv[4][0] != 'N');
  987. ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total,
  988. GNUNET_YES);
  989. GNUNET_assert (NULL != ids);
  990. p_ids = 0;
  991. test_finished = GNUNET_NO;
  992. ports[0] = GC_u2h (1);
  993. ports[1] = 0;
  994. GNUNET_CADET_TEST_run ("cadet-profiler", config_file, peers_total,
  995. &tmain, NULL, /* tmain cls */
  996. &incoming_channel, &channel_cleaner,
  997. handlers, ports);
  998. GNUNET_free (peers);
  999. return 0;
  1000. }
  1001. /* end of gnunet-cadet-profiler.c */