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];
153 fprintf (stderr , "%u %u %u\n", p->bytes_sent, (p->bytes_sent / 1024) / duration, duration);
154 fprintf (stderr , "%u %u %u \n", p->bytes_received, (p->bytes_sent / 1024) / duration, duration);
158 kb_sent_percent = 0.0;
159 kb_recv_percent = 0.0;
164 kb_sent_sec = (p->bytes_sent / 1024) / duration;
165 kb_recv_sec = (p->bytes_received / 1024) / duration;
168 if (mp->total_bytes_sent > 0)
169 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
170 if (mp->total_bytes_received > 0)
171 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
172 if (1000 * p->messages_sent > 0)
173 rtt = p->total_app_rtt / (1000 * p->messages_sent);
178 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
179 (mp->pref_partner == p->dest) ? '*' : ' ',
181 kb_sent_sec, kb_sent_percent,
182 kb_recv_sec, kb_recv_percent);
185 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
186 (mp->pref_partner == p->dest) ? '*' : ' ',
187 mp->no, p->dest->no, rtt);
196 * @param tc the task context
199 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 if (GNUNET_YES == logging)
203 GNUNET_ATS_TEST_logging_stop();
205 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
206 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
208 fprintf (stderr, "0\n");
209 GNUNET_SCHEDULER_cancel (progress_task);
211 progress_task = GNUNET_SCHEDULER_NO_TASK;
214 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
216 GNUNET_ATS_TEST_shutdown_topology();
220 comm_send_ready (void *cls, size_t size, void *buf)
222 static char msgbuf[TEST_MESSAGE_SIZE];
223 struct BenchmarkPartner *p = cls;
224 struct GNUNET_MessageHeader *msg;
226 if (GNUNET_YES == test_core)
236 if (size < TEST_MESSAGE_SIZE)
242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
243 p->me->no, p->dest->no);
246 p->bytes_sent += TEST_MESSAGE_SIZE;
247 p->me->total_messages_sent++;
248 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
250 msg = (struct GNUNET_MessageHeader *) &msgbuf;
251 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
252 msg->type = htons (TEST_MESSAGE_TYPE_PING);
253 msg->size = htons (TEST_MESSAGE_SIZE);
254 memcpy (buf, msg, TEST_MESSAGE_SIZE);
255 return TEST_MESSAGE_SIZE;
259 comm_schedule_send (struct BenchmarkPartner *p)
261 p->last_message_sent = GNUNET_TIME_absolute_get();
262 if (GNUNET_YES == test_core)
264 p->cth = GNUNET_CORE_notify_transmit_ready (
265 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
266 TEST_MESSAGE_SIZE, &comm_send_ready, p);
270 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
271 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
272 &comm_send_ready, p);
281 progress_task = GNUNET_SCHEDULER_NO_TASK;
283 fprintf (stderr, "%llu..",
284 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
287 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
288 &print_progress, NULL );
292 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
294 struct BenchmarkPeer *me = cls;
296 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
299 me->no, me->pref_partner->no, me->pref_value);
300 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
301 &me->pref_partner->id,
302 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
303 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
304 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
305 &ats_pref_task, cls);
309 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
317 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
319 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
320 GNUNET_SCHEDULER_cancel (shutdown_task);
321 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
322 &do_shutdown, NULL );
324 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
326 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Topology connected, start benchmarking...\n");
328 /* Start sending test messages */
329 for (c_m = 0; c_m < num_masters; c_m++)
331 for (c_s = 0; c_s < num_slaves; c_s++)
332 comm_schedule_send (&masters[c_m].partners[c_s]);
333 if (pref_val != GNUNET_ATS_PREFERENCE_END)
334 masters[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &masters[c_m]);
337 if (GNUNET_YES == logging)
338 GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps, num_masters);
343 comm_send_pong_ready (void *cls, size_t size, void *buf)
345 static char msgbuf[TEST_MESSAGE_SIZE];
346 struct BenchmarkPartner *p = cls;
347 struct GNUNET_MessageHeader *msg;
349 if (GNUNET_YES == test_core)
355 p->bytes_sent += TEST_MESSAGE_SIZE;
356 p->me->total_messages_sent++;
357 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
359 msg = (struct GNUNET_MessageHeader *) &msgbuf;
360 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
361 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
362 msg->size = htons (TEST_MESSAGE_SIZE);
363 memcpy (buf, msg, TEST_MESSAGE_SIZE);
365 return TEST_MESSAGE_SIZE;
368 static struct BenchmarkPartner *
369 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
372 GNUNET_assert (NULL != me);
373 GNUNET_assert (NULL != peer);
375 for (c_m = 0; c_m < me->num_partners; c_m++)
377 /* Find a partner with other as destination */
378 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
379 sizeof(struct GNUNET_PeerIdentity)))
381 return &me->partners[c_m];
388 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
389 const struct GNUNET_MessageHeader *message)
392 struct BenchmarkPeer *me = cls;
393 struct BenchmarkPartner *p = NULL;
395 if (NULL == (p = find_partner(me, other)))
398 return GNUNET_SYSERR;
401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
402 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
405 p->messages_received++;
406 p->bytes_received += TEST_MESSAGE_SIZE;
407 p->me->total_messages_received++;
408 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
410 if (GNUNET_YES == test_core)
412 GNUNET_assert (NULL == p->cth);
413 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
414 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
415 &comm_send_pong_ready, p);
419 GNUNET_assert (NULL == p->tth);
420 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
421 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
428 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
429 const struct GNUNET_MessageHeader *message)
431 struct BenchmarkPeer *me = cls;
432 struct BenchmarkPartner *p = NULL;
434 if (NULL == (p = find_partner (me, other)))
437 return GNUNET_SYSERR;
440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
441 "Master [%u]: Received PONG from [%u], next message\n", me->no,
444 p->messages_received++;
445 p->bytes_received += TEST_MESSAGE_SIZE;
446 p->me->total_messages_received++;
447 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
448 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
449 GNUNET_TIME_absolute_get()).rel_value_us;
451 comm_schedule_send (p);
457 test_recv_cb (void *cls,
458 const struct GNUNET_PeerIdentity * peer,
459 const struct GNUNET_MessageHeader * message)
461 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
462 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
463 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
467 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
468 comm_handle_ping (cls, peer, message);
470 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
471 comm_handle_pong (cls, peer, message);
476 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
477 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
478 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
479 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
481 struct BenchmarkPeer *me = cls;
482 struct BenchmarkPartner *p;
487 p = find_partner (me, &address->peer);
490 /* This is not one of my partners
491 * Will happen since the peers will connect to each other due to gossiping
495 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
498 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
499 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
501 p->bandwidth_in = ntohl (bandwidth_in.value__);
502 p->bandwidth_out = ntohl (bandwidth_out.value__);
504 for (c_a = 0; c_a < ats_count; c_a++)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
507 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
509 GNUNET_i2s (&p->dest->id),
510 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
511 ntohl(ats[c_a].value));
512 switch (ntohl (ats[c_a].type ))
514 case GNUNET_ATS_ARRAY_TERMINATOR:
516 case GNUNET_ATS_UTILIZATION_OUT:
517 if (p->ats_utilization_up != ntohl (ats[c_a].value))
519 p->ats_utilization_up = ntohl (ats[c_a].value);
522 case GNUNET_ATS_UTILIZATION_IN:
523 if (p->ats_utilization_down != ntohl (ats[c_a].value))
525 p->ats_utilization_down = ntohl (ats[c_a].value);
527 case GNUNET_ATS_NETWORK_TYPE:
528 if (p->ats_network_type != ntohl (ats[c_a].value))
530 p->ats_network_type = ntohl (ats[c_a].value);
532 case GNUNET_ATS_QUALITY_NET_DELAY:
533 if (p->ats_delay != ntohl (ats[c_a].value))
535 p->ats_delay = ntohl (ats[c_a].value);
537 case GNUNET_ATS_QUALITY_NET_DISTANCE:
538 if (p->ats_distance != ntohl (ats[c_a].value))
540 p->ats_distance = ntohl (ats[c_a].value);
543 case GNUNET_ATS_COST_WAN:
544 if (p->ats_cost_wan != ntohl (ats[c_a].value))
546 p->ats_cost_wan = ntohl (ats[c_a].value);
548 case GNUNET_ATS_COST_LAN:
549 if (p->ats_cost_lan != ntohl (ats[c_a].value))
551 p->ats_cost_lan = ntohl (ats[c_a].value);
553 case GNUNET_ATS_COST_WLAN:
554 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
556 p->ats_cost_wlan = ntohl (ats[c_a].value);
563 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
564 GNUNET_ATS_TEST_logging_now();
566 GNUNET_free(peer_id);
571 * Start the performance test case
574 main (int argc, char *argv[])
582 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
587 /* figure out testname */
588 tmp = strstr (argv[0], TESTNAME_PREFIX);
591 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
592 return GNUNET_SYSERR;
594 tmp += strlen (TESTNAME_PREFIX);
595 solver = GNUNET_strdup (tmp);
596 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
598 tmp_sep = strchr (solver, '_');
601 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
603 return GNUNET_SYSERR;
606 comm_name = GNUNET_strdup (&tmp_sep[1]);
607 tmp_sep = strchr (comm_name, '_');
610 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
612 return GNUNET_SYSERR;
615 for (c = 0; c <= strlen (comm_name); c++)
616 comm_name[c] = toupper (comm_name[c]);
617 if (0 == strcmp (comm_name, "CORE"))
618 test_core = GNUNET_YES;
619 else if (0 == strcmp (comm_name, "TRANSPORT"))
620 test_core = GNUNET_NO;
623 GNUNET_free (comm_name);
624 GNUNET_free (solver);
625 return GNUNET_SYSERR;
628 pref_str = GNUNET_strdup(tmp_sep + 1);
630 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
632 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
634 for (c = 0; c <= strlen (pref_str); c++)
635 pref_str[c] = toupper (pref_str[c]);
638 if (0 != strcmp (pref_str, "NONE"))
640 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
642 if (0 == strcmp (pref_str, prefs[c]))
651 /* abuse terminator to indicate no pref */
652 pref_val = GNUNET_ATS_PREFERENCE_END;
656 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
658 GNUNET_free(pref_str);
659 GNUNET_free (comm_name);
663 for (c = 0; c < (argc - 1); c++)
665 if (0 == strcmp (argv[c], "-d"))
670 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
671 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
675 perf_duration = BENCHMARK_DURATION;
677 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
679 for (c = 0; c < (argc - 1); c++)
681 if (0 == strcmp (argv[c], "-s"))
686 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
687 && (num_slaves >= 1))
688 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
690 num_slaves = DEFAULT_SLAVES_NUM;
693 num_slaves = DEFAULT_SLAVES_NUM;
695 for (c = 0; c < (argc - 1); c++)
697 if (0 == strcmp (argv[c], "-m"))
702 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
703 && (num_masters >= 2))
704 fprintf (stderr, "Starting %u master peers\n", num_masters);
706 num_masters = DEFAULT_MASTERS_NUM;
709 num_masters = DEFAULT_MASTERS_NUM;
712 for (c = 0; c < argc; c++)
714 if (0 == strcmp (argv[c], "-l"))
715 logging = GNUNET_YES;
718 if (GNUNET_YES == logging)
720 for (c = 0; c < (argc - 1); c++)
722 if (0 == strcmp (argv[c], "-f"))
727 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
728 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
732 log_frequency = LOGGING_FREQUENCY;
734 fprintf (stderr, "Using log frequency %llu ms\n",
735 (unsigned long long) (log_frequency.rel_value_us) / (1000));
738 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
740 if (num_slaves < num_masters)
742 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
743 GNUNET_free(test_name);
745 GNUNET_free(pref_str);
746 GNUNET_free (comm_name);
747 return GNUNET_SYSERR;
751 * Core message handler to use for PING/PONG messages
753 static struct GNUNET_CORE_MessageHandler handlers[] = {
754 {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
755 {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
761 GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
762 num_slaves, num_masters,
767 &ats_performance_info_cb);
772 /* end of file perf_ats.c */