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 "ats-testing.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)
43 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
44 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
45 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
46 #define TESTNAME_PREFIX "perf_ats_"
47 #define DEFAULT_SLAVES_NUM 2
48 #define DEFAULT_MASTERS_NUM 1
52 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
57 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
69 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
81 static char *testname;
86 static char *pref_str;
89 * ATS preference value
96 static struct GNUNET_TIME_Relative perf_duration;
101 static struct GNUNET_TIME_Relative log_frequency;
104 * Number master peers
106 static unsigned int num_masters;
109 * Array of master peers
111 static struct BenchmarkPeer *mps;
116 static unsigned int num_slaves;
119 * Array of master peers
121 static struct BenchmarkPeer *sps;
128 unsigned int duration;
129 struct BenchmarkPeer *mp;
130 struct BenchmarkPartner *p;
132 unsigned int kb_sent_sec;
133 double kb_sent_percent;
134 unsigned int kb_recv_sec;
135 double kb_recv_percent;
138 duration = (perf_duration.rel_value_us / (1000 * 1000));
139 for (c_m = 0; c_m < num_masters; c_m++)
143 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
144 mp->no, mp->total_bytes_sent / 1024, duration,
145 (mp->total_bytes_sent / 1024) / duration,
146 mp->total_bytes_received / 1024, duration,
147 (mp->total_bytes_received / 1024) / duration);
149 for (c_s = 0; c_s < num_slaves; c_s++)
151 p = &mp->partners[c_s];
155 kb_sent_percent = 0.0;
156 kb_recv_percent = 0.0;
161 kb_sent_percent = (p->bytes_sent / 1024) / duration;
162 kb_recv_percent = (p->bytes_received / 1024) / duration;
164 if (mp->total_bytes_sent > 0)
165 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
166 if (mp->total_bytes_received > 0)
167 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
168 if (1000 * p->messages_sent > 0)
169 rtt = p->total_app_rtt / (1000 * p->messages_sent);
171 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
172 (mp->pref_partner == p->dest) ? '*' : ' ',
174 kb_sent_sec, kb_sent_percent,
175 kb_recv_sec, kb_recv_percent);
178 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
179 (mp->pref_partner == p->dest) ? '*' : ' ',
180 mp->no, p->dest->no, rtt);
189 * @param tc the task context
192 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 if (GNUNET_YES == logging)
196 GNUNET_ATS_TEST_logging_stop();
198 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
199 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
201 fprintf (stderr, "0\n");
202 GNUNET_SCHEDULER_cancel (progress_task);
204 progress_task = GNUNET_SCHEDULER_NO_TASK;
207 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
209 GNUNET_ATS_TEST_shutdown_topology();
213 comm_send_ready (void *cls, size_t size, void *buf)
215 static char msgbuf[TEST_MESSAGE_SIZE];
216 struct BenchmarkPartner *p = cls;
217 struct GNUNET_MessageHeader *msg;
219 if (GNUNET_YES == test_core)
229 if (size < TEST_MESSAGE_SIZE)
235 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Master [%u]: Sending PING to [%u]\n",
236 p->me->no, p->dest->no);
239 p->bytes_sent += TEST_MESSAGE_SIZE;
240 p->me->total_messages_sent++;
241 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
243 msg = (struct GNUNET_MessageHeader *) &msgbuf;
244 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
245 msg->type = htons (TEST_MESSAGE_TYPE_PING);
246 msg->size = htons (TEST_MESSAGE_SIZE);
247 memcpy (buf, msg, TEST_MESSAGE_SIZE);
248 return TEST_MESSAGE_SIZE;
252 comm_schedule_send (struct BenchmarkPartner *p)
254 p->last_message_sent = GNUNET_TIME_absolute_get();
255 if (GNUNET_YES == test_core)
257 p->cth = GNUNET_CORE_notify_transmit_ready (
258 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
259 TEST_MESSAGE_SIZE, &comm_send_ready, p);
263 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
264 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
265 &comm_send_ready, p);
274 progress_task = GNUNET_SCHEDULER_NO_TASK;
276 fprintf (stderr, "%llu..",
277 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
280 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
281 &print_progress, NULL );
285 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287 struct BenchmarkPeer *me = cls;
289 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
292 me->no, me->pref_partner->no, me->pref_value);
293 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
294 &me->pref_partner->id,
295 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
296 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
297 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
298 &ats_pref_task, cls);
302 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
310 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
312 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
313 GNUNET_SCHEDULER_cancel (shutdown_task);
314 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
315 &do_shutdown, NULL );
317 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
319 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Topology connected, start benchmarking...\n");
321 /* Start sending test messages */
322 for (c_m = 0; c_m < num_masters; c_m++)
324 for (c_s = 0; c_s < num_slaves; c_s++)
325 comm_schedule_send (&masters[c_m].partners[c_s]);
326 if (pref_val != GNUNET_ATS_PREFERENCE_END)
327 masters[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &masters[c_m]);
330 if (GNUNET_YES == logging)
331 GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps, num_masters);
336 comm_send_pong_ready (void *cls, size_t size, void *buf)
338 static char msgbuf[TEST_MESSAGE_SIZE];
339 struct BenchmarkPartner *p = cls;
340 struct GNUNET_MessageHeader *msg;
342 if (GNUNET_YES == test_core)
348 p->bytes_sent += TEST_MESSAGE_SIZE;
349 p->me->total_messages_sent++;
350 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
352 msg = (struct GNUNET_MessageHeader *) &msgbuf;
353 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
354 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
355 msg->size = htons (TEST_MESSAGE_SIZE);
356 memcpy (buf, msg, TEST_MESSAGE_SIZE);
358 return TEST_MESSAGE_SIZE;
361 static struct BenchmarkPartner *
362 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
365 GNUNET_assert (NULL != me);
366 GNUNET_assert (NULL != peer);
368 for (c_m = 0; c_m < me->num_partners; c_m++)
370 /* Find a partner with other as destination */
371 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
372 sizeof(struct GNUNET_PeerIdentity)))
374 return &me->partners[c_m];
381 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
382 const struct GNUNET_MessageHeader *message)
385 struct BenchmarkPeer *me = cls;
386 struct BenchmarkPartner *p = NULL;
388 if (NULL == (p = find_partner(me, other)))
391 return GNUNET_SYSERR;
394 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
395 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
398 p->messages_received++;
399 p->bytes_received += TEST_MESSAGE_SIZE;
400 p->me->total_messages_received++;
401 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
403 if (GNUNET_YES == test_core)
405 GNUNET_assert (NULL == p->cth);
406 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
407 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
408 &comm_send_pong_ready, p);
412 GNUNET_assert (NULL == p->tth);
413 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
414 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
421 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
422 const struct GNUNET_MessageHeader *message)
424 struct BenchmarkPeer *me = cls;
425 struct BenchmarkPartner *p = NULL;
427 if (NULL == (p = find_partner (me, other)))
430 return GNUNET_SYSERR;
433 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
434 "Master [%u]: Received PONG from [%u], next message\n", me->no,
437 p->messages_received++;
438 p->bytes_received += TEST_MESSAGE_SIZE;
439 p->me->total_messages_received++;
440 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
441 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
442 GNUNET_TIME_absolute_get()).rel_value_us;
444 comm_schedule_send (p);
450 test_recv_cb (void *cls,
451 const struct GNUNET_PeerIdentity * peer,
452 const struct GNUNET_MessageHeader * message)
454 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
455 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
456 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
460 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
461 comm_handle_ping (cls, peer, message);
463 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
464 comm_handle_pong (cls, peer, message);
469 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
470 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
471 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
472 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
474 struct BenchmarkPeer *me = cls;
475 struct BenchmarkPartner *p;
480 p = find_partner (me, &address->peer);
483 /* This is not one of my partners
484 * Will happen since the peers will connect to each other due to gossiping
488 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
491 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
492 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
494 p->bandwidth_in = ntohl (bandwidth_in.value__);
495 p->bandwidth_out = ntohl (bandwidth_out.value__);
497 for (c_a = 0; c_a < ats_count; c_a++)
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
500 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
502 GNUNET_i2s (&p->dest->id),
503 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
504 ntohl(ats[c_a].value));
505 switch (ntohl (ats[c_a].type ))
507 case GNUNET_ATS_ARRAY_TERMINATOR:
509 case GNUNET_ATS_UTILIZATION_OUT:
510 if (p->ats_utilization_up != ntohl (ats[c_a].value))
512 p->ats_utilization_up = ntohl (ats[c_a].value);
515 case GNUNET_ATS_UTILIZATION_IN:
516 if (p->ats_utilization_down != ntohl (ats[c_a].value))
518 p->ats_utilization_down = ntohl (ats[c_a].value);
520 case GNUNET_ATS_NETWORK_TYPE:
521 if (p->ats_network_type != ntohl (ats[c_a].value))
523 p->ats_network_type = ntohl (ats[c_a].value);
525 case GNUNET_ATS_QUALITY_NET_DELAY:
526 if (p->ats_delay != ntohl (ats[c_a].value))
528 p->ats_delay = ntohl (ats[c_a].value);
530 case GNUNET_ATS_QUALITY_NET_DISTANCE:
531 if (p->ats_distance != ntohl (ats[c_a].value))
533 p->ats_distance = ntohl (ats[c_a].value);
536 case GNUNET_ATS_COST_WAN:
537 if (p->ats_cost_wan != ntohl (ats[c_a].value))
539 p->ats_cost_wan = ntohl (ats[c_a].value);
541 case GNUNET_ATS_COST_LAN:
542 if (p->ats_cost_lan != ntohl (ats[c_a].value))
544 p->ats_cost_lan = ntohl (ats[c_a].value);
546 case GNUNET_ATS_COST_WLAN:
547 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
549 p->ats_cost_wlan = ntohl (ats[c_a].value);
556 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
557 GNUNET_ATS_TEST_logging_now();
559 GNUNET_free(peer_id);
564 * Start the performance test case
567 main (int argc, char *argv[])
575 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
580 /* figure out testname */
581 tmp = strstr (argv[0], TESTNAME_PREFIX);
584 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
585 return GNUNET_SYSERR;
587 tmp += strlen (TESTNAME_PREFIX);
588 solver = GNUNET_strdup (tmp);
589 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
591 tmp_sep = strchr (solver, '_');
594 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
596 return GNUNET_SYSERR;
599 comm_name = GNUNET_strdup (&tmp_sep[1]);
600 tmp_sep = strchr (comm_name, '_');
603 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
605 return GNUNET_SYSERR;
608 for (c = 0; c <= strlen (comm_name); c++)
609 comm_name[c] = toupper (comm_name[c]);
610 if (0 == strcmp (comm_name, "CORE"))
611 test_core = GNUNET_YES;
612 else if (0 == strcmp (comm_name, "TRANSPORT"))
613 test_core = GNUNET_NO;
616 GNUNET_free (comm_name);
617 GNUNET_free (solver);
618 return GNUNET_SYSERR;
621 pref_str = GNUNET_strdup(tmp_sep + 1);
623 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
625 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
627 for (c = 0; c <= strlen (pref_str); c++)
628 pref_str[c] = toupper (pref_str[c]);
631 if (0 != strcmp (pref_str, "NONE"))
633 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
635 if (0 == strcmp (pref_str, prefs[c]))
644 /* abuse terminator to indicate no pref */
645 pref_val = GNUNET_ATS_PREFERENCE_END;
649 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
651 GNUNET_free(pref_str);
652 GNUNET_free (comm_name);
656 for (c = 0; c < (argc - 1); c++)
658 if (0 == strcmp (argv[c], "-d"))
663 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
664 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
668 perf_duration = BENCHMARK_DURATION;
670 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
672 for (c = 0; c < (argc - 1); c++)
674 if (0 == strcmp (argv[c], "-s"))
679 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
680 && (num_slaves >= 1))
681 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
683 num_slaves = DEFAULT_SLAVES_NUM;
686 num_slaves = DEFAULT_SLAVES_NUM;
688 for (c = 0; c < (argc - 1); c++)
690 if (0 == strcmp (argv[c], "-m"))
695 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
696 && (num_masters >= 2))
697 fprintf (stderr, "Starting %u master peers\n", num_masters);
699 num_masters = DEFAULT_MASTERS_NUM;
702 num_masters = DEFAULT_MASTERS_NUM;
705 for (c = 0; c < argc; c++)
707 if (0 == strcmp (argv[c], "-l"))
708 logging = GNUNET_YES;
711 if (GNUNET_YES == logging)
713 for (c = 0; c < (argc - 1); c++)
715 if (0 == strcmp (argv[c], "-f"))
720 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
721 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
725 log_frequency = LOGGING_FREQUENCY;
727 fprintf (stderr, "Using log frequency %llu ms\n",
728 (unsigned long long) (log_frequency.rel_value_us) / (1000));
731 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
733 if (num_slaves < num_masters)
735 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
736 GNUNET_free(test_name);
738 GNUNET_free(pref_str);
739 GNUNET_free (comm_name);
740 return GNUNET_SYSERR;
744 * Core message handler to use for PING/PONG messages
746 static struct GNUNET_CORE_MessageHandler handlers[] = {
747 {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
748 {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
754 GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
755 num_slaves, num_masters,
760 &ats_performance_info_cb);
765 /* end of file perf_ats.c */