{-# LANGUAGE TemplateHaskell #-}
module System.CGroup.V1.CPU (
getProcessCPUQuota,
getCPUQuota,
CPUQuota (..),
CPU,
resolveCPUController,
) where
import Control.Monad ((<=<))
import Data.Ratio ((%))
import Path
import System.CGroup.Types (CPUQuota (..))
import System.CGroup.V1.Controller (Controller (..), resolveCGroupController)
data CPU
resolveCPUController :: IO (Controller CPU)
resolveCPUController :: IO (Controller CPU)
resolveCPUController = Text -> IO (Controller CPU)
forall a. Text -> IO (Controller a)
resolveCGroupController Text
"cpu"
getProcessCPUQuota :: IO CPUQuota
getProcessCPUQuota :: IO CPUQuota
getProcessCPUQuota = Controller CPU -> IO CPUQuota
getCPUQuota (Controller CPU -> IO CPUQuota)
-> IO (Controller CPU) -> IO CPUQuota
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO (Controller CPU)
resolveCPUController
getCPUQuota :: Controller CPU -> IO CPUQuota
getCPUQuota :: Controller CPU -> IO CPUQuota
getCPUQuota (Controller Path Abs Dir
root) = do
Int
quota <- Path Abs File -> IO Int
forall b. Path b File -> IO Int
readCGroupInt (Path Abs Dir
root Path Abs Dir -> Path Rel File -> Path Abs File
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel File
cpuQuotaPath)
Int
period <- Path Abs File -> IO Int
forall b. Path b File -> IO Int
readCGroupInt (Path Abs Dir
root Path Abs Dir -> Path Rel File -> Path Abs File
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel File
cpuPeriodPath)
case Int
quota of
(-1) -> CPUQuota -> IO CPUQuota
forall (f :: * -> *) a. Applicative f => a -> f a
pure CPUQuota
NoQuota
Int
_ -> CPUQuota -> IO CPUQuota
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ratio Int -> CPUQuota
CPUQuota (Int
quota Int -> Int -> Ratio Int
forall a. Integral a => a -> a -> Ratio a
% Int
period))
readCGroupInt :: Path b File -> IO Int
readCGroupInt :: forall b. Path b File -> IO Int
readCGroupInt = String -> IO Int
forall a. Read a => String -> IO a
readIO (String -> IO Int)
-> (Path b File -> IO String) -> Path b File -> IO Int
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (String -> IO String
readFile (String -> IO String)
-> (Path b File -> String) -> Path b File -> IO String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path b File -> String
forall b t. Path b t -> String
toFilePath)
cpuQuotaPath :: Path Rel File
cpuQuotaPath :: Path Rel File
cpuQuotaPath = $(mkRelFile "cpu.cfs_quota_us")
cpuPeriodPath :: Path Rel File
cpuPeriodPath :: Path Rel File
cpuPeriodPath = $(mkRelFile "cpu.cfs_period_us")