diff options
author | Robert J. Macomber <robertm@dilithium> | 2014-06-27 18:22:30 -0700 |
---|---|---|
committer | Robert J. Macomber <robertm@dilithium> | 2014-06-27 18:22:30 -0700 |
commit | a0397ad2ad0f9382bccf4e10ecf1435dab9cf01c (patch) | |
tree | 527577492e0c7e392ec7eb8154294fb970ca5b58 | |
parent | cfb6773cd3761ae7df279bc2387edb6547aa15b9 (diff) | |
download | xmobar-a0397ad2ad0f9382bccf4e10ecf1435dab9cf01c.tar.gz xmobar-a0397ad2ad0f9382bccf4e10ecf1435dab9cf01c.tar.bz2 |
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.
-rw-r--r-- | src/Plugins/Monitors/Cpu.hs | 10 |
1 files 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: <total>%" ["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] |