load.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/load.c
  18. * @brief functions related to load calculations
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #define LOG(kind, ...) GNUNET_log_from (kind, "util-load", __VA_ARGS__)
  24. /**
  25. * Values we track for load calculations.
  26. */
  27. struct GNUNET_LOAD_Value
  28. {
  29. /**
  30. * How fast should the load decline if no values are added?
  31. */
  32. struct GNUNET_TIME_Relative autodecline;
  33. /**
  34. * Last time this load value was updated by an event.
  35. */
  36. struct GNUNET_TIME_Absolute last_update;
  37. /**
  38. * Sum of all datastore delays ever observed (in ms). Note that
  39. * delays above 64k ms are excluded (to avoid overflow within
  40. * first 4 billion requests).
  41. */
  42. uint64_t cummulative_delay;
  43. /**
  44. * Sum of squares of all datastore delays ever observed (in ms). Note that
  45. * delays above 64k ms are excluded (to avoid overflow within
  46. * first 4 billion requests).
  47. */
  48. uint64_t cummulative_squared_delay;
  49. /**
  50. * Total number of requests included in the cummulative datastore delay values.
  51. */
  52. uint64_t cummulative_request_count;
  53. /**
  54. * Current running average datastore delay. Its relation to the
  55. * average datastore delay and it std. dev. (as calcualted from the
  56. * cummulative values) tells us our current load.
  57. */
  58. double runavg_delay;
  59. /**
  60. * How high is the load? 0 for below average, otherwise
  61. * the number of std. devs we are above average, or 100 if the
  62. * load is so high that we currently cannot calculate it.
  63. */
  64. double load;
  65. };
  66. static void
  67. internal_update (struct GNUNET_LOAD_Value *load)
  68. {
  69. struct GNUNET_TIME_Relative delta;
  70. unsigned int n;
  71. if (load->autodecline.rel_value_us ==
  72. GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
  73. return;
  74. delta = GNUNET_TIME_absolute_get_duration (load->last_update);
  75. if (delta.rel_value_us < load->autodecline.rel_value_us)
  76. return;
  77. if (0 == load->autodecline.rel_value_us)
  78. {
  79. load->runavg_delay = 0.0;
  80. load->load = 0;
  81. return;
  82. }
  83. n = delta.rel_value_us / load->autodecline.rel_value_us;
  84. if (n > 16)
  85. {
  86. load->runavg_delay = 0.0;
  87. load->load = 0;
  88. return;
  89. }
  90. while (n > 0)
  91. {
  92. n--;
  93. load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
  94. }
  95. }
  96. /**
  97. * Create a new load value.
  98. *
  99. * @param autodecline speed at which this value should automatically
  100. * decline in the absence of external events; at the given
  101. * frequency, 0-load values will be added to the load
  102. * @return the new load value
  103. */
  104. struct GNUNET_LOAD_Value *
  105. GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
  106. {
  107. struct GNUNET_LOAD_Value *ret;
  108. ret = GNUNET_new (struct GNUNET_LOAD_Value);
  109. ret->autodecline = autodecline;
  110. ret->last_update = GNUNET_TIME_absolute_get ();
  111. return ret;
  112. }
  113. /**
  114. * Change the value by which the load automatically declines.
  115. *
  116. * @param load load to update
  117. * @param autodecline frequency of load decline
  118. */
  119. void
  120. GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
  121. struct GNUNET_TIME_Relative autodecline)
  122. {
  123. internal_update (load);
  124. load->autodecline = autodecline;
  125. }
  126. /**
  127. * Recalculate our load value.
  128. *
  129. * @param load load to update
  130. */
  131. static void
  132. calculate_load (struct GNUNET_LOAD_Value *load)
  133. {
  134. double stddev;
  135. double avgdel;
  136. double sum_val_i;
  137. double n;
  138. double nm1;
  139. if (load->cummulative_request_count <= 1)
  140. return;
  141. /* calcuate std dev of latency; we have for n values of "i" that:
  142. *
  143. * avg = (sum val_i) / n
  144. * stddev = (sum (val_i - avg)^2) / (n-1)
  145. * = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
  146. * = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
  147. */sum_val_i = (double) load->cummulative_delay;
  148. n = ((double) load->cummulative_request_count);
  149. nm1 = n - 1.0;
  150. avgdel = sum_val_i / n;
  151. stddev =
  152. (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i
  153. + n * avgdel * avgdel) / nm1;
  154. if (stddev <= 0)
  155. stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
  156. /* now calculate load based on how far out we are from
  157. * std dev; or if we are below average, simply assume load zero */
  158. if (load->runavg_delay < avgdel)
  159. load->load = 0.0;
  160. else
  161. load->load = (load->runavg_delay - avgdel) / stddev;
  162. }
  163. /**
  164. * Get the current load.
  165. *
  166. * @param load load handle
  167. * @return zero for below-average load, otherwise
  168. * number of std. devs we are above average;
  169. * 100 if the latest updates were so large
  170. * that we could not do proper calculations
  171. */
  172. double
  173. GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
  174. {
  175. internal_update (load);
  176. calculate_load (load);
  177. return load->load;
  178. }
  179. /**
  180. * Get the average value given to update so far.
  181. *
  182. * @param load load handle
  183. * @return zero if update was never called
  184. */
  185. double
  186. GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
  187. {
  188. double n;
  189. double sum_val_i;
  190. internal_update (load);
  191. if (load->cummulative_request_count == 0)
  192. return 0.0;
  193. n = ((double) load->cummulative_request_count);
  194. sum_val_i = (double) load->cummulative_delay;
  195. return sum_val_i / n;
  196. }
  197. /**
  198. * Update the current load.
  199. *
  200. * @param load to update
  201. * @param data latest measurement value (for example, delay)
  202. */
  203. void
  204. GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data)
  205. {
  206. uint32_t dv;
  207. internal_update (load);
  208. load->last_update = GNUNET_TIME_absolute_get ();
  209. if (data > 64 * 1024)
  210. {
  211. /* very large */
  212. load->load = 100.0;
  213. return;
  214. }
  215. dv = (uint32_t) data;
  216. load->cummulative_delay += dv;
  217. load->cummulative_squared_delay += dv * dv;
  218. load->cummulative_request_count++;
  219. load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
  220. }
  221. /* end of load.c */