-typo-doxygen
[oweals/gnunet.git] / src / testbed / testbed_api_statistics.c
index ec0f126b15ac3fcc5a02944a468a4ce490ef6040..ae5fc131e5b60d041f6edcefe7184b29150f3259 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--2013 Christian Grothoff (and other contributing authors)
 
       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.
  */
 
 /**
@@ -25,7 +25,7 @@
  * @author Sree Harsha Totakura
  */
 #include "platform.h"
-#include "gnunet_common.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_testbed_service.h"
 
 #include "testbed_api_operations.h"
@@ -65,6 +65,16 @@ struct GetStatsContext
    */
   struct GNUNET_TESTBED_Peer **peers;
 
+  /**
+   * The subsystem of peers for which statistics are requested
+   */
+  char *subsystem;
+
+  /**
+   * The particular statistics value of interest
+   */
+  char *name;
+
   /**
    * The iterator to call with statistics information
    */
@@ -84,8 +94,8 @@ struct GetStatsContext
   /**
    * The task for calling the continuation callback
    */
-  GNUNET_SCHEDULER_TaskIdentifier call_completion_task_id;
-  
+  struct GNUNET_SCHEDULER_Task * call_completion_task_id;
+
   /**
    * The number of peers present in the peers array.  This number also
    * represents the number of service connect operations in the ops array
@@ -118,7 +128,7 @@ struct PeerGetStatsContext
   /**
    * Task to mark the statistics service connect operation as done
    */
-  GNUNET_SCHEDULER_TaskIdentifier op_done_task_id;
+  struct GNUNET_SCHEDULER_Task * op_done_task_id;
 
   /**
    * The index of this peer in the peers array of GetStatsContext
@@ -147,8 +157,8 @@ call_completion_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GetStatsContext *sc = cls;
 
-  GNUNET_assert (sc->call_completion_task_id != GNUNET_SCHEDULER_NO_TASK);
-  sc->call_completion_task_id = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_assert (sc->call_completion_task_id != NULL);
+  sc->call_completion_task_id = NULL;
   LOG_DEBUG ("Calling get_statistics() continuation callback\n");
   sc->cont (sc->cb_cls, sc->main_op, NULL);
 }
@@ -169,8 +179,8 @@ op_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_TESTBED_Operation **op;
 
   sc = peer_sc->sc;
-  peer_sc->op_done_task_id = GNUNET_SCHEDULER_NO_TASK;
-  op = &sc->ops[peer_sc->peer_index];  
+  peer_sc->op_done_task_id = NULL;
+  op = &sc->ops[peer_sc->peer_index];
   GNUNET_assert (NULL != *op);
   GNUNET_TESTBED_operation_done (*op);
   *op = NULL;
@@ -190,16 +200,9 @@ iteration_completion_cb (void *cls, int success)
   struct PeerGetStatsContext *peer_sc = cls;
   struct GetStatsContext *sc;
 
-  if (NULL == peer_sc->get_handle)
-  {
-    /* We are being called synchronously in call to GNUNET_STATISTICS_destroy()
-       in statistics_da() after we cancelled the GetHandle */
-    GNUNET_assert (GNUNET_SYSERR == success);
-    return;
-  }
   GNUNET_break (GNUNET_OK == success);
   sc = peer_sc->sc;
-  peer_sc->get_handle = NULL; 
+  peer_sc->get_handle = NULL;
   sc->num_completed++;
   peer_sc->op_done_task_id = GNUNET_SCHEDULER_add_now (&op_done_task, peer_sc);
   if (sc->num_completed == sc->num_peers)
@@ -236,7 +239,7 @@ iterator_cb (void *cls, const char *subsystem,
   LOG_DEBUG ("Peer %u: [%s,%s] -> %lu\n", peer_sc->peer_index,
              subsystem, name, (unsigned long) value);
   ret = sc->proc (sc->cb_cls, peer,
-                  subsystem, name, value, is_persistent);  
+                  subsystem, name, value, is_persistent);
   if (GNUNET_SYSERR == ret)
     LOG_DEBUG ("Aborting iteration for peer %u\n", peer_sc->peer_index);
   return ret;
