2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file ats/perf_ats.c
22 * @brief ats benchmark: start peers and modify preferences, monitor change over time
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "perf_ats_logging.h"
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
34 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
35 #define TESTNAME_PREFIX "perf_ats_"
36 #define DEFAULT_SLAVES_NUM 3
37 #define DEFAULT_MASTERS_NUM 1
39 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 #define TEST_ATS_PREFRENCE_START 1.0
41 #define TEST_ATS_PREFRENCE_DELTA 1.0
43 #define TEST_MESSAGE_TYPE_PING 12345
44 #define TEST_MESSAGE_TYPE_PONG 12346
45 #define TEST_MESSAGE_SIZE 1000
46 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
49 * Information about a benchmarking partner
51 struct BenchmarkPartner
54 * The peer itself this partner belongs to
56 struct BenchmarkPeer *me;
61 struct BenchmarkPeer *dest;
64 * Core transmit handles
66 struct GNUNET_CORE_TransmitHandle *cth;
69 * Transport transmit handles
71 struct GNUNET_TRANSPORT_TransmitHandle *tth;
74 * Number of messages sent to this partner
76 unsigned int messages_sent;
79 * Number of bytes sent to this partner
81 unsigned int bytes_sent;
84 * Number of messages received from this partner
86 unsigned int messages_received;
89 * Number of bytes received from this partner
91 unsigned int bytes_received;
95 * Connect peers with testbed
97 struct TestbedConnectOperation
100 * The benchmarking master initiating this connection
102 struct BenchmarkPeer *master;
105 * The benchmarking slave to connect to
107 struct BenchmarkPeer *slave;
110 * Testbed operation to connect peers
112 struct GNUNET_TESTBED_Operation *connect_op;
116 * Information we track for a peer in the testbed.
121 * Handle with testbed.
123 struct GNUNET_TESTBED_Peer *peer;
131 * Is this peer a measter: GNUNET_YES/GNUNET_NO
138 struct GNUNET_PeerIdentity id;
141 * Testbed operation to get peer information
143 struct GNUNET_TESTBED_Operation *peer_id_op;
146 * Testbed operation to connect to ATS performance service
148 struct GNUNET_TESTBED_Operation *ats_perf_op;
151 * Testbed operation to connect to core
153 struct GNUNET_TESTBED_Operation *comm_op;
156 * ATS performance handle
158 struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
162 * Testbed connect operations to connect masters to slaves
164 struct TestbedConnectOperation *core_connect_ops;
169 struct GNUNET_CORE_Handle *ch;
174 struct GNUNET_TRANSPORT_Handle *th;
178 * Peer to set ATS preferences for
180 struct BenchmarkPeer *pref_partner;
186 GNUNET_SCHEDULER_TaskIdentifier ats_task;
195 * Array of partners with num_slaves entries (if master) or
196 * num_master entries (if slave)
198 struct BenchmarkPartner *partners;
201 * Number of core connections
203 int core_connections;
207 * Number of connections to slave peers
209 int core_slave_connections;
212 * Total number of messages this peer has sent
214 unsigned int total_messages_sent;
217 * Total number of bytes this peer has sent
219 unsigned int total_bytes_sent;
222 * Total number of messages this peer has received
224 unsigned int total_messages_received;
227 * Total number of bytes this peer has received
229 unsigned int total_bytes_received;
233 * Overall state of the performance benchmark
235 struct BenchmarkState
237 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
238 int connected_ATS_service;
240 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
241 int connected_COMM_service;
243 /* Are we connected to all peers: GNUNET_YES/NO */
246 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
249 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
256 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
261 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
269 * Test result logging
273 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
275 static int test_core;
285 static char *pref_str;
288 * ATS preference value
293 * Number master peers
295 static int num_masters;
298 * Array of master peers
300 struct BenchmarkPeer *mps;
305 static int num_slaves;
307 * Array of slave peers
309 struct BenchmarkPeer *sps;
314 static struct BenchmarkState state;
321 unsigned int duration;
322 struct BenchmarkPeer *mp;
324 duration = (BENCHMARK_DURATION.rel_value_us / (1000 * 1000));
325 for (c_m = 0; c_m < num_masters; c_m++)
329 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
330 mp->no, mp->total_bytes_sent / 1024, duration,
331 (mp->total_bytes_sent / 1024) / duration,
332 mp->total_bytes_received / 1024, duration,
333 (mp->total_bytes_received / 1024) / duration);
335 for (c_s = 0; c_s < num_slaves; c_s++)
338 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
339 (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
340 mp->no, mp->partners[c_s].dest->no,
341 (mp->partners[c_s].bytes_sent / 1024) / duration,
342 ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
343 (mp->partners[c_s].bytes_received / 1024) / duration,
344 ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
353 * @param tc the task context
356 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 if (GNUNET_YES == logging)
365 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
366 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
368 fprintf (stderr, "0\n");
369 GNUNET_SCHEDULER_cancel (progress_task);
371 progress_task = GNUNET_SCHEDULER_NO_TASK;
374 state.benchmarking = GNUNET_NO;
375 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
377 for (c_m = 0; c_m < num_masters; c_m++)
379 if (NULL != mps[c_m].peer_id_op)
381 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
382 mps[c_m].peer_id_op = NULL;
385 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
386 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
387 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
389 for (c_op = 0; c_op < num_slaves; c_op++)
392 if (NULL != mps[c_m].partners[c_op].cth)
394 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
395 mps[c_m].partners[c_op].cth = NULL;
397 if (NULL != mps[c_m].partners[c_op].tth)
399 GNUNET_TRANSPORT_notify_transmit_ready_cancel (mps[c_m].partners[c_op].tth);
400 mps[c_m].partners[c_op].tth = NULL;
404 if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
406 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
407 _("Failed to connect peer 0 and %u\n"), c_op);
408 GNUNET_TESTBED_operation_done (
409 mps[c_m].core_connect_ops[c_op].connect_op);
410 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
415 if (NULL != mps[c_m].ats_perf_op)
417 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
418 mps[c_m].ats_perf_op = NULL;
421 if (NULL != mps[c_m].comm_op)
423 GNUNET_TESTBED_operation_done (mps[c_m].comm_op);
424 mps[c_m].comm_op = NULL;
426 GNUNET_free(mps[c_m].core_connect_ops);
427 GNUNET_free(mps[c_m].partners);
428 mps[c_m].partners = NULL;
431 for (c_s = 0; c_s < num_slaves; c_s++)
433 if (NULL != sps[c_s].peer_id_op)
435 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
436 sps[c_s].peer_id_op = NULL;
439 for (c_op = 0; c_op < num_slaves; c_op++)
441 if (NULL != sps[c_s].partners[c_op].cth)
443 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
444 sps[c_s].partners[c_op].cth = NULL;
447 if (NULL != sps[c_s].partners[c_op].tth)
449 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sps[c_s].partners[c_op].tth);
450 sps[c_s].partners[c_op].tth = NULL;
454 if (NULL != sps[c_s].ats_perf_op)
456 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
457 sps[c_s].ats_perf_op = NULL;
459 if (NULL != sps[c_s].comm_op)
461 GNUNET_TESTBED_operation_done (sps[c_s].comm_op);
462 sps[c_s].comm_op = NULL;
465 GNUNET_free(sps[c_s].partners);
466 sps[c_s].partners = NULL;
469 GNUNET_SCHEDULER_shutdown ();
472 static struct BenchmarkPeer *
473 find_peer (const struct GNUNET_PeerIdentity * peer)
477 for (c_p = 0; c_p < num_masters; c_p++)
479 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
483 for (c_p = 0; c_p < num_slaves; c_p++)
485 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
492 * Controller event callback
495 * @param event the controller event
498 controller_event_cb (void *cls,
499 const struct GNUNET_TESTBED_EventInformation *event)
501 //struct BenchmarkPeer *p = cls;
504 case GNUNET_TESTBED_ET_CONNECT:
506 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
511 GNUNET_SCHEDULER_cancel (shutdown_task);
512 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
517 comm_send_ready (void *cls, size_t size, void *buf)
519 static char msgbuf[TEST_MESSAGE_SIZE];
520 struct BenchmarkPartner *partner = cls;
521 struct GNUNET_MessageHeader *msg;
523 if (GNUNET_YES == test_core)
528 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
529 partner->me->no, partner->dest->no);
531 partner->messages_sent++;
532 partner->bytes_sent += TEST_MESSAGE_SIZE;
533 partner->me->total_messages_sent++;
534 partner->me->total_bytes_sent += TEST_MESSAGE_SIZE;
536 msg = (struct GNUNET_MessageHeader *) &msgbuf;
537 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
538 msg->type = htons (TEST_MESSAGE_TYPE_PING);
539 msg->size = htons (TEST_MESSAGE_SIZE);
540 memcpy (buf, msg, TEST_MESSAGE_SIZE);
541 return TEST_MESSAGE_SIZE;
548 progress_task = GNUNET_SCHEDULER_NO_TASK;
550 fprintf (stderr, "%llu..",
551 (long long unsigned) BENCHMARK_DURATION.rel_value_us / (1000 * 1000) - calls);
554 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
555 &print_progress, NULL );
559 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
561 struct BenchmarkPeer *me = cls;
563 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
565 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
566 me->no, me->pref_partner->no, me->pref_value);
567 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
568 &me->pref_partner->id,
569 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
570 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
571 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
572 &ats_pref_task, cls);
581 if ((state.connected_ATS_service == GNUNET_NO)
582 || (state.connected_COMM_service == GNUNET_NO)
583 || (state.connected_PEERS == GNUNET_NO)
584 || (state.connected_CORE == GNUNET_NO))
587 state.benchmarking = GNUNET_YES;
588 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
590 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
591 GNUNET_SCHEDULER_cancel (shutdown_task);
592 shutdown_task = GNUNET_SCHEDULER_add_delayed (BENCHMARK_DURATION,
593 &do_shutdown, NULL );
595 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
597 /* Start sending test messages */
598 for (c_m = 0; c_m < num_masters; c_m++)
600 for (c_s = 0; c_s < num_slaves; c_s++)
602 if (GNUNET_YES == test_core)
603 mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
604 mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
605 TEST_MESSAGE_SIZE, &comm_send_ready, &mps[c_m].partners[c_s]);
608 mps[c_m].partners[c_s].tth = GNUNET_TRANSPORT_notify_transmit_ready (
609 mps[c_m].th, &sps[c_s].id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
610 &comm_send_ready, &mps[c_m].partners[c_s]);
613 if (pref_val != GNUNET_ATS_PREFERENCE_END)
614 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
616 if (GNUNET_YES == logging)
617 perf_logging_start();
621 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
624 struct TestbedConnectOperation *cop = cls;
629 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
630 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
635 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
636 _("Failed to connect master peer [%u] with slave [%u]\n"),
637 cop->master->no, cop->slave->no);
639 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
640 GNUNET_SCHEDULER_cancel (shutdown_task);
641 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
643 GNUNET_TESTBED_operation_done (op);
645 for (c = 0; c < num_slaves; c++)
647 if (cop == &cop->master->core_connect_ops[c])
648 cop->master->core_connect_ops[c].connect_op = NULL;
650 if (ops == num_masters * num_slaves)
652 state.connected_PEERS = GNUNET_YES;
653 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
658 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct BenchmarkPeer *p;
664 if ((state.connected_ATS_service == GNUNET_NO)
665 || (state.connected_COMM_service == GNUNET_NO))
668 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
670 for (c_m = 0; c_m < num_masters; c_m++)
673 p->core_connect_ops = GNUNET_malloc (num_slaves *
674 sizeof (struct TestbedConnectOperation));
676 for (c_s = 0; c_s < num_slaves; c_s++)
678 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
679 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
680 p->core_connect_ops[c_s].master = p;
681 p->core_connect_ops[c_s].slave = &sps[c_s];
682 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
683 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
684 sps[c_s].peer, p->peer);
685 if (NULL == p->core_connect_ops[c_s].connect_op)
687 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
688 _("Could not connect master [%u] and slave [%u]\n"), p->no,
691 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
692 GNUNET_SCHEDULER_cancel (shutdown_task);
693 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
701 * Method called whenever a given peer connects.
704 * @param peer peer identity this notification is about
707 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
709 struct BenchmarkPeer *me = cls;
710 struct BenchmarkPeer *remote;
715 remote = find_peer (peer);
722 id = GNUNET_strdup (GNUNET_i2s (&me->id));
723 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
724 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
725 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
728 me->core_connections++;
729 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
730 && (GNUNET_NO == state.connected_CORE))
732 me->core_slave_connections++;
734 if (me->core_slave_connections == num_slaves)
736 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
739 completed = GNUNET_YES;
740 for (c = 0; c < num_masters; c++)
742 if (mps[c].core_slave_connections != num_slaves)
743 completed = GNUNET_NO;
745 if (GNUNET_YES == completed)
747 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
748 "All master peers connected all slave peers\n", id,
750 state.connected_CORE = GNUNET_YES;
751 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
758 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
760 struct BenchmarkPeer *me = cls;
761 struct BenchmarkPeer *remote;
764 remote = find_peer (peer);
771 id = GNUNET_strdup (GNUNET_i2s (&me->id));
772 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
774 GNUNET_assert(me->core_connections > 0);
775 me->core_connections--;
777 if ((GNUNET_YES == state.benchmarking)
778 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
780 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
781 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
787 comm_send_pong_ready (void *cls, size_t size, void *buf)
789 static char msgbuf[TEST_MESSAGE_SIZE];
790 struct BenchmarkPartner *p = cls;
791 struct GNUNET_MessageHeader *msg;
793 if (GNUNET_YES == test_core)
799 p->bytes_sent += TEST_MESSAGE_SIZE;
800 p->me->total_messages_sent++;
801 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
803 msg = (struct GNUNET_MessageHeader *) &msgbuf;
804 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
805 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
806 msg->size = htons (TEST_MESSAGE_SIZE);
807 memcpy (buf, msg, TEST_MESSAGE_SIZE);
809 return TEST_MESSAGE_SIZE;
813 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
814 const struct GNUNET_MessageHeader *message)
817 struct BenchmarkPeer *me = cls;
818 struct BenchmarkPartner *p = NULL;
819 for (c_m = 0; c_m < num_masters; c_m++)
822 == memcmp (other, &me->partners[c_m].dest->id,
823 sizeof(struct GNUNET_PeerIdentity)))
825 p = &me->partners[c_m];
832 return GNUNET_SYSERR;
835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
836 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
839 p->messages_received++;
840 p->bytes_received += TEST_MESSAGE_SIZE;
841 p->me->total_messages_received++;
842 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
844 if (GNUNET_YES == test_core)
846 GNUNET_assert (NULL == p->cth);
847 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
848 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
849 &comm_send_pong_ready, p);
853 GNUNET_assert (NULL == p->tth);
854 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
855 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
862 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
863 const struct GNUNET_MessageHeader *message)
866 struct BenchmarkPeer *me = cls;
867 struct BenchmarkPartner *p = NULL;
869 for (c_s = 0; c_s < num_slaves; c_s++)
872 == memcmp (other, &me->partners[c_s].dest->id,
873 sizeof(struct GNUNET_PeerIdentity)))
875 p = &me->partners[c_s];
882 return GNUNET_SYSERR;
885 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
886 "Master [%u]: Received PONG from [%u], next message\n", me->no,
889 p->messages_received++;
890 p->bytes_received += TEST_MESSAGE_SIZE;
891 p->me->total_messages_received++;
892 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
894 if (GNUNET_YES == test_core)
896 GNUNET_assert (NULL == p->cth);
897 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
898 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
899 &comm_send_ready, p);
903 GNUNET_assert (NULL == p->tth);
904 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
905 me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
906 &comm_send_ready, p);
913 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
915 struct BenchmarkPeer *me = cls;
917 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
918 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
919 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
921 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
922 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
924 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
929 core_disconnect_adapter (void *cls, void *op_result)
931 struct BenchmarkPeer *me = cls;
933 GNUNET_CORE_disconnect (me->ch);
938 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
939 void *ca_result, const char *emsg)
941 static int comm_done = 0;
942 if ((NULL != emsg) || (NULL == ca_result))
944 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
946 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
947 GNUNET_SCHEDULER_cancel (shutdown_task);
948 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
953 if (comm_done == num_slaves + num_masters)
955 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
956 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
957 state.connected_COMM_service = GNUNET_YES;
958 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
963 transport_recv_cb (void *cls,
964 const struct GNUNET_PeerIdentity * peer,
965 const struct GNUNET_MessageHeader * message)
967 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
968 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
969 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
973 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
974 comm_handle_ping (cls, peer, message);
976 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
977 comm_handle_pong (cls, peer, message);
982 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
984 struct BenchmarkPeer *me = cls;
986 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
987 &comm_connect_cb, &comm_disconnect_cb);
989 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
994 transport_disconnect_adapter (void *cls, void *op_result)
996 struct BenchmarkPeer *me = cls;
998 GNUNET_TRANSPORT_disconnect (me->th);
1003 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
1008 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
1009 for (c_m = 0; c_m < num_masters; c_m++)
1011 if (GNUNET_YES == test_core)
1012 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1013 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
1014 &core_disconnect_adapter, &mps[c_m]);
1017 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1018 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
1019 &transport_disconnect_adapter, &mps[c_m]);
1023 for (c_s = 0; c_s < num_slaves; c_s++)
1025 if (GNUNET_YES == test_core)
1026 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1027 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
1028 &core_disconnect_adapter, &sps[c_s]);
1031 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1032 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
1033 &transport_disconnect_adapter, &sps[c_s]);
1039 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
1040 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1041 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1042 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1044 struct BenchmarkPeer *me = cls;
1048 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
1049 for (c_a = 0; c_a < ats_count; c_a++)
1051 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
1052 (GNUNET_YES == p->master) ? 'M' : 'S',
1054 GNUNET_i2s (&address->peer),
1055 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
1056 ntohl(ats[c_a].value));*/
1059 if ((GNUNET_YES == me->master)
1060 && (0 == memcmp (&address->peer, &me->destination->id,
1061 sizeof(struct GNUNET_PeerIdentity))))
1063 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
1064 me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
1065 (long unsigned int ) ntohl (bandwidth_in.value__));
1068 store_information (&bp->id, address, address_active, bandwidth_in,
1069 bandwidth_out, ats, ats_count);
1071 GNUNET_free(peer_id);
1075 ats_perf_connect_adapter (void *cls,
1076 const struct GNUNET_CONFIGURATION_Handle *cfg)
1078 struct BenchmarkPeer *me = cls;
1080 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
1081 &ats_performance_info_cb, me);
1082 if (NULL == me->ats_perf_handle)
1083 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1084 "Failed to create ATS performance handle \n");
1085 return me->ats_perf_handle;
1089 ats_perf_disconnect_adapter (void *cls, void *op_result)
1091 struct BenchmarkPeer *me = cls;
1093 GNUNET_ATS_performance_done (me->ats_perf_handle);
1094 me->ats_perf_handle = NULL;
1098 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1099 void *ca_result, const char *emsg)
1101 static int op_done = 0;
1103 if ((NULL != emsg) || (NULL == ca_result))
1105 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
1107 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
1108 GNUNET_SCHEDULER_cancel (shutdown_task);
1109 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
1113 if (op_done == (num_masters + num_slaves))
1115 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
1116 state.connected_ATS_service = GNUNET_YES;
1117 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
1122 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1127 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1128 for (c_m = 0; c_m < num_masters; c_m++)
1130 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1131 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1132 &ats_perf_disconnect_adapter, &mps[c_m]);
1136 for (c_s = 0; c_s < num_slaves; c_s++)
1138 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1139 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1140 &ats_perf_disconnect_adapter, &sps[c_s]);
1146 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1147 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1149 struct BenchmarkPeer *p = cb_cls;
1150 static int done = 0;
1152 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1154 p->id = *pinfo->result.id;
1155 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1156 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1157 GNUNET_i2s (&p->id));
1159 GNUNET_TESTBED_operation_done (op);
1160 p->peer_id_op = NULL;
1163 if (done == num_slaves + num_masters)
1165 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1166 "Retrieved all peer ID, connect to ATS\n");
1167 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1172 * Signature of a main function for a testcase.
1174 * @param cls closure
1175 * @param num_peers number of peers in 'peers'
1176 * @param peers_ handle to peers run in the testbed
1177 * @param links_succeeded the number of overlay link connection attempts that
1179 * @param links_failed the number of overlay link connection attempts that
1183 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1184 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1185 unsigned int links_failed)
1189 GNUNET_assert(NULL == cls);
1190 GNUNET_assert(num_masters + num_slaves == num_peers);
1191 GNUNET_assert(NULL != peers_);
1193 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1194 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1195 solver, pref_str, num_masters, num_slaves);
1197 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1198 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1199 &do_shutdown, NULL );
1201 /* Setup master peers */
1202 for (c_m = 0; c_m < num_masters; c_m++)
1204 GNUNET_assert(NULL != peers_[c_m]);
1205 mps[c_m].peer = peers_[c_m];
1207 mps[c_m].master = GNUNET_YES;
1208 mps[c_m].pref_partner = &sps[c_m];
1209 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1211 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1212 /* Initialize partners */
1213 for (c_s = 0; c_s < num_slaves; c_s++)
1215 mps[c_m].partners[c_s].me = &mps[c_m];
1216 mps[c_m].partners[c_s].dest = &sps[c_s];
1218 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1219 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1222 /* Setup slave peers */
1223 for (c_s = 0; c_s < num_slaves; c_s++)
1225 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1226 sps[c_s].peer = peers_[c_s + num_masters];
1227 sps[c_s].no = c_s + num_masters;
1228 sps[c_s].master = GNUNET_NO;
1230 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1231 /* Initialize partners */
1232 for (c_m = 0; c_m < num_masters; c_m++)
1234 sps[c_s].partners[c_m].me = &sps[c_s];
1235 sps[c_s].partners[c_m].dest = &mps[c_m];
1237 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1238 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1243 main (int argc, char *argv[])
1251 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1256 /* figure out testname */
1257 tmp = strstr (argv[0], TESTNAME_PREFIX);
1260 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1261 return GNUNET_SYSERR;
1263 tmp += strlen (TESTNAME_PREFIX);
1264 solver = GNUNET_strdup (tmp);
1265 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1267 tmp_sep = strchr (solver, '_');
1268 if (NULL == tmp_sep)
1270 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1271 GNUNET_free(solver);
1272 return GNUNET_SYSERR;
1275 comm_name = GNUNET_strdup (&tmp_sep[1]);
1276 tmp_sep = strchr (comm_name, '_');
1277 if (NULL == tmp_sep)
1279 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1280 GNUNET_free(solver);
1281 return GNUNET_SYSERR;
1284 for (c = 0; c <= strlen (comm_name); c++)
1285 comm_name[c] = toupper (comm_name[c]);
1286 if (0 == strcmp (comm_name, "CORE"))
1287 test_core = GNUNET_YES;
1288 else if (0 == strcmp (comm_name, "TRANSPORT"))
1289 test_core = GNUNET_NO;
1292 GNUNET_free (comm_name);
1293 GNUNET_free (solver);
1294 return GNUNET_SYSERR;
1297 pref_str = GNUNET_strdup(tmp_sep + 1);
1299 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1301 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1303 for (c = 0; c <= strlen (pref_str); c++)
1304 pref_str[c] = toupper (pref_str[c]);
1307 if (0 != strcmp (pref_str, "NONE"))
1309 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1311 if (0 == strcmp (pref_str, prefs[c]))
1320 /* abuse terminator to indicate no pref */
1321 pref_val = GNUNET_ATS_PREFERENCE_END;
1325 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1326 GNUNET_free(solver);
1327 GNUNET_free(pref_str);
1328 GNUNET_free (comm_name);
1332 for (c = 0; c < (argc - 1); c++)
1334 if (0 == strcmp (argv[c], "-s"))
1339 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1340 && (num_slaves >= 1))
1341 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1343 num_slaves = DEFAULT_SLAVES_NUM;
1346 num_slaves = DEFAULT_SLAVES_NUM;
1348 for (c = 0; c < (argc - 1); c++)
1350 if (0 == strcmp (argv[c], "-m"))
1355 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1356 && (num_masters >= 2))
1357 fprintf (stderr, "Starting %u master peers\n", num_masters);
1359 num_masters = DEFAULT_MASTERS_NUM;
1362 num_masters = DEFAULT_MASTERS_NUM;
1364 logging = GNUNET_NO;
1365 for (c = 0; c < argc; c++)
1367 if (0 == strcmp (argv[c], "-l"))
1368 logging = GNUNET_YES;
1372 if (num_slaves < num_masters)
1374 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1375 GNUNET_free(test_name);
1376 GNUNET_free(solver);
1377 GNUNET_free(pref_str);
1378 GNUNET_free (comm_name);
1379 return GNUNET_SYSERR;
1382 state.connected_ATS_service = GNUNET_NO;
1383 state.connected_COMM_service = GNUNET_NO;
1384 state.connected_PEERS = GNUNET_NO;
1385 state.benchmarking = GNUNET_NO;
1386 state.connected_PEERS = GNUNET_NO;
1388 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1389 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1391 /* Start topology */
1392 uint64_t event_mask;
1394 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1395 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1396 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1397 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1400 GNUNET_free(solver);
1401 GNUNET_free(pref_str);
1402 GNUNET_free(conf_name);
1403 GNUNET_free(test_name);
1404 GNUNET_free (comm_name);
1411 /* end of file perf_ats.c */