Skip to content

Commit

Permalink
trace-resources: version 0.2.1.0; conditional compilation for netstat…
Browse files Browse the repository at this point in the history
… values
  • Loading branch information
mgmeier authored and jutaro committed Nov 22, 2023
1 parent 232f807 commit fb6a311
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
8 changes: 7 additions & 1 deletion trace-resources/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
43 changes: 17 additions & 26 deletions trace-resources/src/Cardano/Logging/Resources/Linux.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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|
Expand Down
9 changes: 9 additions & 0 deletions trace-resources/trace-resources.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,6 +33,7 @@ library
Cardano.Logging.Resources.Dummy

default-extensions: OverloadedStrings
CPP
build-depends: base >=4.12 && <5
, trace-dispatcher
, text
Expand All @@ -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)
Expand Down

0 comments on commit fb6a311

Please sign in to comment.