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