diff options
author | Michal Zielonka <michal.zielonka.8001@gmail.com> | 2022-04-11 22:53:29 +0200 |
---|---|---|
committer | Michal Zielonka <michal.zielonka.8001@gmail.com> | 2022-04-11 22:53:29 +0200 |
commit | 15dccb714af16c786ebb63cf619f885495781f5f (patch) | |
tree | e2f1f8fb8511ee35ddf6f42a974fc9aba7966b31 /src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc | |
parent | 7af1ddc5ebbe9d651ffbc057614be18637e69939 (diff) | |
download | xmobar-15dccb714af16c786ebb63cf619f885495781f5f.tar.gz xmobar-15dccb714af16c786ebb63cf619f885495781f5f.tar.bz2 |
add load monitor for freebsd
Diffstat (limited to 'src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc')
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc b/src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc new file mode 100644 index 0000000..bde7e91 --- /dev/null +++ b/src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc @@ -0,0 +1,58 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +{-# LANGUAGE CPP #-} +{-# LANGUAGE CApiFFI #-} +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.Load.FreeBSD +-- Copyright : Finn Lawler +-- License : BSD-style (see LICENSE) +-- +-- Author : Finn Lawler <flawler@cs.tcd.ie> +-- Maintainer : jao <mail@jao.io> +-- Stability : unstable +-- Portability : unportable +-- +-- A load average monitor for Xmobar. Adapted from +-- Xmobar.Plugins.Monitors.Thermal by Juraj Hercek. +-- +----------------------------------------------------------------------------- + +module Xmobar.Plugins.Monitors.Load.FreeBSD (fetchLoads) where + +import Xmobar.Plugins.Monitors.Load.Common (Result(..)) +import Foreign.C.Types (CUInt, CUIntMax) +import Foreign.Marshal.Array (peekArray) +import Foreign.Ptr (plusPtr) +import Foreign.Storable (Storable, alignment, peek, peekByteOff, poke, sizeOf) +import System.BSD.Sysctl (sysctlPeek) + +#include <sys/resource.h> + + +data LoadAvg = LoadAvg {loads :: [Float]} + + +calcLoad :: CUInt -> CUIntMax -> Float +calcLoad l s = ((fromIntegral . toInteger) l) / ((fromIntegral . toInteger) s) + + +instance Storable LoadAvg where + alignment _ = #{alignment struct loadavg} + sizeOf _ = #{size struct loadavg} + peek ptr = do + load_values <- peekArray 3 $ #{ptr struct loadavg, ldavg} ptr :: IO [CUInt] + scale <- #{peek struct loadavg, fscale} ptr :: IO CUIntMax + let + l1 = calcLoad (load_values !! 0) scale + l5 = calcLoad (load_values !! 1) scale + l15 = calcLoad (load_values !! 2) scale + + return $ LoadAvg{loads = [l1, l5, l15]} + + poke _ _ = pure () + + +fetchLoads :: IO Result +fetchLoads = do + res <- sysctlPeek "vm.loadavg" :: IO LoadAvg + return $ Result (loads res) |