basic log file writing functionality
[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 *partner = cls;
363   struct GNUNET_MessageHeader *msg;
364
365   if (GNUNET_YES == test_core)
366     partner->cth = NULL;
367   else
368     partner->tth = NULL;
369
370   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
371       partner->me->no, partner->dest->no);
372
373   partner->messages_sent++;
374   partner->bytes_sent += TEST_MESSAGE_SIZE;
375   partner->me->total_messages_sent++;
376   partner->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 static void
463 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
464     const char *emsg)
465 {
466   struct TestbedConnectOperation *cop = cls;
467   static int ops = 0;
468   int c;
469   if (NULL == emsg)
470   {
471     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
472         _("Connected master [%u] with slave [%u]\n"), cop->master->no,
473         cop->slave->no);
474   }
475   else
476   {
477     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
478         _("Failed to connect master peer [%u] with slave [%u]\n"),
479         cop->master->no, cop->slave->no);
480     GNUNET_break(0);
481     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
482       GNUNET_SCHEDULER_cancel (shutdown_task);
483     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
484   }
485   GNUNET_TESTBED_operation_done (op);
486   ops++;
487   for (c = 0; c < num_slaves; c++)
488   {
489     if (cop == &cop->master->core_connect_ops[c])
490       cop->master->core_connect_ops[c].connect_op = NULL;
491   }
492   if (ops == num_masters * num_slaves)
493   {
494     state.connected_PEERS = GNUNET_YES;
495     GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
496   }
497 }
498
499 static void
500 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
501 {
502   int c_m;
503   int c_s;
504   struct BenchmarkPeer *p;
505
506   if ((state.connected_ATS_service == GNUNET_NO)
507       || (state.connected_COMM_service == GNUNET_NO))
508     return;
509
510   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
511
512   for (c_m = 0; c_m < num_masters; c_m++)
513   {
514     p = &mps[c_m];
515     p->core_connect_ops = GNUNET_malloc (num_slaves *
516         sizeof (struct TestbedConnectOperation));
517
518     for (c_s = 0; c_s < num_slaves; c_s++)
519     {
520       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
521           _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
522       p->core_connect_ops[c_s].master = p;
523       p->core_connect_ops[c_s].slave = &sps[c_s];
524       p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
525           NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
526           sps[c_s].peer, p->peer);
527       if (NULL == p->core_connect_ops[c_s].connect_op)
528       {
529         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
530             _("Could not connect master [%u] and slave [%u]\n"), p->no,
531             sps[c_s].no);
532         GNUNET_break(0);
533         if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
534           GNUNET_SCHEDULER_cancel (shutdown_task);
535         shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
536         return;
537       }
538     }
539   }
540 }
541
542 /**
543  * Method called whenever a given peer connects.
544  *
545  * @param cls closure
546  * @param peer peer identity this notification is about
547  */
548 static void
549 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
550 {
551   struct BenchmarkPeer *me = cls;
552   struct BenchmarkPeer *remote;
553   char *id;
554   int c;
555   int completed;
556
557   remote = find_peer (peer);
558   if (NULL == remote)
559   {
560     GNUNET_break(0);
561     return;
562   }
563
564   id = GNUNET_strdup (GNUNET_i2s (&me->id));
565   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
566       (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
567       (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
568       GNUNET_i2s (peer));
569
570   me->core_connections++;
571   if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
572       && (GNUNET_NO == state.connected_CORE))
573   {
574     me->core_slave_connections++;
575
576     if (me->core_slave_connections == num_slaves)
577     {
578       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
579           me->no);
580     }
581     completed = GNUNET_YES;
582     for (c = 0; c < num_masters; c++)
583     {
584       if (mps[c].core_slave_connections != num_slaves)
585         completed = GNUNET_NO;
586     }
587     if (GNUNET_YES == completed)
588     {
589       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
590           "All master peers connected all slave peers\n", id,
591           GNUNET_i2s (peer));
592       state.connected_CORE = GNUNET_YES;
593       GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
594     }
595   }
596   GNUNET_free(id);
597 }
598
599 static void
600 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
601 {
602   struct BenchmarkPeer *me = cls;
603   struct BenchmarkPeer *remote;
604   char *id;
605
606   remote = find_peer (peer);
607   if (NULL == remote)
608   {
609     GNUNET_break(0);
610     return;
611   }
612
613   id = GNUNET_strdup (GNUNET_i2s (&me->id));
614   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
615       GNUNET_i2s (peer));
616   GNUNET_assert(me->core_connections > 0);
617   me->core_connections--;
618
619   if ((GNUNET_YES == state.benchmarking)
620       && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
621   {
622     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
623         "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
624   }
625   GNUNET_free(id);
626 }
627
628 static size_t
629 comm_send_pong_ready (void *cls, size_t size, void *buf)
630 {
631   static char msgbuf[TEST_MESSAGE_SIZE];
632   struct BenchmarkPartner *p = cls;
633   struct GNUNET_MessageHeader *msg;
634
635   if (GNUNET_YES == test_core)
636     p->cth = NULL;
637   else
638     p->tth = NULL;
639
640   p->messages_sent++;
641   p->bytes_sent += TEST_MESSAGE_SIZE;
642   p->me->total_messages_sent++;
643   p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
644
645   msg = (struct GNUNET_MessageHeader *) &msgbuf;
646   memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
647   msg->type = htons (TEST_MESSAGE_TYPE_PONG);
648   msg->size = htons (TEST_MESSAGE_SIZE);
649   memcpy (buf, msg, TEST_MESSAGE_SIZE);
650
651   return TEST_MESSAGE_SIZE;
652 }
653
654 static int
655 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
656     const struct GNUNET_MessageHeader *message)
657 {
658   int c_m;
659   struct BenchmarkPeer *me = cls;
660   struct BenchmarkPartner *p = NULL;
661   for (c_m = 0; c_m < num_masters; c_m++)
662   {
663     if (0
664         == memcmp (other, &me->partners[c_m].dest->id,
665             sizeof(struct GNUNET_PeerIdentity)))
666     {
667       p = &me->partners[c_m];
668       break;
669     }
670   }
671   if (NULL == p)
672   {
673     GNUNET_break(0);
674     return GNUNET_SYSERR;
675   }
676
677   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
678       "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
679       p->dest->no);
680
681   p->messages_received++;
682   p->bytes_received += TEST_MESSAGE_SIZE;
683   p->me->total_messages_received++;
684   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
685
686   if (GNUNET_YES == test_core)
687   {
688     GNUNET_assert (NULL == p->cth);
689     p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
690         GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
691         &comm_send_pong_ready, p);
692   }
693   else
694   {
695     GNUNET_assert (NULL == p->tth);
696     p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
697         TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
698         p);
699   }
700   return GNUNET_OK;
701 }
702
703 static int
704 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
705     const struct GNUNET_MessageHeader *message)
706 {
707   int c_s;
708   struct BenchmarkPeer *me = cls;
709   struct BenchmarkPartner *p = NULL;
710
711   for (c_s = 0; c_s < num_slaves; c_s++)
712   {
713     if (0
714         == memcmp (other, &me->partners[c_s].dest->id,
715             sizeof(struct GNUNET_PeerIdentity)))
716     {
717       p = &me->partners[c_s];
718       break;
719     }
720   }
721   if (NULL == p)
722   {
723     GNUNET_break(0);
724     return GNUNET_SYSERR;
725   }
726
727   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
728       "Master [%u]: Received PONG from [%u], next message\n", me->no,
729       p->dest->no);
730
731   p->messages_received++;
732   p->bytes_received += TEST_MESSAGE_SIZE;
733   p->me->total_messages_received++;
734   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
735
736   if (GNUNET_YES == test_core)
737   {
738     GNUNET_assert (NULL == p->cth);
739     p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
740         GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
741         &comm_send_ready, p);
742   }
743   else
744   {
745     GNUNET_assert (NULL == p->tth);
746     p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
747       me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
748       &comm_send_ready, p);
749   }
750
751   return GNUNET_OK;
752 }
753
754 static void *
755 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
756 {
757   struct BenchmarkPeer *me = cls;
758
759   static const struct GNUNET_CORE_MessageHandler handlers[] = { {
760       &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
761       TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
762
763   me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
764       comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
765   if (NULL == me->ch)
766     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
767   return me->ch;
768 }
769
770 static void
771 core_disconnect_adapter (void *cls, void *op_result)
772 {
773   struct BenchmarkPeer *me = cls;
774
775   GNUNET_CORE_disconnect (me->ch);
776   me->ch = NULL;
777 }
778
779 static void
780 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
781     void *ca_result, const char *emsg)
782 {
783   static int comm_done = 0;
784   if ((NULL != emsg) || (NULL == ca_result))
785   {
786     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
787     GNUNET_break(0);
788     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
789       GNUNET_SCHEDULER_cancel (shutdown_task);
790     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
791     return;
792   }
793   comm_done++;
794
795   if (comm_done == num_slaves + num_masters)
796   {
797     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
798         (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
799     state.connected_COMM_service = GNUNET_YES;
800     GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
801   }
802 }
803
804 static void
805 transport_recv_cb (void *cls,
806                    const struct GNUNET_PeerIdentity * peer,
807                    const struct GNUNET_MessageHeader * message)
808 {
809   if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
810       (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
811       TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
812   {
813     return;
814   }
815   if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
816     comm_handle_ping (cls, peer, message);
817
818   if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
819     comm_handle_pong (cls, peer, message);
820 }
821
822
823 static void *
824 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
825 {
826   struct BenchmarkPeer *me = cls;
827
828   me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me,  &transport_recv_cb,
829       &comm_connect_cb, &comm_disconnect_cb);
830   if (NULL == me->th)
831     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
832   return me->th;
833 }
834
835 static void
836 transport_disconnect_adapter (void *cls, void *op_result)
837 {
838   struct BenchmarkPeer *me = cls;
839
840   GNUNET_TRANSPORT_disconnect (me->th);
841   me->th = NULL;
842 }
843
844 static void
845 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
846 {
847   int c_s;
848   int c_m;
849   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
850       (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
851   for (c_m = 0; c_m < num_masters; c_m++)
852   {
853     if (GNUNET_YES == test_core)
854       mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
855         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
856         &core_disconnect_adapter, &mps[c_m]);
857     else
858     {
859       mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
860         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
861         &transport_disconnect_adapter, &mps[c_m]);
862     }
863   }
864
865   for (c_s = 0; c_s < num_slaves; c_s++)
866   {
867     if (GNUNET_YES == test_core)
868       sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
869         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
870         &core_disconnect_adapter, &sps[c_s]);
871     else
872     {
873       sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
874         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
875         &transport_disconnect_adapter, &sps[c_s]);
876     }
877   }
878 }
879
880 static void
881 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
882     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
883     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
884     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
885 {
886   struct BenchmarkPeer *me = cls;
887   int c_a;
888   char *peer_id;
889
890   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
891   for (c_a = 0; c_a < ats_count; c_a++)
892   {
893     /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
894      (GNUNET_YES == p->master) ? 'M' : 'S',
895      p->no,
896      GNUNET_i2s (&address->peer),
897      GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
898      ntohl(ats[c_a].value));*/
899   }
900 #if 0
901   if ((GNUNET_YES == me->master)
902       && (0 == memcmp (&address->peer, &me->destination->id,
903               sizeof(struct GNUNET_PeerIdentity))))
904   {
905     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
906         me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
907         (long unsigned int ) ntohl (bandwidth_in.value__));
908   }
909
910   store_information (&bp->id, address, address_active, bandwidth_in,
911       bandwidth_out, ats, ats_count);
912 #endif
913   GNUNET_free(peer_id);
914 }
915
916 static void *
917 ats_perf_connect_adapter (void *cls,
918     const struct GNUNET_CONFIGURATION_Handle *cfg)
919 {
920   struct BenchmarkPeer *me = cls;
921
922   me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
923       &ats_performance_info_cb, me);
924   if (NULL == me->ats_perf_handle)
925     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
926         "Failed to create ATS performance handle \n");
927   return me->ats_perf_handle;
928 }
929
930 static void
931 ats_perf_disconnect_adapter (void *cls, void *op_result)
932 {
933   struct BenchmarkPeer *me = cls;
934
935   GNUNET_ATS_performance_done (me->ats_perf_handle);
936   me->ats_perf_handle = NULL;
937 }
938
939 static void
940 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
941     void *ca_result, const char *emsg)
942 {
943   static int op_done = 0;
944
945   if ((NULL != emsg) || (NULL == ca_result))
946   {
947     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
948     GNUNET_break(0);
949     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
950       GNUNET_SCHEDULER_cancel (shutdown_task);
951     shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
952     return;
953   }
954   op_done++;
955   if (op_done == (num_masters + num_slaves))
956   {
957     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
958     state.connected_ATS_service = GNUNET_YES;
959     GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
960   }
961 }
962
963 static void
964 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
965 {
966   int c_m;
967   int c_s;
968
969   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
970   for (c_m = 0; c_m < num_masters; c_m++)
971   {
972     mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
973         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
974         &ats_perf_disconnect_adapter, &mps[c_m]);
975
976   }
977
978   for (c_s = 0; c_s < num_slaves; c_s++)
979   {
980     sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
981         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
982         &ats_perf_disconnect_adapter, &sps[c_s]);
983   }
984
985 }
986
987 static void
988 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
989     const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
990 {
991   struct BenchmarkPeer *p = cb_cls;
992   static int done = 0;
993
994   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
995
996   p->id = *pinfo->result.id;
997   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
998       (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
999       GNUNET_i2s (&p->id));
1000
1001   GNUNET_TESTBED_operation_done (op);
1002   p->peer_id_op = NULL;
1003   done++;
1004
1005   if (done == num_slaves + num_masters)
1006   {
1007     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1008         "Retrieved all peer ID, connect to ATS\n");
1009     GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1010   }
1011 }
1012
1013 /**
1014  * Signature of a main function for a testcase.
1015  *
1016  * @param cls closure
1017  * @param num_peers number of peers in 'peers'
1018  * @param peers_ handle to peers run in the testbed
1019  * @param links_succeeded the number of overlay link connection attempts that
1020  *          succeeded
1021  * @param links_failed the number of overlay link connection attempts that
1022  *          failed
1023  */
1024 static void
1025 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1026     struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1027     unsigned int links_failed)
1028 {
1029   int c_m;
1030   int c_s;
1031   GNUNET_assert(NULL == cls);
1032   GNUNET_assert(num_masters + num_slaves == num_peers);
1033   GNUNET_assert(NULL != peers_);
1034
1035   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1036       _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1037       solver, pref_str, num_masters, num_slaves);
1038
1039   shutdown_task = GNUNET_SCHEDULER_add_delayed (
1040       GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1041       &do_shutdown, NULL );
1042
1043   /* Setup master peers */
1044   for (c_m = 0; c_m < num_masters; c_m++)
1045   {
1046     GNUNET_assert(NULL != peers_[c_m]);
1047     mps[c_m].peer = peers_[c_m];
1048     mps[c_m].no = c_m;
1049     mps[c_m].master = GNUNET_YES;
1050     mps[c_m].pref_partner = &sps[c_m];
1051     mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1052     mps[c_m].partners =
1053         GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1054     mps[c_m].num_partners = num_slaves;
1055     /* Initialize partners */
1056     for (c_s = 0; c_s < num_slaves; c_s++)
1057     {
1058       mps[c_m].partners[c_s].me = &mps[c_m];
1059       mps[c_m].partners[c_s].dest = &sps[c_s];
1060     }
1061     mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1062         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1063   }
1064
1065   /* Setup slave peers */
1066   for (c_s = 0; c_s < num_slaves; c_s++)
1067   {
1068     GNUNET_assert(NULL != peers_[c_s + num_masters]);
1069     sps[c_s].peer = peers_[c_s + num_masters];
1070     sps[c_s].no = c_s + num_masters;
1071     sps[c_s].master = GNUNET_NO;
1072     sps[c_s].partners =
1073         GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1074     sps[c_s].num_partners = num_masters;
1075     /* Initialize partners */
1076     for (c_m = 0; c_m < num_masters; c_m++)
1077     {
1078       sps[c_s].partners[c_m].me = &sps[c_s];
1079       sps[c_s].partners[c_m].dest = &mps[c_m];
1080     }
1081     sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1082         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1083   }
1084 }
1085
1086 int
1087 main (int argc, char *argv[])
1088 {
1089   char *tmp;
1090   char *tmp_sep;
1091   char *test_name;
1092   char *conf_name;
1093   char *comm_name;
1094   char *dotexe;
1095   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1096   int c;
1097
1098   result = 0;
1099
1100   /* figure out testname */
1101   tmp = strstr (argv[0], TESTNAME_PREFIX);
1102   if (NULL == tmp)
1103   {
1104     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1105     return GNUNET_SYSERR;
1106   }
1107   tmp += strlen (TESTNAME_PREFIX);
1108   solver = GNUNET_strdup (tmp);
1109   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1110     dotexe[0] = '\0';
1111   tmp_sep = strchr (solver, '_');
1112   if (NULL == tmp_sep)
1113   {
1114     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1115     GNUNET_free(solver);
1116     return GNUNET_SYSERR;
1117   }
1118   tmp_sep[0] = '\0';
1119   comm_name = GNUNET_strdup (&tmp_sep[1]);
1120   tmp_sep = strchr (comm_name, '_');
1121   if (NULL == tmp_sep)
1122   {
1123     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1124     GNUNET_free(solver);
1125     return GNUNET_SYSERR;
1126   }
1127   tmp_sep[0] = '\0';
1128   for (c = 0; c <= strlen (comm_name); c++)
1129     comm_name[c] = toupper (comm_name[c]);
1130   if (0 == strcmp (comm_name, "CORE"))
1131     test_core = GNUNET_YES;
1132   else if (0 == strcmp (comm_name, "TRANSPORT"))
1133     test_core = GNUNET_NO;
1134   else
1135   {
1136     GNUNET_free (comm_name);
1137     GNUNET_free (solver);
1138     return GNUNET_SYSERR;
1139   }
1140
1141   pref_str = GNUNET_strdup(tmp_sep + 1);
1142
1143   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1144       pref_str);
1145   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1146
1147   for (c = 0; c <= strlen (pref_str); c++)
1148     pref_str[c] = toupper (pref_str[c]);
1149   pref_val = -1;
1150
1151   if (0 != strcmp (pref_str, "NONE"))
1152   {
1153     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1154     {
1155       if (0 == strcmp (pref_str, prefs[c]))
1156       {
1157         pref_val = c;
1158         break;
1159       }
1160     }
1161   }
1162   else
1163   {
1164     /* abuse terminator to indicate no pref */
1165     pref_val = GNUNET_ATS_PREFERENCE_END;
1166   }
1167   if (-1 == pref_val)
1168   {
1169     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1170     GNUNET_free(solver);
1171     GNUNET_free(pref_str);
1172     GNUNET_free (comm_name);
1173     return -1;
1174   }
1175
1176   for (c = 0; c < (argc - 1); c++)
1177   {
1178     if (0 == strcmp (argv[c], "-s"))
1179       break;
1180   }
1181   if (c < argc - 1)
1182   {
1183     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1184         && (num_slaves >= 1))
1185       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1186     else
1187       num_slaves = DEFAULT_SLAVES_NUM;
1188   }
1189   else
1190     num_slaves = DEFAULT_SLAVES_NUM;
1191
1192   for (c = 0; c < (argc - 1); c++)
1193   {
1194     if (0 == strcmp (argv[c], "-m"))
1195       break;
1196   }
1197   if (c < argc - 1)
1198   {
1199     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1200         && (num_masters >= 2))
1201       fprintf (stderr, "Starting %u master peers\n", num_masters);
1202     else
1203       num_masters = DEFAULT_MASTERS_NUM;
1204   }
1205   else
1206     num_masters = DEFAULT_MASTERS_NUM;
1207
1208   logging = GNUNET_NO;
1209   for (c = 0; c < argc; c++)
1210   {
1211     if (0 == strcmp (argv[c], "-l"))
1212       logging = GNUNET_YES;
1213   }
1214
1215   GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1216
1217   if (num_slaves < num_masters)
1218   {
1219     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1220     GNUNET_free(test_name);
1221     GNUNET_free(solver);
1222     GNUNET_free(pref_str);
1223     GNUNET_free (comm_name);
1224     return GNUNET_SYSERR;
1225   }
1226
1227   state.connected_ATS_service = GNUNET_NO;
1228   state.connected_COMM_service = GNUNET_NO;
1229   state.connected_PEERS = GNUNET_NO;
1230   state.benchmarking = GNUNET_NO;
1231   state.connected_PEERS = GNUNET_NO;
1232
1233   mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1234   sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1235
1236   /* Start topology */
1237   uint64_t event_mask;
1238   event_mask = 0;
1239   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1240   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1241   (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1242       num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1243       &main_run, NULL );
1244
1245   GNUNET_free(solver);
1246   GNUNET_free(pref_str);
1247   GNUNET_free(conf_name);
1248   GNUNET_free(test_name);
1249   GNUNET_free(testname);
1250   GNUNET_free (comm_name);
1251   GNUNET_free(mps);
1252   GNUNET_free(sps);
1253
1254   return result;
1255 }
1256
1257 /* end of file perf_ats.c */