mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-22 02:47:26 +08:00
* Add adaptive data worker count * Use runtime worker count for validation workers * Reserve cores for networking during transition application * Automatically set GOGC and GOMEMLIMIT
38 lines
813 B
Go
38 lines
813 B
Go
package runtime
|
|
|
|
import "runtime"
|
|
|
|
const minimumCores = 3
|
|
|
|
// WorkerCount returns the number of workers to use CPU bound tasks.
|
|
// It will use GOMAXPROCS as a base, and then subtract a number of CPUs
|
|
// which are meant to be left for other tasks, such as networking.
|
|
func WorkerCount(requested int, validate bool) int {
|
|
n := runtime.GOMAXPROCS(0)
|
|
if validate {
|
|
if n < minimumCores {
|
|
panic("invalid system configuration, must have at least 3 cores")
|
|
}
|
|
if requested > 0 && requested < minimumCores {
|
|
panic("invalid worker count, must have at least 3 workers")
|
|
}
|
|
}
|
|
if requested > 0 {
|
|
return min(requested, n)
|
|
}
|
|
switch {
|
|
case n == 1:
|
|
return 1
|
|
case n <= 4:
|
|
return n - 1
|
|
case n <= 16:
|
|
return n - 2
|
|
case n <= 32:
|
|
return n - 3
|
|
case n <= 64:
|
|
return n - 4
|
|
default:
|
|
return n - 5
|
|
}
|
|
}
|