diff options
author | tv <tv@shackspace.de> | 2014-11-01 20:23:49 +0100 |
---|---|---|
committer | tv <tv@shackspace.de> | 2014-11-02 11:29:50 +0000 |
commit | 049c6cf300d451d058e1465f44fa1b1311988ac8 (patch) | |
tree | 26fb6a2f8973c4e0234eb850489e3661bcbad968 /CGroup.hs |
initial commit
Diffstat (limited to 'CGroup.hs')
-rw-r--r-- | CGroup.hs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/CGroup.hs b/CGroup.hs new file mode 100644 index 0000000..399ec84 --- /dev/null +++ b/CGroup.hs @@ -0,0 +1,53 @@ +-- | +-- Module: CGroup +-- Copyright: (c) 2014 Tomislav Viljetić +-- License: BSD3 +-- Maintainer: Tomislav Viljetić <tomislav@viljetic.de> +-- +-- Basic cgroup virtual filesystem operations. +-- + +module CGroup + ( module CGroup.Types + , createCGroup + , classifyTask + , listTasks + ) where + +import CGroup.Types +import Control.Applicative +import Data.Attoparsec.ByteString.Char8 +import Data.Set (Set) +import qualified Data.Set as Set +import System.Directory (createDirectory) +import System.FilePath ((</>)) +import System.IO.Streams.Attoparsec (parseFromStream) +import System.IO.Streams.File (withFileAsInput) + + +-- | @'createCGroup' g@ creates a new cgroup @g@. +createCGroup :: CGroup -> IO () +createCGroup = + createDirectory . cgroupPath + + +-- | @'classifyTask' g pid@ places task @pid@ into cgroup @g@. +classifyTask :: ProcessID -> CGroup -> IO () +classifyTask pid g = + writeFile (tasksFile g) (show pid) + + +-- | @'listTasks' g@ returns tasks of cgroup @g@. +listTasks :: CGroup -> IO (Set ProcessID) +listTasks g = + withFileAsInput (tasksFile g) $ parseFromStream tasksParser + + +tasksFile :: CGroup -> FilePath +tasksFile = + (</> "tasks") . cgroupPath + + +tasksParser :: Parser (Set ProcessID) +tasksParser = + Set.fromList <$> many' (decimal <* endOfLine) <* endOfInput <?> "tasks" |