mirror of
https://github.com/minio/minio.git
synced 2025-11-09 05:34:56 -05:00
Add cgroup v2 support for memory limit (#18905)
This commit is contained in:
@@ -56,16 +56,31 @@ type apiConfig struct {
|
||||
syncEvents bool
|
||||
}
|
||||
|
||||
const cgroupLimitFile = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
||||
const (
|
||||
cgroupV1MemLimitFile = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
||||
cgroupV2MemLimitFile = "/sys/fs/cgroup/memory.max"
|
||||
cgroupMemNoLimit = 9223372036854771712
|
||||
)
|
||||
|
||||
func cgroupLimit(limitFile string) (limit uint64) {
|
||||
buf, err := os.ReadFile(limitFile)
|
||||
func cgroupMemLimit() (limit uint64) {
|
||||
buf, err := os.ReadFile(cgroupV2MemLimitFile)
|
||||
if err != nil {
|
||||
return 9223372036854771712
|
||||
buf, err = os.ReadFile(cgroupV1MemLimitFile)
|
||||
}
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
limit, err = strconv.ParseUint(string(buf), 10, 64)
|
||||
if err != nil {
|
||||
return 9223372036854771712
|
||||
// The kernel can return valid but non integer values
|
||||
// but still, no need to interpret more
|
||||
return 0
|
||||
}
|
||||
if limit == cgroupMemNoLimit {
|
||||
// No limit set, It's the highest positive signed 64-bit
|
||||
// integer (2^63-1), rounded down to multiples of 4096 (2^12),
|
||||
// the most common page size on x86 systems - for cgroup_limits.
|
||||
return 0
|
||||
}
|
||||
return limit
|
||||
}
|
||||
@@ -74,23 +89,19 @@ func availableMemory() (available uint64) {
|
||||
available = 8 << 30 // Default to 8 GiB when we can't find the limits.
|
||||
|
||||
if runtime.GOOS == "linux" {
|
||||
available = cgroupLimit(cgroupLimitFile)
|
||||
|
||||
// No limit set, It's the highest positive signed 64-bit
|
||||
// integer (2^63-1), rounded down to multiples of 4096 (2^12),
|
||||
// the most common page size on x86 systems - for cgroup_limits.
|
||||
if available != 9223372036854771712 {
|
||||
// This means cgroup memory limit is configured.
|
||||
// Useful in container mode
|
||||
limit := cgroupMemLimit()
|
||||
if limit > 0 {
|
||||
// A valid value is found
|
||||
available = limit
|
||||
return
|
||||
} // no-limit set proceed to set the limits based on virtual memory.
|
||||
|
||||
}
|
||||
} // for all other platforms limits are based on virtual memory.
|
||||
|
||||
memStats, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
available = memStats.Available / 2
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user