GNUnet  0.19.4
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 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 
30 
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-load", __VA_ARGS__)
32 
37 {
42 
47 
54 
61 
66 
72  double runavg_delay;
73 
79  double load;
80 };
81 
82 
83 static void
85 {
87  unsigned int n;
88 
89  if (load->autodecline.rel_value_us ==
90  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
91  return;
93  if (delta.rel_value_us < load->autodecline.rel_value_us)
94  return;
95  if (0 == load->autodecline.rel_value_us)
96  {
97  load->runavg_delay = 0.0;
98  load->load = 0;
99  return;
100  }
101  n = delta.rel_value_us / load->autodecline.rel_value_us;
102  if (n > 16)
103  {
104  load->runavg_delay = 0.0;
105  load->load = 0;
106  return;
107  }
108  while (n > 0)
109  {
110  n--;
111  load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
112  }
113 }
114 
115 
124 struct GNUNET_LOAD_Value *
126 {
127  struct GNUNET_LOAD_Value *ret;
128 
129  ret = GNUNET_new (struct GNUNET_LOAD_Value);
130  ret->autodecline = autodecline;
131  ret->last_update = GNUNET_TIME_absolute_get ();
132  return ret;
133 }
134 
135 
142 void
145 {
147  load->autodecline = autodecline;
148 }
149 
150 
156 static void
158 {
159  double stddev;
160  double avgdel;
161  double sum_val_i;
162  double n;
163  double nm1;
164 
165  if (load->cummulative_request_count <= 1)
166  return;
167  /* calculate std dev of latency; we have for n values of "i" that:
168  *
169  * avg = (sum val_i) / n
170  * stddev = (sum (val_i - avg)^2) / (n-1)
171  * = (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)
173  */sum_val_i = (double) load->cummulative_delay;
174  n = ((double) load->cummulative_request_count);
175  nm1 = n - 1.0;
176  avgdel = sum_val_i / n;
177  stddev =
178  (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i
179  + n * avgdel * avgdel) / nm1;
180  if (stddev <= 0)
181  stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
182  /* now calculate load based on how far out we are from
183  * std dev; or if we are below average, simply assume load zero */
184  if (load->runavg_delay < avgdel)
185  load->load = 0.0;
186  else
187  load->load = (load->runavg_delay - avgdel) / stddev;
188 }
189 
190 
200 double
202 {
205  return load->load;
206 }
207 
208 
215 double
217 {
218  double n;
219  double sum_val_i;
220 
222  if (load->cummulative_request_count == 0)
223  return 0.0;
224  n = ((double) load->cummulative_request_count);
225  sum_val_i = (double) load->cummulative_delay;
226  return sum_val_i / n;
227 }
228 
229 
236 void
238 {
239  uint32_t dv;
240 
242  load->last_update = GNUNET_TIME_absolute_get ();
243  if (data > 64 * 1024)
244  {
245  /* very large */
246  load->load = 100.0;
247  return;
248  }
249  dv = (uint32_t) data;
250  load->cummulative_delay += dv;
251  load->cummulative_squared_delay += dv * dv;
252  load->cummulative_request_count++;
253  load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
254 }
255 
256 
257 /* end of load.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
uint32_t data
The data value.
static void load()
Load persistent values from disk.
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:143
double GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
Get the average value given to update so far.
Definition: load.c:216
struct GNUNET_LOAD_Value * GNUNET_LOAD_value_init(struct GNUNET_TIME_Relative autodecline)
Create a new load value.
Definition: load.c:125
void GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data)
Update the current load.
Definition: load.c:237
double GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
Get the current load.
Definition: load.c:201
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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:436
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
static void internal_update(struct GNUNET_LOAD_Value *load)
Definition: load.c:84
static void calculate_load(struct GNUNET_LOAD_Value *load)
Recalculate our load value.
Definition: load.c:157
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:36
Values we track for load calculations.
Definition: load.c:37
struct GNUNET_TIME_Relative autodecline
How fast should the load decline if no values are added?
Definition: load.c:41
uint64_t cummulative_request_count
Total number of requests included in the cumulative datastore delay values.
Definition: load.c:65
double runavg_delay
Current running average datastore delay.
Definition: load.c:72
struct GNUNET_TIME_Absolute last_update
Last time this load value was updated by an event.
Definition: load.c:46
uint64_t cummulative_squared_delay
Sum of squares of all datastore delays ever observed (in ms).
Definition: load.c:60
uint64_t cummulative_delay
Sum of all datastore delays ever observed (in ms).
Definition: load.c:53
double load
How high is the load? 0 for below average, otherwise the number of std.
Definition: load.c:79
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.