2022-10-05 10:07:01 +00:00
|
|
|
package estimate
|
|
|
|
|
|
|
|
import (
|
2023-11-23 05:00:41 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-10-05 10:07:01 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/cmp"
|
2023-11-01 02:02:34 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/mmap"
|
2022-10-05 10:07:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type estimatedRamPerWorker datasize.ByteSize
|
|
|
|
|
|
|
|
// Workers - return max workers amount based on total Memory/CPU's and estimated RAM per worker
|
|
|
|
func (r estimatedRamPerWorker) Workers() int {
|
2022-11-01 14:54:39 +00:00
|
|
|
// 50% of TotalMemory. Better don't count on 100% because OOM Killer may have aggressive defaults and other software may need RAM
|
2023-11-01 02:02:34 +00:00
|
|
|
maxWorkersForGivenMemory := (mmap.TotalMemory() / 2) / uint64(r)
|
2023-11-29 16:29:15 +00:00
|
|
|
res := cmp.Min(AlmostAllCPUs(), int(maxWorkersForGivenMemory))
|
|
|
|
return cmp.Max(1, res) // must have at-least 1 worker
|
2022-10-05 10:07:01 +00:00
|
|
|
}
|
2023-11-01 02:02:34 +00:00
|
|
|
|
2022-11-21 03:38:22 +00:00
|
|
|
func (r estimatedRamPerWorker) WorkersHalf() int { return cmp.Max(1, r.Workers()/2) }
|
|
|
|
func (r estimatedRamPerWorker) WorkersQuarter() int { return cmp.Max(1, r.Workers()/4) }
|
2022-10-05 10:07:01 +00:00
|
|
|
|
|
|
|
const (
|
2023-12-06 08:20:28 +00:00
|
|
|
//elias-fano index building is single-threaded
|
|
|
|
// when set it to 3GB - observed OOM-kil at server with 128Gb ram and 32CPU
|
|
|
|
IndexSnapshot = estimatedRamPerWorker(4 * datasize.GB)
|
|
|
|
|
|
|
|
//1-file-compression is multi-threaded
|
|
|
|
CompressSnapshot = estimatedRamPerWorker(1 * datasize.GB)
|
|
|
|
|
|
|
|
//state-reconstitution is multi-threaded
|
|
|
|
ReconstituteState = estimatedRamPerWorker(512 * datasize.MB)
|
2022-10-05 10:07:01 +00:00
|
|
|
)
|
2023-01-09 04:01:21 +00:00
|
|
|
|
|
|
|
// AlmostAllCPUs - return all-but-one cpus. Leaving 1 cpu for "work producer", also cloud-providers do recommend leave 1 CPU for their IO software
|
|
|
|
// user can reduce GOMAXPROCS env variable
|
|
|
|
func AlmostAllCPUs() int {
|
|
|
|
return cmp.Max(1, runtime.GOMAXPROCS(-1)-1)
|
|
|
|
}
|