showStats

This commit is contained in:
crumbtoo
2023-11-29 17:53:14 -07:00
parent 066f883178
commit 04c55a9968

View File

@@ -13,8 +13,10 @@ module GM
import Data.Default.Class import Data.Default.Class
import Data.List (mapAccumL, intersperse) import Data.List (mapAccumL, intersperse)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Tuple (swap)
import Lens.Micro import Lens.Micro
import Lens.Micro.TH import Lens.Micro.TH
import Text.Printf
import Text.PrettyPrint hiding ((<>)) import Text.PrettyPrint hiding ((<>))
import Text.PrettyPrint.HughesPJ (maybeParens) import Text.PrettyPrint.HughesPJ (maybeParens)
import Data.Heap import Data.Heap
@@ -52,15 +54,16 @@ data Node = NNum Int
deriving Show deriving Show
data Stats = Stats data Stats = Stats
{ _stsReductions :: Int { _stsReductions :: Int
, _stsAllocations :: Int , _stsPrimReductions :: Int
, _stsDereferences :: Int , _stsAllocations :: Int
, _stsGCCycles :: Int , _stsDereferences :: Int
, _stsGCCycles :: Int
} }
deriving Show deriving Show
instance Default Stats where instance Default Stats where
def = Stats 0 0 0 0 def = Stats 0 0 0 0 0
-- TODO: _gmGlobals should not have a setter -- TODO: _gmGlobals should not have a setter
makeLenses ''GmState makeLenses ''GmState
@@ -235,6 +238,20 @@ qquotes d = "`" <> d <> "'"
showResults :: [GmState] -> String showResults :: [GmState] -> String
showResults st = undefined showResults st = undefined
showStats :: Stats -> Doc
showStats sts = "==== Stats ============" $$ stats
where
info = nest pprTabstop
stats = text $ printf
"Reductions : %5d\n\
\Prim Reductions : %5d\n\
\Allocations : %5d\n\
\GC Cycles : %5d"
(sts ^. stsReductions)
(sts ^. stsPrimReductions)
(sts ^. stsAllocations)
(sts ^. stsGCCycles)
showState :: GmState -> Doc showState :: GmState -> Doc
showState st = vcat showState st = vcat
[ "==== GmState " <> int stnum <> " ====" [ "==== GmState " <> int stnum <> " ===="
@@ -274,10 +291,10 @@ showStack st = vcat $ uncurry showEntry <$> si
where pad = text (replicate (maxWidth - digitalWidth n) ' ') where pad = text (replicate (maxWidth - digitalWidth n) ' ')
showEntry :: Int -> Addr -> Doc showEntry :: Int -> Addr -> Doc
showEntry n a = showIndex n <> showNodeAt h a showEntry n a = showIndex n <> showNodeAt st a
showHeap :: GmState -> Doc showHeap :: GmState -> Doc
showHeap st = vcat $ uncurry showEntry <$> assocs h showHeap st = vcat $ showEntry <$> addresses h
where where
digitalWidth = length . show digitalWidth = length . show
maxWidth = digitalWidth $ maximum (addresses h) maxWidth = digitalWidth $ maximum (addresses h)
@@ -286,27 +303,22 @@ showHeap st = vcat $ uncurry showEntry <$> assocs h
h = st ^. gmHeap h = st ^. gmHeap
showEntry :: Addr -> Node -> Doc showEntry :: Addr -> Doc
showEntry a n = showAddr a <> showNode h n showEntry a = showAddr a <> showNodeAt st a
showNodeAt :: GmHeap -> Addr -> Doc showNodeAt :: GmState -> Addr -> Doc
showNodeAt = showNodeAtP 0 showNodeAt = showNodeAtP 0
showNodeAtP :: Int -> GmHeap -> Addr -> Doc showNodeAtP :: Int -> GmState -> Addr -> Doc
showNodeAtP p h a = case hLookup a h of showNodeAtP p st a = case hLookup a h of
Just n -> showNodeP p h n Just (NNum n) -> int n <> "#"
Nothing -> "<invalid address>" Just (NGlobal d c) -> text name
where
showNode :: GmHeap -> Node -> Doc g = st ^. gmEnv
showNode = showNodeP 0 name = fromMaybe "<unknown>" $ lookup a (swap <$> g)
Just (NAp f x) -> showNodeAtP (p+1) st f <+> showNodeAtP (p+1) st x
showNodeP :: Int -> GmHeap -> Node -> Doc Nothing -> "<invalid address>"
showNodeP _ h (NNum n) = int n <> "#" where h = st ^. gmHeap
showNodeP p h (NGlobal d c) = precp $ "NGlobal" <+> int d
where precp = maybeParens (p > 0)
showNodeP p h (NAp f x) = precp $ showNodeAtP (p+1) h f
<+> showNodeAtP (p+1) h x
where precp = maybeParens (p > 0)
showSc :: GmState -> (Name, Addr) -> Doc showSc :: GmState -> (Name, Addr) -> Doc
showSc st (k,a) = "Supercomb " <> qquotes (text k) <> colon showSc st (k,a) = "Supercomb " <> qquotes (text k) <> colon
@@ -330,5 +342,7 @@ test = GmState c s h'' g sts
(h,a0) = alloc mempty $ NGlobal 2 [Push 2,Push 3,MkAp,Slide 2,Unwind] (h,a0) = alloc mempty $ NGlobal 2 [Push 2,Push 3,MkAp,Slide 2,Unwind]
(h',a1) = alloc h $ NNum 4 (h',a1) = alloc h $ NNum 4
(h'',a2) = alloc h' $ NAp a0 a1 (h'',a2) = alloc h' $ NAp a0 a1
g = [] g = [ ("f", a0)
]
sts = def sts = def