summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins/Monitors/MultiCpu.hs
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2011-02-13 04:57:17 +0100
committerJose Antonio Ortega Ruiz <jao@gnu.org>2011-02-13 04:57:17 +0100
commit31715e3db637702d1289819909131a32abb3777a (patch)
treef2bc074c66479c940c88aadffacf5300619c8af7 /src/Plugins/Monitors/MultiCpu.hs
parent8f7606493f55c503e5be9047b176ad4941c06a0b (diff)
downloadxmobar-31715e3db637702d1289819909131a32abb3777a.tar.gz
xmobar-31715e3db637702d1289819909131a32abb3777a.tar.bz2
Cpu and MultiCpu should also be more accurate now
Diffstat (limited to 'src/Plugins/Monitors/MultiCpu.hs')
-rw-r--r--src/Plugins/Monitors/MultiCpu.hs24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/Plugins/Monitors/MultiCpu.hs b/src/Plugins/Monitors/MultiCpu.hs
index d93c788..a1bb082 100644
--- a/src/Plugins/Monitors/MultiCpu.hs
+++ b/src/Plugins/Monitors/MultiCpu.hs
@@ -12,11 +12,12 @@
--
-----------------------------------------------------------------------------
-module Plugins.Monitors.MultiCpu(multiCpuConfig, runMultiCpu) where
+module Plugins.Monitors.MultiCpu (startMultiCpu) where
import Plugins.Monitors.Common
import qualified Data.ByteString.Lazy.Char8 as B
import Data.List (isPrefixOf, transpose, unfoldr)
+import Data.IORef (IORef, newIORef, readIORef, writeIORef)
multiCpuConfig :: IO MConfig
multiCpuConfig =
@@ -26,6 +27,7 @@ multiCpuConfig =
, k <- monitors]
where monitors = ["bar","total","user","nice","system","idle"]
+type CpuDataRef = IORef [[Float]]
cpuData :: IO [[Float]]
cpuData = parse `fmap` B.readFile "/proc/stat"
@@ -35,9 +37,11 @@ cpuData = parse `fmap` B.readFile "/proc/stat"
isCpu _ = False
parseList = map (parseFloat . B.unpack) . tail
-parseCpuData :: IO [[Float]]
-parseCpuData =
- do (as, bs) <- doActionTwiceWithDelay 950000 cpuData
+parseCpuData :: CpuDataRef -> IO [[Float]]
+parseCpuData cref =
+ do as <- readIORef cref
+ bs <- cpuData
+ writeIORef cref bs
let p0 = zipWith percent bs as
return p0
@@ -68,9 +72,15 @@ formatAutoCpus :: [String] -> Monitor [String]
formatAutoCpus [] = return $ replicate 6 ""
formatAutoCpus xs = return $ map unwords (groupData xs)
-runMultiCpu :: [String] -> Monitor String
-runMultiCpu _ =
- do c <- io parseCpuData
+runMultiCpu :: CpuDataRef -> [String] -> Monitor String
+runMultiCpu cref _ =
+ do c <- io $ parseCpuData cref
l <- formatMultiCpus c
a <- formatAutoCpus l
parseTemplate $ a ++ l
+
+startMultiCpu :: [String] -> Int -> (String -> IO ()) -> IO ()
+startMultiCpu a r cb = do
+ cref <- newIORef [[]]
+ _ <- parseCpuData cref
+ runM a multiCpuConfig (runMultiCpu cref) r cb