diff options
-rw-r--r-- | src/sensor/gnunet-sensor-profiler.c | 25 | ||||
-rw-r--r-- | src/sensor/profiler.py | 68 |
2 files changed, 75 insertions, 18 deletions
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 | |||
52 | */ | 52 | */ |
53 | struct GNUNET_TESTBED_Peer *testbed_peer; | 53 | struct GNUNET_TESTBED_Peer *testbed_peer; |
54 | 54 | ||
55 | /** | ||
56 | * Index of this peer within our list | ||
57 | */ | ||
58 | int index; | ||
59 | |||
55 | }; | 60 | }; |
56 | 61 | ||
57 | struct DisconnectionContext | 62 | struct DisconnectionContext |
@@ -261,8 +266,8 @@ transport_disconnect_cb (void *cls, const int result) | |||
261 | struct DisconnectionContext *dc = cls; | 266 | struct DisconnectionContext *dc = cls; |
262 | 267 | ||
263 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 268 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
264 | "Disconnection request between `%s' and `%s' sent.\n", | 269 | "Peer disconnection request sent: %d,%d\n", dc->p1->index, |
265 | GNUNET_i2s (&dc->p1->peer_id), GNUNET_i2s (&dc->p2->peer_id)); | 270 | dc->p2->index); |
266 | } | 271 | } |
267 | 272 | ||
268 | 273 | ||
@@ -464,8 +469,8 @@ sensor_dir_scanner (void *cls, const char *filename) | |||
464 | GNUNET_CONFIGURATION_parse (sensor_cfg, filename)); | 469 | GNUNET_CONFIGURATION_parse (sensor_cfg, filename)); |
465 | GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename, | 470 | GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename, |
466 | "COLLECTION_POINT", | 471 | "COLLECTION_POINT", |
467 | GNUNET_i2s_full (&all_peers_info | 472 | GNUNET_i2s_full (&all_peers_info[0]. |
468 | [0].peer_id)); | 473 | peer_id)); |
469 | if (sensors_interval > 0) | 474 | if (sensors_interval > 0) |
470 | { | 475 | { |
471 | GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename, | 476 | GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename, |
@@ -545,10 +550,10 @@ peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, | |||
545 | GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id, | 550 | GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id, |
546 | record->peer)); | 551 | record->peer)); |
547 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 552 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
548 | "Anomaly report:\n" " Peer: `%s'\n" " Sensor: `%s'\n" | 553 | "Anomaly report:{'peerid': '%s'," "'peer': %d," "'sensor': '%s'," |
549 | " Anomalous: `%d'\n" " Anomalous neighbors: %f.\n\n", | 554 | "'anomalous': %d," "'neighbors': %f}\n", |
550 | GNUNET_i2s (&peer->peer_id), record->key, anomaly->anomalous, | 555 | GNUNET_i2s (&peer->peer_id), peer->index, record->key, |
551 | anomaly->anomalous_neighbors); | 556 | anomaly->anomalous, anomaly->anomalous_neighbors); |
552 | return GNUNET_YES; | 557 | return GNUNET_YES; |
553 | } | 558 | } |
554 | 559 | ||
@@ -662,7 +667,7 @@ simulate_anomalies (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
662 | delayed_task = GNUNET_SCHEDULER_NO_TASK; | 667 | delayed_task = GNUNET_SCHEDULER_NO_TASK; |
663 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 668 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
664 | "Training period over, simulating anomalies now.\n"); | 669 | "Training period over, simulating anomalies now.\n"); |
665 | //TODO: | 670 | prompt_peer_disconnection (); |
666 | } | 671 | } |
667 | 672 | ||
668 | 673 | ||
@@ -695,7 +700,6 @@ peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
695 | GNUNET_NO)); | 700 | GNUNET_NO)); |
696 | delayed_task = | 701 | delayed_task = |
697 | GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL); | 702 | GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL); |
698 | prompt_peer_disconnection (); //TODO: move to simulate_anomalies() | ||
699 | } | 703 | } |
700 | 704 | ||
701 | 705 | ||
@@ -756,6 +760,7 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, | |||
756 | } | 760 | } |
757 | peer->testbed_peer = testbed_peer; | 761 | peer->testbed_peer = testbed_peer; |
758 | GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); | 762 | GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); |
763 | peer->index = peers_known; | ||
759 | peers_known++; | 764 | peers_known++; |
760 | if (1 == peers_known) /* First peer is collection point */ | 765 | if (1 == peers_known) /* First peer is collection point */ |
761 | { | 766 | { |
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 | |||
5 | import tempfile | 5 | import tempfile |
6 | import os | 6 | import os |
7 | import time | 7 | import time |
8 | import matplotlib.pyplot as plt | ||
8 | from subprocess import Popen, PIPE, STDOUT | 9 | from subprocess import Popen, PIPE, STDOUT |
9 | 10 | ||
11 | node_colors = None | ||
12 | graph = None | ||
13 | pos = None | ||
14 | |||
10 | def get_args(): | 15 | def get_args(): |
11 | parser = argparse.ArgumentParser(description="Sensor profiler") | 16 | parser = argparse.ArgumentParser(description="Sensor profiler") |
12 | parser.add_argument('-p', '--peers', action='store', type=int, required=True, | 17 | parser.add_argument('-p', '--peers', action='store', type=int, required=True, |
@@ -14,17 +19,22 @@ def get_args(): | |||
14 | return parser.parse_args() | 19 | return parser.parse_args() |
15 | 20 | ||
16 | def generate_topology(peers, links): | 21 | def generate_topology(peers, links): |
17 | G = networkx.empty_graph(peers) | 22 | global graph |
23 | global node_colors | ||
24 | global pos | ||
25 | graph = networkx.empty_graph(peers) | ||
18 | for i in range(0, links): | 26 | for i in range(0, links): |
19 | a = 0 | 27 | a = 0 |
20 | b = 0 | 28 | b = 0 |
21 | while a == b: | 29 | while a == b: |
22 | a = random.randint(0, peers) | 30 | a = random.randint(0, peers - 1) |
23 | b = random.randint(0, peers) | 31 | b = random.randint(0, peers - 1) |
24 | G.add_edge(a, b) | 32 | graph.add_edge(a, b) |
25 | return G | 33 | node_colors = [0] * peers |
34 | pos = networkx.layout.spring_layout(graph) | ||
26 | 35 | ||
27 | def create_topology_file(graph): | 36 | def create_topology_file(): |
37 | global graph | ||
28 | nodes = list() | 38 | nodes = list() |
29 | for i in range(len(graph.edge)): | 39 | for i in range(len(graph.edge)): |
30 | nodes.append(list()) | 40 | nodes.append(list()) |
@@ -41,10 +51,51 @@ def create_topology_file(graph): | |||
41 | # f.close() | 51 | # f.close() |
42 | return f.name | 52 | return f.name |
43 | 53 | ||
54 | def draw_graph(): | ||
55 | global graph | ||
56 | global node_colors | ||
57 | global pos | ||
58 | t = int(time.time()) | ||
59 | inc = 2 | ||
60 | name = str(t) + '.png' | ||
61 | while os.path.exists(name): | ||
62 | name = '%d(%d).png' % (t, inc) | ||
63 | inc += 1 | ||
64 | print 'Drawing graph to file: %s' % name | ||
65 | plt.clf() | ||
66 | networkx.draw(graph, pos=pos, node_color=node_colors, with_labels=range(len(graph.node)), cmap=plt.cm.Reds, vmin=0, vmax=2) | ||
67 | plt.savefig(name) | ||
68 | |||
69 | def peers_disconnected(p1, p2): | ||
70 | global graph | ||
71 | print 'Disconnected peers %d and %d' % (p1, p2) | ||
72 | if p2 not in graph[p1]: | ||
73 | print 'Link does not exist' | ||
74 | return | ||
75 | graph.remove_edge(p1, p2) | ||
76 | draw_graph() | ||
77 | |||
78 | def anomaly_report(report): | ||
79 | global node_colors | ||
80 | if 0 == report['anomalous']: | ||
81 | node_colors[report['peer']] = 0 | ||
82 | else: | ||
83 | node_colors[report['peer']] = 1 + report['neighbors'] | ||
84 | draw_graph() | ||
85 | |||
44 | def handle_profiler_line(line): | 86 | def handle_profiler_line(line): |
45 | if not line: | 87 | if not line: |
46 | return | 88 | return |
47 | print line | 89 | print line |
90 | if 'Peer disconnection request sent' in line: # Peers disconnected | ||
91 | parts = line.split(':') | ||
92 | peers = parts[-1].split(',') | ||
93 | peers_disconnected(int(peers[0]), int(peers[1])) | ||
94 | return | ||
95 | if 'Anomaly report:' in line: | ||
96 | parts = line.split('Anomaly report:') | ||
97 | anomaly_report(eval(parts[1])) | ||
98 | return | ||
48 | 99 | ||
49 | def run_profiler(peers, topology_file): | 100 | def run_profiler(peers, topology_file): |
50 | cmd = "GNUNET_FORCE_LOG='gnunet-sensor-profiler;;;;DEBUG' gnunet-sensor-profiler -p %d -t %s > log 2>&1" % (peers, topology_file) | 101 | 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(): | |||
69 | return | 120 | return |
70 | num_links = int(math.log(num_peers) * math.log(num_peers) * num_peers / 2) | 121 | num_links = int(math.log(num_peers) * math.log(num_peers) * num_peers / 2) |
71 | # Generate random topology | 122 | # Generate random topology |
72 | graph = generate_topology(num_peers, num_links) | 123 | generate_topology(num_peers, num_links) |
73 | print 'Generated random topology with %d peers and %d links' % (num_peers, num_links) | 124 | print 'Generated random topology with %d peers and %d links' % (num_peers, num_links) |
74 | # Create TESTBED topology file | 125 | # Create TESTBED topology file |
75 | top_file = create_topology_file(graph) | 126 | top_file = create_topology_file() |
76 | print 'Created TESTBED topology file %s' % top_file | 127 | print 'Created TESTBED topology file %s' % top_file |
128 | draw_graph() | ||
77 | # Run c profiler | 129 | # Run c profiler |
78 | run_profiler(num_peers, top_file) | 130 | run_profiler(num_peers, top_file) |
79 | 131 | ||