avoid global ResourceConstraints.
This commit is contained in:
parent
2ef8acdfc8
commit
e34a759889
@ -15,6 +15,7 @@ import (
|
||||
"github.com/filecoin-project/lotus/chain/vm"
|
||||
"github.com/filecoin-project/lotus/chain/wallet"
|
||||
"github.com/filecoin-project/lotus/node/hello"
|
||||
"github.com/filecoin-project/lotus/system"
|
||||
|
||||
logging "github.com/ipfs/go-log"
|
||||
ci "github.com/libp2p/go-libp2p-core/crypto"
|
||||
@ -176,6 +177,7 @@ func defaults() []Option {
|
||||
Override(new(journal.DisabledEvents), journal.EnvDisabledEvents),
|
||||
Override(new(journal.Journal), modules.OpenFilesystemJournal),
|
||||
|
||||
Override(new(system.MemoryConstraints), modules.MemoryConstraints),
|
||||
Override(InitMemoryWatchdog, modules.MemoryWatchdog),
|
||||
|
||||
Override(new(helpers.MetricsCtx), func() context.Context {
|
||||
|
@ -49,9 +49,19 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator {
|
||||
}
|
||||
}
|
||||
|
||||
// MemoryConstraints returns the memory constraints configured for this system.
|
||||
func MemoryConstraints() system.MemoryConstraints {
|
||||
constraints := system.GetMemoryConstraints()
|
||||
log.Infow("memory limits initialized",
|
||||
"max_mem_heap", constraints.MaxHeapMem,
|
||||
"total_system_mem", constraints.TotalSystemMem,
|
||||
"effective_mem_limit", constraints.EffectiveMemLimit)
|
||||
return constraints
|
||||
}
|
||||
|
||||
// MemoryWatchdog starts the memory watchdog, applying the computed resource
|
||||
// constraints.
|
||||
func MemoryWatchdog(lc fx.Lifecycle) {
|
||||
func MemoryWatchdog(lc fx.Lifecycle, constraints system.MemoryConstraints) {
|
||||
cfg := watchdog.MemConfig{
|
||||
Resolution: 10 * time.Second,
|
||||
Policy: &watchdog.WatermarkPolicy{
|
||||
@ -64,13 +74,13 @@ func MemoryWatchdog(lc fx.Lifecycle) {
|
||||
|
||||
// if user has set max heap limit, apply it. Otherwise, fall back to total
|
||||
// system memory constraint.
|
||||
if maxHeap := system.ResourceConstraints.MaxHeapMem; maxHeap != 0 {
|
||||
if maxHeap := constraints.MaxHeapMem; maxHeap != 0 {
|
||||
log.Infof("memory watchdog will apply max heap constraint: %d bytes", maxHeap)
|
||||
cfg.Limit = maxHeap
|
||||
cfg.Scope = watchdog.ScopeHeap
|
||||
} else {
|
||||
log.Infof("max heap size not provided; memory watchdog will apply total system memory constraint: %d bytes", system.ResourceConstraints.TotalSystemMem)
|
||||
cfg.Limit = system.ResourceConstraints.TotalSystemMem
|
||||
log.Infof("max heap size not provided; memory watchdog will apply total system memory constraint: %d bytes", constraints.TotalSystemMem)
|
||||
cfg.Limit = constraints.TotalSystemMem
|
||||
cfg.Scope = watchdog.ScopeSystem
|
||||
}
|
||||
|
||||
|
@ -17,11 +17,11 @@ var (
|
||||
// be in bytes, or in SI bytes (e.g. 32GiB).
|
||||
const EnvMaximumHeap = "LOTUS_MAX_HEAP"
|
||||
|
||||
// ResourceConstraints represents resource constraints that Lotus and the go
|
||||
// MemoryConstraints represents resource constraints that Lotus and the go
|
||||
// runtime should abide by. It is a singleton object that's populated on
|
||||
// initialization, and can be used by components for size calculations
|
||||
// (e.g. caches).
|
||||
var ResourceConstraints struct {
|
||||
type MemoryConstraints struct {
|
||||
// MaxHeapMem is the maximum heap memory that has been set by the user
|
||||
// through the LOTUS_MAX_HEAP env variable. If zero, there is no max heap
|
||||
// limit set.
|
||||
@ -40,15 +40,14 @@ var ResourceConstraints struct {
|
||||
EffectiveMemLimit uint64
|
||||
}
|
||||
|
||||
// init populates the global resource constraints for this process.
|
||||
func init() {
|
||||
_ = logging.SetLogLevel("system", "INFO")
|
||||
// GetMemoryConstraints returns the memory constraints for this process.
|
||||
func GetMemoryConstraints() (ret MemoryConstraints) {
|
||||
var mem gosigar.Mem
|
||||
if err := mem.Get(); err != nil {
|
||||
logSystem.Warnf("failed to acquire total system memory: %s", err)
|
||||
} else {
|
||||
ResourceConstraints.TotalSystemMem = mem.Total
|
||||
ResourceConstraints.EffectiveMemLimit = mem.Total
|
||||
ret.TotalSystemMem = mem.Total
|
||||
ret.EffectiveMemLimit = mem.Total
|
||||
}
|
||||
|
||||
if v := os.Getenv(EnvMaximumHeap); v != "" {
|
||||
@ -56,13 +55,9 @@ func init() {
|
||||
if err != nil {
|
||||
logSystem.Warnf("failed to parse %s env variable with value %s: %s; ignoring max heap limit", EnvMaximumHeap, v, err)
|
||||
} else {
|
||||
ResourceConstraints.MaxHeapMem = bytes
|
||||
ResourceConstraints.EffectiveMemLimit = bytes
|
||||
ret.MaxHeapMem = bytes
|
||||
ret.EffectiveMemLimit = bytes
|
||||
}
|
||||
}
|
||||
|
||||
logSystem.Infow("memory limits initialized",
|
||||
"max_mem_heap", ResourceConstraints.MaxHeapMem,
|
||||
"total_system_mem", ResourceConstraints.TotalSystemMem,
|
||||
"effective_mem_limit", ResourceConstraints.EffectiveMemLimit)
|
||||
return ret
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user