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