avoid global ResourceConstraints.

This commit is contained in:
Raúl Kripalani 2020-12-02 22:26:30 +00:00
parent 2ef8acdfc8
commit e34a759889
3 changed files with 25 additions and 18 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet" "github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/node/hello" "github.com/filecoin-project/lotus/node/hello"
"github.com/filecoin-project/lotus/system"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
ci "github.com/libp2p/go-libp2p-core/crypto" ci "github.com/libp2p/go-libp2p-core/crypto"
@ -176,6 +177,7 @@ func defaults() []Option {
Override(new(journal.DisabledEvents), journal.EnvDisabledEvents), Override(new(journal.DisabledEvents), journal.EnvDisabledEvents),
Override(new(journal.Journal), modules.OpenFilesystemJournal), Override(new(journal.Journal), modules.OpenFilesystemJournal),
Override(new(system.MemoryConstraints), modules.MemoryConstraints),
Override(InitMemoryWatchdog, modules.MemoryWatchdog), Override(InitMemoryWatchdog, modules.MemoryWatchdog),
Override(new(helpers.MetricsCtx), func() context.Context { Override(new(helpers.MetricsCtx), func() context.Context {

View File

@ -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 // MemoryWatchdog starts the memory watchdog, applying the computed resource
// constraints. // constraints.
func MemoryWatchdog(lc fx.Lifecycle) { func MemoryWatchdog(lc fx.Lifecycle, constraints system.MemoryConstraints) {
cfg := watchdog.MemConfig{ cfg := watchdog.MemConfig{
Resolution: 10 * time.Second, Resolution: 10 * time.Second,
Policy: &watchdog.WatermarkPolicy{ 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 // if user has set max heap limit, apply it. Otherwise, fall back to total
// system memory constraint. // 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) log.Infof("memory watchdog will apply max heap constraint: %d bytes", maxHeap)
cfg.Limit = maxHeap cfg.Limit = maxHeap
cfg.Scope = watchdog.ScopeHeap cfg.Scope = watchdog.ScopeHeap
} else { } else {
log.Infof("max heap size not provided; memory watchdog will apply total system memory constraint: %d bytes", system.ResourceConstraints.TotalSystemMem) log.Infof("max heap size not provided; memory watchdog will apply total system memory constraint: %d bytes", constraints.TotalSystemMem)
cfg.Limit = system.ResourceConstraints.TotalSystemMem cfg.Limit = constraints.TotalSystemMem
cfg.Scope = watchdog.ScopeSystem cfg.Scope = watchdog.ScopeSystem
} }

View File

@ -17,11 +17,11 @@ var (
// be in bytes, or in SI bytes (e.g. 32GiB). // be in bytes, or in SI bytes (e.g. 32GiB).
const EnvMaximumHeap = "LOTUS_MAX_HEAP" 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 // runtime should abide by. It is a singleton object that's populated on
// initialization, and can be used by components for size calculations // initialization, and can be used by components for size calculations
// (e.g. caches). // (e.g. caches).
var ResourceConstraints struct { type MemoryConstraints struct {
// MaxHeapMem is the maximum heap memory that has been set by the user // 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 // through the LOTUS_MAX_HEAP env variable. If zero, there is no max heap
// limit set. // limit set.
@ -40,15 +40,14 @@ var ResourceConstraints struct {
EffectiveMemLimit uint64 EffectiveMemLimit uint64
} }
// init populates the global resource constraints for this process. // GetMemoryConstraints returns the memory constraints for this process.
func init() { func GetMemoryConstraints() (ret MemoryConstraints) {
_ = logging.SetLogLevel("system", "INFO")
var mem gosigar.Mem var mem gosigar.Mem
if err := mem.Get(); err != nil { if err := mem.Get(); err != nil {
logSystem.Warnf("failed to acquire total system memory: %s", err) logSystem.Warnf("failed to acquire total system memory: %s", err)
} else { } else {
ResourceConstraints.TotalSystemMem = mem.Total ret.TotalSystemMem = mem.Total
ResourceConstraints.EffectiveMemLimit = mem.Total ret.EffectiveMemLimit = mem.Total
} }
if v := os.Getenv(EnvMaximumHeap); v != "" { if v := os.Getenv(EnvMaximumHeap); v != "" {
@ -56,13 +55,9 @@ func init() {
if err != nil { if err != nil {
logSystem.Warnf("failed to parse %s env variable with value %s: %s; ignoring max heap limit", EnvMaximumHeap, v, err) logSystem.Warnf("failed to parse %s env variable with value %s: %s; ignoring max heap limit", EnvMaximumHeap, v, err)
} else { } else {
ResourceConstraints.MaxHeapMem = bytes ret.MaxHeapMem = bytes
ResourceConstraints.EffectiveMemLimit = bytes ret.EffectiveMemLimit = bytes
} }
} }
return ret
logSystem.Infow("memory limits initialized",
"max_mem_heap", ResourceConstraints.MaxHeapMem,
"total_system_mem", ResourceConstraints.TotalSystemMem,
"effective_mem_limit", ResourceConstraints.EffectiveMemLimit)
} }