From 3e36950d8d3f6319544b9d627156c70eaaf9bdaf Mon Sep 17 00:00:00 2001 From: Omar Tarabai Date: Wed, 3 Sep 2014 15:01:22 +0000 Subject: sensor profiler complete --- src/sensor/gnunet-sensor-profiler.c | 25 ++++++++------ src/sensor/profiler.py | 68 ++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 18 deletions(-) (limited to 'src/sensor') diff --git a/src/sensor/gnunet-sensor-profiler.c b/src/sensor/gnunet-sensor-profiler.c index ad8b43290..467c80dee 100644 --- a/src/sensor/gnunet-sensor-profiler.c +++ b/src/sensor/gnunet-sensor-profiler.c @@ -52,6 +52,11 @@ struct PeerInfo */ struct GNUNET_TESTBED_Peer *testbed_peer; + /** + * Index of this peer within our list + */ + int index; + }; struct DisconnectionContext @@ -261,8 +266,8 @@ transport_disconnect_cb (void *cls, const int result) struct DisconnectionContext *dc = cls; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Disconnection request between `%s' and `%s' sent.\n", - GNUNET_i2s (&dc->p1->peer_id), GNUNET_i2s (&dc->p2->peer_id)); + "Peer disconnection request sent: %d,%d\n", dc->p1->index, + dc->p2->index); } @@ -464,8 +469,8 @@ sensor_dir_scanner (void *cls, const char *filename) GNUNET_CONFIGURATION_parse (sensor_cfg, filename)); GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename, "COLLECTION_POINT", - GNUNET_i2s_full (&all_peers_info - [0].peer_id)); + GNUNET_i2s_full (&all_peers_info[0]. + peer_id)); if (sensors_interval > 0) { GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename, @@ -545,10 +550,10 @@ peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id, record->peer)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Anomaly report:\n" " Peer: `%s'\n" " Sensor: `%s'\n" - " Anomalous: `%d'\n" " Anomalous neighbors: %f.\n\n", - GNUNET_i2s (&peer->peer_id), record->key, anomaly->anomalous, - anomaly->anomalous_neighbors); + "Anomaly report:{'peerid': '%s'," "'peer': %d," "'sensor': '%s'," + "'anomalous': %d," "'neighbors': %f}\n", + GNUNET_i2s (&peer->peer_id), peer->index, record->key, + anomaly->anomalous, anomaly->anomalous_neighbors); return GNUNET_YES; } @@ -662,7 +667,7 @@ simulate_anomalies (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) delayed_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Training period over, simulating anomalies now.\n"); - //TODO: + prompt_peer_disconnection (); } @@ -695,7 +700,6 @@ peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_NO)); delayed_task = GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL); - prompt_peer_disconnection (); //TODO: move to simulate_anomalies() } @@ -756,6 +760,7 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, } peer->testbed_peer = testbed_peer; GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); + peer->index = peers_known; peers_known++; if (1 == peers_known) /* First peer is collection point */ { diff --git a/src/sensor/profiler.py b/src/sensor/profiler.py index 55187db1c..16af18643 100644 --- a/src/sensor/profiler.py +++ b/src/sensor/profiler.py @@ -5,8 +5,13 @@ import random import tempfile import os import time +import matplotlib.pyplot as plt from subprocess import Popen, PIPE, STDOUT +node_colors = None +graph = None +pos = None + def get_args(): parser = argparse.ArgumentParser(description="Sensor profiler") parser.add_argument('-p', '--peers', action='store', type=int, required=True, @@ -14,17 +19,22 @@ def get_args(): return parser.parse_args() def generate_topology(peers, links): - G = networkx.empty_graph(peers) + global graph + global node_colors + global pos + graph = 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 + a = random.randint(0, peers - 1) + b = random.randint(0, peers - 1) + graph.add_edge(a, b) + node_colors = [0] * peers + pos = networkx.layout.spring_layout(graph) -def create_topology_file(graph): +def create_topology_file(): + global graph nodes = list() for i in range(len(graph.edge)): nodes.append(list()) @@ -41,10 +51,51 @@ def create_topology_file(graph): # f.close() return f.name +def draw_graph(): + global graph + global node_colors + global pos + t = int(time.time()) + inc = 2 + name = str(t) + '.png' + while os.path.exists(name): + name = '%d(%d).png' % (t, inc) + inc += 1 + print 'Drawing graph to file: %s' % name + plt.clf() + networkx.draw(graph, pos=pos, node_color=node_colors, with_labels=range(len(graph.node)), cmap=plt.cm.Reds, vmin=0, vmax=2) + plt.savefig(name) + +def peers_disconnected(p1, p2): + global graph + print 'Disconnected peers %d and %d' % (p1, p2) + if p2 not in graph[p1]: + print 'Link does not exist' + return + graph.remove_edge(p1, p2) + draw_graph() + +def anomaly_report(report): + global node_colors + if 0 == report['anomalous']: + node_colors[report['peer']] = 0 + else: + node_colors[report['peer']] = 1 + report['neighbors'] + draw_graph() + def handle_profiler_line(line): if not line: return print line + if 'Peer disconnection request sent' in line: # Peers disconnected + parts = line.split(':') + peers = parts[-1].split(',') + peers_disconnected(int(peers[0]), int(peers[1])) + return + if 'Anomaly report:' in line: + parts = line.split('Anomaly report:') + anomaly_report(eval(parts[1])) + return def run_profiler(peers, topology_file): cmd = "GNUNET_FORCE_LOG='gnunet-sensor-profiler;;;;DEBUG' gnunet-sensor-profiler -p %d -t %s > log 2>&1" % (peers, topology_file) @@ -69,11 +120,12 @@ def main(): 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) + 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) + top_file = create_topology_file() print 'Created TESTBED topology file %s' % top_file + draw_graph() # Run c profiler run_profiler(num_peers, top_file) -- cgit v1.2.3