a50de992f0f326976b537984ed7fe616312ee7a7
[oweals/gnunet.git] / src / ats-tests / perf_ats.c
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/perf_ats.c
22  * @brief ats benchmark: start peers and modify preferences, monitor change over time
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_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 #define TESTNAME_PREFIX "perf_ats_"
35 #define DEFAULT_SLAVES_NUM 3
36 #define DEFAULT_MASTERS_NUM 1
37
38 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
39 #define TEST_ATS_PREFRENCE_START 1.0
40 #define TEST_ATS_PREFRENCE_DELTA 1.0
41
42 #define TEST_MESSAGE_TYPE_PING 12345
43 #define TEST_MESSAGE_TYPE_PONG 12346
44 #define TEST_MESSAGE_SIZE 1000
45 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
46
47 /**
48  * Information about a benchmarking partner
49  */
50 struct BenchmarkPartner
51 {
52   /**
53    * The peer itself this partner belongs to
54    */
55   struct BenchmarkPeer *me;
56
57   /**
58    * The partner peer
59    */
60   struct BenchmarkPeer *dest;
61
62   /**
63    * Core transmit handles
64    */
65   void *cth;
66
67   /**
68    * Number of messages sent to this partner
69    */
70   unsigned int messages_sent;
71
72   /**
73    * Number of bytes sent to this partner
74    */
75   unsigned int bytes_sent;
76
77   /**
78    * Number of messages received from this partner
79    */
80   unsigned int messages_received;
81
82   /**
83    * Number of bytes received from this partner
84    */
85   unsigned int bytes_received;
86 };
87
88 /**
89  * Connect peers with testbed
90  */
91 struct TestbedConnectOperation
92 {
93   /**
94    * The benchmarking master initiating this connection
95    */
96   struct BenchmarkPeer *master;
97
98   /**
99    * The benchmarking slave to connect to
100    */
101   struct BenchmarkPeer *slave;
102
103   /**
104    * Testbed operation to connect peers
105    */
106   struct GNUNET_TESTBED_Operation *connect_op;
107 };
108
109 /**
110  * Information we track for a peer in the testbed.
111  */
112 struct BenchmarkPeer
113 {
114   /**
115    * Handle with testbed.
116    */
117   struct GNUNET_TESTBED_Peer *peer;
118
119   /**
120    * Unique identifier
121    */
122   int no;
123
124   /**
125    * Is this peer a measter: GNUNET_YES/GNUNET_NO
126    */
127   int master;
128
129   /**
130    *  Peer ID
131    */
132   struct GNUNET_PeerIdentity id;
133
134   /**
135    * Testbed operation to get peer information
136    */
137   struct GNUNET_TESTBED_Operation *peer_id_op;
138
139   /**
140    * Testbed operation to connect to ATS performance service
141    */
142   struct GNUNET_TESTBED_Operation *ats_perf_op;
143
144   /**
145    * Testbed operation to connect to core
146    */
147   struct GNUNET_TESTBED_Operation *core_op;
148
149   /**
150    * ATS performance handle
151    */
152   struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
153
154   /**
155    * Masters only:
156    * Testbed connect operations to connect masters to slaves
157    */
158   struct TestbedConnectOperation *core_connect_ops;
159
160   /**
161    *  Core handle
162    */
163   struct GNUNET_CORE_Handle *ch;
164
165   /**
166    * Masters only:
167    * Peer to set ATS preferences for
168    */
169   struct BenchmarkPeer *pref_partner;
170
171   /**
172    * Masters only
173    * Progress task
174    */
175   GNUNET_SCHEDULER_TaskIdentifier ats_task;
176
177   /**
178    * Masters only
179    * Progress task
180    */
181   double pref_value;
182
183   /**
184    * Array of partners with num_slaves entries (if master) or
185    * num_master entries (if slave)
186    */
187   struct BenchmarkPartner *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 /**
222  * Overall state of the performance benchmark
223  */
224 struct BenchmarkState
225 {
226   /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
227   int connected_ATS_service;
228
229   /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
230   int connected_CORE_service;
231
232   /* Are we connected to all peers: GNUNET_YES/NO */
233   int connected_PEERS;
234
235   /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
236   int connected_CORE;
237
238   /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
239   int benchmarking;
240 };
241
242 /**
243  * Shutdown task
244  */
245 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
246
247 /**
248  * Progress task
249  */
250 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
251
252 /**
253  * Test result
254  */
255 static int result;
256
257 /**
258  * Solver string
259  */
260 static char *solver;
261
262 /**
263  * Preference string
264  */
265 static char *pref_str;
266
267 /**
268  * ATS preference value
269  */
270 static int pref_val;
271
272 /**
273  * Number master peers
274  */
275 static int num_masters;
276
277 /**
278  * Array of master peers
279  */
280 struct BenchmarkPeer *mps;
281
282 /**
283  * Number slave peers
284  */
285 static int num_slaves;
286 /**
287  * Array of slave peers
288  */
289 struct BenchmarkPeer *sps;
290
291 /**
292  * Benchmark state
293  */
294 static struct BenchmarkState state;
295
296 static void
297 evaluate ()
298 {
299   int c_m;
300   int c_s;
301   unsigned int duration;
302   struct BenchmarkPeer *mp;
303
304   duration = (BENCHMARK_DURATION.rel_value_us / (1000 * 1000));
305   for (c_m = 0; c_m < num_masters; c_m++)
306   {
307     mp = &mps[c_m];
308     fprintf (stderr,
309         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
310         mp->no, mp->total_bytes_sent / 1024, duration,
311         (mp->total_bytes_sent / 1024) / duration,
312         mp->total_bytes_received / 1024, duration,
313         (mp->total_bytes_received / 1024) / duration);
314
315     for (c_s = 0; c_s < num_slaves; c_s++)
316     {
317       fprintf (stderr,
318           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
319           (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
320           mp->no, mp->partners[c_s].dest->no,
321           (mp->partners[c_s].bytes_sent / 1024) / duration,
322           ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
323           (mp->partners[c_s].bytes_received / 1024) / duration,
324           ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
325     }
326   }
327 }
328
329 /**
330  * Shutdown nicely
331  *
332  * @param cls NULL
333  * @param tc the task context
334  */
335 static void
336 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 {
338   int c_m;
339   int c_s;
340   int c_op;
341
342   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
343   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
344   {
345     fprintf (stderr, "0\n");
346     GNUNET_SCHEDULER_cancel (progress_task);
347   }
348   progress_task = GNUNET_SCHEDULER_NO_TASK;
349
350   evaluate ();
351   state.benchmarking = GNUNET_NO;
352   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
353
354   for (c_m = 0; c_m < num_masters; c_m++)
355   {
356     if (NULL != mps[c_m].peer_id_op)
357     {
358       GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
359       mps[c_m].peer_id_op = NULL;
360     }
361
362     if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
363       GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
364     mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
365
366     for (c_op = 0; c_op < num_slaves; c_op++)
367     {
368
369       if (NULL != mps[c_m].partners[c_op].cth)
370       {
371         GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
372         mps[c_m].partners[c_op].cth = NULL;
373       }
374
375       if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
376       {
377         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
378             _("Failed to connect peer 0 and %u\n"), c_op);
379         GNUNET_TESTBED_operation_done (
380             mps[c_m].core_connect_ops[c_op].connect_op);
381         mps[c_m].core_connect_ops[c_op].connect_op = NULL;
382         result = 1;
383       }
384     }
385
386     if (NULL != mps[c_m].ats_perf_op)
387     {
388       GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
389       mps[c_m].ats_perf_op = NULL;
390     }
391
392     if (NULL != mps[c_m].core_op)
393     {
394       GNUNET_TESTBED_operation_done (mps[c_m].core_op);
395       mps[c_m].core_op = NULL;
396     }
397     GNUNET_free(mps[c_m].core_connect_ops);
398     GNUNET_free(mps[c_m].partners);
399     mps[c_m].partners = NULL;
400   }
401
402   for (c_s = 0; c_s < num_slaves; c_s++)
403   {
404     if (NULL != sps[c_s].peer_id_op)
405     {
406       GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
407       sps[c_s].peer_id_op = NULL;
408     }
409
410     for (c_op = 0; c_op < num_slaves; c_op++)
411     {
412       if (NULL != sps[c_s].partners[c_op].cth)
413       {
414         GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
415         sps[c_s].partners[c_op].cth = NULL;
416       }
417     }
418
419     if (NULL != sps[c_s].ats_perf_op)
420     {
421       GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
422       sps[c_s].ats_perf_op = NULL;
423     }
424     if (NULL != sps[c_s].core_op)
425     {
426       GNUNET_TESTBED_operation_done (sps[c_s].core_op);
427       sps[c_s].core_op = NULL;
428     }
429
430     GNUNET_free(sps[c_s].partners);
431     sps[c_s].partners = NULL;
432   }
433
434   GNUNET_SCHEDULER_shutdown ();
435 }
436
437 static struct BenchmarkPeer *
438 find_peer (const struct GNUNET_PeerIdentity * peer)
439 {
440   int c_p;
441
442   for (c_p = 0; c_p < num_masters; c_p++)
443   {
444     if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
445       return &mps[c_p];
446   }
447
448   for (c_p = 0; c_p < num_slaves; c_p++)
449   {
450     if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
451       return &sps[c_p];
452   }
453   return NULL ;
454 }
455
456 /**
457  * Controller event callback
458  *
459  * @param cls NULL
460  * @param event the controller event
461  */
462 static void
463 controller_event_cb (void *cls,
464     const struct GNUNET_TESTBED_EventInformation *event)
465 {
466   //struct BenchmarkPeer *p = cls;
467   switch (event->type)
468   {
469   case GNUNET_TESTBED_ET_CONNECT:
470     break;
471   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
472     break;
473   default:
474     GNUNET_break(0);
475     result = 2;
476     GNUNET_SCHEDULER_cancel (shutdown_task);
477     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
478   }
479 }
480
481 static size_t
482 core_send_ready (void *cls, size_t size, void *buf)
483 {
484   static char msgbuf[TEST_MESSAGE_SIZE];
485   struct BenchmarkPartner *partner = cls;
486   struct GNUNET_MessageHeader *msg;
487
488   partner->cth = NULL;
489   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
490       partner->me->no, partner->dest->no);
491
492   partner->messages_sent++;
493   partner->bytes_sent += TEST_MESSAGE_SIZE;
494   partner->me->total_messages_sent++;
495   partner->me->total_bytes_sent += TEST_MESSAGE_SIZE;
496
497   msg = (struct GNUNET_MessageHeader *) &msgbuf;
498   memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
499   msg->type = htons (TEST_MESSAGE_TYPE_PING);
500   msg->size = htons (TEST_MESSAGE_SIZE);
501   memcpy (buf, msg, TEST_MESSAGE_SIZE);
502   return TEST_MESSAGE_SIZE;
503 }
504
505 static void
506 print_progress ()
507 {
508   static int calls;
509   progress_task = GNUNET_SCHEDULER_NO_TASK;
510
511   fprintf (stderr, "%llu..",
512       (long long unsigned) BENCHMARK_DURATION.rel_value_us / (1000 * 1000) - calls);
513   calls++;
514
515   progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
516       &print_progress, NULL );
517 }
518
519 static void
520 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
521 {
522   struct BenchmarkPeer *me = cls;
523
524   me->ats_task = GNUNET_SCHEDULER_NO_TASK;
525
526   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
527       me->no, me->pref_partner->no, me->pref_value);
528   GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
529       &me->pref_partner->id,
530       pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
531   me->pref_value += TEST_ATS_PREFRENCE_DELTA;
532   me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
533       &ats_pref_task, cls);
534 }
535
536 static void
537 do_benchmark ()
538 {
539   int c_m;
540   int c_s;
541
542   if ((state.connected_ATS_service == GNUNET_NO)
543       || (state.connected_CORE_service == GNUNET_NO)
544       || (state.connected_PEERS == GNUNET_NO)
545       || (state.connected_CORE == GNUNET_NO))
546     return;
547
548   state.benchmarking = GNUNET_YES;
549   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
550
551   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
552     GNUNET_SCHEDULER_cancel (shutdown_task);
553   shutdown_task = GNUNET_SCHEDULER_add_delayed (BENCHMARK_DURATION,
554       &do_shutdown, NULL );
555
556   progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
557
558   /* Start sending test messages */
559   for (c_m = 0; c_m < num_masters; c_m++)
560   {
561     for (c_s = 0; c_s < num_slaves; c_s++)
562     {
563       mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
564           mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
565           TEST_MESSAGE_SIZE, &core_send_ready, &mps[c_m].partners[c_s]);
566     }
567     if (pref_val != GNUNET_ATS_PREFERENCE_END)
568       mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
569   }
570 }
571
572 static void
573 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
574     const char *emsg)
575 {
576   struct TestbedConnectOperation *cop = cls;
577   static int ops = 0;
578   int c;
579   if (NULL == emsg)
580   {
581     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
582         _("Connected master [%u] with slave [%u]\n"), cop->master->no,
583         cop->slave->no);
584   }
585   else
586   {
587     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
588         _("Failed to connect master peer [%u] with slave [%u]\n"),
589         cop->master->no, cop->slave->no);
590     GNUNET_break(0);
591     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
592       GNUNET_SCHEDULER_cancel (shutdown_task);
593     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
594   }
595   GNUNET_TESTBED_operation_done (op);
596   ops++;
597   for (c = 0; c < num_slaves; c++)
598   {
599     if (cop == &cop->master->core_connect_ops[c])
600       cop->master->core_connect_ops[c].connect_op = NULL;
601   }
602   if (ops == num_masters * num_slaves)
603   {
604     state.connected_PEERS = GNUNET_YES;
605     GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
606   }
607 }
608
609 static void
610 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
611 {
612   int c_m;
613   int c_s;
614   struct BenchmarkPeer *p;
615
616   if ((state.connected_ATS_service == GNUNET_NO)
617       || (state.connected_CORE_service == GNUNET_NO))
618     return;
619
620   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
621
622   for (c_m = 0; c_m < num_masters; c_m++)
623   {
624     p = &mps[c_m];
625     p->core_connect_ops = GNUNET_malloc (num_slaves *
626         sizeof (struct TestbedConnectOperation));
627
628     for (c_s = 0; c_s < num_slaves; c_s++)
629     {
630       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
631           _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
632       p->core_connect_ops[c_s].master = p;
633       p->core_connect_ops[c_s].slave = &sps[c_s];
634       p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
635           NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
636           sps[c_s].peer, p->peer);
637       if (NULL == p->core_connect_ops[c_s].connect_op)
638       {
639         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
640             _("Could not connect master [%u] and slave [%u]\n"), p->no,
641             sps[c_s].no);
642         GNUNET_break(0);
643         if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
644           GNUNET_SCHEDULER_cancel (shutdown_task);
645         shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
646         return;
647       }
648     }
649   }
650 }
651
652 /**
653  * Method called whenever a given peer connects.
654  *
655  * @param cls closure
656  * @param peer peer identity this notification is about
657  */
658 static void
659 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
660 {
661   struct BenchmarkPeer *me = cls;
662   struct BenchmarkPeer *remote;
663   char *id;
664   int c;
665   int completed;
666
667   remote = find_peer (peer);
668   if (NULL == remote)
669   {
670     GNUNET_break(0);
671     return;
672   }
673
674   id = GNUNET_strdup (GNUNET_i2s (&me->id));
675   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
676       (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
677       (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
678       GNUNET_i2s (peer));
679
680   me->core_connections++;
681   if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
682       && (GNUNET_NO == state.connected_CORE))
683   {
684     me->core_slave_connections++;
685
686     if (me->core_slave_connections == num_slaves)
687     {
688       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
689           me->no);
690     }
691     completed = GNUNET_YES;
692     for (c = 0; c < num_masters; c++)
693     {
694       if (mps[c].core_slave_connections != num_slaves)
695         completed = GNUNET_NO;
696     }
697     if (GNUNET_YES == completed)
698     {
699       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
700           "All master peers connected all slave peers\n", id,
701           GNUNET_i2s (peer));
702       state.connected_CORE = GNUNET_YES;
703       GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
704     }
705   }
706   GNUNET_free(id);
707 }
708
709 static void
710 core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
711 {
712   struct BenchmarkPeer *me = cls;
713   struct BenchmarkPeer *remote;
714   char *id;
715
716   remote = find_peer (peer);
717   if (NULL == remote)
718   {
719     GNUNET_break(0);
720     return;
721   }
722
723   id = GNUNET_strdup (GNUNET_i2s (&me->id));
724   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
725       GNUNET_i2s (peer));
726   GNUNET_assert(me->core_connections > 0);
727   me->core_connections--;
728
729   if ((GNUNET_YES == state.benchmarking)
730       && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
731   {
732     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
733         "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
734   }
735   GNUNET_free(id);
736 }
737
738 static size_t
739 core_send_echo_ready (void *cls, size_t size, void *buf)
740 {
741   static char msgbuf[TEST_MESSAGE_SIZE];
742   struct BenchmarkPartner *p = cls;
743   struct GNUNET_MessageHeader *msg;
744
745   p->cth = NULL;
746
747   p->messages_sent++;
748   p->bytes_sent += TEST_MESSAGE_SIZE;
749   p->me->total_messages_sent++;
750   p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
751
752   msg = (struct GNUNET_MessageHeader *) &msgbuf;
753   memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
754   msg->type = htons (TEST_MESSAGE_TYPE_PONG);
755   msg->size = htons (TEST_MESSAGE_SIZE);
756   memcpy (buf, msg, TEST_MESSAGE_SIZE);
757
758   return TEST_MESSAGE_SIZE;
759 }
760
761 static int
762 core_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
763     const struct GNUNET_MessageHeader *message)
764 {
765   int c_m;
766   struct BenchmarkPeer *me = cls;
767   struct BenchmarkPartner *p = NULL;
768   for (c_m = 0; c_m < num_masters; c_m++)
769   {
770     if (0
771         == memcmp (other, &me->partners[c_m].dest->id,
772             sizeof(struct GNUNET_PeerIdentity)))
773     {
774       p = &me->partners[c_m];
775       break;
776     }
777   }
778   if (NULL == p)
779   {
780     GNUNET_break(0);
781     return GNUNET_SYSERR;
782   }
783   GNUNET_assert(NULL == p->cth);
784
785   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
786       "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
787       p->dest->no);
788
789   p->messages_received++;
790   p->bytes_received += TEST_MESSAGE_SIZE;
791   p->me->total_messages_received++;
792   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
793
794   p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
795       GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
796       &core_send_echo_ready, p);
797   return GNUNET_OK;
798 }
799
800 static int
801 core_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
802     const struct GNUNET_MessageHeader *message)
803 {
804   int c_s;
805   struct BenchmarkPeer *me = cls;
806   struct BenchmarkPartner *p = NULL;
807
808   for (c_s = 0; c_s < num_slaves; c_s++)
809   {
810     if (0
811         == memcmp (other, &me->partners[c_s].dest->id,
812             sizeof(struct GNUNET_PeerIdentity)))
813     {
814       p = &me->partners[c_s];
815       break;
816     }
817   }
818   if (NULL == p)
819   {
820     GNUNET_break(0);
821     return GNUNET_SYSERR;
822   }
823   GNUNET_assert(NULL == p->cth);
824
825   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
826       "Master [%u]: Received PONG from [%u], next message\n", me->no,
827       p->dest->no);
828
829   p->messages_received++;
830   p->bytes_received += TEST_MESSAGE_SIZE;
831   p->me->total_messages_received++;
832   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
833
834   p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
835       GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
836       &core_send_ready, p);
837
838   return GNUNET_OK;
839 }
840
841 static void *
842 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
843 {
844   struct BenchmarkPeer *me = cls;
845
846   static const struct GNUNET_CORE_MessageHandler handlers[] = { {
847       &core_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &core_handle_pong,
848       TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
849
850   me->ch = GNUNET_CORE_connect (cfg, me, NULL, core_connect_cb,
851       core_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
852   if (NULL == me->ch)
853     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
854   return me->ch;
855 }
856
857 static void
858 core_disconnect_adapter (void *cls, void *op_result)
859 {
860   struct BenchmarkPeer *me = cls;
861
862   GNUNET_CORE_disconnect (me->ch);
863   me->ch = NULL;
864 }
865
866 static void
867 core_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
868     void *ca_result, const char *emsg)
869 {
870   static int core_done = 0;
871   if ((NULL != emsg) || (NULL == ca_result))
872   {
873     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
874     GNUNET_break(0);
875     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
876       GNUNET_SCHEDULER_cancel (shutdown_task);
877     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
878     return;
879   }
880   core_done++;
881
882   if (core_done == num_slaves + num_masters)
883   {
884     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all CORE services\n");
885     state.connected_CORE_service = GNUNET_YES;
886     GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
887   }
888 }
889
890 static void
891 do_connect_core (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
892 {
893   int c_s;
894   int c_m;
895   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all CORE services\n");
896   for (c_m = 0; c_m < num_masters; c_m++)
897   {
898     mps[c_m].core_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
899         "core", core_connect_completion_cb, NULL, &core_connect_adapter,
900         &core_disconnect_adapter, &mps[c_m]);
901   }
902
903   for (c_s = 0; c_s < num_slaves; c_s++)
904   {
905     sps[c_s].core_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
906         "core", core_connect_completion_cb, NULL, &core_connect_adapter,
907         &core_disconnect_adapter, &sps[c_s]);
908   }
909 }
910
911 static void
912 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
913     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
914     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
915     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
916 {
917   struct BenchmarkPeer *me = cls;
918   int c_a;
919   char *peer_id;
920
921   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
922   for (c_a = 0; c_a < ats_count; c_a++)
923   {
924     /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
925      (GNUNET_YES == p->master) ? 'M' : 'S',
926      p->no,
927      GNUNET_i2s (&address->peer),
928      GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
929      ntohl(ats[c_a].value));*/
930   }
931 #if 0
932   if ((GNUNET_YES == me->master)
933       && (0 == memcmp (&address->peer, &me->destination->id,
934               sizeof(struct GNUNET_PeerIdentity))))
935   {
936     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
937         me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
938         (long unsigned int ) ntohl (bandwidth_in.value__));
939   }
940
941   store_information (&bp->id, address, address_active, bandwidth_in,
942       bandwidth_out, ats, ats_count);
943 #endif
944   GNUNET_free(peer_id);
945 }
946
947 static void *
948 ats_perf_connect_adapter (void *cls,
949     const struct GNUNET_CONFIGURATION_Handle *cfg)
950 {
951   struct BenchmarkPeer *me = cls;
952
953   me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
954       &ats_performance_info_cb, me);
955   if (NULL == me->ats_perf_handle)
956     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
957         "Failed to create ATS performance handle \n");
958   return me->ats_perf_handle;
959 }
960
961 static void
962 ats_perf_disconnect_adapter (void *cls, void *op_result)
963 {
964   struct BenchmarkPeer *me = cls;
965
966   GNUNET_ATS_performance_done (me->ats_perf_handle);
967   me->ats_perf_handle = NULL;
968 }
969
970 static void
971 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
972     void *ca_result, const char *emsg)
973 {
974   static int op_done = 0;
975
976   if ((NULL != emsg) || (NULL == ca_result))
977   {
978     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
979     GNUNET_break(0);
980     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
981       GNUNET_SCHEDULER_cancel (shutdown_task);
982     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
983     return;
984   }
985   op_done++;
986   if (op_done == (num_masters + num_slaves))
987   {
988     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
989     state.connected_ATS_service = GNUNET_YES;
990     GNUNET_SCHEDULER_add_now (&do_connect_core, NULL );
991   }
992 }
993
994 static void
995 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
996 {
997   int c_m;
998   int c_s;
999
1000   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1001   for (c_m = 0; c_m < num_masters; c_m++)
1002   {
1003     mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1004         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1005         &ats_perf_disconnect_adapter, &mps[c_m]);
1006
1007   }
1008
1009   for (c_s = 0; c_s < num_slaves; c_s++)
1010   {
1011     sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1012         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1013         &ats_perf_disconnect_adapter, &sps[c_s]);
1014   }
1015
1016 }
1017
1018 static void
1019 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1020     const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1021 {
1022   struct BenchmarkPeer *p = cb_cls;
1023   static int done = 0;
1024
1025   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1026
1027   p->id = *pinfo->result.id;
1028   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1029       (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1030       GNUNET_i2s (&p->id));
1031
1032   GNUNET_TESTBED_operation_done (op);
1033   p->peer_id_op = NULL;
1034   done++;
1035
1036   if (done == num_slaves + num_masters)
1037   {
1038     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1039         "Retrieved all peer ID, connect to ATS\n");
1040     GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1041   }
1042 }
1043
1044 /**
1045  * Signature of a main function for a testcase.
1046  *
1047  * @param cls closure
1048  * @param num_peers number of peers in 'peers'
1049  * @param peers_ handle to peers run in the testbed
1050  * @param links_succeeded the number of overlay link connection attempts that
1051  *          succeeded
1052  * @param links_failed the number of overlay link connection attempts that
1053  *          failed
1054  */
1055 static void
1056 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1057     struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1058     unsigned int links_failed)
1059 {
1060   int c_m;
1061   int c_s;
1062   GNUNET_assert(NULL == cls);
1063   GNUNET_assert(num_masters + num_slaves == num_peers);
1064   GNUNET_assert(NULL != peers_);
1065
1066   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1067       _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1068       solver, pref_str, num_masters, num_slaves);
1069
1070   shutdown_task = GNUNET_SCHEDULER_add_delayed (
1071       GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1072       &do_shutdown, NULL );
1073
1074   /* Setup master peers */
1075   for (c_m = 0; c_m < num_masters; c_m++)
1076   {
1077     GNUNET_assert(NULL != peers_[c_m]);
1078     mps[c_m].peer = peers_[c_m];
1079     mps[c_m].no = c_m;
1080     mps[c_m].master = GNUNET_YES;
1081     mps[c_m].pref_partner = &sps[c_m];
1082     mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1083     mps[c_m].partners =
1084         GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1085     /* Initialize partners */
1086     for (c_s = 0; c_s < num_slaves; c_s++)
1087     {
1088       mps[c_m].partners[c_s].me = &mps[c_m];
1089       mps[c_m].partners[c_s].dest = &sps[c_s];
1090     }
1091     mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1092         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1093   }
1094
1095   /* Setup slave peers */
1096   for (c_s = 0; c_s < num_slaves; c_s++)
1097   {
1098     GNUNET_assert(NULL != peers_[c_s + num_masters]);
1099     sps[c_s].peer = peers_[c_s + num_masters];
1100     sps[c_s].no = c_s + num_masters;
1101     sps[c_s].master = GNUNET_NO;
1102     sps[c_s].partners =
1103         GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1104     /* Initialize partners */
1105     for (c_m = 0; c_m < num_masters; c_m++)
1106     {
1107       sps[c_s].partners[c_m].me = &sps[c_s];
1108       sps[c_s].partners[c_m].dest = &mps[c_m];
1109     }
1110     sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1111         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1112   }
1113 }
1114
1115 int
1116 main (int argc, char *argv[])
1117 {
1118   char *tmp;
1119   char *tmp_sep;
1120   char *test_name;
1121   char *conf_name;
1122   char *dotexe;
1123   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1124   int c;
1125
1126   result = 0;
1127
1128   /* figure out testname */
1129   tmp = strstr (argv[0], TESTNAME_PREFIX);
1130   if (NULL == tmp)
1131   {
1132     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1133     return GNUNET_SYSERR;
1134   }
1135   tmp += strlen (TESTNAME_PREFIX);
1136   solver = GNUNET_strdup (tmp);
1137   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1138     dotexe[0] = '\0';
1139   tmp_sep = strchr (solver, '_');
1140   if (NULL == tmp_sep)
1141   {
1142     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1143     GNUNET_free(solver);
1144     return GNUNET_SYSERR;
1145   }
1146   tmp_sep[0] = '\0';
1147   pref_str = GNUNET_strdup(tmp_sep + 1);
1148
1149   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1150       pref_str);
1151   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1152
1153   for (c = 0; c <= strlen (pref_str); c++)
1154     pref_str[c] = toupper (pref_str[c]);
1155   pref_val = -1;
1156
1157   if (0 != strcmp (pref_str, "NONE"))
1158   {
1159     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1160     {
1161       if (0 == strcmp (pref_str, prefs[c]))
1162       {
1163         pref_val = c;
1164         break;
1165       }
1166     }
1167   }
1168   else
1169   {
1170     /* abuse terminator to indicate no pref */
1171     pref_val = GNUNET_ATS_PREFERENCE_END;
1172   }
1173   if (-1 == pref_val)
1174   {
1175     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1176     GNUNET_free(solver);
1177     GNUNET_free(pref_str);
1178     return -1;
1179   }
1180
1181   for (c = 0; c < (argc - 1); c++)
1182   {
1183     if (0 == strcmp (argv[c], "-s"))
1184       break;
1185   }
1186   if (c < argc - 1)
1187   {
1188     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1189         && (num_slaves >= 1))
1190       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1191     else
1192       num_slaves = DEFAULT_SLAVES_NUM;
1193   }
1194   else
1195     num_slaves = DEFAULT_SLAVES_NUM;
1196
1197   for (c = 0; c < (argc - 1); c++)
1198   {
1199     if (0 == strcmp (argv[c], "-m"))
1200       break;
1201   }
1202   if (c < argc - 1)
1203   {
1204     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1205         && (num_masters >= 2))
1206       fprintf (stderr, "Starting %u master peers\n", num_masters);
1207     else
1208       num_masters = DEFAULT_MASTERS_NUM;
1209   }
1210   else
1211     num_masters = DEFAULT_MASTERS_NUM;
1212
1213   if (num_slaves < num_masters)
1214   {
1215     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1216     GNUNET_free(test_name);
1217     GNUNET_free(solver);
1218     GNUNET_free(pref_str);
1219     return GNUNET_SYSERR;
1220   }
1221
1222   state.connected_ATS_service = GNUNET_NO;
1223   state.connected_CORE_service = GNUNET_NO;
1224   state.connected_PEERS = GNUNET_NO;
1225   state.benchmarking = GNUNET_NO;
1226   state.connected_PEERS = GNUNET_NO;
1227
1228   mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1229   sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1230
1231   /* Start topology */
1232   uint64_t event_mask;
1233   event_mask = 0;
1234   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1235   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1236   (void) GNUNET_TESTBED_test_run ("perf_ats", conf_name,
1237       num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1238       &main_run, NULL );
1239
1240   GNUNET_free(solver);
1241   GNUNET_free(pref_str);
1242   GNUNET_free(conf_name);
1243   GNUNET_free(test_name);
1244   GNUNET_free(mps);
1245   GNUNET_free(sps);
1246
1247   return result;
1248 }
1249
1250 /* end of file perf_ats.c */