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)
47 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
52 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
64 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
76 static char *testname;
81 static char *pref_str;
84 * ATS preference value
91 static struct GNUNET_TIME_Relative perf_duration;
96 static struct GNUNET_TIME_Relative log_frequency;
101 static unsigned int num_masters;
104 * Array of master peers
106 static struct BenchmarkPeer *mps;
111 static unsigned int num_slaves;
114 * Array of master peers
116 static struct BenchmarkPeer *sps;
123 unsigned int duration;
124 struct BenchmarkPeer *mp;
125 struct BenchmarkPartner *p;
127 duration = (perf_duration.rel_value_us / (1000 * 1000));
128 for (c_m = 0; c_m < num_masters; c_m++)
132 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
133 mp->no, mp->total_bytes_sent / 1024, duration,
134 (mp->total_bytes_sent / 1024) / duration,
135 mp->total_bytes_received / 1024, duration,
136 (mp->total_bytes_received / 1024) / duration);
138 for (c_s = 0; c_s < num_slaves; c_s++)
140 p = &mp->partners[c_s];
142 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
143 (mp->pref_partner == p->dest) ? '*' : ' ',
145 (p->bytes_sent / 1024) / duration,
146 ((double) p->bytes_sent * 100) / mp->total_bytes_sent,
147 (p->bytes_received / 1024) / duration,
148 ((double) p->bytes_received * 100) / mp->total_bytes_received );
150 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
151 (mp->pref_partner == p->dest) ? '*' : ' ',
153 p->total_app_rtt / (1000 * p->messages_sent));
162 * @param tc the task context
165 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 if (GNUNET_YES == logging)
171 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
172 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
174 fprintf (stderr, "0\n");
175 GNUNET_SCHEDULER_cancel (progress_task);
177 progress_task = GNUNET_SCHEDULER_NO_TASK;
180 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
182 GNUNET_ATS_TEST_shutdown_topology();
186 comm_send_ready (void *cls, size_t size, void *buf)
188 static char msgbuf[TEST_MESSAGE_SIZE];
189 struct BenchmarkPartner *p = cls;
190 struct GNUNET_MessageHeader *msg;
192 if (GNUNET_YES == test_core)
202 if (size < TEST_MESSAGE_SIZE)
208 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Master [%u]: Sending PING to [%u]\n",
209 p->me->no, p->dest->no);
212 p->bytes_sent += TEST_MESSAGE_SIZE;
213 p->me->total_messages_sent++;
214 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
216 msg = (struct GNUNET_MessageHeader *) &msgbuf;
217 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
218 msg->type = htons (TEST_MESSAGE_TYPE_PING);
219 msg->size = htons (TEST_MESSAGE_SIZE);
220 memcpy (buf, msg, TEST_MESSAGE_SIZE);
221 return TEST_MESSAGE_SIZE;
225 comm_schedule_send (struct BenchmarkPartner *p)
227 p->last_message_sent = GNUNET_TIME_absolute_get();
228 if (GNUNET_YES == test_core)
230 p->cth = GNUNET_CORE_notify_transmit_ready (
231 p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
232 TEST_MESSAGE_SIZE, &comm_send_ready, p);
236 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
237 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
238 &comm_send_ready, p);
247 progress_task = GNUNET_SCHEDULER_NO_TASK;
249 fprintf (stderr, "%llu..",
250 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
253 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
254 &print_progress, NULL );
258 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260 struct BenchmarkPeer *me = cls;
262 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
264 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
265 me->no, me->pref_partner->no, me->pref_value);
266 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
267 &me->pref_partner->id,
268 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
269 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
270 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
271 &ats_pref_task, cls);
275 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
283 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
285 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
286 GNUNET_SCHEDULER_cancel (shutdown_task);
287 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
288 &do_shutdown, NULL );
290 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
292 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Topology connected, start benchmarking...\n");
294 /* Start sending test messages */
295 for (c_m = 0; c_m < num_masters; c_m++)
297 for (c_s = 0; c_s < num_slaves; c_s++)
298 comm_schedule_send (&masters[c_m].partners[c_s]);
299 if (pref_val != GNUNET_ATS_PREFERENCE_END)
300 masters[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &masters[c_m]);
304 if (GNUNET_YES == logging)
305 perf_logging_start (log_frequency, testname, mps, num_masters);
311 comm_send_pong_ready (void *cls, size_t size, void *buf)
313 static char msgbuf[TEST_MESSAGE_SIZE];
314 struct BenchmarkPartner *p = cls;
315 struct GNUNET_MessageHeader *msg;
317 if (GNUNET_YES == test_core)
323 p->bytes_sent += TEST_MESSAGE_SIZE;
324 p->me->total_messages_sent++;
325 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
327 msg = (struct GNUNET_MessageHeader *) &msgbuf;
328 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
329 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
330 msg->size = htons (TEST_MESSAGE_SIZE);
331 memcpy (buf, msg, TEST_MESSAGE_SIZE);
333 return TEST_MESSAGE_SIZE;
336 static struct BenchmarkPartner *
337 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
340 GNUNET_assert (NULL != me);
341 GNUNET_assert (NULL != peer);
343 for (c_m = 0; c_m < me->num_partners; c_m++)
345 /* Find a partner with other as destination */
346 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
347 sizeof(struct GNUNET_PeerIdentity)))
349 return &me->partners[c_m];
357 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
358 const struct GNUNET_MessageHeader *message)
361 struct BenchmarkPeer *me = cls;
362 struct BenchmarkPartner *p = NULL;
364 if (NULL == (p = find_partner(me, other)))
367 return GNUNET_SYSERR;
370 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
371 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
374 p->messages_received++;
375 p->bytes_received += TEST_MESSAGE_SIZE;
376 p->me->total_messages_received++;
377 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
379 if (GNUNET_YES == test_core)
381 GNUNET_assert (NULL == p->cth);
382 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
383 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
384 &comm_send_pong_ready, p);
388 GNUNET_assert (NULL == p->tth);
389 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
390 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
397 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
398 const struct GNUNET_MessageHeader *message)
400 struct BenchmarkPeer *me = cls;
401 struct BenchmarkPartner *p = NULL;
403 if (NULL == (p = find_partner (me, other)))
406 return GNUNET_SYSERR;
409 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
410 "Master [%u]: Received PONG from [%u], next message\n", me->no,
413 p->messages_received++;
414 p->bytes_received += TEST_MESSAGE_SIZE;
415 p->me->total_messages_received++;
416 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
417 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
418 GNUNET_TIME_absolute_get()).rel_value_us;
420 comm_schedule_send (p);
427 transport_recv_cb (void *cls,
428 const struct GNUNET_PeerIdentity * peer,
429 const struct GNUNET_MessageHeader * message)
432 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
433 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
434 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
438 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
439 comm_handle_ping (cls, peer, message);
441 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
442 comm_handle_pong (cls, peer, message);
450 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
451 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
452 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
453 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
455 struct BenchmarkPeer *me = cls;
456 struct BenchmarkPartner *p;
461 p = find_partner (me, &address->peer);
464 /* This is not one of my partners
465 * Will happen since the peers will connect to each other due to gossiping
469 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
472 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
473 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
475 p->bandwidth_in = ntohl (bandwidth_in.value__);
476 p->bandwidth_out = ntohl (bandwidth_out.value__);
478 for (c_a = 0; c_a < ats_count; c_a++)
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
481 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
483 GNUNET_i2s (&p->dest->id),
484 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
485 ntohl(ats[c_a].value));
486 switch (ntohl (ats[c_a].type ))
488 case GNUNET_ATS_ARRAY_TERMINATOR:
490 case GNUNET_ATS_UTILIZATION_OUT:
491 if (p->ats_utilization_up != ntohl (ats[c_a].value))
493 p->ats_utilization_up = ntohl (ats[c_a].value);
496 case GNUNET_ATS_UTILIZATION_IN:
497 if (p->ats_utilization_down != ntohl (ats[c_a].value))
499 p->ats_utilization_down = ntohl (ats[c_a].value);
501 case GNUNET_ATS_NETWORK_TYPE:
502 if (p->ats_network_type != ntohl (ats[c_a].value))
504 p->ats_network_type = ntohl (ats[c_a].value);
506 case GNUNET_ATS_QUALITY_NET_DELAY:
507 if (p->ats_delay != ntohl (ats[c_a].value))
509 p->ats_delay = ntohl (ats[c_a].value);
511 case GNUNET_ATS_QUALITY_NET_DISTANCE:
512 if (p->ats_distance != ntohl (ats[c_a].value))
514 p->ats_distance = ntohl (ats[c_a].value);
517 case GNUNET_ATS_COST_WAN:
518 if (p->ats_cost_wan != ntohl (ats[c_a].value))
520 p->ats_cost_wan = ntohl (ats[c_a].value);
522 case GNUNET_ATS_COST_LAN:
523 if (p->ats_cost_lan != ntohl (ats[c_a].value))
525 p->ats_cost_lan = ntohl (ats[c_a].value);
527 case GNUNET_ATS_COST_WLAN:
528 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
530 p->ats_cost_wlan = ntohl (ats[c_a].value);
537 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
540 GNUNET_free(peer_id);
548 main (int argc, char *argv[])
556 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
561 /* figure out testname */
562 tmp = strstr (argv[0], TESTNAME_PREFIX);
565 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
566 return GNUNET_SYSERR;
568 tmp += strlen (TESTNAME_PREFIX);
569 solver = GNUNET_strdup (tmp);
570 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
572 tmp_sep = strchr (solver, '_');
575 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
577 return GNUNET_SYSERR;
580 comm_name = GNUNET_strdup (&tmp_sep[1]);
581 tmp_sep = strchr (comm_name, '_');
584 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
586 return GNUNET_SYSERR;
589 for (c = 0; c <= strlen (comm_name); c++)
590 comm_name[c] = toupper (comm_name[c]);
591 if (0 == strcmp (comm_name, "CORE"))
592 test_core = GNUNET_YES;
593 else if (0 == strcmp (comm_name, "TRANSPORT"))
594 test_core = GNUNET_NO;
597 GNUNET_free (comm_name);
598 GNUNET_free (solver);
599 return GNUNET_SYSERR;
602 pref_str = GNUNET_strdup(tmp_sep + 1);
604 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
606 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
608 for (c = 0; c <= strlen (pref_str); c++)
609 pref_str[c] = toupper (pref_str[c]);
612 if (0 != strcmp (pref_str, "NONE"))
614 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
616 if (0 == strcmp (pref_str, prefs[c]))
625 /* abuse terminator to indicate no pref */
626 pref_val = GNUNET_ATS_PREFERENCE_END;
630 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
632 GNUNET_free(pref_str);
633 GNUNET_free (comm_name);
637 for (c = 0; c < (argc - 1); c++)
639 if (0 == strcmp (argv[c], "-d"))
644 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
645 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
649 perf_duration = BENCHMARK_DURATION;
651 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
653 for (c = 0; c < (argc - 1); c++)
655 if (0 == strcmp (argv[c], "-s"))
660 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
661 && (num_slaves >= 1))
662 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
664 num_slaves = DEFAULT_SLAVES_NUM;
667 num_slaves = DEFAULT_SLAVES_NUM;
669 for (c = 0; c < (argc - 1); c++)
671 if (0 == strcmp (argv[c], "-m"))
676 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
677 && (num_masters >= 2))
678 fprintf (stderr, "Starting %u master peers\n", num_masters);
680 num_masters = DEFAULT_MASTERS_NUM;
683 num_masters = DEFAULT_MASTERS_NUM;
686 for (c = 0; c < argc; c++)
688 if (0 == strcmp (argv[c], "-l"))
689 logging = GNUNET_YES;
692 if (GNUNET_YES == logging)
694 for (c = 0; c < (argc - 1); c++)
696 if (0 == strcmp (argv[c], "-f"))
701 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
702 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
706 log_frequency = LOGGING_FREQUENCY;
708 fprintf (stderr, "Using log frequency %llu ms\n",
709 (unsigned long long) (log_frequency.rel_value_us) / (1000));
712 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
714 if (num_slaves < num_masters)
716 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
717 GNUNET_free(test_name);
719 GNUNET_free(pref_str);
720 GNUNET_free (comm_name);
721 return GNUNET_SYSERR;
724 static struct GNUNET_CORE_MessageHandler handlers[] = {
725 {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
726 {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
729 GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
730 num_slaves, num_masters, test_core,
731 &do_benchmark, NULL, handlers, &transport_recv_cb);
736 /* end of file perf_ats.c */