From: Patrick Donnelly Date: Sun, 30 Jul 2017 23:09:32 +0000 (-0700) Subject: common: add bytes2str pretty print function X-Git-Tag: v13.0.1~926^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7fff24e10e9cacc94ec320b87e60c7735ff028b6;p=ceph.git common: add bytes2str pretty print function Signed-off-by: Patrick Donnelly --- diff --git a/src/common/util.cc b/src/common/util.cc index 762fe374bb5..4f31b932faa 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 b152c1130df..3de4c3d3ec1 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;