diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-08-12 07:26:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-08-12 07:26:11 +0000 |
commit | 3c315f0fff93aa072472abc10815963ac0035268 (patch) | |
tree | a95f6a96e0e7bd139c010f8dc60b40e5b3062a99 /libnetdata/storage_number | |
parent | Adding upstream version 1.35.1. (diff) | |
download | netdata-upstream/1.36.0.tar.xz netdata-upstream/1.36.0.zip |
Adding upstream version 1.36.0.upstream/1.36.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | libnetdata/storage_number/storage_number.c | 71 | ||||
-rw-r--r-- | libnetdata/storage_number/storage_number.h | 222 | ||||
-rw-r--r-- | libnetdata/storage_number/tests/test_storage_number.c | 20 |
3 files changed, 201 insertions, 112 deletions
diff --git a/libnetdata/storage_number/storage_number.c b/libnetdata/storage_number/storage_number.c index ba7a66874..6a8b68c11 100644 --- a/libnetdata/storage_number/storage_number.c +++ b/libnetdata/storage_number/storage_number.c @@ -2,12 +2,7 @@ #include "../libnetdata.h" -#define get_storage_number_flags(value) \ - ((((storage_number)(value)) & (1 << 24)) | \ - (((storage_number)(value)) & (1 << 25)) | \ - (((storage_number)(value)) & (1 << 26))) - -storage_number pack_storage_number(calculated_number value, uint32_t flags) { +storage_number pack_storage_number(NETDATA_DOUBLE value, SN_FLAGS flags) { // bit 32 = sign 0:positive, 1:negative // bit 31 = 0:divide, 1:multiply // bit 30, 29, 28 = (multiplier or divider) 0-7 (8 total) @@ -16,44 +11,48 @@ storage_number pack_storage_number(calculated_number value, uint32_t flags) { // bit 25 SN_ANOMALY_BIT = 0: anomalous, 1: not anomalous // bit 24 to bit 1 = the value - storage_number r = get_storage_number_flags(flags); - if(!value) - goto RET_SN; + if(unlikely(fpclassify(value) == FP_NAN || fpclassify(value) == FP_INFINITE)) + return SN_EMPTY_SLOT; + + storage_number r = flags & SN_USER_FLAGS; + + if(unlikely(fpclassify(value) == FP_ZERO || fpclassify(value) == FP_SUBNORMAL)) + return r; int m = 0; - calculated_number n = value, factor = 10; + NETDATA_DOUBLE n = value, factor = 10; // if the value is negative // add the sign bit and make it positive if(n < 0) { - r += (1 << 31); // the sign bit 32 + r += SN_FLAG_NEGATIVE; // the sign bit 32 n = -n; } if(n / 10000000.0 > 0x00ffffff) { factor = 100; - r |= SN_EXISTS_100; + r |= SN_FLAG_NOT_EXISTS_MUL100; } // make its integer part fit in 0x00ffffff // by dividing it by 10 up to 7 times // and increasing the multiplier - while(m < 7 && n > (calculated_number)0x00ffffff) { + while(m < 7 && n > (NETDATA_DOUBLE)0x00ffffff) { n /= factor; m++; } if(m) { - // the value was too big and we divided it - // so we add a multiplier to unpack it - r += (1 << 30) + (m << 27); // the multiplier m + // the value was too big, and we divided it + // so, we add a multiplier to unpack it + r += SN_FLAG_MULTIPLY + (m << 27); // the multiplier m - if(n > (calculated_number)0x00ffffff) { + if(n > (NETDATA_DOUBLE)0x00ffffff) { #ifdef NETDATA_INTERNAL_CHECKS - error("Number " CALCULATED_NUMBER_FORMAT " is too big.", value); + error("Number " NETDATA_DOUBLE_FORMAT " is too big.", value); #endif r += 0x00ffffff; - goto RET_SN; + return r; } } else { @@ -62,18 +61,18 @@ storage_number pack_storage_number(calculated_number value, uint32_t flags) { // while the value is below 0x0019999e we can // multiply it by 10, up to 7 times, increasing // the multiplier - while(m < 7 && n < (calculated_number)0x0019999e) { + while(m < 7 && n < (NETDATA_DOUBLE)0x0019999e) { n *= 10; m++; } - if (unlikely(n > (calculated_number) (0x00ffffff))) { + if (unlikely(n > (NETDATA_DOUBLE)0x00ffffff)) { n /= 10; m--; } - // the value was small enough and we multiplied it - // so we add a divider to unpack it - r += (0 << 30) + (m << 27); // the divider m + // the value was small enough, and we multiplied it + // so, we add a divider to unpack it + r += (m << 27); // the divider m } #ifdef STORAGE_WITH_MATH @@ -84,15 +83,11 @@ storage_number pack_storage_number(calculated_number value, uint32_t flags) { r += (storage_number)n; #endif -RET_SN: - if (r == SN_EMPTY_SLOT) - r = SN_ANOMALOUS_ZERO; - return r; } // Lookup table to make storage number unpacking efficient. -calculated_number unpack_storage_number_lut10x[4 * 8]; +NETDATA_DOUBLE unpack_storage_number_lut10x[4 * 8]; __attribute__((constructor)) void initialize_lut(void) { // The lookup table is partitioned in 4 subtables based on the @@ -109,7 +104,7 @@ __attribute__((constructor)) void initialize_lut(void) { } /* -int print_calculated_number(char *str, calculated_number value) +int print_netdata_double(char *str, NETDATA_DOUBLE value) { char *wstr = str; @@ -119,9 +114,9 @@ int print_calculated_number(char *str, calculated_number value) #ifdef STORAGE_WITH_MATH // without llrintl() there are rounding problems // for example 0.9 becomes 0.89 - unsigned long long uvalue = (unsigned long long int) llrintl(value * (calculated_number)100000); + unsigned long long uvalue = (unsigned long long int) llrintl(value * (NETDATA_DOUBLE)100000); #else - unsigned long long uvalue = value * (calculated_number)100000; + unsigned long long uvalue = value * (NETDATA_DOUBLE)100000; #endif wstr = print_number_llu_r_smart(str, uvalue); @@ -165,8 +160,8 @@ int print_calculated_number(char *str, calculated_number value) } */ -int print_calculated_number(char *str, calculated_number value) { - // info("printing number " CALCULATED_NUMBER_FORMAT, value); +int print_netdata_double(char *str, NETDATA_DOUBLE value) { + // info("printing number " NETDATA_DOUBLE_FORMAT, value); char integral_str[50], fractional_str[50]; char *wstr = str; @@ -176,22 +171,22 @@ int print_calculated_number(char *str, calculated_number value) { value = -value; } - calculated_number integral, fractional; + NETDATA_DOUBLE integral, fractional; #ifdef STORAGE_WITH_MATH - fractional = calculated_number_modf(value, &integral) * 10000000.0; + fractional = modfndd(value, &integral) * 10000000.0; #else fractional = ((unsigned long long)(value * 10000000ULL) % 10000000ULL); #endif unsigned long long integral_int = (unsigned long long)integral; - unsigned long long fractional_int = (unsigned long long)calculated_number_llrint(fractional); + unsigned long long fractional_int = (unsigned long long)llrintndd(fractional); if(unlikely(fractional_int >= 10000000)) { integral_int += 1; fractional_int -= 10000000; } - // info("integral " CALCULATED_NUMBER_FORMAT " (%llu), fractional " CALCULATED_NUMBER_FORMAT " (%llu)", integral, integral_int, fractional, fractional_int); + // info("integral " NETDATA_DOUBLE_FORMAT " (%llu), fractional " NETDATA_DOUBLE_FORMAT " (%llu)", integral, integral_int, fractional, fractional_int); char *istre; if(unlikely(integral_int == 0)) { diff --git a/libnetdata/storage_number/storage_number.h b/libnetdata/storage_number/storage_number.h index 7e7b511b0..faea47751 100644 --- a/libnetdata/storage_number/storage_number.h +++ b/libnetdata/storage_number/storage_number.h @@ -3,92 +3,114 @@ #ifndef NETDATA_STORAGE_NUMBER_H #define NETDATA_STORAGE_NUMBER_H 1 +#include <math.h> #include "../libnetdata.h" -#ifdef NETDATA_WITHOUT_LONG_DOUBLE +#ifdef NETDATA_WITH_LONG_DOUBLE -#define powl pow -#define modfl modf -#define llrintl llrint -#define roundl round -#define sqrtl sqrt -#define copysignl copysign -#define strtold strtod +typedef long double NETDATA_DOUBLE; +#define NETDATA_DOUBLE_FORMAT "%0.7Lf" +#define NETDATA_DOUBLE_FORMAT_ZERO "%0.0Lf" +#define NETDATA_DOUBLE_FORMAT_AUTO "%Lf" +#define NETDATA_DOUBLE_MODIFIER "Lf" -typedef double calculated_number; -#define CALCULATED_NUMBER_FORMAT "%0.7f" -#define CALCULATED_NUMBER_FORMAT_ZERO "%0.0f" -#define CALCULATED_NUMBER_FORMAT_AUTO "%f" +#define NETDATA_DOUBLE_MAX LDBL_MAX -#define LONG_DOUBLE_MODIFIER "f" -typedef double LONG_DOUBLE; +#define strtondd(s, endptr) strtold(s, endptr) +#define powndd(x, y) powl(x, y) +#define llrintndd(x) llrintl(x) +#define roundndd(x) roundl(x) +#define sqrtndd(x) sqrtl(x) +#define copysignndd(x, y) copysignl(x, y) +#define modfndd(x, y) modfl(x, y) +#define fabsndd(x) fabsl(x) -#else // NETDATA_WITHOUT_LONG_DOUBLE +#else // NETDATA_WITH_LONG_DOUBLE -typedef long double calculated_number; -#define CALCULATED_NUMBER_FORMAT "%0.7Lf" -#define CALCULATED_NUMBER_FORMAT_ZERO "%0.0Lf" -#define CALCULATED_NUMBER_FORMAT_AUTO "%Lf" +typedef double NETDATA_DOUBLE; +#define NETDATA_DOUBLE_FORMAT "%0.7f" +#define NETDATA_DOUBLE_FORMAT_ZERO "%0.0f" +#define NETDATA_DOUBLE_FORMAT_AUTO "%f" +#define NETDATA_DOUBLE_MODIFIER "f" -#define LONG_DOUBLE_MODIFIER "Lf" -typedef long double LONG_DOUBLE; +#define NETDATA_DOUBLE_MAX DBL_MAX -#endif // NETDATA_WITHOUT_LONG_DOUBLE +#define strtondd(s, endptr) strtod(s, endptr) +#define powndd(x, y) pow(x, y) +#define llrintndd(x) llrint(x) +#define roundndd(x) round(x) +#define sqrtndd(x) sqrt(x) +#define copysignndd(x, y) copysign(x, y) +#define modfndd(x, y) modf(x, y) +#define fabsndd(x) fabs(x) -//typedef long long calculated_number; -//#define CALCULATED_NUMBER_FORMAT "%lld" +#endif // NETDATA_WITH_LONG_DOUBLE typedef long long collected_number; #define COLLECTED_NUMBER_FORMAT "%lld" -/* -typedef long double collected_number; -#define COLLECTED_NUMBER_FORMAT "%0.7Lf" -*/ +#define epsilonndd (NETDATA_DOUBLE)0.0000001 +#define considered_equal_ndd(a, b) (fabsndd((a) - (b)) < epsilonndd) -#define calculated_number_modf(x, y) modfl(x, y) -#define calculated_number_llrint(x) llrintl(x) -#define calculated_number_round(x) roundl(x) -#define calculated_number_fabs(x) fabsl(x) -#define calculated_number_pow(x, y) powl(x, y) -#define calculated_number_epsilon (calculated_number)0.0000001 +#if defined(HAVE_ISFINITE) || defined(isfinite) +// The isfinite() macro shall determine whether its argument has a +// finite value (zero, subnormal, or normal, and not infinite or NaN). +#define netdata_double_isnumber(a) (isfinite(a)) +#elif defined(HAVE_FINITE) || defined(finite) +#define netdata_double_isnumber(a) (finite(a)) +#else +#define netdata_double_isnumber(a) (fpclassify(a) != FP_NAN && fpclassify(a) != FP_INFINITE) +#endif -#define calculated_number_equal(a, b) (calculated_number_fabs((a) - (b)) < calculated_number_epsilon) +typedef uint32_t storage_number; -#define calculated_number_isnumber(a) (!(fpclassify(a) & (FP_NAN|FP_INFINITE))) +typedef struct storage_number_tier1 { + float sum_value; + float min_value; + float max_value; + uint16_t count; + uint16_t anomaly_count; +} storage_number_tier1_t; -typedef uint32_t storage_number; #define STORAGE_NUMBER_FORMAT "%u" -#define SN_ANOMALY_BIT (1 << 24) // the anomaly bit of the value -#define SN_EXISTS_RESET (1 << 25) // the value has been overflown -#define SN_EXISTS_100 (1 << 26) // very large value (multiplier is 100 instead of 10) +typedef enum { + SN_FLAG_NONE = 0, + SN_FLAG_NOT_ANOMALOUS = (1 << 24), // the anomaly bit of the value (0:anomalous, 1:not anomalous) + SN_FLAG_RESET = (1 << 25), // the value has been overflown + SN_FLAG_NOT_EXISTS_MUL100 = (1 << 26), // very large value (multiplier is 100 instead of 10) + SN_FLAG_MULTIPLY = (1 << 30), // multiply, else divide + SN_FLAG_NEGATIVE = (1 << 31), // negative, else positive +} SN_FLAGS; -#define SN_DEFAULT_FLAGS SN_ANOMALY_BIT +#define SN_USER_FLAGS (SN_FLAG_NOT_ANOMALOUS | SN_FLAG_RESET) -#define SN_EMPTY_SLOT 0x00000000 +// default flags for all storage numbers +// anomaly bit is reversed, so we set it by default +#define SN_DEFAULT_FLAGS SN_FLAG_NOT_ANOMALOUS // When the calculated number is zero and the value is anomalous (ie. it's bit // is zero) we want to return a storage_number representation that is // different from the empty slot. We achieve this by mapping zero to // SN_EXISTS_100. Unpacking the SN_EXISTS_100 value will return zero because // its fraction field (as well as its exponent factor field) will be zero. -#define SN_ANOMALOUS_ZERO SN_EXISTS_100 +#define SN_EMPTY_SLOT SN_FLAG_NOT_EXISTS_MUL100 // checks -#define does_storage_number_exist(value) (((storage_number) (value)) != SN_EMPTY_SLOT) -#define did_storage_number_reset(value) ((((storage_number) (value)) & SN_EXISTS_RESET) != 0) +#define does_storage_number_exist(value) (((storage_number)(value)) != SN_EMPTY_SLOT) +#define did_storage_number_reset(value) ((((storage_number)(value)) & SN_FLAG_RESET)) +#define is_storage_number_anomalous(value) (does_storage_number_exist(value) && !(((storage_number)(value)) & SN_FLAG_NOT_ANOMALOUS)) -storage_number pack_storage_number(calculated_number value, uint32_t flags); -static inline calculated_number unpack_storage_number(storage_number value) __attribute__((const)); +storage_number pack_storage_number(NETDATA_DOUBLE value, SN_FLAGS flags) __attribute__((const)); +static inline NETDATA_DOUBLE unpack_storage_number(storage_number value) __attribute__((const)); -int print_calculated_number(char *str, calculated_number value); +int print_netdata_double(char *str, NETDATA_DOUBLE value); -// sign div/mul <--- multiplier / divider ---> 10/100 RESET EXISTS VALUE -#define STORAGE_NUMBER_POSITIVE_MAX_RAW (storage_number)( (0 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff ) -#define STORAGE_NUMBER_POSITIVE_MIN_RAW (storage_number)( (0 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 ) -#define STORAGE_NUMBER_NEGATIVE_MAX_RAW (storage_number)( (1 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 ) -#define STORAGE_NUMBER_NEGATIVE_MIN_RAW (storage_number)( (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff ) +// sign div/mul <--- multiplier / divider ---> 10/100 RESET EXISTS VALUE +#define STORAGE_NUMBER_POSITIVE_MAX_RAW (storage_number)( (0 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1 << 27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff ) +#define STORAGE_NUMBER_POSITIVE_MIN_RAW (storage_number)( (0 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1 << 27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 ) +#define STORAGE_NUMBER_NEGATIVE_MAX_RAW (storage_number)( (1 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1 << 27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 ) +#define STORAGE_NUMBER_NEGATIVE_MIN_RAW (storage_number)( (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1 << 27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff ) // accepted accuracy loss #define ACCURACY_LOSS_ACCEPTED_PERCENT 0.0001 @@ -99,40 +121,112 @@ int print_calculated_number(char *str, calculated_number value); #define MAX_INCREMENTAL_PERCENT_RATE 10 -static inline calculated_number unpack_storage_number(storage_number value) { - extern calculated_number unpack_storage_number_lut10x[4 * 8]; +static inline NETDATA_DOUBLE unpack_storage_number(storage_number value) { + extern NETDATA_DOUBLE unpack_storage_number_lut10x[4 * 8]; - if(!value) return 0; + if(unlikely(value == SN_EMPTY_SLOT)) + return NAN; int sign = 1, exp = 0; int factor = 0; // bit 32 = 0:positive, 1:negative - if(unlikely(value & (1 << 31))) + if(unlikely(value & SN_FLAG_NEGATIVE)) sign = -1; // bit 31 = 0:divide, 1:multiply - if(unlikely(value & (1 << 30))) + if(unlikely(value & SN_FLAG_MULTIPLY)) exp = 1; - // bit 27 SN_EXISTS_100 - if(unlikely(value & (1 << 26))) + // bit 27 SN_FLAG_NOT_EXISTS_MUL100 + if(unlikely(value & SN_FLAG_NOT_EXISTS_MUL100)) factor = 1; - // bit 26 SN_EXISTS_RESET - // bit 25 SN_ANOMALY_BIT + // bit 26 SN_FLAG_RESET + // bit 25 SN_FLAG_NOT_ANOMALOUS // bit 30, 29, 28 = (multiplier or divider) 0-7 (8 total) - int mul = (value & ((1<<29)|(1<<28)|(1<<27))) >> 27; + int mul = (int)((value & ((1<<29)|(1<<28)|(1<<27))) >> 27); // bit 24 to bit 1 = the value, so remove all other bits value ^= value & ((1<<31)|(1<<30)|(1<<29)|(1<<28)|(1<<27)|(1<<26)|(1<<25)|(1<<24)); - calculated_number n = value; + NETDATA_DOUBLE n = value; // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, factor = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, factor, n); return sign * unpack_storage_number_lut10x[(factor * 16) + (exp * 8) + mul] * n; } +static inline NETDATA_DOUBLE str2ndd(const char *s, char **endptr) { + int negative = 0; + const char *start = s; + unsigned long long integer_part = 0; + unsigned long decimal_part = 0; + size_t decimal_digits = 0; + + switch(*s) { + case '-': + s++; + negative = 1; + break; + + case '+': + s++; + break; + + case 'n': + if(s[1] == 'a' && s[2] == 'n') { + if(endptr) *endptr = (char *)&s[3]; + return NAN; + } + break; + + case 'i': + if(s[1] == 'n' && s[2] == 'f') { + if(endptr) *endptr = (char *)&s[3]; + return INFINITY; + } + break; + + default: + break; + } + + while (*s >= '0' && *s <= '9') { + integer_part = (integer_part * 10) + (*s - '0'); + s++; + } + + if(unlikely(*s == '.')) { + decimal_part = 0; + s++; + + while (*s >= '0' && *s <= '9') { + decimal_part = (decimal_part * 10) + (*s - '0'); + s++; + decimal_digits++; + } + } + + if(unlikely(*s == 'e' || *s == 'E')) + return strtondd(start, endptr); + + if(unlikely(endptr)) + *endptr = (char *)s; + + if(unlikely(negative)) { + if(unlikely(decimal_digits)) + return -((NETDATA_DOUBLE)integer_part + (NETDATA_DOUBLE)decimal_part / powndd(10.0, decimal_digits)); + else + return -((NETDATA_DOUBLE)integer_part); + } + else { + if(unlikely(decimal_digits)) + return (NETDATA_DOUBLE)integer_part + (NETDATA_DOUBLE)decimal_part / powndd(10.0, decimal_digits); + else + return (NETDATA_DOUBLE)integer_part; + } +} + #endif /* NETDATA_STORAGE_NUMBER_H */ diff --git a/libnetdata/storage_number/tests/test_storage_number.c b/libnetdata/storage_number/tests/test_storage_number.c index f90521cab..19309e5c2 100644 --- a/libnetdata/storage_number/tests/test_storage_number.c +++ b/libnetdata/storage_number/tests/test_storage_number.c @@ -11,34 +11,34 @@ static void test_number_printing(void **state) char value[50]; - print_calculated_number(value, 0); + print_netdata_double(value, 0); assert_string_equal(value, "0"); - print_calculated_number(value, 0.0000001); + print_netdata_double(value, 0.0000001); assert_string_equal(value, "0.0000001"); - print_calculated_number(value, 0.00000009); + print_netdata_double(value, 0.00000009); assert_string_equal(value, "0.0000001"); - print_calculated_number(value, 0.000000001); + print_netdata_double(value, 0.000000001); assert_string_equal(value, "0"); - print_calculated_number(value, 99.99999999999999999); + print_netdata_double(value, 99.99999999999999999); assert_string_equal(value, "100"); - print_calculated_number(value, -99.99999999999999999); + print_netdata_double(value, -99.99999999999999999); assert_string_equal(value, "-100"); - print_calculated_number(value, 123.4567890123456789); + print_netdata_double(value, 123.4567890123456789); assert_string_equal(value, "123.456789"); - print_calculated_number(value, 9999.9999999); + print_netdata_double(value, 9999.9999999); assert_string_equal(value, "9999.9999999"); - print_calculated_number(value, -9999.9999999); + print_netdata_double(value, -9999.9999999); assert_string_equal(value, "-9999.9999999"); - print_calculated_number(value, unpack_storage_number(pack_storage_number(16.777218L, SN_DEFAULT_FLAGS))); + print_netdata_double(value, unpack_storage_number(pack_storage_number(16.777218L, SN_DEFAULT_FLAGS))); assert_string_equal(value, "16.77722"); } |