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