summaryrefslogtreecommitdiffhomepage
path: root/ExpensiveHash.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-30 13:51:48 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-30 13:51:48 -0400
commit006bf20fa523878dd787f93a02326508b1ad240f (patch)
tree9d5b87572e778c6b3903c80fc764f7532e245053 /ExpensiveHash.hs
parente6a706517187c280be652afa54ee75893bdf9608 (diff)
downloadkeysafe-006bf20fa523878dd787f93a02326508b1ad240f.tar.gz
use number of physical cores, not including hyper-threading
CPUCost is per physical core
Diffstat (limited to 'ExpensiveHash.hs')
-rw-r--r--ExpensiveHash.hs19
1 files changed, 16 insertions, 3 deletions
diff --git a/ExpensiveHash.hs b/ExpensiveHash.hs
index fed6d18..5799d83 100644
--- a/ExpensiveHash.hs
+++ b/ExpensiveHash.hs
@@ -19,7 +19,9 @@ import Data.Time.Clock
import Control.DeepSeq
import Control.Monad
import Data.Monoid
-import GHC.Conc (getNumProcessors)
+import Data.List
+import Data.Maybe
+import Text.Read
-- | A hash that is expensive to calculate.
--
@@ -48,6 +50,7 @@ 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
start <- getCurrentTime
forM_ [1..rounds] $ \_ -> do
let ExpensiveHash _ t = expensiveHash tunables
@@ -60,15 +63,25 @@ benchmarkExpensiveHash rounds tunables@(UseArgon2 _ hashopts) expected = do
-- 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.
- numcpus <- fromIntegral <$> getNumProcessors
let maxthreads = Argon2.hashParallelism hashopts
- let usedcpus = if numcpus > maxthreads then maxthreads else numcpus
+ let usedcpus = if numcores > maxthreads then maxthreads else numcores
let adjustedexpected = mapCost (`div` fromIntegral usedcpus) expected
return $ BenchmarkResult
{ expectedBenchmark = adjustedexpected
, 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
+ parse l
+ | "core id" `isPrefixOf` l = readMaybe $ dropWhile (/= ':') l
+ | otherwise = Nothing
+
benchmarkTunables :: Tunables -> IO ()
benchmarkTunables tunables = do
putStrLn "/proc/cpuinfo:"