mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 13:21:37 +00:00
Currently geth ignores the docker `--memory` directive and doesn't adjust its cache size downward when necessary, potentially running into OOM. while gopsutil has functions like `docker/CgroupMem()` they are rather for reading cgroup memory limit of a container from the host.
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
// Copyright 2026 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package memlimit detects the effective memory limit of the current
|
|
// process. On Linux the cgroup limit is consulted first, with total
|
|
// system memory as the fallback on all platforms.
|
|
package memlimit
|
|
|
|
import (
|
|
gopsutil "github.com/shirou/gopsutil/mem"
|
|
)
|
|
|
|
// Source identifies which mechanism produced the limit value.
|
|
type Source int
|
|
|
|
const (
|
|
SourceUnknown Source = iota
|
|
SourceCgroupV2
|
|
SourceCgroupV1
|
|
SourceSystem
|
|
)
|
|
|
|
func (s Source) String() string {
|
|
switch s {
|
|
case SourceCgroupV2:
|
|
return "cgroup-v2"
|
|
case SourceCgroupV1:
|
|
return "cgroup-v1"
|
|
case SourceSystem:
|
|
return "system"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
// Limit returns the memory limit visible to this process in bytes and
|
|
// the source that produced it.
|
|
func Limit() (bytes uint64, source Source) {
|
|
if v, src, ok := platformLimit(); ok {
|
|
return v, src
|
|
}
|
|
if mem, err := gopsutil.VirtualMemory(); err == nil {
|
|
return mem.Total, SourceSystem
|
|
}
|
|
return 0, SourceUnknown
|
|
}
|