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)
431 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
432 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
433 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
437 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
438 comm_handle_ping (cls, peer, message);
440 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
441 comm_handle_pong (cls, peer, message);
449 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
450 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
451 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
452 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
454 struct BenchmarkPeer *me = cls;
455 struct BenchmarkPartner *p;
460 p = find_partner (me, &address->peer);
463 /* This is not one of my partners
464 * Will happen since the peers will connect to each other due to gossiping
468 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
471 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
472 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
474 p->bandwidth_in = ntohl (bandwidth_in.value__);
475 p->bandwidth_out = ntohl (bandwidth_out.value__);
477 for (c_a = 0; c_a < ats_count; c_a++)
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
480 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
482 GNUNET_i2s (&p->dest->id),
483 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
484 ntohl(ats[c_a].value));
485 switch (ntohl (ats[c_a].type ))
487 case GNUNET_ATS_ARRAY_TERMINATOR:
489 case GNUNET_ATS_UTILIZATION_OUT:
490 if (p->ats_utilization_up != ntohl (ats[c_a].value))
492 p->ats_utilization_up = ntohl (ats[c_a].value);
495 case GNUNET_ATS_UTILIZATION_IN:
496 if (p->ats_utilization_down != ntohl (ats[c_a].value))
498 p->ats_utilization_down = ntohl (ats[c_a].value);
500 case GNUNET_ATS_NETWORK_TYPE:
501 if (p->ats_network_type != ntohl (ats[c_a].value))
503 p->ats_network_type = ntohl (ats[c_a].value);
505 case GNUNET_ATS_QUALITY_NET_DELAY:
506 if (p->ats_delay != ntohl (ats[c_a].value))
508 p->ats_delay = ntohl (ats[c_a].value);
510 case GNUNET_ATS_QUALITY_NET_DISTANCE:
511 if (p->ats_distance != ntohl (ats[c_a].value))
513 p->ats_distance = ntohl (ats[c_a].value);
516 case GNUNET_ATS_COST_WAN:
517 if (p->ats_cost_wan != ntohl (ats[c_a].value))
519 p->ats_cost_wan = ntohl (ats[c_a].value);
521 case GNUNET_ATS_COST_LAN:
522 if (p->ats_cost_lan != ntohl (ats[c_a].value))
524 p->ats_cost_lan = ntohl (ats[c_a].value);
526 case GNUNET_ATS_COST_WLAN:
527 if (p->ats_cost_wlan != ntohl (ats[c_a].value))
529 p->ats_cost_wlan = ntohl (ats[c_a].value);
536 if ((GNUNET_YES == logging) && (GNUNET_YES == log))
539 GNUNET_free(peer_id);
547 main (int argc, char *argv[])
555 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
560 /* figure out testname */
561 tmp = strstr (argv[0], TESTNAME_PREFIX);
564 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
565 return GNUNET_SYSERR;
567 tmp += strlen (TESTNAME_PREFIX);
568 solver = GNUNET_strdup (tmp);
569 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
571 tmp_sep = strchr (solver, '_');
574 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
576 return GNUNET_SYSERR;
579 comm_name = GNUNET_strdup (&tmp_sep[1]);
580 tmp_sep = strchr (comm_name, '_');
583 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
585 return GNUNET_SYSERR;
588 for (c = 0; c <= strlen (comm_name); c++)
589 comm_name[c] = toupper (comm_name[c]);
590 if (0 == strcmp (comm_name, "CORE"))
591 test_core = GNUNET_YES;
592 else if (0 == strcmp (comm_name, "TRANSPORT"))
593 test_core = GNUNET_NO;
596 GNUNET_free (comm_name);
597 GNUNET_free (solver);
598 return GNUNET_SYSERR;
601 pref_str = GNUNET_strdup(tmp_sep + 1);
603 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
605 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
607 for (c = 0; c <= strlen (pref_str); c++)
608 pref_str[c] = toupper (pref_str[c]);
611 if (0 != strcmp (pref_str, "NONE"))
613 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
615 if (0 == strcmp (pref_str, prefs[c]))
624 /* abuse terminator to indicate no pref */
625 pref_val = GNUNET_ATS_PREFERENCE_END;
629 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
631 GNUNET_free(pref_str);
632 GNUNET_free (comm_name);
636 for (c = 0; c < (argc - 1); c++)
638 if (0 == strcmp (argv[c], "-d"))
643 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
644 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
648 perf_duration = BENCHMARK_DURATION;
650 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
652 for (c = 0; c < (argc - 1); c++)
654 if (0 == strcmp (argv[c], "-s"))
659 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
660 && (num_slaves >= 1))
661 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
663 num_slaves = DEFAULT_SLAVES_NUM;
666 num_slaves = DEFAULT_SLAVES_NUM;
668 for (c = 0; c < (argc - 1); c++)
670 if (0 == strcmp (argv[c], "-m"))
675 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
676 && (num_masters >= 2))
677 fprintf (stderr, "Starting %u master peers\n", num_masters);
679 num_masters = DEFAULT_MASTERS_NUM;
682 num_masters = DEFAULT_MASTERS_NUM;
685 for (c = 0; c < argc; c++)
687 if (0 == strcmp (argv[c], "-l"))
688 logging = GNUNET_YES;
691 if (GNUNET_YES == logging)
693 for (c = 0; c < (argc - 1); c++)
695 if (0 == strcmp (argv[c], "-f"))
700 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
701 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
705 log_frequency = LOGGING_FREQUENCY;
707 fprintf (stderr, "Using log frequency %llu ms\n",
708 (unsigned long long) (log_frequency.rel_value_us) / (1000));
711 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
713 if (num_slaves < num_masters)
715 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
716 GNUNET_free(test_name);
718 GNUNET_free(pref_str);
719 GNUNET_free (comm_name);
720 return GNUNET_SYSERR;
723 static struct GNUNET_CORE_MessageHandler handlers[] = {
724 {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
725 {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
728 GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
729 num_slaves, num_masters, test_core,
730 &do_benchmark, NULL, handlers, &transport_recv_cb);
735 /* end of file perf_ats.c */