@@ -260,10 +263,10 @@ service_connect_comp (void *cls,
 {
   struct PeerGetStatsContext *peer_sc = cls;
   struct GNUNET_STATISTICS_Handle *h = ca_result;
-  
+
   LOG_DEBUG ("Retrieving statistics of peer %u\n", peer_sc->peer_index);
   peer_sc->get_handle =
-      GNUNET_STATISTICS_get (h, NULL, NULL,
+      GNUNET_STATISTICS_get (h, peer_sc->sc->subsystem, peer_sc->sc->name,
                              GNUNET_TIME_UNIT_FOREVER_REL,
                              &iteration_completion_cb,
                              iterator_cb, peer_sc);
@@ -280,11 +283,11 @@ service_connect_comp (void *cls,
  *          from GNUNET_TESTBED_service_connect()
  * @return service handle to return in 'op_result', NULL on error
  */
-static void * 
+static void *
 statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct PeerGetStatsContext *peer_sc = cls;
-  
+
   LOG_DEBUG ("Connecting to statistics service of peer %u\n",
              peer_sc->peer_index);
   return GNUNET_STATISTICS_create ("<testbed-api>", cfg);
@@ -297,19 +300,19 @@ statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
  * @param cls the PeerGetStatsContext
  * @param op_result service handle returned from the connect adapter
  */
-static void 
+static void
 statistics_da (void *cls, void *op_result)
 {
   struct PeerGetStatsContext *peer_sc = cls;
+  struct GNUNET_STATISTICS_Handle *sh = op_result;
 
   if (NULL != peer_sc->get_handle)
   {
     GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
     peer_sc->get_handle = NULL;
   }
-  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) op_result,
-                             GNUNET_NO);
-  if (GNUNET_SCHEDULER_NO_TASK != peer_sc->op_done_task_id)
+  GNUNET_STATISTICS_destroy (sh, GNUNET_NO);
+  if (NULL != peer_sc->op_done_task_id)
     GNUNET_SCHEDULER_cancel (peer_sc->op_done_task_id);
   GNUNET_free (peer_sc);
 }
@@ -328,16 +331,19 @@ opstart_get_stats (void *cls)
   unsigned int peer;
 
   LOG_DEBUG ("Starting get_statistics operation\n");
-  sc->ops = GNUNET_malloc (sc->num_peers * 
+  sc->ops = GNUNET_malloc (sc->num_peers *
                            sizeof (struct GNUNET_TESTBED_Operation *));
   for (peer = 0; peer < sc->num_peers; peer++)
   {
     if (NULL == sc->peers[peer])
+    {
+      GNUNET_break (0);
       continue;
-    peer_sc = GNUNET_malloc (sizeof (struct PeerGetStatsContext));
+    }
+    peer_sc = GNUNET_new (struct PeerGetStatsContext);
     peer_sc->sc = sc;
     peer_sc->peer_index = peer;
-    sc->ops[peer] = 
+    sc->ops[peer] =
         GNUNET_TESTBED_service_connect (sc, sc->peers[peer], "statistics",
                                         &service_connect_comp,
                                         peer_sc,
@@ -358,9 +364,9 @@ oprelease_get_stats (void *cls)
 {
   struct GetStatsContext *sc = cls;
   unsigned int peer;
-  
+
   LOG_DEBUG ("Cleaning up get_statistics operation\n");
-  if (GNUNET_SCHEDULER_NO_TASK != sc->call_completion_task_id)
+  if (NULL != sc->call_completion_task_id)
     GNUNET_SCHEDULER_cancel (sc->call_completion_task_id);
   if (NULL != sc->ops)
   {
@@ -374,8 +380,10 @@ oprelease_get_stats (void *cls)
     }
     GNUNET_free (sc->ops);
   }
+  GNUNET_free_non_null (sc->subsystem);
+  GNUNET_free_non_null (sc->name);
   GNUNET_free (sc);
-  if (GNUNET_YES == 
+  if (GNUNET_YES ==
       GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
     no_wait_queue = NULL;
 }
@@ -387,6 +395,8 @@ oprelease_get_stats (void *cls)
  *
  * @param num_peers number of peers to iterate over
  * @param peers array of peers to iterate over
+ * @param subsystem limit to the specified subsystem, NULL for all subsystems
+ * @param name name of the statistic value, NULL for all values
  * @param proc processing function for each statistic retrieved
  * @param cont continuation to call once call is completed(?)
  * @param cls closure to pass to proc and cont
@@ -395,6 +405,7 @@ oprelease_get_stats (void *cls)
 struct GNUNET_TESTBED_Operation *
 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
                                struct GNUNET_TESTBED_Peer **peers,
+                               const char *subsystem, const char *name,
                                GNUNET_TESTBED_StatisticsIterator proc,
                                GNUNET_TESTBED_OperationCompletionCallback cont,
                                void *cls)
@@ -404,10 +415,12 @@ GNUNET_TESTBED_get_statistics (unsigned int num_peers,
   GNUNET_assert (NULL != proc);
   GNUNET_assert (NULL != cont);
   if (NULL == no_wait_queue)
-    no_wait_queue =
-        GNUNET_TESTBED_operation_queue_create_ (UINT_MAX);
-  sc = GNUNET_malloc (sizeof (struct GetStatsContext));
+    no_wait_queue = GNUNET_TESTBED_operation_queue_create_
+        (OPERATION_QUEUE_TYPE_FIXED, UINT_MAX);
+  sc = GNUNET_new (struct GetStatsContext);
   sc->peers = peers;
+  sc->subsystem = (NULL == subsystem) ? NULL : GNUNET_strdup (subsystem);
+  sc->name = (NULL == name) ? NULL : GNUNET_strdup (name);
   sc->proc = proc;
   sc->cont = cont;
   sc->cb_cls = cls;