Skip to content

Commit 5ba7584

Browse files
committed
Bring in readFile fixes from new tracing
Bring in the readFile fixes from new tracing subsystem.
1 parent b514257 commit 5ba7584

File tree

1 file changed

+27
-16
lines changed
  • iohk-monitoring/src/Cardano/BM/Counters

1 file changed

+27
-16
lines changed

iohk-monitoring/src/Cardano/BM/Counters/Linux.lhs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ module Cardano.BM.Counters.Linux
1414
1515
#ifdef ENABLE_OBSERVABLES
1616
import Data.Foldable (foldrM)
17-
import Data.Maybe (catMaybes)
18-
import Data.Text (Text, pack)
17+
import Data.Maybe (catMaybes, fromMaybe)
18+
import Data.Text (Text)
1919
import qualified GHC.Stats as GhcStats
2020
import System.FilePath.Posix ((</>))
2121
import System.Posix.Files (getFileStatus,fileMode,ownerReadMode,
2222
intersectFileModes)
2323
import System.Posix.Process (getProcessID)
2424
import System.Posix.Types (ProcessID)
25-
import Text.Read (readMaybe)
25+
import qualified Data.Text as T
26+
import qualified Data.Text.IO as T (readFile)
27+
import qualified Data.Text.Read as T (decimal)
2628
#endif
2729
2830
#ifdef ENABLE_OBSERVABLES
@@ -131,8 +133,8 @@ readProcList fp = do
131133
fs <- getFileStatus fp
132134
if readable fs
133135
then do
134-
cs <- readFile fp
135-
return $ map (\s -> maybe 0 id $ (readMaybe s :: Maybe Integer)) (words cs)
136+
cs <- T.readFile fp
137+
return $ map (fromMaybe 0 . readMaybeText) (T.words cs)
136138
else
137139
return []
138140
where
@@ -544,17 +546,26 @@ IpExt: 0 0 20053 8977 2437 23 3163525943 196480057 2426648 1491754 394285 5523 0
544546
#ifdef ENABLE_OBSERVABLES
545547
readProcNet :: ProcessID -> IO [Counter]
546548
readProcNet pid = do
547-
ipexts0 <- words <$> lastline <$> lines <$> readFile (pathProcNet pid)
548-
let ipexts1 = map (\i -> readMaybe i :: Maybe Integer) ipexts0
549-
return $
550-
if length ipexts1 >= 9 -- enough fields available
551-
then mkCounters [("IpExt:InOctets", ipexts1 !! 7), ("IpExt:OutOctets", ipexts1 !! 8)]
552-
else []
549+
fields <- T.words . fourthLine . T.lines <$> T.readFile (pathProcNet pid)
550+
case -- We're only interested in 'InOctets' & 'OutOctets':
551+
fmap readMaybeText . take 2 . drop 7 $ fields of
552+
[Just netIn, Just netOut] -> return $ [ Counter NetCounter "IpExt:InOctets" (Bytes netIn)
553+
, Counter NetCounter "IpExt:OutOctets" (Bytes netOut) ]
554+
_ -> pure []
553555
where
554-
lastline ls | length ls == 4 = last ls -- ensures we read the fourth line
555-
| otherwise = []
556-
mkCounters = catMaybes . map (\(n,c) -> mkCounter n c)
557-
mkCounter _n Nothing = Nothing
558-
mkCounter n (Just i) = Just (Counter NetCounter (pack n) (Bytes $ fromInteger i))
556+
-- Assumption: 'IpExt:' values are on the fourth line of how the kernel displays the buffer
557+
fourthLine ls = case drop 3 ls of
558+
l:_ -> l
559+
_ -> T.empty
560+
#endif
561+
\end{code}
562+
563+
\begin{code}
564+
#ifdef ENABLE_OBSERVABLES
565+
readMaybeText :: Integral a => T.Text -> Maybe a
566+
readMaybeText t =
567+
case T.decimal t of
568+
Right (v, _) -> Just v
569+
_ -> Nothing
559570
#endif
560571
\end{code}

0 commit comments

Comments
 (0)