aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/counters/counters.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/monlib/counters/counters.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/monlib/counters/counters.cpp')
-rw-r--r--library/cpp/monlib/counters/counters.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/library/cpp/monlib/counters/counters.cpp b/library/cpp/monlib/counters/counters.cpp
new file mode 100644
index 0000000000..50dca4c577
--- /dev/null
+++ b/library/cpp/monlib/counters/counters.cpp
@@ -0,0 +1,49 @@
+
+#include "counters.h"
+
+namespace NMonitoring {
+ char* PrettyNumShort(i64 val, char* buf, size_t size) {
+ static const char shorts[] = {' ', 'K', 'M', 'G', 'T', 'P', 'E'};
+ unsigned i = 0;
+ i64 major = val;
+ i64 minor = 0;
+ const unsigned imax = sizeof(shorts) / sizeof(char);
+ for (i = 0; i < imax; i++) {
+ if (major >> 10 == 0)
+ break;
+ else {
+ minor = major - (major >> 10 << 10);
+ major = major >> 10;
+ }
+ }
+ minor = (minor * 10) >> 10;
+
+ if (i == 0 || i >= imax)
+ *buf = '\0';
+ else
+ snprintf(buf, size, "%" PRId64 ".%" PRId64 "%c", major, minor, shorts[i]);
+
+ return buf;
+ }
+
+ char* PrettyNum(i64 val, char* buf, size_t size) {
+ Y_ASSERT(buf);
+ if (size < 4) {
+ buf[0] = 0;
+ return buf;
+ }
+ PrettyNumShort(val, buf + 2, size - 3);
+ if (buf[2] == 0) {
+ *buf = '\0';
+ } else {
+ size_t len = 2 + strnlen(buf + 2, size - 4);
+ Y_ASSERT(len < size);
+ buf[0] = ' ';
+ buf[1] = '(';
+ buf[len] = ')';
+ buf[len + 1] = '\0';
+ }
+
+ return buf;
+ }
+}