-
Notifications
You must be signed in to change notification settings - Fork 3
/
Monky.hs
122 lines (102 loc) · 4.03 KB
/
Monky.hs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{-
Copyright 2015 Markus Ongyerth, Stephan Guenther
This file is part of Monky.
Monky is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Monky is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Monky. If not, see <http://www.gnu.org/licenses/>.
-}
{-|
Module : Monky
Description : The main module for monky
Maintainer : ongy, moepi
Stability : testing
Portability : Linux
This module contains the main logic for monky.
This has to be included in the Monky.hs. The entry point for monky is startLoop
which has to be called with a ['IO' 'Modules'].
This type was chosen to make the syntax in the config file prettier.
The submodules of this generally provide an interface based on handles.
To use them, get the handle at the beginning of you application and hand it to
other functions later on.
-}
module Monky
( startLoop
, startLoopT
)
where
import System.Timeout
import Data.Time.Clock.POSIX
import Control.Concurrent.MVar
import Data.IORef (IORef, readIORef, writeIORef, newIORef, atomicWriteIORef)
import Monky.Modules
import Control.Monad (when, void)
import Control.Concurrent (forkIO)
data ModuleWrapper = MWrapper Modules (IORef [MonkyOut])
-- |Packs a module into a wrapper with an IORef for cached output
packMod :: MVar Bool -> Modules -> IO ModuleWrapper
packMod _ x@(Poll (NMW m _)) = do
sref <- newIORef []
initialize m
return (MWrapper x sref)
packMod mvar x@(Evt (DW m)) = do
sref <- newIORef []
_ <- forkIO . startEvtLoop m $ \val -> do
atomicWriteIORef sref val
void $ tryPutMVar mvar True
return $ MWrapper x sref
getWrapperText :: Int -> ModuleWrapper -> IO [MonkyOut]
getWrapperText tick (MWrapper (Poll (NMW m i)) r) = do
when (tick `mod` i == 0) (writeIORef r =<< getOutput m)
readIORef r
getWrapperText _ (MWrapper (Evt _) r) = readIORef r
doMonkyLine :: MonkyOutput o => Int -> o -> [ModuleWrapper] -> IO ()
doMonkyLine t o xs =
doLine o =<< mapM (getWrapperText t) xs
doCachedLine :: MonkyOutput o => o -> [ModuleWrapper] -> IO ()
doCachedLine out xs =
doLine out =<< mapM (\(MWrapper _ r) -> readIORef r) xs
waitTick :: MonkyOutput o => Int -> MVar Bool -> o -> [ModuleWrapper] -> IO ()
waitTick limit mvar out xs = do
pre <- getPOSIXTime
ret <- timeout limit $ takeMVar mvar
case ret of
Nothing -> return ()
Just _ -> do
doCachedLine out xs
post <- getPOSIXTime
let passed = round . (* 1000000) $ post - pre
if passed > limit
then return ()
else waitTick (limit - passed) mvar out xs
mainLoop :: MonkyOutput o => Int -> MVar Bool -> Int -> o -> [ModuleWrapper] -> IO ()
mainLoop l r t o xs = do
doMonkyLine t o xs
waitTick l r o xs
mainLoop l r (t+1) o xs
{- | Start the mainLoop of monky. With custom tick timer (in μs)
For 1s tick-timer pass @1000000@ to this.
This does not scale the timers for collection modules passed into it.
So if @500000@ is passed here, and a collector should update every second
it needs to update every 2nd tick, so it has to be scaled.
@
startLoop getAsciiOut [ pollPack 1 $ getRawCPU ]
startLoopT 500000 getAsciiOut [ pollPack 2 $ getRawCPU ]
@
-}
startLoopT :: MonkyOutput o => Int -> IO o -> [IO Modules] -> IO ()
startLoopT t out mods = do
mvar <- newEmptyMVar
m <- sequence mods
l <- mapM (packMod mvar) m
o <- out
mainLoop t mvar 0 o l
-- | Start the mainLoop of monky. This should be the return type of your main
startLoop :: MonkyOutput o => IO o -> [IO Modules] -> IO ()
startLoop out mods = startLoopT 1000000 out mods