move SET to use new MQ API properly
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
index f1b7b4b14bc0290e985774c97aac0cfe393f3e3b..f89817ff57da9dbbe7c7be9d19c56db18766c77e 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 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.
 */
 
 /**
@@ -25,6 +25,7 @@
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_statistics_service.h"
 #include "gnunet_set_service.h"
 #include "gnunet_testbed_service.h"
 
@@ -57,6 +58,23 @@ static struct GNUNET_PeerIdentity local_peer;
 
 static struct GNUNET_SET_ListenHandle *set_listener;
 
+/**
+ * Handle to the statistics service.
+ */
+static struct GNUNET_STATISTICS_Handle *statistics;
+
+/**
+ * The profiler will write statistics
+ * for all peers to the file with this name.
+ */
+static char *statistics_filename;
+
+/**
+ * The profiler will write statistics
+ * for all peers to this file.
+ */
+static FILE *statistics_file;
+
 
 static int
 map_remove_iterator (void *cls,
@@ -76,6 +94,42 @@ map_remove_iterator (void *cls,
 }
 
 
+/**
+ * Callback function to process statistic values.
+ *
+ * @param cls closure
+ * @param subsystem name of subsystem that created the statistic
+ * @param name the name of the datum
+ * @param value the current value
+ * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
+ * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
+ */
+static int
+statistics_result (void *cls,
+                   const char *subsystem,
+                   const char *name,
+                   uint64_t value,
+                   int is_persistent)
+{
+  if (NULL != statistics_file)
+  {
+    fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long) value);
+  }
+  return GNUNET_OK;
+}
+
+
+static void
+statistics_done (void *cls,
+                 int success)
+{
+  GNUNET_assert (GNUNET_YES == success);
+  if (NULL != statistics_file)
+    fclose (statistics_file);
+  GNUNET_SCHEDULER_shutdown ();
+}
+
+
 static void
 check_all_done (void)
 {
@@ -88,16 +142,26 @@ check_all_done (void)
   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
 
-  GNUNET_SCHEDULER_shutdown ();
+  if (NULL == statistics_filename)
+  {
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+
+  statistics_file = fopen (statistics_filename, "w");
+  GNUNET_STATISTICS_get (statistics, NULL, NULL,
+                         &statistics_done,
+                         &statistics_result, NULL);
 }
 
 
 static void
 set_result_cb (void *cls,
-                 const struct GNUNET_SET_Element *element,
-                 enum GNUNET_SET_Status status)
+               const struct GNUNET_SET_Element *element,
+               enum GNUNET_SET_Status status)
 {
   struct SetInfo *info = cls;
+  struct GNUNET_HashCode hash;
 
   GNUNET_assert (GNUNET_NO == info->done);
   switch (status)
@@ -114,15 +178,25 @@ set_result_cb (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
       GNUNET_SCHEDULER_shutdown ();
       return;
-    case GNUNET_SET_STATUS_OK:
+    case GNUNET_SET_STATUS_ADD_LOCAL:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
       break;
+    case GNUNET_SET_STATUS_ADD_REMOTE:
+      GNUNET_CRYPTO_hash (element->data, element->size, &hash);
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
+                  GNUNET_h2s (&hash));
+      // XXX: record and check
+      return;
     default:
       GNUNET_assert (0);
   }
 
   if (element->size != sizeof (struct GNUNET_HashCode))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u\n", element->size);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "wrong element size: %u, expected %u\n",
+                element->size,
+                (unsigned int) sizeof (struct GNUNET_HashCode));
     GNUNET_assert (0);
   }
 
@@ -143,18 +217,19 @@ set_listen_cb (void *cls,
 {
   if (NULL == request)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "listener failed\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "listener failed\n");
     return;
   }
   GNUNET_assert (NULL == info2.oh);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set listen cb called\n");
-  info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "set listen cb called\n");
+  info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
                                set_result_cb, &info2);
   GNUNET_SET_commit (info2.oh, info2.set);
 }
 
 
-
 static int
 set_insert_iterator (void *cls,
                      const struct GNUNET_HashCode *key,
@@ -163,9 +238,10 @@ set_insert_iterator (void *cls,
   struct GNUNET_SET_Handle *set = cls;
   struct GNUNET_SET_Element *el;
 
-  el = GNUNET_malloc (sizeof *el + sizeof *key);
-  el->type = 0;
-  memcpy (&el[1], key, sizeof *key);
+  el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
+                      sizeof (struct GNUNET_HashCode));
+  el->element_type = 0;
+  GNUNET_memcpy (&el[1], key, sizeof *key);
   el->data = &el[1];
   el->size = sizeof *key;
   GNUNET_SET_add_element (set, el, NULL, NULL);
@@ -175,9 +251,10 @@ set_insert_iterator (void *cls,
 
 
 static void
-handle_shutdown (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+handle_shutdown (void *cls)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Shutting down set profiler\n");
   if (NULL != set_listener)
   {
     GNUNET_SET_listen_cancel (set_listener);
@@ -203,11 +280,14 @@ handle_shutdown (void *cls,
     GNUNET_SET_destroy (info2.set);
     info2.set = NULL;
   }
+  GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
 }
 
+
 static void
-run (void *cls, char *const *args, const char *cfgfile,
-     const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls,
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_TESTING_Peer *peer)
 {
   unsigned int i;
   struct GNUNET_HashCode hash;
@@ -221,7 +301,9 @@ run (void *cls, char *const *args, const char *cfgfile,
     return;
   }
 
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
+  statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
+
+  GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
 
   info1.id = "a";
   info2.id = "b";
@@ -254,8 +336,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
   }
 
-  /* use last hash for app id */
-  app_id = hash;
+  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
 
   /* FIXME: also implement intersection etc. */
   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
@@ -270,7 +351,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                                     &app_id, set_listen_cb, NULL);
 
   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
-                                 GNUNET_SET_RESULT_ADDED,
+                                 GNUNET_SET_RESULT_SYMMETRIC,
                                  set_result_cb, &info1);
   GNUNET_SET_commit (info1.oh, info1.set);
   GNUNET_SET_destroy (info1.set);
@@ -278,6 +359,17 @@ run (void *cls, char *const *args, const char *cfgfile,
 }
 
 
+static void
+pre_run (void *cls, char *const *args, const char *cfgfile,
+         const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  if (0 != GNUNET_TESTING_peer_run ("set-profiler",
+                                    cfgfile,
+                                    &run, NULL))
+    ret = 2;
+}
+
+
 int
 main (int argc, char **argv)
 {
@@ -292,13 +384,15 @@ main (int argc, char **argv)
         gettext_noop ("number of values"),
         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
       { 'x', "operation", NULL,
-        gettext_noop ("oeration to execute"),
+        gettext_noop ("operation to execute"),
         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
+      { 's', "statistics", NULL,
+        gettext_noop ("write statistics to file"),
+        GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
       GNUNET_GETOPT_OPTION_END
   };
-  GNUNET_PROGRAM_run (argc, argv, "gnunet-set-profiler",
-                      "help",
-                      options, &run, NULL);
+  GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
+                     "help",
+                     options, &pre_run, NULL, GNUNET_YES);
   return ret;
 }
-