aboutsummaryrefslogtreecommitdiff
path: root/src/util/load.c
blob: 451aca295515eb6e1a674702c33850b80deaabac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
     This file is part of GNUnet.
     (C) 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/load.c
 * @brief functions related to load calculations
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_load_lib.h"

#define DEBUG_LOAD GNUNET_NO

/**
 * Values we track for load calculations.
 */
struct GNUNET_LOAD_Value 
{

  /**
   * Sum of all datastore delays ever observed (in ms).  Note that
   * delays above 64k ms are excluded (to avoid overflow within
   * first 4 billion requests).
   */
  uint64_t cummulative_delay;
  
  /**
   * Sum of squares of all datastore delays ever observed (in ms).   Note that
   * delays above 64k ms are excluded (to avoid overflow within
   * first 4 billion requests).
   */
  uint64_t cummulative_squared_delay;
  
  /**
   * Total number of requests included in the cummulative datastore delay values.
   */
  uint64_t cummulative_request_count;
  
  /**
   * Current running average datastore delay.  Its relation to the
   * average datastore delay and it std. dev. (as calcualted from the
   * cummulative values) tells us our current load.
   */
  double runavg_delay;

  /**
   * How high is the load?  0 for below average, otherwise
   * the number of std. devs we are above average, or 100 if the
   * load is so high that we currently cannot calculate it.
   */
  double load;

};


/**
 * Create a new load value.
 *
 * @return the new load value
 */
struct GNUNET_LOAD_Value *
GNUNET_LOAD_value_init ()
{
  return GNUNET_malloc (sizeof (struct GNUNET_LOAD_Value));
}


/**
 * Get the current load.
 *
 * @param load load handle
 * @return zero for below-average load, otherwise
 *         number of std. devs we are above average;
 *         100 if the latest updates were so large
 *         that we could not do proper calculations
 */
double
GNUNET_LOAD_get_load (const struct GNUNET_LOAD_Value *load)
{
  return load->load;
}


/**
 * Get the average value given to update so far.
 *
 * @param load load handle
 * @return zero if update was never called
 */
double
GNUNET_LOAD_get_average (const struct GNUNET_LOAD_Value *load)
{
  double n;
  double avg;
  double sum_val_i;

  if (load->cummulative_request_count == 0)
    return 0.0;
  n = ((double) load->cummulative_request_count);
  sum_val_i = (double) load->cummulative_delay;
  return sum_val_i / n;
}


/**
 * Update the current load.
 *
 * @param load to update
 * @param data latest measurement value (for example, delay)
 */
void
GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load,
		    uint64_t data)
{
  uint32_t dv;
  double stddev;
  double avgdel;
  double sum_val_i;
  double n;
  double nm1;

  if (data > 64 * 1024)
    {
      /* very large */
      load->load = 100.0;
      return;
    }
  dv = (uint32_t) data;
  load->cummulative_delay += dv;
  load->cummulative_squared_delay += dv * dv; 
  load->cummulative_request_count++;
  load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
  if (load->cummulative_request_count > 1)
    {
      /* calcuate std dev of latency; we have for n values of "i" that:

	 avg = (sum val_i) / n
	 stddev = (sum (val_i - avg)^2) / (n-1)
	        = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
                = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
      */
      sum_val_i = (double) load->cummulative_delay;
      n = ((double) load->cummulative_request_count);
      nm1 = n - 1.0;
      avgdel = sum_val_i / n;
      stddev = (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + n * avgdel * avgdel) / nm1; 
      if (stddev <= 0)
	stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
      /* now calculate load based on how far out we are from
	 std dev; or if we are below average, simply assume load zero */
      if (load->runavg_delay < avgdel)
	load->load = 0.0;
      else
	load->load = (load->runavg_delay - avgdel) / stddev;
    }  
}


/* end of load.c */