822666b2f062fd253095ad2f2f52f29b871f7a51
[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   int c_s;
754   struct BenchmarkPeer *me = cls;
755   struct BenchmarkPartner *p = NULL;
756
757   for (c_s = 0; c_s < num_slaves; c_s++)
758   {
759     if (0
760         == memcmp (other, &me->partners[c_s].dest->id,
761             sizeof(struct GNUNET_PeerIdentity)))
762     {
763       p = &me->partners[c_s];
764       break;
765     }
766   }
767   if (NULL == p)
768   {
769     GNUNET_break(0);
770     return GNUNET_SYSERR;
771   }
772
773
774   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
775       "Master [%u]: Received PONG from [%u], next message\n", me->no,
776       p->dest->no);
777
778   p->messages_received++;
779   p->bytes_received += TEST_MESSAGE_SIZE;
780   p->me->total_messages_received++;
781   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
782   p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
783       GNUNET_TIME_absolute_get()).rel_value_us;
784
785   comm_schedule_send (p);
786   return GNUNET_OK;
787 }
788
789 static void *
790 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
791 {
792   struct BenchmarkPeer *me = cls;
793
794   static const struct GNUNET_CORE_MessageHandler handlers[] = { {
795       &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
796       TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
797
798   me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
799       comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
800   if (NULL == me->ch)
801     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
802   return me->ch;
803 }
804
805 static void
806 core_disconnect_adapter (void *cls, void *op_result)
807 {
808   struct BenchmarkPeer *me = cls;
809
810   GNUNET_CORE_disconnect (me->ch);
811   me->ch = NULL;
812 }
813
814 static void
815 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
816     void *ca_result, const char *emsg)
817 {
818   static int comm_done = 0;
819   if ((NULL != emsg) || (NULL == ca_result))
820   {
821     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
822     GNUNET_break(0);
823     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
824       GNUNET_SCHEDULER_cancel (shutdown_task);
825     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
826     return;
827   }
828   comm_done++;
829
830   if (comm_done == num_slaves + num_masters)
831   {
832     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
833         (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
834     state.connected_COMM_service = GNUNET_YES;
835     GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
836   }
837 }
838
839 static void
840 transport_recv_cb (void *cls,
841                    const struct GNUNET_PeerIdentity * peer,
842                    const struct GNUNET_MessageHeader * message)
843 {
844   if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
845       (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
846       TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
847   {
848     return;
849   }
850   if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
851     comm_handle_ping (cls, peer, message);
852
853   if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
854     comm_handle_pong (cls, peer, message);
855 }
856
857
858 static void *
859 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
860 {
861   struct BenchmarkPeer *me = cls;
862
863   me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me,  &transport_recv_cb,
864       &comm_connect_cb, &comm_disconnect_cb);
865   if (NULL == me->th)
866     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
867   return me->th;
868 }
869
870 static void
871 transport_disconnect_adapter (void *cls, void *op_result)
872 {
873   struct BenchmarkPeer *me = cls;
874
875   GNUNET_TRANSPORT_disconnect (me->th);
876   me->th = NULL;
877 }
878
879 static void
880 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
881 {
882   int c_s;
883   int c_m;
884   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
885       (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
886   for (c_m = 0; c_m < num_masters; c_m++)
887   {
888     if (GNUNET_YES == test_core)
889       mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
890         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
891         &core_disconnect_adapter, &mps[c_m]);
892     else
893     {
894       mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
895         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
896         &transport_disconnect_adapter, &mps[c_m]);
897     }
898   }
899
900   for (c_s = 0; c_s < num_slaves; c_s++)
901   {
902     if (GNUNET_YES == test_core)
903       sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
904         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
905         &core_disconnect_adapter, &sps[c_s]);
906     else
907     {
908       sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
909         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
910         &transport_disconnect_adapter, &sps[c_s]);
911     }
912   }
913 }
914
915 static void
916 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
917     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
918     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
919     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
920 {
921   struct BenchmarkPeer *me = cls;
922   struct BenchmarkPartner *p;
923   int c_s;
924   int c_a;
925   char *peer_id;
926
927   p = NULL;
928   for (c_s = 0; c_s < me->num_partners; c_s++)
929   {
930
931     if (0 == memcmp (&address->peer, &me->partners[c_s].dest->id,
932         sizeof (struct GNUNET_PeerIdentity)))
933     {
934       p = &me->partners[c_s];
935       break;
936     }
937
938   }
939
940   if (NULL == p)
941   {
942     /* This is not one of my partners
943      * Will happen since the peers will connect to each other due to gossiping
944      */
945     return;
946   }
947
948   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
949
950   for (c_a = 0; c_a < ats_count; c_a++)
951   {
952     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
953         (GNUNET_YES == p->me->master) ? "Master" : "Slave",
954         p->me->no,
955         GNUNET_i2s (&p->dest->id),
956         GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
957         ntohl(ats[c_a].value));
958     switch (ntohl (ats[c_a].type ))
959     {
960       case GNUNET_ATS_ARRAY_TERMINATOR:
961         break;
962       case GNUNET_ATS_UTILIZATION_UP:
963         p->ats_utilization_up = ntohl (ats[c_a].value);
964         break;
965       case GNUNET_ATS_UTILIZATION_DOWN:
966         p->ats_utilization_down = ntohl (ats[c_a].value);
967         break;
968       case GNUNET_ATS_NETWORK_TYPE:
969         p->ats_network_type = ntohl (ats[c_a].value);
970         break;
971       case GNUNET_ATS_QUALITY_NET_DELAY:
972         p->ats_delay = ntohl (ats[c_a].value);
973         break;
974       case GNUNET_ATS_QUALITY_NET_DISTANCE:
975         p->ats_distance = ntohl (ats[c_a].value);
976         GNUNET_break (0);
977         break;
978       case GNUNET_ATS_COST_WAN:
979         p->ats_cost_wan = ntohl (ats[c_a].value);
980         break;
981       case GNUNET_ATS_COST_LAN:
982         p->ats_cost_lan = ntohl (ats[c_a].value);
983         break;
984       case GNUNET_ATS_COST_WLAN:
985         p->ats_cost_wlan = ntohl (ats[c_a].value);
986         break;
987         break;
988       default:
989         break;
990     }
991   }
992   if (GNUNET_YES == logging)
993     collect_log_now();
994   GNUNET_free(peer_id);
995 }
996
997 static void *
998 ats_perf_connect_adapter (void *cls,
999     const struct GNUNET_CONFIGURATION_Handle *cfg)
1000 {
1001   struct BenchmarkPeer *me = cls;
1002
1003   me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
1004       &ats_performance_info_cb, me);
1005   if (NULL == me->ats_perf_handle)
1006     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1007         "Failed to create ATS performance handle \n");
1008   return me->ats_perf_handle;
1009 }
1010
1011 static void
1012 ats_perf_disconnect_adapter (void *cls, void *op_result)
1013 {
1014   struct BenchmarkPeer *me = cls;
1015
1016   GNUNET_ATS_performance_done (me->ats_perf_handle);
1017   me->ats_perf_handle = NULL;
1018 }
1019
1020 static void
1021 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1022     void *ca_result, const char *emsg)
1023 {
1024   static int op_done = 0;
1025
1026   if ((NULL != emsg) || (NULL == ca_result))
1027   {
1028     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
1029     GNUNET_break(0);
1030     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
1031       GNUNET_SCHEDULER_cancel (shutdown_task);
1032     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
1033     return;
1034   }
1035   op_done++;
1036   if (op_done == (num_masters + num_slaves))
1037   {
1038     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
1039     state.connected_ATS_service = GNUNET_YES;
1040     GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
1041   }
1042 }
1043
1044 static void
1045 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 {
1047   int c_m;
1048   int c_s;
1049
1050   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1051   for (c_m = 0; c_m < num_masters; c_m++)
1052   {
1053     mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1054         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1055         &ats_perf_disconnect_adapter, &mps[c_m]);
1056
1057   }
1058
1059   for (c_s = 0; c_s < num_slaves; c_s++)
1060   {
1061     sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1062         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1063         &ats_perf_disconnect_adapter, &sps[c_s]);
1064   }
1065
1066 }
1067
1068 static void
1069 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1070     const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1071 {
1072   struct BenchmarkPeer *p = cb_cls;
1073   static int done = 0;
1074
1075   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1076
1077   p->id = *pinfo->result.id;
1078   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1079       (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1080       GNUNET_i2s (&p->id));
1081
1082   GNUNET_TESTBED_operation_done (op);
1083   p->peer_id_op = NULL;
1084   done++;
1085
1086   if (done == num_slaves + num_masters)
1087   {
1088     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1089         "Retrieved all peer ID, connect to ATS\n");
1090     GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1091   }
1092 }
1093
1094 /**
1095  * Signature of a main function for a testcase.
1096  *
1097  * @param cls closure
1098  * @param num_peers number of peers in 'peers'
1099  * @param peers_ handle to peers run in the testbed
1100  * @param links_succeeded the number of overlay link connection attempts that
1101  *          succeeded
1102  * @param links_failed the number of overlay link connection attempts that
1103  *          failed
1104  */
1105 static void
1106 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1107     struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1108     unsigned int links_failed)
1109 {
1110   int c_m;
1111   int c_s;
1112   GNUNET_assert(NULL == cls);
1113   GNUNET_assert(num_masters + num_slaves == num_peers);
1114   GNUNET_assert(NULL != peers_);
1115
1116   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1117       _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1118       solver, pref_str, num_masters, num_slaves);
1119
1120   shutdown_task = GNUNET_SCHEDULER_add_delayed (
1121       GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1122       &do_shutdown, NULL );
1123
1124   /* Setup master peers */
1125   for (c_m = 0; c_m < num_masters; c_m++)
1126   {
1127     GNUNET_assert(NULL != peers_[c_m]);
1128     mps[c_m].peer = peers_[c_m];
1129     mps[c_m].no = c_m;
1130     mps[c_m].master = GNUNET_YES;
1131     mps[c_m].pref_partner = &sps[c_m];
1132     mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1133     mps[c_m].partners =
1134         GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1135     mps[c_m].num_partners = num_slaves;
1136     /* Initialize partners */
1137     for (c_s = 0; c_s < num_slaves; c_s++)
1138     {
1139       mps[c_m].partners[c_s].me = &mps[c_m];
1140       mps[c_m].partners[c_s].dest = &sps[c_s];
1141     }
1142     mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1143         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1144   }
1145
1146   /* Setup slave peers */
1147   for (c_s = 0; c_s < num_slaves; c_s++)
1148   {
1149     GNUNET_assert(NULL != peers_[c_s + num_masters]);
1150     sps[c_s].peer = peers_[c_s + num_masters];
1151     sps[c_s].no = c_s + num_masters;
1152     sps[c_s].master = GNUNET_NO;
1153     sps[c_s].partners =
1154         GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1155     sps[c_s].num_partners = num_masters;
1156     /* Initialize partners */
1157     for (c_m = 0; c_m < num_masters; c_m++)
1158     {
1159       sps[c_s].partners[c_m].me = &sps[c_s];
1160       sps[c_s].partners[c_m].dest = &mps[c_m];
1161     }
1162     sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1163         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1164   }
1165 }
1166
1167 int
1168 main (int argc, char *argv[])
1169 {
1170   char *tmp;
1171   char *tmp_sep;
1172   char *test_name;
1173   char *conf_name;
1174   char *comm_name;
1175   char *dotexe;
1176   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1177   int c;
1178
1179   result = 0;
1180
1181   /* figure out testname */
1182   tmp = strstr (argv[0], TESTNAME_PREFIX);
1183   if (NULL == tmp)
1184   {
1185     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1186     return GNUNET_SYSERR;
1187   }
1188   tmp += strlen (TESTNAME_PREFIX);
1189   solver = GNUNET_strdup (tmp);
1190   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1191     dotexe[0] = '\0';
1192   tmp_sep = strchr (solver, '_');
1193   if (NULL == tmp_sep)
1194   {
1195     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1196     GNUNET_free(solver);
1197     return GNUNET_SYSERR;
1198   }
1199   tmp_sep[0] = '\0';
1200   comm_name = GNUNET_strdup (&tmp_sep[1]);
1201   tmp_sep = strchr (comm_name, '_');
1202   if (NULL == tmp_sep)
1203   {
1204     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1205     GNUNET_free(solver);
1206     return GNUNET_SYSERR;
1207   }
1208   tmp_sep[0] = '\0';
1209   for (c = 0; c <= strlen (comm_name); c++)
1210     comm_name[c] = toupper (comm_name[c]);
1211   if (0 == strcmp (comm_name, "CORE"))
1212     test_core = GNUNET_YES;
1213   else if (0 == strcmp (comm_name, "TRANSPORT"))
1214     test_core = GNUNET_NO;
1215   else
1216   {
1217     GNUNET_free (comm_name);
1218     GNUNET_free (solver);
1219     return GNUNET_SYSERR;
1220   }
1221
1222   pref_str = GNUNET_strdup(tmp_sep + 1);
1223
1224   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1225       pref_str);
1226   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1227
1228   for (c = 0; c <= strlen (pref_str); c++)
1229     pref_str[c] = toupper (pref_str[c]);
1230   pref_val = -1;
1231
1232   if (0 != strcmp (pref_str, "NONE"))
1233   {
1234     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1235     {
1236       if (0 == strcmp (pref_str, prefs[c]))
1237       {
1238         pref_val = c;
1239         break;
1240       }
1241     }
1242   }
1243   else
1244   {
1245     /* abuse terminator to indicate no pref */
1246     pref_val = GNUNET_ATS_PREFERENCE_END;
1247   }
1248   if (-1 == pref_val)
1249   {
1250     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1251     GNUNET_free(solver);
1252     GNUNET_free(pref_str);
1253     GNUNET_free (comm_name);
1254     return -1;
1255   }
1256
1257   for (c = 0; c < (argc - 1); c++)
1258   {
1259     if (0 == strcmp (argv[c], "-d"))
1260       break;
1261   }
1262   if (c < argc - 1)
1263   {
1264     if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1265         fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1266   }
1267   else
1268   {
1269     perf_duration = BENCHMARK_DURATION;
1270   }
1271   fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1272
1273   for (c = 0; c < (argc - 1); c++)
1274   {
1275     if (0 == strcmp (argv[c], "-s"))
1276       break;
1277   }
1278   if (c < argc - 1)
1279   {
1280     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1281         && (num_slaves >= 1))
1282       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1283     else
1284       num_slaves = DEFAULT_SLAVES_NUM;
1285   }
1286   else
1287     num_slaves = DEFAULT_SLAVES_NUM;
1288
1289   for (c = 0; c < (argc - 1); c++)
1290   {
1291     if (0 == strcmp (argv[c], "-m"))
1292       break;
1293   }
1294   if (c < argc - 1)
1295   {
1296     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1297         && (num_masters >= 2))
1298       fprintf (stderr, "Starting %u master peers\n", num_masters);
1299     else
1300       num_masters = DEFAULT_MASTERS_NUM;
1301   }
1302   else
1303     num_masters = DEFAULT_MASTERS_NUM;
1304
1305   logging = GNUNET_NO;
1306   for (c = 0; c < argc; c++)
1307   {
1308     if (0 == strcmp (argv[c], "-l"))
1309       logging = GNUNET_YES;
1310   }
1311
1312   if (GNUNET_YES == logging)
1313   {
1314     for (c = 0; c < (argc - 1); c++)
1315     {
1316       if (0 == strcmp (argv[c], "-f"))
1317         break;
1318     }
1319     if (c < argc - 1)
1320     {
1321       if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
1322           fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1323     }
1324     else
1325     {
1326       log_frequency = LOGGING_FREQUENCY;
1327     }
1328     fprintf (stderr, "Using log frequency %llu ms\n",
1329         (unsigned long long) (log_frequency.rel_value_us) / (1000));
1330   }
1331
1332   GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1333
1334   if (num_slaves < num_masters)
1335   {
1336     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1337     GNUNET_free(test_name);
1338     GNUNET_free(solver);
1339     GNUNET_free(pref_str);
1340     GNUNET_free (comm_name);
1341     return GNUNET_SYSERR;
1342   }
1343
1344   state.connected_ATS_service = GNUNET_NO;
1345   state.connected_COMM_service = GNUNET_NO;
1346   state.connected_PEERS = GNUNET_NO;
1347   state.benchmarking = GNUNET_NO;
1348   state.connected_PEERS = GNUNET_NO;
1349
1350   mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1351   sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1352
1353   /* Start topology */
1354   uint64_t event_mask;
1355   event_mask = 0;
1356   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1357   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1358   (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1359       num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1360       &main_run, NULL );
1361
1362   GNUNET_free(solver);
1363   GNUNET_free(pref_str);
1364   GNUNET_free(conf_name);
1365   GNUNET_free(test_name);
1366   GNUNET_free(testname);
1367   GNUNET_free (comm_name);
1368   GNUNET_free(mps);
1369   GNUNET_free(sps);
1370
1371   return result;
1372 }
1373
1374 /* end of file perf_ats.c */