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
+2
View File
@@ -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 {
+14 -4
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
// 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
}