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