Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
avoid global ResourceConstraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Dec 2, 2020
1 parent 2ef8acd commit e34a759
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions node/modules/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

Expand Down
23 changes: 9 additions & 14 deletions system/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,29 +40,24 @@ 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 != "" {
bytes, err := humanize.ParseBytes(v)
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
}

0 comments on commit e34a759

Please sign in to comment.