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