2017-12-11 13:01:53 +00:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2021-08-25 14:35:30 +00:00
|
|
|
//go:build !windows
|
2017-12-11 13:01:53 +00:00
|
|
|
|
2018-08-22 10:41:11 +00:00
|
|
|
package runtime
|
2017-12-11 13:01:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-06 03:07:10 +00:00
|
|
|
"math"
|
2017-12-11 13:01:53 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2023-04-06 03:07:10 +00:00
|
|
|
// syscall.RLIM_INFINITY is a constant.
|
|
|
|
// Its type is int on most architectures but there are exceptions such as loong64.
|
2024-09-10 20:32:03 +00:00
|
|
|
// Uniform it to uint according to the standard.
|
2023-04-06 03:07:10 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_resource.h.html
|
|
|
|
var unlimited uint64 = syscall.RLIM_INFINITY & math.MaxUint64
|
2018-07-25 13:51:27 +00:00
|
|
|
|
2018-07-26 08:26:58 +00:00
|
|
|
func limitToString(v uint64, unit string) string {
|
2023-04-06 03:07:10 +00:00
|
|
|
if v == unlimited {
|
2018-07-25 13:51:27 +00:00
|
|
|
return "unlimited"
|
|
|
|
}
|
2018-07-26 08:26:58 +00:00
|
|
|
return fmt.Sprintf("%d%s", v, unit)
|
2018-07-25 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 08:26:58 +00:00
|
|
|
func getLimits(resource int, unit string) string {
|
2018-07-25 13:51:27 +00:00
|
|
|
rlimit := syscall.Rlimit{}
|
|
|
|
err := syscall.Getrlimit(resource, &rlimit)
|
2017-12-11 13:01:53 +00:00
|
|
|
if err != nil {
|
2019-01-16 21:50:56 +00:00
|
|
|
panic("syscall.Getrlimit failed: " + err.Error())
|
2017-12-11 13:01:53 +00:00
|
|
|
}
|
2023-04-20 14:19:50 +00:00
|
|
|
// rlimit.Cur and rlimit.Max are int64 on some platforms, such as dragonfly.
|
|
|
|
// We need to cast them explicitly to uint64.
|
|
|
|
return fmt.Sprintf("(soft=%s, hard=%s)", limitToString(uint64(rlimit.Cur), unit), limitToString(uint64(rlimit.Max), unit)) //nolint:unconvert
|
2018-07-25 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FdLimits returns the soft and hard limits for file descriptors.
|
|
|
|
func FdLimits() string {
|
2018-07-26 08:26:58 +00:00
|
|
|
return getLimits(syscall.RLIMIT_NOFILE, "")
|
2018-07-25 13:51:27 +00:00
|
|
|
}
|