Skip to content

Commit b83a455

Browse files
goodlyrottenapplegithub-actions
andauthored
Add count-aborts binary (#3911)
Add `count-aborts` binary which can count aborts via `count-aborts file_1 ... file_n`, producing a simple table. The files expected as input to this tool are produced by the booster via `--log-format json --log-file <file>`. --------- Co-authored-by: github-actions <[email protected]>
1 parent 14a27a9 commit b83a455

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

dev-tools/count-aborts/Main.hs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{- | Stand-alone parser executable for testing and profiling
2+
3+
Copyright : (c) Runtime Verification, 2022
4+
License : BSD-3-Clause
5+
-}
6+
module Main (
7+
main,
8+
) where
9+
10+
import Control.Monad (foldM, forM_)
11+
import Data.Aeson (decode)
12+
import Data.ByteString.Lazy.Char8 qualified as BS
13+
import Data.List (foldl', sortOn)
14+
import Data.Map qualified as Map
15+
import Data.Maybe (fromMaybe)
16+
import Data.Ord (Down (..))
17+
import Data.Text (unpack)
18+
import System.Environment (getArgs)
19+
import Types
20+
21+
{- | Utility for parsing and extracting information from context logs,
22+
produced by running the booster binary with `--log-format json --log-file <path>`.
23+
This tool collects the number of aborts for each rewrite rule and displays the informantion in a table.
24+
Call via `count-aborts <path_1> ... <path_n>`
25+
-}
26+
main :: IO ()
27+
main =
28+
getArgs >>= \case
29+
files
30+
| "-h" `elem` files || "--help" `elem` files -> do
31+
putStrLn
32+
"This tool parses the JSON contextual logs, collects the number of aborts for each rewrite rule and displays the informantion in a table."
33+
putStrLn "Call via `count-aborts <path_1> ... <path_n>`"
34+
putStrLn
35+
"To produce the correct context logs, run kore-rpc-booster with `--log-format json --log-file <file>`"
36+
| otherwise -> do
37+
let countContexts m f = foldl' (foldl' countAborts) m . map decode . BS.lines <$> BS.readFile f
38+
(counts, rIdTorLoc) <- foldM countContexts mempty files
39+
forM_ (sortOn (Down . snd) $ Map.toList counts) $ \(k, v) -> do
40+
let (rType, rLoc) = fromMaybe ("-", "-") $ Map.lookup k rIdTorLoc
41+
putStrLn $ unpack rType <> " " <> unpack k <> " | " <> unpack rLoc <> " | " <> show v

dev-tools/count-aborts/Types.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Types (module Types) where
2+
3+
import Data.Aeson (FromJSON (..), Value (..))
4+
import Data.Aeson.Key (toString)
5+
import Data.Aeson.KeyMap (toList)
6+
import Data.Map (Map)
7+
import Data.Map qualified as Map
8+
import Data.Sequence (Seq (..))
9+
import Data.Text (Text, pack)
10+
import GHC.Generics (Generic)
11+
12+
data Context = Plain Text | Ref Text Text deriving (Eq, Ord)
13+
14+
instance FromJSON Context where
15+
parseJSON (String t) = pure $ Plain t
16+
parseJSON (Object o) | [(k, String v)] <- toList o = pure $ Ref (pack $ toString k) v
17+
parseJSON _ = fail "Invalid context"
18+
19+
data LogMessage = LogMessage
20+
{ context :: Seq Context
21+
, message :: Value
22+
}
23+
deriving (Generic, FromJSON)
24+
25+
countAborts ::
26+
(Map Text Int, Map Text (Text, Text)) -> LogMessage -> (Map Text Int, Map Text (Text, Text))
27+
countAborts maps@(countMap, ruleMap) LogMessage{context, message} = case context of
28+
(_ :|> Ref "rewrite" ruleId :|> Plain "match" :|> Plain "abort") -> increment ruleId
29+
(_ :|> Ref "rewrite" ruleId :|> Plain "abort") -> increment ruleId
30+
(_ :|> Ref "function" ruleId :|> Plain "failure" :|> Plain "break") -> increment ruleId
31+
(_ :|> Ref "simplification" ruleId :|> Plain "failure" :|> Plain "break") -> increment ruleId
32+
(_ :|> Ref "function" ruleId :|> Plain "match" :|> Plain "failure" :|> Plain "break") -> increment ruleId
33+
(_ :|> Ref "simplification" ruleId :|> Plain "match" :|> Plain "failure" :|> Plain "break") -> increment ruleId
34+
(_ :|> Ref "rewrite" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("rewrite", ruleLoc) ruleMap)
35+
(_ :|> Ref "function" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("function", ruleLoc) ruleMap)
36+
(_ :|> Ref "simplification" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("simplification", ruleLoc) ruleMap)
37+
_ -> maps
38+
where
39+
increment rid = (Map.alter (maybe (Just 1) (Just . (+ 1))) rid countMap, ruleMap)

dev-tools/package.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ executables:
8989
- transformers
9090
ghc-options:
9191
- -rtsopts
92+
count-aborts:
93+
source-dirs: count-aborts
94+
main: Main.hs
95+
dependencies:
96+
- aeson
97+
- base
98+
- bytestring
99+
- containers
100+
- directory
101+
- filepath
102+
- text
103+
ghc-options:
104+
- -rtsopts
92105
parsetest-binary:
93106
source-dirs: parsetest-binary
94107
main: ParsetestBinary.hs

0 commit comments

Comments
 (0)