GNUnet  0.10.x
load.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 
29 
30 #define LOG(kind, ...) GNUNET_log_from(kind, "util-load", __VA_ARGS__)
31 
40 
45 
52 
59 
64 
70  double runavg_delay;
71 
77  double load;
78 };
79 
80 
81 static void
83 {
84  struct GNUNET_TIME_Relative delta;
85  unsigned int n;
86 
87  if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
88  return;
90  if (delta.rel_value_us < load->autodecline.rel_value_us)
91  return;
92  if (0 == load->autodecline.rel_value_us)
93  {
94  load->runavg_delay = 0.0;
95  load->load = 0;
96  return;
97  }
98  n = delta.rel_value_us / load->autodecline.rel_value_us;
99  if (n > 16)
100  {
101  load->runavg_delay = 0.0;
102  load->load = 0;
103  return;
104  }
105  while (n > 0)
106  {
107  n--;
108  load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
109  }
110 }
111 
112 
121 struct GNUNET_LOAD_Value *
123 {
124  struct GNUNET_LOAD_Value *ret;
125 
126  ret = GNUNET_new(struct GNUNET_LOAD_Value);
127  ret->autodecline = autodecline;
129  return ret;
130 }
131 
132 
139 void
142 {
143  internal_update(load);
144  load->autodecline = autodecline;
145 }
146 
147 
153 static void
155 {
156  double stddev;
157  double avgdel;
158  double sum_val_i;
159  double n;
160  double nm1;
161 
162  if (load->cummulative_request_count <= 1)
163  return;
164  /* calcuate std dev of latency; we have for n values of "i" that:
165  *
166  * avg = (sum val_i) / n
167  * stddev = (sum (val_i - avg)^2) / (n-1)
168  * = (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)
170  */
171  sum_val_i = (double)load->cummulative_delay;
172  n = ((double)load->cummulative_request_count);
173  nm1 = n - 1.0;
174  avgdel = sum_val_i / n;
175  stddev =
176  (((double)load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i +
177  n * avgdel * avgdel) / nm1;
178  if (stddev <= 0)
179  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
181  * std dev; or if we are below average, simply assume load zero */
182  if (load->runavg_delay < avgdel)
183  load->load = 0.0;
184  else
185  load->load = (load->runavg_delay - avgdel) / stddev;
186 }
187 
188 
198 double
200 {
201  internal_update(load);
202  calculate_load(load);
203  return load->load;
204 }
205 
206 
213 double
215 {
216  double n;
217  double sum_val_i;
218 
219  internal_update(load);
220  if (load->cummulative_request_count == 0)
221  return 0.0;
222  n = ((double)load->cummulative_request_count);
223  sum_val_i = (double)load->cummulative_delay;
224  return sum_val_i / n;
225 }
226 
227 
234 void
236 {
237  uint32_t dv;
238 
239  internal_update(load);
241  if (data > 64 * 1024)
242  {
243  /* very large */
244  load->load = 100.0;
245  return;
246  }
247  dv = (uint32_t)data;
248  load->cummulative_delay += dv;
249  load->cummulative_squared_delay += dv * dv;
251  load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
252 }
253 
254 
255 
256 /* end of load.c */
double runavg_delay
Current running average datastore delay.
Definition: load.c:70
uint64_t rel_value_us
The actual value.
struct GNUNET_LOAD_Value * GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline)
Create a new load value.
Definition: load.c:122
double load
How high is the load? 0 for below average, otherwise the number of std.
Definition: load.c:77
double GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
Get the current load.
Definition: load.c:199
double GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
Get the average value given to update so far.
Definition: load.c:214
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
uint64_t cummulative_delay
Sum of all datastore delays ever observed (in ms).
Definition: load.c:51
void GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data)
Update the current load.
Definition: load.c:235
uint64_t cummulative_request_count
Total number of requests included in the cummulative datastore delay values.
Definition: load.c:63
struct GNUNET_TIME_Relative autodecline
How fast should the load decline if no values are added?
Definition: load.c:39
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static void internal_update(struct GNUNET_LOAD_Value *load)
Definition: load.c:82
Values we track for load calculations.
Definition: load.c:35
static void calculate_load(struct GNUNET_LOAD_Value *load)
Recalculate our load value.
Definition: load.c:154
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:373
struct GNUNET_TIME_Absolute last_update
Last time this load value was updated by an event.
Definition: load.c:44
Time for absolute times used by GNUnet, in microseconds.
uint64_t cummulative_squared_delay
Sum of squares of all datastore delays ever observed (in ms).
Definition: load.c:58
void GNUNET_LOAD_value_set_decline(struct GNUNET_LOAD_Value *load, struct GNUNET_TIME_Relative autodecline)
Change the value by which the load automatically declines.
Definition: load.c:140
uint32_t data
The data value.
Time for relative time used by GNUnet, in microseconds.