From 15dccb714af16c786ebb63cf619f885495781f5f Mon Sep 17 00:00:00 2001 From: Michal Zielonka Date: Mon, 11 Apr 2022 22:53:29 +0200 Subject: add load monitor for freebsd --- src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc (limited to 'src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc') 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 +-- Maintainer : jao +-- 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 + + +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) -- cgit v1.2.3