paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / util / load.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/load.c
21  * @brief functions related to load calculations
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-load", __VA_ARGS__)
29
30 /**
31  * Values we track for load calculations.
32  */
33 struct GNUNET_LOAD_Value
34 {
35
36   /**
37    * How fast should the load decline if no values are added?
38    */
39   struct GNUNET_TIME_Relative autodecline;
40
41   /**
42    * Last time this load value was updated by an event.
43    */
44   struct GNUNET_TIME_Absolute last_update;
45
46   /**
47    * Sum of all datastore delays ever observed (in ms).  Note that
48    * delays above 64k ms are excluded (to avoid overflow within
49    * first 4 billion requests).
50    */
51   uint64_t cummulative_delay;
52
53   /**
54    * Sum of squares of all datastore delays ever observed (in ms).   Note that
55    * delays above 64k ms are excluded (to avoid overflow within
56    * first 4 billion requests).
57    */
58   uint64_t cummulative_squared_delay;
59
60   /**
61    * Total number of requests included in the cummulative datastore delay values.
62    */
63   uint64_t cummulative_request_count;
64
65   /**
66    * Current running average datastore delay.  Its relation to the
67    * average datastore delay and it std. dev. (as calcualted from the
68    * cummulative values) tells us our current load.
69    */
70   double runavg_delay;
71
72   /**
73    * How high is the load?  0 for below average, otherwise
74    * the number of std. devs we are above average, or 100 if the
75    * load is so high that we currently cannot calculate it.
76    */
77   double load;
78
79 };
80
81
82 static void
83 internal_update (struct GNUNET_LOAD_Value *load)
84 {
85   struct GNUNET_TIME_Relative delta;
86   unsigned int n;
87
88   if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
89     return;
90   delta = GNUNET_TIME_absolute_get_duration (load->last_update);
91   if (delta.rel_value_us < load->autodecline.rel_value_us)
92     return;
93   if (0 == load->autodecline.rel_value_us)
94   {
95     load->runavg_delay = 0.0;
96     load->load = 0;
97     return;
98   }
99   n = delta.rel_value_us / load->autodecline.rel_value_us;
100   if (n > 16)
101   {
102     load->runavg_delay = 0.0;
103     load->load = 0;
104     return;
105   }
106   while (n > 0)
107   {
108     n--;
109     load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
110   }
111 }
112
113
114 /**
115  * Create a new load value.
116  *
117  * @param autodecline speed at which this value should automatically
118  *        decline in the absence of external events; at the given
119  *        frequency, 0-load values will be added to the load
120  * @return the new load value
121  */
122 struct GNUNET_LOAD_Value *
123 GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
124 {
125   struct GNUNET_LOAD_Value *ret;
126
127   ret = GNUNET_new (struct GNUNET_LOAD_Value);
128   ret->autodecline = autodecline;
129   ret->last_update = GNUNET_TIME_absolute_get ();
130   return ret;
131 }
132
133
134 /**
135  * Change the value by which the load automatically declines.
136  *
137  * @param load load to update
138  * @param autodecline frequency of load decline
139  */
140 void
141 GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
142                                struct GNUNET_TIME_Relative autodecline)
143 {
144   internal_update (load);
145   load->autodecline = autodecline;
146 }
147
148
149 /**
150  * Recalculate our load value.
151  *
152  * @param load load to update
153  */
154 static void
155 calculate_load (struct GNUNET_LOAD_Value *load)
156 {
157   double stddev;
158   double avgdel;
159   double sum_val_i;
160   double n;
161   double nm1;
162
163   if (load->cummulative_request_count <= 1)
164     return;
165   /* calcuate std dev of latency; we have for n values of "i" that:
166    *
167    * avg = (sum val_i) / n
168    * stddev = (sum (val_i - avg)^2) / (n-1)
169    * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
170    * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
171    */
172   sum_val_i = (double) load->cummulative_delay;
173   n = ((double) load->cummulative_request_count);
174   nm1 = n - 1.0;
175   avgdel = sum_val_i / n;
176   stddev =
177       (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i +
178        n * avgdel * avgdel) / nm1;
179   if (stddev <= 0)
180     stddev = 0.01;              /* must have been rounding error or zero; prevent division by zero */
181   /* now calculate load based on how far out we are from
182    * std dev; or if we are below average, simply assume load zero */
183   if (load->runavg_delay < avgdel)
184     load->load = 0.0;
185   else
186     load->load = (load->runavg_delay - avgdel) / stddev;
187 }
188
189
190 /**
191  * Get the current load.
192  *
193  * @param load load handle
194  * @return zero for below-average load, otherwise
195  *         number of std. devs we are above average;
196  *         100 if the latest updates were so large
197  *         that we could not do proper calculations
198  */
199 double
200 GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
201 {
202   internal_update (load);
203   calculate_load (load);
204   return load->load;
205 }
206
207
208 /**
209  * Get the average value given to update so far.
210  *
211  * @param load load handle
212  * @return zero if update was never called
213  */
214 double
215 GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
216 {
217   double n;
218   double sum_val_i;
219
220   internal_update (load);
221   if (load->cummulative_request_count == 0)
222     return 0.0;
223   n = ((double) load->cummulative_request_count);
224   sum_val_i = (double) load->cummulative_delay;
225   return sum_val_i / n;
226 }
227
228
229 /**
230  * Update the current load.
231  *
232  * @param load to update
233  * @param data latest measurement value (for example, delay)
234  */
235 void
236 GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
237 {
238   uint32_t dv;
239
240   internal_update (load);
241   load->last_update = GNUNET_TIME_absolute_get ();
242   if (data > 64 * 1024)
243   {
244     /* very large */
245     load->load = 100.0;
246     return;
247   }
248   dv = (uint32_t) data;
249   load->cummulative_delay += dv;
250   load->cummulative_squared_delay += dv * dv;
251   load->cummulative_request_count++;
252   load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
253 }
254
255
256
257 /* end of load.c */