From a845465fec735d9818a61d078337653b5293da5c Mon Sep 17 00:00:00 2001 From: Michal Zielonka Date: Thu, 7 Oct 2021 16:39:20 +0200 Subject: add support swap info for freebsd in freebsd swap info is available by sysctl --- src/Xmobar/Plugins/Monitors/Swap.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Xmobar/Plugins/Monitors/Swap.hs b/src/Xmobar/Plugins/Monitors/Swap.hs index fcaab84..ca46010 100644 --- a/src/Xmobar/Plugins/Monitors/Swap.hs +++ b/src/Xmobar/Plugins/Monitors/Swap.hs @@ -1,3 +1,4 @@ +{-#LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Plugins.Monitors.Swap @@ -16,13 +17,44 @@ module Xmobar.Plugins.Monitors.Swap where import Xmobar.Plugins.Monitors.Common +#ifdef FREEBSD +import System.BSD.Sysctl (sysctlReadUInt, sysctlReadULong) +#else import qualified Data.ByteString.Lazy.Char8 as B +#endif swapConfig :: IO MConfig swapConfig = mkMConfig "Swap: %" -- template ["usedratio", "total", "used", "free"] -- available replacements +#ifdef FREEBSD + +isEnabled :: IO Bool +isEnabled = do + enabled <- sysctlReadUInt "vm.swap_enabled" + return $ enabled == 1 + +parseMEM' :: Bool -> IO [Float] +parseMEM' False = return $ [] +parseMEM' True = do + swapIn <- sysctlReadUInt "vm.stats.vm.v_swapin" + swapTotal <- sysctlReadULong "vm.swap_total" + let tot = toInteger swapTotal + free = tot - (toInteger swapIn) + + return $ res (fromInteger tot) (fromInteger free) + where + res :: Float -> Float -> [Float] + res _ 0 = [] + res tot free = [(tot - free) / tot, tot, tot - free, free] + +parseMEM :: IO [Float] +parseMEM = do + enabled <- isEnabled + parseMEM' enabled + +#else fileMEM :: IO B.ByteString fileMEM = B.readFile "/proc/meminfo" @@ -41,6 +73,8 @@ parseMEM = free = get_data "SwapFree:" st return [(tot - free) / tot, tot, tot - free, free] +#endif + formatSwap :: [Float] -> Monitor [String] formatSwap (r:xs) = do d <- getConfigValue decDigits -- cgit v1.2.3