From 7fff24e10e9cacc94ec320b87e60c7735ff028b6 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Sun, 30 Jul 2017 16:09:32 -0700 Subject: [PATCH] common: add bytes2str pretty print function Signed-off-by: Patrick Donnelly --- src/common/util.cc | 16 ++++++++++++++++ src/include/util.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/common/util.cc b/src/common/util.cc index 762fe374bb578..4f31b932faa43 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -30,6 +30,10 @@ #include #endif +#include + +#include + int64_t unit_to_bytesize(string val, ostream *pss) { if (val.empty()) { @@ -302,3 +306,15 @@ string cleanbin(string &str) result = "Base64:" + result; return result; } + +std::string bytes2str(uint64_t count) { + static char s[][2] = {"\0", "k", "M", "G", "T", "P", "E", "\0"}; + int i = 0; + while (count >= 1024 && *s[i+1]) { + count >>= 10; + i++; + } + char str[128]; + snprintf(str, sizeof str, "%" PRIu64 "%sB", count, s[i]); + return std::string(str); +} diff --git a/src/include/util.h b/src/include/util.h index b152c1130df94..3de4c3d3ec1e3 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -19,6 +19,8 @@ int64_t unit_to_bytesize(string val, ostream *pss); +std::string bytes2str(uint64_t count); + struct ceph_data_stats { uint64_t byte_total; -- 2.39.5