summaryrefslogtreecommitdiffhomepage
path: root/ExpensiveHash.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-30 14:42:06 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-30 14:42:06 -0400
commitc273ac0173c9b277b2ba08a086e3091ba32d4319 (patch)
tree3cb1d60625b537f38e6194c1c80a8b119a9b0aaf /ExpensiveHash.hs
parentfe975ad122c77b4936f3e28c868b056fdaf2f842 (diff)
downloadkeysafe-c273ac0173c9b277b2ba08a086e3091ba32d4319.tar.gz
Improve time estimates, taking into account the number of cores.
This only affects time estimates while keysafe is generating hashes; it does not affect cost estimates to brute-force.
Diffstat (limited to 'ExpensiveHash.hs')
-rw-r--r--ExpensiveHash.hs19
1 files changed, 4 insertions, 15 deletions
diff --git a/ExpensiveHash.hs b/ExpensiveHash.hs
index 8c473d3..ff9c51c 100644
--- a/ExpensiveHash.hs
+++ b/ExpensiveHash.hs
@@ -50,7 +50,8 @@ expensiveHash (UseArgon2 cost opts) (Salt s) b = ExpensiveHash cost $
benchmarkExpensiveHash :: Int -> ExpensiveHashTunable -> Cost op -> IO (BenchmarkResult (Cost op))
benchmarkExpensiveHash rounds tunables@(UseArgon2 _ hashopts) expected = do
- numcores <- fromIntegral <$> getNumCores
+ numcores <- fromIntegral . fromMaybe (error "Unknown number of physical cores.")
+ <$> getNumCores
start <- getCurrentTime
forM_ [1..rounds] $ \_ -> do
let ExpensiveHash _ t = expensiveHash tunables
@@ -59,11 +60,11 @@ benchmarkExpensiveHash rounds tunables@(UseArgon2 _ hashopts) expected = do
t `deepseq` return ()
end <- getCurrentTime
let diff = floor $ end `diffUTCTime` start
- let actual = CPUCost $ Seconds diff
+ let maxthreads = Argon2.hashParallelism hashopts
+ let actual = CPUCost (Seconds diff) (Divisibility $ fromIntegral maxthreads)
-- The expected cost is for a single core, so adjust it
-- based on the number of cores, up to a maximum of the number
-- of threads that the hash is configred to use.
- let maxthreads = Argon2.hashParallelism hashopts
let usedcores = min maxthreads numcores
let adjustedexpected = mapCost (`div` fromIntegral usedcores) expected
return $ BenchmarkResult
@@ -71,18 +72,6 @@ benchmarkExpensiveHash rounds tunables@(UseArgon2 _ hashopts) expected = do
, actualBenchmark = actual
}
--- Number of physical cores. This is not the same as
--- getNumProcessors, which includes hyper-threading.
-getNumCores :: IO Integer
-getNumCores = getmax . mapMaybe parse . lines <$> readFile "/proc/cpuinfo"
- where
- getmax [] = error "Unknown number of physical cores."
- getmax l = maximum l + 1 -- add 1 because /proc/cpuinfo counts from 0
- parse l
- | "core id" `isPrefixOf` l =
- readMaybe $ drop 1 $ dropWhile (/= ':') l
- | otherwise = Nothing
-
benchmarkTunables :: Tunables -> IO ()
benchmarkTunables tunables = do
putStrLn "/proc/cpuinfo:"