Skip to content

Commit

Permalink
trace-resources: optimize; add microbenchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeier authored and jutaro committed Nov 22, 2023
1 parent 8862441 commit 232f807
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
22 changes: 22 additions & 0 deletions trace-resources/bench/trace-resources-bench.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{-# LANGUAGE CPP #-}

import Criterion.Main
import Criterion.Types


#if defined(linux_HOST_OS)
import qualified Cardano.Logging.Resources.Linux as Platform
#elif defined(mingw32_HOST_OS)
import qualified Cardano.Logging.Resources.Windows as Platform
#elif defined(darwin_HOST_OS)
import qualified Cardano.Logging.Resources.Darwin as Platform
#else
import qualified Cardano.Logging.Resources.Dummy as Platform
#endif


main :: IO ()
main =
defaultMainWith defaultConfig{ timeLimit = 15 }
[ bench "create record ResourceStats" (whnfIO Platform.readResourceStatsInternal)
]
14 changes: 11 additions & 3 deletions trace-resources/src/Cardano/Logging/Resources/Linux.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ module Cardano.Logging.Resources.Linux

import Cardano.Logging.Resources.Types
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import qualified Data.Text.IO as T (readFile)
import qualified Data.Text.Read as T (decimal)
import Data.Word
import qualified GHC.Stats as GhcStats
import System.Posix.Files (fileMode, getFileStatus, intersectFileModes, ownerReadMode)
import Text.Read (readMaybe)

-- * Disk IO stats:
-- /proc/[pid]/io (since kernel 2.6.20)
Expand Down Expand Up @@ -146,9 +148,15 @@ readProcList fp = do
fs <- getFileStatus fp
if readable fs
then do
cs <- readFile fp
return $ map (\s -> fromMaybe 0 (readMaybe s :: Maybe Integer)) (words cs)
cs <- T.readFile fp
return $ map (fromMaybe 0 . readMaybeText) (T.words cs)
else
return []
where
readable fs = intersectFileModes (fileMode fs) ownerReadMode == ownerReadMode

readMaybeText :: Integral a => T.Text -> Maybe a
readMaybeText t =
case T.decimal t of
Right (v, _) -> Just v
_ -> Nothing
16 changes: 15 additions & 1 deletion trace-resources/trace-resources.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.0

name: trace-resources
version: 0.2.0.2
version: 0.2.1.0
synopsis: Package for tracing resources for linux, mac and windows
description: Package for tracing resources for linux, mac and windows.
category: Cardano,
Expand Down Expand Up @@ -84,3 +84,17 @@ test-suite trace-resources-test
-Wredundant-constraints
-Wmissing-export-lists
-Wno-incomplete-patterns

benchmark bench
import: project-config
type: exitcode-stdio-1.0
main-is: trace-resources-bench.hs
hs-source-dirs: bench
build-depends: base >=4.12 && <5
, criterion
, trace-resources
ghc-options:
-Wall -Wcompat -Wincomplete-record-updates
-Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints
-Wunused-packages -threaded -rtsopts -O2
"-with-rtsopts=-T"

0 comments on commit 232f807

Please sign in to comment.