From 7d03bee648557b220331c73e83801914152560c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 17 Oct 2018 13:22:23 +0300 Subject: [PATCH] Add type formatting helpers Created the header that contains various helper functions for formatting values into human readable forms. Currently only binary to human readable size conversion is implemented. --- maxutils/maxbase/include/maxbase/format.hh | 32 ++++++++++ maxutils/maxbase/src/CMakeLists.txt | 1 + maxutils/maxbase/src/format.cc | 68 ++++++++++++++++++++++ server/core/config.cc | 23 +------- 4 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 maxutils/maxbase/include/maxbase/format.hh create mode 100644 maxutils/maxbase/src/format.cc diff --git a/maxutils/maxbase/include/maxbase/format.hh b/maxutils/maxbase/include/maxbase/format.hh new file mode 100644 index 000000000..66c6047b1 --- /dev/null +++ b/maxutils/maxbase/include/maxbase/format.hh @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#pragma once + +#include + +#include +#include + +namespace maxbase +{ + +/** + * Convert a number into the IEC human readable representation + * + * @param size Value to convert + * + * @return Value as a human readable size e.g. 5.01MiB + */ +std::string to_binary_size(int64_t size); +} diff --git a/maxutils/maxbase/src/CMakeLists.txt b/maxutils/maxbase/src/CMakeLists.txt index 8cc720f60..0e958370e 100644 --- a/maxutils/maxbase/src/CMakeLists.txt +++ b/maxutils/maxbase/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(maxbase STATIC atomic.cc eventcount.cc + format.cc log.cc logger.cc maxbase.cc diff --git a/maxutils/maxbase/src/format.cc b/maxutils/maxbase/src/format.cc new file mode 100644 index 000000000..8ef1bb28f --- /dev/null +++ b/maxutils/maxbase/src/format.cc @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include + +#include +#include + +namespace +{ + +const char* get_binary_size_suffix(int i) +{ + switch (i) + { + case 0: + return "B"; + + case 1: + return "KiB"; + + case 2: + return "MiB"; + + case 3: + return "GiB"; + + case 4: + return "TiB"; + + case 5: + return "PiB"; + + case 6: + return "EiB"; + + case 7: + return "ZiB"; + + default: + return "YiB"; + } +} +} + +namespace maxbase +{ + +std::string to_binary_size(int64_t size) +{ + // Calculate log1024(size) and round it up + int idx = ceil(log(size) / log(1024)); + double num = size / pow(1024, idx); + char buf[200]; // Enough for all possible values + snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx)); + return buf; +} +} diff --git a/server/core/config.cc b/server/core/config.cc index 06a6538d3..5e81cdcb7 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -1226,26 +1227,8 @@ bool config_load_global(const char* filename) } else { - std::vector suffixes - { - "B", - "KiB", - "MiB", - "GiB", - "TiB", - "PiB", - "EiB", - "ZiB", - "YiB" - }; - // Get the total used cache memory - int64_t total_mem = mem_per_thr * gateway.n_threads; - // Calculate log1024(total_mem) and round it up - double c = ceil(log(total_mem) / log(1024)); - // In case someone still uses MaxScale in the year 3054 - int idx = std::min(c, (double)(suffixes.size())) - 1; - double num = total_mem / pow(1024, idx); - MXS_NOTICE("Using up to %.2lf%s of memory for query classifier cache", num, suffixes[idx]); + MXS_NOTICE("Using up to %s of memory for query classifier cache", + mxb::to_binary_size(mem_per_thr * gateway.n_threads).c_str()); } } }