move SET to use new MQ API properly
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
index 4b6a1e202a59cc176f8d6a63fcf05fa6340a3096..f89817ff57da9dbbe7c7be9d19c56db18766c77e 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      Copyright (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
@@ -36,8 +36,6 @@ static unsigned int num_a = 5;
 static unsigned int num_b = 5;
 static unsigned int num_c = 20;
 
-static unsigned int dump_statistics;
-
 static char *op_str = "union";
 
 const static struct GNUNET_CONFIGURATION_Handle *config;
@@ -60,8 +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,
@@ -98,7 +111,10 @@ statistics_result (void *cls,
                    uint64_t value,
                    int is_persistent)
 {
-  printf ("stat %s/%s=%lu\n", subsystem, name, (unsigned long) value);
+  if (NULL != statistics_file)
+  {
+    fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long) value);
+  }
   return GNUNET_OK;
 }
 
@@ -108,10 +124,12 @@ statistics_done (void *cls,
                  int success)
 {
   GNUNET_assert (GNUNET_YES == success);
-  printf("dumped statistics\n");
+  if (NULL != statistics_file)
+    fclose (statistics_file);
   GNUNET_SCHEDULER_shutdown ();
 }
 
+
 static void
 check_all_done (void)
 {
@@ -124,21 +142,23 @@ 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));
 
-  if (0 == dump_statistics)
+  if (NULL == statistics_filename)
   {
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
 
-  GNUNET_STATISTICS_get (statistics, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL,
-                         statistics_done, statistics_result, NULL);
+  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;
@@ -173,7 +193,10 @@ set_result_cb (void *cls,
 
   if (element->size != sizeof (struct GNUNET_HashCode))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u, expected %u\n", element->size, sizeof (struct GNUNET_HashCode));
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "wrong element size: %u, expected %u\n",
+                element->size,
+                (unsigned int) sizeof (struct GNUNET_HashCode));
     GNUNET_assert (0);
   }
 
@@ -218,7 +241,7 @@ set_insert_iterator (void *cls,
   el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
                       sizeof (struct GNUNET_HashCode));
   el->element_type = 0;
-  memcpy (&el[1], key, sizeof *key);
+  GNUNET_memcpy (&el[1], key, sizeof *key);
   el->data = &el[1];
   el->size = sizeof *key;
   GNUNET_SET_add_element (set, el, NULL, NULL);
@@ -228,8 +251,7 @@ 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");
@@ -281,7 +303,7 @@ run (void *cls,
 
   statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
 
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
+  GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
 
   info1.id = "a";
   info2.id = "b";
@@ -365,8 +387,8 @@ main (int argc, char **argv)
         gettext_noop ("operation to execute"),
         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
       { 's', "statistics", NULL,
-        gettext_noop ("dump statistics to stdout after completion"),
-        GNUNET_NO, &GNUNET_GETOPT_set_one, &dump_statistics },
+        gettext_noop ("write statistics to file"),
+        GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
       GNUNET_GETOPT_OPTION_END
   };
   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
@@ -374,4 +396,3 @@ main (int argc, char **argv)
                      options, &pre_run, NULL, GNUNET_YES);
   return ret;
 }
-