fixing resource leaks
[oweals/gnunet.git] / src / ats / gnunet-ats-solver-eval.c
index 234492962bca0f164baf4a5440634daf83a0d636..412ed47d646fcd869b678418d4bc00f6db5022b2 100644 (file)
@@ -60,6 +60,11 @@ static int opt_plot;
  */
 static int opt_verbose;
 
+/**
+ * cmd option -p: print logs
+ */
+static int opt_print;
+
 static int res;
 
 static void
@@ -133,14 +138,28 @@ find_address_by_id (struct TestPeer *peer, int aid)
  * Logging
  */
 
+void
+GNUNET_ATS_solver_logging_now (struct LoggingHandle *l)
+{
+  struct LoggingTimeStep *lts;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging\n");
+
+  lts = GNUNET_new (struct LoggingTimeStep);
+  lts->timestamp = GNUNET_TIME_absolute_get();
+
+  /* Store logging data here */
+
+  GNUNET_CONTAINER_DLL_insert_tail(l->head, l->tail, lts);
+
+}
+
 static void
 logging_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct LoggingHandle *l = cls;
-
   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging\n");
+  GNUNET_ATS_solver_logging_now (l);
 
   l->logging_task = GNUNET_SCHEDULER_add_delayed (l->log_freq, &logging_task, l);
 
@@ -163,12 +182,6 @@ GNUNET_ATS_solver_logging_start (struct GNUNET_TIME_Relative freq)
   return l;
 }
 
-void
-GNUNET_ATS_solver_logging_now (struct LoggingHandle *l)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging\n");
-}
-
 void
 GNUNET_ATS_solver_logging_stop (struct LoggingHandle *l)
 {
@@ -183,15 +196,32 @@ GNUNET_ATS_solver_logging_stop (struct LoggingHandle *l)
 void
 GNUNET_ATS_solver_logging_eval (struct LoggingHandle *l)
 {
+  struct LoggingTimeStep *lts;
 
+  for (lts = l->head; NULL != lts; lts = lts->next)
+  {
+    fprintf (stderr, "Log %llu: \n", (long long unsigned int) lts->timestamp.abs_value_us);
+  }
 }
 
 void
 GNUNET_ATS_solver_logging_free (struct LoggingHandle *l)
 {
+  struct LoggingTimeStep *cur;
+  struct LoggingTimeStep *next;
+
   if (GNUNET_SCHEDULER_NO_TASK != l->logging_task)
     GNUNET_SCHEDULER_cancel (l->logging_task);
   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
+
+  next = l->head;
+  while (NULL != (cur = next))
+  {
+    next = cur->next;
+    GNUNET_CONTAINER_DLL_remove (l->head, l->tail, cur);
+    GNUNET_free (cur);
+  }
+
   GNUNET_free (l);
 }
 
@@ -296,7 +326,6 @@ set_prop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       pg->test_address->ats_addr, &atsi, 1);
   sh->env.sf.s_bulk_stop (sh->solver);
 
-
   switch (pg->ats_property) {
     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
       //p->pref_bandwidth = pref_value;
@@ -345,17 +374,20 @@ GNUNET_ATS_solver_generate_property_stop (struct PropertyGenerator *pg)
 
 
 /**
- * Generate between the source master and the partner and set preferences with a
+ * Generate between the source master and the partner and set property with a
  * value depending on the generator.
  *
- * @param src source
- * @param dest partner
- * @param type type of preferences to generate
- * @param base_rate traffic base rate to send data with
- * @param max_rate  traffic maximum rate to send data with
- * @param period duration of a period of traffic generation (~ 1/frequency)
- * @param duration how long to generate traffic
- * @return the traffic generator
+ * @param peer source
+ * @param address_id partner
+ * @param test_peer the peer
+ * @param test_address the address
+ * @param type type of generator
+ * @param base_value base value
+ * @param value_rate maximum value
+ * @param period duration of a period of generation (~ 1/frequency)
+ * @param frequency how long to generate property
+ * @param ats_property ATS property to generate
+ * @return the property generator
  */
 struct PropertyGenerator *
 GNUNET_ATS_solver_generate_property_start (unsigned int peer,
@@ -515,20 +547,29 @@ set_pref_task (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PreferenceGenerator *pg = cls;
+  struct TestPeer *p;
   double pref_value;
   pg->set_task = GNUNET_SCHEDULER_NO_TASK;
 
+  if (NULL == (p = find_peer_by_id (pg->peer)))
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+        "Setting preference for unknown peer %u\n", pg->peer);
+    return;
+  }
+
   pref_value = get_preference (pg);
 
   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-      "Setting preference for peer [%u] address [%u] for %s to %f\n",
-      pg->peer, pg->address_id,
+      "Setting preference for peer [%u] address [%u] for client %p pref %s to %f\n",
+      pg->peer, pg->address_id, NULL + (pg->client_id),
       GNUNET_ATS_print_preference_type (pg->kind), pref_value);
 
