From 769549a2710faf032e156b14d391351e63433d8a Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Mon, 24 Oct 2022 13:05:36 +0000 Subject: move SetResourceLimit to util --- client/go/script-utils/startcbinary/setrlimit.go | 58 -------------------- .../script-utils/startcbinary/setrlimit_windows.go | 18 ------- client/go/script-utils/startcbinary/tuning.go | 6 +-- client/go/util/setrlimit.go | 62 ++++++++++++++++++++++ client/go/util/setrlimit_windows.go | 18 +++++++ 5 files changed, 83 insertions(+), 79 deletions(-) delete mode 100644 client/go/script-utils/startcbinary/setrlimit.go delete mode 100644 client/go/script-utils/startcbinary/setrlimit_windows.go create mode 100644 client/go/util/setrlimit.go create mode 100644 client/go/util/setrlimit_windows.go (limited to 'client') diff --git a/client/go/script-utils/startcbinary/setrlimit.go b/client/go/script-utils/startcbinary/setrlimit.go deleted file mode 100644 index 5d7f33d5a0e..00000000000 --- a/client/go/script-utils/startcbinary/setrlimit.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -//go:build !windows - -package startcbinary - -import ( - "github.com/vespa-engine/vespa/client/go/trace" - "golang.org/x/sys/unix" - "os" -) - -type ResourceId int - -const ( - RLIMIT_CORE ResourceId = unix.RLIMIT_AS - RLIMIT_NOFILE ResourceId = unix.RLIMIT_NOFILE - RLIMIT_NPROC ResourceId = unix.RLIMIT_NPROC - NO_RLIMIT uint64 = ^uint64(0) -) - -func (rid ResourceId) String() string { - switch rid { - case RLIMIT_CORE: - return "core file size" - case RLIMIT_NOFILE: - return "open files" - case RLIMIT_NPROC: - return "max user processes" - } - return "unknown resource id" -} - -func setResourceLimit(resource ResourceId, newVal uint64) { - var current unix.Rlimit - err := unix.Getrlimit(int(resource), ¤t) - if err != nil { - trace.Warning("Could not get current resource limit:", err) - return - } - wanted := current - if current.Max < newVal { - if os.Getuid() == 0 { - wanted.Max = newVal - } else { - newVal = current.Max - } - } - if current.Cur < newVal { - wanted.Cur = newVal - } - err = unix.Setrlimit(int(resource), &wanted) - if err != nil { - trace.Trace("Failed setting resource limit:", err) - } else { - trace.Trace("Resource limit", resource, "adjusted OK:", wanted.Cur, "/", wanted.Max) - } -} diff --git a/client/go/script-utils/startcbinary/setrlimit_windows.go b/client/go/script-utils/startcbinary/setrlimit_windows.go deleted file mode 100644 index 5bed1916246..00000000000 --- a/client/go/script-utils/startcbinary/setrlimit_windows.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -//go:build windows - -package startcbinary - -type ResourceId int - -const ( - RLIMIT_CORE ResourceId = iota - RLIMIT_NOFILE - RLIMIT_NPROC - NO_RLIMIT uint64 = ^uint64(0) -) - -func setResourceLimit(resource ResourceId, max uint64) { - // nop -} diff --git a/client/go/script-utils/startcbinary/tuning.go b/client/go/script-utils/startcbinary/tuning.go index 80fec7cc6c2..dbe4d34dabe 100644 --- a/client/go/script-utils/startcbinary/tuning.go +++ b/client/go/script-utils/startcbinary/tuning.go @@ -44,7 +44,7 @@ func getThpSizeMb() int { func (spec *ProgSpec) configureTuning() { spec.optionallyReduceBaseFrequency() - setResourceLimit(RLIMIT_CORE, NO_RLIMIT) - setResourceLimit(RLIMIT_NOFILE, 262144) - setResourceLimit(RLIMIT_NPROC, 409600) + util.SetResourceLimit(util.RLIMIT_CORE, util.NO_RLIMIT) + util.SetResourceLimit(util.RLIMIT_NOFILE, 262144) + util.SetResourceLimit(util.RLIMIT_NPROC, 409600) } diff --git a/client/go/util/setrlimit.go b/client/go/util/setrlimit.go new file mode 100644 index 00000000000..4ec1ea60a3c --- /dev/null +++ b/client/go/util/setrlimit.go @@ -0,0 +1,62 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +//go:build !windows + +package util + +import ( + "github.com/vespa-engine/vespa/client/go/trace" + "golang.org/x/sys/unix" + "os" +) + +type ResourceId int + +const ( + RLIMIT_CORE ResourceId = unix.RLIMIT_AS + RLIMIT_NOFILE ResourceId = unix.RLIMIT_NOFILE + RLIMIT_NPROC ResourceId = unix.RLIMIT_NPROC + NO_RLIMIT uint64 = ^uint64(0) +) + +func (rid ResourceId) String() string { + switch rid { + case RLIMIT_CORE: + return "core file size" + case RLIMIT_NOFILE: + return "open files" + case RLIMIT_NPROC: + return "max user processes" + } + return "unknown resource id" +} + +func SetResourceLimit(resource ResourceId, newVal uint64) { + var current unix.Rlimit + err := unix.Getrlimit(int(resource), ¤t) + if err != nil { + trace.Warning("Could not get current resource limit:", err) + return + } + wanted := current + if current.Max < newVal { + if os.Getuid() == 0 { + wanted.Max = newVal + } else if newVal > current.Max { + trace.Warning( + "Wanted", newVal, + "as limit for", resource.String(), + "but cannot exceed current hard limit:", current.Max) + newVal = current.Max + } + } + if current.Cur < newVal { + wanted.Cur = newVal + } + err = unix.Setrlimit(int(resource), &wanted) + if err != nil { + trace.Trace("Failed setting resource limit:", err) + } else { + trace.Trace("Resource limit", resource, "adjusted OK:", wanted.Cur, "/", wanted.Max) + } +} diff --git a/client/go/util/setrlimit_windows.go b/client/go/util/setrlimit_windows.go new file mode 100644 index 00000000000..c40b1cb1364 --- /dev/null +++ b/client/go/util/setrlimit_windows.go @@ -0,0 +1,18 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +//go:build windows + +package util + +type ResourceId int + +const ( + RLIMIT_CORE ResourceId = iota + RLIMIT_NOFILE + RLIMIT_NPROC + NO_RLIMIT uint64 = ^uint64(0) +) + +func SetResourceLimit(resource ResourceId, max uint64) { + // nop +} -- cgit v1.2.3