2022-10-05 10:07:01 +00:00
|
|
|
package estimate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/c2h5oh/datasize"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/cmp"
|
|
|
|
"github.com/pbnjay/memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
totalMemory := memory.TotalMemory() / 2
|
2022-10-18 08:38:14 +00:00
|
|
|
|
|
|
|
maxWorkersForGivenMemory := totalMemory / uint64(r)
|
2022-10-05 10:07:01 +00:00
|
|
|
maxWorkersForGivenCPU := runtime.NumCPU() - 1 // reserve 1 cpu for "work-producer thread", also IO software on machine in cloud-providers using 1 CPU
|
|
|
|
return cmp.InRange(1, maxWorkersForGivenCPU, int(maxWorkersForGivenMemory))
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-11-04 06:21:44 +00:00
|
|
|
IndexSnapshot = estimatedRamPerWorker(2 * datasize.GB) //elias-fano index building is single-threaded
|
|
|
|
CompressSnapshot = estimatedRamPerWorker(1 * datasize.GB) //1-file-compression is multi-threaded
|
|
|
|
ReconstituteState = estimatedRamPerWorker(512 * datasize.MB) //state-reconstitution is multi-threaded
|
2022-10-05 10:07:01 +00:00
|
|
|
)
|