| 1 |
(* Copyright (C) 2022 Matthew Fluet |
| 2 |
* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh |
| 3 |
* Jagannathan, and Stephen Weeks. |
| 4 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 5 |
* |
| 6 |
* MLton is released under a HPND-style license. |
| 7 |
* See the file MLton-LICENSE for details. |
| 8 |
*) |
| 9 |
|
| 10 |
structure Main : sig val main : unit -> unit end = |
| 11 |
struct |
| 12 |
|
| 13 |
type int = Int.t |
| 14 |
type word = Word.t |
| 15 |
|
| 16 |
val debug = false |
| 17 |
|
| 18 |
val callGraphFile: File.t option ref = ref NONE |
| 19 |
val gray: bool ref = ref false |
| 20 |
val longName: bool ref = ref true |
| 21 |
val mlmonFiles: string list ref = ref [] |
| 22 |
val raw = ref false |
| 23 |
val showLine = ref false |
| 24 |
val splitReg: Regexp.t ref = ref Regexp.none |
| 25 |
val title: string option ref = ref NONE |
| 26 |
val tolerant: bool ref = ref false |
| 27 |
|
| 28 |
structure Source = |
| 29 |
struct |
| 30 |
datatype t = |
| 31 |
NamePos of {name: string, |
| 32 |
pos: string} |
| 33 |
| Simple of string |
| 34 |
|
| 35 |
fun toString n = |
| 36 |
case n of |
| 37 |
NamePos {name, pos} => concat [name, " ", pos] |
| 38 |
| Simple s => s |
| 39 |
|
| 40 |
fun toStringMaybeLine n = |
| 41 |
case n of |
| 42 |
NamePos {name, pos} => |
| 43 |
if !showLine |
| 44 |
then concat [name, " ", pos] |
| 45 |
else name |
| 46 |
| Simple s => s |
| 47 |
|
| 48 |
val layout = Layout.str o toString |
| 49 |
|
| 50 |
fun fromString s = |
| 51 |
case String.tokens (s, fn c => Char.equals (c, #"\t")) of |
| 52 |
[s] => Simple s |
| 53 |
| [name, pos] => |
| 54 |
let |
| 55 |
val name = |
| 56 |
if !longName |
| 57 |
then name |
| 58 |
else |
| 59 |
List.last |
| 60 |
(String.tokens (name, fn c => Char.equals (c, #"."))) |
| 61 |
in |
| 62 |
NamePos {name = name, pos = pos} |
| 63 |
end |
| 64 |
| _ => Error.bug "strange source" |
| 65 |
|
| 66 |
fun toDotLabel s = |
| 67 |
case s of |
| 68 |
NamePos {name, pos} => |
| 69 |
if !showLine |
| 70 |
then [(name, Dot.Center), |
| 71 |
(pos, Dot.Center)] |
| 72 |
else [(name, Dot.Center)] |
| 73 |
| Simple s => |
| 74 |
[(s, Dot.Center)] |
| 75 |
end |
| 76 |
|
| 77 |
structure Graph = DirectedGraph |
| 78 |
local |
| 79 |
open Graph |
| 80 |
in |
| 81 |
structure Edge = Edge |
| 82 |
structure Node = Node |
| 83 |
end |
| 84 |
local |
| 85 |
open Dot |
| 86 |
in |
| 87 |
structure EdgeOption = EdgeOption |
| 88 |
structure NodeOption = NodeOption |
| 89 |
end |
| 90 |
|
| 91 |
structure AFile = |
| 92 |
struct |
| 93 |
datatype t = T of {callGraph: unit Graph.t, |
| 94 |
magic: word, |
| 95 |
master: {isSplit: bool, |
| 96 |
source: Source.t} vector, |
| 97 |
name: string, |
| 98 |
split: {masterIndex: int, |
| 99 |
node: unit Node.t} vector} |
| 100 |
|
| 101 |
fun layout (T {magic, name, master, ...}) = |
| 102 |
Layout.record |
| 103 |
[("name", String.layout name), |
| 104 |
("magic", Word.layout magic), |
| 105 |
("master", |
| 106 |
Vector.layout (fn {isSplit, source} => |
| 107 |
Layout.record [("isSplit", Bool.layout isSplit), |
| 108 |
("source", Source.layout source)]) |
| 109 |
master)] |
| 110 |
|
| 111 |
fun new {afile: File.t}: t = |
| 112 |
let |
| 113 |
fun userBug m = |
| 114 |
Error.bug (concat ["Error: executable '", afile, "' ", m, "."]) |
| 115 |
in |
| 116 |
if not (File.doesExist afile) then |
| 117 |
userBug "does not exist" |
| 118 |
else if not (File.canRun afile) then |
| 119 |
userBug "does not run" |
| 120 |
else |
| 121 |
Process.executeWithIn |
| 122 |
(OS.Path.mkAbsolute {path = afile, |
| 123 |
relativeTo = OS.FileSys.getDir ()}, |
| 124 |
["@MLton", "show-sources"], |
| 125 |
fn ins => |
| 126 |
let |
| 127 |
fun line () = |
| 128 |
case In.inputLine ins of |
| 129 |
NONE => Error.bug "unexpected end of show-sources data" |
| 130 |
| SOME l => l |
| 131 |
val magic = |
| 132 |
case Word.fromString (line ()) of |
| 133 |
NONE => Error.bug "expected magic" |
| 134 |
| SOME w => w |
| 135 |
fun vector (f: string -> 'a): 'a vector = |
| 136 |
Vector.tabulate (valOf (Int.fromString (line ())), |
| 137 |
fn _ => f (line ())) |
| 138 |
val rc = Regexp.compileNFA (!splitReg) |
| 139 |
val master = |
| 140 |
vector |
| 141 |
(fn s => |
| 142 |
let |
| 143 |
val source = Source.fromString (String.dropSuffix (s, 1)) |
| 144 |
val isSplit = |
| 145 |
Regexp.Compiled.matchesPrefix |
| 146 |
(rc, Source.toString source) |
| 147 |
in |
| 148 |
{isSplit = isSplit, |
| 149 |
source = source} |
| 150 |
end) |
| 151 |
val _ = |
| 152 |
if 0 = Vector.length master then |
| 153 |
userBug "is not compiled for profiling" |
| 154 |
else () |
| 155 |
val sources = |
| 156 |
vector |
| 157 |
(fn s => |
| 158 |
case String.tokens (s, Char.isSpace) of |
| 159 |
[masterIndex, successorsIndex] => |
| 160 |
{masterIndex = valOf (Int.fromString masterIndex), |
| 161 |
successorsIndex = valOf (Int.fromString |
| 162 |
successorsIndex)} |
| 163 |
| _ => Error.bug "AFile.new") |
| 164 |
val sourceSeqs = |
| 165 |
vector |
| 166 |
(fn s => |
| 167 |
Vector.fromListMap |
| 168 |
(String.tokens (s, Char.isSpace), fn s => |
| 169 |
valOf (Int.fromString s))) |
| 170 |
val graph = Graph.new () |
| 171 |
val split = |
| 172 |
Vector.map |
| 173 |
(sources, fn {masterIndex, ...} => |
| 174 |
let |
| 175 |
val n = Graph.newNode graph |
| 176 |
in |
| 177 |
{masterIndex = masterIndex, |
| 178 |
node = n} |
| 179 |
end) |
| 180 |
val _ = |
| 181 |
Vector.foreach2 |
| 182 |
(sources, split, |
| 183 |
fn ({successorsIndex, ...}, {node = from, ...}) => |
| 184 |
Vector.foreach |
| 185 |
(Vector.sub (sourceSeqs, successorsIndex), |
| 186 |
fn to => |
| 187 |
(ignore o Graph.addEdge) |
| 188 |
(graph, {from = from, |
| 189 |
to = #node (Vector.sub (split, to))}))) |
| 190 |
val _ = |
| 191 |
case In.inputLine ins of |
| 192 |
NONE => () |
| 193 |
| SOME _ => Error.bug "expected end of file" |
| 194 |
in |
| 195 |
T {callGraph = graph, |
| 196 |
magic = magic, |
| 197 |
master = master, |
| 198 |
name = afile, |
| 199 |
split = split} |
| 200 |
end) |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
structure Kind = |
| 205 |
struct |
| 206 |
datatype t = Alloc | Count | Empty | Time |
| 207 |
|
| 208 |
val toString = |
| 209 |
fn Alloc => "Alloc" |
| 210 |
| Count => "Count" |
| 211 |
| Empty => "Empty" |
| 212 |
| Time => "Time" |
| 213 |
|
| 214 |
val layout = Layout.str o toString |
| 215 |
|
| 216 |
val merge: t * t -> t = |
| 217 |
fn (k, k') => |
| 218 |
case (k, k') of |
| 219 |
(Alloc, Alloc) => Alloc |
| 220 |
| (Count, Count) => Count |
| 221 |
| (_, Empty) => k |
| 222 |
| (Empty, _) => k' |
| 223 |
| (Time, Time) => Time |
| 224 |
| _ => Error.bug "Kind.merge" |
| 225 |
end |
| 226 |
|
| 227 |
structure Style = |
| 228 |
struct |
| 229 |
datatype t = Current | Stack |
| 230 |
|
| 231 |
(* val toString = fn Current => "Current" | Stack => "Stack" *) |
| 232 |
|
| 233 |
(* val layout = Layout.str o toString *) |
| 234 |
end |
| 235 |
|
| 236 |
structure Counts = |
| 237 |
struct |
| 238 |
datatype t = |
| 239 |
Current of {master: IntInf.t vector, |
| 240 |
split: IntInf.t vector} |
| 241 |
| Empty |
| 242 |
| Stack of {master: {current: IntInf.t, |
| 243 |
stack: IntInf.t, |
| 244 |
stackGC: IntInf.t} vector, |
| 245 |
split: {current: IntInf.t, |
| 246 |
stack: IntInf.t, |
| 247 |
stackGC: IntInf.t} vector} |
| 248 |
|
| 249 |
val layout = |
| 250 |
fn Current {master, split} => |
| 251 |
Layout.record [("master", Vector.layout IntInf.layout master), |
| 252 |
("split", Vector.layout IntInf.layout split)] |
| 253 |
| Empty => Layout.str "empty" |
| 254 |
| Stack {master, split} => |
| 255 |
let |
| 256 |
fun lay v = |
| 257 |
Vector.layout |
| 258 |
(fn {current, stack, stackGC} => |
| 259 |
Layout.record [("current", IntInf.layout current), |
| 260 |
("stack", IntInf.layout stack), |
| 261 |
("stackGC", IntInf.layout stackGC)]) |
| 262 |
v |
| 263 |
in |
| 264 |
Layout.record [("master", lay master), |
| 265 |
("split", lay split)] |
| 266 |
end |
| 267 |
|
| 268 |
fun merge (c: t, c': t): t = |
| 269 |
case (c, c') of |
| 270 |
(Current {master = m, split = s}, |
| 271 |
Current {master = m', split = s'}) => |
| 272 |
let |
| 273 |
fun merge (v, v') = Vector.map2 (v, v', op +) |
| 274 |
in |
| 275 |
Current {master = merge (m, m'), |
| 276 |
split = merge (s, s')} |
| 277 |
end |
| 278 |
| (Empty, _) => c' |
| 279 |
| (_, Empty) => c |
| 280 |
| (Stack {master = m, split = s}, Stack {master = m', split = s'}) => |
| 281 |
let |
| 282 |
fun merge (v, v') = |
| 283 |
Vector.map2 |
| 284 |
(v, v', fn ({current = c, stack = s, stackGC = g}, |
| 285 |
{current = c', stack = s', stackGC = g'}) => |
| 286 |
{current = c + c', |
| 287 |
stack = s + s', |
| 288 |
stackGC = g + g'}) |
| 289 |
in |
| 290 |
Stack {master = merge (m, m'), |
| 291 |
split = merge (s, s')} |
| 292 |
end |
| 293 |
| _ => |
| 294 |
Error.bug |
| 295 |
"cannot merge -profile-stack false with -profile-stack true" |
| 296 |
end |
| 297 |
|
| 298 |
structure ProfFile = |
| 299 |
struct |
| 300 |
datatype t = T of {counts: Counts.t, |
| 301 |
kind: Kind.t, |
| 302 |
magic: word, |
| 303 |
total: IntInf.t, |
| 304 |
totalGC: IntInf.t} |
| 305 |
|
| 306 |
fun empty (AFile.T {magic, ...}) = |
| 307 |
T {counts = Counts.Empty, |
| 308 |
kind = Kind.Empty, |
| 309 |
magic = magic, |
| 310 |
total = 0, |
| 311 |
totalGC = 0} |
| 312 |
|
| 313 |
fun layout (T {counts, kind, magic, total, totalGC}) = |
| 314 |
Layout.record [("kind", Kind.layout kind), |
| 315 |
("magic", Word.layout magic), |
| 316 |
("total", IntInf.layout total), |
| 317 |
("totalGC", IntInf.layout totalGC), |
| 318 |
("counts", Counts.layout counts)] |
| 319 |
|
| 320 |
fun new {mlmonfile: File.t}: t = |
| 321 |
File.withIn |
| 322 |
(mlmonfile, fn ins => |
| 323 |
let |
| 324 |
fun line () = |
| 325 |
case In.inputLine ins of |
| 326 |
NONE => Error.bug "unexpected end of mlmon file" |
| 327 |
| SOME s => String.dropSuffix (s, 1) |
| 328 |
val _ = |
| 329 |
if "MLton prof" = line () |
| 330 |
then () |
| 331 |
else Error.bug "bad header" |
| 332 |
val kind = |
| 333 |
case line () of |
| 334 |
"alloc" => Kind.Alloc |
| 335 |
| "count" => Kind.Count |
| 336 |
| "time" => Kind.Time |
| 337 |
| _ => Error.bug "invalid profile kind" |
| 338 |
val style = |
| 339 |
case line () of |
| 340 |
"current" => Style.Current |
| 341 |
| "stack" => Style.Stack |
| 342 |
| _ => Error.bug "invalid profile style" |
| 343 |
val magic = |
| 344 |
case Word.fromString (line ()) of |
| 345 |
NONE => Error.bug "invalid magic" |
| 346 |
| SOME w => w |
| 347 |
fun s2i s = |
| 348 |
case IntInf.fromString s of |
| 349 |
NONE => Error.bug "invalid count" |
| 350 |
| SOME i => i |
| 351 |
val (total, totalGC) = |
| 352 |
case String.tokens (line (), Char.isSpace) of |
| 353 |
[total, totalGC] => (s2i total, s2i totalGC) |
| 354 |
| _ => Error.bug "invalid totals" |
| 355 |
fun getCounts (f: string -> 'a): {master: 'a vector, |
| 356 |
split: 'a vector} = |
| 357 |
let |
| 358 |
fun vector () = |
| 359 |
Vector.tabulate (valOf (Int.fromString (line ())), |
| 360 |
fn _ => f (line ())) |
| 361 |
val split = vector () |
| 362 |
val master = vector () |
| 363 |
in |
| 364 |
{master = master, split = split} |
| 365 |
end |
| 366 |
val counts = |
| 367 |
case style of |
| 368 |
Style.Current => Counts.Current (getCounts s2i) |
| 369 |
| Style.Stack => |
| 370 |
Counts.Stack |
| 371 |
(getCounts |
| 372 |
(fn s => |
| 373 |
case String.tokens (s, Char.isSpace) of |
| 374 |
[c, s, sGC] => |
| 375 |
{current = s2i c, |
| 376 |
stack = s2i s, |
| 377 |
stackGC = s2i sGC} |
| 378 |
| _ => |
| 379 |
Error.bug |
| 380 |
(concat ["strange line: ", |
| 381 |
String.dropSuffix (s, 1)]))) |
| 382 |
in |
| 383 |
T {counts = counts, |
| 384 |
kind = kind, |
| 385 |
magic = magic, |
| 386 |
total = total, |
| 387 |
totalGC = totalGC} |
| 388 |
end) |
| 389 |
|
| 390 |
fun merge (T {counts = c, kind = k, magic = m, total = t, totalGC = g}, |
| 391 |
T {counts = c', kind = k', magic = m', total = t', |
| 392 |
totalGC = g'}): t = |
| 393 |
if m <> m' |
| 394 |
then Error.bug "wrong magic number" |
| 395 |
else |
| 396 |
T {counts = Counts.merge (c, c'), |
| 397 |
kind = Kind.merge (k, k'), |
| 398 |
magic = m, |
| 399 |
total = t + t', |
| 400 |
totalGC = g + g'} |
| 401 |
end |
| 402 |
|
| 403 |
structure Atomic = |
| 404 |
struct |
| 405 |
datatype t = |
| 406 |
Name of string * Regexp.Compiled.t |
| 407 |
| Thresh of real |
| 408 |
| ThreshGC of real |
| 409 |
| ThreshStack of real |
| 410 |
|
| 411 |
val toSexp: t -> Sexp.t = |
| 412 |
fn a => |
| 413 |
let |
| 414 |
datatype z = datatype Sexp.t |
| 415 |
in |
| 416 |
case a of |
| 417 |
Name (s, _) => String s |
| 418 |
| Thresh x => List [Atom "thresh", Atom (Real.toString x)] |
| 419 |
| ThreshGC x => List [Atom "thresh-gc", Atom (Real.toString x)] |
| 420 |
| ThreshStack x => |
| 421 |
List [Atom "thresh-stack", Atom (Real.toString x)] |
| 422 |
end |
| 423 |
end |
| 424 |
|
| 425 |
structure NodePred = |
| 426 |
struct |
| 427 |
datatype t = |
| 428 |
All |
| 429 |
| And of t vector |
| 430 |
| Atomic of Atomic.t |
| 431 |
| Not of t |
| 432 |
| Or of t vector |
| 433 |
| PathFrom of t |
| 434 |
| PathTo of t |
| 435 |
| Pred of t |
| 436 |
| Succ of t |
| 437 |
|
| 438 |
val rec toSexp: t -> Sexp.t = |
| 439 |
fn p => |
| 440 |
let |
| 441 |
datatype z = datatype Sexp.t |
| 442 |
fun nAry (name, ps) = |
| 443 |
List (Atom name :: Vector.toListMap (ps, toSexp)) |
| 444 |
fun unary (name, p) = |
| 445 |
List [Atom name, toSexp p] |
| 446 |
in |
| 447 |
case p of |
| 448 |
All => Sexp.Atom "all" |
| 449 |
| And ps => nAry ("and", ps) |
| 450 |
| Atomic a => Atomic.toSexp a |
| 451 |
| Not p => unary ("not", p) |
| 452 |
| Or ps => nAry ("or", ps) |
| 453 |
| PathFrom p => unary ("from", p) |
| 454 |
| PathTo p => unary ("to", p) |
| 455 |
| Pred p => unary ("pred", p) |
| 456 |
| Succ p => unary ("succ", p) |
| 457 |
end |
| 458 |
|
| 459 |
(* val layout = Sexp.layout o toSexp *) |
| 460 |
|
| 461 |
val fromString: string -> t = |
| 462 |
fn s => |
| 463 |
case Sexp.fromString s of |
| 464 |
Sexp.Eof => Error.bug "empty" |
| 465 |
| Sexp.Error s => Error.bug s |
| 466 |
| Sexp.Sexp s => |
| 467 |
let |
| 468 |
fun parse (s: Sexp.t): t = |
| 469 |
let |
| 470 |
fun err () = Error.bug (Sexp.toString s) |
| 471 |
in |
| 472 |
case s of |
| 473 |
Sexp.Atom s => |
| 474 |
(case s of |
| 475 |
"all" => All |
| 476 |
| _ => err ()) |
| 477 |
| Sexp.List ss => |
| 478 |
(case ss of |
| 479 |
[] => err () |
| 480 |
| s :: ss => |
| 481 |
let |
| 482 |
fun nAry f = |
| 483 |
f (Vector.fromListMap (ss, parse)) |
| 484 |
fun unary f = |
| 485 |
case ss of |
| 486 |
[s] => f (parse s) |
| 487 |
| _ => err () |
| 488 |
fun thresh f = |
| 489 |
case ss of |
| 490 |
[Sexp.Atom x] => |
| 491 |
(case Real.fromString x of |
| 492 |
NONE => err () |
| 493 |
| SOME x => |
| 494 |
if 0.0 <= x |
| 495 |
andalso x <= 100.0 |
| 496 |
then Atomic (f x) |
| 497 |
else err ()) |
| 498 |
| _ => err () |
| 499 |
datatype z = datatype Atomic.t |
| 500 |
in |
| 501 |
case s of |
| 502 |
Sexp.Atom s => |
| 503 |
(case s of |
| 504 |
"and" => nAry And |
| 505 |
| "from" => unary PathFrom |
| 506 |
| "not" => unary Not |
| 507 |
| "or" => nAry Or |
| 508 |
| "pred" => unary Pred |
| 509 |
| "succ" => unary Succ |
| 510 |
| "thresh" => thresh Thresh |
| 511 |
| "thresh-gc" => thresh ThreshGC |
| 512 |
| "thresh-stack" => |
| 513 |
thresh ThreshStack |
| 514 |
| "to" => unary PathTo |
| 515 |
| _ => err ()) |
| 516 |
| _ => err () |
| 517 |
end) |
| 518 |
| Sexp.String s => |
| 519 |
(case Regexp.fromString s of |
| 520 |
NONE => err () |
| 521 |
| SOME (r, _) => |
| 522 |
Atomic |
| 523 |
(Atomic.Name (s, Regexp.compileNFA r))) |
| 524 |
end |
| 525 |
in |
| 526 |
parse s |
| 527 |
end |
| 528 |
|
| 529 |
fun nodes (p: t, g: 'a Graph.t, |
| 530 |
atomic: 'a Node.t * Atomic.t -> bool): 'a Node.t vector = |
| 531 |
let |
| 532 |
val {get = nodeIndex: 'a Node.t -> int, |
| 533 |
set = setNodeIndex, ...} = |
| 534 |
Property.getSet (Node.plist, |
| 535 |
Property.initRaise ("index", Node.layout)) |
| 536 |
val nodes = Vector.fromList (Graph.nodes g) |
| 537 |
val numNodes = Vector.length nodes |
| 538 |
val _ = Vector.foreachi (nodes, fn (i, n) => setNodeIndex (n, i)) |
| 539 |
val transpose = |
| 540 |
Promise.lazy |
| 541 |
(fn () => |
| 542 |
let |
| 543 |
val {get = nodeIndex': 'a Graph.u Node.t -> int, |
| 544 |
set = setNodeIndex, ...} = |
| 545 |
Property.getSet (Node.plist, |
| 546 |
Property.initRaise ("index", Node.layout)) |
| 547 |
val (transpose, {newNode, ...}) = Graph.transpose g |
| 548 |
val _ = |
| 549 |
Graph.foreachNode |
| 550 |
(g, fn n => setNodeIndex (newNode n, nodeIndex n)) |
| 551 |
in |
| 552 |
(transpose, newNode, nodeIndex') |
| 553 |
end) |
| 554 |
fun vectorToNodes (v: bool vector): 'a Node.t vector = |
| 555 |
Vector.keepAllMapi |
| 556 |
(v, fn (i, b) => |
| 557 |
if b |
| 558 |
then SOME (Vector.sub (nodes, i)) |
| 559 |
else NONE) |
| 560 |
val all = Promise.lazy (fn () => |
| 561 |
Vector.tabulate (numNodes, fn _ => true)) |
| 562 |
val none = Promise.lazy (fn () => |
| 563 |
Vector.tabulate (numNodes, fn _ => false)) |
| 564 |
fun path (v: bool vector, |
| 565 |
(g: 'b Graph.t, |
| 566 |
getNode: 'a Node.t -> 'b Node.t, |
| 567 |
nodeIndex: 'b Node.t -> int)): bool vector = |
| 568 |
let |
| 569 |
val roots = vectorToNodes v |
| 570 |
val a = Array.array (numNodes, false) |
| 571 |
val _ = |
| 572 |
Graph.dfsNodes |
| 573 |
(g, |
| 574 |
Vector.toListMap (roots, getNode), |
| 575 |
Graph.DfsParam.startNode (fn n => |
| 576 |
Array.update |
| 577 |
(a, nodeIndex n, true))) |
| 578 |
in |
| 579 |
Vector.fromArray a |
| 580 |
end |
| 581 |
fun loop (p: t): bool vector = |
| 582 |
case p of |
| 583 |
All => all () |
| 584 |
| And ps => |
| 585 |
Vector.fold (ps, all (), fn (p, v) => |
| 586 |
Vector.map2 (v, loop p, fn (b, b') => |
| 587 |
b andalso b')) |
| 588 |
| Atomic a => Vector.map (nodes, fn n => atomic (n, a)) |
| 589 |
| Not p => Vector.map (loop p, not) |
| 590 |
| Or ps => |
| 591 |
Vector.fold (ps, none (), fn (p, v) => |
| 592 |
Vector.map2 (v, loop p, fn (b, b') => |
| 593 |
b orelse b')) |
| 594 |
| PathFrom p => path (loop p, (g, fn n => n, nodeIndex)) |
| 595 |
| PathTo p => path (loop p, transpose ()) |
| 596 |
| Pred p => |
| 597 |
let |
| 598 |
val ns = vectorToNodes (loop p) |
| 599 |
val {destroy, get, set, ...} = |
| 600 |
Property.destGetSetOnce |
| 601 |
(Node.plist, Property.initConst false) |
| 602 |
val _ = Vector.foreach (ns, fn n => set (n, true)) |
| 603 |
val v = |
| 604 |
Vector.map |
| 605 |
(nodes, fn n => |
| 606 |
get n orelse |
| 607 |
List.exists (Node.successors n, get o Edge.to)) |
| 608 |
val _ = destroy () |
| 609 |
in |
| 610 |
v |
| 611 |
end |
| 612 |
| Succ p => |
| 613 |
let |
| 614 |
val a = Array.array (numNodes, false) |
| 615 |
fun yes n = Array.update (a, nodeIndex n, true) |
| 616 |
val _ = |
| 617 |
Vector.foreach |
| 618 |
(vectorToNodes (loop p), fn n => |
| 619 |
(yes n |
| 620 |
; List.foreach (Node.successors n, yes o Edge.to))) |
| 621 |
in |
| 622 |
Vector.fromArray a |
| 623 |
end |
| 624 |
val v = loop p |
| 625 |
in |
| 626 |
vectorToNodes v |
| 627 |
end |
| 628 |
end |
| 629 |
|
| 630 |
val keep: NodePred.t ref = ref NodePred.All |
| 631 |
|
| 632 |
val ticksPerSecond = 100.0 |
| 633 |
|
| 634 |
fun display (AFile.T {callGraph, master, name = aname, split, ...}, |
| 635 |
ProfFile.T {counts, kind, total, totalGC, ...}): unit = |
| 636 |
let |
| 637 |
val {get = nodeInfo: (unit Node.t |
| 638 |
-> {index: int, |
| 639 |
keep: bool ref, |
| 640 |
mayKeep: (Atomic.t -> bool) ref}), |
| 641 |
set = setNodeInfo, ...} = |
| 642 |
Property.getSetOnce (Node.plist, |
| 643 |
Property.initRaise ("info", Node.layout)) |
| 644 |
val _ = |
| 645 |
Vector.foreachi (split, fn (i, {node, ...}) => |
| 646 |
setNodeInfo (node, |
| 647 |
{index = i, |
| 648 |
keep = ref false, |
| 649 |
mayKeep = ref (fn _ => false)})) |
| 650 |
val profileStack = |
| 651 |
case counts of |
| 652 |
Counts.Current _ => false |
| 653 |
| Counts.Empty => false |
| 654 |
| Counts.Stack _ => true |
| 655 |
val totalReal = Real.fromIntInf (total + totalGC) |
| 656 |
val per: IntInf.t -> real = |
| 657 |
if Real.equals (0.0, totalReal) |
| 658 |
then fn _ => 0.0 |
| 659 |
else |
| 660 |
fn ticks => 100.0 * Real.fromIntInf ticks / totalReal |
| 661 |
fun doit ({master = masterCount: 'a vector, |
| 662 |
split = splitCount: 'a vector}, |
| 663 |
f: 'a -> {current: IntInf.t, |
| 664 |
stack: IntInf.t, |
| 665 |
stackGC: IntInf.t}) = |
| 666 |
let |
| 667 |
val _ = |
| 668 |
Vector.foreachi |
| 669 |
(split, fn (i, {masterIndex, node, ...}) => |
| 670 |
let |
| 671 |
val {mayKeep, ...} = nodeInfo node |
| 672 |
val {isSplit, source, ...} = Vector.sub (master, masterIndex) |
| 673 |
val name = Source.toString source |
| 674 |
in |
| 675 |
mayKeep := |
| 676 |
(fn a => |
| 677 |
let |
| 678 |
fun thresh (x: real, sel) = |
| 679 |
let |
| 680 |
val (v, i) = |
| 681 |
if isSplit |
| 682 |
then (splitCount, i) |
| 683 |
else (masterCount, masterIndex) |
| 684 |
in |
| 685 |
per (sel (f (Vector.sub (v, i)))) >= x |
| 686 |
end |
| 687 |
datatype z = datatype Atomic.t |
| 688 |
in |
| 689 |
case a of |
| 690 |
Name (_, rc) => |
| 691 |
Regexp.Compiled.matchesPrefix (rc, name) |
| 692 |
| Thresh x => thresh (x, #current) |
| 693 |
| ThreshGC x => thresh (x, #stackGC) |
| 694 |
| ThreshStack x => thresh (x, #stack) |
| 695 |
end) |
| 696 |
end) |
| 697 |
fun row (ticks: IntInf.t): string list = |
| 698 |
(concat [Real.format (per ticks, Real.Format.fix (SOME 1)), "%"]) |
| 699 |
:: (if !raw |
| 700 |
then |
| 701 |
[concat |
| 702 |
(case kind of |
| 703 |
Kind.Alloc => |
| 704 |
["(", IntInf.toCommaString ticks, ")"] |
| 705 |
| Kind.Count => |
| 706 |
["(", IntInf.toCommaString ticks, ")"] |
| 707 |
| Kind.Empty => [] |
| 708 |
| Kind.Time => |
| 709 |
["(", |
| 710 |
Real.format |
| 711 |
(Real.fromIntInf ticks / ticksPerSecond, |
| 712 |
Real.Format.fix (SOME 2)), |
| 713 |
"s)"])] |
| 714 |
else []) |
| 715 |
fun info (source: Source.t, a: 'a) = |
| 716 |
let |
| 717 |
val {current, stack, stackGC} = f a |
| 718 |
val row = |
| 719 |
row current |
| 720 |
@ (if profileStack |
| 721 |
then row stack @ row stackGC |
| 722 |
else []) |
| 723 |
val pc = per current |
| 724 |
val isNonZero = current > 0 orelse stack > 0 orelse stackGC > 0 |
| 725 |
val tableInfo = |
| 726 |
if isNonZero orelse (kind = Kind.Count |
| 727 |
andalso (case source of |
| 728 |
Source.NamePos _ => true |
| 729 |
| _ => false)) |
| 730 |
then SOME {per = pc, |
| 731 |
row = Source.toStringMaybeLine source :: row} |
| 732 |
else NONE |
| 733 |
val nodeOptions = |
| 734 |
[Dot.NodeOption.Shape Dot.Box, |
| 735 |
Dot.NodeOption.Label |
| 736 |
(Source.toDotLabel source |
| 737 |
@ (if isNonZero |
| 738 |
then [(concat (List.separate (row, " ")), |
| 739 |
Dot.Center)] |
| 740 |
else [])), |
| 741 |
Dot.NodeOption.Color |
| 742 |
(if !gray |
| 743 |
then DotColor.gray (100 - Real.round (per stack)) |
| 744 |
else DotColor.Black)] |
| 745 |
in |
| 746 |
{nodeOptions = nodeOptions, |
| 747 |
tableInfo = tableInfo} |
| 748 |
end |
| 749 |
val masterOptions = |
| 750 |
Vector.map2 |
| 751 |
(master, masterCount, fn ({source, ...}, a) => |
| 752 |
info (source, a)) |
| 753 |
val splitOptions = |
| 754 |
Vector.map2 |
| 755 |
(split, splitCount, fn ({masterIndex, ...}, a) => |
| 756 |
info (#source (Vector.sub (master, masterIndex)), a)) |
| 757 |
in |
| 758 |
(masterOptions, splitOptions) |
| 759 |
end |
| 760 |
val (masterInfo, splitInfo) = |
| 761 |
case counts of |
| 762 |
Counts.Current ms => |
| 763 |
doit (ms, fn z => {current = z, |
| 764 |
stack = 0, |
| 765 |
stackGC = 0}) |
| 766 |
| Counts.Empty => |
| 767 |
doit ({master = Vector.new (Vector.length master, ()), |
| 768 |
split = Vector.new (Vector.length split, ())}, |
| 769 |
fn () => {current = 0, |
| 770 |
stack = 0, |
| 771 |
stackGC = 0}) |
| 772 |
| Counts.Stack ms => |
| 773 |
doit (ms, fn z => z) |
| 774 |
val keep = !keep |
| 775 |
val keepNodes = |
| 776 |
NodePred.nodes |
| 777 |
(keep, callGraph, fn (n, a) => (! (#mayKeep (nodeInfo n))) a) |
| 778 |
val _ = Vector.foreach (keepNodes, fn n => |
| 779 |
#keep (nodeInfo n) := true) |
| 780 |
(* keep a master node if it is not split and some copy of it is kept. *) |
| 781 |
val keepMaster = Array.new (Vector.length master, false) |
| 782 |
val _ = |
| 783 |
Vector.foreach |
| 784 |
(split, fn {masterIndex, node, ...} => |
| 785 |
let |
| 786 |
val {keep, ...} = nodeInfo node |
| 787 |
val {isSplit, ...} = Vector.sub (master, masterIndex) |
| 788 |
in |
| 789 |
if !keep andalso not isSplit |
| 790 |
then Array.update (keepMaster, masterIndex, true) |
| 791 |
else () |
| 792 |
end) |
| 793 |
datatype keep = T |
| 794 |
val keepGraph: keep Graph.t = Graph.new () |
| 795 |
val {get = nodeOptions: keep Node.t -> NodeOption.t list, |
| 796 |
set = setNodeOptions, ...} = |
| 797 |
Property.getSetOnce (Node.plist, |
| 798 |
Property.initRaise ("options", Node.layout)) |
| 799 |
val tableInfos = ref [] |
| 800 |
fun newNode {nodeOptions: NodeOption.t list, |
| 801 |
tableInfo} = |
| 802 |
let |
| 803 |
val _ = Option.app (tableInfo, fn z => List.push (tableInfos, z)) |
| 804 |
val n = Graph.newNode keepGraph |
| 805 |
val _ = setNodeOptions (n, nodeOptions) |
| 806 |
in |
| 807 |
n |
| 808 |
end |
| 809 |
val masterNodes = |
| 810 |
Vector.tabulate |
| 811 |
(Vector.length master, fn i => |
| 812 |
if Array.sub (keepMaster, i) |
| 813 |
then SOME (newNode (Vector.sub (masterInfo, i))) |
| 814 |
else NONE) |
| 815 |
val splitNodes = |
| 816 |
Vector.mapi |
| 817 |
(split, fn (i, {masterIndex, node, ...}) => |
| 818 |
let |
| 819 |
val {keep, ...} = nodeInfo node |
| 820 |
val {isSplit, ...} = Vector.sub (master, masterIndex) |
| 821 |
in |
| 822 |
if isSplit |
| 823 |
then |
| 824 |
if !keep |
| 825 |
then SOME (newNode (Vector.sub (splitInfo, i))) |
| 826 |
else NONE |
| 827 |
else Vector.sub (masterNodes, masterIndex) |
| 828 |
end) |
| 829 |
val _ = |
| 830 |
Graph.foreachEdge |
| 831 |
(callGraph, fn (from, e) => |
| 832 |
let |
| 833 |
val to = Edge.to e |
| 834 |
fun f n = Vector.sub (splitNodes, #index (nodeInfo n)) |
| 835 |
in |
| 836 |
case (f from, f to) of |
| 837 |
(SOME from, SOME to) => |
| 838 |
(ignore o Graph.addEdge) |
| 839 |
(keepGraph, {from = from, to = to}) |
| 840 |
| _ => () |
| 841 |
end) |
| 842 |
val {get = edgeOptions: keep Edge.t -> EdgeOption.t list ref, ...} = |
| 843 |
Property.get (Edge.plist, Property.initFun (fn _ => ref [])) |
| 844 |
(* Add a dashed edge from A to B if there is path from A to B of length |
| 845 |
* >= 2 going through only ignored nodes. |
| 846 |
*) |
| 847 |
fun newNode (n: unit Node.t): keep Node.t option = |
| 848 |
Vector.sub (splitNodes, #index (nodeInfo n)) |
| 849 |
fun reach (root: unit Node.t, f: keep Node.t -> unit): unit = |
| 850 |
let |
| 851 |
val {get = isKept: keep Node.t -> bool ref, ...} = |
| 852 |
Property.get (Node.plist, Property.initFun (fn _ => ref false)) |
| 853 |
val {get = isSeen: unit Node.t -> bool ref, ...} = |
| 854 |
Property.get (Node.plist, Property.initFun (fn _ => ref false)) |
| 855 |
fun loop n = |
| 856 |
List.foreach |
| 857 |
(Node.successors n, fn e => |
| 858 |
let |
| 859 |
val n = Edge.to e |
| 860 |
val s = isSeen n |
| 861 |
in |
| 862 |
if !s |
| 863 |
then () |
| 864 |
else |
| 865 |
let |
| 866 |
val _ = s := true |
| 867 |
in |
| 868 |
case newNode n of |
| 869 |
NONE => loop n |
| 870 |
| SOME keepN => |
| 871 |
let |
| 872 |
val r = isKept keepN |
| 873 |
in |
| 874 |
if !r |
| 875 |
then () |
| 876 |
else (r := true; f keepN) |
| 877 |
end |
| 878 |
end |
| 879 |
end) |
| 880 |
val _ = |
| 881 |
List.foreach (Node.successors root, fn e => |
| 882 |
let |
| 883 |
val n = Edge.to e |
| 884 |
in |
| 885 |
if Option.isNone (newNode n) |
| 886 |
then loop n |
| 887 |
else () |
| 888 |
end) |
| 889 |
in |
| 890 |
() |
| 891 |
end |
| 892 |
val _ = |
| 893 |
Vector.foreach2 |
| 894 |
(split, splitNodes, fn ({node = from, ...}, z) => |
| 895 |
Option.app |
| 896 |
(z, fn from' => |
| 897 |
(reach (from, fn to => |
| 898 |
let |
| 899 |
val e = Graph.addEdge (keepGraph, {from = from', to = to}) |
| 900 |
val _ = List.push (edgeOptions e, |
| 901 |
EdgeOption.Style Dot.Dashed) |
| 902 |
in |
| 903 |
() |
| 904 |
end)))) |
| 905 |
val _ = Graph.removeDuplicateEdges keepGraph |
| 906 |
val title = |
| 907 |
case !title of |
| 908 |
NONE => concat [aname, " call-stack graph"] |
| 909 |
| SOME s => s |
| 910 |
val _ = |
| 911 |
Option.app |
| 912 |
(!callGraphFile, fn f => |
| 913 |
File.withOut |
| 914 |
(f, fn out => |
| 915 |
Layout.output |
| 916 |
(Graph.layoutDot (keepGraph, |
| 917 |
fn _ => {edgeOptions = ! o edgeOptions, |
| 918 |
nodeOptions = nodeOptions, |
| 919 |
options = [], |
| 920 |
title = title}), |
| 921 |
out))) |
| 922 |
(* Display the table. *) |
| 923 |
val tableRows = |
| 924 |
QuickSort.sortVector |
| 925 |
(Vector.fromList (!tableInfos), fn (z, z') => #per z >= #per z') |
| 926 |
val _ = |
| 927 |
print |
| 928 |
(concat |
| 929 |
(case kind of |
| 930 |
Kind.Alloc => |
| 931 |
[IntInf.toCommaString total, " bytes allocated (", |
| 932 |
IntInf.toCommaString totalGC, " bytes by GC)\n"] |
| 933 |
| Kind.Count => |
| 934 |
[IntInf.toCommaString total, " ticks\n"] |
| 935 |
| Kind.Empty => [] |
| 936 |
| Kind.Time => |
| 937 |
let |
| 938 |
fun t2s i = |
| 939 |
Real.format (Real.fromIntInf i / ticksPerSecond, |
| 940 |
Real.Format.fix (SOME 2)) |
| 941 |
in |
| 942 |
[t2s total, " seconds of CPU time (", |
| 943 |
t2s totalGC, " seconds GC)\n"] |
| 944 |
end)) |
| 945 |
val columnHeads = |
| 946 |
"function" |
| 947 |
:: let |
| 948 |
val pers = |
| 949 |
if profileStack |
| 950 |
then ["cur", "stack", "GC"] |
| 951 |
else ["cur"] |
| 952 |
in |
| 953 |
if !raw |
| 954 |
then List.concatMap (pers, fn p => [p, "raw"]) |
| 955 |
else pers |
| 956 |
end |
| 957 |
val cols = |
| 958 |
(if profileStack then 3 else 1) * (if !raw then 2 else 1) |
| 959 |
val _ = |
| 960 |
let |
| 961 |
open Justify |
| 962 |
in |
| 963 |
outputTable |
| 964 |
(table {columnHeads = SOME columnHeads, |
| 965 |
justs = Left :: List.duplicate (cols, fn () => Right), |
| 966 |
rows = Vector.toListMap (tableRows, #row)}, |
| 967 |
Out.standard) |
| 968 |
end |
| 969 |
in |
| 970 |
() |
| 971 |
end |
| 972 |
|
| 973 |
fun makeOptions {usage} = |
| 974 |
let |
| 975 |
open Popt |
| 976 |
in |
| 977 |
List.map |
| 978 |
([(Normal, "call-graph", " <file>", "write call graph to dot file", |
| 979 |
SpaceString (fn s => callGraphFile := SOME s)), |
| 980 |
(Normal, "graph-title", " <string>", "set call-graph title", |
| 981 |
SpaceString (fn s => title := SOME s)), |
| 982 |
(Normal, "gray", " {false|true}", "gray nodes according to stack %", |
| 983 |
boolRef gray), |
| 984 |
(Normal, "keep", " <exp>", "which functions to display", |
| 985 |
SpaceString (fn s => |
| 986 |
keep := NodePred.fromString s |
| 987 |
handle e => usage (concat ["invalid -keep arg: ", |
| 988 |
Exn.toString e]))), |
| 989 |
(Expert, "long-name", " {true|false}", |
| 990 |
" show long names of functions", |
| 991 |
boolRef longName), |
| 992 |
(Normal, "mlmon", " <file>", "process mlmon files listed in <file>", |
| 993 |
SpaceString (fn s => |
| 994 |
mlmonFiles := |
| 995 |
List.concat [String.tokens (File.contents s, Char.isSpace), |
| 996 |
!mlmonFiles])), |
| 997 |
(Normal, "raw", " {false|true}", "show raw counts", |
| 998 |
boolRef raw), |
| 999 |
(Normal, "show-line", " {false|true}", "show line numbers", |
| 1000 |
boolRef showLine), |
| 1001 |
(Normal, "split", " <regexp>", "split matching functions", |
| 1002 |
SpaceString (fn s => |
| 1003 |
case Regexp.fromString s of |
| 1004 |
NONE => usage (concat ["invalid -split regexp: ", s]) |
| 1005 |
| SOME (r, _) => splitReg := Regexp.or [r, !splitReg])), |
| 1006 |
(Normal, "thresh", " [0.0,100.0]", "-keep (thresh x)", |
| 1007 |
Real (fn x => if x < 0.0 orelse x > 100.0 |
| 1008 |
then usage "invalid -thresh" |
| 1009 |
else keep := NodePred.Atomic (Atomic.Thresh x))), |
| 1010 |
(Normal, "tolerant", " {false|true}", "ignore broken mlmon files", |
| 1011 |
boolRef tolerant)], |
| 1012 |
fn (style, name, arg, desc, opt) => |
| 1013 |
{arg = arg, desc = desc, name = name, opt = opt, style = style}) |
| 1014 |
end |
| 1015 |
|
| 1016 |
val mainUsage = "mlprof [option ...] a.out [mlmon.out ...]" |
| 1017 |
val {parse, usage} = |
| 1018 |
Popt.makeUsage {mainUsage = mainUsage, |
| 1019 |
makeOptions = makeOptions, |
| 1020 |
showExpert = fn () => false} |
| 1021 |
|
| 1022 |
val die = Process.fail |
| 1023 |
|
| 1024 |
fun commandLine (_, args) = |
| 1025 |
let |
| 1026 |
val rest = parse args |
| 1027 |
in |
| 1028 |
case rest of |
| 1029 |
Result.No msg => usage msg |
| 1030 |
| Result.Yes (afile :: files) => |
| 1031 |
let |
| 1032 |
val mlmonFiles = files @ !mlmonFiles |
| 1033 |
val aInfo = AFile.new {afile = afile} |
| 1034 |
val _ = |
| 1035 |
if debug |
| 1036 |
then |
| 1037 |
(print "AFile:\n" |
| 1038 |
; Layout.outputl (AFile.layout aInfo, Out.standard)) |
| 1039 |
else () |
| 1040 |
val profFile = |
| 1041 |
List.fold |
| 1042 |
(mlmonFiles, ProfFile.empty aInfo, |
| 1043 |
fn (mlmonfile, profFile) => |
| 1044 |
ProfFile.merge |
| 1045 |
(profFile, ProfFile.new {mlmonfile = mlmonfile}) |
| 1046 |
handle e => |
| 1047 |
let |
| 1048 |
val msg = |
| 1049 |
concat ["Error loading mlmon file '", mlmonfile, |
| 1050 |
"': ", Exn.toString e] |
| 1051 |
in |
| 1052 |
if !tolerant |
| 1053 |
then |
| 1054 |
(Out.outputl (Out.error, msg) |
| 1055 |
; profFile) |
| 1056 |
else die msg |
| 1057 |
end) |
| 1058 |
val _ = |
| 1059 |
if debug |
| 1060 |
then |
| 1061 |
(print "ProfFile:\n" |
| 1062 |
; Layout.outputl (ProfFile.layout profFile, |
| 1063 |
Out.standard)) |
| 1064 |
else () |
| 1065 |
val _ = display (aInfo, profFile) |
| 1066 |
in |
| 1067 |
() |
| 1068 |
end |
| 1069 |
| Result.Yes _ => usage "wrong number of args" |
| 1070 |
end |
| 1071 |
|
| 1072 |
val main = CommandLine.make commandLine |
| 1073 |
|
| 1074 |
end |