2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file ats/perf_ats.c
22 * @brief ats benchmark: start peers and modify preferences, monitor change over time
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
34 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_ATS_PREFRENCE_START 1.0
36 #define TEST_ATS_PREFRENCE_DELTA 1.0
38 #define TEST_MESSAGE_TYPE_PING 12345
39 #define TEST_MESSAGE_TYPE_PONG 12346
40 #define TEST_MESSAGE_SIZE 1000
41 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 * Connect peers with testbed
46 struct TestbedConnectOperation
49 * The benchmarking master initiating this connection
51 struct BenchmarkPeer *master;
54 * The benchmarking slave to connect to
56 struct BenchmarkPeer *slave;
59 * Testbed operation to connect peers
61 struct GNUNET_TESTBED_Operation *connect_op;
65 * Overall state of the performance benchmark
69 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
70 int connected_ATS_service;
72 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
73 int connected_COMM_service;
75 /* Are we connected to all peers: GNUNET_YES/NO */
78 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
81 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
88 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
93 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
101 * Test result logging
105 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
107 static int test_core;
117 static char *testname;
122 static char *pref_str;
125 * ATS preference value
130 * Number master peers
132 static int num_masters;
135 * Array of master peers
137 static struct BenchmarkPeer *mps;
142 static int num_slaves;
144 * Array of slave peers
146 static struct BenchmarkPeer *sps;
151 static struct GNUNET_TIME_Relative perf_duration;
156 static struct GNUNET_TIME_Relative log_frequency;
161 static struct BenchmarkState state;
168 unsigned int duration;
169 struct BenchmarkPeer *mp;
171 duration = (perf_duration.rel_value_us / (1000 * 1000));
172 for (c_m = 0; c_m < num_masters; c_m++)
176 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
177 mp->no, mp->total_bytes_sent / 1024, duration,
178 (mp->total_bytes_sent / 1024) / duration,
179 mp->total_bytes_received / 1024, duration,
180 (mp->total_bytes_received / 1024) / duration);
182 for (c_s = 0; c_s < num_slaves; c_s++)
185 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
186 (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
187 mp->no, mp->partners[c_s].dest->no,
188 (mp->partners[c_s].bytes_sent / 1024) / duration,
189 ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
190 (mp->partners[c_s].bytes_received / 1024) / duration,
191 ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
200 * @param tc the task context
203 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 if (GNUNET_YES == logging)
212 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
213 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
215 fprintf (stderr, "0\n");
216 GNUNET_SCHEDULER_cancel (progress_task);
218 progress_task = GNUNET_SCHEDULER_NO_TASK;
221 state.benchmarking = GNUNET_NO;
222 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
224 for (c_m = 0; c_m < num_masters; c_m++)
226 if (NULL != mps[c_m].peer_id_op)
228 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
229 mps[c_m].peer_id_op = NULL;
232 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
233 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
234 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
236 for (c_op = 0; c_op < num_slaves; c_op++)
239 if (NULL != mps[c_m].partners[c_op].cth)
241 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
242 mps[c_m].partners[c_op].cth = NULL;
244 if (NULL != mps[c_m].partners[c_op].tth)
246 GNUNET_TRANSPORT_notify_transmit_ready_cancel (mps[c_m].partners[c_op].tth);
247 mps[c_m].partners[c_op].tth = NULL;
251 if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
253 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
254 _("Failed to connect peer 0 and %u\n"), c_op);
255 GNUNET_TESTBED_operation_done (
256 mps[c_m].core_connect_ops[c_op].connect_op);
257 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
262 if (NULL != mps[c_m].ats_perf_op)
264 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
265 mps[c_m].ats_perf_op = NULL;
268 if (NULL != mps[c_m].comm_op)
270 GNUNET_TESTBED_operation_done (mps[c_m].comm_op);
271 mps[c_m].comm_op = NULL;
273 GNUNET_free(mps[c_m].core_connect_ops);
274 GNUNET_free(mps[c_m].partners);
275 mps[c_m].partners = NULL;
278 for (c_s = 0; c_s < num_slaves; c_s++)
280 if (NULL != sps[c_s].peer_id_op)
282 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
283 sps[c_s].peer_id_op = NULL;
286 for (c_op = 0; c_op < num_slaves; c_op++)
288 if (NULL != sps[c_s].partners[c_op].cth)
290 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
291 sps[c_s].partners[c_op].cth = NULL;
294 if (NULL != sps[c_s].partners[c_op].tth)
296 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sps[c_s].partners[c_op].tth);
297 sps[c_s].partners[c_op].tth = NULL;
301 if (NULL != sps[c_s].ats_perf_op)
303 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
304 sps[c_s].ats_perf_op = NULL;
306 if (NULL != sps[c_s].comm_op)
308 GNUNET_TESTBED_operation_done (sps[c_s].comm_op);
309 sps[c_s].comm_op = NULL;
312 GNUNET_free(sps[c_s].partners);
313 sps[c_s].partners = NULL;
316 GNUNET_SCHEDULER_shutdown ();
319 static struct BenchmarkPeer *
320 find_peer (const struct GNUNET_PeerIdentity * peer)
324 for (c_p = 0; c_p < num_masters; c_p++)
326 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
330 for (c_p = 0; c_p < num_slaves; c_p++)
332 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
339 * Controller event callback
342 * @param event the controller event
345 controller_event_cb (void *cls,
346 const struct GNUNET_TESTBED_EventInformation *event)
348 //struct BenchmarkPeer *p = cls;
351 case GNUNET_TESTBED_ET_CONNECT:
353 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
358 GNUNET_SCHEDULER_cancel (shutdown_task);
359 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
364 comm_send_ready (void *cls, size_t size, void *buf)
366 static char msgbuf[TEST_MESSAGE_SIZE];
367 struct BenchmarkPartner *p = cls;
368 struct GNUNET_MessageHeader *msg;
370 if (GNUNET_YES == test_core)
375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
376 p->me->no, p->dest->no);
379 p->bytes_sent += TEST_MESSAGE_SIZE;
380 p->me->total_messages_sent++;
381 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
383 msg = (struct GNUNET_MessageHeader *) &msgbuf;
384 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
385 msg->type = htons (TEST_MESSAGE_TYPE_PING);
386 msg->size = htons (TEST_MESSAGE_SIZE);
387 memcpy (buf, msg, TEST_MESSAGE_SIZE);
388 return TEST_MESSAGE_SIZE;
395 progress_task = GNUNET_SCHEDULER_NO_TASK;
397 fprintf (stderr, "%llu..",
398 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
401 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
402 &print_progress, NULL );
406 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct BenchmarkPeer *me = cls;
410 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
413 me->no, me->pref_partner->no, me->pref_value);
414 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
415 &me->pref_partner->id,
416 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
417 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
418 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
419 &ats_pref_task, cls);
428 if ((state.connected_ATS_service == GNUNET_NO)
429 || (state.connected_COMM_service == GNUNET_NO)
430 || (state.connected_PEERS == GNUNET_NO)
431 || (state.connected_CORE == GNUNET_NO))
434 state.benchmarking = GNUNET_YES;
435 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
437 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
438 GNUNET_SCHEDULER_cancel (shutdown_task);
439 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
440 &do_shutdown, NULL );
442 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
444 /* Start sending test messages */
445 for (c_m = 0; c_m < num_masters; c_m++)
447 for (c_s = 0; c_s < num_slaves; c_s++)
449 if (GNUNET_YES == test_core)
450 mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
451 mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
452 TEST_MESSAGE_SIZE, &comm_send_ready, &mps[c_m].partners[c_s]);
455 mps[c_m].partners[c_s].tth = GNUNET_TRANSPORT_notify_transmit_ready (
456 mps[c_m].th, &sps[c_s].id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
457 &comm_send_ready, &mps[c_m].partners[c_s]);
460 if (pref_val != GNUNET_ATS_PREFERENCE_END)
461 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
463 if (GNUNET_YES == logging)
464 perf_logging_start (log_frequency, testname, mps, num_masters);
469 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
472 struct TestbedConnectOperation *cop = cls;
477 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
478 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
483 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
484 _("Failed to connect master peer [%u] with slave [%u]\n"),
485 cop->master->no, cop->slave->no);
487 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
488 GNUNET_SCHEDULER_cancel (shutdown_task);
489 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
491 GNUNET_TESTBED_operation_done (op);
493 for (c = 0; c < num_slaves; c++)
495 if (cop == &cop->master->core_connect_ops[c])
496 cop->master->core_connect_ops[c].connect_op = NULL;
498 if (ops == num_masters * num_slaves)
500 state.connected_PEERS = GNUNET_YES;
501 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
506 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
510 struct BenchmarkPeer *p;
512 if ((state.connected_ATS_service == GNUNET_NO)
513 || (state.connected_COMM_service == GNUNET_NO))
516 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
518 for (c_m = 0; c_m < num_masters; c_m++)
521 p->core_connect_ops = GNUNET_malloc (num_slaves *
522 sizeof (struct TestbedConnectOperation));
524 for (c_s = 0; c_s < num_slaves; c_s++)
526 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
527 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
528 p->core_connect_ops[c_s].master = p;
529 p->core_connect_ops[c_s].slave = &sps[c_s];
530 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
531 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
532 sps[c_s].peer, p->peer);
533 if (NULL == p->core_connect_ops[c_s].connect_op)
535 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
536 _("Could not connect master [%u] and slave [%u]\n"), p->no,
539 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
540 GNUNET_SCHEDULER_cancel (shutdown_task);
541 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
549 * Method called whenever a given peer connects.
552 * @param peer peer identity this notification is about
555 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
557 struct BenchmarkPeer *me = cls;
558 struct BenchmarkPeer *remote;
563 remote = find_peer (peer);
570 id = GNUNET_strdup (GNUNET_i2s (&me->id));
571 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
572 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
573 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
576 me->core_connections++;
577 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
578 && (GNUNET_NO == state.connected_CORE))
580 me->core_slave_connections++;
582 if (me->core_slave_connections == num_slaves)
584 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
587 completed = GNUNET_YES;
588 for (c = 0; c < num_masters; c++)
590 if (mps[c].core_slave_connections != num_slaves)
591 completed = GNUNET_NO;
593 if (GNUNET_YES == completed)
595 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
596 "All master peers connected all slave peers\n", id,
598 state.connected_CORE = GNUNET_YES;
599 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
606 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
608 struct BenchmarkPeer *me = cls;
609 struct BenchmarkPeer *remote;
612 remote = find_peer (peer);
619 id = GNUNET_strdup (GNUNET_i2s (&me->id));
620 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
622 GNUNET_assert(me->core_connections > 0);
623 me->core_connections--;
625 if ((GNUNET_YES == state.benchmarking)
626 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
628 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
629 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
635 comm_send_pong_ready (void *cls, size_t size, void *buf)
637 static char msgbuf[TEST_MESSAGE_SIZE];
638 struct BenchmarkPartner *p = cls;
639 struct GNUNET_MessageHeader *msg;
641 if (GNUNET_YES == test_core)
647 p->bytes_sent += TEST_MESSAGE_SIZE;
648 p->me->total_messages_sent++;
649 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
651 msg = (struct GNUNET_MessageHeader *) &msgbuf;
652 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
653 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
654 msg->size = htons (TEST_MESSAGE_SIZE);
655 memcpy (buf, msg, TEST_MESSAGE_SIZE);
657 return TEST_MESSAGE_SIZE;
661 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
662 const struct GNUNET_MessageHeader *message)
665 struct BenchmarkPeer *me = cls;
666 struct BenchmarkPartner *p = NULL;
667 for (c_m = 0; c_m < num_masters; c_m++)
669 /* Find a partner with other as destination */
670 if (0 == memcmp (other, &me->partners[c_m].dest->id,
671 sizeof(struct GNUNET_PeerIdentity)))
673 p = &me->partners[c_m];
680 return GNUNET_SYSERR;
683 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
684 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
687 p->messages_received++;
688 p->bytes_received += TEST_MESSAGE_SIZE;
689 p->me->total_messages_received++;
690 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
692 if (GNUNET_YES == test_core)
694 GNUNET_assert (NULL == p->cth);
695 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
696 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
697 &comm_send_pong_ready, p);
701 GNUNET_assert (NULL == p->tth);
702 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
703 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
710 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
711 const struct GNUNET_MessageHeader *message)
714 struct BenchmarkPeer *me = cls;
715 struct BenchmarkPartner *p = NULL;
717 for (c_s = 0; c_s < num_slaves; c_s++)
720 == memcmp (other, &me->partners[c_s].dest->id,
721 sizeof(struct GNUNET_PeerIdentity)))
723 p = &me->partners[c_s];
730 return GNUNET_SYSERR;
733 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
734 "Master [%u]: Received PONG from [%u], next message\n", me->no,
737 p->messages_received++;
738 p->bytes_received += TEST_MESSAGE_SIZE;
739 p->me->total_messages_received++;
740 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
742 if (GNUNET_YES == test_core)
744 GNUNET_assert (NULL == p->cth);
745 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
746 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
747 &comm_send_ready, p);
751 GNUNET_assert (NULL == p->tth);
752 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
753 me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
754 &comm_send_ready, p);
761 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
763 struct BenchmarkPeer *me = cls;
765 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
766 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
767 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
769 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
770 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
772 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
777 core_disconnect_adapter (void *cls, void *op_result)
779 struct BenchmarkPeer *me = cls;
781 GNUNET_CORE_disconnect (me->ch);
786 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
787 void *ca_result, const char *emsg)
789 static int comm_done = 0;
790 if ((NULL != emsg) || (NULL == ca_result))
792 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
794 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
795 GNUNET_SCHEDULER_cancel (shutdown_task);
796 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
801 if (comm_done == num_slaves + num_masters)
803 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
804 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
805 state.connected_COMM_service = GNUNET_YES;
806 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
811 transport_recv_cb (void *cls,
812 const struct GNUNET_PeerIdentity * peer,
813 const struct GNUNET_MessageHeader * message)
815 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
816 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
817 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
821 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
822 comm_handle_ping (cls, peer, message);
824 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
825 comm_handle_pong (cls, peer, message);
830 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
832 struct BenchmarkPeer *me = cls;
834 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
835 &comm_connect_cb, &comm_disconnect_cb);
837 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
842 transport_disconnect_adapter (void *cls, void *op_result)
844 struct BenchmarkPeer *me = cls;
846 GNUNET_TRANSPORT_disconnect (me->th);
851 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
856 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
857 for (c_m = 0; c_m < num_masters; c_m++)
859 if (GNUNET_YES == test_core)
860 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
861 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
862 &core_disconnect_adapter, &mps[c_m]);
865 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
866 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
867 &transport_disconnect_adapter, &mps[c_m]);
871 for (c_s = 0; c_s < num_slaves; c_s++)
873 if (GNUNET_YES == test_core)
874 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
875 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
876 &core_disconnect_adapter, &sps[c_s]);
879 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
880 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
881 &transport_disconnect_adapter, &sps[c_s]);
887 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
888 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
889 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
890 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
892 struct BenchmarkPeer *me = cls;
896 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
897 for (c_a = 0; c_a < ats_count; c_a++)
899 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
900 (GNUNET_YES == p->master) ? 'M' : 'S',
902 GNUNET_i2s (&address->peer),
903 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
904 ntohl(ats[c_a].value));*/
907 if ((GNUNET_YES == me->master)
908 && (0 == memcmp (&address->peer, &me->destination->id,
909 sizeof(struct GNUNET_PeerIdentity))))
911 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
912 me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
913 (long unsigned int ) ntohl (bandwidth_in.value__));
916 store_information (&bp->id, address, address_active, bandwidth_in,
917 bandwidth_out, ats, ats_count);
919 GNUNET_free(peer_id);
923 ats_perf_connect_adapter (void *cls,
924 const struct GNUNET_CONFIGURATION_Handle *cfg)
926 struct BenchmarkPeer *me = cls;
928 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
929 &ats_performance_info_cb, me);
930 if (NULL == me->ats_perf_handle)
931 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
932 "Failed to create ATS performance handle \n");
933 return me->ats_perf_handle;
937 ats_perf_disconnect_adapter (void *cls, void *op_result)
939 struct BenchmarkPeer *me = cls;
941 GNUNET_ATS_performance_done (me->ats_perf_handle);
942 me->ats_perf_handle = NULL;
946 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
947 void *ca_result, const char *emsg)
949 static int op_done = 0;
951 if ((NULL != emsg) || (NULL == ca_result))
953 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
955 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
956 GNUNET_SCHEDULER_cancel (shutdown_task);
957 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
961 if (op_done == (num_masters + num_slaves))
963 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
964 state.connected_ATS_service = GNUNET_YES;
965 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
970 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
975 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
976 for (c_m = 0; c_m < num_masters; c_m++)
978 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
979 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
980 &ats_perf_disconnect_adapter, &mps[c_m]);
984 for (c_s = 0; c_s < num_slaves; c_s++)
986 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
987 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
988 &ats_perf_disconnect_adapter, &sps[c_s]);
994 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
995 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
997 struct BenchmarkPeer *p = cb_cls;
1000 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
1002 p->id = *pinfo->result.id;
1003 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
1004 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1005 GNUNET_i2s (&p->id));
1007 GNUNET_TESTBED_operation_done (op);
1008 p->peer_id_op = NULL;
1011 if (done == num_slaves + num_masters)
1013 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1014 "Retrieved all peer ID, connect to ATS\n");
1015 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1020 * Signature of a main function for a testcase.
1022 * @param cls closure
1023 * @param num_peers number of peers in 'peers'
1024 * @param peers_ handle to peers run in the testbed
1025 * @param links_succeeded the number of overlay link connection attempts that
1027 * @param links_failed the number of overlay link connection attempts that
1031 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1032 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1033 unsigned int links_failed)
1037 GNUNET_assert(NULL == cls);
1038 GNUNET_assert(num_masters + num_slaves == num_peers);
1039 GNUNET_assert(NULL != peers_);
1041 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1042 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1043 solver, pref_str, num_masters, num_slaves);
1045 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1046 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1047 &do_shutdown, NULL );
1049 /* Setup master peers */
1050 for (c_m = 0; c_m < num_masters; c_m++)
1052 GNUNET_assert(NULL != peers_[c_m]);
1053 mps[c_m].peer = peers_[c_m];
1055 mps[c_m].master = GNUNET_YES;
1056 mps[c_m].pref_partner = &sps[c_m];
1057 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1059 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1060 mps[c_m].num_partners = num_slaves;
1061 /* Initialize partners */
1062 for (c_s = 0; c_s < num_slaves; c_s++)
1064 mps[c_m].partners[c_s].me = &mps[c_m];
1065 mps[c_m].partners[c_s].dest = &sps[c_s];
1067 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1068 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1071 /* Setup slave peers */
1072 for (c_s = 0; c_s < num_slaves; c_s++)
1074 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1075 sps[c_s].peer = peers_[c_s + num_masters];
1076 sps[c_s].no = c_s + num_masters;
1077 sps[c_s].master = GNUNET_NO;
1079 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1080 sps[c_s].num_partners = num_masters;
1081 /* Initialize partners */
1082 for (c_m = 0; c_m < num_masters; c_m++)
1084 sps[c_s].partners[c_m].me = &sps[c_s];
1085 sps[c_s].partners[c_m].dest = &mps[c_m];
1087 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1088 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1093 main (int argc, char *argv[])
1101 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1106 /* figure out testname */
1107 tmp = strstr (argv[0], TESTNAME_PREFIX);
1110 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1111 return GNUNET_SYSERR;
1113 tmp += strlen (TESTNAME_PREFIX);
1114 solver = GNUNET_strdup (tmp);
1115 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1117 tmp_sep = strchr (solver, '_');
1118 if (NULL == tmp_sep)
1120 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1121 GNUNET_free(solver);
1122 return GNUNET_SYSERR;
1125 comm_name = GNUNET_strdup (&tmp_sep[1]);
1126 tmp_sep = strchr (comm_name, '_');
1127 if (NULL == tmp_sep)
1129 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1130 GNUNET_free(solver);
1131 return GNUNET_SYSERR;
1134 for (c = 0; c <= strlen (comm_name); c++)
1135 comm_name[c] = toupper (comm_name[c]);
1136 if (0 == strcmp (comm_name, "CORE"))
1137 test_core = GNUNET_YES;
1138 else if (0 == strcmp (comm_name, "TRANSPORT"))
1139 test_core = GNUNET_NO;
1142 GNUNET_free (comm_name);
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);
1178 GNUNET_free (comm_name);
1182 for (c = 0; c < (argc - 1); c++)
1184 if (0 == strcmp (argv[c], "-d"))
1189 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1190 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1194 perf_duration = BENCHMARK_DURATION;
1196 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1198 for (c = 0; c < (argc - 1); c++)
1200 if (0 == strcmp (argv[c], "-s"))
1205 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1206 && (num_slaves >= 1))
1207 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1209 num_slaves = DEFAULT_SLAVES_NUM;
1212 num_slaves = DEFAULT_SLAVES_NUM;
1214 for (c = 0; c < (argc - 1); c++)
1216 if (0 == strcmp (argv[c], "-m"))
1221 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1222 && (num_masters >= 2))
1223 fprintf (stderr, "Starting %u master peers\n", num_masters);
1225 num_masters = DEFAULT_MASTERS_NUM;
1228 num_masters = DEFAULT_MASTERS_NUM;
1230 logging = GNUNET_NO;
1231 for (c = 0; c < argc; c++)
1233 if (0 == strcmp (argv[c], "-l"))
1234 logging = GNUNET_YES;
1237 if (GNUNET_YES == logging)
1239 for (c = 0; c < (argc - 1); c++)
1241 if (0 == strcmp (argv[c], "-f"))
1246 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
1247 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1251 log_frequency = LOGGING_FREQUENCY;
1253 fprintf (stderr, "Using log frequency %llu ms\n",
1254 (unsigned long long) (log_frequency.rel_value_us) / (1000));
1257 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1259 if (num_slaves < num_masters)
1261 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1262 GNUNET_free(test_name);
1263 GNUNET_free(solver);
1264 GNUNET_free(pref_str);
1265 GNUNET_free (comm_name);
1266 return GNUNET_SYSERR;
1269 state.connected_ATS_service = GNUNET_NO;
1270 state.connected_COMM_service = GNUNET_NO;
1271 state.connected_PEERS = GNUNET_NO;
1272 state.benchmarking = GNUNET_NO;
1273 state.connected_PEERS = GNUNET_NO;
1275 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1276 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1278 /* Start topology */
1279 uint64_t event_mask;
1281 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1282 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1283 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1284 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1287 GNUNET_free(solver);
1288 GNUNET_free(pref_str);
1289 GNUNET_free(conf_name);
1290 GNUNET_free(test_name);
1291 GNUNET_free(testname);
1292 GNUNET_free (comm_name);
1299 /* end of file perf_ats.c */