fixes for ats measurement
[oweals/gnunet.git] / src / ats-tests / ats-testing.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 "ats-testing.h"
32
33
34
35
36 static struct GNUNET_ATS_TEST_Topology *top;
37
38 /**
39  * Shutdown nicely
40  *
41  * @param cls NULL
42  * @param tc the task context
43  */
44 static void
45 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 {
47   int c_m;
48   int c_s;
49   int c_op;
50   struct BenchmarkPeer *p;
51 /*
52   if (GNUNET_YES == logging)
53     perf_logging_stop();
54 */
55   top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
56 /*
57   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
58   {
59     fprintf (stderr, "0\n");
60     GNUNET_SCHEDULER_cancel (progress_task);
61   }
62   progress_task = GNUNET_SCHEDULER_NO_TASK;
63 */
64   //evaluate ();
65
66   top->state.benchmarking = GNUNET_NO;
67   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
68
69   for (c_m = 0; c_m < top->num_masters; c_m++)
70   {
71     p = &top->mps[c_m];
72     if (NULL != top->mps[c_m].peer_id_op)
73     {
74       GNUNET_TESTBED_operation_done (p->peer_id_op);
75       p->peer_id_op = NULL;
76     }
77
78     if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
79       GNUNET_SCHEDULER_cancel (p->ats_task);
80     p->ats_task = GNUNET_SCHEDULER_NO_TASK;
81
82     for (c_op = 0; c_op < p->num_partners; c_op++)
83     {
84       if (NULL != p->partners[c_op].cth)
85       {
86         GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
87         p->partners[c_op].cth = NULL;
88       }
89       if (NULL != p->partners[c_op].tth)
90       {
91         GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
92         p->partners[c_op].tth = NULL;
93       }
94       if ( (NULL != p->core_connect_ops) &&
95            (NULL != p->core_connect_ops[c_op].connect_op) )
96       {
97         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
98             _("Failed to connect peer 0 and %u\n"), c_op);
99         GNUNET_TESTBED_operation_done (
100             p->core_connect_ops[c_op].connect_op);
101         p->core_connect_ops[c_op].connect_op = NULL;
102       }
103     }
104
105     if (NULL != p->ats_perf_op)
106     {
107       GNUNET_TESTBED_operation_done (p->ats_perf_op);
108       p->ats_perf_op = NULL;
109     }
110
111     if (NULL != p->comm_op)
112     {
113       GNUNET_TESTBED_operation_done (p->comm_op);
114       p->comm_op = NULL;
115     }
116     GNUNET_free_non_null (p->core_connect_ops);
117     GNUNET_free(p->partners);
118     p->partners = NULL;
119   }
120
121   for (c_s = 0; c_s < top->num_slaves; c_s++)
122   {
123     p = &top->sps[c_s];
124     if (NULL != p->peer_id_op)
125     {
126       GNUNET_TESTBED_operation_done (p->peer_id_op);
127       p->peer_id_op = NULL;
128     }
129
130     for (c_op = 0; c_op < p->num_partners; c_op++)
131     {
132       if (NULL != p->partners[c_op].cth)
133       {
134         GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
135         p->partners[c_op].cth = NULL;
136       }
137       if (NULL != p->partners[c_op].tth)
138       {
139         GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
140         p->partners[c_op].tth = NULL;
141       }
142     }
143     if (NULL != p->ats_perf_op)
144     {
145       GNUNET_TESTBED_operation_done (p->ats_perf_op);
146       p->ats_perf_op = NULL;
147     }
148     if (NULL != p->comm_op)
149     {
150       GNUNET_TESTBED_operation_done (p->comm_op);
151       p->comm_op = NULL;
152     }
153     GNUNET_free(p->partners);
154     p->partners = NULL;
155   }
156
157   GNUNET_SCHEDULER_shutdown ();
158 }
159
160 static struct BenchmarkPartner *
161 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
162 {
163   int c_m;
164   GNUNET_assert (NULL != me);
165   GNUNET_assert (NULL != peer);
166
167   for (c_m = 0; c_m < me->num_partners; c_m++)
168   {
169     /* Find a partner with other as destination */
170     if (0 == memcmp (peer, &me->partners[c_m].dest->id,
171             sizeof(struct GNUNET_PeerIdentity)))
172     {
173       return &me->partners[c_m];
174     }
175   }
176
177   return NULL;
178 }
179
180
181 static struct BenchmarkPeer *
182 find_peer (const struct GNUNET_PeerIdentity * peer)
183 {
184   int c_p;
185
186   for (c_p = 0; c_p < top->num_masters; c_p++)
187   {
188     if (0 == memcmp (&top->mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
189       return &top->mps[c_p];
190   }
191
192   for (c_p = 0; c_p < top->num_slaves; c_p++)
193   {
194     if (0 == memcmp (&top->sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
195       return &top->sps[c_p];
196   }
197   return NULL ;
198 }
199
200
201 /**
202  * Method called whenever a given peer connects.
203  *
204  * @param cls closure
205  * @param peer peer identity this notification is about
206  */
207 static void
208 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
209 {
210   struct BenchmarkPeer *me = cls;
211   struct BenchmarkPeer *remote;
212   char *id;
213   int c;
214   int completed;
215
216   remote = find_peer (peer);
217   if (NULL == remote)
218   {
219     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown peer connected: `%s'\n", GNUNET_i2s (peer));
220     GNUNET_break(0);
221     return;
222   }
223
224   id = GNUNET_strdup (GNUNET_i2s (&me->id));
225   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
226       (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
227       (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
228       GNUNET_i2s (peer));
229
230   me->core_connections++;
231   if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
232       && (GNUNET_NO == top->state.connected_CORE))
233   {
234     me->core_slave_connections++;
235
236     if (me->core_slave_connections == top->num_slaves)
237     {
238       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
239           me->no);
240     }
241     completed = GNUNET_YES;
242     for (c = 0; c < top->num_masters; c++)
243     {
244       if (top->mps[c].core_slave_connections != top->num_slaves)
245         completed = GNUNET_NO;
246     }
247     if (GNUNET_YES == completed)
248     {
249       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250           "All master peers connected all slave peers\n", id,
251           GNUNET_i2s (peer));
252       top->state.connected_CORE = GNUNET_YES;
253     }
254   }
255   GNUNET_free(id);
256 }
257
258 static void
259 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
260 {
261   struct BenchmarkPeer *me = cls;
262   struct BenchmarkPartner *p;
263   char *id;
264
265   if (NULL == (p = find_partner (me, peer)))
266     return;
267
268   id = GNUNET_strdup (GNUNET_i2s (&me->id));
269   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
270       GNUNET_i2s (peer));
271   GNUNET_assert(me->core_connections > 0);
272   me->core_connections--;
273
274   if ((GNUNET_YES == top->state.benchmarking)
275       && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
276   {
277     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
278         "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
279     if (NULL != p->tth)
280     {
281       GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
282       p->tth = NULL;
283     }
284     if (NULL != p->cth)
285     {
286       GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
287       p->cth = NULL;
288     }
289   }
290   GNUNET_free(id);
291 }
292
293
294 static void *
295 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
296 {
297   struct BenchmarkPeer *me = cls;
298
299   me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
300       comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, top->handlers);
301   if (NULL == me->ch)
302     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
303   return me->ch;
304 }
305
306 static void
307 core_disconnect_adapter (void *cls, void *op_result)
308 {
309   struct BenchmarkPeer *me = cls;
310
311   GNUNET_CORE_disconnect (me->ch);
312   me->ch = NULL;
313 }
314
315
316 static void *
317 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
318 {
319   struct BenchmarkPeer *me = cls;
320
321   me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, top->transport_recv_cb,
322       &comm_connect_cb, &comm_disconnect_cb);
323   if (NULL == me->th)
324     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
325   return me->th;
326 }
327
328 static void
329 transport_disconnect_adapter (void *cls, void *op_result)
330 {
331   struct BenchmarkPeer *me = cls;
332
333   GNUNET_TRANSPORT_disconnect (me->th);
334   me->th = NULL;
335 }
336
337
338 static void
339 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
340     const char *emsg)
341 {
342   struct TestbedConnectOperation *cop = cls;
343   static int ops = 0;
344   int c;
345   if (NULL == emsg)
346   {
347     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
348         _("Connected master [%u] with slave [%u]\n"), cop->master->no,
349         cop->slave->no);
350   }
351   else
352   {
353     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
354         _("Failed to connect master peer [%u] with slave [%u]\n"),
355         cop->master->no, cop->slave->no);
356     GNUNET_break(0);
357     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
358       GNUNET_SCHEDULER_cancel (top->shutdown_task);
359     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
360   }
361   GNUNET_TESTBED_operation_done (op);
362   ops++;
363   for (c = 0; c < top->num_slaves; c++)
364   {
365     if (cop == &cop->master->core_connect_ops[c])
366       cop->master->core_connect_ops[c].connect_op = NULL;
367   }
368   if (ops == top->num_masters * top->num_slaves)
369   {
370     top->state.connected_PEERS = GNUNET_YES;
371     /* Notify about setup done */
372     if (NULL != top->done_cb)
373       top->done_cb (top->done_cb_cls, top->mps, top->sps);
374   }
375 }
376
377 static void
378 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
379 {
380   int c_m;
381   int c_s;
382   struct BenchmarkPeer *p;
383
384   if ((top->state.connected_ATS_service == GNUNET_NO) ||
385       (top->state.connected_COMM_service == GNUNET_NO))
386     return;
387
388   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
389
390   for (c_m = 0; c_m < top->num_masters; c_m++)
391   {
392     p = &top->mps[c_m];
393     p->core_connect_ops = GNUNET_malloc (top->num_slaves *
394         sizeof (struct TestbedConnectOperation));
395
396     for (c_s = 0; c_s < top->num_slaves; c_s++)
397     {
398       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
399           _("Connecting master [%u] with slave [%u]\n"), p->no, top->sps[c_s].no);
400       p->core_connect_ops[c_s].master = p;
401       p->core_connect_ops[c_s].slave = &top->sps[c_s];
402       p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
403           NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
404           top->sps[c_s].peer, p->peer);
405       if (NULL == p->core_connect_ops[c_s].connect_op)
406       {
407         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
408             _("Could not connect master [%u] and slave [%u]\n"), p->no,
409             top->sps[c_s].no);
410         GNUNET_break(0);
411         if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
412           GNUNET_SCHEDULER_cancel (top->shutdown_task);
413         top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
414         return;
415       }
416     }
417   }
418 }
419
420
421 static void
422 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
423     void *ca_result, const char *emsg)
424 {
425   static int comm_done = 0;
426   if ((NULL != emsg) || (NULL == ca_result))
427   {
428     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
429     GNUNET_break(0);
430     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
431       GNUNET_SCHEDULER_cancel (top->shutdown_task);
432     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
433     return;
434   }
435   comm_done++;
436
437   if (comm_done == top->num_slaves + top->num_masters)
438   {
439     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
440         (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
441     top->state.connected_COMM_service = GNUNET_YES;
442     GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
443   }
444 }
445
446 static void
447 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 {
449   int c_s;
450   int c_m;
451   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
452       (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
453   for (c_m = 0; c_m < top->num_masters; c_m++)
454   {
455     if (GNUNET_YES == top->test_core)
456       top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
457         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
458         &core_disconnect_adapter, &top->mps[c_m]);
459     else
460     {
461       top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
462         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
463         &transport_disconnect_adapter, &top->mps[c_m]);
464     }
465   }
466
467   for (c_s = 0; c_s < top->num_slaves; c_s++)
468   {
469     if (GNUNET_YES == top->test_core)
470       top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
471         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
472         &core_disconnect_adapter, &top->sps[c_s]);
473     else
474     {
475       top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
476         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
477         &transport_disconnect_adapter, &top->sps[c_s]);
478     }
479   }
480 }
481
482
483 static void *
484 ats_perf_connect_adapter (void *cls,
485     const struct GNUNET_CONFIGURATION_Handle *cfg)
486 {
487   struct BenchmarkPeer *me = cls;
488
489   me->ats_perf_handle = GNUNET_ATS_performance_init (cfg, top->ats_perf_cb, me);
490   if (NULL == me->ats_perf_handle)
491     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
492         "Failed to create ATS performance handle \n");
493   return me->ats_perf_handle;
494 }
495
496 static void
497 ats_perf_disconnect_adapter (void *cls, void *op_result)
498 {
499   struct BenchmarkPeer *me = cls;
500
501   GNUNET_ATS_performance_done (me->ats_perf_handle);
502   me->ats_perf_handle = NULL;
503 }
504
505 static void
506 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
507     void *ca_result, const char *emsg)
508 {
509   static int op_done = 0;
510
511   if ((NULL != emsg) || (NULL == ca_result))
512   {
513     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
514     GNUNET_break(0);
515     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
516       GNUNET_SCHEDULER_cancel (top->shutdown_task);
517     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
518     return;
519   }
520   op_done++;
521   if (op_done == (top->num_masters + top->num_slaves))
522   {
523     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
524     top->state.connected_ATS_service = GNUNET_YES;
525     GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
526   }
527 }
528
529
530 static void
531 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 {
533   int c_m;
534   int c_s;
535
536   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
537   for (c_m = 0; c_m < top->num_masters; c_m++)
538   {
539     top->mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL,
540         top->mps[c_m].peer,
541         "ats", ats_connect_completion_cb, NULL,
542         &ats_perf_connect_adapter,
543         &ats_perf_disconnect_adapter, &top->mps[c_m]);
544   }
545
546   for (c_s = 0; c_s < top->num_slaves; c_s++)
547   {
548     top->sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
549         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
550         &ats_perf_disconnect_adapter, &top->sps[c_s]);
551   }
552 }
553
554
555
556 static void
557 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
558     const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
559 {
560   struct BenchmarkPeer *p = cb_cls;
561   static int done = 0;
562
563   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
564
565   p->id = *pinfo->result.id;
566   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
567       (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
568       GNUNET_i2s (&p->id));
569
570   GNUNET_TESTBED_operation_done (op);
571   p->peer_id_op = NULL;
572   done++;
573
574   if (done == top->num_slaves + top->num_masters)
575   {
576     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
577         "Retrieved all peer ID, connect to ATS\n");
578     GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
579   }
580 }
581
582 /**
583  * Signature of a main function for a testcase.
584  *
585  * @param cls closure
586  * @param num_peers number of peers in 'peers'
587  * @param peers_ handle to peers run in the testbed
588  * @param links_succeeded the number of overlay link connection attempts that
589  *          succeeded
590  * @param links_failed the number of overlay link connection attempts that
591  *          failed
592  */
593 static void
594 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
595           unsigned int num_peers,
596           struct GNUNET_TESTBED_Peer **peers_,
597           unsigned int links_succeeded,
598           unsigned int links_failed)
599 {
600   int c_m;
601   int c_s;
602   GNUNET_assert(NULL == cls);
603   GNUNET_assert(top->num_masters + top->num_slaves == num_peers);
604   GNUNET_assert(NULL != peers_);
605
606   top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
607       GNUNET_TIME_relative_multiply (TEST_TIMEOUT, top->num_masters + top->num_slaves),
608       &do_shutdown, top );
609
610   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting up %u masters and %u slaves\n",
611       top->num_masters, top->num_slaves);
612
613   /* Setup master peers */
614   for (c_m = 0; c_m < top->num_masters; c_m++)
615   {
616     GNUNET_assert(NULL != peers_[c_m]);
617     top->mps[c_m].peer = peers_[c_m];
618     top->mps[c_m].no = c_m;
619     top->mps[c_m].master = GNUNET_YES;
620     top->mps[c_m].pref_partner = &top->sps[c_m];
621     top->mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
622     top->mps[c_m].partners =
623         GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
624     top->mps[c_m].num_partners = top->num_slaves;
625     /* Initialize partners */
626     for (c_s = 0; c_s < top->num_slaves; c_s++)
627     {
628       top->mps[c_m].partners[c_s].me = &top->mps[c_m];
629       top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
630     }
631     /* Get configuration */
632     top->mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
633         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->mps[c_m]);
634   }
635
636   /* Setup slave peers */
637   for (c_s = 0; c_s < top->num_slaves; c_s++)
638   {
639     GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
640     top->sps[c_s].peer = peers_[c_s + top->num_masters];
641     top->sps[c_s].no = c_s + top->num_masters;
642     top->sps[c_s].master = GNUNET_NO;
643     top->sps[c_s].partners =
644         GNUNET_malloc (top->num_masters * sizeof (struct BenchmarkPartner));
645     top->sps[c_s].num_partners = top->num_masters;
646     /* Initialize partners */
647     for (c_m = 0; c_m < top->num_masters; c_m++)
648     {
649       top->sps[c_s].partners[c_m].me = &top->sps[c_s];
650       top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
651     }
652     /* Get configuration */
653     top->sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
654         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->sps[c_s]);
655   }
656 }
657
658 /**
659  * Controller event callback
660  *
661  * @param cls NULL
662  * @param event the controller event
663  */
664 static void
665 controller_event_cb (void *cls,
666     const struct GNUNET_TESTBED_EventInformation *event)
667 {
668   struct GNUNET_ATS_TEST_Topology *top = cls;
669   switch (event->type)
670   {
671   case GNUNET_TESTBED_ET_CONNECT:
672     break;
673   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
674     break;
675   default:
676     GNUNET_break(0);
677     GNUNET_SCHEDULER_cancel (top->shutdown_task);
678     top->shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
679   }
680 }
681
682 void
683 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
684     unsigned int num_slaves,
685     unsigned int num_masters,
686     int test_core,
687     GNUNET_ATS_TESTING_TopologySetupDoneCallback done_cb,
688     void *done_cb_cls,
689     struct GNUNET_CORE_MessageHandler *handlers,
690     GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
691     GNUNET_ATS_AddressInformationCallback ats_perf_cb)
692 {
693
694   top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
695   top->num_masters = num_masters;
696   top->num_slaves = num_slaves;
697   top->handlers = handlers;
698   top->done_cb = done_cb;
699   top->done_cb_cls = done_cb_cls;
700   top->test_core = test_core;
701   top->transport_recv_cb = transport_recv_cb;
702   top->ats_perf_cb = ats_perf_cb;
703
704   top->mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
705   top->sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
706
707   /* Start topology */
708   uint64_t event_mask;
709   event_mask = 0;
710   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
711   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
712   (void) GNUNET_TESTBED_test_run (name, cfg_file,
713       num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
714       &main_run, NULL);
715 }
716
717 void
718 GNUNET_ATS_TEST_shutdown_topology (void)
719 {
720   GNUNET_SCHEDULER_shutdown();
721 }
722
723 /* end of file perf_ats.c */