summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Plugins/Monitors/Load/FreeBSD.hsc
blob: bde7e919d6cf4ff214031253ff7919bcc7b8026e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)