From 9670c0b9e835965717486ed815db993244df9cea Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Fri, 12 Feb 2010 21:45:20 +0100 Subject: Using plain IORef instead of MVar in TopCpu Ignore-this: cb81e0c1b5ba9b3837924199f731b20f darcs-hash:20100212204520-1d908-2b1e352c3fb7499373e6e362157b795750e511cc.gz --- Plugins/Monitors/Top.hs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/Plugins/Monitors/Top.hs b/Plugins/Monitors/Top.hs index 3473785..2e235d8 100644 --- a/Plugins/Monitors/Top.hs +++ b/Plugins/Monitors/Top.hs @@ -19,13 +19,13 @@ module Plugins.Monitors.Top (startTopCpu, topMemConfig, runTopMem) where import Plugins.Monitors.Common import Control.Exception (SomeException, handle, evaluate) -import Control.Concurrent import System.Directory import System.FilePath import System.Posix.Unistd (getSysVar, SysVar(ClockTick)) import Foreign.C.Types import Data.List (sortBy, foldl') import Data.Ord (comparing) +import Data.IORef import Data.IntMap (IntMap) import qualified Data.IntMap as M @@ -109,7 +109,7 @@ type Pid = Int type TimeInfo = (String, Float) type TimeEntry = (Pid, TimeInfo) type Times = IntMap TimeInfo -type TIVar = MVar Times +type TimesRef = IORef Times timeinfo :: FilePath -> IO TimeEntry timeinfo = handlePidFile (0, ("", 0)) $ \fs -> @@ -130,28 +130,29 @@ combineTimeInfos :: Times -> Times -> Times combineTimeInfos !t0 !t1 = M.intersectionWith timeDiff t1 t0 where timeDiff (n, x1) (_, x0) = (n, (x1 - x0)) -topTimeProcesses :: Int -> TIVar -> Float -> IO [TimeInfo] -topTimeProcesses n tivar lapse = do - modifyMVar tivar $ \t0 -> - timeinfos >>= (\t1 -> - let !ts = M.elems $ combineTimeInfos t0 t1 - !sts = take n $ sortBy (flip (comparing snd)) ts - !nts = map norm sts - norm (nm, t) = (nm, 100 * t / lapse) - in return $ (t1, nts)) +topTimeProcesses :: Int -> TimesRef -> Float -> IO [TimeInfo] +topTimeProcesses n tref lapse = do + t1 <- timeinfos + t0 <- readIORef tref + writeIORef tref $! t1 + let !ts = M.elems $ combineTimeInfos t0 t1 + !sts = take n $ sortBy (flip (comparing snd)) ts + !nts = map norm sts + norm (nm, t) = (nm, 100 * t / lapse) + return nts showTimeInfo :: TimeInfo -> Monitor [String] showTimeInfo (n, t) = showInfo n (showDigits 1 t) t -runTopCpu :: TIVar -> Float -> [String] -> Monitor String -runTopCpu tivar lapse _ = do - ps <- io $ topTimeProcesses maxProc tivar lapse +runTopCpu :: TimesRef -> Float -> [String] -> Monitor String +runTopCpu tref lapse _ = do + ps <- io $ topTimeProcesses maxProc tref lapse pstr <- mapM showTimeInfo ps parseTemplate $ concat pstr startTopCpu :: [String] -> Int -> (String -> IO ()) -> IO () startTopCpu a r cb = do t <- getSysVar ClockTick - tivar <- newMVar M.empty + tref <- newIORef M.empty let lapse = (fromIntegral r * fromIntegral t) / 10 - runM a topCpuConfig (runTopCpu tivar lapse) r cb + runM a topCpuConfig (runTopCpu tref lapse) r cb -- cgit v1.2.3