Add comment and print units

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-07-26 10:26:58 +02:00
parent ba22b10113
commit 208d21a393

View file

@ -21,30 +21,33 @@ import (
"syscall" "syscall"
) )
// syscall.RLIM_INFINITY is a constant and its default type is int.
// It needs to be converted to an int64 variable to be compared with uint64 values.
// See https://golang.org/ref/spec#Conversions
var unlimited int64 = syscall.RLIM_INFINITY var unlimited int64 = syscall.RLIM_INFINITY
func limitToString(v uint64) string { func limitToString(v uint64, unit string) string {
if v == uint64(unlimited) { if v == uint64(unlimited) {
return "unlimited" return "unlimited"
} }
return fmt.Sprintf("%d", v) return fmt.Sprintf("%d%s", v, unit)
} }
func getLimits(resource int) string { func getLimits(resource int, unit string) string {
rlimit := syscall.Rlimit{} rlimit := syscall.Rlimit{}
err := syscall.Getrlimit(resource, &rlimit) err := syscall.Getrlimit(resource, &rlimit)
if err != nil { if err != nil {
log.Fatal("Error!") log.Fatal("Error!")
} }
return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(rlimit.Cur), limitToString(rlimit.Max)) return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(rlimit.Cur, unit), limitToString(rlimit.Max, unit))
} }
// FdLimits returns the soft and hard limits for file descriptors. // FdLimits returns the soft and hard limits for file descriptors.
func FdLimits() string { func FdLimits() string {
return getLimits(syscall.RLIMIT_NOFILE) return getLimits(syscall.RLIMIT_NOFILE, "")
} }
// VmLimits returns the soft and hard limits for virtual memory. // VmLimits returns the soft and hard limits for virtual memory.
func VmLimits() string { func VmLimits() string {
return getLimits(syscall.RLIMIT_AS) return getLimits(syscall.RLIMIT_AS, "b")
} }