aboutsummaryrefslogtreecommitdiff
path: root/src/sensor
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-09-22 13:19:57 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-09-22 13:19:57 +0000
commitb97fc3b79ac05184f0928af8071e76dcbd8f99ea (patch)
tree88bb54e44e2b7fc3c8c385553223276b360a54d5 /src/sensor
parent97f7c9dcb0555f28989b4223151ff0be58ff8e52 (diff)
downloadgnunet-b97fc3b79ac05184f0928af8071e76dcbd8f99ea.tar.gz
gnunet-b97fc3b79ac05184f0928af8071e76dcbd8f99ea.zip
sensor: profiler updates
Diffstat (limited to 'src/sensor')
-rw-r--r--src/sensor/gnunet-sensor-profiler.c144
-rw-r--r--src/sensor/gnunet-service-sensor_reporting.c37
-rw-r--r--src/sensor/profiler.py57
3 files changed, 174 insertions, 64 deletions
diff --git a/src/sensor/gnunet-sensor-profiler.c b/src/sensor/gnunet-sensor-profiler.c
index 4cd6b808d..3e25cb732 100644
--- a/src/sensor/gnunet-sensor-profiler.c
+++ b/src/sensor/gnunet-sensor-profiler.c
@@ -85,6 +85,19 @@ struct ConnectionContext
85 85
86}; 86};
87 87
88struct Split
89{
90
91 struct Split *next;
92
93 struct Split *prev;
94
95 int p1;
96
97 int p2;
98
99};
100
88 101
89/** 102/**
90 * Name of the configuration file used 103 * Name of the configuration file used
@@ -127,6 +140,11 @@ static unsigned int sensors_interval = 0;
127static char *topology_file; 140static char *topology_file;
128 141
129/** 142/**
143 * Path to topology file (Option -s)
144 */
145static char *split_file;
146
147/**
130 * Array of peer info for all peers 148 * Array of peer info for all peers
131 */ 149 */
132static struct PeerInfo *all_peers_info; 150static struct PeerInfo *all_peers_info;
@@ -176,11 +194,21 @@ static GNUNET_SCHEDULER_TaskIdentifier delayed_task = GNUNET_SCHEDULER_NO_TASK;
176 */ 194 */
177static struct DisconnectionContext *dc_head; 195static struct DisconnectionContext *dc_head;
178 196
179/* 197/**
180 * Tail of list of disconnection contexts 198 * Tail of list of disconnection contexts
181 */ 199 */
182static struct DisconnectionContext *dc_tail; 200static struct DisconnectionContext *dc_tail;
183 201
202/**
203 * Head of splits list
204 */
205static struct Split *split_head;
206
207/**
208 * Tail of splits list
209 */
210static struct Split *split_tail;
211
184 212
185/** 213/**
186 * Copy directory recursively 214 * Copy directory recursively
@@ -417,7 +445,7 @@ disconnect_peers (struct PeerInfo *p1, struct PeerInfo *p2)
417 */ 445 */
418static void 446static void
419overlay_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, 447overlay_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
420 const char *emsg) 448 const char *emsg)
421{ 449{
422 struct ConnectionContext *cc = cls; 450 struct ConnectionContext *cc = cls;
423 451
@@ -449,7 +477,7 @@ connect_peers (struct PeerInfo *p1, struct PeerInfo *p2)
449 { 477 {
450 if ((dc->p1 == p1 && dc->p2 == p2) || (dc->p1 == p2 && dc->p2 == p1)) 478 if ((dc->p1 == p1 && dc->p2 == p2) || (dc->p1 == p2 && dc->p2 == p1))
451 break; 479 break;
452 dc = dc_head->next; 480 dc = dc->next;
453 } 481 }
454 if (NULL != dc) 482 if (NULL != dc)
455 { 483 {
@@ -458,10 +486,11 @@ connect_peers (struct PeerInfo *p1, struct PeerInfo *p2)
458 } 486 }
459 /* Connect peers using testbed */ 487 /* Connect peers using testbed */
460 cc = GNUNET_new (struct ConnectionContext); 488 cc = GNUNET_new (struct ConnectionContext);
489
461 cc->p1 = p1; 490 cc->p1 = p1;
462 cc->p2 = p2; 491 cc->p2 = p2;
463 GNUNET_TESTBED_overlay_connect (cc, &overlay_connect_cb, cc, 492 GNUNET_TESTBED_overlay_connect (cc, &overlay_connect_cb, cc, p1->testbed_peer,
464 p1->testbed_peer, p2->testbed_peer); 493 p2->testbed_peer);
465} 494}
466 495
467/*****************************************************************************/ 496/*****************************************************************************/
@@ -552,8 +581,8 @@ sensor_dir_scanner (void *cls, const char *filename)
552 GNUNET_CONFIGURATION_parse (sensor_cfg, filename)); 581 GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
553 GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename, 582 GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
554 "COLLECTION_POINT", 583 "COLLECTION_POINT",
555 GNUNET_i2s_full (&all_peers_info[0]. 584 GNUNET_i2s_full (&all_peers_info
556 peer_id)); 585 [0].peer_id));
557 if (sensors_interval > 0) 586 if (sensors_interval > 0)
558 { 587 {
559 GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename, 588 GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
@@ -716,28 +745,18 @@ peerstore_disconnect_adapter (void *cls, void *op_result)
716 * Prompty the user to reconnect two peers 745 * Prompty the user to reconnect two peers
717 */ 746 */
718static void 747static void
719prompt_peer_reconnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 748prompt_peer_reconnection (void *cls,
749 const struct GNUNET_SCHEDULER_TaskContext *tc)
720{ 750{
721 int p1; 751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting one link.\n");
722 int p2; 752 connect_peers (&all_peers_info[split_head->p1],
723 char line[10]; 753 &all_peers_info[split_head->p2]);
724 754 GNUNET_SCHEDULER_cancel (shutdown_task);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down in 5 mins.\n");
726 "Connect peers (e.g. '0,2') or empty line to execute:\n"); 756 shutdown_task =
727 if (NULL == fgets (line, sizeof (line), stdin) || 1 == strlen (line)) 757 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
728 { 758 (GNUNET_TIME_UNIT_MINUTES, 5), do_shutdown,
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Continuing.\n"); 759 NULL);
730 return;
731 }
732 if (2 != sscanf (line, "%d,%d", &p1, &p2) || p1 >= num_peers ||
733 p2 >= num_peers || p1 < 0 || p2 < 0 || p1 == p2)
734 {
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid input.\n");
736 prompt_peer_reconnection (NULL, NULL);
737 return;
738 }
739 connect_peers (&all_peers_info[p1], &all_peers_info[p2]);
740 prompt_peer_reconnection (NULL, NULL);
741} 760}
742 761
743 762
@@ -747,27 +766,19 @@ prompt_peer_reconnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *
747static void 766static void
748prompt_peer_disconnection () 767prompt_peer_disconnection ()
749{ 768{
750 int p1; 769 struct Split *s;
751 int p2;
752 char line[10];
753 770
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 771 s = split_head;
755 "Disconnect peers (e.g. '0,2') or empty line to execute:\n"); 772 while (NULL != s)
756 if (NULL == fgets (line, sizeof (line), stdin) || 1 == strlen (line))
757 { 773 {
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will prompt for reconnection in 1 min.\n"); 774 disconnect_peers (&all_peers_info[s->p1], &all_peers_info[s->p2]);
759 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1) ,&prompt_peer_reconnection, NULL); 775 s = s->next;
760 return;
761 } 776 }
762 if (2 != sscanf (line, "%d,%d", &p1, &p2) || p1 >= num_peers || 777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 p2 >= num_peers || p1 < 0 || p2 < 0 || p1 == p2) 778 "Will prompt for reconnection in 1 min.\n");
764 { 779 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid input.\n"); 780 (GNUNET_TIME_UNIT_MINUTES, 5),
766 prompt_peer_disconnection (); 781 &prompt_peer_reconnection, NULL);
767 return;
768 }
769 disconnect_peers (&all_peers_info[p1], &all_peers_info[p2]);
770 prompt_peer_disconnection ();
771} 782}
772 783
773 784
@@ -964,6 +975,44 @@ verify_args ()
964 975
965 976
966/** 977/**
978 * Parse split file (name passed as parameter).
979 * Split file contains sequence of peer pairs to disconenct.
980 */
981static void
982parse_split_file ()
983{
984 uint64_t f_size;
985 char *splits;
986 char *ptr;
987 int p1;
988 int p2;
989 struct Split *s;
990
991 GNUNET_assert (NULL != split_file);
992 GNUNET_assert (GNUNET_OK ==
993 GNUNET_DISK_file_size (split_file, &f_size, GNUNET_NO,
994 GNUNET_YES));
995 splits = malloc (f_size);
996 GNUNET_assert (f_size == GNUNET_DISK_fn_read (split_file, splits, f_size));
997 ptr = splits;
998 while (ptr < (splits + f_size))
999 {
1000 GNUNET_assert (2 == sscanf (ptr, "%d,%d", &p1, &p2));
1001 s = GNUNET_new (struct Split);
1002
1003 s->p1 = p1;
1004 s->p2 = p2;
1005 GNUNET_CONTAINER_DLL_insert_tail (split_head, split_tail, s);
1006 while (ptr < (splits + f_size) && *ptr != '\n')
1007 ptr++;
1008 if (*ptr == '\n')
1009 ptr++;
1010 }
1011 GNUNET_free (splits);
1012}
1013
1014
1015/**
967 * Actual main function. 1016 * Actual main function.
968 * 1017 *
969 * @param cls unused 1018 * @param cls unused
@@ -986,6 +1035,7 @@ run (void *cls, char *const *args, const char *cf,
986 cfg, "TESTBED", 1035 cfg, "TESTBED",
987 "OVERLAY_TOPOLOGY_FILE", 1036 "OVERLAY_TOPOLOGY_FILE",
988 topology_file); 1037 topology_file);
1038 parse_split_file ();
989 shutdown_task = 1039 shutdown_task =
990 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, 1040 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
991 NULL); 1041 NULL);
@@ -1009,6 +1059,8 @@ main (int argc, char *const *argv)
1009 {'i', "sensors-interval", "INTERVAL", 1059 {'i', "sensors-interval", "INTERVAL",
1010 gettext_noop ("Change the interval of running sensors to given value"), 1060 gettext_noop ("Change the interval of running sensors to given value"),
1011 GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval}, 1061 GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
1062 {'s', "split-file", "FILEPATH", gettext_noop ("Path to split file"),
1063 GNUNET_YES, &GNUNET_GETOPT_set_filename, &split_file},
1012 GNUNET_GETOPT_OPTION_END 1064 GNUNET_GETOPT_OPTION_END
1013 }; 1065 };
1014 1066
diff --git a/src/sensor/gnunet-service-sensor_reporting.c b/src/sensor/gnunet-service-sensor_reporting.c
index 3b2c08e42..1c19b38f9 100644
--- a/src/sensor/gnunet-service-sensor_reporting.c
+++ b/src/sensor/gnunet-service-sensor_reporting.c
@@ -423,20 +423,33 @@ static void
423destroy_core_peer (struct CorePeer *corep) 423destroy_core_peer (struct CorePeer *corep)
424{ 424{
425 struct AnomalyInfo *ai; 425 struct AnomalyInfo *ai;
426 struct AnomalyReportingQueueItem *ar_item;
426 427
427 if (NULL != corep->mq)
428 {
429 GNUNET_MQ_destroy (corep->mq);
430 corep->mq = NULL;
431 }
432 ai = ai_head; 428 ai = ai_head;
433 while (NULL != ai) 429 while (NULL != ai)
434 { 430 {
435 GNUNET_assert (NULL != ai->anomalous_neighbors); 431 GNUNET_assert (NULL != ai->anomalous_neighbors);
436 GNUNET_CONTAINER_multipeermap_remove_all (ai->anomalous_neighbors, 432 GNUNET_CONTAINER_multipeermap_remove_all (ai->anomalous_neighbors,
437 corep->peer_id); 433 corep->peer_id);
434 /* Remove the core peer from any reporting queues */
435 ar_item = ai->reporting_queue_head;
436 while (NULL != ar_item)
437 {
438 if (ar_item->dest_mq == corep->mq)
439 {
440 GNUNET_CONTAINER_DLL_remove (ai->reporting_queue_head,
441 ai->reporting_queue_tail, ar_item);
442 break;
443 }
444 ar_item = ar_item->next;
445 }
438 ai = ai->next; 446 ai = ai->next;
439 } 447 }
448 if (NULL != corep->mq)
449 {
450 GNUNET_MQ_destroy (corep->mq);
451 corep->mq = NULL;
452 }
440 GNUNET_free (corep); 453 GNUNET_free (corep);
441} 454}
442 455
@@ -873,9 +886,9 @@ update_anomaly_report_pow_block (struct AnomalyInfo *ai)
873 arm->anomalous = htons (ai->anomalous); 886 arm->anomalous = htons (ai->anomalous);
874 arm->anomalous_neighbors = 887 arm->anomalous_neighbors =
875 (0 == 888 (0 ==
876 neighborhood) ? 0 : ((float) GNUNET_CONTAINER_multipeermap_size (ai-> 889 neighborhood) ? 0 : ((float)
877 anomalous_neighbors)) 890 GNUNET_CONTAINER_multipeermap_size
878 / neighborhood; 891 (ai->anomalous_neighbors)) / neighborhood;
879 timestamp = GNUNET_TIME_absolute_get (); 892 timestamp = GNUNET_TIME_absolute_get ();
880 ai->report_creation_cx = 893 ai->report_creation_cx =
881 GNUNET_SENSOR_crypto_pow_sign (arm, 894 GNUNET_SENSOR_crypto_pow_sign (arm,
@@ -968,8 +981,8 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other,
968 my_anomaly_info = get_anomaly_info_by_sensor (sensor); 981 my_anomaly_info = get_anomaly_info_by_sensor (sensor);
969 GNUNET_assert (NULL != my_anomaly_info); 982 GNUNET_assert (NULL != my_anomaly_info);
970 peer_in_anomalous_list = 983 peer_in_anomalous_list =
971 GNUNET_CONTAINER_multipeermap_contains (my_anomaly_info-> 984 GNUNET_CONTAINER_multipeermap_contains
972 anomalous_neighbors, other); 985 (my_anomaly_info->anomalous_neighbors, other);
973 peer_anomalous = ntohs (arm->anomalous); 986 peer_anomalous = ntohs (arm->anomalous);
974 LOG (GNUNET_ERROR_TYPE_DEBUG, 987 LOG (GNUNET_ERROR_TYPE_DEBUG,
975 "Received an anomaly update from neighbour `%s' (%d).\n", 988 "Received an anomaly update from neighbour `%s' (%d).\n",
@@ -988,8 +1001,8 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other,
988 if (GNUNET_NO == peer_in_anomalous_list) /* repeated negative report */ 1001 if (GNUNET_NO == peer_in_anomalous_list) /* repeated negative report */
989 GNUNET_break_op (0); 1002 GNUNET_break_op (0);
990 else 1003 else
991 GNUNET_CONTAINER_multipeermap_remove_all (my_anomaly_info-> 1004 GNUNET_CONTAINER_multipeermap_remove_all
992 anomalous_neighbors, other); 1005 (my_anomaly_info->anomalous_neighbors, other);
993 } 1006 }
994 /* This is important to create an updated block since the data changed */ 1007 /* This is important to create an updated block since the data changed */
995 update_anomaly_report_pow_block (my_anomaly_info); 1008 update_anomaly_report_pow_block (my_anomaly_info);
diff --git a/src/sensor/profiler.py b/src/sensor/profiler.py
index 7b77b48bb..8e48ab838 100644
--- a/src/sensor/profiler.py
+++ b/src/sensor/profiler.py
@@ -16,6 +16,8 @@ def get_args():
16 parser = argparse.ArgumentParser(description="Sensor profiler") 16 parser = argparse.ArgumentParser(description="Sensor profiler")
17 parser.add_argument('-p', '--peers', action='store', type=int, required=True, 17 parser.add_argument('-p', '--peers', action='store', type=int, required=True,
18 help='Number of peers to run') 18 help='Number of peers to run')
19 parser.add_argument('-l', '--links', action='store', type=int, required=False,
20 help='Number of links to create')
19 parser.add_argument('-i', '--sensors-interval', action='store', type=int, 21 parser.add_argument('-i', '--sensors-interval', action='store', type=int,
20 required=False, 22 required=False,
21 help='Change the interval of running sensors to given value') 23 help='Change the interval of running sensors to given value')
@@ -66,9 +68,22 @@ def draw_graph():
66 inc += 1 68 inc += 1
67 print 'Drawing graph to file: %s' % name 69 print 'Drawing graph to file: %s' % name
68 plt.clf() 70 plt.clf()
71 anomaly_lbls = {}
72 for i in range(len(graph.node)):
73 if node_colors[i] >= 1:
74 anomaly_lbls[i] = '\n\n\n' + str(node_colors[i] - 1)
69 networkx.draw(graph, pos=pos, node_color=node_colors, with_labels=range(len(graph.node)), cmap=plt.cm.Reds, vmin=0, vmax=2) 75 networkx.draw(graph, pos=pos, node_color=node_colors, with_labels=range(len(graph.node)), cmap=plt.cm.Reds, vmin=0, vmax=2)
76 networkx.draw_networkx_labels(graph, pos, anomaly_lbls)
70 plt.savefig(name) 77 plt.savefig(name)
71 78
79def peers_reconnected(p1, p2):
80 global graph
81 if p2 in graph[p1]:
82 print 'Link already exists'
83 return
84 graph.add_edge(p1, p2)
85 draw_graph()
86
72def peers_disconnected(p1, p2): 87def peers_disconnected(p1, p2):
73 global graph 88 global graph
74 print 'Disconnected peers %d and %d' % (p1, p2) 89 print 'Disconnected peers %d and %d' % (p1, p2)
@@ -83,7 +98,10 @@ def anomaly_report(report):
83 if 0 == report['anomalous']: 98 if 0 == report['anomalous']:
84 node_colors[report['peer']] = 0 99 node_colors[report['peer']] = 0
85 else: 100 else:
86 node_colors[report['peer']] = 1 + report['neighbors'] 101 clr = 1 + report['neighbors']
102 if node_colors[report['peer']] >= clr:
103 return
104 node_colors[report['peer']] = clr
87 draw_graph() 105 draw_graph()
88 106
89def handle_profiler_line(line): 107def handle_profiler_line(line):
@@ -99,9 +117,13 @@ def handle_profiler_line(line):
99 parts = line.split('Anomaly report:') 117 parts = line.split('Anomaly report:')
100 anomaly_report(eval(parts[1])) 118 anomaly_report(eval(parts[1]))
101 return 119 return
120 if 'Peer connection request sent' in line: # Peers reconnected
121 parts = line.split(':')
122 peers = parts[-1].split(',')
123 peers_reconnected(int(peers[0]), int(peers[1]))
102 124
103def run_profiler(peers, topology_file, sensors_interval): 125def run_profiler(peers, topology_file, sensors_interval, split_file):
104 cmd1 = "GNUNET_FORCE_LOG='gnunet-sensor-profiler;;;;DEBUG' gnunet-sensor-profiler -p %d -t %s" % (peers, topology_file) 126 cmd1 = "./gnunet-sensor-profiler -p %d -t %s -s %s" % (peers, topology_file, split_file)
105 if sensors_interval: 127 if sensors_interval:
106 cmd1 += " -i %d" % sensors_interval 128 cmd1 += " -i %d" % sensors_interval
107 cmd2 = "> log 2>&1" 129 cmd2 = "> log 2>&1"
@@ -120,6 +142,22 @@ def run_profiler(peers, topology_file, sensors_interval):
120 line += c 142 line += c
121 os.remove('log') 143 os.remove('log')
122 144
145def create_split():
146 global graph
147 f = open('split', 'w+')
148 half_size = len(graph.node) / 2
149 half1 = []
150 half2 = []
151 for n in graph.node:
152 if n < half_size:
153 half1.append(n)
154 else:
155 half2.append(n)
156 for e in graph.edges():
157 if (e[0] in half1 and e[1] in half2) or (e[0] in half2 and e[1] in half1):
158 f.write('%d,%d\n' % (e[0], e[1]))
159 f.close()
160
123def main(): 161def main():
124 args = vars(get_args()) 162 args = vars(get_args())
125 num_peers = args['peers'] 163 num_peers = args['peers']
@@ -129,17 +167,24 @@ def main():
129 sensors_interval = None 167 sensors_interval = None
130 if 'sensors_interval' in args: 168 if 'sensors_interval' in args:
131 sensors_interval = args['sensors_interval'] 169 sensors_interval = args['sensors_interval']
132 #num_links = int(math.log(num_peers) * math.log(num_peers) * num_peers / 2) 170 if 'links' in args:
133 num_links = int(math.log(num_peers) * num_peers) 171 num_links = args['links']
172 else:
173 #num_links = int(math.log(num_peers) * math.log(num_peers) * num_peers / 2)
174 num_links = int(math.log(num_peers) * num_peers)
134 # Generate random topology 175 # Generate random topology
135 generate_topology(num_peers, num_links) 176 generate_topology(num_peers, num_links)
136 print 'Generated random topology with %d peers and %d links' % (num_peers, num_links) 177 print 'Generated random topology with %d peers and %d links' % (num_peers, num_links)
178 # Create a file with links to cut to split the topology into two
179 create_split()
137 # Create TESTBED topology file 180 # Create TESTBED topology file
138 top_file = create_topology_file() 181 top_file = create_topology_file()
139 print 'Created TESTBED topology file %s' % top_file 182 print 'Created TESTBED topology file %s' % top_file
140 draw_graph() 183 draw_graph()
141 # Run c profiler 184 # Run c profiler
142 run_profiler(num_peers, top_file, sensors_interval) 185 if os.path.isfile('log'):
186 os.remove('log')
187 run_profiler(num_peers, top_file, sensors_interval, 'split')
143 188
144if __name__ == "__main__": 189if __name__ == "__main__":
145 main() 190 main()