Skip to content

Commit

Permalink
Add usage examples to the README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
adetokunbo committed Jan 15, 2024
1 parent de26050 commit 93524a9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,61 @@ giving output like this:
```

## Haskell Usage

The functions exported by `System.MemInfo` can be used to obtain memory usage
of programs from haskell code:

#### Example: print the program name and memory usage of single process

```haskell
import System.MemInfo (
readForOnePid,
printUsage,
ProcessID
)

showUsageOf :: ProcessID -> IO ()
showUsageOf pid = do
orError <- readForOnePid pid
case orError of
Left err -> putStrLn $ show err
Right usage -> printUsage usage

main :: IO ()
main = showUsageOf 96334 -- replace with your own process ID
```

#### Example: monitor the memory of a single process continuously

```haskell
import System.MemInfo (
mkReportBud,
printUsage,
unfoldMemUsageAfter,
ProcessID
)

-- | Use 'unfoldMemUsageAfter' to periodically print out the memory usage
monitorRamOf :: ProcessID -> IO ()
monitorRamOf pid = do
budMb <- mkReportBud $ NE.singleton pid
case budMb of
Nothing -> putStrLn $ "Failed to read the system info"
Just bud -> do
let gap = 10 :: Int -- print every 10 sec
handleNext (Left _) = putStrLn "the process has stopped"
handleNext (Right ((mu, _), updated)) = do
putStrLn $ show mu
go updated
go x = unfoldMemUsageAfter gap x >>= handleNext
go bud

main :: IO ()
main = monitorRamOf 96334 -- replace with your own process ID
```


[hackage-deps-badge]: <https://img.shields.io/hackage-deps/v/mem-info.svg>
[hackage-deps]: <http://packdeps.haskellers.com/feed?needle=mem-info>
[hackage-badge]: <https://img.shields.io/hackage/v/mem-info.svg>
Expand Down
3 changes: 3 additions & 0 deletions mem-info.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ description:

* a library to enable core memory tracking on linux in haskell programs

See the [README](https://github.com/adetokunbo/mem-info/blob/master/README.md)
for further details

license: BSD-3-Clause
license-file: LICENSE
author: Tim Emiola
Expand Down
16 changes: 16 additions & 0 deletions src/System/MemInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ module System.MemInfo (
dropId,
withPid,

-- * print @MemUsage@
printUsage,
printUsage',

-- * re-export
mkReportBud,
ProcessID,
AsCmdName (..),
) where

import Data.Bifunctor (Bifunctor (..))
Expand Down Expand Up @@ -125,6 +131,16 @@ printMemUsages bud showSwap onlyTotal totals = do
reportFlaws bud showSwap onlyTotal


-- | Print the program name and memory usage, optionally hiding the swap value.
printUsage' :: AsCmdName a => (a, MemUsage) -> Bool -> IO ()
printUsage' (name, mu) showSwap = Text.putStrLn $ fmtMemUsage showSwap name mu


-- | Like printUsage, but alway shows the swap value
printUsage :: AsCmdName a => (a, MemUsage) -> IO ()
printUsage = flip printUsage' True


onlyPrintTotal :: ReportBud -> Bool -> Bool -> Map k MemUsage -> IO ()
onlyPrintTotal bud showSwap onlyTotal totals = do
let (private, swap) = overallTotals $ Map.elems totals
Expand Down

0 comments on commit 93524a9

Please sign in to comment.