From 5ca683d08663cf623422dcfd2085db799bf761df Mon Sep 17 00:00:00 2001 From: Samuli Thomasson Date: Wed, 12 Nov 2014 09:10:15 +0200 Subject: Introduce available memory variables in memory monitor Available memory is determined from meminfo's `MemAvailable:` on 3.14 and newer, and falls back to `free + buffer + cache` on older systems. Introduces new variables ``. --- readme.md | 6 ++++-- src/Plugins/Monitors/Mem.hs | 36 +++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/readme.md b/readme.md index 074f9bc..4cdc571 100644 --- a/readme.md +++ b/readme.md @@ -768,10 +768,12 @@ something like: - Args: default monitor arguments, plus: - `--used-icon-pattern`: dynamic string for used memory ratio in `usedipat`. - `--free-icon-pattern`: dynamic string for free memory ratio in `freeipat`. + - `--available-icon-pattern`: dynamic string for available memory ratio in `availableipat`. - Variables that can be used with the `-t`/`--template` argument: - `total`, `free`, `buffer`, `cache`, `rest`, `used`, + `total`, `free`, `buffer`, `cache`, `available`, `used`, `usedratio`, `usedbar`, `usedvbar`, `usedipat`, - `freeratio`, `freebar`, `freevbar`, `freeipat` + `freeratio`, `freebar`, `freevbar`, `freeipat`, + `availableratio`, `availablebar`, `availablevbar`, `availableipat` - Default template: `Mem: % (M)` ### `Swap Args RefreshRate` diff --git a/src/Plugins/Monitors/Mem.hs b/src/Plugins/Monitors/Mem.hs index b19c5a7..403fa43 100644 --- a/src/Plugins/Monitors/Mem.hs +++ b/src/Plugins/Monitors/Mem.hs @@ -21,12 +21,14 @@ import System.Console.GetOpt data MemOpts = MemOpts { usedIconPattern :: Maybe IconPattern , freeIconPattern :: Maybe IconPattern + , availableIconPattern :: Maybe IconPattern } defaultOpts :: MemOpts defaultOpts = MemOpts { usedIconPattern = Nothing , freeIconPattern = Nothing + , availableIconPattern = Nothing } options :: [OptDescr (MemOpts -> MemOpts)] @@ -35,6 +37,8 @@ options = o { usedIconPattern = Just $ parseIconPattern x }) "") "" , Option "" ["free-icon-pattern"] (ReqArg (\x o -> o { freeIconPattern = Just $ parseIconPattern x }) "") "" + , Option "" ["available-icon-pattern"] (ReqArg (\x o -> + o { availableIconPattern = Just $ parseIconPattern x }) "") "" ] parseOpts :: [String] -> IO MemOpts @@ -46,8 +50,10 @@ parseOpts argv = memConfig :: IO MConfig memConfig = mkMConfig "Mem: % (M)" -- template - ["usedbar", "usedvbar", "usedipat", "freebar", "freevbar", "freeipat", "usedratio", "freeratio", - "total", "free", "buffer", "cache", "rest", "used"] -- available replacements + ["usedbar", "usedvbar", "usedipat", "freebar", "freevbar", "freeipat", + "availablebar", "availablevbar", "availableipat", + "usedratio", "freeratio", "availableratio", + "total", "free", "buffer", "cache", "available", "used"] -- available replacements fileMEM :: IO String fileMEM = readFile "/proc/meminfo" @@ -58,11 +64,12 @@ parseMEM = let content = map words $ take 8 $ lines file info = M.fromList $ map (\line -> (head line, (read $ line !! 1 :: Float) / 1024)) content [total, free, buffer, cache] = map (info M.!) ["MemTotal:", "MemFree:", "Buffers:", "Cached:"] - rest = free + buffer + cache - used = total - M.findWithDefault rest "MemAvailable:" info + available = M.findWithDefault (free + buffer + cache) "MemAvailable:" info + used = total - available usedratio = used / total freeratio = free / total - return [usedratio, freeratio, total, free, buffer, cache, rest, used, freeratio] + availableratio = available / total + return [usedratio, freeratio, availableratio, total, free, buffer, cache, available, used] totalMem :: IO Float totalMem = fmap ((*1024) . (!!1)) parseMEM @@ -71,19 +78,14 @@ usedMem :: IO Float usedMem = fmap ((*1024) . (!!6)) parseMEM formatMem :: MemOpts -> [Float] -> Monitor [String] -formatMem opts (r:fr:xs) = +formatMem opts (r:fr:ar:xs) = do let f = showDigits 0 - rr = 100 * r - ub <- showPercentBar rr r - uvb <- showVerticalBar rr r - uipat <- showIconPattern (usedIconPattern opts) r - fb <- showPercentBar (100 - rr) (1 - r) - fvb <- showVerticalBar (100 - rr) ( 1 - r) - fipat <- showIconPattern (freeIconPattern opts) (1 - r) - rs <- showPercentWithColors r - fs <- showPercentWithColors fr - s <- mapM (showWithColors f) xs - return (ub:uvb:uipat:fb:fvb:fipat:rs:fs:s) + mon i x = [showPercentBar (100 * x) x, showVerticalBar (100 * x) x, showIconPattern i x] + sequence $ mon (usedIconPattern opts) r + ++ mon (freeIconPattern opts) fr + ++ mon (availableIconPattern opts) ar + ++ map showPercentWithColors [r, fr, ar] + ++ map (showWithColors f) xs formatMem _ _ = replicate 10 `fmap` getConfigValue naString runMem :: [String] -> Monitor String -- cgit v1.2.3