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