format status memory sizes

This commit is contained in:
Radu Ioan Fericean
2016-04-11 13:17:32 +03:00
parent df7e92268b
commit 5a102384e3
2 changed files with 16 additions and 2 deletions

View File

@@ -592,8 +592,8 @@ func (rs *Responder) Status(arg string, reply *map[string]interface{}) (err erro
if rs.Bal != nil {
response["Raters"] = rs.Bal.GetClientAddresses()
}
response["memstat"] = memstats.HeapAlloc / 1024
response["footprint"] = memstats.Sys / 1024
response["memstat"] = utils.SizeFmt(float64(memstats.HeapAlloc), "")
response["footprint"] = utils.SizeFmt(float64(memstats.Sys), "")
*reply = response
return
}

View File

@@ -533,3 +533,17 @@ func GetEndOfMonth(ref time.Time) time.Time {
eom := time.Date(year, month, 1, 0, 0, 0, 0, ref.Location())
return eom.Add(-time.Second)
}
// formats number in K,M,G, etc.
func SizeFmt(num float64, suffix string) string {
if suffix == "" {
suffix = "B"
}
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
if math.Abs(num) < 1024.0 {
return fmt.Sprintf("%3.1f%s%s", num, unit, suffix)
}
num /= 1024.0
}
return fmt.Sprintf("%.1f%s%s", num, "Yi", suffix)
}