add function conv param string
[oweals/gnunet.git] / src / ats-tests / perf_ats.c
index 7b5a903bebc759ef89404f852289e1b6a81841d1..0e4056a6cedc5c793584d8aa85bf6864aa49ec4a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- (C) 2010-2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010-2013, 2016 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 /**
  * @file ats/perf_ats.c
 #define TESTNAME_PREFIX "perf_ats_"
 #define DEFAULT_SLAVES_NUM 2
 #define DEFAULT_MASTERS_NUM 1
+
 /**
- * Shutdown task
+ * timeout task
  */
-static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+static struct GNUNET_SCHEDULER_Task *timeout_task;
 
 /**
  * Progress task
  */
-static GNUNET_SCHEDULER_TaskIdentifier progress_task;
+static struct GNUNET_SCHEDULER_Task *progress_task;
 
 /**
  * Test result
@@ -63,7 +64,8 @@ static int result;
  */
 static int logging;
 
-/**Test core (GNUNET_YES) or transport (GNUNET_NO)
+/**
+ * Test core (#GNUNET_YES) or transport (#GNUNET_NO)
  */
 static int test_core;
 
@@ -119,6 +121,7 @@ static struct BenchmarkPeer *sps;
 
 static struct LoggingHandle *l;
 
+
 static void
 evaluate ()
 {
@@ -138,6 +141,8 @@ evaluate ()
   for (c_m = 0; c_m < num_masters; c_m++)
   {
     mp = &mps[c_m];
+    if (NULL == mp)
+      continue;
     fprintf (stderr,
         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
         mp->no, mp->total_bytes_sent / 1024, duration,
@@ -148,7 +153,8 @@ evaluate ()
     for (c_s = 0; c_s < num_slaves; c_s++)
     {
       p = &mp->partners[c_s];
-
+      if (NULL == p)
+        continue;
       kb_sent_sec = 0;
       kb_recv_sec = 0;
       kb_sent_percent = 0.0;
@@ -168,67 +174,86 @@ evaluate ()
       if (1000 * p->messages_sent > 0)
          rtt = p->total_app_rtt / (1000 * p->messages_sent);
       fprintf (stderr,
-          "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
-          (mp->pref_partner == p->dest) ? '*' : ' ',
-          mp->no, p->dest->no,
-          kb_sent_sec, kb_sent_percent,
-                 kb_recv_sec, kb_recv_percent);
+               "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
+               (mp->pref_partner == p->dest) ? '*' : ' ',
+               mp->no, p->dest->no,
+               kb_sent_sec, kb_sent_percent,
+               kb_recv_sec, kb_recv_percent);
       fprintf (stderr,
-          "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
-          (mp->pref_partner == p->dest) ? '*' : ' ',
-          mp->no, p->dest->no, rtt);
+               "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
+               (mp->pref_partner == p->dest) ? '*' : ' ',
+               mp->no, p->dest->no, rtt);
     }
   }
 }
 
+
 /**
  * Shutdown nicely
  *
  * @param cls NULL
- * @param tc the task context
  */
 static void
-do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
-
   if (GNUNET_YES == logging)
     GNUNET_ATS_TEST_logging_clean_up(l);
-
-  shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-  if (GNUNET_SCHEDULER_NO_TASK != progress_task)
+  if (NULL != timeout_task)
+  {
+    GNUNET_SCHEDULER_cancel (timeout_task);
+    timeout_task = NULL;
+  }
+  if (NULL != progress_task)
   {
     fprintf (stderr, "0\n");
     GNUNET_SCHEDULER_cancel (progress_task);
+    progress_task = NULL;
   }
-  progress_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Benchmarking done\n");
+  GNUNET_ATS_TEST_shutdown_topology ();
+}
 
-  evaluate ();
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
 
-  GNUNET_ATS_TEST_shutdown_topology();
+/**
+ * Shutdown nicely
+ *
+ * @param cls NULL
+ */
+static void
+do_timeout (void *cls)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Terminating with timeout\n");
+  timeout_task = NULL;
+  evaluate ();
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
 static void
