X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fload.c;h=d374d7a17ca6c52aed41d6ec098f4c25ac02d857;hb=f249a80a2b1e52b24969782e8afb30b86c373ae2;hp=fd8a5afabbd759045b073d646582cd81ae84d675;hpb=fa4128cdbd9857b3446c1fe337c0796eaba76f18;p=oweals%2Fgnunet.git diff --git a/src/util/load.c b/src/util/load.c index fd8a5afab..d374d7a17 100644 --- a/src/util/load.c +++ b/src/util/load.c @@ -1,10 +1,10 @@ /* This file is part of GNUnet. - (C) 2010 Christian Grothoff (and other contributing authors) + Copyright (C) 2010, 2013 GNUnet e.V. 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 + by the Free Software Foundation; either version 3, or (at your option) any later version. GNUnet is distributed in the hope that it will be useful, but @@ -14,8 +14,8 @@ 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. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** @@ -24,14 +24,15 @@ * @author Christian Grothoff */ #include "platform.h" -#include "gnunet_load_lib.h" +#include "gnunet_util_lib.h" -#define DEBUG_LOAD GNUNET_NO + +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) /** * Values we track for load calculations. */ -struct GNUNET_LOAD_Value +struct GNUNET_LOAD_Value { /** @@ -41,7 +42,7 @@ struct GNUNET_LOAD_Value /** * Last time this load value was updated by an event. - */ + */ struct GNUNET_TIME_Absolute last_update; /** @@ -50,19 +51,19 @@ struct GNUNET_LOAD_Value * 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 @@ -86,23 +87,29 @@ internal_update (struct GNUNET_LOAD_Value *load) struct GNUNET_TIME_Relative delta; unsigned int n; - if (load->autodecline.value == GNUNET_TIME_UNIT_FOREVER_REL.value) + if (load->autodecline.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) return; delta = GNUNET_TIME_absolute_get_duration (load->last_update); - if (delta.value < load->autodecline.value) + if (delta.rel_value_us < load->autodecline.rel_value_us) return; - n = delta.value / load->autodecline.value; + if (0 == load->autodecline.rel_value_us) + { + load->runavg_delay = 0.0; + load->load = 0; + return; + } + n = delta.rel_value_us / load->autodecline.rel_value_us; if (n > 16) - { - load->runavg_delay = 0.0; - load->load = 0; - return; - } + { + load->runavg_delay = 0.0; + load->load = 0; + return; + } while (n > 0) - { - n--; - load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; - } + { + n--; + load->runavg_delay = (load->runavg_delay * 7.0) / 8.0; + } } @@ -119,14 +126,28 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline) { struct GNUNET_LOAD_Value *ret; - GNUNET_assert (autodecline.value != 0); - ret = GNUNET_malloc (sizeof (struct GNUNET_LOAD_Value)); + ret = GNUNET_new (struct GNUNET_LOAD_Value); ret->autodecline = autodecline; ret->last_update = GNUNET_TIME_absolute_get (); return ret; } +/** + * Change the value by which the load automatically declines. + * + * @param load load to update + * @param autodecline frequency of load decline + */ +void +GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load, + struct GNUNET_TIME_Relative autodecline) +{ + internal_update (load); + load->autodecline = autodecline; +} + + /** * Recalculate our load value. * @@ -141,28 +162,30 @@ calculate_load (struct GNUNET_LOAD_Value *load) double n; double nm1; - if (load->cummulative_request_count == 0) + if (load->cummulative_request_count <= 1) return; /* 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) - */ + * + * 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; + 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 */ + 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 */ + * 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; + load->load = (load->runavg_delay - avgdel) / stddev; } @@ -212,22 +235,21 @@ GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load) * @param data latest measurement value (for example, delay) */ void -GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, - uint64_t data) +GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load, uint64_t data) { uint32_t dv; internal_update (load); load->last_update = GNUNET_TIME_absolute_get (); if (data > 64 * 1024) - { - /* very large */ - load->load = 100.0; - return; - } + { + /* very large */ + load->load = 100.0; + return; + } dv = (uint32_t) data; load->cummulative_delay += dv; - load->cummulative_squared_delay += dv * dv; + load->cummulative_squared_delay += dv * dv; load->cummulative_request_count++; load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0; }