From a0397ad2ad0f9382bccf4e10ecf1435dab9cf01c Mon Sep 17 00:00:00 2001 From: "Robert J. Macomber" Date: Fri, 27 Jun 2014 18:22:30 -0700 Subject: Fix for CPU monitor on long-running systems After running long enough, the numbers in /proc/stat get big enough that they will not fit in a Float without loss of precision, which leads to erratic results such as reporting "NaN%" CPU usage. This commit simply keeps the numbers integral until producing the final percentage result. --- src/Plugins/Monitors/Cpu.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Plugins/Monitors/Cpu.hs b/src/Plugins/Monitors/Cpu.hs index 6e83c67..df2dc4e 100644 --- a/src/Plugins/Monitors/Cpu.hs +++ b/src/Plugins/Monitors/Cpu.hs @@ -24,12 +24,12 @@ cpuConfig = mkMConfig "Cpu: %" ["bar","vbar","total","user","nice","system","idle","iowait"] -type CpuDataRef = IORef [Float] +type CpuDataRef = IORef [Int] -cpuData :: IO [Float] +cpuData :: IO [Int] cpuData = cpuParser `fmap` B.readFile "/proc/stat" -cpuParser :: B.ByteString -> [Float] +cpuParser :: B.ByteString -> [Int] cpuParser = map (read . B.unpack) . tail . B.words . head . B.lines parseCpu :: CpuDataRef -> IO [Float] @@ -38,8 +38,8 @@ parseCpu cref = b <- cpuData writeIORef cref b let dif = zipWith (-) b a - tot = foldr (+) 0 dif - percent = map (/ tot) dif + tot = fromIntegral $ foldr (+) 0 dif + percent = map (/ tot) (map fromIntegral dif) return percent formatCpu :: [Float] -> Monitor [String] -- cgit v1.2.3