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 unsigned int num_masters;
135 * Array of master peers
137 static struct BenchmarkPeer *mps;
142 static unsigned int num_slaves;
145 * Array of slave peers
147 static struct BenchmarkPeer *sps;
152 static struct GNUNET_TIME_Relative perf_duration;
157 static struct GNUNET_TIME_Relative log_frequency;
162 static struct BenchmarkState state;
169 unsigned int duration;
170 struct BenchmarkPeer *mp;
171 struct BenchmarkPartner *p;
173 duration = (perf_duration.rel_value_us / (1000 * 1000));
174 for (c_m = 0; c_m < num_masters; c_m++)
178 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
179 mp->no, mp->total_bytes_sent / 1024, duration,
180 (mp->total_bytes_sent / 1024) / duration,
181 mp->total_bytes_received / 1024, duration,
182 (mp->total_bytes_received / 1024) / duration);
184 for (c_s = 0; c_s < num_slaves; c_s++)
186 p = &mp->partners[c_s];
188 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
189 (mp->pref_partner == p->dest) ? '*' : ' ',
191 (p->bytes_sent / 1024) / duration,
192 ((double) p->bytes_sent * 100) / mp->total_bytes_sent,
193 (p->bytes_received / 1024) / duration,
194 ((double) p->bytes_received * 100) / mp->total_bytes_received );
196 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
197 (mp->pref_partner == p->dest) ? '*' : ' ',
199 p->total_app_rtt / (1000 * p->messages_sent));
208 * @param tc the task context
211 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct BenchmarkPeer *p;
218 if (GNUNET_YES == logging)
221 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
222 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
224 fprintf (stderr, "0\n");
225 GNUNET_SCHEDULER_cancel (progress_task);
227 progress_task = GNUNET_SCHEDULER_NO_TASK;
230 state.benchmarking = GNUNET_NO;
231 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
233 for (c_m = 0; c_m < num_masters; c_m++)
236 if (NULL != mps[c_m].peer_id_op)
238 GNUNET_TESTBED_operation_done (p->peer_id_op);
239 p->peer_id_op = NULL;
242 if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
243 GNUNET_SCHEDULER_cancel (p->ats_task);
244 p->ats_task = GNUNET_SCHEDULER_NO_TASK;
246 for (c_op = 0; c_op < p->num_partners; c_op++)
248 if (NULL != p->partners[c_op].cth)
250 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
251 p->partners[c_op].cth = NULL;
253 if (NULL != p->partners[c_op].tth)
255 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
256 p->partners[c_op].tth = NULL;
258 if (NULL != p->core_connect_ops[c_op].connect_op)
260 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
261 _("Failed to connect peer 0 and %u\n"), c_op);
262 GNUNET_TESTBED_operation_done (
263 p->core_connect_ops[c_op].connect_op);
264 p->core_connect_ops[c_op].connect_op = NULL;
269 if (NULL != p->ats_perf_op)
271 GNUNET_TESTBED_operation_done (p->ats_perf_op);
272 p->ats_perf_op = NULL;
275 if (NULL != p->comm_op)
277 GNUNET_TESTBED_operation_done (p->comm_op);
280 GNUNET_free(p->core_connect_ops);
281 GNUNET_free(p->partners);
285 for (c_s = 0; c_s < num_slaves; c_s++)
288 if (NULL != p->peer_id_op)
290 GNUNET_TESTBED_operation_done (p->peer_id_op);
291 p->peer_id_op = NULL;
294 for (c_op = 0; c_op < p->num_partners; c_op++)
296 if (NULL != p->partners[c_op].cth)
298 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
299 p->partners[c_op].cth = NULL;
301 if (NULL != p->partners[c_op].tth)
303 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
304 p->partners[c_op].tth = NULL;
307 if (NULL != p->ats_perf_op)
309 GNUNET_TESTBED_operation_done (p->ats_perf_op);
310 p->ats_perf_op = NULL;
312 if (NULL != p->comm_op)
314 GNUNET_TESTBED_operation_done (p->comm_op);
317 GNUNET_free(p->partners);
321 GNUNET_SCHEDULER_shutdown ();
324 static struct BenchmarkPeer *
325 find_peer (const struct GNUNET_PeerIdentity * peer)
329 for (c_p = 0; c_p < num_masters; c_p++)
331 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
335 for (c_p = 0; c_p < num_slaves; c_p++)
337 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
344 * Controller event callback
347 * @param event the controller event
350 controller_event_cb (void *cls,
351 const struct GNUNET_TESTBED_EventInformation *event)
353 //struct BenchmarkPeer *p = cls;
356 case GNUNET_TESTBED_ET_CONNECT:
358 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
363 GNUNET_SCHEDULER_cancel (shutdown_task);
364 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
369 comm_send_ready (void *cls, size_t size, void *buf)
371 static char msgbuf[TEST_MESSAGE_SIZE];
372 struct BenchmarkPartner *p = cls;
373 struct GNUNET_MessageHeader *msg;
375 if (GNUNET_YES == test_core)
385 if (size < TEST_MESSAGE_SIZE)
391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
392 p->me->no, p->dest->no);
395 p->bytes_sent += TEST_MESSAGE_SIZE;
396 p->me->total_messages_sent++;
397 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
399 msg = (struct GNUNET_MessageHeader *) &msgbuf;
400 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
401 msg->type = htons (TEST_MESSAGE_TYPE_PING);
402 msg->size = htons (TEST_MESSAGE_SIZE);
403 memcpy (buf, msg, TEST_MESSAGE_SIZE);
404 return TEST_MESSAGE_SIZE;
408 comm_schedule_send (struct BenchmarkPartner *p)
410 p->last_message_sent = GNUNET_TIME_absolute_get();
411 if (GNUNET_YES == test_core)
413 p->cth = GNUNET_CORE_notify_transmit_ready (
414 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
415 TEST_MESSAGE_SIZE, &comm_send_ready, p);
419 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
420 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
421 &comm_send_ready, p);
430 progress_task = GNUNET_SCHEDULER_NO_TASK;
432 fprintf (stderr, "%llu..",
433 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
436 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
437 &print_progress, NULL );
441 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
443 struct BenchmarkPeer *me = cls;
445 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
448 me->no, me->pref_partner->no, me->pref_value);
449 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
450 &me->pref_partner->id,
451 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
452 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
453 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
454 &ats_pref_task, cls);
463 if ((state.connected_ATS_service == GNUNET_NO)
464 || (state.connected_COMM_service == GNUNET_NO)
465 || (state.connected_PEERS == GNUNET_NO)
466 || (state.connected_CORE == GNUNET_NO))
469 state.benchmarking = GNUNET_YES;
470 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
472 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
473 GNUNET_SCHEDULER_cancel (shutdown_task);
474 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
475 &do_shutdown, NULL );
477 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
479 /* Start sending test messages */
480 for (c_m = 0; c_m < num_masters; c_m++)
482 for (c_s = 0; c_s < num_slaves; c_s++)
483 comm_schedule_send (&mps[c_m].partners[c_s]);
484 if (pref_val != GNUNET_ATS_PREFERENCE_END)
485 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
487 if (GNUNET_YES == logging)
488 perf_logging_start (log_frequency, testname, mps, num_masters);
493 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
496 struct TestbedConnectOperation *cop = cls;
501 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
502 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
507 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
508 _("Failed to connect master peer [%u] with slave [%u]\n"),
509 cop->master->no, cop->slave->no);
511 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
512 GNUNET_SCHEDULER_cancel (shutdown_task);
513 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
515 GNUNET_TESTBED_operation_done (op);
517 for (c = 0; c < num_slaves; c++)
519 if (cop == &cop->master->core_connect_ops[c])
520 cop->master->core_connect_ops[c].connect_op = NULL;
522 if (ops == num_masters * num_slaves)
524 state.connected_PEERS = GNUNET_YES;
525 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
530 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
534 struct BenchmarkPeer *p;
536 if ((state.connected_ATS_service == GNUNET_NO)
537 || (state.connected_COMM_service == GNUNET_NO))
540 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
542 for (c_m = 0; c_m < num_masters; c_m++)
545 p->core_connect_ops = GNUNET_malloc (num_slaves *
546 sizeof (struct TestbedConnectOperation));
548 for (c_s = 0; c_s < num_slaves; c_s++)
550 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
551 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
552 p->core_connect_ops[c_s].master = p;
553 p->core_connect_ops[c_s].slave = &sps[c_s];
554 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
555 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
556 sps[c_s].peer, p->peer);
557 if (NULL == p->core_connect_ops[c_s].connect_op)
559 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
560 _("Could not connect master [%u] and slave [%u]\n"), p->no,
563 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
564 GNUNET_SCHEDULER_cancel (shutdown_task);
565 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
573 * Method called whenever a given peer connects.
576 * @param peer peer identity this notification is about
579 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
581 struct BenchmarkPeer *me = cls;
582 struct BenchmarkPeer *remote;
587 remote = find_peer (peer);
594 id = GNUNET_strdup (GNUNET_i2s (&me->id));
595 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
596 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
597 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
600 me->core_connections++;
601 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
602 && (GNUNET_NO == state.connected_CORE))
604 me->core_slave_connections++;
606 if (me->core_slave_connections == num_slaves)
608 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
611 completed = GNUNET_YES;
612 for (c = 0; c < num_masters; c++)
614 if (mps[c].core_slave_connections != num_slaves)
615 completed = GNUNET_NO;
617 if (GNUNET_YES == completed)
619 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
620 "All master peers connected all slave peers\n", id,
622 state.connected_CORE = GNUNET_YES;
623 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
629 static struct BenchmarkPartner *
630 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
633 GNUNET_assert (NULL != me);
634 GNUNET_assert (NULL != peer);
636 for (c_m = 0; c_m < me->num_partners; c_m++)
638 /* Find a partner with other as destination */
639 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
640 sizeof(struct GNUNET_PeerIdentity)))
642 return &me->partners[c_m];
650 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
652 struct BenchmarkPeer *me = cls;
653 struct BenchmarkPartner *p;
656 if (NULL == (p = find_partner (me, peer)))
659 id = GNUNET_strdup (GNUNET_i2s (&me->id));
660 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
662 GNUNET_assert(me->core_connections > 0);
663 me->core_connections--;
665 if ((GNUNET_YES == state.benchmarking)
666 && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
668 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
669 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
672 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
677 GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
685 comm_send_pong_ready (void *cls, size_t size, void *buf)
687 static char msgbuf[TEST_MESSAGE_SIZE];
688 struct BenchmarkPartner *p = cls;
689 struct GNUNET_MessageHeader *msg;
691 if (GNUNET_YES == test_core)
697 p->bytes_sent += TEST_MESSAGE_SIZE;
698 p->me->total_messages_sent++;
699 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
701 msg = (struct GNUNET_MessageHeader *) &msgbuf;
702 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
703 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
704 msg->size = htons (TEST_MESSAGE_SIZE);
705 memcpy (buf, msg, TEST_MESSAGE_SIZE);
707 return TEST_MESSAGE_SIZE;
711 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
712 const struct GNUNET_MessageHeader *message)
715 struct BenchmarkPeer *me = cls;
716 struct BenchmarkPartner *p = NULL;
718 if (NULL == (p = find_partner(me, other)))
721 return GNUNET_SYSERR;
724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
725 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
728 p->messages_received++;
729 p->bytes_received += TEST_MESSAGE_SIZE;
730 p->me->total_messages_received++;
731 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
733 if (GNUNET_YES == test_core)
735 GNUNET_assert (NULL == p->cth);
736 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
737 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
738 &comm_send_pong_ready, p);
742 GNUNET_assert (NULL == p->tth);
743 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
744 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
751 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
752 const struct GNUNET_MessageHeader *message)
754 struct BenchmarkPeer *me = cls;
755 struct BenchmarkPartner *p = NULL;
757 if (NULL == (p = find_partner (me, other)))
760 return GNUNET_SYSERR;
763 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
764 "Master [%u]: Received PONG from [%u], next message\n", me->no,
767 p->messages_received++;
768 p->bytes_received += TEST_MESSAGE_SIZE;
769 p->me->total_messages_received++;
770 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
771 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
772 GNUNET_TIME_absolute_get()).rel_value_us;
774 comm_schedule_send (p);
779 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
781 struct BenchmarkPeer *me = cls;
783 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
784 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
785 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
787 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
788 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
790 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
795 core_disconnect_adapter (void *cls, void *op_result)
797 struct BenchmarkPeer *me = cls;
799 GNUNET_CORE_disconnect (me->ch);
804 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
805 void *ca_result, const char *emsg)
807 static int comm_done = 0;
808 if ((NULL != emsg) || (NULL == ca_result))
810 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
812 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
813 GNUNET_SCHEDULER_cancel (shutdown_task);
814 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
819 if (comm_done == num_slaves + num_masters)
821 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
822 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
823 state.connected_COMM_service = GNUNET_YES;
824 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
829 transport_recv_cb (void *cls,
830 const struct GNUNET_PeerIdentity * peer,
831 const struct GNUNET_MessageHeader * message)
833 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
834 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
835 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
839 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
840 comm_handle_ping (cls, peer, message);
842 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
843 comm_handle_pong (cls, peer, message);
848 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
850 struct BenchmarkPeer *me = cls;
852 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
853 &comm_connect_cb, &comm_disconnect_cb);
855 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
860 transport_disconnect_adapter (void *cls, void *op_result)
862 struct BenchmarkPeer *me = cls;
864 GNUNET_TRANSPORT_disconnect (me->th);
869 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
874 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
875 for (c_m = 0; c_m < num_masters; c_m++)
877 if (GNUNET_YES == test_core)
878 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
879 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
880 &core_disconnect_adapter, &mps[c_m]);
883 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
884 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
885 &transport_disconnect_adapter, &mps[c_m]);
889 for (c_s = 0; c_s < num_slaves; c_s++)
891 if (GNUNET_YES == test_core)
892 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
893 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
894 &core_disconnect_adapter, &sps[c_s]);
897 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
898 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
899 &transport_disconnect_adapter, &sps[c_s]);
905 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
906 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
907 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
908 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
910 struct BenchmarkPeer *me = cls;
911 struct BenchmarkPartner *p;
916 p = find_partner (me, &address->peer);
919 /* This is not one of my partners
920 * Will happen since the peers will connect to each other due to gossiping
924 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
927 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
928 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
930 p->bandwidth_in = ntohl (bandwidth_in.value__);
931 p->bandwidth_out = ntohl (bandwidth_out.value__);
933 for (c_a = 0; c_a < ats_count; c_a++)
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
936 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
938 GNUNET_i2s (&p->dest->id),
939 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
940 ntohl(ats[c_a].value));
941 switch (ntohl (ats[c_a].type ))
943 case GNUNET_ATS_ARRAY_TERMINATOR:
945 case GNUNET_ATS_UTILIZATION_OUT:
946 if (p->ats_utilization_up != ntohl (ats[c_a].value))
948 p->ats_utilization_up = ntohl (ats[c_a].value);
951 case GNUNET_ATS_UTILIZATION_IN:
952 if (p->ats_utilization_down != ntohl (ats[c_a].value))
954 p->ats_utilization_down = ntohl (ats[c_a].value);
956 case GNUNET_ATS_NETWORK_TYPE:
957 if (p->ats_network_type != ntohl (ats[c_a].value))
959 p->ats_network_type = ntohl (ats[c_a].value);
961 case GNUNET_ATS_QUALITY_NET_DELAY:
962 if (p->ats_delay != ntohl (ats[c_a].value))
964 p->ats_delay = ntohl (ats[c_a].value);
966 case GNUNET_ATS_QUALITY_NET_DISTANCE:
967 if (p->ats_distance != ntohl (ats[c_a].value))
969 p->ats_distance = ntohl (ats[c_a].value);
972 case GNUNET_ATS_COST_WAN:
973 if (p->ats_cost_wan != ntohl (ats[c_a].value))
975 p->ats_cost_wan = ntohl (ats[c_a].value);
977 case GNUNET_ATS_COST_LAN:
978 if (p->ats_cost_lan != ntohl (ats[c_a].value))
980 p->ats_cost_lan = ntohl (ats[c_a].value);
982 case GNUNET_ATS_COST_WLAN:
983 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
985 p->ats_cost_wlan = ntohl (ats[c_a].value);
991 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
993 GNUNET_free(peer_id);
997 ats_perf_connect_adapter (void *cls,
998 const struct GNUNET_CONFIGURATION_Handle *cfg)
1000 struct BenchmarkPeer *me = cls;
1002 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
1003 &ats_performance_info_cb, me);
1004 if (NULL == me->ats_perf_handle)
1005 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1006 "Failed to create ATS performance handle \n");
1007 return me->ats_perf_handle;
1011 ats_perf_disconnect_adapter (void *cls, void *op_result)
1013 struct BenchmarkPeer *me = cls;
1015 GNUNET_ATS_performance_done (me->ats_perf_handle);
1016 me->ats_perf_handle = NULL;
1020 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1021 void *ca_result, const char *emsg)
1023 static int op_done = 0;
1025 if ((NULL != emsg) || (NULL == ca_result))
1027 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
1029 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
1030 GNUNET_SCHEDULER_cancel (shutdown_task);
1031 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
1035 if (op_done == (num_masters + num_slaves))
1037 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
1038 state.connected_ATS_service = GNUNET_YES;
1039 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
1044 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1050 for (c_m = 0; c_m < num_masters; c_m++)
1052 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1053 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1054 &ats_perf_disconnect_adapter, &mps[c_m]);
1058 for (c_s = 0; c_s < num_slaves; c_s++)
1060 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1061 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1062 &ats_perf_disconnect_adapter, &sps[c_s]);
1068 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1069 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1071 struct BenchmarkPeer *p = cb_cls;
1072 static int done = 0;
1074 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1076 p->id = *pinfo->result.id;
1077 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1078 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1079 GNUNET_i2s (&p->id));
1081 GNUNET_TESTBED_operation_done (op);
1082 p->peer_id_op = NULL;
1085 if (done == num_slaves + num_masters)
1087 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1088 "Retrieved all peer ID, connect to ATS\n");
1089 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1094 * Signature of a main function for a testcase.
1096 * @param cls closure
1097 * @param num_peers number of peers in 'peers'
1098 * @param peers_ handle to peers run in the testbed
1099 * @param links_succeeded the number of overlay link connection attempts that
1101 * @param links_failed the number of overlay link connection attempts that
1105 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
1106 unsigned int num_peers,
1107 struct GNUNET_TESTBED_Peer **peers_,
1108 unsigned int links_succeeded,
1109 unsigned int links_failed)
1113 GNUNET_assert(NULL == cls);
1114 GNUNET_assert(num_masters + num_slaves == num_peers);
1115 GNUNET_assert(NULL != peers_);
1117 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1118 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1119 solver, pref_str, num_masters, num_slaves);
1121 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1122 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1123 &do_shutdown, NULL );
1125 /* Setup master peers */
1126 for (c_m = 0; c_m < num_masters; c_m++)
1128 GNUNET_assert(NULL != peers_[c_m]);
1129 mps[c_m].peer = peers_[c_m];
1131 mps[c_m].master = GNUNET_YES;
1132 mps[c_m].pref_partner = &sps[c_m];
1133 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1135 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPartner));
1136 mps[c_m].num_partners = num_slaves;
1137 /* Initialize partners */
1138 for (c_s = 0; c_s < num_slaves; c_s++)
1140 mps[c_m].partners[c_s].me = &mps[c_m];
1141 mps[c_m].partners[c_s].dest = &sps[c_s];
1143 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1144 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1147 /* Setup slave peers */
1148 for (c_s = 0; c_s < num_slaves; c_s++)
1150 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1151 sps[c_s].peer = peers_[c_s + num_masters];
1152 sps[c_s].no = c_s + num_masters;
1153 sps[c_s].master = GNUNET_NO;
1155 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPartner));
1156 sps[c_s].num_partners = num_masters;
1157 /* Initialize partners */
1158 for (c_m = 0; c_m < num_masters; c_m++)
1160 sps[c_s].partners[c_m].me = &sps[c_s];
1161 sps[c_s].partners[c_m].dest = &mps[c_m];
1163 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1164 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1169 main (int argc, char *argv[])
1177 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1182 /* figure out testname */
1183 tmp = strstr (argv[0], TESTNAME_PREFIX);
1186 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1187 return GNUNET_SYSERR;
1189 tmp += strlen (TESTNAME_PREFIX);
1190 solver = GNUNET_strdup (tmp);
1191 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1193 tmp_sep = strchr (solver, '_');
1194 if (NULL == tmp_sep)
1196 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1197 GNUNET_free(solver);
1198 return GNUNET_SYSERR;
1201 comm_name = GNUNET_strdup (&tmp_sep[1]);
1202 tmp_sep = strchr (comm_name, '_');
1203 if (NULL == tmp_sep)
1205 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1206 GNUNET_free(solver);
1207 return GNUNET_SYSERR;
1210 for (c = 0; c <= strlen (comm_name); c++)
1211 comm_name[c] = toupper (comm_name[c]);
1212 if (0 == strcmp (comm_name, "CORE"))
1213 test_core = GNUNET_YES;
1214 else if (0 == strcmp (comm_name, "TRANSPORT"))
1215 test_core = GNUNET_NO;
1218 GNUNET_free (comm_name);
1219 GNUNET_free (solver);
1220 return GNUNET_SYSERR;
1223 pref_str = GNUNET_strdup(tmp_sep + 1);
1225 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1227 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1229 for (c = 0; c <= strlen (pref_str); c++)
1230 pref_str[c] = toupper (pref_str[c]);
1233 if (0 != strcmp (pref_str, "NONE"))
1235 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1237 if (0 == strcmp (pref_str, prefs[c]))
1246 /* abuse terminator to indicate no pref */
1247 pref_val = GNUNET_ATS_PREFERENCE_END;
1251 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1252 GNUNET_free(solver);
1253 GNUNET_free(pref_str);
1254 GNUNET_free (comm_name);
1258 for (c = 0; c < (argc - 1); c++)
1260 if (0 == strcmp (argv[c], "-d"))
1265 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1266 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1270 perf_duration = BENCHMARK_DURATION;
1272 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1274 for (c = 0; c < (argc - 1); c++)
1276 if (0 == strcmp (argv[c], "-s"))
1281 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1282 && (num_slaves >= 1))
1283 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1285 num_slaves = DEFAULT_SLAVES_NUM;
1288 num_slaves = DEFAULT_SLAVES_NUM;
1290 for (c = 0; c < (argc - 1); c++)
1292 if (0 == strcmp (argv[c], "-m"))
1297 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1298 && (num_masters >= 2))
1299 fprintf (stderr, "Starting %u master peers\n", num_masters);
1301 num_masters = DEFAULT_MASTERS_NUM;
1304 num_masters = DEFAULT_MASTERS_NUM;
1306 logging = GNUNET_NO;
1307 for (c = 0; c < argc; c++)
1309 if (0 == strcmp (argv[c], "-l"))
1310 logging = GNUNET_YES;
1313 if (GNUNET_YES == logging)
1315 for (c = 0; c < (argc - 1); c++)
1317 if (0 == strcmp (argv[c], "-f"))
1322 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
1323 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1327 log_frequency = LOGGING_FREQUENCY;
1329 fprintf (stderr, "Using log frequency %llu ms\n",
1330 (unsigned long long) (log_frequency.rel_value_us) / (1000));
1333 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1335 if (num_slaves < num_masters)
1337 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1338 GNUNET_free(test_name);
1339 GNUNET_free(solver);
1340 GNUNET_free(pref_str);
1341 GNUNET_free (comm_name);
1342 return GNUNET_SYSERR;
1345 state.connected_ATS_service = GNUNET_NO;
1346 state.connected_COMM_service = GNUNET_NO;
1347 state.connected_PEERS = GNUNET_NO;
1348 state.benchmarking = GNUNET_NO;
1349 state.connected_PEERS = GNUNET_NO;
1351 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1352 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1354 /* Start topology */
1355 uint64_t event_mask;
1357 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1358 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1359 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1360 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1363 GNUNET_free(solver);
1364 GNUNET_free(pref_str);
1365 GNUNET_free(conf_name);
1366 GNUNET_free(test_name);
1367 GNUNET_free(testname);
1368 GNUNET_free (comm_name);
1375 /* end of file perf_ats.c */