diff options
Diffstat (limited to 'Plugins')
-rw-r--r-- | Plugins/Monitors.hs | 4 | ||||
-rw-r--r-- | Plugins/Monitors/MultiCpu.hs | 67 |
2 files changed, 71 insertions, 0 deletions
diff --git a/Plugins/Monitors.hs b/Plugins/Monitors.hs index 07662be..72e0d50 100644 --- a/Plugins/Monitors.hs +++ b/Plugins/Monitors.hs @@ -22,6 +22,7 @@ import Plugins.Monitors.Net import Plugins.Monitors.Mem import Plugins.Monitors.Swap import Plugins.Monitors.Cpu +import Plugins.Monitors.MultiCpu import Plugins.Monitors.Batt import Plugins.Monitors.Thermal import Plugins.Monitors.CpuFreq @@ -32,6 +33,7 @@ data Monitors = Weather Station Args Rate | Memory Args Rate | Swap Args Rate | Cpu Args Rate + | MultiCpu Args Rate | Battery Args Rate | BatteryP [String] Args Rate | Thermal Zone Args Rate @@ -54,6 +56,7 @@ instance Exec Monitors where alias (Memory _ _) = "memory" alias (Swap _ _) = "swap" alias (Cpu _ _) = "cpu" + alias (MultiCpu _ _) = "multicpu" alias (Battery _ _) = "battery" alias (BatteryP _ _ _)= "battery" alias (CpuFreq _ _) = "cpufreq" @@ -64,6 +67,7 @@ instance Exec Monitors where start (Memory a r) = runM a memConfig runMem r start (Swap a r) = runM a swapConfig runSwap r start (Cpu a r) = runM a cpuConfig runCpu r + start (MultiCpu a r) = runM a multiCpuConfig runMultiCpu r start (Battery a r) = runM a battConfig runBatt r start (BatteryP s a r) = runM a battConfig (runBatt' s) r start (CpuFreq a r) = runM a cpuFreqConfig runCpuFreq r 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 |