From ba22b101131ce67aa8189409272d97fd03f4c3a5 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Wed, 25 Jul 2018 15:51:27 +0200 Subject: [PATCH 1/4] prometheus: log virtual memory limits Signed-off-by: Simon Pasquier --- ...{fdlimits_default.go => limits_default.go} | 28 +++++++++++++++---- ...{fdlimits_windows.go => limits_windows.go} | 5 ++++ cmd/prometheus/main.go | 1 + 3 files changed, 29 insertions(+), 5 deletions(-) rename cmd/prometheus/{fdlimits_default.go => limits_default.go} (59%) rename cmd/prometheus/{fdlimits_windows.go => limits_windows.go} (89%) diff --git a/cmd/prometheus/fdlimits_default.go b/cmd/prometheus/limits_default.go similarity index 59% rename from cmd/prometheus/fdlimits_default.go rename to cmd/prometheus/limits_default.go index 197810e28..f93fc6207 100644 --- a/cmd/prometheus/fdlimits_default.go +++ b/cmd/prometheus/limits_default.go @@ -21,12 +21,30 @@ import ( "syscall" ) -// FdLimits returns the soft and hard limits for file descriptors -func FdLimits() string { - flimit := syscall.Rlimit{} - err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &flimit) +var unlimited int64 = syscall.RLIM_INFINITY + +func limitToString(v uint64) string { + if v == uint64(unlimited) { + return "unlimited" + } + return fmt.Sprintf("%d", v) +} + +func getLimits(resource int) string { + rlimit := syscall.Rlimit{} + err := syscall.Getrlimit(resource, &rlimit) if err != nil { log.Fatal("Error!") } - return fmt.Sprintf("(soft=%d, hard=%d)", flimit.Cur, flimit.Max) + return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(rlimit.Cur), limitToString(rlimit.Max)) +} + +// FdLimits returns the soft and hard limits for file descriptors. +func FdLimits() string { + return getLimits(syscall.RLIMIT_NOFILE) +} + +// VmLimits returns the soft and hard limits for virtual memory. +func VmLimits() string { + return getLimits(syscall.RLIMIT_AS) } diff --git a/cmd/prometheus/fdlimits_windows.go b/cmd/prometheus/limits_windows.go similarity index 89% rename from cmd/prometheus/fdlimits_windows.go rename to cmd/prometheus/limits_windows.go index 3fcff4905..d18d58041 100644 --- a/cmd/prometheus/fdlimits_windows.go +++ b/cmd/prometheus/limits_windows.go @@ -19,3 +19,8 @@ package main func FdLimits() string { return "N/A" } + +// VmLimits not supported on Windows +func VmLimits() string { + return "N/A" +} diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 5a44e21f7..271cc939b 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -223,6 +223,7 @@ func main() { level.Info(logger).Log("build_context", version.BuildContext()) level.Info(logger).Log("host_details", Uname()) level.Info(logger).Log("fd_limits", FdLimits()) + level.Info(logger).Log("vm_limits", VmLimits()) var ( localStorage = &tsdb.ReadyStorage{} From 208d21a3936b3696f34dd5005198e0577ca6f031 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Thu, 26 Jul 2018 10:26:58 +0200 Subject: [PATCH 2/4] Add comment and print units Signed-off-by: Simon Pasquier --- cmd/prometheus/limits_default.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/prometheus/limits_default.go b/cmd/prometheus/limits_default.go index f93fc6207..06833065c 100644 --- a/cmd/prometheus/limits_default.go +++ b/cmd/prometheus/limits_default.go @@ -21,30 +21,33 @@ import ( "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 -func limitToString(v uint64) string { +func limitToString(v uint64, unit string) string { if v == uint64(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{} err := syscall.Getrlimit(resource, &rlimit) if err != nil { 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. func FdLimits() string { - return getLimits(syscall.RLIMIT_NOFILE) + return getLimits(syscall.RLIMIT_NOFILE, "") } // VmLimits returns the soft and hard limits for virtual memory. func VmLimits() string { - return getLimits(syscall.RLIMIT_AS) + return getLimits(syscall.RLIMIT_AS, "b") } From 141c188ae6554d142fd554d4aca257a470e66646 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Thu, 26 Jul 2018 14:58:56 +0200 Subject: [PATCH 3/4] Enforce conversion for freebsd Signed-off-by: Simon Pasquier --- cmd/prometheus/limits_default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/prometheus/limits_default.go b/cmd/prometheus/limits_default.go index 06833065c..e2c6b10f6 100644 --- a/cmd/prometheus/limits_default.go +++ b/cmd/prometheus/limits_default.go @@ -39,7 +39,7 @@ func getLimits(resource int, unit string) string { if err != nil { log.Fatal("Error!") } - return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(rlimit.Cur, unit), limitToString(rlimit.Max, unit)) + return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(uint64(rlimit.Cur), unit), limitToString(uint64(rlimit.Max), unit)) } // FdLimits returns the soft and hard limits for file descriptors. From a94450c2889d209dccb73e699870c7832188343c Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Tue, 31 Jul 2018 14:41:30 +0200 Subject: [PATCH 4/4] Fix build for openbsd Signed-off-by: Simon Pasquier --- cmd/prometheus/limits_default.go | 5 ----- cmd/prometheus/vmlimits_default.go | 26 ++++++++++++++++++++++++++ cmd/prometheus/vmlimits_openbsd.go | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 cmd/prometheus/vmlimits_default.go create mode 100644 cmd/prometheus/vmlimits_openbsd.go diff --git a/cmd/prometheus/limits_default.go b/cmd/prometheus/limits_default.go index e2c6b10f6..054b72525 100644 --- a/cmd/prometheus/limits_default.go +++ b/cmd/prometheus/limits_default.go @@ -46,8 +46,3 @@ func getLimits(resource int, unit string) string { func FdLimits() string { return getLimits(syscall.RLIMIT_NOFILE, "") } - -// VmLimits returns the soft and hard limits for virtual memory. -func VmLimits() string { - return getLimits(syscall.RLIMIT_AS, "b") -} diff --git a/cmd/prometheus/vmlimits_default.go b/cmd/prometheus/vmlimits_default.go new file mode 100644 index 000000000..5a3a6802d --- /dev/null +++ b/cmd/prometheus/vmlimits_default.go @@ -0,0 +1,26 @@ +// Copyright 2018 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !windows +// +build !openbsd + +package main + +import ( + "syscall" +) + +// VmLimits returns the soft and hard limits for virtual memory. +func VmLimits() string { + return getLimits(syscall.RLIMIT_AS, "b") +} diff --git a/cmd/prometheus/vmlimits_openbsd.go b/cmd/prometheus/vmlimits_openbsd.go new file mode 100644 index 000000000..b033e6421 --- /dev/null +++ b/cmd/prometheus/vmlimits_openbsd.go @@ -0,0 +1,25 @@ +// Copyright 2018 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build openbsd + +package main + +import ( + "syscall" +) + +// VmLimits returns the soft and hard limits for virtual memory. +func VmLimits() string { + return getLimits(syscall.RLIMIT_DATA, "b") +}