summaryrefslogtreecommitdiff
path: root/src/util/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/load.c')
-rw-r--r--src/util/load.c94
1 files changed, 48 insertions, 46 deletions
diff --git a/src/util/load.c b/src/util/load.c
index 5c4d9d37b..bab2f375e 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -27,12 +27,13 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-load", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-load", __VA_ARGS__)
31 31
32/** 32/**
33 * Values we track for load calculations. 33 * Values we track for load calculations.
34 */ 34 */
35struct GNUNET_LOAD_Value { 35struct GNUNET_LOAD_Value
36{
36 /** 37 /**
37 * How fast should the load decline if no values are added? 38 * How fast should the load decline if no values are added?
38 */ 39 */
@@ -79,34 +80,35 @@ struct GNUNET_LOAD_Value {
79 80
80 81
81static void 82static void
82internal_update(struct GNUNET_LOAD_Value *load) 83internal_update (struct GNUNET_LOAD_Value *load)
83{ 84{
84 struct GNUNET_TIME_Relative delta; 85 struct GNUNET_TIME_Relative delta;
85 unsigned int n; 86 unsigned int n;
86 87
87 if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 88 if (load->autodecline.rel_value_us ==
89 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
88 return; 90 return;
89 delta = GNUNET_TIME_absolute_get_duration(load->last_update); 91 delta = GNUNET_TIME_absolute_get_duration (load->last_update);
90 if (delta.rel_value_us < load->autodecline.rel_value_us) 92 if (delta.rel_value_us < load->autodecline.rel_value_us)
91 return; 93 return;
92 if (0 == load->autodecline.rel_value_us) 94 if (0 == load->autodecline.rel_value_us)
93 { 95 {
94 load->runavg_delay = 0.0; 96 load->runavg_delay = 0.0;
95 load->load = 0; 97 load->load = 0;
96 return; 98 return;
97 } 99 }
98 n = delta.rel_value_us / load->autodecline.rel_value_us; 100 n = delta.rel_value_us / load->autodecline.rel_value_us;
99 if (n > 16) 101 if (n > 16)
100 { 102 {
101 load->runavg_delay = 0.0; 103 load->runavg_delay = 0.0;
102 load->load = 0; 104 load->load = 0;
103 return; 105 return;
104 } 106 }
105 while (n > 0) 107 while (n > 0)
106 { 108 {
107 n--; 109 n--;
108 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; 110 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
109 } 111 }
110} 112}
111 113
112 114
@@ -119,13 +121,13 @@ internal_update(struct GNUNET_LOAD_Value *load)
119 * @return the new load value 121 * @return the new load value
120 */ 122 */
121struct GNUNET_LOAD_Value * 123struct GNUNET_LOAD_Value *
122GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline) 124GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
123{ 125{
124 struct GNUNET_LOAD_Value *ret; 126 struct GNUNET_LOAD_Value *ret;
125 127
126 ret = GNUNET_new(struct GNUNET_LOAD_Value); 128 ret = GNUNET_new (struct GNUNET_LOAD_Value);
127 ret->autodecline = autodecline; 129 ret->autodecline = autodecline;
128 ret->last_update = GNUNET_TIME_absolute_get(); 130 ret->last_update = GNUNET_TIME_absolute_get ();
129 return ret; 131 return ret;
130} 132}
131 133
@@ -137,10 +139,10 @@ GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline)
137 * @param autodecline frequency of load decline 139 * @param autodecline frequency of load decline
138 */ 140 */
139void 141void
140GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load, 142GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
141 struct GNUNET_TIME_Relative autodecline) 143 struct GNUNET_TIME_Relative autodecline)
142{ 144{
143 internal_update(load); 145 internal_update (load);
144 load->autodecline = autodecline; 146 load->autodecline = autodecline;
145} 147}
146 148
@@ -151,7 +153,7 @@ GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load,
151 * @param load load to update 153 * @param load load to update
152 */ 154 */
153static void 155static void
154calculate_load(struct GNUNET_LOAD_Value *load) 156calculate_load (struct GNUNET_LOAD_Value *load)
155{ 157{
156 double stddev; 158 double stddev;
157 double avgdel; 159 double avgdel;
@@ -168,13 +170,13 @@ calculate_load(struct GNUNET_LOAD_Value *load)
168 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1) 170 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
169 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1) 171 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
170 */ 172 */
171 sum_val_i = (double)load->cummulative_delay; 173 sum_val_i = (double) load->cummulative_delay;
172 n = ((double)load->cummulative_request_count); 174 n = ((double) load->cummulative_request_count);
173 nm1 = n - 1.0; 175 nm1 = n - 1.0;
174 avgdel = sum_val_i / n; 176 avgdel = sum_val_i / n;
175 stddev = 177 stddev =
176 (((double)load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + 178 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i
177 n * avgdel * avgdel) / nm1; 179 + n * avgdel * avgdel) / nm1;
178 if (stddev <= 0) 180 if (stddev <= 0)
179 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ 181 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
180 /* now calculate load based on how far out we are from 182 /* now calculate load based on how far out we are from
@@ -196,10 +198,10 @@ calculate_load(struct GNUNET_LOAD_Value *load)
196 * that we could not do proper calculations 198 * that we could not do proper calculations
197 */ 199 */
198double 200double
199GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load) 201GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
200{ 202{
201 internal_update(load); 203 internal_update (load);
202 calculate_load(load); 204 calculate_load (load);
203 return load->load; 205 return load->load;
204} 206}
205 207
@@ -211,16 +213,16 @@ GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
211 * @return zero if update was never called 213 * @return zero if update was never called
212 */ 214 */
213double 215double
214GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load) 216GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
215{ 217{
216 double n; 218 double n;
217 double sum_val_i; 219 double sum_val_i;
218 220
219 internal_update(load); 221 internal_update (load);
220 if (load->cummulative_request_count == 0) 222 if (load->cummulative_request_count == 0)
221 return 0.0; 223 return 0.0;
222 n = ((double)load->cummulative_request_count); 224 n = ((double) load->cummulative_request_count);
223 sum_val_i = (double)load->cummulative_delay; 225 sum_val_i = (double) load->cummulative_delay;
224 return sum_val_i / n; 226 return sum_val_i / n;
225} 227}
226 228
@@ -232,19 +234,19 @@ GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
232 * @param data latest measurement value (for example, delay) 234 * @param data latest measurement value (for example, delay)
233 */ 235 */
234void 236void
235GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data) 237GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
236{ 238{
237 uint32_t dv; 239 uint32_t dv;
238 240
239 internal_update(load); 241 internal_update (load);
240 load->last_update = GNUNET_TIME_absolute_get(); 242 load->last_update = GNUNET_TIME_absolute_get ();
241 if (data > 64 * 1024) 243 if (data > 64 * 1024)
242 { 244 {
243 /* very large */ 245 /* very large */
244 load->load = 100.0; 246 load->load = 100.0;
245 return; 247 return;
246 } 248 }
247 dv = (uint32_t)data; 249 dv = (uint32_t) data;
248 load->cummulative_delay += dv; 250 load->cummulative_delay += dv;
249 load->cummulative_squared_delay += dv * dv; 251 load->cummulative_squared_delay += dv * dv;
250 load->cummulative_request_count++; 252 load->cummulative_request_count++;