From fb6a311de587ed3721538434d688524d2b2e2c95 Mon Sep 17 00:00:00 2001 From: Michael Karg Date: Tue, 21 Nov 2023 17:02:01 +0100 Subject: [PATCH] trace-resources: version 0.2.1.0; conditional compilation for netstat values --- trace-resources/CHANGELOG.md | 8 +++- .../src/Cardano/Logging/Resources/Linux.hs | 43 ++++++++----------- trace-resources/trace-resources.cabal | 9 ++++ 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/trace-resources/CHANGELOG.md b/trace-resources/CHANGELOG.md index 641629c3477..66a5b636a80 100644 --- a/trace-resources/CHANGELOG.md +++ b/trace-resources/CHANGELOG.md @@ -1,4 +1,10 @@ -# Revision history for trace-dispatcher +# Revision history for trace-resources + +## 0.2.1.0 -- Nov 2023 + +* Optimized resource record creation on Linux +* Add microbenchmark for resource record creation +* Add cabal flag `with-netstat` (default: False) to enable netstat values in Linux resource traces (potentially expensive) ## 0.2.0.2 -- Sep 2023 diff --git a/trace-resources/src/Cardano/Logging/Resources/Linux.hs b/trace-resources/src/Cardano/Logging/Resources/Linux.hs index 0c02a9be047..7f560905150 100644 --- a/trace-resources/src/Cardano/Logging/Resources/Linux.hs +++ b/trace-resources/src/Cardano/Logging/Resources/Linux.hs @@ -8,9 +8,9 @@ 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 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) @@ -84,30 +84,21 @@ readProcBlockInOut = do -- IpExt: 0 0 20053 8977 2437 23 3163525943 196480057 2426648 1491754 394285 5523 0 3513269 0 217426 0 -- readProcNetInOut :: IO (Word64, Word64) +#ifdef WITH_NETSTAT +readProcNetInOut = do + fields <- T.words . fourthLine . T.lines <$> T.readFile "/proc/self/net/netstat" + case -- We're only interested in 'InOctets' & 'OutOctets': + fmap readMaybeText . take 2 . drop 7 $ fields of + [Just netIn, Just netOut] -> pure (netIn, netOut) + _ -> pure (0, 0) + where + -- Assumption: 'IpExt:' values are on the fourth line of how the kernel displays the buffer + fourthLine ls = case drop 3 ls of + l:_ -> l + _ -> T.empty +#else readProcNetInOut = pure (0, 0) --- do --- fields <- words . lastline . lines <$> readFile "/proc/self/net/netstat" --- case -- We're only interested in 'InOctets' & 'OutOctets': --- fmap readMaybe . take 2 . drop 7 $ fields of --- [Just netIn, Just netOut] -> pure (netIn, netOut) --- _ -> pure (0, 0) --- where --- lastline ls | length ls == 4 = last ls -- ensures we read the fourth line --- | otherwise = [] - --- readProcNetInOut = do --- ipexts0 <- words <$> lastline <$> lines <$> readFile (pathProcNet pid) --- let ipexts1 = map (\i -> readMaybe i :: Maybe Integer) ipexts0 --- return $ --- if length ipexts1 >= 9 -- enough fields available --- then mkCounters [("IpExt:InOctets", ipexts1 !! 7), ("IpExt:OutOctets", ipexts1 !! 8)] --- else [] --- where --- lastline ls | length ls == 4 = last ls -- ensures we read the fourth line --- | otherwise = [] --- mkCounters = catMaybes . map (\(n,c) -> mkCounter n c) --- mkCounter _n Nothing = Nothing --- mkCounter n (Just i) = Just (Counter NetCounter (pack n) (Bytes $ fromInteger i)) +#endif -- | TODO we have to expand the |readMemStats| function -- to read full data from |proc| diff --git a/trace-resources/trace-resources.cabal b/trace-resources/trace-resources.cabal index fe3e6deac01..a1b35f2ce80 100644 --- a/trace-resources/trace-resources.cabal +++ b/trace-resources/trace-resources.cabal @@ -17,6 +17,11 @@ extra-source-files: include/os-support-darwin.h extra-doc-files: CHANGELOG.md README.md +Flag with-netstat + Description: Enable netstat values in Linux resource traces (potentially expensive) + Manual: True + Default: False + common project-config default-language: Haskell2010 @@ -28,6 +33,7 @@ library Cardano.Logging.Resources.Dummy default-extensions: OverloadedStrings + CPP build-depends: base >=4.12 && <5 , trace-dispatcher , text @@ -48,6 +54,9 @@ library -Wmissing-export-lists -Wno-incomplete-patterns + if flag(with-netstat) + CPP-options: -DWITH_NETSTAT + if os(linux) exposed-modules: Cardano.Logging.Resources.Linux if os(windows)