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