Skip to content

Commit

Permalink
Only use up to MaxParallelism CPUs
Browse files Browse the repository at this point in the history
This prevents panics on 256-core systems, and has a 300-core system use
255 CPUs (the max) rather than 44 CPUs (300 casted to a uint8).

Signed-off-by: Joe Richey <joerichey@google.com>
[ebiggers: also set TruncationFixed at the end of getHashingCosts()]
Signed-off-by: Eric Biggers <ebiggers@google.com>
  • Loading branch information
josephlr authored and ebiggers committed Dec 4, 2022
1 parent 9d96413 commit 5c7c3bc
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions actions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,17 @@ func getHashingCosts(target time.Duration) (*metadata.HashingCosts, error) {
log.Printf("Finding hashing costs that take %v\n", target)

// Start out with the minimal possible costs that use all the CPUs.
nCPUs := int64(runtime.NumCPU())
parallelism := int64(runtime.NumCPU())
// golang.org/x/crypto/argon2 only supports parallelism up to 255.
// For compatibility, don't use more than that amount.
if parallelism > metadata.MaxParallelism {
parallelism = metadata.MaxParallelism
}
costs := &metadata.HashingCosts{
Time: 1,
Memory: 8 * nCPUs,
Parallelism: nCPUs,
Time: 1,
Memory: 8 * parallelism,
Parallelism: parallelism,
TruncationFixed: true,
}

// If even the minimal costs are not fast enough, just return the
Expand Down Expand Up @@ -233,9 +239,10 @@ func getHashingCosts(target time.Duration) (*metadata.HashingCosts, error) {
if t >= target {
f := float64(target-tPrev) / float64(t-tPrev)
return &metadata.HashingCosts{
Time: betweenCosts(costsPrev.Time, costs.Time, f),
Memory: betweenCosts(costsPrev.Memory, costs.Memory, f),
Parallelism: costs.Parallelism,
Time: betweenCosts(costsPrev.Time, costs.Time, f),
Memory: betweenCosts(costsPrev.Memory, costs.Memory, f),
Parallelism: costs.Parallelism,
TruncationFixed: costs.TruncationFixed,
}, nil
}
}
Expand Down

0 comments on commit 5c7c3bc

Please sign in to comment.