-fix (C) notices
[oweals/gnunet.git] / src / ats-tests / ats-testing-experiment.c
index 80f30d9f0f7288dfacc3e28f03547b1d3a40b8df..38600219be2e9f6964b52e49e91b5536f2d0a0e7 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 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-tests/ats-testing-experiment.c
@@ -35,10 +35,10 @@ print_op (enum OperationType op)
       return "START_SEND";
     case STOP_SEND:
       return "STOP_SEND";
-    case SET_RATE:
-      return "SET_RATE";
-    case SET_PREFERENCE:
-      return "SET_PREFERENCE";
+    case START_PREFERENCE:
+      return "START_PREFERENCE";
+    case STOP_PREFERENCE:
+      return "STOP_PREFERENCE";
     default:
       break;
   }
@@ -64,8 +64,8 @@ free_experiment (struct Experiment *e)
 {
   struct Episode *cur;
   struct Episode *next;
-  struct Operation *cur_o;
-  struct Operation *next_o;
+  struct GNUNET_ATS_TEST_Operation *cur_o;
+  struct GNUNET_ATS_TEST_Operation *next_o;
 
   next = e->start;
   for (cur = next; NULL != cur; cur = next)
@@ -86,29 +86,34 @@ free_experiment (struct Experiment *e)
   GNUNET_free (e);
 }
 
+
 static int
-load_episode (struct Experiment *e, struct Episode *cur,
-    struct GNUNET_CONFIGURATION_Handle *cfg)
+load_episode (struct Experiment *e,
+              struct Episode *cur,
+              struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  struct Operation *o;
+  struct GNUNET_ATS_TEST_Operation *o;
   char *sec_name;
   char *op_name;
   char *op;
   char *type;
+  char *pref;
   int op_counter = 0;
+
   fprintf (stderr, "Parsing episode %u\n",cur->id);
-  GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
+  GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
 
   while (1)
   {
     /* Load operation */
-    GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
+    GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
         sec_name, op_name, &op))
     {
+      GNUNET_free (op_name);
       break;
     }
-    o = GNUNET_new (struct Operation);
+    o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
     /* operations = set_rate, start_send, stop_send, set_preference */
     if (0 == strcmp (op, "start_send"))
     {
@@ -118,13 +123,13 @@ load_episode (struct Experiment *e, struct Episode *cur,
     {
       o->type = STOP_SEND;
     }
-    else if (0 == strcmp (op, "set_rate"))
+    else if (0 == strcmp (op, "start_preference"))
     {
-      o->type = SET_RATE;
+      o->type = START_PREFERENCE;
     }
-    else if (0 == strcmp (op, "set_preference"))
+    else if (0 == strcmp (op, "stop_preference"))
     {
-      o->type = SET_PREFERENCE;
+      o->type = STOP_PREFERENCE;
     }
     else
     {
@@ -132,6 +137,8 @@ load_episode (struct Experiment *e, struct Episode *cur,
           op_counter, op, cur->id);
       GNUNET_free (op);
       GNUNET_free (op_name);
+      GNUNET_free (o);
+      GNUNET_free (sec_name);
       return GNUNET_SYSERR;
     }
     GNUNET_free (op_name);
@@ -145,14 +152,18 @@ load_episode (struct Experiment *e, struct Episode *cur,
           op_counter, op, cur->id);
       GNUNET_free (op);
       GNUNET_free (op_name);
+      GNUNET_free (o);
+      GNUNET_free (sec_name);
       return GNUNET_SYSERR;
     }
-    if (o->src_id > e->num_masters)
+    if (o->src_id > (e->num_masters - 1))
     {
       fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
           o->src_id, op_counter, op, cur->id);
       GNUNET_free (op);
       GNUNET_free (op_name);
+      GNUNET_free (o);
+      GNUNET_free (sec_name);
       return GNUNET_SYSERR;
     }
     GNUNET_free (op_name);
@@ -166,50 +177,53 @@ load_episode (struct Experiment *e, struct Episode *cur,
           op_counter, op, cur->id);
       GNUNET_free (op);
       GNUNET_free (op_name);
+      GNUNET_free (o);
+      GNUNET_free (sec_name);
       return GNUNET_SYSERR;
     }
-    if (o->dest_id > e->num_slaves)
+    if (o->dest_id > (e->num_slaves - 1))
     {
       fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
           o->dest_id, op_counter, op, cur->id);
       GNUNET_free (op);
       GNUNET_free (op_name);
+      GNUNET_free (o);
+      GNUNET_free (sec_name);
       return GNUNET_SYSERR;
     }
     GNUNET_free (op_name);
 
     GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
