GNUnet  0.11.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 
36 {
41 
46 
53 
60 
65 
71  double runavg_delay;
72 
78  double load;
79 };
80 
81 
82 static void
84 {
85  struct GNUNET_TIME_Relative delta;
86  unsigned int n;
87 
88  if (load->autodecline.rel_value_us ==
89  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
90  return;
92  if (delta.rel_value_us < load->autodecline.rel_value_us)
93  return;
94  if (0 == load->autodecline.rel_value_us)
95  {
96  load->runavg_delay = 0.0;
97  load->load = 0;
98  return;
99  }
100  n = delta.rel_value_us / load->autodecline.rel_value_us;
101  if (n > 16)
102  {
103  load->runavg_delay = 0.0;
104  load->load = 0;
105  return;
106  }
107  while (n > 0)
108  {
109  n--;
110  load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
111  }
112 }
113 
114 
123 struct GNUNET_LOAD_Value *
125 {
126  struct GNUNET_LOAD_Value *ret;
127 
128  ret = GNUNET_new (struct GNUNET_LOAD_Value);
129  ret->autodecline = autodecline;
131  return ret;
132 }
133 
134 
141 void
144 {
145  internal_update (load);
146  load->autodecline = autodecline;
147 }
148 
149 
155 static void
157 {
158  double stddev;
159  double avgdel;
160  double sum_val_i;
161  double n;
162  double nm1;
163 
164  if (load->cummulative_request_count <= 1)
165  return;
166  /* calcuate std dev of latency; we have for n values of "i" that:
167  *
168  * avg = (sum val_i) / n
169  * stddev = (sum (val_i - avg)^2) / (n-1)
170  * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
171  * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
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 
199 double
201 {
202  internal_update (load);
203  calculate_load (load);
204  return load->load;
205 }
206 
207 
214 double
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 
235 void
237 {
238  uint32_t dv;
239 
240  internal_update (load);
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;
252  load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
253 }
254 
255 
256 /* end of load.c */
double runavg_delay
Current running average datastore delay.
Definition: load.c:71
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:124
double load
How high is the load? 0 for below average, otherwise the number of std.
Definition: load.c:78
double GNUNET_LOAD_get_load(struct GNUNET_LOAD_Value *load)
Get the current load.
Definition: load.c:200
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
double GNUNET_LOAD_get_average(struct GNUNET_LOAD_Value *load)
Get the average value given to update so far.
Definition: load.c:215
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint64_t cummulative_delay
Sum of all datastore delays ever observed (in ms).
Definition: load.c:52
void GNUNET_LOAD_update(struct GNUNET_LOAD_Value *load, uint64_t data)
Update the current load.
Definition: load.c:236
uint64_t cummulative_request_count
Total number of requests included in the cummulative datastore delay values.
Definition: load.c:64
struct GNUNET_TIME_Relative autodecline
How fast should the load decline if no values are added?
Definition: load.c:40
#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:83
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:156
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:375
struct GNUNET_TIME_Absolute last_update
Last time this load value was updated by an event.
Definition: load.c:45
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:59
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:142
uint32_t data
The data value.
Time for relative time used by GNUnet, in microseconds.