-  /* set performance here!
-  GNUNET_ATS_performance_change_preference(p->me->ats_perf_handle,
-      &p->dest->id, p->pg->kind, pref_value, GNUNET_ATS_PREFERENCE_END);
-*/
+  sh->env.sf.s_bulk_start (sh->solver);
+  GAS_normalization_normalize_preference (NULL + (pg->client_id), &p->peer_id,
+      pg->kind, pref_value);
+  sh->env.sf.s_bulk_stop (sh->solver);
 
   switch (pg->kind) {
     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
@@ -576,21 +617,24 @@ GNUNET_ATS_solver_generate_preferences_stop (struct PreferenceGenerator *pg)
 
 
 /**
- * Generate between the source master and the partner and set preferences with a
+ * Generate between the source master and the partner and set property with a
  * value depending on the generator.
  *
- * @param src source
- * @param dest partner
- * @param type type of preferences to generate
- * @param base_rate traffic base rate to send data with
- * @param max_rate  traffic maximum rate to send data with
- * @param period duration of a period of traffic generation (~ 1/frequency)
- * @param duration how long to generate traffic
- * @return the traffic generator
+ * @param peer source
+ * @param address_id partner
+ * @param client_id the client
+ * @param type type of generator
+ * @param base_value base value
+ * @param value_rate maximum value
+ * @param period duration of a period of generation (~ 1/frequency)
+ * @param frequency how long to generate property
+ * @param kind ATS preference to generate
+ * @return the preference generator
  */
 struct PreferenceGenerator *
 GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
     unsigned int address_id,
+    unsigned int client_id,
     enum GeneratorType type,
     long int base_value,
     long int value_rate,
@@ -605,6 +649,7 @@ GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
   pg->type = type;
   pg->peer = peer;
   pg->address_id = address_id;
+  pg->client_id = client_id;
   pg->kind = kind;
   pg->base_value = base_value;
   pg->max_value = value_rate;
@@ -960,6 +1005,18 @@ load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
   }
   GNUNET_free (op_name);
 
+  /* address id */
+  GNUNET_asprintf(&op_name, "op-%u-client-id", op_counter);
+  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
+      sec_name, op_name, &o->client_id))
+  {
+    fprintf (stderr, "Missing client-id in operation %u `%s' in episode `%s'\n",
+        op_counter, "START_SET_PREFERENCE", op_name);
+    GNUNET_free (op_name);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_free (op_name);
+
   /* generator */
   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
@@ -1365,7 +1422,6 @@ load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
           op_counter, op_name, e->id);
       GNUNET_free (op_name);
-      GNUNET_free (pref);
       GNUNET_free_non_null (pref);
       return GNUNET_SYSERR;
   }
@@ -1483,7 +1539,6 @@ load_episode (struct Experiment *e, struct Episode *cur,
       o->type = SOLVER_OP_START_SET_PREFERENCE;
       res =  load_op_start_set_preference (o, cur,
           op_counter, sec_name, cfg);
-        break;
     }
     else if (0 == strcmp (op, "stop_set_preference"))
     {
@@ -1637,10 +1692,11 @@ enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
   }
 
-  if (NULL != (a = find_address_by_id (p, op->address_id)))
+  if (NULL != (find_address_by_id (p, op->address_id)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
         op->address_id, op->peer_id);
+    return;
   }
 
   a = GNUNET_new (struct TestAddress);
