{-# LANGUAGE TemplateHaskell #-}

-- | Types and operations for the CPU cgroup controller.
module System.CGroup.V1.CPU (
  -- * Operations on the CPU controller
  getProcessCPUQuota,
  getCPUQuota,
  CPUQuota (..),

  -- * The CPU cgroup controller
  CPU,
  resolveCPUController,
) where

import Control.Monad ((<=<))
import Data.Ratio ((%))
import Path
import System.CGroup.Types (CPUQuota (..))
import System.CGroup.V1.Controller (Controller (..), resolveCGroupController)

-- | The "cpu" cgroup controller
data CPU

-- | Resolve the CPU cgroup controller for the current process
--
-- Throws an Exception if the CPU controller is not able to be found, or when
-- running outside of a cgroup
resolveCPUController :: IO (Controller CPU)
resolveCPUController :: IO (Controller CPU)
resolveCPUController = Text -> IO (Controller CPU)
forall a. Text -> IO (Controller a)
resolveCGroupController Text
"cpu"

-- | Get the current process' CPU quota
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

-- | Read "cpu.cfs_quota_us" and "cpu.cfs_period_us" files into a CPUQuota.
--
-- For example:
--
-- @
-- | cpu.cfs_quota_us | cpu.cfs_period_us | CPUQuota         |
-- | ---------------- | ----------------- | ---------------- |
-- |           100000 |            100000 | CPUQuota (1 % 1) |
-- |           200000 |            100000 | CPUQuota (2 % 1) |
-- |            50000 |            100000 | CPUQuota (1 % 2) |
-- |               -1 |            100000 | NoQuota          |
-- @
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))

-- | Read a cgroup configuration value from its file
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)

-- | Path to the "cpu quota" file
--
-- When this file contains "-1", there is no quota set
cpuQuotaPath :: Path Rel File
cpuQuotaPath :: Path Rel File
cpuQuotaPath = $(mkRelFile "cpu.cfs_quota_us")

-- | Path to the "cpu period" file
cpuPeriodPath :: Path Rel File
cpuPeriodPath :: Path Rel File
cpuPeriodPath = $(mkRelFile "cpu.cfs_period_us")