mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-21 18:37:26 +08:00
* v2.1.0 [omit consensus and adjacent] - this commit will be amended with the full release after the file copy is complete * 2.1.0 main node rollup
50 lines
920 B
Go
50 lines
920 B
Go
package runtime
|
|
|
|
import (
|
|
"log"
|
|
"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, legacy bool) int {
|
|
n := runtime.GOMAXPROCS(0)
|
|
if validate {
|
|
if n < minimumCores {
|
|
log.Panic("invalid system configuration, must have at least 3 cores")
|
|
}
|
|
if requested > 0 && requested < minimumCores {
|
|
log.Panic("invalid worker count, must have at least 3 workers")
|
|
}
|
|
}
|
|
if requested > 0 {
|
|
return min(requested, n)
|
|
}
|
|
|
|
if legacy {
|
|
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
|
|
}
|
|
}
|
|
|
|
if n == 1 {
|
|
return 1
|
|
}
|
|
|
|
return n - 1
|
|
}
|