From: John Spray Date: Wed, 30 Aug 2017 19:32:11 +0000 (+0100) Subject: mgr/status: format byte quantities in base 2 multiples X-Git-Tag: v12.2.2~146^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0618f19128d0eb04004e65e6eab477b7b1b7fb6a;p=ceph.git mgr/status: format byte quantities in base 2 multiples Fixes: http://tracker.ceph.com/issues/21189 Signed-off-by: John Spray (cherry picked from commit 2a5ae210928f8c9f1e3b72863c277ae5655bb771) --- diff --git a/src/pybind/mgr/status/module.py b/src/pybind/mgr/status/module.py index 35b9dbb66b81..249baad3f48b 100644 --- a/src/pybind/mgr/status/module.py +++ b/src/pybind/mgr/status/module.py @@ -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()