@@ -1665,7 +1721,6 @@ static void
 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
 {
   struct TestPeer *p;
-  struct TestAddress *a;
   struct AddressLookupCtx ctx;
 
   if (NULL == (p = find_peer_by_id (op->peer_id)))
@@ -1689,14 +1744,13 @@ enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
     return;
   }
 
-  if (NULL == (a = find_address_by_id (p, op->address_id)))
+  if (NULL == (find_address_by_id (p, op->address_id)))
   {
     GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
         "Deleting address for unknown peer %u\n", op->peer_id);
     return;
   }
-  GNUNET_CONTAINER_DLL_remove (p->addr_head, p->addr_tail, a);
 
   GNUNET_CONTAINER_multipeermap_remove (sh->addresses, &p->peer_id, ctx.res);
 
@@ -1733,7 +1787,7 @@ enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
   {
     GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-        "Setting proprterty for unknown address %u\n", op->peer_id);
+        "Setting property for unknown address %u\n", op->peer_id);
     return;
   }
 
@@ -1767,8 +1821,17 @@ enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
     GNUNET_free (pg);
   }
 
+  if (NULL == (find_peer_by_id (op->peer_id)))
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+        "Starting preference generation for unknown peer %u\n", op->peer_id);
+    return;
+  }
+
   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
     op->address_id,
+    op->client_id,
     op->gen_type,
     op->base_rate,
     op->max_rate,
@@ -2068,7 +2131,8 @@ free_all_it (void *cls,
     void *value)
 {
   struct ATS_Address *address = value;
-  GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses, key, value);
+  GNUNET_break (GNUNET_OK != GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
+      key, value));
   GNUNET_free (address);
 
   return GNUNET_OK;
@@ -2193,6 +2257,7 @@ GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
 /**
  * Information callback for the solver
  *
+ * @param cls the closure
  * @param op the solver operation
  * @param stat status of the solver operation
  * @param add additional solver information
@@ -2335,6 +2400,21 @@ normalized_property_changed_cb (void *cls, struct ATS_Address *address,
 }
 
 
+static void
+normalized_preference_changed_cb (void *cls,
+    const struct GNUNET_PeerIdentity *peer,
+    enum GNUNET_ATS_PreferenceKind kind,
+    double pref_rel)
+{
+  GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+      "Normalized preference %s for peer `%s' changed to %.3f \n",
+      GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer),
+      pref_rel);
+
+  sh->env.sf.s_pref (sh->solver, peer, kind, pref_rel);
+}
+
+
 struct SolverHandle *
 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
 {
@@ -2374,7 +2454,8 @@ GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
 
 
   /* start normalization */
-  GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
+  GAS_normalization_start (&normalized_preference_changed_cb, NULL,
+      &normalized_property_changed_cb, NULL );
 
   /* load quotas */
   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
@@ -2425,6 +2506,9 @@ done ()
     e = NULL;
   }
 
+  if (opt_print)
+    GNUNET_ATS_solver_logging_eval (l);
+
   if (NULL != l)
   {
     GNUNET_ATS_solver_logging_free (l);
@@ -2445,7 +2529,6 @@ done ()
     }
     GNUNET_free (cur);
   }
-
   if (NULL != sh)
   {
     GNUNET_ATS_solvers_solver_stop (sh);
@@ -2453,7 +2536,6 @@ done ()
   }
   /* Shutdown */
   end_now();
-
 }
 
 static void
@@ -2465,19 +2547,6 @@ experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,i
   else
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
 
-
-  /*
-  evaluate (duration);
-  if (opt_log)
-    GNUNET_ATS_TEST_logging_write_to_file(l, opt_exp_file, opt_plot);
-
-  if (NULL != l)
-  {
-    GNUNET_ATS_TEST_logging_stop (l);
-    GNUNET_ATS_TEST_logging_clean_up (l);
-    l = NULL;
-  }
-  */
   GNUNET_SCHEDULER_add_now (&done, NULL);
 }
 
@@ -2607,6 +2676,9 @@ main (int argc, char *argv[])
     {  'e', "experiment", NULL,
       gettext_noop ("experiment to use"),
       1, &GNUNET_GETOPT_set_one, &opt_verbose},
+    {  'p', "print", NULL,
+      gettext_noop ("print logging"),
+      0, &GNUNET_GETOPT_set_one, &opt_print},
     GNUNET_GETOPT_OPTION_END
   };