2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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"
36 static struct GNUNET_ATS_TEST_Topology *top;
42 * @param tc the task context
45 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 struct BenchmarkPeer *p;
52 if (GNUNET_YES == logging)
55 top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
57 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
59 fprintf (stderr, "0\n");
60 GNUNET_SCHEDULER_cancel (progress_task);
62 progress_task = GNUNET_SCHEDULER_NO_TASK;
66 top->state.benchmarking = GNUNET_NO;
67 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
69 for (c_m = 0; c_m < top->num_masters; c_m++)
72 if (NULL != top->mps[c_m].peer_id_op)
74 GNUNET_TESTBED_operation_done (p->peer_id_op);
78 if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
79 GNUNET_SCHEDULER_cancel (p->ats_task);
80 p->ats_task = GNUNET_SCHEDULER_NO_TASK;
82 for (c_op = 0; c_op < p->num_partners; c_op++)
84 if (NULL != p->partners[c_op].cth)
86 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
87 p->partners[c_op].cth = NULL;
89 if (NULL != p->partners[c_op].tth)
91 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
92 p->partners[c_op].tth = NULL;
94 if ( (NULL != p->core_connect_ops) &&
95 (NULL != p->core_connect_ops[c_op].connect_op) )
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;
105 if (NULL != p->ats_perf_op)
107 GNUNET_TESTBED_operation_done (p->ats_perf_op);
108 p->ats_perf_op = NULL;
111 if (NULL != p->comm_op)
113 GNUNET_TESTBED_operation_done (p->comm_op);
116 GNUNET_free_non_null (p->core_connect_ops);
117 GNUNET_free(p->partners);
121 for (c_s = 0; c_s < top->num_slaves; c_s++)
124 if (NULL != p->peer_id_op)
126 GNUNET_TESTBED_operation_done (p->peer_id_op);
127 p->peer_id_op = NULL;
130 for (c_op = 0; c_op < p->num_partners; c_op++)
132 if (NULL != p->partners[c_op].cth)
134 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
135 p->partners[c_op].cth = NULL;
137 if (NULL != p->partners[c_op].tth)
139 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
140 p->partners[c_op].tth = NULL;
143 if (NULL != p->ats_perf_op)
145 GNUNET_TESTBED_operation_done (p->ats_perf_op);
146 p->ats_perf_op = NULL;
148 if (NULL != p->comm_op)
150 GNUNET_TESTBED_operation_done (p->comm_op);
153 GNUNET_free(p->partners);
157 GNUNET_SCHEDULER_shutdown ();
160 static struct BenchmarkPartner *
161 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
164 GNUNET_assert (NULL != me);
165 GNUNET_assert (NULL != peer);
167 for (c_m = 0; c_m < me->num_partners; c_m++)
169 /* Find a partner with other as destination */
170 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
171 sizeof(struct GNUNET_PeerIdentity)))
173 return &me->partners[c_m];
181 static struct BenchmarkPeer *
182 find_peer (const struct GNUNET_PeerIdentity * peer)
186 for (c_p = 0; c_p < top->num_masters; c_p++)
188 if (0 == memcmp (&top->mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
189 return &top->mps[c_p];
192 for (c_p = 0; c_p < top->num_slaves; c_p++)
194 if (0 == memcmp (&top->sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
195 return &top->sps[c_p];
202 * Method called whenever a given peer connects.
205 * @param peer peer identity this notification is about
208 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
210 struct BenchmarkPeer *me = cls;
211 struct BenchmarkPeer *remote;
216 remote = find_peer (peer);
219 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown peer connected: `%s'\n", GNUNET_i2s (peer));
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,
230 me->core_connections++;
231 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
232 && (GNUNET_NO == top->state.connected_CORE))
234 me->core_slave_connections++;
236 if (me->core_slave_connections == top->num_slaves)
238 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
241 completed = GNUNET_YES;
242 for (c = 0; c < top->num_masters; c++)
244 if (top->mps[c].core_slave_connections != top->num_slaves)
245 completed = GNUNET_NO;
247 if (GNUNET_YES == completed)
249 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250 "All master peers connected all slave peers\n", id,
252 top->state.connected_CORE = GNUNET_YES;
259 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
261 struct BenchmarkPeer *me = cls;
262 struct BenchmarkPartner *p;
265 if (NULL == (p = find_partner (me, peer)))
268 id = GNUNET_strdup (GNUNET_i2s (&me->id));
269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
271 GNUNET_assert(me->core_connections > 0);
272 me->core_connections--;
274 if ((GNUNET_YES == top->state.benchmarking)
275 && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
277 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
278 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
281 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
286 GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
295 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
297 struct BenchmarkPeer *me = cls;
299 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
300 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, top->handlers);
302 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
307 core_disconnect_adapter (void *cls, void *op_result)
309 struct BenchmarkPeer *me = cls;
311 GNUNET_CORE_disconnect (me->ch);
317 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
319 struct BenchmarkPeer *me = cls;
321 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, top->transport_recv_cb,
322 &comm_connect_cb, &comm_disconnect_cb);
324 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
329 transport_disconnect_adapter (void *cls, void *op_result)
331 struct BenchmarkPeer *me = cls;
333 GNUNET_TRANSPORT_disconnect (me->th);
339 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
342 struct TestbedConnectOperation *cop = cls;
347 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
348 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
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);
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 );
361 GNUNET_TESTBED_operation_done (op);
363 for (c = 0; c < top->num_slaves; c++)
365 if (cop == &cop->master->core_connect_ops[c])
366 cop->master->core_connect_ops[c].connect_op = NULL;
368 if (ops == top->num_masters * top->num_slaves)
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);
378 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
382 struct BenchmarkPeer *p;
384 if ((top->state.connected_ATS_service == GNUNET_NO) ||
385 (top->state.connected_COMM_service == GNUNET_NO))
388 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
390 for (c_m = 0; c_m < top->num_masters; c_m++)
393 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
394 sizeof (struct TestbedConnectOperation));
396 for (c_s = 0; c_s < top->num_slaves; c_s++)
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)
407 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
408 _("Could not connect master [%u] and slave [%u]\n"), p->no,
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 );
422 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
423 void *ca_result, const char *emsg)
425 static int comm_done = 0;
426 if ((NULL != emsg) || (NULL == ca_result))
428 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
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 );
437 if (comm_done == top->num_slaves + top->num_masters)
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 );
447 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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++)
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]);
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]);
467 for (c_s = 0; c_s < top->num_slaves; c_s++)
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]);
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]);
484 ats_perf_connect_adapter (void *cls,
485 const struct GNUNET_CONFIGURATION_Handle *cfg)
487 struct BenchmarkPeer *me = cls;
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;
497 ats_perf_disconnect_adapter (void *cls, void *op_result)
499 struct BenchmarkPeer *me = cls;
501 GNUNET_ATS_performance_done (me->ats_perf_handle);
502 me->ats_perf_handle = NULL;
506 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
507 void *ca_result, const char *emsg)
509 static int op_done = 0;
511 if ((NULL != emsg) || (NULL == ca_result))
513 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
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 );
521 if (op_done == (top->num_masters + top->num_slaves))
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 );
531 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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++)
539 top->mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL,
541 "ats", ats_connect_completion_cb, NULL,
542 &ats_perf_connect_adapter,
543 &ats_perf_disconnect_adapter, &top->mps[c_m]);
546 for (c_s = 0; c_s < top->num_slaves; c_s++)
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]);
557 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
558 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
560 struct BenchmarkPeer *p = cb_cls;
563 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
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));
570 GNUNET_TESTBED_operation_done (op);
571 p->peer_id_op = NULL;
574 if (done == top->num_slaves + top->num_masters)
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 );
583 * Signature of a main function for a testcase.
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
590 * @param links_failed the number of overlay link connection attempts that
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)
602 GNUNET_assert(NULL == cls);
603 GNUNET_assert(top->num_masters + top->num_slaves == num_peers);
604 GNUNET_assert(NULL != peers_);
606 top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
607 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, top->num_masters + top->num_slaves),
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting up %u masters and %u slaves\n",
611 top->num_masters, top->num_slaves);
613 /* Setup master peers */
614 for (c_m = 0; c_m < top->num_masters; c_m++)
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++)
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];
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]);
636 /* Setup slave peers */
637 for (c_s = 0; c_s < top->num_slaves; c_s++)
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++)
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];
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]);
659 * Controller event callback
662 * @param event the controller event
665 controller_event_cb (void *cls,
666 const struct GNUNET_TESTBED_EventInformation *event)
668 struct GNUNET_ATS_TEST_Topology *top = cls;
671 case GNUNET_TESTBED_ET_CONNECT:
673 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
677 GNUNET_SCHEDULER_cancel (top->shutdown_task);
678 top->shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
683 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
684 unsigned int num_slaves,
685 unsigned int num_masters,
687 GNUNET_ATS_TESTING_TopologySetupDoneCallback done_cb,
689 struct GNUNET_CORE_MessageHandler *handlers,
690 GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
691 GNUNET_ATS_AddressInformationCallback ats_perf_cb)
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;
704 top->mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
705 top->sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
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,
718 GNUNET_ATS_TEST_shutdown_topology (void)
720 GNUNET_SCHEDULER_shutdown();
723 /* end of file perf_ats.c */