-    if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
-        sec_name, op_name, &type))
-    {
-      break;
-    }
-
-    if (STOP_SEND != o->type)
+    if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
+            sec_name, op_name, &type)) &&
+        ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
     {
       /* Load arguments for set_rate, start_send, set_preference */
       if (0 == strcmp (type, "constant"))
       {
-        o->tg_type = GNUNET_ATS_TEST_TG_CONSTANT;
+        o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
       }
       else if (0 == strcmp (type, "linear"))
       {
-        o->tg_type = GNUNET_ATS_TEST_TG_LINEAR;
+        o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
       }
       else if (0 == strcmp (type, "sinus"))
       {
-        o->tg_type = GNUNET_ATS_TEST_TG_SINUS;
+        o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
       }
       else if (0 == strcmp (type, "random"))
       {
-        o->tg_type = GNUNET_ATS_TEST_TG_RANDOM;
+        o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
       }
       else
       {
         fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
             op_counter, op, cur->id);
+        GNUNET_free (type);
         GNUNET_free (op);
         GNUNET_free (op_name);
+        GNUNET_free (sec_name);
+        GNUNET_free (o);
         return GNUNET_SYSERR;
       }
       GNUNET_free (op_name);
@@ -221,8 +235,11 @@ load_episode (struct Experiment *e, struct Episode *cur,
       {
         fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
             op_counter, op, cur->id);
+        GNUNET_free (type);
         GNUNET_free (op);
         GNUNET_free (op_name);
+        GNUNET_free (sec_name);
+        GNUNET_free (o);
         return GNUNET_SYSERR;
       }
       GNUNET_free (op_name);
@@ -232,33 +249,90 @@ load_episode (struct Experiment *e, struct Episode *cur,
       if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
           sec_name, op_name, &o->max_rate))
       {
-        if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
-            (GNUNET_ATS_TEST_TG_RANDOM == o->tg_type) ||
-            (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
+        if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
+            (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
+            (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
         {
           fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
               op_counter, op, cur->id);
+          GNUNET_free (type);
+          GNUNET_free (op_name);
           GNUNET_free (op);
+          GNUNET_free (o);
+          GNUNET_free (sec_name);
           return GNUNET_SYSERR;
         }
       }
       GNUNET_free (op_name);
 
+      /* Get period */
+      GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
+      if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
+          sec_name, op_name, &o->period))
       {
-        /* Get period */
-        GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
-        if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
-            sec_name, op_name, &o->period))
-        {
-          o->period = cur->duration;
-        }
-        GNUNET_free (op_name);
+        o->period = cur->duration;
+      }
+      GNUNET_free (op_name);
+
+      if (START_PREFERENCE == o->type)
+      {
+          /* Get frequency */
+          GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
+          if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
+              sec_name, op_name, &o->frequency))
+          {
+              fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
+                  op_counter, op, cur->id);
+              GNUNET_free (type);
+              GNUNET_free (op_name);
+              GNUNET_free (op);
+              GNUNET_free (o);
+              GNUNET_free (sec_name);
+              return GNUNET_SYSERR;
+          }
+          GNUNET_free (op_name);
+
+          /* Get preference */
+          GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
+          if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
+              sec_name, op_name, &pref))
+          {
+              fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
+                  op_counter, op, cur->id);
+              GNUNET_free (type);
+              GNUNET_free (op_name);
+              GNUNET_free (op);
+              GNUNET_free_non_null (pref);
+              GNUNET_free (o);
+              GNUNET_free (sec_name);
+              return GNUNET_SYSERR;
+          }
+
+          if (0 == strcmp(pref, "bandwidth"))
+            o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
+          else if (0 == strcmp(pref, "latency"))
+            o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
+          else
+          {
+              fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
+                  op_counter, op, cur->id);
+              GNUNET_free (type);
+              GNUNET_free (op_name);
+              GNUNET_free (op);
+              GNUNET_free (pref);
+              GNUNET_free_non_null (pref);
+              GNUNET_free (o);
+              GNUNET_free (sec_name);
+              return GNUNET_SYSERR;
+          }
+          GNUNET_free (pref);
+          GNUNET_free (op_name);
       }
     }
 
     /* Safety checks */