-print_progress ()
+print_progress (void *cls)
 {
   static int calls;
-  progress_task = GNUNET_SCHEDULER_NO_TASK;
 
-  fprintf (stderr, "%llu..",
-      (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
+  progress_task = NULL;
+  fprintf (stderr,
+           "%llu..",
+           (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
   calls++;
 
   progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-      &print_progress, NULL );
+                                                &print_progress,
+                                                NULL);
 }
 
+
 static void
-ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ats_pref_task (void *cls)
 {
   struct BenchmarkPeer *me = cls;
 
-  me->ats_task = GNUNET_SCHEDULER_NO_TASK;
+  me->ats_task = NULL;
 
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
       me->no, me->pref_partner->no, me->pref_value);
@@ -240,56 +265,65 @@ ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       &ats_pref_task, cls);
 }
 
+
 static void
-start_benchmark()
+start_benchmark (void *cls)
 {
   int c_m;
   int c_s;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
-
-  if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
-    GNUNET_SCHEDULER_cancel(shutdown_task);
-  shutdown_task = GNUNET_SCHEDULER_add_delayed(perf_duration, &do_shutdown,
-      NULL );
-
-  progress_task = GNUNET_SCHEDULER_add_now(&print_progress, NULL );
+  progress_task = GNUNET_SCHEDULER_add_now (&print_progress,
+                                            NULL);
 
   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-      "Topology connected, start benchmarking...\n");
+             "Topology connected, start benchmarking...\n");
 
   /* Start sending test messages */
   for (c_m = 0; c_m < num_masters; c_m++)
+  {
+    for (c_s = 0; c_s < num_slaves; c_s++)
     {
-      for (c_s = 0; c_s < num_slaves; c_s++)
-      {
-        GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m], &mps[c_m].partners[c_s],
-            GNUNET_ATS_TEST_TG_LINEAR, UINT32_MAX, UINT32_MAX,
-            GNUNET_TIME_UNIT_MINUTES, GNUNET_TIME_UNIT_FOREVER_REL);
-      }
-      if (pref_val != GNUNET_ATS_PREFERENCE_END)
-        mps[c_m].ats_task = GNUNET_SCHEDULER_add_now(&ats_pref_task, &mps[c_m]);
+      GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m],
+                                              &mps[c_m].partners[c_s],
+                                              GNUNET_ATS_TEST_TG_LINEAR,
+                                              UINT32_MAX,
+                                              UINT32_MAX,
+                                              GNUNET_TIME_UNIT_MINUTES,
+                                              GNUNET_TIME_UNIT_FOREVER_REL);
     }
+    if (pref_val != GNUNET_ATS_PREFERENCE_END)
+      mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task,
+                                                    &mps[c_m]);
+  }
 
   if (GNUNET_YES == logging)
-    l = GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps,
-        num_masters, num_slaves, GNUNET_NO);
+    l = GNUNET_ATS_TEST_logging_start (log_frequency,
+                                       testname, mps,
+                                       num_masters, num_slaves,
+                                       GNUNET_NO);
 }
 
+
 static void
-do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
+do_benchmark (void *cls,
+              struct BenchmarkPeer *masters,
+              struct BenchmarkPeer *slaves)
 {
   mps = masters;
   sps = slaves;
-
-  GNUNET_SCHEDULER_add_now(&start_benchmark, NULL);
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                 NULL);
+  timeout_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
+                                               &do_timeout,
+                                               NULL);
+  progress_task = GNUNET_SCHEDULER_add_now (&start_benchmark,
+                                            NULL);
 }
 
 
-
-
 static struct BenchmarkPartner *
-find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
+find_partner (struct BenchmarkPeer *me,
+              const struct GNUNET_PeerIdentity *peer)
 {
   int c_m;
   GNUNET_assert (NULL != me);
@@ -307,24 +341,26 @@ find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
   return NULL;
 }
 
