@@ -37,9 +37,24 @@ import Unison.Util.Pretty as P
3737newtype ProfTrie k a = ProfT (Map k (a , ProfTrie k a ))
3838 deriving (Functor )
3939
40+ trimEmpty :: (Ord k , Eq a , Num a ) => ProfTrie k a -> ProfTrie k a
41+ trimEmpty (ProfT m) = case M. traverseMaybeWithKey f m of
42+ Identity m -> ProfT m
43+ where
44+ f _ (a, trimEmpty -> sub@ (ProfT m))
45+ | a == 0 , null m = pure Nothing
46+ | otherwise = pure $ Just (a, sub)
47+
48+ demux ::
49+ (Ord k , Eq a , Eq b , Num a , Num b ) =>
50+ ProfTrie k (a , b ) ->
51+ (ProfTrie k a , ProfTrie k b )
52+ demux tr = (trimEmpty $ fst <$> tr, trimEmpty $ snd <$> tr)
53+
4054-- A profile pairs the above arbitrary key based profile trie with a
4155-- decoding of the integers to references and a total sample count.
42- data Profile k = Prof ! Int ! (ProfTrie k Int ) ! (Map k Reference )
56+ data Profile k
57+ = Prof ! (Int , Int ) ! (ProfTrie k (Int , Int )) ! (Map k Reference )
4358
4459-- Abstracts over the exact key type used in a profile.
4560data SomeProfile = forall k . (Ord k ) => SomeProf (Profile k )
@@ -48,28 +63,51 @@ data ProfileSpec = NoProf | MiniProf | FullProf String
4863 deriving (Eq , Ord , Show )
4964
5065emptyProfile :: Profile k
51- emptyProfile = Prof 0 (ProfT M. empty) M. empty
66+ emptyProfile = Prof zero (ProfT M. empty) M. empty
5267
53- -- Creates a singleton profile trie from a path.
54- singlePath :: (Ord k ) => [k ] -> (Int , ProfTrie k Int )
55- singlePath [] = (1 , ProfT M. empty)
56- singlePath (i : is) = (0 ,) . ProfT $! M. singleton i (singlePath is)
68+ zero :: (Int , Int )
69+ zero = (0 , 0 )
70+
71+ inc :: Bool -> (Int , Int ) -> (Int , Int )
72+ inc b (m, n) = pair (m + 1 ) (if b then n + 1 else n)
73+ where
74+ pair ! x ! y = (x, y)
5775
58- addPath0 :: (Ord k ) => [k ] -> (Int , ProfTrie k Int ) -> (Int , ProfTrie k Int )
59- addPath0 [] (m, p) = (,p) $! m + 1
60- addPath0 (i : is) (m, ProfT p) = (m,) . ProfT $! M. alter f i p
76+ -- Creates a singleton profile trie from a path.
77+ singlePath ::
78+ (Ord k ) =>
79+ Bool ->
80+ [k ] ->
81+ ((Int , Int ), ProfTrie k (Int , Int ))
82+ singlePath b [] = (inc b zero, ProfT M. empty)
83+ singlePath b (i : is) =
84+ (zero,) . ProfT $! M. singleton i (singlePath b is)
85+
86+ addPath0 ::
87+ (Ord k ) =>
88+ Bool ->
89+ [k ] ->
90+ ((Int , Int ), ProfTrie k (Int , Int )) ->
91+ ((Int , Int ), ProfTrie k (Int , Int ))
92+ addPath0 b [] (t, p) = (,p) $! inc b t
93+ addPath0 b (i : is) (m, ProfT p) = (m,) . ProfT $! M. alter f i p
6194 where
62- f Nothing = Just $ singlePath is
63- f (Just q) = Just $ addPath0 is q
95+ f Nothing = Just $ singlePath b is
96+ f (Just q) = Just $ addPath0 b is q
6497
6598-- Adds a path to a profile trie, incrementing the count for the given
6699-- path.
67- addPath :: (Ord k ) => [k ] -> ProfTrie k Int -> ProfTrie k Int
68- addPath [] p = p
69- addPath (i : is) (ProfT m) = ProfT $ M. alter f i m
100+ addPath ::
101+ (Ord k ) =>
102+ Bool ->
103+ [k ] ->
104+ ProfTrie k (Int , Int ) ->
105+ ProfTrie k (Int , Int )
106+ addPath _ [] p = p
107+ addPath b (i : is) (ProfT m) = ProfT $ M. alter f i m
70108 where
71- f Nothing = Just $ singlePath is
72- f (Just q) = Just $ addPath0 is q
109+ f Nothing = Just $ singlePath b is
110+ f (Just q) = Just $ addPath0 b is q
73111
74112data AggInfo k = Ag
75113 { -- inherited sample count
@@ -116,6 +154,7 @@ prune keep (ProfT m) = case M.traverseMaybeWithKey (prune0 keep) m of
116154topN :: (Ord k ) => Int -> Map k Int -> [(k , Int )]
117155topN n0 = M. foldlWithKey (ins n0) []
118156 where
157+ ins _ pss _ 0 = pss
119158 ins 0 _ _ _ = []
120159 ins _ [] k i = [(k, i)]
121160 ins n pss@ ((k1, j) : ps) k0 i
@@ -193,9 +232,15 @@ dispProfEntry ppe misc refs (k, ks) (inh, self) =
193232 where
194233 ind = fromIntegral $ length ks
195234
196- dispFunc :: PrettyPrintEnv -> Reference -> Pretty ColorText
197- dispFunc ppe =
198- syntaxToColor . prettyHashQualified . termName ppe . Ref
235+ dispFunc ::
236+ PrettyPrintEnv ->
237+ Map Reference (Pretty ColorText ) ->
238+ Reference ->
239+ Pretty ColorText
240+ dispFunc ppe misc r
241+ | Just pr <- M. lookup r misc = pr
242+ | otherwise =
243+ syntaxToColor . prettyHashQualified . termName ppe $ Ref r
199244
200245dispKey ::
201246 (Ord k ) =>
@@ -205,9 +250,7 @@ dispKey ::
205250 k ->
206251 Pretty ColorText
207252dispKey ppe misc refs k = case M. lookup k refs of
208- Just r
209- | Just pr <- M. lookup r misc -> pr
210- | otherwise -> dispFunc ppe r
253+ Just r -> dispFunc ppe misc r
211254 Nothing -> " <unknown>"
212255
213256dispProfTrie ::
@@ -223,10 +266,11 @@ dispProfTrie ppe misc refs ag =
223266dispTopEntry ::
224267 (Ord k ) =>
225268 PrettyPrintEnv ->
269+ Map Reference (Pretty ColorText ) ->
226270 Map k Reference ->
227271 (k , Double ) ->
228272 Pretty ColorText
229- dispTopEntry ppe refs (k, frac) =
273+ dispTopEntry ppe misc refs (k, frac) =
230274 mconcat
231275 [ P. indentN 3 . fromString $ showPercent frac,
232276 P. indentN 4 dr,
@@ -235,16 +279,24 @@ dispTopEntry ppe refs (k, frac) =
235279 where
236280 dr :: Pretty ColorText
237281 dr
238- | Just r <- M. lookup k refs = dispFunc ppe r
282+ | Just r <- M. lookup k refs = dispFunc ppe misc r
239283 | otherwise = " <unknown>"
240284
241285dispTop ::
242286 (Ord k ) =>
243287 PrettyPrintEnv ->
288+ Map Reference (Pretty ColorText ) ->
244289 Map k Reference ->
245290 [(k , Double )] ->
246291 Pretty ColorText
247- dispTop ppe refs = foldMap (dispTopEntry ppe refs)
292+ dispTop ppe misc refs = foldMap (dispTopEntry ppe misc refs)
293+
294+ overallHeader :: Pretty ColorText -> Int -> Pretty ColorText
295+ overallHeader label samps = label <> " : " <> dsamps <> newline
296+ where
297+ dsamps
298+ | samps == 1 = " 1 sample"
299+ | otherwise = fromString (show samps) <> " samples"
248300
249301profileTopHeader :: Pretty ColorText
250302profileTopHeader =
@@ -264,38 +316,64 @@ miniProfile ::
264316 Map Reference (Pretty ColorText ) ->
265317 Profile k ->
266318 Pretty ColorText
267- miniProfile ppe misc (Prof total tr refs) =
268- profileTreeHeader
269- <> dispProfTrie ppe misc refs ag
319+ miniProfile ppe misc (Prof (total, wtotal) tr refs) =
320+ P. lines
321+ [ overallHeader " Complete Profile" total,
322+ profileTreeHeader <> dispProfTrie ppe misc refs ag,
323+ " " ,
324+ if wtotal > 0
325+ then
326+ overallHeader " Post-wakeup Profile" wtotal
327+ <> newline
328+ <> profileTreeHeader
329+ <> dispProfTrie ppe misc refs agw
330+ else " Threads never missed ticks"
331+ ]
270332 where
271- ag = aggregatePruned total tr
333+ (full, wait) = demux tr
334+ ag = aggregatePruned total full
335+ agw = aggregatePruned wtotal wait
272336
273337fullProfile ::
274338 (Ord k ) =>
275339 PrettyPrintEnv ->
276340 Map Reference (Pretty ColorText ) ->
277341 Profile k ->
278- Pretty ColorText
279- fullProfile ppe misc (Prof total tr refs) =
280- profileTopHeader
281- <> dispTop ppe refs top
282- <> " \n\n "
283- <> profileTreeHeader
284- <> dispProfTrie ppe misc refs ag
342+ (Pretty ColorText , Pretty ColorText )
343+ fullProfile ppe misc (Prof (total, wtotal) tr0 refs) =
344+ ( make " Complete Profile" total comp,
345+ make " Post-wakeup Profile" wtotal wait
346+ )
285347 where
286- (top, ag) = aggregate total tr
348+ (comp, wait) = demux tr0
349+
350+ make label tot tr =
351+ overallHeader label tot
352+ <> newline
353+ <> profileTopHeader
354+ <> dispTop ppe misc refs top
355+ <> newline
356+ <> newline
357+ <> profileTreeHeader
358+ <> dispProfTrie ppe misc refs ag
359+ where
360+ (top, ag) = aggregate tot tr
287361
288362foldedProfile ::
289363 (Ord k ) =>
290364 PrettyPrintEnv ->
291365 Map Reference (Pretty ColorText ) ->
292366 Profile k ->
293- String
367+ ( String , String )
294368foldedProfile ppe misc (Prof _ tr refs) =
295- toPlain 0 $ foldMapTrie f tr
369+ ( toPlain 0 $ foldMapTrie f comp,
370+ toPlain 0 $ foldMapTrie f wake
371+ )
296372 where
297373 dk = dispKey ppe misc refs
298374
375+ (comp, wake) = demux tr
376+
299377 f (k, ks) n =
300378 mconcat
301379 [ foldl (\ tx k -> dk k <> " ;" <> tx) (dk k) ks,
0 commit comments