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
68 * Number of messages sent to this partner
70 unsigned int messages_sent;
73 * Number of bytes sent to this partner
75 unsigned int bytes_sent;
78 * Number of messages received from this partner
80 unsigned int messages_received;
83 * Number of bytes received from this partner
85 unsigned int bytes_received;
89 * Connect peers with testbed
91 struct TestbedConnectOperation
94 * The benchmarking master initiating this connection
96 struct BenchmarkPeer *master;
99 * The benchmarking slave to connect to
101 struct BenchmarkPeer *slave;
104 * Testbed operation to connect peers
106 struct GNUNET_TESTBED_Operation *connect_op;
110 * Information we track for a peer in the testbed.
115 * Handle with testbed.
117 struct GNUNET_TESTBED_Peer *peer;
125 * Is this peer a measter: GNUNET_YES/GNUNET_NO
132 struct GNUNET_PeerIdentity id;
135 * Testbed operation to get peer information
137 struct GNUNET_TESTBED_Operation *peer_id_op;
140 * Testbed operation to connect to ATS performance service
142 struct GNUNET_TESTBED_Operation *ats_perf_op;
145 * Testbed operation to connect to core
147 struct GNUNET_TESTBED_Operation *core_op;
150 * ATS performance handle
152 struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
156 * Testbed connect operations to connect masters to slaves
158 struct TestbedConnectOperation *core_connect_ops;
163 struct GNUNET_CORE_Handle *ch;
167 * Peer to set ATS preferences for
169 struct BenchmarkPeer *pref_partner;
175 GNUNET_SCHEDULER_TaskIdentifier ats_task;
184 * Array of partners with num_slaves entries (if master) or
185 * num_master entries (if slave)
187 struct BenchmarkPartner *partners;
190 * Number of core connections
192 int core_connections;
196 * Number of connections to slave peers
198 int core_slave_connections;
201 * Total number of messages this peer has sent
203 unsigned int total_messages_sent;
206 * Total number of bytes this peer has sent
208 unsigned int total_bytes_sent;
211 * Total number of messages this peer has received
213 unsigned int total_messages_received;
216 * Total number of bytes this peer has received
218 unsigned int total_bytes_received;
222 * Overall state of the performance benchmark
224 struct BenchmarkState
226 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
227 int connected_ATS_service;
229 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
230 int connected_CORE_service;
232 /* Are we connected to all peers: GNUNET_YES/NO */
235 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
238 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
245 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
250 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
265 static char *pref_str;
268 * ATS preference value
273 * Number master peers
275 static int num_masters;
278 * Array of master peers
280 struct BenchmarkPeer *mps;
285 static int num_slaves;
287 * Array of slave peers
289 struct BenchmarkPeer *sps;
294 static struct BenchmarkState state;
301 unsigned int duration;
302 struct BenchmarkPeer *mp;
304 duration = (BENCHMARK_DURATION.rel_value_us / (1000 * 1000));
305 for (c_m = 0; c_m < num_masters; c_m++)
309 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
310 mp->no, mp->total_bytes_sent / 1024, duration,
311 (mp->total_bytes_sent / 1024) / duration,
312 mp->total_bytes_received / 1024, duration,
313 (mp->total_bytes_received / 1024) / duration);
315 for (c_s = 0; c_s < num_slaves; c_s++)
318 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
319 (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
320 mp->no, mp->partners[c_s].dest->no,
321 (mp->partners[c_s].bytes_sent / 1024) / duration,
322 ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
323 (mp->partners[c_s].bytes_received / 1024) / duration,
324 ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
333 * @param tc the task context
336 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
343 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
345 fprintf (stderr, "0\n");
346 GNUNET_SCHEDULER_cancel (progress_task);
348 progress_task = GNUNET_SCHEDULER_NO_TASK;
351 state.benchmarking = GNUNET_NO;
352 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
354 for (c_m = 0; c_m < num_masters; c_m++)
356 if (NULL != mps[c_m].peer_id_op)
358 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
359 mps[c_m].peer_id_op = NULL;
362 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
363 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
364 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
366 for (c_op = 0; c_op < num_slaves; c_op++)
369 if (NULL != mps[c_m].partners[c_op].cth)
371 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
372 mps[c_m].partners[c_op].cth = NULL;
375 if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
377 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
378 _("Failed to connect peer 0 and %u\n"), c_op);
379 GNUNET_TESTBED_operation_done (
380 mps[c_m].core_connect_ops[c_op].connect_op);
381 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
386 if (NULL != mps[c_m].ats_perf_op)
388 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
389 mps[c_m].ats_perf_op = NULL;
392 if (NULL != mps[c_m].core_op)
394 GNUNET_TESTBED_operation_done (mps[c_m].core_op);
395 mps[c_m].core_op = NULL;
397 GNUNET_free(mps[c_m].core_connect_ops);
398 GNUNET_free(mps[c_m].partners);
399 mps[c_m].partners = NULL;
402 for (c_s = 0; c_s < num_slaves; c_s++)
404 if (NULL != sps[c_s].peer_id_op)
406 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
407 sps[c_s].peer_id_op = NULL;
410 for (c_op = 0; c_op < num_slaves; c_op++)
412 if (NULL != sps[c_s].partners[c_op].cth)
414 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
415 sps[c_s].partners[c_op].cth = NULL;
419 if (NULL != sps[c_s].ats_perf_op)
421 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
422 sps[c_s].ats_perf_op = NULL;
424 if (NULL != sps[c_s].core_op)
426 GNUNET_TESTBED_operation_done (sps[c_s].core_op);
427 sps[c_s].core_op = NULL;
430 GNUNET_free(sps[c_s].partners);
431 sps[c_s].partners = NULL;
434 GNUNET_SCHEDULER_shutdown ();
437 static struct BenchmarkPeer *
438 find_peer (const struct GNUNET_PeerIdentity * peer)
442 for (c_p = 0; c_p < num_masters; c_p++)
444 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
448 for (c_p = 0; c_p < num_slaves; c_p++)
450 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
457 * Controller event callback
460 * @param event the controller event
463 controller_event_cb (void *cls,
464 const struct GNUNET_TESTBED_EventInformation *event)
466 //struct BenchmarkPeer *p = cls;
469 case GNUNET_TESTBED_ET_CONNECT:
471 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
476 GNUNET_SCHEDULER_cancel (shutdown_task);
477 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
482 core_send_ready (void *cls, size_t size, void *buf)
484 static char msgbuf[TEST_MESSAGE_SIZE];
485 struct BenchmarkPartner *partner = cls;
486 struct GNUNET_MessageHeader *msg;
489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
490 partner->me->no, partner->dest->no);
492 partner->messages_sent++;
493 partner->bytes_sent += TEST_MESSAGE_SIZE;
494 partner->me->total_messages_sent++;
495 partner->me->total_bytes_sent += TEST_MESSAGE_SIZE;
497 msg = (struct GNUNET_MessageHeader *) &msgbuf;
498 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
499 msg->type = htons (TEST_MESSAGE_TYPE_PING);
500 msg->size = htons (TEST_MESSAGE_SIZE);
501 memcpy (buf, msg, TEST_MESSAGE_SIZE);
502 return TEST_MESSAGE_SIZE;
509 progress_task = GNUNET_SCHEDULER_NO_TASK;
511 fprintf (stderr, "%llu..",
512 (long long unsigned) BENCHMARK_DURATION.rel_value_us / (1000 * 1000) - calls);
515 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
516 &print_progress, NULL );
520 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
522 struct BenchmarkPeer *me = cls;
524 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
527 me->no, me->pref_partner->no, me->pref_value);
528 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
529 &me->pref_partner->id,
530 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
531 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
532 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
533 &ats_pref_task, cls);
542 if ((state.connected_ATS_service == GNUNET_NO)
543 || (state.connected_CORE_service == GNUNET_NO)
544 || (state.connected_PEERS == GNUNET_NO)
545 || (state.connected_CORE == GNUNET_NO))
548 state.benchmarking = GNUNET_YES;
549 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
551 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
552 GNUNET_SCHEDULER_cancel (shutdown_task);
553 shutdown_task = GNUNET_SCHEDULER_add_delayed (BENCHMARK_DURATION,
554 &do_shutdown, NULL );
556 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
558 /* Start sending test messages */
559 for (c_m = 0; c_m < num_masters; c_m++)
561 for (c_s = 0; c_s < num_slaves; c_s++)
563 mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
564 mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
565 TEST_MESSAGE_SIZE, &core_send_ready, &mps[c_m].partners[c_s]);
567 if (pref_val != GNUNET_ATS_PREFERENCE_END)
568 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
573 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
576 struct TestbedConnectOperation *cop = cls;
581 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
582 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
587 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
588 _("Failed to connect master peer [%u] with slave [%u]\n"),
589 cop->master->no, cop->slave->no);
591 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
592 GNUNET_SCHEDULER_cancel (shutdown_task);
593 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
595 GNUNET_TESTBED_operation_done (op);
597 for (c = 0; c < num_slaves; c++)
599 if (cop == &cop->master->core_connect_ops[c])
600 cop->master->core_connect_ops[c].connect_op = NULL;
602 if (ops == num_masters * num_slaves)
604 state.connected_PEERS = GNUNET_YES;
605 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
610 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
614 struct BenchmarkPeer *p;
616 if ((state.connected_ATS_service == GNUNET_NO)
617 || (state.connected_CORE_service == GNUNET_NO))
620 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
622 for (c_m = 0; c_m < num_masters; c_m++)
625 p->core_connect_ops = GNUNET_malloc (num_slaves *
626 sizeof (struct TestbedConnectOperation));
628 for (c_s = 0; c_s < num_slaves; c_s++)
630 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
631 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
632 p->core_connect_ops[c_s].master = p;
633 p->core_connect_ops[c_s].slave = &sps[c_s];
634 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
635 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
636 sps[c_s].peer, p->peer);
637 if (NULL == p->core_connect_ops[c_s].connect_op)
639 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
640 _("Could not connect master [%u] and slave [%u]\n"), p->no,
643 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
644 GNUNET_SCHEDULER_cancel (shutdown_task);
645 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
653 * Method called whenever a given peer connects.
656 * @param peer peer identity this notification is about
659 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
661 struct BenchmarkPeer *me = cls;
662 struct BenchmarkPeer *remote;
667 remote = find_peer (peer);
674 id = GNUNET_strdup (GNUNET_i2s (&me->id));
675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
676 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
677 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
680 me->core_connections++;
681 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
682 && (GNUNET_NO == state.connected_CORE))
684 me->core_slave_connections++;
686 if (me->core_slave_connections == num_slaves)
688 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
691 completed = GNUNET_YES;
692 for (c = 0; c < num_masters; c++)
694 if (mps[c].core_slave_connections != num_slaves)
695 completed = GNUNET_NO;
697 if (GNUNET_YES == completed)
699 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
700 "All master peers connected all slave peers\n", id,
702 state.connected_CORE = GNUNET_YES;
703 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
710 core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
712 struct BenchmarkPeer *me = cls;
713 struct BenchmarkPeer *remote;
716 remote = find_peer (peer);
723 id = GNUNET_strdup (GNUNET_i2s (&me->id));
724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
726 GNUNET_assert(me->core_connections > 0);
727 me->core_connections--;
729 if ((GNUNET_YES == state.benchmarking)
730 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
732 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
733 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
739 core_send_echo_ready (void *cls, size_t size, void *buf)
741 static char msgbuf[TEST_MESSAGE_SIZE];
742 struct BenchmarkPartner *p = cls;
743 struct GNUNET_MessageHeader *msg;
748 p->bytes_sent += TEST_MESSAGE_SIZE;
749 p->me->total_messages_sent++;
750 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
752 msg = (struct GNUNET_MessageHeader *) &msgbuf;
753 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
754 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
755 msg->size = htons (TEST_MESSAGE_SIZE);
756 memcpy (buf, msg, TEST_MESSAGE_SIZE);
758 return TEST_MESSAGE_SIZE;
762 core_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
763 const struct GNUNET_MessageHeader *message)
766 struct BenchmarkPeer *me = cls;
767 struct BenchmarkPartner *p = NULL;
768 for (c_m = 0; c_m < num_masters; c_m++)
771 == memcmp (other, &me->partners[c_m].dest->id,
772 sizeof(struct GNUNET_PeerIdentity)))
774 p = &me->partners[c_m];
781 return GNUNET_SYSERR;
783 GNUNET_assert(NULL == p->cth);
785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
786 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
789 p->messages_received++;
790 p->bytes_received += TEST_MESSAGE_SIZE;
791 p->me->total_messages_received++;
792 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
794 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
795 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
796 &core_send_echo_ready, p);
801 core_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
802 const struct GNUNET_MessageHeader *message)
805 struct BenchmarkPeer *me = cls;
806 struct BenchmarkPartner *p = NULL;
808 for (c_s = 0; c_s < num_slaves; c_s++)
811 == memcmp (other, &me->partners[c_s].dest->id,
812 sizeof(struct GNUNET_PeerIdentity)))
814 p = &me->partners[c_s];
821 return GNUNET_SYSERR;
823 GNUNET_assert(NULL == p->cth);
825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
826 "Master [%u]: Received PONG from [%u], next message\n", me->no,
829 p->messages_received++;
830 p->bytes_received += TEST_MESSAGE_SIZE;
831 p->me->total_messages_received++;
832 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
834 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
835 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
836 &core_send_ready, p);
842 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
844 struct BenchmarkPeer *me = cls;
846 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
847 &core_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &core_handle_pong,
848 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
850 me->ch = GNUNET_CORE_connect (cfg, me, NULL, core_connect_cb,
851 core_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
853 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
858 core_disconnect_adapter (void *cls, void *op_result)
860 struct BenchmarkPeer *me = cls;
862 GNUNET_CORE_disconnect (me->ch);
867 core_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
868 void *ca_result, const char *emsg)
870 static int core_done = 0;
871 if ((NULL != emsg) || (NULL == ca_result))
873 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
875 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
876 GNUNET_SCHEDULER_cancel (shutdown_task);
877 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
882 if (core_done == num_slaves + num_masters)
884 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all CORE services\n");
885 state.connected_CORE_service = GNUNET_YES;
886 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
891 do_connect_core (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
895 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all CORE services\n");
896 for (c_m = 0; c_m < num_masters; c_m++)
898 mps[c_m].core_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
899 "core", core_connect_completion_cb, NULL, &core_connect_adapter,
900 &core_disconnect_adapter, &mps[c_m]);
903 for (c_s = 0; c_s < num_slaves; c_s++)
905 sps[c_s].core_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
906 "core", core_connect_completion_cb, NULL, &core_connect_adapter,
907 &core_disconnect_adapter, &sps[c_s]);
912 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
913 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
914 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
915 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
917 struct BenchmarkPeer *me = cls;
921 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
922 for (c_a = 0; c_a < ats_count; c_a++)
924 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
925 (GNUNET_YES == p->master) ? 'M' : 'S',
927 GNUNET_i2s (&address->peer),
928 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
929 ntohl(ats[c_a].value));*/
932 if ((GNUNET_YES == me->master)
933 && (0 == memcmp (&address->peer, &me->destination->id,
934 sizeof(struct GNUNET_PeerIdentity))))
936 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
937 me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
938 (long unsigned int ) ntohl (bandwidth_in.value__));
941 store_information (&bp->id, address, address_active, bandwidth_in,
942 bandwidth_out, ats, ats_count);
944 GNUNET_free(peer_id);
948 ats_perf_connect_adapter (void *cls,
949 const struct GNUNET_CONFIGURATION_Handle *cfg)
951 struct BenchmarkPeer *me = cls;
953 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
954 &ats_performance_info_cb, me);
955 if (NULL == me->ats_perf_handle)
956 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
957 "Failed to create ATS performance handle \n");
958 return me->ats_perf_handle;
962 ats_perf_disconnect_adapter (void *cls, void *op_result)
964 struct BenchmarkPeer *me = cls;
966 GNUNET_ATS_performance_done (me->ats_perf_handle);
967 me->ats_perf_handle = NULL;
971 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
972 void *ca_result, const char *emsg)
974 static int op_done = 0;
976 if ((NULL != emsg) || (NULL == ca_result))
978 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
980 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
981 GNUNET_SCHEDULER_cancel (shutdown_task);
982 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
986 if (op_done == (num_masters + num_slaves))
988 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
989 state.connected_ATS_service = GNUNET_YES;
990 GNUNET_SCHEDULER_add_now (&do_connect_core, NULL );
995 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1000 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
1001 for (c_m = 0; c_m < num_masters; c_m++)
1003 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
1004 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1005 &ats_perf_disconnect_adapter, &mps[c_m]);
1009 for (c_s = 0; c_s < num_slaves; c_s++)
1011 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
1012 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
1013 &ats_perf_disconnect_adapter, &sps[c_s]);
1019 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
1020 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
1022 struct BenchmarkPeer *p = cb_cls;
1023 static int done = 0;
1025 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1027 p->id = *pinfo->result.id;
1028 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1029 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1030 GNUNET_i2s (&p->id));
1032 GNUNET_TESTBED_operation_done (op);
1033 p->peer_id_op = NULL;
1036 if (done == num_slaves + num_masters)
1038 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1039 "Retrieved all peer ID, connect to ATS\n");
1040 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1045 * Signature of a main function for a testcase.
1047 * @param cls closure
1048 * @param num_peers number of peers in 'peers'
1049 * @param peers_ handle to peers run in the testbed
1050 * @param links_succeeded the number of overlay link connection attempts that
1052 * @param links_failed the number of overlay link connection attempts that
1056 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1057 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1058 unsigned int links_failed)
1062 GNUNET_assert(NULL == cls);
1063 GNUNET_assert(num_masters + num_slaves == num_peers);
1064 GNUNET_assert(NULL != peers_);
1066 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1067 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1068 solver, pref_str, num_masters, num_slaves);
1070 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1071 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1072 &do_shutdown, NULL );
1074 /* Setup master peers */
1075 for (c_m = 0; c_m < num_masters; c_m++)
1077 GNUNET_assert(NULL != peers_[c_m]);
1078 mps[c_m].peer = peers_[c_m];
1080 mps[c_m].master = GNUNET_YES;
1081 mps[c_m].pref_partner = &sps[c_m];
1082 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1084 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1085 /* Initialize partners */
1086 for (c_s = 0; c_s < num_slaves; c_s++)
1088 mps[c_m].partners[c_s].me = &mps[c_m];
1089 mps[c_m].partners[c_s].dest = &sps[c_s];
1091 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1092 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1095 /* Setup slave peers */
1096 for (c_s = 0; c_s < num_slaves; c_s++)
1098 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1099 sps[c_s].peer = peers_[c_s + num_masters];
1100 sps[c_s].no = c_s + num_masters;
1101 sps[c_s].master = GNUNET_NO;
1103 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1104 /* Initialize partners */
1105 for (c_m = 0; c_m < num_masters; c_m++)
1107 sps[c_s].partners[c_m].me = &sps[c_s];
1108 sps[c_s].partners[c_m].dest = &mps[c_m];
1110 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1111 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1116 main (int argc, char *argv[])
1123 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1128 /* figure out testname */
1129 tmp = strstr (argv[0], TESTNAME_PREFIX);
1132 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1133 return GNUNET_SYSERR;
1135 tmp += strlen (TESTNAME_PREFIX);
1136 solver = GNUNET_strdup (tmp);
1137 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1139 tmp_sep = strchr (solver, '_');
1140 if (NULL == tmp_sep)
1142 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1143 GNUNET_free(solver);
1144 return GNUNET_SYSERR;
1147 pref_str = GNUNET_strdup(tmp_sep + 1);
1149 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1151 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1153 for (c = 0; c <= strlen (pref_str); c++)
1154 pref_str[c] = toupper (pref_str[c]);
1157 if (0 != strcmp (pref_str, "NONE"))
1159 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1161 if (0 == strcmp (pref_str, prefs[c]))
1170 /* abuse terminator to indicate no pref */
1171 pref_val = GNUNET_ATS_PREFERENCE_END;
1175 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1176 GNUNET_free(solver);
1177 GNUNET_free(pref_str);
1181 for (c = 0; c < (argc - 1); c++)
1183 if (0 == strcmp (argv[c], "-s"))
1188 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1189 && (num_slaves >= 1))
1190 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1192 num_slaves = DEFAULT_SLAVES_NUM;
1195 num_slaves = DEFAULT_SLAVES_NUM;
1197 for (c = 0; c < (argc - 1); c++)
1199 if (0 == strcmp (argv[c], "-m"))
1204 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1205 && (num_masters >= 2))
1206 fprintf (stderr, "Starting %u master peers\n", num_masters);
1208 num_masters = DEFAULT_MASTERS_NUM;
1211 num_masters = DEFAULT_MASTERS_NUM;
1213 if (num_slaves < num_masters)
1215 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1216 GNUNET_free(test_name);
1217 GNUNET_free(solver);
1218 GNUNET_free(pref_str);
1219 return GNUNET_SYSERR;
1222 state.connected_ATS_service = GNUNET_NO;
1223 state.connected_CORE_service = GNUNET_NO;
1224 state.connected_PEERS = GNUNET_NO;
1225 state.benchmarking = GNUNET_NO;
1226 state.connected_PEERS = GNUNET_NO;
1228 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1229 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1231 /* Start topology */
1232 uint64_t event_mask;
1234 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1235 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1236 (void) GNUNET_TESTBED_test_run ("perf_ats", conf_name,
1237 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1240 GNUNET_free(solver);
1241 GNUNET_free(pref_str);
1242 GNUNET_free(conf_name);
1243 GNUNET_free(test_name);
1250 /* end of file perf_ats.c */