sensor: towards profiler
authorOmar Tarabai <tarabai@devegypt.com>
Mon, 1 Sep 2014 17:17:29 +0000 (17:17 +0000)
committerOmar Tarabai <tarabai@devegypt.com>
Mon, 1 Sep 2014 17:17:29 +0000 (17:17 +0000)
src/sensor/gnunet-sensor-profiler.c
src/sensor/profiler.py [new file with mode: 0644]

index 814413ba1b2bbf94088859932b77f067152b027e..e89e0f025212b6bce099ae9a71885170cf611333 100644 (file)
      Boston, MA 02111-1307, USA.
 */
 
-/**
- * TODO:
- * - Run X peers
- * - Rewrite interval time (optional)
- * - Run 1 dashboard
- * - Monitor dashboard records
- * - Prompt for anomalies when ready:
- *  -- Cut Y peers (remove their connections to other X-Y peers but not the connections among themselves)
- */
-
 /**
  * @file sensor/gnunet-sensor-profiler.c
  * @brief Profiler for the sensor service
 #include "gnunet_sensor_service.h"
 #include "gnunet_sensor_util_lib.h"
 
+/**
+ * Time to wait for the peer to startup completely
+ */
+#define PEER_STARTUP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
+
 /**
  * Information about a single peer
  */
@@ -150,7 +145,7 @@ copy_dir (const char *src, const char *dst);
  * Do clean up and shutdown scheduler
  */
 static void
-do_shutdown ()                  // TODO: schedule timeout shutdown
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int i;
 
@@ -308,7 +303,6 @@ dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
   GNUNET_TESTBED_operation_done (op);
   dashboard_service_started = GNUNET_YES;
-  //TODO:
 }
 
 
@@ -417,6 +411,18 @@ peerstore_disconnect_adapter (void *cls, void *op_result)
 }
 
 
+/**
+ * This function is called after a delay which ensures that all peers are
+ * properly initialized
+ */
+static void
+peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers are ready.\n");
+  //TODO
+}
+
+
 /**
  * Callback to be called when sensor service is started
  *
@@ -440,9 +446,12 @@ sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
               GNUNET_i2s (&peer->peer_id));
   GNUNET_TESTBED_operation_done (op);
   sensor_services_started++;
-  if (sensor_services_started == num_peers)     //TODO: remove
-    do_shutdown ();
-  //TODO
+  if (sensor_services_started == num_peers)
+  {
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                  (PEER_STARTUP_TIME, num_peers), &peers_ready,
+                                  NULL);
+  }
 }
 
 
@@ -575,7 +584,7 @@ run (void *cls, char *const *args, const char *cf,
 
   if (GNUNET_OK != verify_args ())
   {
-    do_shutdown ();
+    do_shutdown (NULL, NULL);
     return;
   }
   cfg = GNUNET_CONFIGURATION_create ();
@@ -584,6 +593,8 @@ run (void *cls, char *const *args, const char *cf,
                                          cfg, "TESTBED",
                                          "OVERLAY_TOPOLOGY_FILE",
                                          topology_file);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
+                                NULL);
   GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
   GNUNET_CONFIGURATION_destroy (cfg);
 }
diff --git a/src/sensor/profiler.py b/src/sensor/profiler.py
new file mode 100644 (file)
index 0000000..ce3d684
--- /dev/null
@@ -0,0 +1,57 @@
+import argparse
+import math
+import networkx
+import random
+import tempfile
+
+def get_args():
+  parser = argparse.ArgumentParser(description="Sensor profiler")
+  parser.add_argument('-p', '--peers', action='store', type=int, required=True,
+                      help='Number of peers to run')
+  return parser.parse_args()
+
+def generate_topology(peers, links):
+  G = networkx.empty_graph(peers)
+  for i in range(0, links):
+    a = 0
+    b = 0
+    while a == b:
+      a = random.randint(0, peers)
+      b = random.randint(0, peers)
+    G.add_edge(a, b)
+  return G
+
+def create_topology_file(graph):
+  nodes = list()
+  for i in range(len(graph.edge)):
+    nodes.append(list())
+  for e in graph.edges():
+    nodes[e[0]].append(e[1])
+  print nodes
+  f = tempfile.NamedTemporaryFile(delete=False)
+  for i in range(len(nodes)):
+    if len(nodes[i]) == 0:
+      continue
+    f.write('%d:' % i)
+    f.write('|'.join(map(str,nodes[i])))
+    f.write('\n')
+  #f.close()
+  return f.name
+
+def main():
+  args = vars(get_args())
+  num_peers = args['peers']
+  if num_peers < 3:
+    print 'Min number of peers is 3'
+    return
+  num_links = int(math.log(num_peers) * math.log(num_peers) * num_peers / 2)
+  # Generate random topology
+  graph = generate_topology(num_peers, num_links)
+  print 'Generated random topology with %d peers and %d links' % (num_peers, num_links)
+  # Create TESTBED topology file
+  top_file = create_topology_file(graph)
+  print 'Created TESTBED topology file %s' % top_file
+  # Run c profiler
+  
+if __name__ == "__main__":
+  main()