module Monky.Memory
( MemoryHandle
, getMemoryAvailable
, getMemoryHandle
, getMemoryTotal
, getMemoryUsed
, getMemoryFree
, getMemoryStats
)
where
import Monky.Utility (fopen, readContent, File)
import Data.Maybe (fromMaybe, listToMaybe)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
data MemoryHandle = MemoryH File
path :: String
path = "/proc/meminfo"
getVal :: ByteString -> Int
getVal = fst . fromMaybe (error "Failed to read as int in memory module") . BS.readInt . (!! 1) . BS.words
findLine :: ByteString -> [ByteString] -> Maybe ByteString
findLine x = listToMaybe . filter (x `BS.isPrefixOf`)
getMemoryAvailable :: MemoryHandle -> IO Int
getMemoryAvailable (MemoryH f) = do
contents <- readContent f
let line = findLine "MemAvailable" contents
case line of
Nothing -> do
let buffs = fromMaybe err $ findLine "Buffers" contents
cached = fromMaybe err $ findLine "Cached" contents
free <- getMemoryFree (MemoryH f)
return $ getVal buffs + getVal cached + free
(Just x) -> return . getVal $ x
where err = error "Could not find one of the fallback values for MemAvailable, please report this together with the content of /proc/meminfo"
getMemoryTotal :: MemoryHandle -> IO Int
getMemoryTotal (MemoryH f) = do
contents <- readContent f
return . getVal . fromMaybe err . findLine "MemTotal" $ contents
where err = error "Could not find MemTotal in /proc/meminfo. Please report this bug with the content of /proc/meminfo"
getMemoryFree :: MemoryHandle -> IO Int
getMemoryFree (MemoryH f) = do
contents <- readContent f
return . getVal . fromMaybe err . findLine "MemFree" $ contents
where err = error "Could not find MemFree in /proc/meminfo. Please report this bug with the content of /proc/meminfo"
getMemoryUsed :: MemoryHandle -> IO Int
getMemoryUsed h = do
(_, _, _, used) <- getMemoryStats h
return used
getMemoryStats :: MemoryHandle -> IO (Int, Int, Int, Int)
getMemoryStats (MemoryH f) = do
contents <- readContent f
let avail = getVal . fromMaybe "a 0" . findLine "MemAvailable" $ contents
total = getVal . fromMaybe "a 0" . findLine "MemTotal" $ contents
free = getVal . fromMaybe "a 0" . findLine "MemFree" $ contents
used = (total avail)
return (total, avail, free, used)
getMemoryHandle :: IO MemoryHandle
getMemoryHandle = do
file <- fopen path
return $MemoryH file