+
 static void
 test_recv_cb (void *cls,
-                     const struct GNUNET_PeerIdentity * peer,
-                     const struct GNUNET_MessageHeader * message)
+              const struct GNUNET_PeerIdentity * peer,
+              const struct GNUNET_MessageHeader * message)
 {
 
 }
 
 
 static void
-log_request_cb (void *cls, const struct GNUNET_HELLO_Address *address,
-    int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
-    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
-    const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
+log_request_cb (void *cls,
+                const struct GNUNET_HELLO_Address *address,
+                int address_active,
+                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+                const struct GNUNET_ATS_Properties *ats)
 {
   struct BenchmarkPeer *me = cls;
   struct BenchmarkPartner *p;
-  int c_a;
   char *peer_id;
 
   p = find_partner (me, &address->peer);
@@ -342,16 +378,13 @@ log_request_cb (void *cls, const struct GNUNET_HELLO_Address *address,
   p->bandwidth_in = ntohl (bandwidth_in.value__);
   p->bandwidth_out = ntohl (bandwidth_out.value__);
 
-  for (c_a = 0; c_a < ats_count; c_a++)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
-        (GNUNET_YES == p->me->master) ? "Master" : "Slave",
-        p->me->no,
-        GNUNET_i2s (&p->dest->id),
-        GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
-        ntohl(ats[c_a].value));
-  }
-  GNUNET_free(peer_id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s [%u] received ATS information for peers `%s'\n",
+              (GNUNET_YES == p->me->master) ? "Master" : "Slave",
+              p->me->no,
+              GNUNET_i2s (&p->dest->id));
+
+  GNUNET_free (peer_id);
   if (NULL != l)
     GNUNET_ATS_TEST_logging_now (l);
 }
@@ -369,22 +402,32 @@ main (int argc, char *argv[])
   char *conf_name;
   char *comm_name;
   char *dotexe;
-  char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
+  char *prefs[] = GNUNET_ATS_PreferenceTypeString;
   int c;
 
   result = 0;
 
-  /* figure out testname */
+  /* Determine testname
+   * perf_ats_<solver>_<transport>_<preference>[.exe]*/
+
+  /* Find test prefix, store in temp */
   tmp = strstr (argv[0], TESTNAME_PREFIX);
   if (NULL == tmp)
   {
     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
     return GNUNET_SYSERR;
   }
+
+  /* Set tmp to end of test name prefix */
   tmp += strlen (TESTNAME_PREFIX);
+
+  /* Determine solver name */
   solver = GNUNET_strdup (tmp);
+  /* Remove .exe prefix */
   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
     dotexe[0] = '\0';
+
+  /* Determine first '_' after solver */
   tmp_sep = strchr (solver, '_');
   if (NULL == tmp_sep)
   {
@@ -427,7 +470,7 @@ main (int argc, char *argv[])
 
   if (0 != strcmp (pref_str, "NONE"))
   {
-    for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
+    for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
     {
       if (0 == strcmp (pref_str, prefs[c]))
       {
@@ -529,7 +572,8 @@ main (int argc, char *argv[])
 
   if (num_slaves < num_masters)
   {
-    fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
+    fprintf (stderr,
+             "Number of master peers is lower than slaves! exit...\n");
     GNUNET_free(test_name);
     GNUNET_free(solver);
     GNUNET_free(pref_str);
@@ -540,13 +584,14 @@ main (int argc, char *argv[])
   /**
    * Setup the topology
    */
-  GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
-      num_slaves, num_masters,
-      test_core,
-      &do_benchmark,
-      NULL,
-      &test_recv_cb,
-      &log_request_cb);
+  GNUNET_ATS_TEST_create_topology ("perf-ats",
+                                   conf_name,
+                                   num_slaves, num_masters,
+                                   test_core,
+                                   &do_benchmark,
+                                   NULL,
+                                   &test_recv_cb,
+                                   &log_request_cb);
 
   return result;
 }