From e34a759889c4b9ccd7d8348abf21760e276f7d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Wed, 2 Dec 2020 22:26:30 +0000 Subject: [PATCH] avoid global ResourceConstraints. --- node/builder.go | 2 ++ node/modules/core.go | 18 ++++++++++++++---- system/resources.go | 23 +++++++++-------------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/node/builder.go b/node/builder.go index 6986a6d7d..8e1617f18 100644 --- a/node/builder.go +++ b/node/builder.go @@ -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 { diff --git a/node/modules/core.go b/node/modules/core.go index ec88be8fe..ba284d0b4 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -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 } diff --git a/system/resources.go b/system/resources.go index b486c2c2c..4c0d38943 100644 --- a/system/resources.go +++ b/system/resources.go @@ -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 }