Skip to content

Commit

Permalink
Rename System.CGroup.{Types,Controller}
Browse files Browse the repository at this point in the history
  • Loading branch information
cnr committed Oct 28, 2021
1 parent c6eeca9 commit 9805832
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 25 deletions.
5 changes: 3 additions & 2 deletions cgroup-rts-threads.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ library
Control.Concurrent.CGroup
System.CGroup
System.CGroup.CPU
System.CGroup.Types
System.CGroup.Controller
System.CGroup.Controller.Internal

build-depends:
, base >=4.13 && <5
Expand All @@ -59,7 +60,7 @@ test-suite test
main-is: Main.hs
other-modules:
System.CGroup.CPUSpec
System.CGroup.TypesSpec
System.CGroup.ControllerSpec

build-depends:
, base
Expand Down
2 changes: 1 addition & 1 deletion src/System/CGroup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module System.CGroup (
) where

import System.CGroup.CPU as X
import System.CGroup.Types as X
import System.CGroup.Controller as X
2 changes: 1 addition & 1 deletion src/System/CGroup/CPU.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module System.CGroup.CPU (

import Control.Monad ((<=<))
import Path
import System.CGroup.Types (Controller (..), resolveCGroupController)
import System.CGroup.Controller (Controller (..), resolveCGroupController)

-- | The "cpu" cgroup controller
data CPU
Expand Down
9 changes: 9 additions & 0 deletions src/System/CGroup/Controller.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- | Common types and operations for CGroup controllers.
module System.CGroup.Controller (
-- * CGroup Controllers
Controller (..),
resolveCGroupController,
resolveCGroupController',
) where

import System.CGroup.Controller.Internal (Controller (..), resolveCGroupController, resolveCGroupController')
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- | Common types and operations for CGroup controllers.
module System.CGroup.Types (
-- | Internal types and functions for cgroup controllers
module System.CGroup.Controller.Internal (
-- * CGroup Controllers
Controller (..),
resolveCGroupController,
Expand All @@ -11,7 +11,7 @@ module System.CGroup.Types (
-- * Mounts
Mount (..),

-- * Exported for testing
-- * Internal intermediate operations
findMatchingCGroup,
resolveControllerMountPath,
tryResolveMount,
Expand All @@ -38,17 +38,14 @@ import qualified Text.Megaparsec.Char.Lexer as L
newtype Controller a = Controller {unController :: Path Abs Dir}
deriving (Eq, Ord, Show)

-- | Resolve a CGroup controller's filepath, as viewed by the current process
-- | Resolve a CGroup controller by name, as viewed by the current process
--
-- see cgroups(7): \/proc\/self\/cgroup is a file that contains information about
-- control groups applied to this process
--
-- see proc(5): \/proc\/self\/mountinfo is a file that contains information about
-- mounts available to this process
--
-- Because these aren't valid paths on Windows, we have to parse them into @Path
-- Abs File@ at runtime
--
-- Throws an Exception when the controller is not able to be found, or when
-- running outside of a cgroup
resolveCGroupController :: Text -> IO (Controller a)
Expand All @@ -57,7 +54,7 @@ resolveCGroupController controller = do
mountinfoPath <- parseAbsFile "/proc/self/mountinfo"
resolveCGroupController' cgroupPath mountinfoPath controller

-- | Resolve a CGroup controller's filepath, under the given cgroup and
-- | Resolve a CGroup controller by name, under the given cgroup and
-- mountinfo paths
--
-- Throws an Exception when the controller is not able to be found, or when
Expand Down
4 changes: 2 additions & 2 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Main (
) where

import qualified System.CGroup.CPUSpec as CPUSpec
import qualified System.CGroup.TypesSpec as TypesSpec
import qualified System.CGroup.ControllerSpec as ControllerSpec
import Test.Hspec.Core.Runner (hspec)
import Test.Hspec.Core.Spec (Spec)

Expand All @@ -13,4 +13,4 @@ main = hspec tests
tests :: Spec
tests = do
CPUSpec.tests
TypesSpec.tests
ControllerSpec.tests
2 changes: 1 addition & 1 deletion test/System/CGroup/CPUSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module System.CGroup.CPUSpec (
import Control.Monad.IO.Class (liftIO)
import Path.IO (resolveDir')
import System.CGroup.CPU
import System.CGroup.Types (Controller (..))
import System.CGroup.Controller (Controller (..))
import Test.Hspec.Core.Spec (Spec, describe, it)
import Test.Hspec.Expectations (shouldBe)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module System.CGroup.TypesSpec (
module System.CGroup.ControllerSpec (
tests,
) where

import Control.Monad.IO.Class (liftIO)
import Path
import Path.IO (resolveFile')
import System.CGroup.Types
import System.CGroup.Controller
import System.Info (os)
import Test.Hspec.Core.Spec (Spec, describe, it)
import Test.Hspec.Expectations (shouldBe)
Expand All @@ -15,32 +15,32 @@ tests :: Spec
tests = exceptOnWindows $ do
describe "resolveGroupController" $ do
it "should work on a real world example" $ do
cgroup <- resolveFile' "test/System/CGroup/testdata-types/realworld/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-types/realworld/mountinfo"
cgroup <- resolveFile' "test/System/CGroup/testdata-controller/realworld/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-controller/realworld/mountinfo"
expected <- parseAbsDir "/sys/fs/cgroup/cpu"

controller <- liftIO $ resolveCGroupController' cgroup mountinfo "cpu"
controller `shouldBe` Controller expected

it "should resolve a direct mount root" $ do
cgroup <- resolveFile' "test/System/CGroup/testdata-types/direct/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-types/direct/mountinfo"
cgroup <- resolveFile' "test/System/CGroup/testdata-controller/direct/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-controller/direct/mountinfo"
expected <- parseAbsDir "/sys/fs/cgroup/cpu"

controller <- liftIO $ resolveCGroupController' cgroup mountinfo "cpu"
controller `shouldBe` Controller expected

it "should resolve subdirectories of a mount root" $ do
cgroup <- resolveFile' "test/System/CGroup/testdata-types/indirect/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-types/indirect/mountinfo"
cgroup <- resolveFile' "test/System/CGroup/testdata-controller/indirect/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-controller/indirect/mountinfo"
expected <- parseAbsDir "/sys/fs/cgroup/cpu/subdir"

controller <- liftIO $ resolveCGroupController' cgroup mountinfo "cpu"
controller `shouldBe` Controller expected

it "should work for cgroups v2" $ do
cgroup <- resolveFile' "test/System/CGroup/testdata-types/cgroupsv2/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-types/cgroupsv2/mountinfo"
cgroup <- resolveFile' "test/System/CGroup/testdata-controller/cgroupsv2/cgroup"
mountinfo <- resolveFile' "test/System/CGroup/testdata-controller/cgroupsv2/mountinfo"
expected <- parseAbsDir "/sys/fs/cgroup/cpu"

controller <- liftIO $ resolveCGroupController' cgroup mountinfo "cpu"
Expand Down

0 comments on commit 9805832

Please sign in to comment.