summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJose A Ortega Ruiz <jao@gnu.org>2010-02-11 15:43:31 +0100
committerJose A Ortega Ruiz <jao@gnu.org>2010-02-11 15:43:31 +0100
commitbd038ecc92206601eadbc094b67bd652d1bd5d5b (patch)
tree1d6bf58ae159103f0147ffedb9ff0a121c0a67a1
parent5d719b4cde41e27c28b8c6342ccc40d97af04370 (diff)
downloadxmobar-bd038ecc92206601eadbc094b67bd652d1bd5d5b.tar.gz
xmobar-bd038ecc92206601eadbc094b67bd652d1bd5d5b.tar.bz2
Tweaks to top monitors
Ignore-this: 5080c889f37d103d6b1c1552b5daee73 darcs-hash:20100211144331-748be-85386cdc1a70b85520d0c7231f1c7b793c8e73be.gz
-rw-r--r--Plugins/Monitors/Top.hs33
1 files changed, 18 insertions, 15 deletions
diff --git a/Plugins/Monitors/Top.hs b/Plugins/Monitors/Top.hs
index 1c9443f..0d9c6bd 100644
--- a/Plugins/Monitors/Top.hs
+++ b/Plugins/Monitors/Top.hs
@@ -25,6 +25,7 @@ import Plugins.Monitors.Common
import Control.Exception
import System.Directory
import System.FilePath
+import System.IO
import Foreign.C.Types
import Data.List (sortBy, foldl')
import Data.Ord (comparing)
@@ -111,9 +112,11 @@ data TimesVal = TiV !Int !String !Float
cpuTime :: IO Integer
cpuTime = do
- s <- readFile "/proc/stat"
- let ts = map read . tail . words . (!!0) . lines
- return $ sum (ts s)
+ h <- openFile "/proc/stat" ReadMode
+ s <- hGetLine h
+ let ts = (map read . tail . words) s
+ hClose h
+ return $! sum $! ts
nullTimesVal :: TimesVal
nullTimesVal = TiV 0 "" 0
@@ -126,14 +129,15 @@ timeinfo pidf =
pid = read (head fs)
rf = read . (fs!!)
n = drop 1 $ init (fs!!1)
- evaluate $ TiV pid n (rf 14 + rf 15))
+ evaluate $! TiV pid n (rf 14 + rf 15))
-timeinfos :: IO [Times]
+timeinfos :: IO [(Integer, Times)]
timeinfos = do
fs <- processes
- tis <- mapM timeinfo fs
- return [foldl' acc M.empty tis]
- where acc m (TiV p n t) = if p > 10 then M.insert p (TI n t) m else m
+ tis <- mapM timeinfo $! fs
+ ct <- cpuTime
+ return $! [(ct, foldl' acc M.empty tis)]
+ where acc m (TiV p n t) = M.insert p (TI n t) m
combineTimeInfos :: Times -> Times -> Times
combineTimeInfos t0 t1 = M.intersectionWith timeDiff t1 t0
@@ -141,15 +145,14 @@ combineTimeInfos t0 t1 = M.intersectionWith timeDiff t1 t0
topTimeProcesses :: Int -> IO [Timeinfo]
topTimeProcesses n = do
- c0 <- cpuTime
- (t0:_, t1:_) <- doActionTwiceWithDelay 500000 timeinfos
- c1 <- cpuTime
+ ((c0, t0):_, (c1, t1):_) <- doActionTwiceWithDelay 1000000 timeinfos
let ts = M.elems $ combineTimeInfos t0 t1
- sts = take n $ sortBy (flip (comparing tm)) ts
+ sts = take n $ sortBy (flip (comparing tm)) (filter nzr ts)
+ nzr = (>0) . tm
tm (TI _ t) = t
- lapse = fromIntegral (c1 - c0) / 100
- norm (TI nm t) = TI nm (t/lapse)
- return $ if lapse > 0 then map norm sts else sts
+ lapse = fromIntegral (c1 - c0)
+ norm (TI nm t) = TI nm (100 * t / lapse)
+ return $! map norm sts ++ replicate 5 (TI "" 0)
showTimeInfo :: Timeinfo -> Monitor [String]
showTimeInfo (TI n t) = showInfo n (showDigits 2 t) t