diff options
author | Jose A. Ortega Ruiz <jao@gnu.org> | 2010-01-17 23:15:00 +0100 |
---|---|---|
committer | Jose A. Ortega Ruiz <jao@gnu.org> | 2010-01-17 23:15:00 +0100 |
commit | 75780faeb8d16686f8bedd169e04b430b4618e19 (patch) | |
tree | 1e076af70433a037e54f03eb875e95cee01089b6 /Plugins/Monitors/MultiCpu.hs | |
parent | 796d67c238deaa23fcc08d116d16193c8c6e014f (diff) | |
download | xmobar-75780faeb8d16686f8bedd169e04b430b4618e19.tar.gz xmobar-75780faeb8d16686f8bedd169e04b430b4618e19.tar.bz2 |
New MultiCpu monitor that works like Cpu for multiple CPU cores
Ignore-this: 64152f888f1e007612dc8e1f1eace875
darcs-hash:20100117221500-40885-23930af67e2b5446712b1bced29ce56ec696965e.gz
Diffstat (limited to 'Plugins/Monitors/MultiCpu.hs')
-rw-r--r-- | Plugins/Monitors/MultiCpu.hs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Plugins/Monitors/MultiCpu.hs b/Plugins/Monitors/MultiCpu.hs new file mode 100644 index 0000000..02fa842 --- /dev/null +++ b/Plugins/Monitors/MultiCpu.hs @@ -0,0 +1,67 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.MultiCpu +-- Copyright : (c) Jose A Ortega Ruiz +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A Ortega <jao@gnu.org> +-- Stability : unstable +-- Portability : unportable +-- +-- A multi-cpu monitor for Xmobar +-- +----------------------------------------------------------------------------- + +module Plugins.Monitors.MultiCpu where + +import Plugins.Monitors.Common +import qualified Data.ByteString.Lazy.Char8 as B +import Data.List(isPrefixOf) + +multiCpuConfig :: IO MConfig +multiCpuConfig = mkMConfig + "Cpu: <total>" + [ k ++ n | n <- "":(map show [0 :: Int ..]) + , k <- ["total","user","nice","system","idle"]] + + +multiCpuData :: IO [[Float]] +multiCpuData = do s <- B.readFile "/proc/stat" + return $ multiCpuParser s + +multiCpuParser :: B.ByteString -> [[Float]] +multiCpuParser = map (map read . tail) . lns + where lns = takeWhile isCpu . map unpW . B.lines + isCpu (w:_) = "cpu" `isPrefixOf` w + isCpu _ = False + unpW = map B.unpack . B.words + +parseMultiCpu :: IO [[Float]] +parseMultiCpu = + do (as, bs) <- doActionTwiceWithDelay 350000 multiCpuData + let p0 = zipWith percent bs as + (as', bs') <- doActionTwiceWithDelay 350000 multiCpuData + let p1 = zipWith percent bs' as' + return $ zipWith (\x y -> zipWith (\a b -> (a + b) / 2.0) x y) p1 p0 + +percent :: [Float] -> [Float] -> [Float] +percent b a = map (/ tot) $ take 4 dif + where dif = zipWith (-) b a + tot = foldr (+) 0 dif + +formatMultiCpus :: [[Float]] -> Monitor [String] +formatMultiCpus [] = return $ take 15 (repeat "-1%") +formatMultiCpus xs = fmap concat $ mapM formatMultiCpu xs + +formatMultiCpu :: [Float] -> Monitor [String] +formatMultiCpu x + | length x < 4 = return $ take 5 (repeat "") + | otherwise = mapM (showWithColors f) . map (* 100) $ (t:x) + where f s = floatToPercent (s / 100) + t = foldr (+) 0 $ take 3 x + +runMultiCpu :: [String] -> Monitor String +runMultiCpu _ = + do c <- io $ parseMultiCpu + l <- formatMultiCpus c + parseTemplate l |