aboutsummaryrefslogtreecommitdiff
path: root/src/util/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/load.c')
-rw-r--r--src/util/load.c97
1 files changed, 47 insertions, 50 deletions
diff --git a/src/util/load.c b/src/util/load.c
index f5ea875bb..5c4d9d37b 100644
--- a/src/util/load.c
+++ b/src/util/load.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file util/load.c 22 * @file util/load.c
@@ -27,14 +27,12 @@
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{
37
38 /** 36 /**
39 * How fast should the load decline if no values are added? 37 * How fast should the load decline if no values are added?
40 */ 38 */
@@ -77,39 +75,38 @@ struct GNUNET_LOAD_Value
77 * load is so high that we currently cannot calculate it. 75 * load is so high that we currently cannot calculate it.
78 */ 76 */
79 double load; 77 double load;
80
81}; 78};
82 79
83 80
84static void 81static void
85internal_update (struct GNUNET_LOAD_Value *load) 82internal_update(struct GNUNET_LOAD_Value *load)
86{ 83{
87 struct GNUNET_TIME_Relative delta; 84 struct GNUNET_TIME_Relative delta;
88 unsigned int n; 85 unsigned int n;
89 86
90 if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 87 if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
91 return; 88 return;
92 delta = GNUNET_TIME_absolute_get_duration (load->last_update); 89 delta = GNUNET_TIME_absolute_get_duration(load->last_update);
93 if (delta.rel_value_us < load->autodecline.rel_value_us) 90 if (delta.rel_value_us < load->autodecline.rel_value_us)
94 return; 91 return;
95 if (0 == load->autodecline.rel_value_us) 92 if (0 == load->autodecline.rel_value_us)
96 { 93 {
97 load->runavg_delay = 0.0; 94 load->runavg_delay = 0.0;
98 load->load = 0; 95 load->load = 0;
99 return; 96 return;
100 } 97 }
101 n = delta.rel_value_us / load->autodecline.rel_value_us; 98 n = delta.rel_value_us / load->autodecline.rel_value_us;
102 if (n > 16) 99 if (n > 16)
103 { 100 {
104 load->runavg_delay = 0.0; 101 load->runavg_delay = 0.0;
105 load->load = 0; 102 load->load = 0;
106 return; 103 return;
107 } 104 }
108 while (n > 0) 105 while (n > 0)
109 { 106 {
110 n--; 107 n--;
111 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; 108 load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
112 } 109 }
113} 110}
114 111
115 112
@@ -122,13 +119,13 @@ internal_update (struct GNUNET_LOAD_Value *load)
122 * @return the new load value 119 * @return the new load value
123 */ 120 */
124struct GNUNET_LOAD_Value * 121struct GNUNET_LOAD_Value *
125GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline) 122GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline)
126{ 123{
127 struct GNUNET_LOAD_Value *ret; 124 struct GNUNET_LOAD_Value *ret;
128 125
129 ret = GNUNET_new (struct GNUNET_LOAD_Value); 126 ret = GNUNET_new(struct GNUNET_LOAD_Value);
130 ret->autodecline = autodecline; 127 ret->autodecline = autodecline;
131 ret->last_update = GNUNET_TIME_absolute_get (); 128 ret->last_update = GNUNET_TIME_absolute_get();
132 return ret; 129 return ret;
133} 130}
134 131
@@ -140,10 +137,10 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
140 * @param autodecline frequency of load decline 137 * @param autodecline frequency of load decline
141 */ 138 */
142void 139void
143GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load, 140GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load,
144 struct GNUNET_TIME_Relative autodecline) 141 struct GNUNET_TIME_Relative autodecline)
145{ 142{
146 internal_update (load); 143 internal_update(load);
147 load->autodecline = autodecline; 144 load->autodecline = autodecline;
148} 145}
149 146
@@ -154,7 +151,7 @@ GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
154 * @param load load to update 151 * @param load load to update
155 */ 152 */
156static void 153static void
157calculate_load (struct GNUNET_LOAD_Value *load) 154calculate_load(struct GNUNET_LOAD_Value *load)
158{ 155{
159 double stddev; 156 double stddev;
160 double avgdel; 157 double avgdel;
@@ -171,13 +168,13 @@ calculate_load (struct GNUNET_LOAD_Value *load)
171 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1) 168 * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
172 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1) 169 * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
173 */ 170 */
174 sum_val_i = (double) load->cummulative_delay; 171 sum_val_i = (double)load->cummulative_delay;
175 n = ((double) load->cummulative_request_count); 172 n = ((double)load->cummulative_request_count);
176 nm1 = n - 1.0; 173 nm1 = n - 1.0;
177 avgdel = sum_val_i / n; 174 avgdel = sum_val_i / n;
178 stddev = 175 stddev =
179 (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + 176 (((double)load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i +
180 n * avgdel * avgdel) / nm1; 177 n * avgdel * avgdel) / nm1;
181 if (stddev <= 0) 178 if (stddev <= 0)
182 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */ 179 stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
183 /* now calculate load based on how far out we are from 180 /* now calculate load based on how far out we are from
@@ -199,10 +196,10 @@ calculate_load (struct GNUNET_LOAD_Value *load)
199 * that we could not do proper calculations 196 * that we could not do proper calculations
200 */ 197 */
201double 198double
202GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load) 199GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
203{ 200{
204 internal_update (load); 201 internal_update(load);
205 calculate_load (load); 202 calculate_load(load);
206 return load->load; 203 return load->load;
207} 204}
208 205
@@ -214,16 +211,16 @@ GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
214 * @return zero if update was never called 211 * @return zero if update was never called
215 */ 212 */
216double 213double
217GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load) 214GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
218{ 215{
219 double n; 216 double n;
220 double sum_val_i; 217 double sum_val_i;
221 218
222 internal_update (load); 219 internal_update(load);
223 if (load->cummulative_request_count == 0) 220 if (load->cummulative_request_count == 0)
224 return 0.0; 221 return 0.0;
225 n = ((double) load->cummulative_request_count); 222 n = ((double)load->cummulative_request_count);
226 sum_val_i = (double) load->cummulative_delay; 223 sum_val_i = (double)load->cummulative_delay;
227 return sum_val_i / n; 224 return sum_val_i / n;
228} 225}
229 226
@@ -235,19 +232,19 @@ GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
235 * @param data latest measurement value (for example, delay) 232 * @param data latest measurement value (for example, delay)
236 */ 233 */
237void 234void
238GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data) 235GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data)
239{ 236{
240 uint32_t dv; 237 uint32_t dv;
241 238
242 internal_update (load); 239 internal_update(load);
243 load->last_update = GNUNET_TIME_absolute_get (); 240 load->last_update = GNUNET_TIME_absolute_get();
244 if (data > 64 * 1024) 241 if (data > 64 * 1024)
245 { 242 {
246 /* very large */ 243 /* very large */
247 load->load = 100.0; 244 load->load = 100.0;
248 return; 245 return;
249 } 246 }
250 dv = (uint32_t) data; 247 dv = (uint32_t)data;
251 load->cummulative_delay += dv; 248 load->cummulative_delay += dv;
252 load->cummulative_squared_delay += dv * dv; 249 load->cummulative_squared_delay += dv * dv;
253 load->cummulative_request_count++; 250 load->cummulative_request_count++;