Skip to content

Commit 471129d

Browse files
committed
Boot memory detect & update
1 parent 8c649b2 commit 471129d

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

cmd/cortex/main.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ import (
3131
"github.com/CortexFoundation/CortexTheseus/node"
3232
_ "github.com/CortexFoundation/statik"
3333
"github.com/arsham/figurine/figurine"
34-
gopsutil "github.com/shirou/gopsutil/mem"
3534
cli "gopkg.in/urfave/cli.v1"
36-
"math"
3735
"math/big"
3836
"os"
39-
godebug "runtime/debug"
4037
"sort"
4138
"strconv"
4239
"strings"
@@ -302,28 +299,6 @@ func prepare(ctx *cli.Context) {
302299
log.Info("Bumping default cache on mainnet", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 4096)
303300
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(4096))
304301
}
305-
// Cap the cache allowance and tune the garbage collector
306-
mem, err := gopsutil.VirtualMemory()
307-
// Workaround until OpenBSD support lands into gosigar
308-
// Check https://github.com/elastic/gosigar#supported-platforms
309-
if err == nil {
310-
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
311-
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
312-
mem.Total = 2 * 1024 * 1024 * 1024
313-
}
314-
allowance := int(mem.Total / 1024 / 1024 / 3)
315-
if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
316-
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
317-
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
318-
}
319-
}
320-
// Ensure Go's GC ignores the database cache for trigger percentage
321-
cache := ctx.GlobalInt(utils.CacheFlag.Name)
322-
gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
323-
324-
log.Info("Sanitizing Go's GC trigger", "percent", int(gogc), "cache", cache)
325-
godebug.SetGCPercent(int(gogc))
326-
327302
// Start metrics export if enabled
328303
utils.SetupMetrics(ctx)
329304

cmd/utils/flags.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"io/ioutil"
2525
"os"
2626

27+
"math"
2728
"math/big"
2829
"path/filepath"
2930
"runtime"
31+
godebug "runtime/debug"
3032
"strconv"
3133
"strings"
3234
"time"
@@ -1377,6 +1379,27 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
13771379
setGPO(ctx, &cfg.GPO)
13781380
setTxPool(ctx, &cfg.TxPool)
13791381
setWhitelist(ctx, cfg)
1382+
// Cap the cache allowance and tune the garbage collector
1383+
mem, err := gopsutil.VirtualMemory()
1384+
// Workaround until OpenBSD support lands into gosigar
1385+
// Check https://github.com/elastic/gosigar#supported-platforms
1386+
if err == nil {
1387+
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
1388+
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
1389+
mem.Total = 2 * 1024 * 1024 * 1024
1390+
}
1391+
allowance := int(mem.Total / 1024 / 1024 / 3)
1392+
if cache := ctx.GlobalInt(CacheFlag.Name); cache > allowance {
1393+
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
1394+
ctx.GlobalSet(CacheFlag.Name, strconv.Itoa(allowance))
1395+
}
1396+
}
1397+
// Ensure Go's GC ignores the database cache for trigger percentage
1398+
cache := ctx.GlobalInt(CacheFlag.Name)
1399+
gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
1400+
1401+
log.Info("Sanitizing Go's GC trigger", "percent", int(gogc), "cache", cache)
1402+
godebug.SetGCPercent(int(gogc))
13801403

13811404
if ctx.GlobalIsSet(SyncModeFlag.Name) {
13821405
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
@@ -1524,8 +1547,9 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
15241547
} else {
15251548
panic(fmt.Sprintf("invalid device: %s", cfg.InferDeviceType))
15261549
}
1550+
15271551
cfg.InferDeviceId = ctx.GlobalInt(InferDeviceIdFlag.Name)
1528-
mem, err := gopsutil.VirtualMemory()
1552+
mem, err = gopsutil.VirtualMemory()
15291553
if err == nil {
15301554
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
15311555
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
@@ -1537,6 +1561,7 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
15371561
ctx.GlobalSet(InferMemoryFlag.Name, strconv.Itoa(allowance))
15381562
}
15391563
}
1564+
15401565
cfg.InferMemoryUsage = int64(ctx.GlobalInt(InferMemoryFlag.Name))
15411566
cfg.InferMemoryUsage = cfg.InferMemoryUsage << 20
15421567
//log.Warn("C MEMORY FOR CVM", "cache", cfg.InferMemoryUsage)

0 commit comments

Comments
 (0)