additional check for sinus generator
[oweals/gnunet.git] / src / ats-tests / ats-testing.h
1 /*
2  This file is part of GNUnet.
3  (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., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, 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 TrafficGeneratorType
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 ats ats information
88  * @param ats_count number of ats inforation
89  */
90 typedef void
91 (*GNUNET_ATS_TEST_LogRequest) (void *cls,
92     const struct GNUNET_HELLO_Address *address,
93     int address_active,
94     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
95     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
96     const struct GNUNET_ATS_Information *ats,
97     uint32_t ats_count);
98
99 /**
100  * Information we track for a peer in the testbed.
101  */
102 struct BenchmarkPeer
103 {
104   /**
105    * Handle with testbed.
106    */
107   struct GNUNET_TESTBED_Peer *peer;
108
109   /**
110    * Unique identifier
111    */
112   int no;
113
114   /**
115    * Is this peer a measter: GNUNET_YES/GNUNET_NO
116    */
117   int master;
118
119   /**
120    *  Peer ID
121    */
122   struct GNUNET_PeerIdentity id;
123
124   /**
125    * Testbed operation to get peer information
126    */
127   struct GNUNET_TESTBED_Operation *peer_id_op;
128
129   /**
130    * Testbed operation to connect to ATS performance service
131    */
132   struct GNUNET_TESTBED_Operation *ats_perf_op;
133
134   /**
135    * Testbed operation to connect to core
136    */
137   struct GNUNET_TESTBED_Operation *comm_op;
138
139   /**
140    * ATS performance handle
141    */
142   struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
143
144   /**
145    * Masters only:
146    * Testbed connect operations to connect masters to slaves
147    */
148   struct TestbedConnectOperation *core_connect_ops;
149
150   /**
151    *  Core handle
152    */
153   struct GNUNET_CORE_Handle *ch;
154
155   /**
156    *  Core handle
157    */
158   struct GNUNET_TRANSPORT_Handle *th;
159
160   /**
161    * Masters only:
162    * Peer to set ATS preferences for
163    */
164   struct BenchmarkPeer *pref_partner;
165
166   /**
167    * Masters only
168    * Progress task
169    */
170   GNUNET_SCHEDULER_TaskIdentifier ats_task;
171
172   /**
173    * Masters only
174    * Progress task
175    */
176   double pref_value;
177
178   /**
179    * Array of partners with num_slaves entries (if master) or
180    * num_master entries (if slave)
181    */
182   struct BenchmarkPartner *partners;
183
184   /**
185    * Number of partners
186    */
187   int num_partners;
188
189   /**
190    * Number of core connections
191    */
192   int core_connections;
193
194   /**
195    * Masters only:
196    * Number of connections to slave peers
197    */
198   int core_slave_connections;
199
200   /**
201    * Total number of messages this peer has sent
202    */
203   unsigned int total_messages_sent;
204
205   /**
206    * Total number of bytes this peer has sent
207    */
208   unsigned int total_bytes_sent;
209
210   /**
211    * Total number of messages this peer has received
212    */
213   unsigned int total_messages_received;
214
215   /**
216    * Total number of bytes this peer has received
217    */
218   unsigned int total_bytes_received;
219 };
220
221 struct TrafficGenerator
222 {
223   struct TrafficGenerator *prev;
224   struct TrafficGenerator *next;
225
226   enum TrafficGeneratorType type;
227
228   struct BenchmarkPeer *src;
229   struct BenchmarkPartner *dest;
230
231   long int base_rate;
232   long int max_rate;
233   struct GNUNET_TIME_Relative duration_period;
234
235   GNUNET_SCHEDULER_TaskIdentifier send_task;
236   struct GNUNET_TIME_Absolute next_ping_transmission;
237   struct GNUNET_TIME_Absolute time_start;
238 };
239
240
241 /**
242  * Information about a benchmarking partner
243  */
244 struct BenchmarkPartner
245 {
246   /**
247    * The peer itself this partner belongs to
248    */
249   struct BenchmarkPeer *me;
250
251   /**
252    * The partner peer
253    */
254   struct BenchmarkPeer *dest;
255
256   /**
257    * Core transmit handles
258    */
259   struct GNUNET_CORE_TransmitHandle *cth;
260
261   /**
262    * Transport transmit handles
263    */
264   struct GNUNET_TRANSPORT_TransmitHandle *tth;
265
266   struct TrafficGenerator *tg;
267
268   /**
269    * Timestamp to calculate communication layer delay
270    */
271   struct GNUNET_TIME_Absolute last_message_sent;
272
273   /**
274    * Accumulated RTT for all messages
275    */
276   unsigned int total_app_rtt;
277
278   /**
279    * Number of messages sent to this partner
280    */
281   unsigned int messages_sent;
282
283   /**
284    * Number of bytes sent to this partner
285    */
286   unsigned int bytes_sent;
287
288   /**
289    * Number of messages received from this partner
290    */
291   unsigned int messages_received;
292
293   /**
294    * Number of bytes received from this partner
295    */
296   unsigned int bytes_received;
297
298   /* Current ATS properties */
299
300   uint32_t ats_distance;
301
302   uint32_t ats_delay;
303
304   uint32_t bandwidth_in;
305
306   uint32_t bandwidth_out;
307
308   uint32_t ats_utilization_up;
309
310   uint32_t ats_utilization_down;
311
312   uint32_t ats_network_type;
313
314   uint32_t ats_cost_wan;
315
316   uint32_t ats_cost_lan;
317
318   uint32_t ats_cost_wlan;
319 };
320
321 /**
322  * Overall state of the performance benchmark
323  */
324 struct BenchmarkState
325 {
326   /**
327    * Are we connected to ATS service of all peers: GNUNET_YES/NO
328    */
329   int connected_ATS_service;
330
331   /**
332    * Are we connected to CORE service of all peers: GNUNET_YES/NO
333    */
334   int connected_COMM_service;
335
336   /**
337    * Are we connected to all peers: GNUNET_YES/NO
338    */
339   int connected_PEERS;
340
341   /**
342    * Are we connected to all slave peers on CORE level: GNUNET_YES/NO
343    */
344   int connected_CORE;
345
346   /**
347    * Are we connected to CORE service of all peers: GNUNET_YES/NO
348    */
349   int benchmarking;
350 };
351
352
353 struct GNUNET_ATS_TEST_Topology
354 {
355   /**
356    * Shutdown task
357    */
358   GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
359
360   /**
361    * Progress task
362    */
363   GNUNET_SCHEDULER_TaskIdentifier progress_task;
364
365   /**
366    * Test result
367    */
368   int result;
369
370   /**Test core (GNUNET_YES) or transport (GNUNET_NO)
371    */
372   int test_core;
373
374   /**
375    * Solver string
376    */
377   char *solver;
378
379   /**
380    * Preference string
381    */
382   char *testname;
383
384   /**
385    * Preference string
386    */
387   char *pref_str;
388
389   /**
390    * ATS preference value
391    */
392   int pref_val;
393
394   /**
395    * Number master peers
396    */
397   unsigned int num_masters;
398
399   /**
400    * Array of master peers
401    */
402   struct BenchmarkPeer *mps;
403
404   /**
405    * Number slave peers
406    */
407   unsigned int num_slaves;
408
409   /**
410    * Array of slave peers
411    */
412   struct BenchmarkPeer *sps;
413
414   /**
415    * Benchmark duration
416    */
417   struct GNUNET_TIME_Relative perf_duration;
418
419   /**
420    * Logging frequency
421    */
422   struct GNUNET_TIME_Relative log_frequency;
423
424   /**
425    * Benchmark state
426    */
427   struct BenchmarkState state;
428
429   struct GNUNET_CORE_MessageHandler *handlers;
430
431   GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb;
432
433   GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb;
434   GNUNET_ATS_AddressInformationCallback ats_perf_cb;
435   void *done_cb_cls;
436 };
437
438 enum OperationType
439 {
440   START_SEND,
441   STOP_SEND,
442   SET_RATE,
443   SET_PREFERENCE
444 };
445
446 struct Episode;
447
448 struct Experiment;
449
450 typedef void (*GNUNET_ATS_TESTING_EpisodeDoneCallback) (
451     struct Episode *e);
452
453 typedef void (*GNUNET_ATS_TESTING_ExperimentDoneCallback) (struct Experiment *e,
454     struct GNUNET_TIME_Relative duration,int success);
455
456 struct Operation
457 {
458   struct Operation *next;
459   struct Operation *prev;
460   long long unsigned int src_id;
461   long long unsigned int dest_id;
462   long long unsigned int value;
463   enum OperationType type;
464 };
465
466 struct Episode
467 {
468   int id;
469   struct Episode *next;
470   struct GNUNET_TIME_Relative duration;
471
472   struct Operation *head;
473   struct Operation *tail;
474 };
475
476
477 struct Experiment
478 {
479   char *name;
480   char *cfg_file;
481   unsigned long long int num_masters;
482   unsigned long long int num_slaves;
483   struct GNUNET_TIME_Relative max_duration;
484   struct GNUNET_TIME_Relative total_duration;
485   struct GNUNET_TIME_Absolute start_time;
486   unsigned int num_episodes;
487   struct Episode *start;
488
489   GNUNET_SCHEDULER_TaskIdentifier experiment_timeout_task;
490   GNUNET_SCHEDULER_TaskIdentifier episode_timeout_task;
491   struct Episode *cur;
492
493   GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb;
494   GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb;
495 };
496
497 /*
498  * Experiment related functions
499  */
500
501
502 /**
503  * Execute the specified experiment
504  *
505  * @param e the Experiment
506  * @param ep_done_cb a episode is completed
507  * @param e_done_cb the experiment is completed
508  */
509 void
510 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
511     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
512     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb);
513
514 /**
515  * Load an experiment from a file
516  *
517  * @param filename the file
518  * @return the Experiment or NULL on failure
519  */
520 struct Experiment *
521 GNUNET_ATS_TEST_experimentation_load (char *filename);
522
523
524 /**
525  * Stop an experiment
526  *
527  * @param e the experiment
528  */
529 void
530 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e);
531
532 /*
533  * Traffic related functions
534  */
535
536 void
537 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p);
538
539 void
540 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p);
541
542
543 /**
544  * Generate between the source master and the partner and send traffic with a
545  * maximum rate.
546  *
547  * @param src traffic source
548  * @param dest traffic partner
549  * @param type type of traffic to generate
550  * @param base_rate traffic base rate to send data with
551  * @param max_rate  traffic maximum rate to send data with
552  * @param period duration of a period of traffic generation (~ 1/frequency)
553  * @param duration how long to generate traffic
554  * @return the traffic generator
555  */
556 struct TrafficGenerator *
557 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
558     struct BenchmarkPartner *dest,
559     enum TrafficGeneratorType type,
560     long int base_rate,
561     long int max_rate,
562     struct GNUNET_TIME_Relative period,
563     struct GNUNET_TIME_Relative duration);
564
565 void
566 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg);
567
568 /**
569  * Stop all traffic generators
570  */
571 void
572 GNUNET_ATS_TEST_generate_traffic_stop_all ();
573
574
575 /*
576  * Logging related functions
577  */
578
579 /**
580  * Start logging
581  *
582  * @param log_frequency the logging frequency
583  * @param testname the testname
584  * @param masters the master peers used for benchmarking
585  * @param num_master the number of master peers
586  * @return the logging handle or NULL on error
587  */
588 struct LoggingHandle *
589 GNUNET_ATS_TEST_logging_start (struct GNUNET_TIME_Relative log_frequency,
590     char * testname,
591     struct BenchmarkPeer *masters,
592     int num_masters);
593
594 /**
595  * Stop logging
596  *
597  * @param l the logging handle
598  */
599 void
600 GNUNET_ATS_TEST_logging_clean_up (struct LoggingHandle *l);
601
602 /**
603  * Stop logging
604  *
605  * @param l the logging handle
606  */
607 void
608 GNUNET_ATS_TEST_logging_stop (struct LoggingHandle *l);
609
610 /**
611  * Log all data now
612  *
613  * @param l logging handle to use
614  */
615 void
616 GNUNET_ATS_TEST_logging_now (struct LoggingHandle *l);
617
618
619 /**
620  * Write logging data to file
621  *
622  * @param l logging handle to use
623  * @param test_name name of the current test
624  * @param plots create gnuplots: GNUNET_YES or GNUNET_NO
625  */
626 void
627 GNUNET_ATS_TEST_logging_write_to_file (struct LoggingHandle *l,
628     char *test_name, int plots);
629
630 /*
631  * Topology related functions
632  */
633
634 /**
635  * Create a topology for ats testing
636  *
637  * @param name test name
638  * @param cfg_file configuration file to use for the peers
639  * @param num_slaves number of slaves
640  * @param num_masters number of masters
641  * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
642  * @param done_cb function to call when topology is setup
643  * @param done_cb_cls cls for callback
644  * @param recv_cb callback to call when data are received
645  * @param perf_cb callback to call when performance info are received
646  */
647 void
648 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
649     unsigned int num_slaves,
650     unsigned int num_masters,
651     int test_core,
652     GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
653     void *done_cb_cls,
654     GNUNET_TRANSPORT_ReceiveCallback recv_cb,
655     GNUNET_ATS_TEST_LogRequest ats_perf_cb);
656
657 /**
658  * Shutdown topology
659  */
660 void
661 GNUNET_ATS_TEST_shutdown_topology (void);
662
663 /* end of file ats-testing.h */