This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit capabilities to respect cgroups cpu quota (#403)
- Loading branch information
Showing
22 changed files
with
472 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Control.Concurrent.Extra ( | ||
initCapabilities, | ||
) where | ||
|
||
import Control.Exception (SomeException) | ||
import Control.Exception.Extra (safeCatch) | ||
import GHC.Conc (getNumProcessors, setNumCapabilities) | ||
import System.CGroup | ||
|
||
-- | Sets the number of available capabilities via 'GHC.Conc.setNumCapabilities' | ||
-- | ||
-- On most platforms, this sets the number of capabilities to the number of | ||
-- physical processors ('GHC.Conc.getNumProcessors'). | ||
-- | ||
-- When running within a cgroup on linux (most often within a container), this | ||
-- takes into account the available cpu quota | ||
initCapabilities :: IO () | ||
initCapabilities = | ||
initCapabilitiesFromCGroup | ||
`safeCatch` (\(_ :: SomeException) -> defaultInitCapabilities) | ||
|
||
initCapabilitiesFromCGroup :: IO () | ||
initCapabilitiesFromCGroup = do | ||
cpuController <- resolveCPUController | ||
cgroupCpuQuota <- getCPUQuota cpuController | ||
case cgroupCpuQuota of | ||
NoQuota -> defaultInitCapabilities | ||
CPUQuota quota period -> do | ||
procs <- getNumProcessors | ||
let capabilities = clamp 1 procs (quota `div` period) | ||
setNumCapabilities capabilities | ||
|
||
-- | Set number of capabilities to the number of available processors | ||
defaultInitCapabilities :: IO () | ||
defaultInitCapabilities = setNumCapabilities =<< getNumProcessors | ||
|
||
-- | Clamp a value within a range | ||
clamp :: Int -> Int -> Int -> Int | ||
clamp lower upper = max lower . min upper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module System.CGroup ( | ||
module X, | ||
) where | ||
|
||
import System.CGroup.CPU as X | ||
import System.CGroup.Types as X |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module System.CGroup.CPU ( | ||
-- * The CPU cgroup controller | ||
CPU, | ||
resolveCPUController, | ||
|
||
-- * Operations on the CPU controller | ||
CPUQuota (..), | ||
getCPUQuota, | ||
) where | ||
|
||
import Control.Monad ((<=<)) | ||
import Path | ||
import System.CGroup.Types (Controller (..), resolveCGroupController) | ||
|
||
-- | The "cpu" cgroup controller | ||
data CPU | ||
|
||
resolveCPUController :: IO (Controller CPU) | ||
resolveCPUController = resolveCGroupController "cpu" | ||
|
||
-- | A CPU quota is the amount of CPU time we have relative to the scheduler period | ||
-- | ||
-- For example: | ||
-- | ||
-- | cpu.cfs_quota_us | cpu.cfs_period_us | description | | ||
-- | ---------------- | ----------------- | ----------- | | ||
-- | 100000 | 100000 | (1) | | ||
-- | 200000 | 100000 | (2) | | ||
-- | 50000 | 100000 | (3) | | ||
-- | -1 | 100000 | (4) | | ||
-- | ||
-- (1): we can use up to a single CPU core | ||
-- | ||
-- (2): we can use up to two CPU cores | ||
-- | ||
-- (3): the scheduler will give us a single CPU core for up to 50% of the time | ||
-- | ||
-- (4): we can use all available CPU resources (there is no quota) | ||
data CPUQuota | ||
= NoQuota | ||
| -- | cpu.cfs_quota_us, cpu.cfs_period_us | ||
CPUQuota Int Int | ||
deriving (Eq, Ord, Show) | ||
|
||
-- | Read a CGroup configuration value from its file | ||
readCGroupInt :: Path b File -> IO Int | ||
readCGroupInt = readIO <=< (readFile . toFilePath) | ||
|
||
-- | Get the process CPU quota | ||
getCPUQuota :: Controller CPU -> IO CPUQuota | ||
getCPUQuota (Controller root) = do | ||
quota <- readCGroupInt (root </> cpuQuotaPath) | ||
case quota of | ||
(-1) -> pure NoQuota | ||
_ -> CPUQuota quota <$> readCGroupInt (root </> cpuPeriodPath) | ||
|
||
-- Path to the "cpu quota" file | ||
-- | ||
-- When this file contains "-1", there is no quota set | ||
cpuQuotaPath :: Path Rel File | ||
cpuQuotaPath = $(mkRelFile "cpu.cfs_quota_us") | ||
|
||
-- Path to the "cpu period" file | ||
cpuPeriodPath :: Path Rel File | ||
cpuPeriodPath = $(mkRelFile "cpu.cfs_period_us") |
Oops, something went wrong.