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"
34 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_ATS_PREFRENCE_START 1.0
36 #define TEST_ATS_PREFRENCE_DELTA 1.0
38 #define TEST_MESSAGE_TYPE_PING 12345
39 #define TEST_MESSAGE_TYPE_PONG 12346
40 #define TEST_MESSAGE_SIZE 1000
41 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 * Connect peers with testbed
46 struct TestbedConnectOperation
49 * The benchmarking master initiating this connection
51 struct BenchmarkPeer *master;
54 * The benchmarking slave to connect to
56 struct BenchmarkPeer *slave;
59 * Testbed operation to connect peers
61 struct GNUNET_TESTBED_Operation *connect_op;
65 * Overall state of the performance benchmark
69 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
70 int connected_ATS_service;
72 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
73 int connected_COMM_service;
75 /* Are we connected to all peers: GNUNET_YES/NO */
78 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
81 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
88 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
93 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
101 * Test result logging
105 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
107 static int test_core;
117 static char *testname;
122 static char *pref_str;
125 * ATS preference value
130 * Number master peers
132 static int num_masters;
135 * Array of master peers
137 static struct BenchmarkPeer *mps;
142 static int num_slaves;
144 * Array of slave peers
146 static struct BenchmarkPeer *sps;
151 static struct GNUNET_TIME_Relative perf_duration;
156 static struct GNUNET_TIME_Relative log_frequency;
161 static struct BenchmarkState state;
168 unsigned int duration;
169 struct BenchmarkPeer *mp;
170 struct BenchmarkPartner *p;
172 duration = (perf_duration.rel_value_us / (1000 * 1000));
173 for (c_m = 0; c_m < num_masters; c_m++)
177 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
178 mp->no, mp->total_bytes_sent / 1024, duration,
179 (mp->total_bytes_sent / 1024) / duration,
180 mp->total_bytes_received / 1024, duration,
181 (mp->total_bytes_received / 1024) / duration);
183 for (c_s = 0; c_s < num_slaves; c_s++)
185 p = &mp->partners[c_s];
187 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
188 (mp->pref_partner == p->dest) ? '*' : ' ',
190 (p->bytes_sent / 1024) / duration,
191 ((double) p->bytes_sent * 100) / mp->total_bytes_sent,
192 (p->bytes_received / 1024) / duration,
193 ((double) p->bytes_received * 100) / mp->total_bytes_received );
195 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
196 (mp->pref_partner == p->dest) ? '*' : ' ',
198 p->total_app_rtt / (1000 * p->messages_sent));
207 * @param tc the task context
210 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 struct BenchmarkPeer *p;
217 if (GNUNET_YES == logging)
220 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
221 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
223 fprintf (stderr, "0\n");
224 GNUNET_SCHEDULER_cancel (progress_task);
226 progress_task = GNUNET_SCHEDULER_NO_TASK;
229 state.benchmarking = GNUNET_NO;
230 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
232 for (c_m = 0; c_m < num_masters; c_m++)
235 if (NULL != mps[c_m].peer_id_op)
237 GNUNET_TESTBED_operation_done (p->peer_id_op);
238 p->peer_id_op = NULL;
241 if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
242 GNUNET_SCHEDULER_cancel (p->ats_task);
243 p->ats_task = GNUNET_SCHEDULER_NO_TASK;
245 for (c_op = 0; c_op < p->num_partners; c_op++)
247 if (NULL != p->partners[c_op].cth)
249 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
250 p->partners[c_op].cth = NULL;
252 if (NULL != p->partners[c_op].tth)
254 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
255 p->partners[c_op].tth = NULL;
257 if (NULL != p->core_connect_ops[c_op].connect_op)
259 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
260 _("Failed to connect peer 0 and %u\n"), c_op);
261 GNUNET_TESTBED_operation_done (
262 p->core_connect_ops[c_op].connect_op);
263 p->core_connect_ops[c_op].connect_op = NULL;
268 if (NULL != p->ats_perf_op)
270 GNUNET_TESTBED_operation_done (p->ats_perf_op);
271 p->ats_perf_op = NULL;
274 if (NULL != p->comm_op)
276 GNUNET_TESTBED_operation_done (p->comm_op);
279 GNUNET_free(p->core_connect_ops);
280 GNUNET_free(p->partners);
284 for (c_s = 0; c_s < num_slaves; c_s++)
287 if (NULL != p->peer_id_op)
289 GNUNET_TESTBED_operation_done (p->peer_id_op);
290 p->peer_id_op = NULL;
293 for (c_op = 0; c_op < p->num_partners; c_op++)
295 if (NULL != p->partners[c_op].cth)
297 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
298 p->partners[c_op].cth = NULL;
300 if (NULL != p->partners[c_op].tth)
302 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
303 p->partners[c_op].tth = NULL;
306 if (NULL != p->ats_perf_op)
308 GNUNET_TESTBED_operation_done (p->ats_perf_op);
309 p->ats_perf_op = NULL;
311 if (NULL != p->comm_op)
313 GNUNET_TESTBED_operation_done (p->comm_op);
316 GNUNET_free(p->partners);
320 GNUNET_SCHEDULER_shutdown ();
323 static struct BenchmarkPeer *
324 find_peer (const struct GNUNET_PeerIdentity * peer)
328 for (c_p = 0; c_p < num_masters; c_p++)
330 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
334 for (c_p = 0; c_p < num_slaves; c_p++)
336 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
343 * Controller event callback
346 * @param event the controller event
349 controller_event_cb (void *cls,
350 const struct GNUNET_TESTBED_EventInformation *event)
352 //struct BenchmarkPeer *p = cls;
355 case GNUNET_TESTBED_ET_CONNECT:
357 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
362 GNUNET_SCHEDULER_cancel (shutdown_task);
363 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
368 comm_send_ready (void *cls, size_t size, void *buf)
370 static char msgbuf[TEST_MESSAGE_SIZE];
371 struct BenchmarkPartner *p = cls;
372 struct GNUNET_MessageHeader *msg;
374 if (GNUNET_YES == test_core)
384 if (size < TEST_MESSAGE_SIZE)
390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
391 p->me->no, p->dest->no);
394 p->bytes_sent += TEST_MESSAGE_SIZE;
395 p->me->total_messages_sent++;
396 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
398 msg = (struct GNUNET_MessageHeader *) &msgbuf;
399 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
400 msg->type = htons (TEST_MESSAGE_TYPE_PING);
401 msg->size = htons (TEST_MESSAGE_SIZE);
402 memcpy (buf, msg, TEST_MESSAGE_SIZE);
403 return TEST_MESSAGE_SIZE;
407 comm_schedule_send (struct BenchmarkPartner *p)
409 p->last_message_sent = GNUNET_TIME_absolute_get();
410 if (GNUNET_YES == test_core)
412 p->cth = GNUNET_CORE_notify_transmit_ready (
413 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
414 TEST_MESSAGE_SIZE, &comm_send_ready, p);
418 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
419 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
420 &comm_send_ready, p);
429 progress_task = GNUNET_SCHEDULER_NO_TASK;
431 fprintf (stderr, "%llu..",
432 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
435 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
436 &print_progress, NULL );
440 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
442 struct BenchmarkPeer *me = cls;
444 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
447 me->no, me->pref_partner->no, me->pref_value);
448 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
449 &me->pref_partner->id,
450 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
451 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
452 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
453 &ats_pref_task, cls);
462 if ((state.connected_ATS_service == GNUNET_NO)
463 || (state.connected_COMM_service == GNUNET_NO)
464 || (state.connected_PEERS == GNUNET_NO)
465 || (state.connected_CORE == GNUNET_NO))
468 state.benchmarking = GNUNET_YES;
469 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
471 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
472 GNUNET_SCHEDULER_cancel (shutdown_task);
473 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
474 &do_shutdown, NULL );
476 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
478 /* Start sending test messages */
479 for (c_m = 0; c_m < num_masters; c_m++)
481 for (c_s = 0; c_s < num_slaves; c_s++)
482 comm_schedule_send (&mps[c_m].partners[c_s]);
483 if (pref_val != GNUNET_ATS_PREFERENCE_END)
484 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
486 if (GNUNET_YES == logging)
487 perf_logging_start (log_frequency, testname, mps, num_masters);
492 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
495 struct TestbedConnectOperation *cop = cls;
500 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
501 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
506 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
507 _("Failed to connect master peer [%u] with slave [%u]\n"),
508 cop->master->no, cop->slave->no);
510 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
511 GNUNET_SCHEDULER_cancel (shutdown_task);
512 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
514 GNUNET_TESTBED_operation_done (op);
516 for (c = 0; c < num_slaves; c++)
518 if (cop == &cop->master->core_connect_ops[c])
519 cop->master->core_connect_ops[c].connect_op = NULL;
521 if (ops == num_masters * num_slaves)
523 state.connected_PEERS = GNUNET_YES;
524 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
529 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 struct BenchmarkPeer *p;
535 if ((state.connected_ATS_service == GNUNET_NO)
536 || (state.connected_COMM_service == GNUNET_NO))
539 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
541 for (c_m = 0; c_m < num_masters; c_m++)
544 p->core_connect_ops = GNUNET_malloc (num_slaves *
545 sizeof (struct TestbedConnectOperation));
547 for (c_s = 0; c_s < num_slaves; c_s++)
549 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
550 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
551 p->core_connect_ops[c_s].master = p;
552 p->core_connect_ops[c_s].slave = &sps[c_s];
553 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
554 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
555 sps[c_s].peer, p->peer);
556 if (NULL == p->core_connect_ops[c_s].connect_op)
558 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
559 _("Could not connect master [%u] and slave [%u]\n"), p->no,
562 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
563 GNUNET_SCHEDULER_cancel (shutdown_task);
564 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
572 * Method called whenever a given peer connects.
575 * @param peer peer identity this notification is about
578 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
580 struct BenchmarkPeer *me = cls;
581 struct BenchmarkPeer *remote;
586 remote = find_peer (peer);
593 id = GNUNET_strdup (GNUNET_i2s (&me->id));
594 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
595 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
596 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
599 me->core_connections++;
600 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
601 && (GNUNET_NO == state.connected_CORE))
603 me->core_slave_connections++;
605 if (me->core_slave_connections == num_slaves)
607 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
610 completed = GNUNET_YES;
611 for (c = 0; c < num_masters; c++)
613 if (mps[c].core_slave_connections != num_slaves)
614 completed = GNUNET_NO;
616 if (GNUNET_YES == completed)
618 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
619 "All master peers connected all slave peers\n", id,
621 state.connected_CORE = GNUNET_YES;
622 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
628 static struct BenchmarkPartner *
629 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
632 GNUNET_assert (NULL != me);
633 GNUNET_assert (NULL != peer);
635 for (c_m = 0; c_m < me->num_partners; c_m++)
637 /* Find a partner with other as destination */
638 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
639 sizeof(struct GNUNET_PeerIdentity)))
641 return &me->partners[c_m];
649 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
651 struct BenchmarkPeer *me = cls;
652 struct BenchmarkPartner *p;
655 if (NULL == (p = find_partner (me, peer)))
658 id = GNUNET_strdup (GNUNET_i2s (&me->id));
659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
661 GNUNET_assert(me->core_connections > 0);
662 me->core_connections--;
664 if ((GNUNET_YES == state.benchmarking)
665 && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
667 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
668 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
671 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
676 GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
684 comm_send_pong_ready (void *cls, size_t size, void *buf)
686 static char msgbuf[TEST_MESSAGE_SIZE];
687 struct BenchmarkPartner *p = cls;
688 struct GNUNET_MessageHeader *msg;
690 if (GNUNET_YES == test_core)
696 p->bytes_sent += TEST_MESSAGE_SIZE;
697 p->me->total_messages_sent++;
698 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
700 msg = (struct GNUNET_MessageHeader *) &msgbuf;
701 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
702 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
703 msg->size = htons (TEST_MESSAGE_SIZE);
704 memcpy (buf, msg, TEST_MESSAGE_SIZE);
706 return TEST_MESSAGE_SIZE;
710 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
711 const struct GNUNET_MessageHeader *message)
714 struct BenchmarkPeer *me = cls;
715 struct BenchmarkPartner *p = NULL;
717 if (NULL == (p = find_partner(me, other)))
720 return GNUNET_SYSERR;
723 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
724 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
727 p->messages_received++;
728 p->bytes_received += TEST_MESSAGE_SIZE;
729 p->me->total_messages_received++;
730 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
732 if (GNUNET_YES == test_core)
734 GNUNET_assert (NULL == p->cth);
735 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
736 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
737 &comm_send_pong_ready, p);
741 GNUNET_assert (NULL == p->tth);
742 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
743 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
750 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
751 const struct GNUNET_MessageHeader *message)
753 struct BenchmarkPeer *me = cls;
754 struct BenchmarkPartner *p = NULL;
756 if (NULL == (p = find_partner (me, other)))
759 return GNUNET_SYSERR;
762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
763 "Master [%u]: Received PONG from [%u], next message\n", me->no,
766 p->messages_received++;
767 p->bytes_received += TEST_MESSAGE_SIZE;
768 p->me->total_messages_received++;
769 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
770 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
771 GNUNET_TIME_absolute_get()).rel_value_us;
773 comm_schedule_send (p);
778 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
780 struct BenchmarkPeer *me = cls;
782 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
783 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
784 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
786 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
787 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
789 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
794 core_disconnect_adapter (void *cls, void *op_result)
796 struct BenchmarkPeer *me = cls;
798 GNUNET_CORE_disconnect (me->ch);
803 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
804 void *ca_result, const char *emsg)
806 static int comm_done = 0;
807 if ((NULL != emsg) || (NULL == ca_result))
809 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
811 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
812 GNUNET_SCHEDULER_cancel (shutdown_task);
813 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
818 if (comm_done == num_slaves + num_masters)
820 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
821 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
822 state.connected_COMM_service = GNUNET_YES;
823 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
828 transport_recv_cb (void *cls,
829 const struct GNUNET_PeerIdentity * peer,
830 const struct GNUNET_MessageHeader * message)
832 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
833 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
834 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
838 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
839 comm_handle_ping (cls, peer, message);
841 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
842 comm_handle_pong (cls, peer, message);
847 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
849 struct BenchmarkPeer *me = cls;
851 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
852 &comm_connect_cb, &comm_disconnect_cb);
854 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
859 transport_disconnect_adapter (void *cls, void *op_result)
861 struct BenchmarkPeer *me = cls;
863 GNUNET_TRANSPORT_disconnect (me->th);
868 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
872 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
873 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
874 for (c_m = 0; c_m < num_masters; c_m++)
876 if (GNUNET_YES == test_core)
877 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
878 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
879 &core_disconnect_adapter, &mps[c_m]);
882 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
883 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
884 &transport_disconnect_adapter, &mps[c_m]);
888 for (c_s = 0; c_s < num_slaves; c_s++)
890 if (GNUNET_YES == test_core)
891 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
892 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
893 &core_disconnect_adapter, &sps[c_s]);
896 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
897 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
898 &transport_disconnect_adapter, &sps[c_s]);
904 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
905 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
906 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
907 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
909 struct BenchmarkPeer *me = cls;
910 struct BenchmarkPartner *p;
915 p = find_partner (me, &address->peer);
918 /* This is not one of my partners
919 * Will happen since the peers will connect to each other due to gossiping
923 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
926 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
927 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
929 p->bandwidth_in = ntohl (bandwidth_in.value__);
930 p->bandwidth_out = ntohl (bandwidth_out.value__);
932 for (c_a = 0; c_a < ats_count; c_a++)
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
935 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
937 GNUNET_i2s (&p->dest->id),
938 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
939 ntohl(ats[c_a].value));
940 switch (ntohl (ats[c_a].type ))
942 case GNUNET_ATS_ARRAY_TERMINATOR:
944 case GNUNET_ATS_UTILIZATION_UP:
945 if (p->ats_utilization_up != ntohl (ats[c_a].value))
947 p->ats_utilization_up = ntohl (ats[c_a].value);
950 case GNUNET_ATS_UTILIZATION_DOWN:
951 if (p->ats_utilization_down != ntohl (ats[c_a].value))
953 p->ats_utilization_down = ntohl (ats[c_a].value);
955 case GNUNET_ATS_NETWORK_TYPE:
956 if (p->ats_network_type != ntohl (ats[c_a].value))
958 p->ats_network_type = ntohl (ats[c_a].value);
960 case GNUNET_ATS_QUALITY_NET_DELAY:
961 if (p->ats_delay != ntohl (ats[c_a].value))
963 p->ats_delay = ntohl (ats[c_a].value);
965 case GNUNET_ATS_QUALITY_NET_DISTANCE:
966 if (p->ats_distance != ntohl (ats[c_a].value))
968 p->ats_distance = ntohl (ats[c_a].value);
971 case GNUNET_ATS_COST_WAN:
972 if (p->ats_cost_wan != ntohl (ats[c_a].value))
974 p->ats_cost_wan = ntohl (ats[c_a].value);
976 case GNUNET_ATS_COST_LAN:
977 if (p->ats_cost_lan != ntohl (ats[c_a].value))
979 p->ats_cost_lan = ntohl (ats[c_a].value);
981 case GNUNET_ATS_COST_WLAN:
982 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
984 p->ats_cost_wlan = ntohl (ats[c_a].value);
990 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
992 GNUNET_free(peer_id);
996 ats_perf_connect_adapter (void *cls,
997 const struct GNUNET_CONFIGURATION_Handle *cfg)
999 struct BenchmarkPeer *me = cls;
1001 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
1002 &ats_performance_info_cb, me);
1003 if (NULL == me->ats_perf_handle)
1004 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1005 "Failed to create ATS performance handle \n");
1006 return me->ats_perf_handle;
1010 ats_perf_disconnect_adapter (void *cls, void *op_result)
1012 struct BenchmarkPeer *me = cls;
1014 GNUNET_ATS_performance_done (me->ats_perf_handle);
1015 me->ats_perf_handle = NULL;
1019 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1020 void *ca_result, const char *emsg)
1022 static int op_done = 0;
1024 if ((NULL != emsg) || (NULL == ca_result))
1026 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
1028 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
1029 GNUNET_SCHEDULER_cancel (shutdown_task);
1030 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
1034 if (op_done == (num_masters + num_slaves))
1036 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
1037 state.connected_ATS_service = GNUNET_YES;
1038 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
1043 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1048 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1049 for (c_m = 0; c_m < num_masters; c_m++)
1051 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1052 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1053 &ats_perf_disconnect_adapter, &mps[c_m]);
1057 for (c_s = 0; c_s < num_slaves; c_s++)
1059 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1060 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1061 &ats_perf_disconnect_adapter, &sps[c_s]);
1067 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1068 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1070 struct BenchmarkPeer *p = cb_cls;
1071 static int done = 0;
1073 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1075 p->id = *pinfo->result.id;
1076 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1077 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1078 GNUNET_i2s (&p->id));
1080 GNUNET_TESTBED_operation_done (op);
1081 p->peer_id_op = NULL;
1084 if (done == num_slaves + num_masters)
1086 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1087 "Retrieved all peer ID, connect to ATS\n");
1088 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1093 * Signature of a main function for a testcase.
1095 * @param cls closure
1096 * @param num_peers number of peers in 'peers'
1097 * @param peers_ handle to peers run in the testbed
1098 * @param links_succeeded the number of overlay link connection attempts that
1100 * @param links_failed the number of overlay link connection attempts that
1104 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1105 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1106 unsigned int links_failed)
1110 GNUNET_assert(NULL == cls);
1111 GNUNET_assert(num_masters + num_slaves == num_peers);
1112 GNUNET_assert(NULL != peers_);
1114 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1115 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1116 solver, pref_str, num_masters, num_slaves);
1118 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1119 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1120 &do_shutdown, NULL );
1122 /* Setup master peers */
1123 for (c_m = 0; c_m < num_masters; c_m++)
1125 GNUNET_assert(NULL != peers_[c_m]);
1126 mps[c_m].peer = peers_[c_m];
1128 mps[c_m].master = GNUNET_YES;
1129 mps[c_m].pref_partner = &sps[c_m];
1130 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1132 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1133 mps[c_m].num_partners = num_slaves;
1134 /* Initialize partners */
1135 for (c_s = 0; c_s < num_slaves; c_s++)
1137 mps[c_m].partners[c_s].me = &mps[c_m];
1138 mps[c_m].partners[c_s].dest = &sps[c_s];
1140 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1141 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1144 /* Setup slave peers */
1145 for (c_s = 0; c_s < num_slaves; c_s++)
1147 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1148 sps[c_s].peer = peers_[c_s + num_masters];
1149 sps[c_s].no = c_s + num_masters;
1150 sps[c_s].master = GNUNET_NO;
1152 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1153 sps[c_s].num_partners = num_masters;
1154 /* Initialize partners */
1155 for (c_m = 0; c_m < num_masters; c_m++)
1157 sps[c_s].partners[c_m].me = &sps[c_s];
1158 sps[c_s].partners[c_m].dest = &mps[c_m];
1160 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1161 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1166 main (int argc, char *argv[])
1174 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1179 /* figure out testname */
1180 tmp = strstr (argv[0], TESTNAME_PREFIX);
1183 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1184 return GNUNET_SYSERR;
1186 tmp += strlen (TESTNAME_PREFIX);
1187 solver = GNUNET_strdup (tmp);
1188 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1190 tmp_sep = strchr (solver, '_');
1191 if (NULL == tmp_sep)
1193 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1194 GNUNET_free(solver);
1195 return GNUNET_SYSERR;
1198 comm_name = GNUNET_strdup (&tmp_sep[1]);
1199 tmp_sep = strchr (comm_name, '_');
1200 if (NULL == tmp_sep)
1202 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1203 GNUNET_free(solver);
1204 return GNUNET_SYSERR;
1207 for (c = 0; c <= strlen (comm_name); c++)
1208 comm_name[c] = toupper (comm_name[c]);
1209 if (0 == strcmp (comm_name, "CORE"))
1210 test_core = GNUNET_YES;
1211 else if (0 == strcmp (comm_name, "TRANSPORT"))
1212 test_core = GNUNET_NO;
1215 GNUNET_free (comm_name);
1216 GNUNET_free (solver);
1217 return GNUNET_SYSERR;
1220 pref_str = GNUNET_strdup(tmp_sep + 1);
1222 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1224 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1226 for (c = 0; c <= strlen (pref_str); c++)
1227 pref_str[c] = toupper (pref_str[c]);
1230 if (0 != strcmp (pref_str, "NONE"))
1232 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1234 if (0 == strcmp (pref_str, prefs[c]))
1243 /* abuse terminator to indicate no pref */
1244 pref_val = GNUNET_ATS_PREFERENCE_END;
1248 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1249 GNUNET_free(solver);
1250 GNUNET_free(pref_str);
1251 GNUNET_free (comm_name);
1255 for (c = 0; c < (argc - 1); c++)
1257 if (0 == strcmp (argv[c], "-d"))
1262 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1263 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1267 perf_duration = BENCHMARK_DURATION;
1269 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1271 for (c = 0; c < (argc - 1); c++)
1273 if (0 == strcmp (argv[c], "-s"))
1278 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1279 && (num_slaves >= 1))
1280 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1282 num_slaves = DEFAULT_SLAVES_NUM;
1285 num_slaves = DEFAULT_SLAVES_NUM;
1287 for (c = 0; c < (argc - 1); c++)
1289 if (0 == strcmp (argv[c], "-m"))
1294 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1295 && (num_masters >= 2))
1296 fprintf (stderr, "Starting %u master peers\n", num_masters);
1298 num_masters = DEFAULT_MASTERS_NUM;
1301 num_masters = DEFAULT_MASTERS_NUM;
1303 logging = GNUNET_NO;
1304 for (c = 0; c < argc; c++)
1306 if (0 == strcmp (argv[c], "-l"))
1307 logging = GNUNET_YES;
1310 if (GNUNET_YES == logging)
1312 for (c = 0; c < (argc - 1); c++)
1314 if (0 == strcmp (argv[c], "-f"))
1319 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
1320 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1324 log_frequency = LOGGING_FREQUENCY;
1326 fprintf (stderr, "Using log frequency %llu ms\n",
1327 (unsigned long long) (log_frequency.rel_value_us) / (1000));
1330 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1332 if (num_slaves < num_masters)
1334 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1335 GNUNET_free(test_name);
1336 GNUNET_free(solver);
1337 GNUNET_free(pref_str);
1338 GNUNET_free (comm_name);
1339 return GNUNET_SYSERR;
1342 state.connected_ATS_service = GNUNET_NO;
1343 state.connected_COMM_service = GNUNET_NO;
1344 state.connected_PEERS = GNUNET_NO;
1345 state.benchmarking = GNUNET_NO;
1346 state.connected_PEERS = GNUNET_NO;
1348 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1349 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1351 /* Start topology */
1352 uint64_t event_mask;
1354 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1355 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1356 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1357 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1360 GNUNET_free(solver);
1361 GNUNET_free(pref_str);
1362 GNUNET_free(conf_name);
1363 GNUNET_free(test_name);
1364 GNUNET_free(testname);
1365 GNUNET_free (comm_name);
1372 /* end of file perf_ats.c */