profile2html.icl 11.3 KB
Newer Older
Camil Staps's avatar
Camil Staps committed
1 2 3 4
module profile2html

import StdEnv

Camil Staps's avatar
Camil Staps committed
5
import Data._Array
Camil Staps's avatar
Camil Staps committed
6 7
import Data.Error
import Data.Func
Camil Staps's avatar
Camil Staps committed
8
import Data.Maybe
9
import qualified Data.Set
Camil Staps's avatar
Camil Staps committed
10 11 12 13 14 15 16 17
import System.CommandLine
import System.File
import System.FilePath
import System.Options
from Text import class Text(concat,join), instance Text String, <+
import qualified Text.HTML

:: Profile =
Camil Staps's avatar
Camil Staps committed
18 19 20 21 22 23 24 25
	{ modules      :: !.{#String}
	, cost_centres :: !.{#CostCentre}
	, profile      :: !Stack
	}

:: CostCentre =
	{ cc_module   :: !Int
	, cc_name     :: !String
Camil Staps's avatar
Camil Staps committed
26 27 28
	}

:: Stack =
Camil Staps's avatar
Camil Staps committed
29 30 31 32 33 34 35
	{ cost_centre      :: !Int
	, ticks            :: !Int
	, words            :: !Int
	, scalls           :: !Int
	, lcalls           :: !Int
	, ccalls           :: !Int
	, children         :: ![Stack]
Camil Staps's avatar
Camil Staps committed
36 37 38 39 40
	, cumulative_ticks :: !Maybe Int // Filled in by this application
	, cumulative_words :: !Maybe Int // Filled in by this application
	}

:: Options =
41 42 43 44
	{ input                :: !FilePath
	, output               :: !FilePath
	, min_cumulative_ticks :: !Int
	, exclude              :: !'Data.Set'.Set String
Camil Staps's avatar
Camil Staps committed
45 46
	}
defaultOptions =
47 48 49 50
	{ input                = ""
	, output               = ""
	, min_cumulative_ticks = 0
	, exclude              = 'Data.Set'.newSet
Camil Staps's avatar
Camil Staps committed
51 52 53 54 55 56 57 58 59 60 61 62
	}

Start w
	# ([prog:args],w) = getCommandLine w
	# args = parseOptions option_description args defaultOptions
	| isError args
		= exit (join "\n" (fromError args)) w
	# args = fromOk args
	| args.input==""
		= exit "Specify an input file with -i/--input" w
	| args.output==""
		= exit "Specify an output file with -o/--output" w
Camil Staps's avatar
Camil Staps committed
63 64 65 66 67 68 69 70
	# (ok,input,w) = fopen args.input FReadData w
	| not ok
		= exit "Could not open input file" w
	# (profile,input) = read_profile input
	# (_,w) = fclose input w
	| isError profile
		= exit ("Could not parse input: "+++fromError profile) w
	# profile = prepare args.min_cumulative_ticks args.exclude (fromOk profile)
Camil Staps's avatar
Camil Staps committed
71 72 73
	# (ok,f,w) = fopen args.output FWriteText w
	| not ok
		= exit "Could not open output file" w
Camil Staps's avatar
Camil Staps committed
74
	# f = f <<< profile
Camil Staps's avatar
Camil Staps committed
75 76 77 78 79 80 81 82 83 84 85 86
	# (_,w) = fclose f w
	= w
where
	option_description = WithHelp True $ Options
		[ Shorthand "-i" "--input" $ Option "--input"
			(\ip opts -> Ok {opts & input=ip})
			"INPUT"
			"The input file"
		, Shorthand "-o" "--output" $ Option "--output"
			(\op opts -> Ok {opts & output=op})
			"OUTPUT"
			"The output file"
87 88 89 90
		, Shorthand "-e" "--exclude" $ Option "--exclude"
			(\e opts -> Ok {opts & exclude='Data.Set'.insert e opts.exclude})
			"MODULE:FUNCTION"
			"A function to exclude (may be specified multiple times)"
91 92 93 94 95 96 97 98 99 100
		, Option "--min-ticks"
			(\t opts -> case toInt t of
				0
					| t=="0"
						-> Ok {opts & min_cumulative_ticks=0}
						-> Error [concat ["--min-ticks: invalid integer '",t,"'"]]
				t
					-> Ok {opts & min_cumulative_ticks=t})
			"N"
			"Only include cost centres with at least N ticks (cumulatively)"
Camil Staps's avatar
Camil Staps committed
101 102 103 104 105 106
		]

	exit err w
		# (_,w) = fclose (stderr <<< err <<< '\n') w
		= setReturnCode -1 w

Camil Staps's avatar
Camil Staps committed
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
read_profile :: !*File -> (!MaybeError String Profile, !*File)
read_profile f
	# (header,f) = freads f 4
	| header <> "prof" = (Error "invalid header",f)
	# (ok,version,f) = freadi f
	| not ok = (unexpected_eof,f)
	| version <> 1 = (Error "invalid version number",f)
	# (ok,n_modules,f) = freadi f
	| not ok = (unexpected_eof,f)
	# (ok,n_cost_centres,f) = freadi f
	| not ok = (unexpected_eof,f)
	# (modules,f) = read_modules 0 n_modules (unsafeCreateArray n_modules) f
	| isError modules = (Error (fromError modules),f)
	# (cost_centres,f) = read_cost_centres 0 n_cost_centres (unsafeCreateArray n_cost_centres) f
	| isError cost_centres = (Error (fromError cost_centres),f)
	# (profile,f) = read_profile_stack f
	| isError profile = (Error (fromError profile),f)
	# profile =
		{ modules      = fromOk modules
		, cost_centres = fromOk cost_centres
		, profile      = fromOk profile
		}
	= (Ok profile,f)
where
	unexpected_eof :: .MaybeError String .a
	unexpected_eof = Error "unexpected end of file"

	read_modules :: !Int !Int !*{#String} !*File -> (!MaybeError String *{#String}, !*File)
	read_modules i n arr f
		| i==n
			= (Ok arr,f)
			# (name,f) = read_nul_terminated_string [] f
			| isError name
				= (Error (fromError name),f)
				= read_modules (i+1) n {arr & [i]=fromOk name} f

	read_cost_centres :: !Int !Int !*{#CostCentre} !*File -> (!MaybeError String *{#CostCentre}, !*File)
	read_cost_centres i n arr f
		| i==n
			= (Ok arr,f)
			# (ok,module_id,f) = freadi f
			| not ok = (unexpected_eof,f)
			# (name,f) = read_nul_terminated_string [] f
			| isError name
				= (Error (fromError name),f)
				= read_cost_centres (i+1) n {arr & [i]={cc_module=module_id-1,cc_name=fromOk name}} f

	read_profile_stack :: !*File -> (!MaybeError String Stack, !*File)
	read_profile_stack f
		# (ok,cost_centre,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,ticks,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,words,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,scalls,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,lcalls,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,ccalls,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (ok,n_children,f) = freadi f
		| not ok = (unexpected_eof,f)
		# (children,f) = read_profile_stacks n_children [] f
		| isError children = (Error (fromError children),f)
		# profile =
			{ cost_centre      = cost_centre-1
			, ticks            = ticks
			, words            = words
			, scalls           = scalls
			, lcalls           = lcalls
			, ccalls           = ccalls
			, children         = fromOk children
			, cumulative_ticks = Nothing
			, cumulative_words = Nothing
			}
		= (Ok profile,f)
	where
		read_profile_stacks :: !Int ![Stack] !*File -> (!MaybeError String [Stack], !*File)
		read_profile_stacks 0 ss f
			= (Ok ss,f)
		read_profile_stacks i ss f
			# (stack,f) = read_profile_stack f
			| isError stack
				= (Error (fromError stack),f)
				= read_profile_stacks (i-1) [fromOk stack:ss] f

	read_nul_terminated_string :: ![Char] !*File -> (!MaybeError String {#Char},!*File)
	read_nul_terminated_string cs f
		# (ok,c,f) = freadc f
		| not ok
			= (unexpected_eof,f)
		| c=='\0'
			= (Ok {c \\ c <- reverse cs},f)
			= read_nul_terminated_string [c:cs] f

203 204
prepare :: !Int !('Data.Set'.Set String) !Profile -> Profile
prepare min_cumulative_ticks excluded p =
205 206
	{ p
	& profile =
Camil Staps's avatar
Camil Staps committed
207 208 209 210 211
		compute_sums_prune_and_sort (fromMaybe -1 gc_id) min_cumulative_ticks $
		(case gc_id of
			Nothing -> id
			Just id -> lift_garbage_collector id) $
		(if ('Data.Set'.size excluded==0) id (remove_excluded_functions excluded_ids))
212 213
		p.profile
	}
Camil Staps's avatar
Camil Staps committed
214 215 216 217 218 219 220 221 222 223
where
	gc_id =
		case
			[ i \\ c <-: p.cost_centres & i <- [0..]
			| c.cc_name=="garbage_collector"
				&& p.modules.[c.cc_module]=="System"]
		of
			[id:_]
				-> Just id
				-> Nothing
224

Camil Staps's avatar
Camil Staps committed
225 226 227 228 229 230
	excluded_ids = 'Data.Set'.fromList
		[ i \\ c <-: p.cost_centres & i <- [0..]
		| 'Data.Set'.member (concat [p.modules.[c.cc_module],":",c.cc_name]) excluded
		]

remove_excluded_functions :: !('Data.Set'.Set Int) !Stack -> Stack
231 232 233 234 235
remove_excluded_functions excluded s =
	{ s
	& children =
		[ remove_excluded_functions excluded c
		\\ c <- s.children
Camil Staps's avatar
Camil Staps committed
236
		| not ('Data.Set'.member c.cost_centre excluded)
237 238 239
		]
	}

Camil Staps's avatar
Camil Staps committed
240 241
lift_garbage_collector :: !Int !Stack -> Stack
lift_garbage_collector cost_centre_id s
Camil Staps's avatar
Camil Staps committed
242 243 244 245 246 247
	# (s,(ticks,calls)) = walk s 0 0
	| calls==0
		= s
		= {s & children=[gc:s.children]}
		with
			gc =
Camil Staps's avatar
Camil Staps committed
248 249 250 251 252 253 254
				{ cost_centre      = cost_centre_id
				, ccalls           = 0
				, lcalls           = 0
				, scalls           = calls
				, words            = 0
				, ticks            = ticks
				, children         = []
Camil Staps's avatar
Camil Staps committed
255 256 257 258 259 260
				, cumulative_ticks = Just ticks
				, cumulative_words = Just 0
				}
where
	walk :: !Stack !Int !Int -> (!Stack, !(!Int,!Int))
	walk s ticks calls
Camil Staps's avatar
Camil Staps committed
261
		| s.cost_centre==cost_centre_id
Camil Staps's avatar
Camil Staps committed
262 263
			= (s, (ticks+s.ticks, calls+s.scalls))
		# (children,counts) = mapSt (\c (ts,cs) -> walk c ts cs) s.children (ticks,calls)
Camil Staps's avatar
Camil Staps committed
264
		= ({s & children=[c \\ c <- children | c.cost_centre <> cost_centre_id]}, counts)
Camil Staps's avatar
Camil Staps committed
265

Camil Staps's avatar
Camil Staps committed
266 267 268
compute_sums_prune_and_sort :: !Int !Int !Stack -> Stack
compute_sums_prune_and_sort gc_id min_cumulative_ticks s
	# children = reverse (sort [compute_sums_prune_and_sort gc_id min_cumulative_ticks c \\ c <- s.children])
269 270
	  s & cumulative_ticks = Just (s.ticks + sum [t \\ {cumulative_ticks=Just t} <- children])
	  s & cumulative_words = Just (s.words + sum [w \\ {cumulative_words=Just w} <- children])
Camil Staps's avatar
Camil Staps committed
271 272 273 274 275
	  s & children =
		[ c \\ c <- children
		| fromJust c.cumulative_ticks >= min_cumulative_ticks
			|| c.cost_centre==gc_id
		]
Camil Staps's avatar
Camil Staps committed
276 277 278 279
	= s

instance < Stack
where
280
	< a b = fromJust a.cumulative_ticks < fromJust b.cumulative_ticks
Camil Staps's avatar
Camil Staps committed
281 282 283

instance <<< Profile
where
Camil Staps's avatar
Camil Staps committed
284 285
	<<< f p
		# f = f <<< "<!DOCTYPE html>"
Camil Staps's avatar
Camil Staps committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
			<<< "<html lang=\"en\">"
			<<< "<head>"
			<<< "<title>Profile</title>"
			<<< "<meta charset=\"utf-8\">"
			<<< "<link rel=\"stylesheet\" type=\"text/css\" href=\"profile.css\">"
			<<< "</head>"
			<<< "<body>"
			<<< "<div class=\"header\">"
			<<< "<span class=\"entry-data\">Function</span>"
			<<< "<span class=\"progress-header\">Words</span>"
			<<< "<span class=\"progress-header\">Ticks</span>"
			<<< "<span class=\"entry-data\">Curried calls</span>"
			<<< "<span class=\"entry-data\">Lazy calls</span>"
			<<< "<span class=\"entry-data\">Strict calls</span>"
			<<< "</div>"
Camil Staps's avatar
Camil Staps committed
301 302
		# f = write_stack p p.profile f
		= f
Camil Staps's avatar
Camil Staps committed
303 304 305 306
			<<< "<script defer=\"defer\" src=\"profile.js\"></script>"
			<<< "</body>"
			<<< "</html>"

Camil Staps's avatar
Camil Staps committed
307 308
write_stack :: !Profile !Stack !*File -> *File
write_stack p s f = write (fromJust s.cumulative_ticks) (fromJust s.cumulative_words) s f
Camil Staps's avatar
Camil Staps committed
309
where
Camil Staps's avatar
Camil Staps committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	write :: !Int !Int !Stack !*File -> *File
	write parent_ticks parent_words s f
		# f = f <<< "<div class=\"entry\">"
			<<< "<div class=\"entry-header\">"
			<<< if (isEmpty s.children) "" "<span class=\"toggler open\">&#x229f;</span>"
			<<< "<span class=\"entry-content\">" <<< 'Text.HTML'.escapeStr p.modules.[cost_centre.cc_module]
				<<< ": <span class=\"function\">" <<< 'Text.HTML'.escapeStr cost_centre.cc_name <<< "</span></span>"
			<<< "&nbsp;"
			<<< "<span class=\"progress words\" title=\"" <<< words <<< "\">"
				<<< if (parent_words==0) "" (concat ["<span class=\"bar\" style=\"",words_width,"\"></span>"])
				<<< "</span>"
			<<< "<span class=\"progress ticks\" title=\"" <<< ticks <<< "\">"
				<<< if (parent_ticks==0) "" (concat ["<span class=\"bar\" style=\"",ticks_width,"\"></span>"])
				<<< "</span>"
			<<< "<span class=\"entry-data\">" <<< if (s.ccalls==0) "&nbsp;" (toString s.ccalls) <<< "</span>"
			<<< "<span class=\"entry-data\">" <<< if (s.lcalls==0) "&nbsp;" (toString s.lcalls) <<< "</span>"
			<<< "<span class=\"entry-data\">" <<< if (s.scalls==0) "&nbsp;" (toString s.scalls) <<< "</span>"
			<<< "</div>"
		# f = foldl (\f c -> write cumulative_ticks cumulative_words c f) f s.children
		= f <<< "</div>"
Camil Staps's avatar
Camil Staps committed
330
	where
Camil Staps's avatar
Camil Staps committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		cost_centre = p.cost_centres.[s.cost_centre]

		cumulative_ticks = fromJust s.cumulative_ticks
		ticks = if (isEmpty s.children)
			(toString s.ticks)
			(concat [toString cumulative_ticks," (",toString s.ticks,")"])
		ticks_width = toWidth (toReal cumulative_ticks / toReal parent_ticks)

		cumulative_words = fromJust s.cumulative_words
		words = if (isEmpty s.children)
			(toString s.words)
			(concat [toString cumulative_words," (",toString s.words,")"])
		words_width = toWidth (toReal cumulative_words / toReal parent_words)

		toWidth :: !Real -> String
		toWidth w
			| wi>=10000
				= "width:100%;"
				=
					{ 'w','i','d','t','h',':'
					, toChar (wi/1000)+'0'
					, toChar ((wi rem 1000)/100)+'0'
					, '.'
					, toChar ((wi rem 100)/10)+'0'
					, toChar (wi rem 10)+'0'
					, '%',';'
					}
Camil Staps's avatar
Camil Staps committed
358
		where
Camil Staps's avatar
Camil Staps committed
359
			wi = toInt (w * 10000.0)