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"
32 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 #define TESTNAME_PREFIX "perf_ats_"
35 #define DEFAULT_SLAVES_NUM 3
36 #define DEFAULT_MASTERS_NUM 1
38 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
39 #define TEST_ATS_PREFRENCE_START 1.0
40 #define TEST_ATS_PREFRENCE_DELTA 1.0
42 #define TEST_MESSAGE_TYPE_PING 12345
43 #define TEST_MESSAGE_TYPE_PONG 12346
44 #define TEST_MESSAGE_SIZE 1000
45 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
48 * Information about a benchmarking partner
50 struct BenchmarkPartner
53 * The peer itself this partner belongs to
55 struct BenchmarkPeer *me;
60 struct BenchmarkPeer *dest;
63 * Core transmit handles
65 struct GNUNET_CORE_TransmitHandle *cth;
68 * Transport transmit handles
70 struct GNUNET_TRANSPORT_TransmitHandle *tth;
73 * Number of messages sent to this partner
75 unsigned int messages_sent;
78 * Number of bytes sent to this partner
80 unsigned int bytes_sent;
83 * Number of messages received from this partner
85 unsigned int messages_received;
88 * Number of bytes received from this partner
90 unsigned int bytes_received;
94 * Connect peers with testbed
96 struct TestbedConnectOperation
99 * The benchmarking master initiating this connection
101 struct BenchmarkPeer *master;
104 * The benchmarking slave to connect to
106 struct BenchmarkPeer *slave;
109 * Testbed operation to connect peers
111 struct GNUNET_TESTBED_Operation *connect_op;
115 * Information we track for a peer in the testbed.
120 * Handle with testbed.
122 struct GNUNET_TESTBED_Peer *peer;
130 * Is this peer a measter: GNUNET_YES/GNUNET_NO
137 struct GNUNET_PeerIdentity id;
140 * Testbed operation to get peer information
142 struct GNUNET_TESTBED_Operation *peer_id_op;
145 * Testbed operation to connect to ATS performance service
147 struct GNUNET_TESTBED_Operation *ats_perf_op;
150 * Testbed operation to connect to core
152 struct GNUNET_TESTBED_Operation *comm_op;
155 * ATS performance handle
157 struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
161 * Testbed connect operations to connect masters to slaves
163 struct TestbedConnectOperation *core_connect_ops;
168 struct GNUNET_CORE_Handle *ch;
173 struct GNUNET_TRANSPORT_Handle *th;
177 * Peer to set ATS preferences for
179 struct BenchmarkPeer *pref_partner;
185 GNUNET_SCHEDULER_TaskIdentifier ats_task;
194 * Array of partners with num_slaves entries (if master) or
195 * num_master entries (if slave)
197 struct BenchmarkPartner *partners;
200 * Number of core connections
202 int core_connections;
206 * Number of connections to slave peers
208 int core_slave_connections;
211 * Total number of messages this peer has sent
213 unsigned int total_messages_sent;
216 * Total number of bytes this peer has sent
218 unsigned int total_bytes_sent;
221 * Total number of messages this peer has received
223 unsigned int total_messages_received;
226 * Total number of bytes this peer has received
228 unsigned int total_bytes_received;
232 * Overall state of the performance benchmark
234 struct BenchmarkState
236 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
237 int connected_ATS_service;
239 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
240 int connected_COMM_service;
242 /* Are we connected to all peers: GNUNET_YES/NO */
245 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
248 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
255 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
260 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
267 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
269 static int test_core;
279 static char *pref_str;
282 * ATS preference value
287 * Number master peers
289 static int num_masters;
292 * Array of master peers
294 struct BenchmarkPeer *mps;
299 static int num_slaves;
301 * Array of slave peers
303 struct BenchmarkPeer *sps;
308 static struct BenchmarkState state;
315 unsigned int duration;
316 struct BenchmarkPeer *mp;
318 duration = (BENCHMARK_DURATION.rel_value_us / (1000 * 1000));
319 for (c_m = 0; c_m < num_masters; c_m++)
323 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
324 mp->no, mp->total_bytes_sent / 1024, duration,
325 (mp->total_bytes_sent / 1024) / duration,
326 mp->total_bytes_received / 1024, duration,
327 (mp->total_bytes_received / 1024) / duration);
329 for (c_s = 0; c_s < num_slaves; c_s++)
332 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
333 (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
334 mp->no, mp->partners[c_s].dest->no,
335 (mp->partners[c_s].bytes_sent / 1024) / duration,
336 ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
337 (mp->partners[c_s].bytes_received / 1024) / duration,
338 ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
347 * @param tc the task context
350 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
357 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
359 fprintf (stderr, "0\n");
360 GNUNET_SCHEDULER_cancel (progress_task);
362 progress_task = GNUNET_SCHEDULER_NO_TASK;
365 state.benchmarking = GNUNET_NO;
366 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
368 for (c_m = 0; c_m < num_masters; c_m++)
370 if (NULL != mps[c_m].peer_id_op)
372 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
373 mps[c_m].peer_id_op = NULL;
376 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
377 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
378 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
380 for (c_op = 0; c_op < num_slaves; c_op++)
383 if (NULL != mps[c_m].partners[c_op].cth)
385 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
386 mps[c_m].partners[c_op].cth = NULL;
388 if (NULL != mps[c_m].partners[c_op].tth)
390 GNUNET_TRANSPORT_notify_transmit_ready_cancel (mps[c_m].partners[c_op].tth);
391 mps[c_m].partners[c_op].tth = NULL;
395 if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
397 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
398 _("Failed to connect peer 0 and %u\n"), c_op);
399 GNUNET_TESTBED_operation_done (
400 mps[c_m].core_connect_ops[c_op].connect_op);
401 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
406 if (NULL != mps[c_m].ats_perf_op)
408 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
409 mps[c_m].ats_perf_op = NULL;
412 if (NULL != mps[c_m].comm_op)
414 GNUNET_TESTBED_operation_done (mps[c_m].comm_op);
415 mps[c_m].comm_op = NULL;
417 GNUNET_free(mps[c_m].core_connect_ops);
418 GNUNET_free(mps[c_m].partners);
419 mps[c_m].partners = NULL;
422 for (c_s = 0; c_s < num_slaves; c_s++)
424 if (NULL != sps[c_s].peer_id_op)
426 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
427 sps[c_s].peer_id_op = NULL;
430 for (c_op = 0; c_op < num_slaves; c_op++)
432 if (NULL != sps[c_s].partners[c_op].cth)
434 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
435 sps[c_s].partners[c_op].cth = NULL;
438 if (NULL != sps[c_s].partners[c_op].tth)
440 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sps[c_s].partners[c_op].tth);
441 sps[c_s].partners[c_op].tth = NULL;
445 if (NULL != sps[c_s].ats_perf_op)
447 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
448 sps[c_s].ats_perf_op = NULL;
450 if (NULL != sps[c_s].comm_op)
452 GNUNET_TESTBED_operation_done (sps[c_s].comm_op);
453 sps[c_s].comm_op = NULL;
456 GNUNET_free(sps[c_s].partners);
457 sps[c_s].partners = NULL;
460 GNUNET_SCHEDULER_shutdown ();
463 static struct BenchmarkPeer *
464 find_peer (const struct GNUNET_PeerIdentity * peer)
468 for (c_p = 0; c_p < num_masters; c_p++)
470 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
474 for (c_p = 0; c_p < num_slaves; c_p++)
476 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
483 * Controller event callback
486 * @param event the controller event
489 controller_event_cb (void *cls,
490 const struct GNUNET_TESTBED_EventInformation *event)
492 //struct BenchmarkPeer *p = cls;
495 case GNUNET_TESTBED_ET_CONNECT:
497 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
502 GNUNET_SCHEDULER_cancel (shutdown_task);
503 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
508 comm_send_ready (void *cls, size_t size, void *buf)
510 static char msgbuf[TEST_MESSAGE_SIZE];
511 struct BenchmarkPartner *partner = cls;
512 struct GNUNET_MessageHeader *msg;
514 if (GNUNET_YES == test_core)
519 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
520 partner->me->no, partner->dest->no);
522 partner->messages_sent++;
523 partner->bytes_sent += TEST_MESSAGE_SIZE;
524 partner->me->total_messages_sent++;
525 partner->me->total_bytes_sent += TEST_MESSAGE_SIZE;
527 msg = (struct GNUNET_MessageHeader *) &msgbuf;
528 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
529 msg->type = htons (TEST_MESSAGE_TYPE_PING);
530 msg->size = htons (TEST_MESSAGE_SIZE);
531 memcpy (buf, msg, TEST_MESSAGE_SIZE);
532 return TEST_MESSAGE_SIZE;
539 progress_task = GNUNET_SCHEDULER_NO_TASK;
541 fprintf (stderr, "%llu..",
542 (long long unsigned) BENCHMARK_DURATION.rel_value_us / (1000 * 1000) - calls);
545 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
546 &print_progress, NULL );
550 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552 struct BenchmarkPeer *me = cls;
554 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
556 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
557 me->no, me->pref_partner->no, me->pref_value);
558 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
559 &me->pref_partner->id,
560 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
561 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
562 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
563 &ats_pref_task, cls);
572 if ((state.connected_ATS_service == GNUNET_NO)
573 || (state.connected_COMM_service == GNUNET_NO)
574 || (state.connected_PEERS == GNUNET_NO)
575 || (state.connected_CORE == GNUNET_NO))
578 state.benchmarking = GNUNET_YES;
579 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
581 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
582 GNUNET_SCHEDULER_cancel (shutdown_task);
583 shutdown_task = GNUNET_SCHEDULER_add_delayed (BENCHMARK_DURATION,
584 &do_shutdown, NULL );
586 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
588 /* Start sending test messages */
589 for (c_m = 0; c_m < num_masters; c_m++)
591 for (c_s = 0; c_s < num_slaves; c_s++)
593 if (GNUNET_YES == test_core)
594 mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
595 mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
596 TEST_MESSAGE_SIZE, &comm_send_ready, &mps[c_m].partners[c_s]);
599 mps[c_m].partners[c_s].tth = GNUNET_TRANSPORT_notify_transmit_ready (
600 mps[c_m].th, &sps[c_s].id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
601 &comm_send_ready, &mps[c_m].partners[c_s]);
604 if (pref_val != GNUNET_ATS_PREFERENCE_END)
605 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
610 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
613 struct TestbedConnectOperation *cop = cls;
618 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
619 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
624 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
625 _("Failed to connect master peer [%u] with slave [%u]\n"),
626 cop->master->no, cop->slave->no);
628 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
629 GNUNET_SCHEDULER_cancel (shutdown_task);
630 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
632 GNUNET_TESTBED_operation_done (op);
634 for (c = 0; c < num_slaves; c++)
636 if (cop == &cop->master->core_connect_ops[c])
637 cop->master->core_connect_ops[c].connect_op = NULL;
639 if (ops == num_masters * num_slaves)
641 state.connected_PEERS = GNUNET_YES;
642 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
647 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
651 struct BenchmarkPeer *p;
653 if ((state.connected_ATS_service == GNUNET_NO)
654 || (state.connected_COMM_service == GNUNET_NO))
657 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
659 for (c_m = 0; c_m < num_masters; c_m++)
662 p->core_connect_ops = GNUNET_malloc (num_slaves *
663 sizeof (struct TestbedConnectOperation));
665 for (c_s = 0; c_s < num_slaves; c_s++)
667 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
668 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
669 p->core_connect_ops[c_s].master = p;
670 p->core_connect_ops[c_s].slave = &sps[c_s];
671 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
672 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
673 sps[c_s].peer, p->peer);
674 if (NULL == p->core_connect_ops[c_s].connect_op)
676 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
677 _("Could not connect master [%u] and slave [%u]\n"), p->no,
680 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
681 GNUNET_SCHEDULER_cancel (shutdown_task);
682 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
690 * Method called whenever a given peer connects.
693 * @param peer peer identity this notification is about
696 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
698 struct BenchmarkPeer *me = cls;
699 struct BenchmarkPeer *remote;
704 remote = find_peer (peer);
711 id = GNUNET_strdup (GNUNET_i2s (&me->id));
712 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
713 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
714 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
717 me->core_connections++;
718 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
719 && (GNUNET_NO == state.connected_CORE))
721 me->core_slave_connections++;
723 if (me->core_slave_connections == num_slaves)
725 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
728 completed = GNUNET_YES;
729 for (c = 0; c < num_masters; c++)
731 if (mps[c].core_slave_connections != num_slaves)
732 completed = GNUNET_NO;
734 if (GNUNET_YES == completed)
736 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
737 "All master peers connected all slave peers\n", id,
739 state.connected_CORE = GNUNET_YES;
740 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
747 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
749 struct BenchmarkPeer *me = cls;
750 struct BenchmarkPeer *remote;
753 remote = find_peer (peer);
760 id = GNUNET_strdup (GNUNET_i2s (&me->id));
761 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
763 GNUNET_assert(me->core_connections > 0);
764 me->core_connections--;
766 if ((GNUNET_YES == state.benchmarking)
767 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
769 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
770 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
776 comm_send_pong_ready (void *cls, size_t size, void *buf)
778 static char msgbuf[TEST_MESSAGE_SIZE];
779 struct BenchmarkPartner *p = cls;
780 struct GNUNET_MessageHeader *msg;
782 if (GNUNET_YES == test_core)
788 p->bytes_sent += TEST_MESSAGE_SIZE;
789 p->me->total_messages_sent++;
790 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
792 msg = (struct GNUNET_MessageHeader *) &msgbuf;
793 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
794 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
795 msg->size = htons (TEST_MESSAGE_SIZE);
796 memcpy (buf, msg, TEST_MESSAGE_SIZE);
798 return TEST_MESSAGE_SIZE;
802 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
803 const struct GNUNET_MessageHeader *message)
806 struct BenchmarkPeer *me = cls;
807 struct BenchmarkPartner *p = NULL;
808 for (c_m = 0; c_m < num_masters; c_m++)
811 == memcmp (other, &me->partners[c_m].dest->id,
812 sizeof(struct GNUNET_PeerIdentity)))
814 p = &me->partners[c_m];
821 return GNUNET_SYSERR;
824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
825 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
828 p->messages_received++;
829 p->bytes_received += TEST_MESSAGE_SIZE;
830 p->me->total_messages_received++;
831 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
833 if (GNUNET_YES == test_core)
835 GNUNET_assert (NULL == p->cth);
836 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
837 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
838 &comm_send_pong_ready, p);
842 GNUNET_assert (NULL == p->tth);
843 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
844 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
851 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
852 const struct GNUNET_MessageHeader *message)
855 struct BenchmarkPeer *me = cls;
856 struct BenchmarkPartner *p = NULL;
858 for (c_s = 0; c_s < num_slaves; c_s++)
861 == memcmp (other, &me->partners[c_s].dest->id,
862 sizeof(struct GNUNET_PeerIdentity)))
864 p = &me->partners[c_s];
871 return GNUNET_SYSERR;
874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
875 "Master [%u]: Received PONG from [%u], next message\n", me->no,
878 p->messages_received++;
879 p->bytes_received += TEST_MESSAGE_SIZE;
880 p->me->total_messages_received++;
881 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
883 if (GNUNET_YES == test_core)
885 GNUNET_assert (NULL == p->cth);
886 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
887 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
888 &comm_send_ready, p);
892 GNUNET_assert (NULL == p->tth);
893 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
894 me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
895 &comm_send_ready, p);
902 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
904 struct BenchmarkPeer *me = cls;
906 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
907 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
908 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
910 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
911 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
913 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
918 core_disconnect_adapter (void *cls, void *op_result)
920 struct BenchmarkPeer *me = cls;
922 GNUNET_CORE_disconnect (me->ch);
927 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
928 void *ca_result, const char *emsg)
930 static int comm_done = 0;
931 if ((NULL != emsg) || (NULL == ca_result))
933 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
935 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
936 GNUNET_SCHEDULER_cancel (shutdown_task);
937 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
942 if (comm_done == num_slaves + num_masters)
944 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
945 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
946 state.connected_COMM_service = GNUNET_YES;
947 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
952 transport_recv_cb (void *cls,
953 const struct GNUNET_PeerIdentity * peer,
954 const struct GNUNET_MessageHeader * message)
956 struct BenchmarkPeer *me = cls;
958 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
959 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
960 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
964 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
965 comm_handle_ping (cls, peer, message);
967 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
968 comm_handle_pong (cls, peer, message);
973 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
975 struct BenchmarkPeer *me = cls;
977 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
978 &comm_connect_cb, &comm_disconnect_cb);
980 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
985 transport_disconnect_adapter (void *cls, void *op_result)
987 struct BenchmarkPeer *me = cls;
989 GNUNET_TRANSPORT_disconnect (me->th);
994 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
998 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
999 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
1000 for (c_m = 0; c_m < num_masters; c_m++)
1002 if (GNUNET_YES == test_core)
1003 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1004 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
1005 &core_disconnect_adapter, &mps[c_m]);
1008 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1009 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
1010 &transport_disconnect_adapter, &mps[c_m]);
1014 for (c_s = 0; c_s < num_slaves; c_s++)
1016 if (GNUNET_YES == test_core)
1017 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1018 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
1019 &core_disconnect_adapter, &sps[c_s]);
1022 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1023 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
1024 &transport_disconnect_adapter, &sps[c_s]);
1030 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
1031 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1032 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1033 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1035 struct BenchmarkPeer *me = cls;
1039 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
1040 for (c_a = 0; c_a < ats_count; c_a++)
1042 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
1043 (GNUNET_YES == p->master) ? 'M' : 'S',
1045 GNUNET_i2s (&address->peer),
1046 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
1047 ntohl(ats[c_a].value));*/
1050 if ((GNUNET_YES == me->master)
1051 && (0 == memcmp (&address->peer, &me->destination->id,
1052 sizeof(struct GNUNET_PeerIdentity))))
1054 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
1055 me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
1056 (long unsigned int ) ntohl (bandwidth_in.value__));
1059 store_information (&bp->id, address, address_active, bandwidth_in,
1060 bandwidth_out, ats, ats_count);
1062 GNUNET_free(peer_id);
1066 ats_perf_connect_adapter (void *cls,
1067 const struct GNUNET_CONFIGURATION_Handle *cfg)
1069 struct BenchmarkPeer *me = cls;
1071 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
1072 &ats_performance_info_cb, me);
1073 if (NULL == me->ats_perf_handle)
1074 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1075 "Failed to create ATS performance handle \n");
1076 return me->ats_perf_handle;
1080 ats_perf_disconnect_adapter (void *cls, void *op_result)
1082 struct BenchmarkPeer *me = cls;
1084 GNUNET_ATS_performance_done (me->ats_perf_handle);
1085 me->ats_perf_handle = NULL;
1089 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1090 void *ca_result, const char *emsg)
1092 static int op_done = 0;
1094 if ((NULL != emsg) || (NULL == ca_result))
1096 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
1098 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
1099 GNUNET_SCHEDULER_cancel (shutdown_task);
1100 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
1104 if (op_done == (num_masters + num_slaves))
1106 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
1107 state.connected_ATS_service = GNUNET_YES;
1108 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
1113 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1118 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1119 for (c_m = 0; c_m < num_masters; c_m++)
1121 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1122 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1123 &ats_perf_disconnect_adapter, &mps[c_m]);
1127 for (c_s = 0; c_s < num_slaves; c_s++)
1129 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1130 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1131 &ats_perf_disconnect_adapter, &sps[c_s]);
1137 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1138 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1140 struct BenchmarkPeer *p = cb_cls;
1141 static int done = 0;
1143 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1145 p->id = *pinfo->result.id;
1146 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1147 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1148 GNUNET_i2s (&p->id));
1150 GNUNET_TESTBED_operation_done (op);
1151 p->peer_id_op = NULL;
1154 if (done == num_slaves + num_masters)
1156 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1157 "Retrieved all peer ID, connect to ATS\n");
1158 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1163 * Signature of a main function for a testcase.
1165 * @param cls closure
1166 * @param num_peers number of peers in 'peers'
1167 * @param peers_ handle to peers run in the testbed
1168 * @param links_succeeded the number of overlay link connection attempts that
1170 * @param links_failed the number of overlay link connection attempts that
1174 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1175 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1176 unsigned int links_failed)
1180 GNUNET_assert(NULL == cls);
1181 GNUNET_assert(num_masters + num_slaves == num_peers);
1182 GNUNET_assert(NULL != peers_);
1184 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1185 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1186 solver, pref_str, num_masters, num_slaves);
1188 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1189 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1190 &do_shutdown, NULL );
1192 /* Setup master peers */
1193 for (c_m = 0; c_m < num_masters; c_m++)
1195 GNUNET_assert(NULL != peers_[c_m]);
1196 mps[c_m].peer = peers_[c_m];
1198 mps[c_m].master = GNUNET_YES;
1199 mps[c_m].pref_partner = &sps[c_m];
1200 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1202 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1203 /* Initialize partners */
1204 for (c_s = 0; c_s < num_slaves; c_s++)
1206 mps[c_m].partners[c_s].me = &mps[c_m];
1207 mps[c_m].partners[c_s].dest = &sps[c_s];
1209 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1210 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1213 /* Setup slave peers */
1214 for (c_s = 0; c_s < num_slaves; c_s++)
1216 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1217 sps[c_s].peer = peers_[c_s + num_masters];
1218 sps[c_s].no = c_s + num_masters;
1219 sps[c_s].master = GNUNET_NO;
1221 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1222 /* Initialize partners */
1223 for (c_m = 0; c_m < num_masters; c_m++)
1225 sps[c_s].partners[c_m].me = &sps[c_s];
1226 sps[c_s].partners[c_m].dest = &mps[c_m];
1228 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1229 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1234 main (int argc, char *argv[])
1242 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1247 /* figure out testname */
1248 tmp = strstr (argv[0], TESTNAME_PREFIX);
1251 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1252 return GNUNET_SYSERR;
1254 tmp += strlen (TESTNAME_PREFIX);
1255 solver = GNUNET_strdup (tmp);
1256 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1258 tmp_sep = strchr (solver, '_');
1259 if (NULL == tmp_sep)
1261 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1262 GNUNET_free(solver);
1263 return GNUNET_SYSERR;
1266 comm_name = GNUNET_strdup (&tmp_sep[1]);
1267 tmp_sep = strchr (comm_name, '_');
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 for (c = 0; c <= strlen (comm_name); c++)
1276 comm_name[c] = toupper (comm_name[c]);
1277 if (0 == strcmp (comm_name, "CORE"))
1278 test_core = GNUNET_YES;
1279 else if (0 == strcmp (comm_name, "TRANSPORT"))
1280 test_core = GNUNET_NO;
1283 GNUNET_free (comm_name);
1284 GNUNET_free (solver);
1285 return GNUNET_SYSERR;
1288 pref_str = GNUNET_strdup(tmp_sep + 1);
1290 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1292 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1294 for (c = 0; c <= strlen (pref_str); c++)
1295 pref_str[c] = toupper (pref_str[c]);
1298 if (0 != strcmp (pref_str, "NONE"))
1300 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1302 if (0 == strcmp (pref_str, prefs[c]))
1311 /* abuse terminator to indicate no pref */
1312 pref_val = GNUNET_ATS_PREFERENCE_END;
1316 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1317 GNUNET_free(solver);
1318 GNUNET_free(pref_str);
1319 GNUNET_free (comm_name);
1323 for (c = 0; c < (argc - 1); c++)
1325 if (0 == strcmp (argv[c], "-s"))
1330 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1331 && (num_slaves >= 1))
1332 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1334 num_slaves = DEFAULT_SLAVES_NUM;
1337 num_slaves = DEFAULT_SLAVES_NUM;
1339 for (c = 0; c < (argc - 1); c++)
1341 if (0 == strcmp (argv[c], "-m"))
1346 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1347 && (num_masters >= 2))
1348 fprintf (stderr, "Starting %u master peers\n", num_masters);
1350 num_masters = DEFAULT_MASTERS_NUM;
1353 num_masters = DEFAULT_MASTERS_NUM;
1355 if (num_slaves < num_masters)
1357 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1358 GNUNET_free(test_name);
1359 GNUNET_free(solver);
1360 GNUNET_free(pref_str);
1361 GNUNET_free (comm_name);
1362 return GNUNET_SYSERR;
1365 state.connected_ATS_service = GNUNET_NO;
1366 state.connected_COMM_service = GNUNET_NO;
1367 state.connected_PEERS = GNUNET_NO;
1368 state.benchmarking = GNUNET_NO;
1369 state.connected_PEERS = GNUNET_NO;
1371 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1372 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1374 /* Start topology */
1375 uint64_t event_mask;
1377 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1378 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1379 (void) GNUNET_TESTBED_test_run ("perf_ats", conf_name,
1380 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1383 GNUNET_free(solver);
1384 GNUNET_free(pref_str);
1385 GNUNET_free(conf_name);
1386 GNUNET_free(test_name);
1387 GNUNET_free (comm_name);
1394 /* end of file perf_ats.c */