@@ -24,9 +24,11 @@ import (
24
24
"io/ioutil"
25
25
"os"
26
26
27
+ "math"
27
28
"math/big"
28
29
"path/filepath"
29
30
"runtime"
31
+ godebug "runtime/debug"
30
32
"strconv"
31
33
"strings"
32
34
"time"
@@ -1377,6 +1379,27 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
1377
1379
setGPO (ctx , & cfg .GPO )
1378
1380
setTxPool (ctx , & cfg .TxPool )
1379
1381
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 ))
1380
1403
1381
1404
if ctx .GlobalIsSet (SyncModeFlag .Name ) {
1382
1405
cfg .SyncMode = * GlobalTextMarshaler (ctx , SyncModeFlag .Name ).(* downloader.SyncMode )
@@ -1524,8 +1547,9 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
1524
1547
} else {
1525
1548
panic (fmt .Sprintf ("invalid device: %s" , cfg .InferDeviceType ))
1526
1549
}
1550
+
1527
1551
cfg .InferDeviceId = ctx .GlobalInt (InferDeviceIdFlag .Name )
1528
- mem , err : = gopsutil .VirtualMemory ()
1552
+ mem , err = gopsutil .VirtualMemory ()
1529
1553
if err == nil {
1530
1554
if 32 << (^ uintptr (0 )>> 63 ) == 32 && mem .Total > 2 * 1024 * 1024 * 1024 {
1531
1555
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) {
1537
1561
ctx .GlobalSet (InferMemoryFlag .Name , strconv .Itoa (allowance ))
1538
1562
}
1539
1563
}
1564
+
1540
1565
cfg .InferMemoryUsage = int64 (ctx .GlobalInt (InferMemoryFlag .Name ))
1541
1566
cfg .InferMemoryUsage = cfg .InferMemoryUsage << 20
1542
1567
//log.Warn("C MEMORY FOR CVM", "cache", cfg.InferMemoryUsage)
0 commit comments