From 82461417abbcd2e03e4329d68df4a15866885dd2 Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Tue, 30 Mar 2021 14:43:56 -0700 Subject: Add FreeBSD support to Cpu plugin --- src/Xmobar/Plugins/Monitors/Cpu.hs | 51 +++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Xmobar/Plugins/Monitors/Cpu.hs b/src/Xmobar/Plugins/Monitors/Cpu.hs index 895eeb3..cf1b1ec 100644 --- a/src/Xmobar/Plugins/Monitors/Cpu.hs +++ b/src/Xmobar/Plugins/Monitors/Cpu.hs @@ -1,3 +1,4 @@ +{-#LANGUAGE CPP #-} {-#LANGUAGE RecordWildCards#-} ----------------------------------------------------------------------------- @@ -29,6 +30,9 @@ module Xmobar.Plugins.Monitors.Cpu import Xmobar.Plugins.Monitors.Common import qualified Data.ByteString.Lazy.Char8 as B import Data.IORef (IORef, newIORef, readIORef, writeIORef) +#ifdef FREEBSD +import System.BSD.Sysctl (sysctlPeekArray) +#endif import System.Console.GetOpt import Xmobar.App.Timer (doEveryTenthSeconds) import Control.Monad (void) @@ -90,6 +94,43 @@ cpuConfig = , iowaitField ] +data CpuData = CpuData { + cpuUser :: !Float, + cpuNice :: !Float, + cpuSystem :: !Float, + cpuIdle :: !Float, + cpuIowait :: !Float, + cpuTotal :: !Float + } + +#ifdef FREEBSD +-- kern.cp_time data from the previous iteration for computing the difference +type CpuDataRef = IORef [Word] + +cpuData :: IO [Word] +cpuData = sysctlPeekArray "kern.cp_time" :: IO [Word] + +parseCpu :: CpuDataRef -> IO CpuData +parseCpu cref = do + prev <- readIORef cref + curr <- cpuData + writeIORef cref curr + let diff = map fromIntegral $ zipWith (-) curr prev + user = diff !! 0 + nice = diff !! 1 + system = diff !! 2 + intr = diff !! 3 + idle = diff !! 4 + total = user + nice + system + intr + idle + return CpuData + { cpuUser = user/total + , cpuNice = nice/total + , cpuSystem = (system+intr)/total + , cpuIdle = idle/total + , cpuIowait = 0 + , cpuTotal = user/total + } +#else type CpuDataRef = IORef [Int] -- Details about the fields here: https://www.kernel.org/doc/Documentation/filesystems/proc.txt @@ -104,15 +145,6 @@ readInt bs = case B.readInt bs of cpuParser :: B.ByteString -> [Int] cpuParser = map readInt . tail . B.words . head . B.lines -data CpuData = CpuData { - cpuUser :: !Float, - cpuNice :: !Float, - cpuSystem :: !Float, - cpuIdle :: !Float, - cpuIowait :: !Float, - cpuTotal :: !Float - } - convertToCpuData :: [Float] -> CpuData convertToCpuData (u:n:s:ie:iw:_) = CpuData @@ -137,6 +169,7 @@ parseCpu cref = v -> fromIntegral n / v percent = map safeDiv dif return $ convertToCpuData percent +#endif data Field = Field { fieldName :: !String, -- cgit v1.2.3