/
Driver.hs
318 lines (282 loc) · 12 KB
/
Driver.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns -Wno-name-shadowing #-}
module Cardano.Analysis.Driver
( AnalysisCmdError
, renderAnalysisCmdError
, runAnalysisCommand
) where
import Prelude (String, error)
import Cardano.Prelude
import Control.Arrow ((&&&))
import Control.Monad.Trans.Except.Extra (firstExceptT, newExceptT)
import Control.Concurrent.Async (mapConcurrently)
import Data.Aeson qualified as AE
import Data.ByteString.Lazy.Char8 qualified as LBS
import Data.Text qualified as T
import Data.Text.IO qualified as T
import System.FilePath qualified as F
import Graphics.Histogram qualified as Hist
import Graphics.Gnuplot.Frame.OptionSet qualified as Opts
import Text.Printf
import Cardano.Analysis.API
import Cardano.Analysis.BlockProp
import Cardano.Analysis.ChainFilter
import Cardano.Analysis.MachTimeline
import Cardano.Analysis.Profile
import Cardano.Unlog.Commands
import Cardano.Unlog.LogObject hiding (Text)
import Cardano.Unlog.Render
import Cardano.Unlog.SlotStats
data AnalysisCmdError
= AnalysisCmdError !Text
| RunMetaParseError !JsonRunMetafile !Text
| GenesisParseError !JsonGenesisFile !Text
| ChainFiltersParseError !JsonSelectorFile !Text
deriving Show
renderAnalysisCmdError :: AnalysisCommand -> AnalysisCmdError -> Text
renderAnalysisCmdError cmd err =
case err of
AnalysisCmdError err' -> renderError cmd err'
"Analysis command failed"
pure
RunMetaParseError (JsonRunMetafile fp) err' -> renderError cmd err'
("Benchmark run metafile parse failed: " <> T.pack fp)
pure
GenesisParseError (JsonGenesisFile fp) err' -> renderError cmd err'
("Genesis parse failed: " <> T.pack fp)
pure
ChainFiltersParseError (JsonSelectorFile fp) err' -> renderError cmd err'
("Chain filter list parse failed: " <> T.pack fp)
pure
where
renderError :: AnalysisCommand -> a -> Text -> (a -> [Text]) -> Text
renderError cmd' cmdErr desc renderer =
mconcat [ desc, ": "
, renderAnalysisCommand cmd'
, " Error: "
, mconcat (renderer cmdErr)
]
--
-- Analysis command dispatch
--
runAnalysisCommand :: AnalysisCommand -> ExceptT AnalysisCmdError IO ()
runAnalysisCommand (MachineTimelineCmd genesisFile metaFile mChFiltersFile logfiles oFiles) = do
progress "genesis" (Q $ unJsonGenesisFile genesisFile)
progress "meta" (Q $ unJsonRunMetafile metaFile)
chainInfo <-
ChainInfo
<$> firstExceptT (RunMetaParseError metaFile . T.pack)
(newExceptT $
AE.eitherDecode @Profile <$> LBS.readFile (unJsonRunMetafile metaFile))
<*> firstExceptT (GenesisParseError genesisFile . T.pack)
(newExceptT $
AE.eitherDecode @Genesis <$> LBS.readFile (unJsonGenesisFile genesisFile))
liftIO $ LBS.putStrLn (AE.encode chainInfo)
chFilters <- fmap (fromMaybe []) $
forM mChFiltersFile $
\jf@(JsonSelectorFile f) -> do
firstExceptT (ChainFiltersParseError jf . T.pack)
(newExceptT $
AE.eitherDecode @[ChainFilter] <$> LBS.readFile f)
firstExceptT AnalysisCmdError $
runMachineTimeline chainInfo logfiles chFilters oFiles
runAnalysisCommand (BlockPropagationCmd genesisFile metaFile mChFiltersFile logfiles oFiles) = do
progress "genesis" (Q $ unJsonGenesisFile genesisFile)
progress "meta" (Q $ unJsonRunMetafile metaFile)
chainInfo <-
ChainInfo
<$> firstExceptT (RunMetaParseError metaFile . T.pack)
(newExceptT $
AE.eitherDecode @Profile <$> LBS.readFile (unJsonRunMetafile metaFile))
<*> firstExceptT (GenesisParseError genesisFile . T.pack)
(newExceptT $
AE.eitherDecode @Genesis <$> LBS.readFile (unJsonGenesisFile genesisFile))
liftIO $ LBS.putStrLn (AE.encode chainInfo)
chFilters <- fmap (fromMaybe []) $
forM mChFiltersFile $
\jf@(JsonSelectorFile f) -> do
firstExceptT (ChainFiltersParseError jf . T.pack)
(newExceptT $
AE.eitherDecode @[ChainFilter] <$> LBS.readFile f)
firstExceptT AnalysisCmdError $
runBlockPropagation chainInfo chFilters logfiles oFiles
runAnalysisCommand SubstringKeysCmd =
liftIO $ mapM_ putStrLn logObjectStreamInterpreterKeys
runAnalysisCommand (ChainInfoCmd genesisFile metaFile) = do
progress "genesis" (Q $ unJsonGenesisFile genesisFile)
progress "meta" (Q $ unJsonRunMetafile metaFile)
chainInfo <-
ChainInfo
<$> firstExceptT (RunMetaParseError metaFile . T.pack)
(newExceptT $
AE.eitherDecode @Profile <$> LBS.readFile (unJsonRunMetafile metaFile))
<*> firstExceptT (GenesisParseError genesisFile . T.pack)
(newExceptT $
AE.eitherDecode @Genesis <$> LBS.readFile (unJsonGenesisFile genesisFile))
liftIO $ LBS.putStrLn (AE.encode chainInfo)
runBlockPropagation ::
ChainInfo -> [ChainFilter] -> [JsonLogfile] -> BlockPropagationOutputFiles -> ExceptT Text IO ()
runBlockPropagation cInfo chConds logfiles BlockPropagationOutputFiles{..} = do
liftIO $ do
progress "inputs" (L $ unJsonLogfile <$> logfiles)
-- 0. Recover LogObjects
objLists :: [(JsonLogfile, [LogObject])] <- flip mapConcurrently logfiles
(joinT . (pure &&& readLogObjectStream))
forM_ bpofLogObjects . const $ do
flip mapConcurrently objLists $
\(JsonLogfile f, objs) -> do
dumpLOStream objs
(JsonOutputFile $ F.dropExtension f <> ".logobjects.json")
blockPropagation <- blockProp cInfo chConds objLists
forM_ bpofTimelinePretty $
\(TextOutputFile f) ->
withFile f WriteMode $ \hnd -> do
progress "pretty-timeline" (Q f)
hPutStrLn hnd . T.pack $ printf "--- input: %s" f
mapM_ (T.hPutStrLn hnd)
(renderDistributions (cProfile cInfo) RenderPretty blockPropagation)
mapM_ (T.hPutStrLn hnd)
(renderTimeline (cProfile cInfo) $ bpChainBlockEvents blockPropagation)
forM_ bpofAnalysis $
\(JsonOutputFile f) ->
withFile f WriteMode $ \hnd -> do
progress "analysis" (Q f)
LBS.hPutStrLn hnd (AE.encode blockPropagation)
where
joinT :: (IO a, IO b) -> IO (a, b)
joinT (a, b) = (,) <$> a <*> b
data F
= R String
| Q String
| L [String]
progress :: MonadIO m => String -> F -> m ()
progress key = putStrLn . T.pack . \case
R x -> printf "{ \"%s\": %s }" key x
Q x -> printf "{ \"%s\": \"%s\" }" key x
L xs -> printf "{ \"%s\": \"%s\" }" key (intercalate "\", \"" xs)
runMachineTimeline ::
ChainInfo -> [JsonLogfile] -> [ChainFilter] -> MachineTimelineOutputFiles -> ExceptT Text IO ()
runMachineTimeline chainInfo logfiles chFilters MachineTimelineOutputFiles{..} = do
liftIO $ do
-- 0. Recover LogObjects
progress "inputs" (L $ unJsonLogfile <$> logfiles)
objs :: [LogObject] <- concat <$> mapM readLogObjectStream logfiles
forM_ mtofLogObjects
(dumpLOStream objs)
-- 1. Derive the basic scalars and vectors
let (,) runStats noisySlotStats = timelineFromLogObjects chainInfo objs
forM_ mtofSlotStats $
\(JsonOutputFile f) -> do
progress "raw-slots" (Q f)
withFile f WriteMode $ \hnd ->
forM_ noisySlotStats $ LBS.hPutStrLn hnd . AE.encode
-- 2. Reprocess the slot stats
let slotStats = filterSlotStats chFilters noisySlotStats
let rawSlotFirst = (head noisySlotStats <&> slSlot) & fromMaybe 0
rawSlotLast = (lastMay noisySlotStats <&> slSlot) & fromMaybe 0
anaSlotFirst = (head slotStats <&> slSlot) & fromMaybe 0
anaSlotLast = (lastMay slotStats <&> slSlot) & fromMaybe 0
liftIO . LBS.putStrLn . AE.encode $
DataDomain
rawSlotFirst rawSlotLast
anaSlotFirst anaSlotLast
-- 3. Derive the timeline
let drvVectors0, _drvVectors1 :: [DerivedSlot]
(,) drvVectors0 _drvVectors1 = computeDerivedVectors slotStats
timeline :: MachTimeline
timeline = slotStatsMachTimeline chainInfo slotStats
timelineOutput :: LBS.ByteString
timelineOutput = AE.encode timeline
-- 4. Render various outputs
forM_ mtofTimelinePretty
(renderPrettyMachTimeline slotStats timeline)
forM_ mtofStatsCsv
(renderExportStats runStats timeline)
forM_ mtofTimelineCsv
(renderExportTimeline slotStats)
forM_ mtofDerivedVectors0Csv
(renderDerivedSlots drvVectors0)
forM_ mtofHistogram
(renderHistogram "CPU usage spans over 85%" "Span length"
(toList $ sort $ sSpanLensCPU85 timeline))
flip (maybe $ LBS.putStrLn timelineOutput) mtofAnalysis $
\case
JsonOutputFile f ->
withFile f WriteMode $ \hnd -> do
progress "analysis" (Q f)
LBS.hPutStrLn hnd timelineOutput
where
-- | Use the supplied chain filters.
--
-- The idea is that the initial part is useless until the node actually starts
-- to interact with the blockchain, so we drop all slots until they start
-- getting non-zero chain density reported.
--
-- On the trailing part, we drop everything since the last leadership check.
filterSlotStats :: [ChainFilter] -> [SlotStats] -> [SlotStats]
filterSlotStats chFilters =
filter (\x -> all (testSlotStats p x) slotFilters)
where
slotFilters :: [SlotCond]
slotFilters = catSlotFilters chFilters
p = cProfile chainInfo
renderHistogram :: Integral a
=> String -> String -> [a] -> OutputFile -> IO ()
renderHistogram desc ylab xs (OutputFile f) =
Hist.plotAdv f opts hist >> pure ()
where
hist = Hist.histogram Hist.binFreedmanDiaconis $ fromIntegral <$> xs
opts = Opts.title desc $ Opts.yLabel ylab $ Opts.xLabel "Population" $
Hist.defOpts hist
renderPrettyMachTimeline ::
[SlotStats] -> MachTimeline -> TextOutputFile -> IO ()
renderPrettyMachTimeline xs s (TextOutputFile f) =
withFile f WriteMode $ \hnd -> do
progress "pretty-timeline" (Q f)
hPutStrLn hnd . T.pack $
printf "--- input: %s" f
mapM_ (T.hPutStrLn hnd)
(renderDistributions p RenderPretty s)
mapM_ (T.hPutStrLn hnd)
(renderTimeline p xs)
renderExportStats :: RunScalars -> MachTimeline -> CsvOutputFile -> IO ()
renderExportStats rs s (CsvOutputFile f) =
withFile f WriteMode $
\h -> do
progress "csv-stats" (Q f)
mapM_ (hPutStrLn h)
(renderDistributions p RenderCsv s)
mapM_ (hPutStrLn h) $
renderChainInfoExport chainInfo
<>
renderRunScalars rs
renderExportTimeline :: [SlotStats] -> CsvOutputFile -> IO ()
renderExportTimeline _xs (CsvOutputFile _o) =
error "Timeline export is not supported."
-- withFile o WriteMode $
-- mapM_ (T.hPutStrLn hnd) (renderTimeline xs)
renderDerivedSlots :: [DerivedSlot] -> CsvOutputFile -> IO ()
renderDerivedSlots slots (CsvOutputFile f) = do
withFile f WriteMode $ \hnd -> do
progress "derived-slots" (Q f)
hPutStrLn hnd derivedSlotsHeader
forM_ slots $
hPutStrLn hnd . renderDerivedSlot
dumpLOStream :: [LogObject] -> JsonOutputFile -> IO ()
dumpLOStream objs (JsonOutputFile f) = do
progress "logobjects" (Q f)
withFile f WriteMode $ \hnd -> do
forM_ objs $ LBS.hPutStrLn hnd . AE.encode
renderRunScalars :: RunScalars -> [Text]
renderRunScalars RunScalars{..} =
T.intercalate "," <$>
[[ "Run time", maybe "---" show rsElapsed ]
,[ "Txs submitted", maybe "---" show rsSubmitted ]
,[ "Submission TPS", maybe "---" (show . sum) rsThreadwiseTps]
]