]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/status: format byte quantities in base 2 multiples
authorJohn Spray <jspray@redhat.com>
Wed, 30 Aug 2017 19:32:11 +0000 (20:32 +0100)
committerGitHub <noreply@github.com>
Wed, 30 Aug 2017 19:32:11 +0000 (20:32 +0100)
Fixes: http://tracker.ceph.com/issues/21189
Signed-off-by: John Spray <john.spray@redhat.com>
src/pybind/mgr/status/module.py

index 35b9dbb66b8170fd04463c3fe4debdf8b0708f14..249baad3f48b16259a0e893d6b1c332e094c036f 100644 (file)
@@ -58,18 +58,22 @@ class Module(MgrModule):
         """
         return self.BOLD_SEQ + msg + self.RESET_SEQ
 
-    def format_dimless(self, n, width, colored=True):
+    def format_units(self, n, width, colored, decimal):
         """
         Format a number without units, so as to fit into `width` characters, substituting
         an appropriate unit suffix.
+        
+        Use decimal for dimensionless things, use base 2 (decimal=False) for byte sizes/rates.
         """
+        
+        factor = 1000 if decimal else 1024
         units = [' ', 'k', 'M', 'G', 'T', 'P']
         unit = 0
-        while len("%s" % (int(n) // (1000**unit))) > width - 1:
+        while len("%s" % (int(n) // (factor**unit))) > width - 1:
             unit += 1
 
         if unit > 0:
-            truncated_float = ("%f" % (n / (1000.0 ** unit)))[0:width - 1]
+            truncated_float = ("%f" % (n / (float(factor) ** unit)))[0:width - 1]
             if truncated_float[-1] == '.':
                 truncated_float = " " + truncated_float[0:-1]
         else:
@@ -86,6 +90,12 @@ class Module(MgrModule):
         else:
             return formatted
 
+    def format_dimless(self, n, width, colored=True):
+        return self.format_units(n, width, colored, decimal=True)
+    
+    def format_bytes(self, n, width, colored=True):
+        return self.format_units(n, width, colored, decimal=False)
+        
     def get_latest(self, daemon_type, daemon_name, stat):
         data = self.get_counter(daemon_type, daemon_name, stat)[stat]
         #self.log.error("get_latest {0} data={1}".format(stat, data))
@@ -157,7 +167,7 @@ class Module(MgrModule):
                     activity = ""
 
                     if state == "active":
-                        activity = "Reqs: " + self.format_dimless(
+                        activity = "Reqs: " + self.format_units(
                             self.get_rate("mds", info['name'], "mds_server.handle_client_request"),
                             5
                         ) + "/s"
@@ -209,8 +219,8 @@ class Module(MgrModule):
                 stats = pool_stats[pool_id]
                 pools_table.add_row([
                     pools[pool_id]['pool_name'], pool_type,
-                    self.format_dimless(stats['bytes_used'], 5),
-                    self.format_dimless(stats['max_avail'], 5)
+                    self.format_bytes(stats['bytes_used'], 5),
+                    self.format_bytes(stats['max_avail'], 5)
                 ])
 
             output += "{0} - {1} clients\n".format(
@@ -273,13 +283,13 @@ class Module(MgrModule):
             stats = osd_stats[osd_id]
 
             osd_table.add_row([osd_id, metadata['hostname'],
-                               self.format_dimless(stats['kb_used'] * 1024, 5),
-                               self.format_dimless(stats['kb_avail'] * 1024, 5),
+                               self.format_bytes(stats['kb_used'] * 1024, 5),
+                               self.format_bytes(stats['kb_avail'] * 1024, 5),
                                self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_w") +
                                self.get_rate("osd", osd_id.__str__(), "osd.op_rw"), 5),
-                               self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_in_bytes"), 5),
+                               self.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_in_bytes"), 5),
                                self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_r"), 5),
-                               self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_out_bytes"), 5),
+                               self.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_out_bytes"), 5),
                                ])
 
         return 0, "", osd_table.get_string()