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)
216 if (GNUNET_YES == logging)
219 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
220 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
222 fprintf (stderr, "0\n");
223 GNUNET_SCHEDULER_cancel (progress_task);
225 progress_task = GNUNET_SCHEDULER_NO_TASK;
228 state.benchmarking = GNUNET_NO;
229 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
231 for (c_m = 0; c_m < num_masters; c_m++)
233 if (NULL != mps[c_m].peer_id_op)
235 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
236 mps[c_m].peer_id_op = NULL;
239 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
240 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
241 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
243 for (c_op = 0; c_op < num_slaves; c_op++)
246 if (NULL != mps[c_m].partners[c_op].cth)
248 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
249 mps[c_m].partners[c_op].cth = NULL;
251 if (NULL != mps[c_m].partners[c_op].tth)
253 GNUNET_TRANSPORT_notify_transmit_ready_cancel (mps[c_m].partners[c_op].tth);
254 mps[c_m].partners[c_op].tth = NULL;
258 if (NULL != mps[c_m].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 mps[c_m].core_connect_ops[c_op].connect_op);
264 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
269 if (NULL != mps[c_m].ats_perf_op)
271 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
272 mps[c_m].ats_perf_op = NULL;
275 if (NULL != mps[c_m].comm_op)
277 GNUNET_TESTBED_operation_done (mps[c_m].comm_op);
278 mps[c_m].comm_op = NULL;
280 GNUNET_free(mps[c_m].core_connect_ops);
281 GNUNET_free(mps[c_m].partners);
282 mps[c_m].partners = NULL;
285 for (c_s = 0; c_s < num_slaves; c_s++)
287 if (NULL != sps[c_s].peer_id_op)
289 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
290 sps[c_s].peer_id_op = NULL;
293 for (c_op = 0; c_op < num_slaves; c_op++)
295 if (NULL != sps[c_s].partners[c_op].cth)
297 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
298 sps[c_s].partners[c_op].cth = NULL;
301 if (NULL != sps[c_s].partners[c_op].tth)
303 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sps[c_s].partners[c_op].tth);
304 sps[c_s].partners[c_op].tth = NULL;
308 if (NULL != sps[c_s].ats_perf_op)
310 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
311 sps[c_s].ats_perf_op = NULL;
313 if (NULL != sps[c_s].comm_op)
315 GNUNET_TESTBED_operation_done (sps[c_s].comm_op);
316 sps[c_s].comm_op = NULL;
319 GNUNET_free(sps[c_s].partners);
320 sps[c_s].partners = NULL;
323 GNUNET_SCHEDULER_shutdown ();
326 static struct BenchmarkPeer *
327 find_peer (const struct GNUNET_PeerIdentity * peer)
331 for (c_p = 0; c_p < num_masters; c_p++)
333 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
337 for (c_p = 0; c_p < num_slaves; c_p++)
339 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
346 * Controller event callback
349 * @param event the controller event
352 controller_event_cb (void *cls,
353 const struct GNUNET_TESTBED_EventInformation *event)
355 //struct BenchmarkPeer *p = cls;
358 case GNUNET_TESTBED_ET_CONNECT:
360 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
365 GNUNET_SCHEDULER_cancel (shutdown_task);
366 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
371 comm_send_ready (void *cls, size_t size, void *buf)
373 static char msgbuf[TEST_MESSAGE_SIZE];
374 struct BenchmarkPartner *p = cls;
375 struct GNUNET_MessageHeader *msg;
377 if (GNUNET_YES == test_core)
387 if (size < TEST_MESSAGE_SIZE)
393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
394 p->me->no, p->dest->no);
397 p->bytes_sent += TEST_MESSAGE_SIZE;
398 p->me->total_messages_sent++;
399 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
401 msg = (struct GNUNET_MessageHeader *) &msgbuf;
402 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
403 msg->type = htons (TEST_MESSAGE_TYPE_PING);
404 msg->size = htons (TEST_MESSAGE_SIZE);
405 memcpy (buf, msg, TEST_MESSAGE_SIZE);
406 return TEST_MESSAGE_SIZE;
410 comm_schedule_send (struct BenchmarkPartner *p)
412 p->last_message_sent = GNUNET_TIME_absolute_get();
413 if (GNUNET_YES == test_core)
415 p->cth = GNUNET_CORE_notify_transmit_ready (
416 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
417 TEST_MESSAGE_SIZE, &comm_send_ready, p);
421 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
422 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
423 &comm_send_ready, p);
432 progress_task = GNUNET_SCHEDULER_NO_TASK;
434 fprintf (stderr, "%llu..",
435 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
438 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
439 &print_progress, NULL );
443 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
445 struct BenchmarkPeer *me = cls;
447 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
450 me->no, me->pref_partner->no, me->pref_value);
451 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
452 &me->pref_partner->id,
453 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
454 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
455 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
456 &ats_pref_task, cls);
465 if ((state.connected_ATS_service == GNUNET_NO)
466 || (state.connected_COMM_service == GNUNET_NO)
467 || (state.connected_PEERS == GNUNET_NO)
468 || (state.connected_CORE == GNUNET_NO))
471 state.benchmarking = GNUNET_YES;
472 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
474 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
475 GNUNET_SCHEDULER_cancel (shutdown_task);
476 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
477 &do_shutdown, NULL );
479 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
481 /* Start sending test messages */
482 for (c_m = 0; c_m < num_masters; c_m++)
484 for (c_s = 0; c_s < num_slaves; c_s++)
485 comm_schedule_send (&mps[c_m].partners[c_s]);
486 if (pref_val != GNUNET_ATS_PREFERENCE_END)
487 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
489 if (GNUNET_YES == logging)
490 perf_logging_start (log_frequency, testname, mps, num_masters);
495 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
498 struct TestbedConnectOperation *cop = cls;
503 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
504 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
509 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
510 _("Failed to connect master peer [%u] with slave [%u]\n"),
511 cop->master->no, cop->slave->no);
513 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
514 GNUNET_SCHEDULER_cancel (shutdown_task);
515 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
517 GNUNET_TESTBED_operation_done (op);
519 for (c = 0; c < num_slaves; c++)
521 if (cop == &cop->master->core_connect_ops[c])
522 cop->master->core_connect_ops[c].connect_op = NULL;
524 if (ops == num_masters * num_slaves)
526 state.connected_PEERS = GNUNET_YES;
527 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
532 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
536 struct BenchmarkPeer *p;
538 if ((state.connected_ATS_service == GNUNET_NO)
539 || (state.connected_COMM_service == GNUNET_NO))
542 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
544 for (c_m = 0; c_m < num_masters; c_m++)
547 p->core_connect_ops = GNUNET_malloc (num_slaves *
548 sizeof (struct TestbedConnectOperation));
550 for (c_s = 0; c_s < num_slaves; c_s++)
552 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
553 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
554 p->core_connect_ops[c_s].master = p;
555 p->core_connect_ops[c_s].slave = &sps[c_s];
556 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
557 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
558 sps[c_s].peer, p->peer);
559 if (NULL == p->core_connect_ops[c_s].connect_op)
561 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
562 _("Could not connect master [%u] and slave [%u]\n"), p->no,
565 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
566 GNUNET_SCHEDULER_cancel (shutdown_task);
567 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
575 * Method called whenever a given peer connects.
578 * @param peer peer identity this notification is about
581 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
583 struct BenchmarkPeer *me = cls;
584 struct BenchmarkPeer *remote;
589 remote = find_peer (peer);
596 id = GNUNET_strdup (GNUNET_i2s (&me->id));
597 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
598 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
599 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
602 me->core_connections++;
603 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
604 && (GNUNET_NO == state.connected_CORE))
606 me->core_slave_connections++;
608 if (me->core_slave_connections == num_slaves)
610 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
613 completed = GNUNET_YES;
614 for (c = 0; c < num_masters; c++)
616 if (mps[c].core_slave_connections != num_slaves)
617 completed = GNUNET_NO;
619 if (GNUNET_YES == completed)
621 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
622 "All master peers connected all slave peers\n", id,
624 state.connected_CORE = GNUNET_YES;
625 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
632 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
634 struct BenchmarkPeer *me = cls;
635 struct BenchmarkPeer *remote;
638 remote = find_peer (peer);
645 id = GNUNET_strdup (GNUNET_i2s (&me->id));
646 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
648 GNUNET_assert(me->core_connections > 0);
649 me->core_connections--;
651 if ((GNUNET_YES == state.benchmarking)
652 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
654 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
655 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
661 comm_send_pong_ready (void *cls, size_t size, void *buf)
663 static char msgbuf[TEST_MESSAGE_SIZE];
664 struct BenchmarkPartner *p = cls;
665 struct GNUNET_MessageHeader *msg;
667 if (GNUNET_YES == test_core)
673 p->bytes_sent += TEST_MESSAGE_SIZE;
674 p->me->total_messages_sent++;
675 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
677 msg = (struct GNUNET_MessageHeader *) &msgbuf;
678 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
679 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
680 msg->size = htons (TEST_MESSAGE_SIZE);
681 memcpy (buf, msg, TEST_MESSAGE_SIZE);
683 return TEST_MESSAGE_SIZE;
687 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
688 const struct GNUNET_MessageHeader *message)
691 struct BenchmarkPeer *me = cls;
692 struct BenchmarkPartner *p = NULL;
693 for (c_m = 0; c_m < num_masters; c_m++)
695 /* Find a partner with other as destination */
696 if (0 == memcmp (other, &me->partners[c_m].dest->id,
697 sizeof(struct GNUNET_PeerIdentity)))
699 p = &me->partners[c_m];
706 return GNUNET_SYSERR;
709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
710 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
713 p->messages_received++;
714 p->bytes_received += TEST_MESSAGE_SIZE;
715 p->me->total_messages_received++;
716 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
718 if (GNUNET_YES == test_core)
720 GNUNET_assert (NULL == p->cth);
721 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
722 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
723 &comm_send_pong_ready, p);
727 GNUNET_assert (NULL == p->tth);
728 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
729 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
736 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
737 const struct GNUNET_MessageHeader *message)
740 struct BenchmarkPeer *me = cls;
741 struct BenchmarkPartner *p = NULL;
743 for (c_s = 0; c_s < num_slaves; c_s++)
746 == memcmp (other, &me->partners[c_s].dest->id,
747 sizeof(struct GNUNET_PeerIdentity)))
749 p = &me->partners[c_s];
756 return GNUNET_SYSERR;
760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
761 "Master [%u]: Received PONG from [%u], next message\n", me->no,
764 p->messages_received++;
765 p->bytes_received += TEST_MESSAGE_SIZE;
766 p->me->total_messages_received++;
767 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
768 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
769 GNUNET_TIME_absolute_get()).rel_value_us;
771 comm_schedule_send (p);
776 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
778 struct BenchmarkPeer *me = cls;
780 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
781 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
782 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
784 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
785 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
787 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
792 core_disconnect_adapter (void *cls, void *op_result)
794 struct BenchmarkPeer *me = cls;
796 GNUNET_CORE_disconnect (me->ch);
801 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
802 void *ca_result, const char *emsg)
804 static int comm_done = 0;
805 if ((NULL != emsg) || (NULL == ca_result))
807 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
809 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
810 GNUNET_SCHEDULER_cancel (shutdown_task);
811 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
816 if (comm_done == num_slaves + num_masters)
818 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
819 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
820 state.connected_COMM_service = GNUNET_YES;
821 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
826 transport_recv_cb (void *cls,
827 const struct GNUNET_PeerIdentity * peer,
828 const struct GNUNET_MessageHeader * message)
830 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
831 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
832 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
836 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
837 comm_handle_ping (cls, peer, message);
839 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
840 comm_handle_pong (cls, peer, message);
845 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
847 struct BenchmarkPeer *me = cls;
849 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
850 &comm_connect_cb, &comm_disconnect_cb);
852 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
857 transport_disconnect_adapter (void *cls, void *op_result)
859 struct BenchmarkPeer *me = cls;
861 GNUNET_TRANSPORT_disconnect (me->th);
866 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
870 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
871 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
872 for (c_m = 0; c_m < num_masters; c_m++)
874 if (GNUNET_YES == test_core)
875 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
876 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
877 &core_disconnect_adapter, &mps[c_m]);
880 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
881 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
882 &transport_disconnect_adapter, &mps[c_m]);
886 for (c_s = 0; c_s < num_slaves; c_s++)
888 if (GNUNET_YES == test_core)
889 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
890 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
891 &core_disconnect_adapter, &sps[c_s]);
894 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
895 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
896 &transport_disconnect_adapter, &sps[c_s]);
902 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
903 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
904 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
905 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
907 struct BenchmarkPeer *me = cls;
908 struct BenchmarkPartner *p;
914 for (c_s = 0; c_s < me->num_partners; c_s++)
917 if (0 == memcmp (&address->peer, &me->partners[c_s].dest->id,
918 sizeof (struct GNUNET_PeerIdentity)))
920 p = &me->partners[c_s];
928 /* This is not one of my partners
929 * Will happen since the peers will connect to each other due to gossiping
934 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
936 for (c_a = 0; c_a < ats_count; c_a++)
939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%c %03u: %s %s %u\n"),
940 (GNUNET_YES == p->me->master) ? 'M' : 'S',
942 GNUNET_i2s (&address->peer),
943 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
944 ntohl(ats[c_a].value));
948 GNUNET_free(peer_id);
952 ats_perf_connect_adapter (void *cls,
953 const struct GNUNET_CONFIGURATION_Handle *cfg)
955 struct BenchmarkPeer *me = cls;
957 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
958 &ats_performance_info_cb, me);
959 if (NULL == me->ats_perf_handle)
960 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
961 "Failed to create ATS performance handle \n");
962 return me->ats_perf_handle;
966 ats_perf_disconnect_adapter (void *cls, void *op_result)
968 struct BenchmarkPeer *me = cls;
970 GNUNET_ATS_performance_done (me->ats_perf_handle);
971 me->ats_perf_handle = NULL;
975 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
976 void *ca_result, const char *emsg)
978 static int op_done = 0;
980 if ((NULL != emsg) || (NULL == ca_result))
982 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
984 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
985 GNUNET_SCHEDULER_cancel (shutdown_task);
986 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
990 if (op_done == (num_masters + num_slaves))
992 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
993 state.connected_ATS_service = GNUNET_YES;
994 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
999 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1004 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1005 for (c_m = 0; c_m < num_masters; c_m++)
1007 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1008 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1009 &ats_perf_disconnect_adapter, &mps[c_m]);
1013 for (c_s = 0; c_s < num_slaves; c_s++)
1015 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1016 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1017 &ats_perf_disconnect_adapter, &sps[c_s]);
1023 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1024 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1026 struct BenchmarkPeer *p = cb_cls;
1027 static int done = 0;
1029 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1031 p->id = *pinfo->result.id;
1032 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1033 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1034 GNUNET_i2s (&p->id));
1036 GNUNET_TESTBED_operation_done (op);
1037 p->peer_id_op = NULL;
1040 if (done == num_slaves + num_masters)
1042 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1043 "Retrieved all peer ID, connect to ATS\n");
1044 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1049 * Signature of a main function for a testcase.
1051 * @param cls closure
1052 * @param num_peers number of peers in 'peers'
1053 * @param peers_ handle to peers run in the testbed
1054 * @param links_succeeded the number of overlay link connection attempts that
1056 * @param links_failed the number of overlay link connection attempts that
1060 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1061 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1062 unsigned int links_failed)
1066 GNUNET_assert(NULL == cls);
1067 GNUNET_assert(num_masters + num_slaves == num_peers);
1068 GNUNET_assert(NULL != peers_);
1070 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1071 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1072 solver, pref_str, num_masters, num_slaves);
1074 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1075 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1076 &do_shutdown, NULL );
1078 /* Setup master peers */
1079 for (c_m = 0; c_m < num_masters; c_m++)
1081 GNUNET_assert(NULL != peers_[c_m]);
1082 mps[c_m].peer = peers_[c_m];
1084 mps[c_m].master = GNUNET_YES;
1085 mps[c_m].pref_partner = &sps[c_m];
1086 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1088 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1089 mps[c_m].num_partners = num_slaves;
1090 /* Initialize partners */
1091 for (c_s = 0; c_s < num_slaves; c_s++)
1093 mps[c_m].partners[c_s].me = &mps[c_m];
1094 mps[c_m].partners[c_s].dest = &sps[c_s];
1096 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1097 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1100 /* Setup slave peers */
1101 for (c_s = 0; c_s < num_slaves; c_s++)
1103 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1104 sps[c_s].peer = peers_[c_s + num_masters];
1105 sps[c_s].no = c_s + num_masters;
1106 sps[c_s].master = GNUNET_NO;
1108 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1109 sps[c_s].num_partners = num_masters;
1110 /* Initialize partners */
1111 for (c_m = 0; c_m < num_masters; c_m++)
1113 sps[c_s].partners[c_m].me = &sps[c_s];
1114 sps[c_s].partners[c_m].dest = &mps[c_m];
1116 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1117 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1122 main (int argc, char *argv[])
1130 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1135 /* figure out testname */
1136 tmp = strstr (argv[0], TESTNAME_PREFIX);
1139 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1140 return GNUNET_SYSERR;
1142 tmp += strlen (TESTNAME_PREFIX);
1143 solver = GNUNET_strdup (tmp);
1144 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1146 tmp_sep = strchr (solver, '_');
1147 if (NULL == tmp_sep)
1149 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1150 GNUNET_free(solver);
1151 return GNUNET_SYSERR;
1154 comm_name = GNUNET_strdup (&tmp_sep[1]);
1155 tmp_sep = strchr (comm_name, '_');
1156 if (NULL == tmp_sep)
1158 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1159 GNUNET_free(solver);
1160 return GNUNET_SYSERR;
1163 for (c = 0; c <= strlen (comm_name); c++)
1164 comm_name[c] = toupper (comm_name[c]);
1165 if (0 == strcmp (comm_name, "CORE"))
1166 test_core = GNUNET_YES;
1167 else if (0 == strcmp (comm_name, "TRANSPORT"))
1168 test_core = GNUNET_NO;
1171 GNUNET_free (comm_name);
1172 GNUNET_free (solver);
1173 return GNUNET_SYSERR;
1176 pref_str = GNUNET_strdup(tmp_sep + 1);
1178 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1180 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1182 for (c = 0; c <= strlen (pref_str); c++)
1183 pref_str[c] = toupper (pref_str[c]);
1186 if (0 != strcmp (pref_str, "NONE"))
1188 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1190 if (0 == strcmp (pref_str, prefs[c]))
1199 /* abuse terminator to indicate no pref */
1200 pref_val = GNUNET_ATS_PREFERENCE_END;
1204 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1205 GNUNET_free(solver);
1206 GNUNET_free(pref_str);
1207 GNUNET_free (comm_name);
1211 for (c = 0; c < (argc - 1); c++)
1213 if (0 == strcmp (argv[c], "-d"))
1218 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1219 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1223 perf_duration = BENCHMARK_DURATION;
1225 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1227 for (c = 0; c < (argc - 1); c++)
1229 if (0 == strcmp (argv[c], "-s"))
1234 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1235 && (num_slaves >= 1))
1236 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1238 num_slaves = DEFAULT_SLAVES_NUM;
1241 num_slaves = DEFAULT_SLAVES_NUM;
1243 for (c = 0; c < (argc - 1); c++)
1245 if (0 == strcmp (argv[c], "-m"))
1250 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1251 && (num_masters >= 2))
1252 fprintf (stderr, "Starting %u master peers\n", num_masters);
1254 num_masters = DEFAULT_MASTERS_NUM;
1257 num_masters = DEFAULT_MASTERS_NUM;
1259 logging = GNUNET_NO;
1260 for (c = 0; c < argc; c++)
1262 if (0 == strcmp (argv[c], "-l"))
1263 logging = GNUNET_YES;
1266 if (GNUNET_YES == logging)
1268 for (c = 0; c < (argc - 1); c++)
1270 if (0 == strcmp (argv[c], "-f"))
1275 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
1276 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1280 log_frequency = LOGGING_FREQUENCY;
1282 fprintf (stderr, "Using log frequency %llu ms\n",
1283 (unsigned long long) (log_frequency.rel_value_us) / (1000));
1286 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1288 if (num_slaves < num_masters)
1290 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1291 GNUNET_free(test_name);
1292 GNUNET_free(solver);
1293 GNUNET_free(pref_str);
1294 GNUNET_free (comm_name);
1295 return GNUNET_SYSERR;
1298 state.connected_ATS_service = GNUNET_NO;
1299 state.connected_COMM_service = GNUNET_NO;
1300 state.connected_PEERS = GNUNET_NO;
1301 state.benchmarking = GNUNET_NO;
1302 state.connected_PEERS = GNUNET_NO;
1304 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1305 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1307 /* Start topology */
1308 uint64_t event_mask;
1310 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1311 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1312 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1313 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1316 GNUNET_free(solver);
1317 GNUNET_free(pref_str);
1318 GNUNET_free(conf_name);
1319 GNUNET_free(test_name);
1320 GNUNET_free(testname);
1321 GNUNET_free (comm_name);
1328 /* end of file perf_ats.c */