From 006bf20fa523878dd787f93a02326508b1ad240f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 30 Aug 2016 13:51:48 -0400 Subject: use number of physical cores, not including hyper-threading CPUCost is per physical core --- ExpensiveHash.hs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'ExpensiveHash.hs') 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:" -- cgit v1.2.3