-    if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
-        (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
+    if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
+        (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
     {
       if ((o->max_rate - o->base_rate) > o->base_rate)
       {
@@ -268,10 +342,18 @@ load_episode (struct Experiment *e, struct Episode *cur,
       fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
     }
 
-    fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
+    if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
+      fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
         op_counter, cur->id, print_op (o->type), o->src_id,
-        o->dest_id, type, o->base_rate, o->max_rate,
+        o->dest_id, (NULL != type) ? type : "",
+        o->base_rate, o->max_rate,
         GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
+    else
+      fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
+        op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
+
+    GNUNET_free_non_null (type);
+    GNUNET_free (op);
 
     GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
     op_counter++;
@@ -281,6 +363,7 @@ load_episode (struct Experiment *e, struct Episode *cur,
   return GNUNET_OK;
 }
 
+
 static int
 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
 {
@@ -337,13 +420,13 @@ static void
 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
 {
   struct Experiment *e = cls;
-  e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  e->experiment_timeout_task = NULL;
   fprintf (stderr, "Experiment timeout!\n");
 
-  if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
+  if (NULL != e->episode_timeout_task)
   {
     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
-    e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    e->episode_timeout_task = NULL;
   }
 
   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
@@ -351,32 +434,122 @@ timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
 }
 
 static void
-enforce_start_send (struct Operation *op)
+enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
 {
-  GNUNET_break (0);
+  struct BenchmarkPeer *peer;
+  struct BenchmarkPartner *partner;
+
+  peer = GNUNET_ATS_TEST_get_peer (op->src_id);
+  if (NULL == peer)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
+  if (NULL == partner)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
+
+  if (NULL != partner->tg)
+  {
+    fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
+    GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
+    partner->tg = NULL;
+  }
+
+  partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
+      op->gen_type, op->base_rate, op->max_rate, op->period,
+      GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 static void
-enforce_stop_send (struct Operation *op)
+enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
 {
-  GNUNET_break (0);
+  struct BenchmarkPartner *p;
+  p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
+  if (NULL == p)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
+
+  if (NULL != p->tg)
+  {
+    fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
+        op->src_id, op->dest_id);
+    GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
+    p->tg = NULL;
+  }
 }
 
+
 static void
-enforce_set_rate (struct Operation *op)
+enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
 {
-  GNUNET_break (0);
+  struct BenchmarkPeer *peer;
+  struct BenchmarkPartner *partner;
+
+  peer = GNUNET_ATS_TEST_get_peer (op->src_id);
+  if (NULL == peer)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
+  if (NULL == partner)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
+
+  if (NULL != partner->pg)
+  {
+    fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
+        op->src_id, op->dest_id);
+    GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
+    partner->pg = NULL;
+  }
+
+  partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
+      op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
+      op->pref_type);
 }
 
 static void
-enforce_set_preference (struct Operation *op)
+enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
 {
-  GNUNET_break (0);
+  struct BenchmarkPartner *p;
+  p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
+  if (NULL == p)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
+
+  if (NULL != p->pg)
+  {
+    fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
+        op->src_id, op->dest_id);
+    GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
+    p->pg = NULL;
+  }
 }
 
 static void enforce_episode (struct Episode *ep)
 {
-  struct Operation *cur;
+  struct GNUNET_ATS_TEST_Operation *cur;
   for (cur = ep->head; NULL != cur; cur = cur->next)
   {
 
@@ -389,11 +562,11 @@ static void enforce_episode (struct Episode *ep)
       case STOP_SEND:
         enforce_stop_send (cur);
         break;
-      case SET_RATE:
-        enforce_set_rate (cur);
+      case START_PREFERENCE:
+        enforce_start_preference (cur);
         break;
-      case SET_PREFERENCE:
-        enforce_set_preference (cur);
+      case STOP_PREFERENCE:
+        enforce_stop_preference (cur);
         break;
       default:
         break;
@@ -405,7 +578,7 @@ static void
 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
 {
   struct Experiment *e = cls;
-  e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  e->episode_timeout_task = NULL;
   if (NULL != e->ep_done_cb)
     e->ep_done_cb (e->cur);
 
@@ -415,10 +588,10 @@ timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
   {
     /* done */
     fprintf (stderr, "Last episode done!\n");
-    if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
+    if (NULL != e->experiment_timeout_task)
     {
       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
-      e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+      e->experiment_timeout_task = NULL;
     }
     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
     return;
@@ -521,6 +694,17 @@ GNUNET_ATS_TEST_experimentation_load (char *filename)
     fprintf (stderr, "Experiment slaves: `%llu'\n",
         e->num_slaves);
 
+  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
+      "log_freq", &e->log_freq))
+  {
+    fprintf (stderr, "Invalid %s", "log_freq");
+    free_experiment (e);
+    return NULL;
+  }
+  else
+    fprintf (stderr, "Experiment logging frequency: `%s'\n",
+        GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
+
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
       "max_duration", &e->max_duration))
   {
@@ -544,10 +728,15 @@ GNUNET_ATS_TEST_experimentation_load (char *filename)
 void
 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
+  if (NULL != e->experiment_timeout_task)
   {
     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
-    e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    e->experiment_timeout_task = NULL;
+  }
+  if (NULL != e->episode_timeout_task)
+  {
+    GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
+    e->episode_timeout_task = NULL;
   }
   free_experiment (e);
 }