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-01-09 04:01:21 +00:00
|
|
|
return cmp.Min(AlmostAllCPUs(), int(maxWorkersForGivenMemory))
|
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-09-14 05:25:01 +00:00
|
|
|
IndexSnapshot = estimatedRamPerWorker(3 * datasize.GB) //elias-fano index building is single-threaded
|
2022-11-04 06:21:44 +00:00
|
|
|
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
|
|
|
)
|
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)
|
|
|
|
}
|