forked from maoe/tkyprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfilingReport.hs
281 lines (244 loc) · 8.89 KB
/
ProfilingReport.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
{-# LANGUAGE FlexibleInstances, RecordWildCards, OverloadedStrings, BangPatterns #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module ProfilingReport
( -- * Parsers for profiling reports
profilingReport
, profilingReportI
-- * Parsers for sub-parts of the report
, timestamp
, title
, commandLine
, totalTime
, totalAlloc
, hotCostCentres
, costCentres
-- * Data types
, ProfilingReport(..)
, Timestamp
, CommandLine
, TotalTime(..)
, TotalAlloc(..)
, BriefCostCentre(..)
, CostCentre(..)
-- * Re-exported modules
, module Data.Tree
) where
import Control.Applicative hiding (many)
import Control.Monad.Catch (MonadThrow(..))
import Data.Aeson
import Data.Attoparsec.Char8 as A8
import Data.Conduit.Attoparsec (sinkParser)
import Data.ByteString (ByteString)
import Data.Conduit
import Data.Foldable (asum, foldl')
import Data.Time (UTCTime(..), TimeOfDay(..), timeOfDayToTime, fromGregorian)
import Data.Tree (Tree(..), Forest)
import Data.Tree.Zipper (TreePos, Full)
import Prelude hiding (takeWhile)
import qualified Data.Attoparsec as A
import qualified Data.HashMap.Strict as M
import qualified Data.Tree.Zipper as Z
import qualified Data.Vector as V
import Data.Text (Text)
import qualified Data.Text.Encoding as T
data ProfilingReport = ProfilingReport
{ reportTimestamp :: Timestamp
, reportCommandLine :: CommandLine
, reportTotalTime :: TotalTime
, reportTotalAlloc :: TotalAlloc
, reportHotCostCentres :: [BriefCostCentre]
, reportCostCentres :: Tree CostCentre
} deriving Show
type Timestamp = UTCTime
type CommandLine = Text
data TotalTime = TotalTime
{ totalSecs :: Double
, totalTicks :: Integer
, resolution :: Integer
, processors :: Integer
} deriving Show
newtype TotalAlloc = TotalAlloc
{ totalAllocBytes :: Integer
} deriving Show
data BriefCostCentre = BriefCostCentre
{ briefCostCentreName :: Text
, briefCostCentreModule :: Text
, briefCostCentreTime :: Double
, briefCostCentreAlloc :: Double
} deriving Show
data CostCentre = CostCentre
{ costCentreName :: Text
, costCentreModule :: Text
, costCentreNo :: Integer
, costCentreEntries :: Integer
, individualTime :: Double
, individualAlloc :: Double
, inheritedTime :: Double
, inheritedAlloc :: Double
} deriving Show
profilingReportI :: MonadThrow m => Sink ByteString m ProfilingReport
profilingReportI = sinkParser profilingReport
profilingReport :: Parser ProfilingReport
profilingReport = spaces >>
ProfilingReport <$> timestamp
<* title <* spaces
<*> commandLine <* spaces
<*> totalTime <* spaces
<*> totalAlloc <* spaces
<*> hotCostCentres <* spaces
<*> costCentres
timestamp :: Parser Timestamp
timestamp = do
dayOfTheWeek <* spaces
m <- month <* spaces
d <- day <* spaces
tod <- timeOfDay <* spaces
y <- year <* spaces
return UTCTime { utctDay = fromGregorian y m d
, utctDayTime = timeOfDayToTime tod }
where
year = decimal
month = toNum <$> A8.take 3
where toNum m = case m of
"Jan" -> 1; "Feb" -> 2; "Mar" -> 3; "Apr" -> 4;
"May" -> 5; "Jun" -> 6; "Jul" -> 7; "Aug" -> 8;
"Sep" -> 9; "Oct" -> 10; "Nov" -> 11; "Dec" -> 12
_ -> error "timestamp.toNum: impossible"
day = decimal
timeOfDay = TimeOfDay <$> decimal <* string ":" <*> decimal <*> pure 0
dayOfTheWeek = takeTill isSpace
title :: Parser ByteString
title = string "Time and Allocation Profiling Report (Final)"
commandLine :: Parser CommandLine
commandLine = T.decodeUtf8 <$> line
totalTime :: Parser TotalTime
totalTime = do
string "total time ="; spaces
secs <- double
string " secs"; spaces
(ticks, res, procs) <- parens $ (,,)
<$> decimal <* string " ticks @ "
<*> time <* string ", "
<*> decimal <* many1 (notChar ')')
return TotalTime { totalSecs = secs
, totalTicks = ticks
, resolution = res
, processors = procs }
where
time = asum
[ decimal <* string " us"
, pure (*1000) <*> decimal <* string " ms"
]
totalAlloc :: Parser TotalAlloc
totalAlloc = do
string "total alloc ="; spaces
n <- groupedDecimal
string " bytes" <* spaces <* parens (string "excludes profiling overheads")
return TotalAlloc { totalAllocBytes = n }
groupedDecimal :: Parser Integer
groupedDecimal = foldl' go 0 <$> decimal `sepBy` char8 ','
where go z n = z*1000 + n
hotCostCentres :: Parser [BriefCostCentre]
hotCostCentres = header *> spaces *> many1 briefCostCentre
where header :: Parser ByteString
header = line
briefCostCentre :: Parser BriefCostCentre
briefCostCentre =
BriefCostCentre <$> symbolText <* spaces
<*> symbolText <* spaces
<*> double <* spaces
<*> double <* spaces
costCentres :: Parser (Tree CostCentre)
costCentres = header *> spaces *> costCentreTree
where header = count 2 line
-- Internal functions
costCentreTree :: Parser (Tree CostCentre)
costCentreTree = buildTree <$> costCentreMap >>= maybe empty pure
where
costCentreMap = nestedCostCentre `sepBy1` endOfLine
nestedCostCentre = (,) <$> nestLevel <*> costCentre
nestLevel :: Parser Int
nestLevel = howMany space
costCentre :: Parser CostCentre
costCentre =
CostCentre <$> (T.decodeUtf8 <$> takeWhile (not . isSpace)) <* spaces
<*> (T.decodeUtf8 <$> takeWhile (not . isSpace)) <* spaces
<*> decimal <* spaces
<*> decimal <* spaces
<*> double <* spaces
<*> double <* spaces
<*> double <* spaces
<*> double
type Zipper = TreePos Full
type Level = Int
buildTree :: [(Level, a)] -> Maybe (Tree a)
buildTree [] = Nothing
buildTree ((lvl, t):xs) = Z.toTree <$> snd (foldl' go (lvl, Just z) xs)
where
z = Z.fromTree $ Node t []
go :: (Level, Maybe (Zipper a)) -> (Level, a) -> (Level, Maybe (Zipper a))
go (curLvl, mzipper) a@(lvl', x)
| curLvl > lvl' = go (curLvl-1, mzipper >>= Z.parent) a
| curLvl < lvl' = case mzipper >>= Z.lastChild of
Nothing -> (lvl', Z.insert (Node x []) . Z.children <$> mzipper)
mzipper' -> go (curLvl+1, mzipper') a
| otherwise = (lvl', Z.insert (Node x []) . Z.nextSpace <$> mzipper)
-- Small utilities
howMany :: Parser a -> Parser Int
howMany p = howMany' 0
where howMany' !n = (p >> howMany' (succ n)) <|> return n
spaces :: Parser ()
spaces = () <$ skipMany space
line :: Parser ByteString
line = A.takeWhile (not . isEndOfLine) <* spaces
parens :: Parser a -> Parser a
parens p = string "(" *> p <* string ")"
symbol :: Parser ByteString
symbol = takeWhile (not . isSpace)
symbolText :: Parser Text
symbolText = T.decodeUtf8 <$> symbol
-- Aeson
instance ToJSON ProfilingReport where
toJSON ProfilingReport {..} =
object [ "timestamp" .= reportTimestamp
, "commandLine" .= reportCommandLine
, "totalTime" .= reportTotalTime
, "totalAlloc" .= reportTotalAlloc
, "hotCostCentres" .= reportHotCostCentres
, "costCentres" .= reportCostCentres
]
instance ToJSON TotalTime where
toJSON TotalTime {..} =
object [ "secs" .= totalSecs
, "ticks" .= totalTicks
, "resolution" .= resolution
]
instance ToJSON TotalAlloc where
toJSON TotalAlloc {..} =
object [ "bytes" .= totalAllocBytes ]
instance ToJSON BriefCostCentre where
toJSON BriefCostCentre {..} =
object [ "name" .= briefCostCentreName
, "module" .= briefCostCentreModule
, "time" .= briefCostCentreTime
, "alloc" .= briefCostCentreAlloc
]
instance ToJSON (Tree CostCentre) where
toJSON (Node cc@(CostCentre {..}) subForest)
| null subForest = cc'
| otherwise = branch
where
branch = Object $ M.insert "subForest" subForestWithParent unwrappedCC
parent = Object $ M.insert "isParent" (toJSON True) unwrappedCC
subForestWithParent = Array $ V.fromList $ parent:map toJSON subForest
cc'@(Object unwrappedCC) = toJSON cc
instance ToJSON CostCentre where
toJSON CostCentre {..} =
object [ "name" .= costCentreName
, "module" .= costCentreModule
, "no" .= costCentreNo
, "entries" .= costCentreEntries
, "individualTime" .= individualTime
, "individualAlloc" .= individualAlloc
, "inheritedTime" .= inheritedTime
, "inheritedAlloc" .= inheritedAlloc ]