ats-testing.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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/ats-testing.h
  18. * @brief ats testing library: setup topology and provide logging to test ats
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testbed_service.h"
  25. #include "gnunet_ats_service.h"
  26. #include "gnunet_core_service.h"
  27. #include "gnunet_transport_service.h"
  28. #define TEST_ATS_PREFERENCE_DEFAULT 1.0
  29. /**
  30. * Message type sent for traffic generation
  31. */
  32. #define TEST_MESSAGE_TYPE_PING 12345
  33. /**
  34. * Message type sent as response during traffic generation
  35. */
  36. #define TEST_MESSAGE_TYPE_PONG 12346
  37. /**
  38. * Size of test messages
  39. */
  40. #define TEST_MESSAGE_SIZE 100
  41. struct TestMessage
  42. {
  43. struct GNUNET_MessageHeader header;
  44. uint8_t padding[TEST_MESSAGE_SIZE - sizeof(struct GNUNET_MessageHeader)];
  45. };
  46. struct BenchmarkPartner;
  47. struct BenchmarkPeer;
  48. struct GNUNET_ATS_TEST_Topology;
  49. struct TrafficGenerator;
  50. struct LoggingHandle;
  51. enum GeneratorType
  52. {
  53. GNUNET_ATS_TEST_TG_LINEAR,
  54. GNUNET_ATS_TEST_TG_CONSTANT,
  55. GNUNET_ATS_TEST_TG_RANDOM,
  56. GNUNET_ATS_TEST_TG_SINUS
  57. };
  58. /**
  59. * Callback to call when topology setup is completed
  60. *
  61. * @param cls the closure
  62. * @param masters array of master peers
  63. * @param slaves array of master peers
  64. */
  65. typedef void (*GNUNET_ATS_TEST_TopologySetupDoneCallback) (
  66. void *cls,
  67. struct BenchmarkPeer *masters,
  68. struct BenchmarkPeer *slaves);
  69. /**
  70. * Callback called when logging is required for the data contained
  71. *
  72. * @param cls the closure
  73. * @param address an address
  74. * @param address_active is address active
  75. * @param bandwidth_out bandwidth outbound
  76. * @param bandwidth_in bandwidth inbound
  77. * @param prop performance information
  78. */
  79. typedef void (*GNUNET_ATS_TEST_LogRequest) (
  80. void *cls,
  81. const struct GNUNET_HELLO_Address *address,
  82. int address_active,
  83. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  84. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  85. const struct GNUNET_ATS_Properties *prop);
  86. /**
  87. * Information we track for a peer in the testbed.
  88. */
  89. struct BenchmarkPeer
  90. {
  91. /**
  92. * Handle with testbed.
  93. */
  94. struct GNUNET_TESTBED_Peer *peer;
  95. /**
  96. * Unique identifier
  97. */
  98. int no;
  99. /**
  100. * Is this peer a measter: GNUNET_YES/GNUNET_NO
  101. */
  102. int master;
  103. /**
  104. * Peer ID
  105. */
  106. struct GNUNET_PeerIdentity id;
  107. /**
  108. * Testbed operation to get peer information
  109. */
  110. struct GNUNET_TESTBED_Operation *peer_id_op;
  111. /**
  112. * Testbed operation to connect to ATS performance service
  113. */
  114. struct GNUNET_TESTBED_Operation *ats_perf_op;
  115. /**
  116. * Testbed operation to connect to core
  117. */
  118. struct GNUNET_TESTBED_Operation *comm_op;
  119. /**
  120. * ATS performance handle
  121. */
  122. struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
  123. /**
  124. * Masters only:
  125. * Testbed connect operations to connect masters to slaves
  126. */
  127. struct TestbedConnectOperation *core_connect_ops;
  128. /**
  129. * Core handle
  130. */
  131. struct GNUNET_CORE_Handle *ch;
  132. /**
  133. * Transport handle
  134. */
  135. struct GNUNET_TRANSPORT_CoreHandle *th;
  136. /**
  137. * Masters only:
  138. * Peer to set ATS preferences for
  139. */
  140. struct BenchmarkPeer *pref_partner;
  141. /**
  142. * Masters only
  143. * Progress task
  144. */
  145. struct GNUNET_SCHEDULER_Task *ats_task;
  146. /**
  147. * Masters only
  148. * Progress task
  149. */
  150. double pref_value;
  151. /**
  152. * Array of partners with num_slaves entries (if master) or
  153. * num_master entries (if slave)
  154. */
  155. struct BenchmarkPartner *partners;
  156. /**
  157. * Number of partners
  158. */
  159. int num_partners;
  160. /**
  161. * Number of core connections
  162. */
  163. int core_connections;
  164. /**
  165. * Masters only:
  166. * Number of connections to slave peers
  167. */
  168. int core_slave_connections;
  169. /**
  170. * Total number of messages this peer has sent
  171. */
  172. unsigned int total_messages_sent;
  173. /**
  174. * Total number of bytes this peer has sent
  175. */
  176. unsigned int total_bytes_sent;
  177. /**
  178. * Total number of messages this peer has received
  179. */
  180. unsigned int total_messages_received;
  181. /**
  182. * Total number of bytes this peer has received
  183. */
  184. unsigned int total_bytes_received;
  185. };
  186. struct TrafficGenerator
  187. {
  188. struct TrafficGenerator *prev;
  189. struct TrafficGenerator *next;
  190. enum GeneratorType type;
  191. struct BenchmarkPeer *src;
  192. struct BenchmarkPartner *dest;
  193. long int base_rate;
  194. long int max_rate;
  195. struct GNUNET_TIME_Relative duration_period;
  196. struct GNUNET_SCHEDULER_Task *send_task;
  197. struct GNUNET_TIME_Absolute next_ping_transmission;
  198. struct GNUNET_TIME_Absolute time_start;
  199. };
  200. struct PreferenceGenerator
  201. {
  202. struct PreferenceGenerator *prev;
  203. struct PreferenceGenerator *next;
  204. enum GeneratorType type;
  205. struct BenchmarkPeer *src;
  206. struct BenchmarkPartner *dest;
  207. enum GNUNET_ATS_PreferenceKind kind;
  208. long int base_value;
  209. long int max_value;
  210. struct GNUNET_TIME_Relative duration_period;
  211. struct GNUNET_TIME_Relative frequency;
  212. struct GNUNET_SCHEDULER_Task *set_task;
  213. struct GNUNET_TIME_Absolute next_ping_transmission;
  214. struct GNUNET_TIME_Absolute time_start;
  215. };
  216. /**
  217. * Information about a benchmarking partner
  218. */
  219. struct BenchmarkPartner
  220. {
  221. /**
  222. * The peer itself this partner belongs to
  223. */
  224. struct BenchmarkPeer *me;
  225. /**
  226. * The partner peer
  227. */
  228. struct BenchmarkPeer *dest;
  229. /**
  230. * Message queue handle.
  231. */
  232. struct GNUNET_MQ_Handle *mq;
  233. /**
  234. * Handle for traffic generator
  235. */
  236. struct TrafficGenerator *tg;
  237. /**
  238. * Handle for preference generator
  239. */
  240. struct PreferenceGenerator *pg;
  241. /**
  242. * Timestamp to calculate communication layer delay
  243. */
  244. struct GNUNET_TIME_Absolute last_message_sent;
  245. /**
  246. * Accumulated RTT for all messages
  247. */
  248. unsigned int total_app_rtt;
  249. /**
  250. * Number of messages sent to this partner
  251. */
  252. unsigned int messages_sent;
  253. /**
  254. * Number of bytes sent to this partner
  255. */
  256. unsigned int bytes_sent;
  257. /**
  258. * Number of messages received from this partner
  259. */
  260. unsigned int messages_received;
  261. /**
  262. * Number of bytes received from this partner
  263. */
  264. unsigned int bytes_received;
  265. /**
  266. * Current ATS properties
  267. */
  268. struct GNUNET_ATS_Properties props;
  269. /**
  270. * Bandwidth assigned inbound
  271. */
  272. uint32_t bandwidth_in;
  273. /**
  274. * Bandwidth assigned outbound
  275. */
  276. uint32_t bandwidth_out;
  277. /**
  278. * Current preference values for bandwidth
  279. */
  280. double pref_bandwidth;
  281. /**
  282. * Current preference values for delay
  283. */
  284. double pref_delay;
  285. };
  286. /**
  287. * Overall state of the performance benchmark
  288. */
  289. struct BenchmarkState
  290. {
  291. /**
  292. * Are we connected to ATS service of all peers: GNUNET_YES/NO
  293. */
  294. int connected_ATS_service;
  295. /**
  296. * Are we connected to CORE service of all peers: GNUNET_YES/NO
  297. */
  298. int connected_COMM_service;
  299. /**
  300. * Are we connected to all peers: GNUNET_YES/NO
  301. */
  302. int connected_PEERS;
  303. /**
  304. * Are we connected to all slave peers on CORE level: GNUNET_YES/NO
  305. */
  306. int connected_CORE;
  307. /**
  308. * Are we connected to CORE service of all peers: GNUNET_YES/NO
  309. */
  310. int benchmarking;
  311. };
  312. struct GNUNET_ATS_TEST_Topology
  313. {
  314. /**
  315. * Progress task
  316. */
  317. struct GNUNET_SCHEDULER_Task *progress_task;
  318. /**
  319. * Test result
  320. */
  321. int result;
  322. /**
  323. * Test core (#GNUNET_YES) or transport (#GNUNET_NO)
  324. */
  325. int test_core;
  326. /**
  327. * Solver string
  328. */
  329. char *solver;
  330. /**
  331. * Preference string
  332. */
  333. char *testname;
  334. /**
  335. * Preference string
  336. */
  337. char *pref_str;
  338. /**
  339. * ATS preference value
  340. */
  341. int pref_val;
  342. /**
  343. * Number master peers
  344. */
  345. unsigned int num_masters;
  346. /**
  347. * Array of master peers
  348. */
  349. struct BenchmarkPeer *mps;
  350. /**
  351. * Number slave peers
  352. */
  353. unsigned int num_slaves;
  354. /**
  355. * Array of slave peers
  356. */
  357. struct BenchmarkPeer *sps;
  358. /**
  359. * Benchmark duration
  360. */
  361. struct GNUNET_TIME_Relative perf_duration;
  362. /**
  363. * Logging frequency
  364. */
  365. struct GNUNET_TIME_Relative log_frequency;
  366. /**
  367. * Benchmark state
  368. */
  369. struct BenchmarkState state;
  370. GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb;
  371. GNUNET_ATS_AddressInformationCallback ats_perf_cb;
  372. void *done_cb_cls;
  373. };
  374. enum OperationType
  375. {
  376. START_SEND,
  377. STOP_SEND,
  378. START_PREFERENCE,
  379. STOP_PREFERENCE
  380. };
  381. struct Episode;
  382. struct Experiment;
  383. typedef void (*GNUNET_ATS_TESTING_EpisodeDoneCallback) (struct Episode *e);
  384. typedef void (*GNUNET_ATS_TESTING_ExperimentDoneCallback) (
  385. struct Experiment *e,
  386. struct GNUNET_TIME_Relative duration,
  387. int success);
  388. /**
  389. * An operation in an experiment
  390. */
  391. struct GNUNET_ATS_TEST_Operation
  392. {
  393. struct GNUNET_ATS_TEST_Operation *next;
  394. struct GNUNET_ATS_TEST_Operation *prev;
  395. long long unsigned int src_id;
  396. long long unsigned int dest_id;
  397. long long unsigned int base_rate;
  398. long long unsigned int max_rate;
  399. struct GNUNET_TIME_Relative period;
  400. struct GNUNET_TIME_Relative frequency;
  401. enum OperationType type;
  402. enum GeneratorType gen_type;
  403. enum GNUNET_ATS_PreferenceKind pref_type;
  404. };
  405. struct Episode
  406. {
  407. int id;
  408. struct Episode *next;
  409. struct GNUNET_TIME_Relative duration;
  410. struct GNUNET_ATS_TEST_Operation *head;
  411. struct GNUNET_ATS_TEST_Operation *tail;
  412. };
  413. struct Experiment
  414. {
  415. char *name;
  416. char *cfg_file;
  417. unsigned long long int num_masters;
  418. unsigned long long int num_slaves;
  419. struct GNUNET_TIME_Relative log_freq;
  420. struct GNUNET_TIME_Relative max_duration;
  421. struct GNUNET_TIME_Relative total_duration;
  422. struct GNUNET_TIME_Absolute start_time;
  423. unsigned int num_episodes;
  424. struct Episode *start;
  425. struct GNUNET_SCHEDULER_Task *experiment_timeout_task;
  426. struct GNUNET_SCHEDULER_Task *episode_timeout_task;
  427. struct Episode *cur;
  428. GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb;
  429. GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb;
  430. };
  431. extern struct GNUNET_CONFIGURATION_Handle *cfg;
  432. /**
  433. * Execute the specified experiment
  434. *
  435. * @param e the Experiment
  436. * @param ep_done_cb a episode is completed
  437. * @param e_done_cb the experiment is completed
  438. */
  439. void
  440. GNUNET_ATS_TEST_experimentation_run (
  441. struct Experiment *e,
  442. GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
  443. GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb);
  444. /**
  445. * Load an experiment from a file
  446. *
  447. * @param filename the file
  448. * @return the Experiment or NULL on failure
  449. */
  450. struct Experiment *
  451. GNUNET_ATS_TEST_experimentation_load (const char *filename);
  452. /**
  453. * Stop an experiment
  454. *
  455. * @param e the experiment
  456. */
  457. void
  458. GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e);
  459. void
  460. GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p);
  461. void
  462. GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p);
  463. /**
  464. * Generate between the source master and the partner and send traffic with a
  465. * maximum rate.
  466. *
  467. * @param src traffic source
  468. * @param dest traffic partner
  469. * @param type type of traffic to generate
  470. * @param base_rate traffic base rate to send data with
  471. * @param max_rate traffic maximum rate to send data with
  472. * @param period duration of a period of traffic generation (~ 1/frequency)
  473. * @param duration how long to generate traffic
  474. * @return the traffic generator
  475. */
  476. struct TrafficGenerator *
  477. GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
  478. struct BenchmarkPartner *dest,
  479. enum GeneratorType type,
  480. unsigned int base_rate,
  481. unsigned int max_rate,
  482. struct GNUNET_TIME_Relative period,
  483. struct GNUNET_TIME_Relative duration);
  484. void
  485. GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg);
  486. /**
  487. * Stop all traffic generators
  488. */
  489. void
  490. GNUNET_ATS_TEST_generate_traffic_stop_all (void);
  491. /**
  492. * Generate between the source master and the partner and set preferences with a
  493. * value depending on the generator.
  494. *
  495. * @param src source
  496. * @param dest partner
  497. * @param type type of preferences to generate
  498. * @param base_value traffic base rate to send data with
  499. * @param value_rate traffic maximum rate to send data with
  500. * @param period duration of a period of preferences generation (~ 1/frequency)
  501. * @param frequency how long to generate preferences
  502. * @param kind ATS preference to generate
  503. * @return the traffic generator
  504. */
  505. struct PreferenceGenerator *
  506. GNUNET_ATS_TEST_generate_preferences_start (
  507. struct BenchmarkPeer *src,
  508. struct BenchmarkPartner *dest,
  509. enum GeneratorType type,
  510. unsigned int base_value,
  511. unsigned int value_rate,
  512. struct GNUNET_TIME_Relative period,
  513. struct GNUNET_TIME_Relative frequency,
  514. enum GNUNET_ATS_PreferenceKind kind);
  515. void
  516. GNUNET_ATS_TEST_generate_preferences_stop (struct PreferenceGenerator *pg);
  517. void
  518. GNUNET_ATS_TEST_generate_preferences_stop_all (void);
  519. /**
  520. * Start logging
  521. *
  522. * @param log_frequency the logging frequency
  523. * @param testname the testname
  524. * @param masters the master peers used for benchmarking
  525. * @param num_masters the number of master peers
  526. * @param num_slaves the number of slave peers
  527. * @param verbose verbose logging
  528. * @return the logging handle or NULL on error
  529. */
  530. struct LoggingHandle *
  531. GNUNET_ATS_TEST_logging_start (struct GNUNET_TIME_Relative log_frequency,
  532. const char *testname,
  533. struct BenchmarkPeer *masters,
  534. int num_masters,
  535. int num_slaves,
  536. int verbose);
  537. /**
  538. * Stop logging
  539. *
  540. * @param l the logging handle
  541. */
  542. void
  543. GNUNET_ATS_TEST_logging_clean_up (struct LoggingHandle *l);
  544. /**
  545. * Stop logging
  546. *
  547. * @param l the logging handle
  548. */
  549. void
  550. GNUNET_ATS_TEST_logging_stop (struct LoggingHandle *l);
  551. /**
  552. * Log all data now
  553. *
  554. * @param l logging handle to use
  555. */
  556. void
  557. GNUNET_ATS_TEST_logging_now (struct LoggingHandle *l);
  558. /**
  559. * Write logging data to file
  560. *
  561. * @param l logging handle to use
  562. * @param test_name name of the current test
  563. * @param plots create gnuplots: #GNUNET_YES or #GNUNET_NO
  564. */
  565. void
  566. GNUNET_ATS_TEST_logging_write_to_file (struct LoggingHandle *l,
  567. const char *test_name,
  568. int plots);
  569. /**
  570. * Topology related functions
  571. */
  572. struct BenchmarkPeer *
  573. GNUNET_ATS_TEST_get_peer (int src);
  574. struct BenchmarkPartner *
  575. GNUNET_ATS_TEST_get_partner (int src, int dest);
  576. /**
  577. * Create a topology for ats testing
  578. *
  579. * @param name test name
  580. * @param cfg_file configuration file to use for the peers
  581. * @param num_slaves number of slaves
  582. * @param num_masters number of masters
  583. * @param test_core connect to CORE service (#GNUNET_YES) or transport
  584. * (#GNUNET_NO)
  585. * @param done_cb function to call when topology is setup
  586. * @param done_cb_cls cls for callback
  587. * @param log_request_cb callback to call when logging is required
  588. */
  589. void
  590. GNUNET_ATS_TEST_create_topology (
  591. char *name,
  592. char *cfg_file,
  593. unsigned int num_slaves,
  594. unsigned int num_masters,
  595. int test_core,
  596. GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
  597. void *done_cb_cls,
  598. GNUNET_ATS_TEST_LogRequest ats_perf_cb);
  599. /**
  600. * Shutdown topology
  601. */
  602. void
  603. GNUNET_ATS_TEST_shutdown_topology (void);
  604. /* end of file ats-testing.h */