summaryrefslogtreecommitdiffstats
path: root/src/CGroup.hs
blob: 11b54cdd737ce363f77a30af4690fba23128e7d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)


-- | Create a new cgroup.
createCGroup :: CGroup -> IO ()
createCGroup =
    createDirectory . cgroupPath


-- | Places a task into a cgroup.
classifyTask :: ProcessID -> CGroup -> IO ()
classifyTask pid g =
    writeFile (tasksFile g) (show pid)


-- | Retrieve the tasks of a cgroup.
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"