]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
types: pretty_si_t
authorSage Weil <sage@inktank.com>
Tue, 13 Aug 2013 23:12:08 +0000 (16:12 -0700)
committerSage Weil <sage@inktank.com>
Tue, 13 Aug 2013 23:51:56 +0000 (16:51 -0700)
Similar to si_t, but leaves a space between the numbers and the units.  In
the degenerate case (no M, K, etc. modifier) there's simply a trailing
space.  For example,

  out << pretty_si_t(num) << "objects/sec";

will look pretty.

Signed-off-by: Sage Weil <sage@inktank.com>
src/include/types.h

index bed00175dd41b17cb2ebd11a2d9bc1fd030dbb73..7e6ddb7117ebf6d8004829d5182da1366a203fc6 100644 (file)
@@ -407,6 +407,29 @@ inline ostream& operator<<(ostream& out, const si_t& b)
   return out << b.v;
 }
 
+struct pretty_si_t {
+  uint64_t v;
+  pretty_si_t(uint64_t _v) : v(_v) {}
+};
+
+inline ostream& operator<<(ostream& out, const pretty_si_t& b)
+{
+  uint64_t bump_after = 100;
+  if (b.v > bump_after << 60)
+    return out << (b.v >> 60) << " E";
+  if (b.v > bump_after << 50)
+    return out << (b.v >> 50) << " P";
+  if (b.v > bump_after << 40)
+    return out << (b.v >> 40) << " T";
+  if (b.v > bump_after << 30)
+    return out << (b.v >> 30) << " G";
+  if (b.v > bump_after << 20)
+    return out << (b.v >> 20) << " M";
+  if (b.v > bump_after << 10)
+    return out << (b.v >> 10) << " K";
+  return out << b.v << " ";
+}
+
 struct kb_t {
   uint64_t v;
   kb_t(uint64_t _v) : v(_v) {}