adding DEBUG logic for #3863
[oweals/gnunet.git] / src / ats-tests / ats-testing.h
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19  */
20 /**
21  * @file ats-tests/ats-testing.h
22  * @brief ats testing library: setup topology and provide logging to test ats
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31
32 #define TEST_ATS_PREFERENCE_DEFAULT 1.0
33
34 /**
35  * Message type sent for traffic generation
36  */
37 #define TEST_MESSAGE_TYPE_PING 12345
38
39 /**
40  * Message type sent as response during traffic generation
41  */
42 #define TEST_MESSAGE_TYPE_PONG 12346
43
44 /**
45  * Size of test messages
46  */
47 #define TEST_MESSAGE_SIZE 100
48
49 struct BenchmarkPartner;
50
51 struct BenchmarkPeer;
52
53 struct GNUNET_ATS_TEST_Topology;
54
55 struct TrafficGenerator;
56
57 struct LoggingHandle;
58
59 enum GeneratorType
60 {
61   GNUNET_ATS_TEST_TG_LINEAR,
62   GNUNET_ATS_TEST_TG_CONSTANT,
63   GNUNET_ATS_TEST_TG_RANDOM,
64   GNUNET_ATS_TEST_TG_SINUS
65 };
66
67
68 /**
69  * Callback to call when topology setup is completed
70  *
71  * @param cls the closure
72  * @param masters array of master peers
73  * @param slaves array of master peers
74  */
75 typedef void (*GNUNET_ATS_TEST_TopologySetupDoneCallback) (void *cls,
76     struct BenchmarkPeer *masters,
77     struct BenchmarkPeer *slaves);
78
79 /**
80  * Callback called when logging is required for the data contained
81  *
82  * @param cls the closure
83  * @param address an address
84  * @param address_active is address active
85  * @param bandwidth_out bandwidth outbound
86  * @param bandwidth_in bandwidth inbound
87  * @param prop performance information
88  */
89 typedef void
90 (*GNUNET_ATS_TEST_LogRequest) (void *cls,
91     const struct GNUNET_HELLO_Address *address,
92     int address_active,
93     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
94     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
95     const struct GNUNET_ATS_Properties *prop);
96
97 /**
98  * Information we track for a peer in the testbed.
99  */
100 struct BenchmarkPeer
101 {
102   /**
103    * Handle with testbed.
104    */
105   struct GNUNET_TESTBED_Peer *peer;
106
107   /**
108    * Unique identifier
109    */
110   int no;
111
112   /**
113    * Is this peer a measter: GNUNET_YES/GNUNET_NO
114    */
115   int master;
116
117   /**
118    *  Peer ID
119    */
120   struct GNUNET_PeerIdentity id;
121
122   /**
123    * Testbed operation to get peer information
124    */
125   struct GNUNET_TESTBED_Operation *peer_id_op;
126
127   /**
128    * Testbed operation to connect to ATS performance service
129    */
130   struct GNUNET_TESTBED_Operation *ats_perf_op;
131
132   /**
133    * Testbed operation to connect to core
134    */
135   struct GNUNET_TESTBED_Operation *comm_op;
136
137   /**
138    * ATS performance handle
139    */
140   struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
141
142   /**
143    * Masters only:
144    * Testbed connect operations to connect masters to slaves
145    */
146   struct TestbedConnectOperation *core_connect_ops;
147
148   /**
149    *  Core handle
150    */
151   struct GNUNET_CORE_Handle *ch;
152
153   /**
154    *  Core handle
155    */
156   struct GNUNET_TRANSPORT_Handle *th;
157
158   /**
159    * Masters only:
160    * Peer to set ATS preferences for
161    */
162   struct BenchmarkPeer *pref_partner;
163
164   /**
165    * Masters only
166    * Progress task
167    */
168   struct GNUNET_SCHEDULER_Task * ats_task;
169
170   /**
171    * Masters only
172    * Progress task
173    */
174   double pref_value;
175
176   /**
177    * Array of partners with num_slaves entries (if master) or
178    * num_master entries (if slave)
179    */
180   struct BenchmarkPartner *partners;
181
182   /**
183    * Number of partners
184    */
185   int num_partners;
186
187   /**
188    * Number of core connections
189    */
190   int core_connections;
191
192   /**
193    * Masters only:
194    * Number of connections to slave peers
195    */
196   int core_slave_connections;
197
198   /**
199    * Total number of messages this peer has sent
200    */
201   unsigned int total_messages_sent;
202
203   /**
204    * Total number of bytes this peer has sent
205    */
206   unsigned int total_bytes_sent;
207
208   /**
209    * Total number of messages this peer has received
210    */
211   unsigned int total_messages_received;
212
213   /**
214    * Total number of bytes this peer has received
215    */
216   unsigned int total_bytes_received;
217 };
218
219 struct TrafficGenerator
220 {
221   struct TrafficGenerator *prev;
222   struct TrafficGenerator *next;
223
224   enum GeneratorType type;
225
226   struct BenchmarkPeer *src;
227   struct BenchmarkPartner *dest;
228
229   long int base_rate;
230   long int max_rate;
231   struct GNUNET_TIME_Relative duration_period;
232
233   struct GNUNET_SCHEDULER_Task * send_task;
234   struct GNUNET_TIME_Absolute next_ping_transmission;
235   struct GNUNET_TIME_Absolute time_start;
236 };
237
238
239 struct PreferenceGenerator
240 {
241   struct PreferenceGenerator *prev;
242   struct PreferenceGenerator *next;
243
244   enum GeneratorType type;
245
246   struct BenchmarkPeer *src;
247   struct BenchmarkPartner *dest;
248
249   enum GNUNET_ATS_PreferenceKind kind;
250
251   long int base_value;
252   long int max_value;
253   struct GNUNET_TIME_Relative duration_period;
254   struct GNUNET_TIME_Relative frequency;
255
256   struct GNUNET_SCHEDULER_Task * set_task;
257   struct GNUNET_TIME_Absolute next_ping_transmission;
258   struct GNUNET_TIME_Absolute time_start;
259 };
260
261 /**
262  * Information about a benchmarking partner
263  */
264 struct BenchmarkPartner
265 {
266   /**
267    * The peer itself this partner belongs to
268    */
269   struct BenchmarkPeer *me;
270
271   /**
272    * The partner peer
273    */
274   struct BenchmarkPeer *dest;
275
276   /**
277    * Core transmit handles
278    */
279   struct GNUNET_CORE_TransmitHandle *cth;
280
281   /**
282    * Transport transmit handles
283    */
284   struct GNUNET_TRANSPORT_TransmitHandle *tth;
285
286
287   /**
288    * Handle for traffic generator
289    */
290   struct TrafficGenerator *tg;
291
292   /**
293    * Handle for preference generator
294    */
295   struct PreferenceGenerator *pg;
296
297   /**
298    * Timestamp to calculate communication layer delay
299    */
300   struct GNUNET_TIME_Absolute last_message_sent;
301
302   /**
303    * Accumulated RTT for all messages
304    */
305   unsigned int total_app_rtt;
306
307   /**
308    * Number of messages sent to this partner
309    */
310   unsigned int messages_sent;
311
312   /**
313    * Number of bytes sent to this partner
314    */
315   unsigned int bytes_sent;
316
317   /**
318    * Number of messages received from this partner
319    */
320   unsigned int messages_received;
321
322   /**
323    * Number of bytes received from this partner
324    */
325   unsigned int bytes_received;
326
327   /* Current ATS properties */
328   struct GNUNET_ATS_Properties props;
329
330   /* Bandwidth assigned inbound */
331   uint32_t bandwidth_in;
332
333   /* Bandwidth assigned outbound */
334   uint32_t bandwidth_out;
335
336   /* Current preference values for bandwidth */
337   double pref_bandwidth;
338
339   /* Current preference values for delay */
340   double pref_delay;
341
342
343 };
344
345 /**
346  * Overall state of the performance benchmark
347  */
348 struct BenchmarkState
349 {
350   /**
351    * Are we connected to ATS service of all peers: GNUNET_YES/NO
352    */
353   int connected_ATS_service;
354
355   /**
356    * Are we connected to CORE service of all peers: GNUNET_YES/NO
357    */
358   int connected_COMM_service;
359
360   /**
361    * Are we connected to all peers: GNUNET_YES/NO
362    */
363   int connected_PEERS;
364
365   /**
366    * Are we connected to all slave peers on CORE level: GNUNET_YES/NO
367    */
368   int connected_CORE;
369
370   /**
371    * Are we connected to CORE service of all peers: GNUNET_YES/NO
372    */
373   int benchmarking;
374 };
375
376
377 struct GNUNET_ATS_TEST_Topology
378 {
379   /**
380    * Shutdown task
381    */
382   struct GNUNET_SCHEDULER_Task * shutdown_task;
383
384   /**
385    * Progress task
386    */
387   struct GNUNET_SCHEDULER_Task * progress_task;
388
389   /**
390    * Test result
391    */
392   int result;
393
394   /**Test core (GNUNET_YES) or transport (GNUNET_NO)
395    */
396   int test_core;
397
398   /**
399    * Solver string
400    */
401   char *solver;
402
403   /**
404    * Preference string
405    */
406   char *testname;
407
408   /**
409    * Preference string
410    */
411   char *pref_str;
412
413   /**
414    * ATS preference value
415    */
416   int pref_val;
417
418   /**
419    * Number master peers
420    */
421   unsigned int num_masters;
422
423   /**
424    * Array of master peers
425    */
426   struct BenchmarkPeer *mps;
427
428   /**
429    * Number slave peers
430    */
431   unsigned int num_slaves;
432
433   /**
434    * Array of slave peers
435    */
436   struct BenchmarkPeer *sps;
437
438   /**
439    * Benchmark duration
440    */
441   struct GNUNET_TIME_Relative perf_duration;
442
443   /**
444    * Logging frequency
445    */
446   struct GNUNET_TIME_Relative log_frequency;
447
448   /**
449    * Benchmark state
450    */
451   struct BenchmarkState state;
452
453   struct GNUNET_CORE_MessageHandler *handlers;
454
455   GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb;
456
457   GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb;
458   GNUNET_ATS_AddressInformationCallback ats_perf_cb;
459   void *done_cb_cls;
460 };
461
462 enum OperationType
463 {
464   START_SEND,
465   STOP_SEND,
466   START_PREFERENCE,
467   STOP_PREFERENCE
468 };
469
470 struct Episode;
471
472 struct Experiment;
473
474 typedef void (*GNUNET_ATS_TESTING_EpisodeDoneCallback) (
475     struct Episode *e);
476
477 typedef void (*GNUNET_ATS_TESTING_ExperimentDoneCallback) (struct Experiment *e,
478     struct GNUNET_TIME_Relative duration,int success);
479
480 /**
481  * An operation in an experiment
482  */
483 struct GNUNET_ATS_TEST_Operation
484 {
485   struct GNUNET_ATS_TEST_Operation *next;
486   struct GNUNET_ATS_TEST_Operation *prev;
487
488   long long unsigned int src_id;
489   long long unsigned int dest_id;
490
491   long long unsigned int base_rate;
492   long long unsigned int max_rate;
493   struct GNUNET_TIME_Relative period;
494   struct GNUNET_TIME_Relative frequency;
495
496   enum OperationType type;
497   enum GeneratorType gen_type;
498   enum GNUNET_ATS_PreferenceKind pref_type;
499 };
500
501 struct Episode
502 {
503   int id;
504   struct Episode *next;
505   struct GNUNET_TIME_Relative duration;
506
507   struct GNUNET_ATS_TEST_Operation *head;
508   struct GNUNET_ATS_TEST_Operation *tail;
509 };
510
511
512 struct Experiment
513 {
514   char *name;
515   char *cfg_file;
516   unsigned long long int num_masters;
517   unsigned long long int num_slaves;
518   struct GNUNET_TIME_Relative log_freq;
519   struct GNUNET_TIME_Relative max_duration;
520   struct GNUNET_TIME_Relative total_duration;
521   struct GNUNET_TIME_Absolute start_time;
522   unsigned int num_episodes;
523   struct Episode *start;
524
525   struct GNUNET_SCHEDULER_Task * experiment_timeout_task;
526   struct GNUNET_SCHEDULER_Task * episode_timeout_task;
527   struct Episode *cur;
528
529   GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb;
530   GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb;
531 };
532
533 /*
534  * Experiment related functions
535  */
536 extern struct GNUNET_CONFIGURATION_Handle *cfg;
537
538 /**
539  * Execute the specified experiment
540  *
541  * @param e the Experiment
542  * @param ep_done_cb a episode is completed
543  * @param e_done_cb the experiment is completed
544  */
545 void
546 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
547     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
548     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb);
549
550 /**
551  * Load an experiment from a file
552  *
553  * @param filename the file
554  * @return the Experiment or NULL on failure
555  */
556 struct Experiment *
557 GNUNET_ATS_TEST_experimentation_load (char *filename);
558
559
560 /**
561  * Stop an experiment
562  *
563  * @param e the experiment
564  */
565 void
566 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e);
567
568 /*
569  * Traffic related functions
570  */
571
572 void
573 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p);
574
575 void
576 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p);
577
578
579 /**
580  * Generate between the source master and the partner and send traffic with a
581  * maximum rate.
582  *
583  * @param src traffic source
584  * @param dest traffic partner
585  * @param type type of traffic to generate
586  * @param base_rate traffic base rate to send data with
587  * @param max_rate  traffic maximum rate to send data with
588  * @param period duration of a period of traffic generation (~ 1/frequency)
589  * @param duration how long to generate traffic
590  * @return the traffic generator
591  */
592 struct TrafficGenerator *
593 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
594     struct BenchmarkPartner *dest,
595     enum GeneratorType type,
596     long int base_rate,
597     long int max_rate,
598     struct GNUNET_TIME_Relative period,
599     struct GNUNET_TIME_Relative duration);
600
601 void
602 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg);
603
604 /**
605  * Stop all traffic generators
606  */
607 void
608 GNUNET_ATS_TEST_generate_traffic_stop_all ();
609
610 /**
611  * Generate between the source master and the partner and set preferences with a
612  * value depending on the generator.
613  *
614  * @param src source
615  * @param dest partner
616  * @param type type of preferences to generate
617  * @param base_value traffic base rate to send data with
618  * @param value_rate  traffic maximum rate to send data with
619  * @param period duration of a period of preferences generation (~ 1/frequency)
620  * @param frequency how long to generate preferences
621  * @param kind ATS preference to generate
622  * @return the traffic generator
623  */
624 struct PreferenceGenerator *
625 GNUNET_ATS_TEST_generate_preferences_start (struct BenchmarkPeer *src,
626     struct BenchmarkPartner *dest,
627     enum GeneratorType type,
628     long int base_value,
629     long int value_rate,
630     struct GNUNET_TIME_Relative period,
631     struct GNUNET_TIME_Relative frequency,
632     enum GNUNET_ATS_PreferenceKind kind);
633
634 void
635 GNUNET_ATS_TEST_generate_preferences_stop (struct PreferenceGenerator *pg);
636
637 void
638 GNUNET_ATS_TEST_generate_preferences_stop_all ();
639
640 /*
641  * Logging related functions
642  */
643
644 /**
645  * Start logging
646  *
647  * @param log_frequency the logging frequency
648  * @param testname the testname
649  * @param masters the master peers used for benchmarking
650  * @param num_masters the number of master peers
651  * @param num_slaves the number of slave peers
652  * @param verbose verbose logging
653  * @return the logging handle or NULL on error
654  */
655 struct LoggingHandle *
656 GNUNET_ATS_TEST_logging_start(struct GNUNET_TIME_Relative log_frequency,
657     char *testname, struct BenchmarkPeer *masters, int num_masters, int num_slaves,
658     int verbose);
659
660 /**
661  * Stop logging
662  *
663  * @param l the logging handle
664  */
665 void
666 GNUNET_ATS_TEST_logging_clean_up (struct LoggingHandle *l);
667
668 /**
669  * Stop logging
670  *
671  * @param l the logging handle
672  */
673 void
674 GNUNET_ATS_TEST_logging_stop (struct LoggingHandle *l);
675
676 /**
677  * Log all data now
678  *
679  * @param l logging handle to use
680  */
681 void
682 GNUNET_ATS_TEST_logging_now (struct LoggingHandle *l);
683
684
685 /**
686  * Write logging data to file
687  *
688  * @param l logging handle to use
689  * @param test_name name of the current test
690  * @param plots create gnuplots: GNUNET_YES or GNUNET_NO
691  */
692 void
693 GNUNET_ATS_TEST_logging_write_to_file (struct LoggingHandle *l,
694     char *test_name, int plots);
695
696 /*
697  * Topology related functions
698  */
699
700 struct BenchmarkPeer *
701 GNUNET_ATS_TEST_get_peer (int src);
702
703 struct BenchmarkPartner *
704 GNUNET_ATS_TEST_get_partner (int src, int dest);
705
706 /**
707  * Create a topology for ats testing
708  *
709  * @param name test name
710  * @param cfg_file configuration file to use for the peers
711  * @param num_slaves number of slaves
712  * @param num_masters number of masters
713  * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
714  * @param done_cb function to call when topology is setup
715  * @param done_cb_cls cls for callback
716  * @param transport_recv_cb callback to call when data are received
717  * @param log_request_cb callback to call when logging is required
718  */
719 void
720 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
721     unsigned int num_slaves,
722     unsigned int num_masters,
723     int test_core,
724     GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
725     void *done_cb_cls,
726     GNUNET_TRANSPORT_ReceiveCallback recv_cb,
727     GNUNET_ATS_TEST_LogRequest ats_perf_cb);
728
729 /**
730  * Shutdown topology
731  */
732 void
733 GNUNET_ATS_TEST_shutdown_topology (void);
734
735 /* end of file ats-testing.h */