aboutsummaryrefslogtreecommitdiff
path: root/src/sensor
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-09-15 13:15:31 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-09-15 13:15:31 +0000
commitbe73250fb373dbdcd18180ee9eff8b1bc87374af (patch)
treed8f888c476c33ede18bcd4a306e7acc3228104f4 /src/sensor
parentd94c5185fcdf47c6a57d3233b946fa7e621f7ad9 (diff)
downloadgnunet-be73250fb373dbdcd18180ee9eff8b1bc87374af.tar.gz
gnunet-be73250fb373dbdcd18180ee9eff8b1bc87374af.zip
sensor: update to gaussian model + optimized parameters
Diffstat (limited to 'src/sensor')
-rw-r--r--src/sensor/gnunet-service-sensor_analysis.c6
-rw-r--r--src/sensor/plugin_sensor_model_gaussian.c74
-rw-r--r--src/sensor/sensor.conf.in8
-rw-r--r--src/sensor/sensor_util_lib_crypto.c1
-rw-r--r--src/sensor/sensors/transport-tcp-bytes-transmitted2
5 files changed, 60 insertions, 31 deletions
diff --git a/src/sensor/gnunet-service-sensor_analysis.c b/src/sensor/gnunet-service-sensor_analysis.c
index a53c22589..d9ca48dd0 100644
--- a/src/sensor/gnunet-service-sensor_analysis.c
+++ b/src/sensor/gnunet-service-sensor_analysis.c
@@ -89,7 +89,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
89/** 89/**
90 * Hashmap of loaded sensors 90 * Hashmap of loaded sensors
91 */ 91 */
92struct GNUNET_CONTAINER_MultiHashMap *sensors; 92static struct GNUNET_CONTAINER_MultiHashMap *sensors;
93 93
94/* 94/*
95 * Model library name 95 * Model library name
@@ -119,13 +119,13 @@ static struct SensorModel *models_tail;
119/** 119/**
120 * My peer id 120 * My peer id
121 */ 121 */
122struct GNUNET_PeerIdentity peerid; 122static struct GNUNET_PeerIdentity peerid;
123 123
124/** 124/**
125 * How many subsequent values required to flip anomaly label. 125 * How many subsequent values required to flip anomaly label.
126 * E.g. After 3 subsequent anomaly reports, status change to anomalous. 126 * E.g. After 3 subsequent anomaly reports, status change to anomalous.
127 */ 127 */
128unsigned long long confirmation_count; 128static unsigned long long confirmation_count;
129 129
130/** 130/**
131 * Destroy a created model 131 * Destroy a created model
diff --git a/src/sensor/plugin_sensor_model_gaussian.c b/src/sensor/plugin_sensor_model_gaussian.c
index 8b4b09089..af879ef93 100644
--- a/src/sensor/plugin_sensor_model_gaussian.c
+++ b/src/sensor/plugin_sensor_model_gaussian.c
@@ -18,7 +18,7 @@
18 * Boston, MA 02111-1307, USA. 18 * Boston, MA 02111-1307, USA.
19 */ 19 */
20 20
21/* 21/**
22 * @file sensor/plugin_sensor_model_gaussian.c 22 * @file sensor/plugin_sensor_model_gaussian.c
23 * @brief Gaussian model for sensor analysis 23 * @brief Gaussian model for sensor analysis
24 * @author Omar Tarabai 24 * @author Omar Tarabai
@@ -31,54 +31,59 @@
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "sensor-model-gaussian", __VA_ARGS__) 32#define LOG(kind,...) GNUNET_log_from (kind, "sensor-model-gaussian", __VA_ARGS__)
33 33
34/* 34/**
35 * Plugin state information 35 * Plugin state information
36 */ 36 */
37struct Plugin 37struct Plugin
38{ 38{
39 39
40 /* 40 /**
41 * Configuration handle 41 * Configuration handle
42 */ 42 */
43 const struct GNUNET_CONFIGURATION_Handle *cfg; 43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 44
45 /* 45 /**
46 * Number of initial readings to be used for training only 46 * Number of initial readings to be used for training only
47 */ 47 */
48 int training_window; 48 int training_window;
49 49
50 /* 50 /**
51 * Number of standard deviations considered within "normal" 51 * Number of standard deviations considered within "normal"
52 */ 52 */
53 int confidence_interval; 53 int confidence_interval;
54 54
55 /**
56 * Increase in weight with each reading
57 */
58 float weight_inc;
59
55}; 60};
56 61
57/* 62/**
58 * State of single model instance 63 * State of single model instance
59 */ 64 */
60struct Model 65struct Model
61{ 66{
62 67
63 /* 68 /**
64 * Pointer to the plugin state 69 * Pointer to the plugin state
65 */ 70 */
66 struct Plugin *plugin; 71 struct Plugin *plugin;
67 72
68 /* 73 /**
69 * Number of readings so far 74 * Gaussian sums
70 */ 75 */
71 int n; 76 long double s[3];
72 77
73 /* 78 /**
74 * Sum of readings 79 * Number of readings so far
75 */ 80 */
76 long double sum; 81 int n;
77 82
78 /* 83 /**
79 * Sum square of readings 84 * Weight to be used for the next reading
80 */ 85 */
81 long double sumsq; 86 double w;
82 87
83}; 88};
84 89
@@ -91,8 +96,11 @@ struct Model
91static void 96static void
92update_sums (struct Model *model, double val) 97update_sums (struct Model *model, double val)
93{ 98{
94 model->sum += val; 99 int i;
95 model->sumsq += val * val; 100
101 for (i = 0; i < 3; i++)
102 model->s[i] += model->w * pow (val, (double) i);
103 model->w += model->plugin->weight_inc;
96 model->n++; 104 model->n++;
97} 105}
98 106
@@ -120,10 +128,14 @@ sensor_gaussian_model_feed (void *cls, double val)
120 } 128 }
121 if (model->n == plugin->training_window) 129 if (model->n == plugin->training_window)
122 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model out of training period.\n"); 130 LOG (GNUNET_ERROR_TYPE_DEBUG, "Gaussian model out of training period.\n");
123 mean = model->sum / model->n; 131 mean = model->s[1] / model->s[0];
124 stddev = 132 stddev =
125 sqrt ((model->sumsq - 2 * mean * model->sum + 133 (model->s[0] * model->s[2] -
126 model->n * mean * mean) / (model->n - 1)); 134 model->s[1] * model->s[1]) / (model->s[0] * (model->s[0] - 1));
135 if (stddev < 0) /* Value can be slightly less than 0 due to rounding errors */
136 stddev = 0;
137 stddev = sqrt (stddev);
138 LOG (GNUNET_ERROR_TYPE_DEBUG, "Mean: %Lf, Stddev: %Lf\n", mean, stddev);
127 allowed_variance = (plugin->confidence_interval * stddev); 139 allowed_variance = (plugin->confidence_interval * stddev);
128 if ((val < (mean - allowed_variance)) || (val > (mean + allowed_variance))) 140 if ((val < (mean - allowed_variance)) || (val > (mean + allowed_variance)))
129 return GNUNET_YES; 141 return GNUNET_YES;
@@ -161,6 +173,7 @@ sensor_gaussian_model_create_model (void *cls)
161 model = GNUNET_new (struct Model); 173 model = GNUNET_new (struct Model);
162 174
163 model->plugin = plugin; 175 model->plugin = plugin;
176 model->w = 1;
164 return model; 177 return model;
165} 178}
166 179
@@ -191,7 +204,16 @@ libgnunet_plugin_sensor_model_gaussian_init (void *cls)
191 _("Missing `TRAINING_WINDOW' value in configuration.\n")); 204 _("Missing `TRAINING_WINDOW' value in configuration.\n"));
192 return NULL; 205 return NULL;
193 } 206 }
194 plugin.training_window = (int) num; 207 if (num < 1)
208 {
209 LOG (GNUNET_ERROR_TYPE_WARNING,
210 "Minimum training window invalid (<1), setting to 1.\n");
211 plugin.training_window = 1;
212 }
213 else
214 {
215 plugin.training_window = (int) num;
216 }
195 if (GNUNET_OK != 217 if (GNUNET_OK !=
196 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian", 218 GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-model-gaussian",
197 "CONFIDENCE_INTERVAL", &num)) 219 "CONFIDENCE_INTERVAL", &num))
@@ -200,6 +222,14 @@ libgnunet_plugin_sensor_model_gaussian_init (void *cls)
200 _("Missing `CONFIDENCE_INTERVAL' value in configuration.\n")); 222 _("Missing `CONFIDENCE_INTERVAL' value in configuration.\n"));
201 return NULL; 223 return NULL;
202 } 224 }
225 if (GNUNET_OK !=
226 GNUNET_CONFIGURATION_get_value_float (cfg, "sensor-model-gaussian",
227 "WEIGHT_INC", &plugin.weight_inc))
228 {
229 LOG (GNUNET_ERROR_TYPE_ERROR,
230 _("Missing `WEIGHT_INC' value in configuration.\n"));
231 return NULL;
232 }
203 plugin.confidence_interval = (int) num; 233 plugin.confidence_interval = (int) num;
204 api = GNUNET_new (struct GNUNET_SENSOR_ModelFunctions); 234 api = GNUNET_new (struct GNUNET_SENSOR_ModelFunctions);
205 235
diff --git a/src/sensor/sensor.conf.in b/src/sensor/sensor.conf.in
index d85fab258..08e1abaad 100644
--- a/src/sensor/sensor.conf.in
+++ b/src/sensor/sensor.conf.in
@@ -18,12 +18,12 @@ START_UPDATE = YES
18[sensor-analysis] 18[sensor-analysis]
19MODEL = gaussian 19MODEL = gaussian
20# How many subsequent values required to flip anomaly label. (Default: 1) 20# How many subsequent values required to flip anomaly label. (Default: 1)
21# E.g. After 3 subsequent anomaly reports, status change to anomalous. 21CONFIRMATION_COUNT = 1
22CONFIRMATION_COUNT = 3
23 22
24[sensor-model-gaussian] 23[sensor-model-gaussian]
25TRAINING_WINDOW = 10 24TRAINING_WINDOW = 400
26CONFIDENCE_INTERVAL = 3 25CONFIDENCE_INTERVAL = 8
26WEIGHT_INC = 0
27 27
28[sensor-reporting] 28[sensor-reporting]
29POW_MATCHING_BITS = 15 29POW_MATCHING_BITS = 15
diff --git a/src/sensor/sensor_util_lib_crypto.c b/src/sensor/sensor_util_lib_crypto.c
index bf271e792..677750e47 100644
--- a/src/sensor/sensor_util_lib_crypto.c
+++ b/src/sensor/sensor_util_lib_crypto.c
@@ -128,7 +128,6 @@ check_pow (void *msg, size_t msg_size, uint64_t pow, int matching_bits)
128 char buf[msg_size + sizeof (pow)] GNUNET_ALIGN; 128 char buf[msg_size + sizeof (pow)] GNUNET_ALIGN;
129 struct GNUNET_HashCode result; 129 struct GNUNET_HashCode result;
130 130
131 LOG (GNUNET_ERROR_TYPE_DEBUG, "Msg size: %" PRIu64 ".\n", msg_size);
132 memcpy (buf, &pow, sizeof (pow)); 131 memcpy (buf, &pow, sizeof (pow));
133 memcpy (&buf[sizeof (pow)], msg, msg_size); 132 memcpy (&buf[sizeof (pow)], msg, msg_size);
134 pow_hash (buf, sizeof (buf), &result); 133 pow_hash (buf, sizeof (buf), &result);
diff --git a/src/sensor/sensors/transport-tcp-bytes-transmitted b/src/sensor/sensors/transport-tcp-bytes-transmitted
index 5452d7ac0..4a75ac3fb 100644
--- a/src/sensor/sensors/transport-tcp-bytes-transmitted
+++ b/src/sensor/sensors/transport-tcp-bytes-transmitted
@@ -1,7 +1,7 @@
1[transport-tcp-bytes-transmitted] 1[transport-tcp-bytes-transmitted]
2 2
3VERSION = 1.0 3VERSION = 1.0
4DESCRIPTION = Number of GNUnet transport service active TCP sessions 4DESCRIPTION = Number of GNUnet transport TCP bytes transmitted
5CATEGORY = GNUnet 5CATEGORY = GNUnet
6ENABLED = YES 6ENABLED = YES
7 7