From c273ac0173c9b277b2ba08a086e3091ba32d4319 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 30 Aug 2016 14:42:06 -0400 Subject: 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. --- ExpensiveHash.hs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'ExpensiveHash.hs') 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:" -- cgit v1.2.3