2020-04-21 15:20:10 +00:00
|
|
|
{- git-annex concurrency utilities
|
|
|
|
-
|
|
|
|
- Copyright 2020 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Concurrent.Utility where
|
|
|
|
|
2020-09-16 15:41:28 +00:00
|
|
|
import Annex
|
2020-04-21 15:20:10 +00:00
|
|
|
import Types.Concurrency
|
|
|
|
|
|
|
|
import GHC.Conc
|
|
|
|
|
2020-09-16 15:41:28 +00:00
|
|
|
getConcurrency :: Annex Concurrency
|
|
|
|
getConcurrency = getConcurrency' <$> getState concurrency
|
|
|
|
|
|
|
|
getConcurrency' :: ConcurrencySetting -> Concurrency
|
|
|
|
getConcurrency' (ConcurrencyCmdLine c) = c
|
|
|
|
getConcurrency' (ConcurrencyGitConfig c) = c
|
|
|
|
|
2020-04-21 15:20:10 +00:00
|
|
|
{- Honor the requested level of concurrency, but only up to the number of
|
|
|
|
- CPU cores. Useful for things that are known to be CPU bound. -}
|
|
|
|
concurrencyUpToCpus :: Concurrency -> IO Int
|
|
|
|
concurrencyUpToCpus c = do
|
|
|
|
let cn = case c of
|
|
|
|
Concurrent n -> n
|
|
|
|
NonConcurrent -> 1
|
|
|
|
ConcurrentPerCpu -> 1
|
|
|
|
pn <- getNumProcessors
|
|
|
|
return (min cn pn)
|