Skip to content

Commit

Permalink
Add support for cgroups v2
Browse files Browse the repository at this point in the history
  • Loading branch information
cnr committed Nov 3, 2021
1 parent 9805832 commit 4ab02aa
Show file tree
Hide file tree
Showing 37 changed files with 694 additions and 386 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for cgroup-rts-threads

## 0.2.0.0

- Adds support for cgroups v2

## 0.1.0.0

- Initial release
24 changes: 14 additions & 10 deletions cgroup-rts-threads.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cgroup-rts-threads
version: 0.1.0.0
version: 0.2.0.0
synopsis:
A container-/cgroup-aware substitute for the GHC RTS `-N` flag

Expand Down Expand Up @@ -40,15 +40,17 @@ library
-- cabal-fmt: expand src
exposed-modules:
Control.Concurrent.CGroup
System.CGroup
System.CGroup.CPU
System.CGroup.Controller
System.CGroup.Controller.Internal
System.CGroup.Types
System.CGroup.V1.CPU
System.CGroup.V1.Controller
System.CGroup.V2.CGroup
System.CGroup.V2.CPU

build-depends:
, base >=4.13 && <5
, megaparsec >=8 && <10
, path >=0.7 && <0.10
, base >=4.13 && <5
, directory ^>=1.3.4.0
, megaparsec >=8 && <10
, path >=0.7 && <0.10
, text

hs-source-dirs: src
Expand All @@ -59,8 +61,10 @@ test-suite test
hs-source-dirs: test
main-is: Main.hs
other-modules:
System.CGroup.CPUSpec
System.CGroup.ControllerSpec
System.CGroup.V1.CPUSpec
System.CGroup.V1.ControllerSpec
System.CGroup.V2.CGroupSpec
System.CGroup.V2.CPUSpec

build-depends:
, base
Expand Down
39 changes: 21 additions & 18 deletions src/Control/Concurrent/CGroup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module Control.Concurrent.CGroup (
) where

import Control.Exception (Exception (..), SomeAsyncException (SomeAsyncException), SomeException, catch, throwIO)
import qualified Data.Ratio as Ratio
import GHC.Conc (getNumProcessors, setNumCapabilities)
import System.CGroup.CPU (CPUQuota (..), getCPUQuota, resolveCPUController)
import System.CGroup.Types (CPUQuota (..))
import qualified System.CGroup.V1.CPU as V1
import qualified System.CGroup.V2.CPU as V2

-- | A container-/cgroup-aware substitute for GHC's RTS @-N@ flag.
--
Expand All @@ -20,24 +23,20 @@ import System.CGroup.CPU (CPUQuota (..), getCPUQuota, resolveCPUController)
--
-- See 'CPUQuota'
initRTSThreads :: IO ()
initRTSThreads =
initRTSThreadsFromCGroup
`safeCatch` (\(_ :: SomeException) -> defaultInitRTSThreads)
initRTSThreads = do
quota <-
V1.getProcessCPUQuota
`fallback` V2.getProcessEffectiveCPUQuota
`fallback` pure NoQuota
initRTSThreadsFromQuota quota

-- | Uses the current process' cgroup cpu quota to set the number of runtime
-- threads.
--
-- Throws an Exception when the current process is not running within a cgroup.
initRTSThreadsFromCGroup :: IO ()
initRTSThreadsFromCGroup = do
cpuController <- resolveCPUController
cgroupCpuQuota <- getCPUQuota cpuController
case cgroupCpuQuota of
NoQuota -> defaultInitRTSThreads
CPUQuota quota period -> do
procs <- getNumProcessors
let capabilities = clamp 1 procs (quota `div` period)
setNumCapabilities capabilities
-- | Use a CPU quota to set the number of runtime threads.
initRTSThreadsFromQuota :: CPUQuota -> IO ()
initRTSThreadsFromQuota NoQuota = defaultInitRTSThreads
initRTSThreadsFromQuota (CPUQuota ratio) = do
procs <- getNumProcessors
let capabilities = clamp 1 procs (Ratio.numerator ratio `div` Ratio.denominator ratio)
setNumCapabilities capabilities

-- | Set number of runtime threads to the number of available processors. This
-- matches the behavior of GHC's RTS @-N@ flag.
Expand All @@ -57,3 +56,7 @@ isSyncException e =
case fromException (toException e) of
Just (SomeAsyncException _) -> False
Nothing -> True

-- | Return the result of the first successful action
fallback :: IO a -> IO a -> IO a
fallback a b = a `safeCatch` (\(_ :: SomeException) -> b)
7 changes: 0 additions & 7 deletions src/System/CGroup.hs

This file was deleted.

75 changes: 0 additions & 75 deletions src/System/CGroup/CPU.hs

This file was deleted.

9 changes: 0 additions & 9 deletions src/System/CGroup/Controller.hs

This file was deleted.

Loading

0 comments on commit 4ab02aa

Please sign in to comment.