author | wenzelm |
Thu, 15 Aug 2024 12:43:17 +0200 | |
changeset 80712 | 05b16602a683 |
parent 73843 | 014b944f4972 |
permissions | -rw-r--r-- |
62508
d0b68218ea55
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents:
62501
diff
changeset
|
1 |
(* Title: Pure/ML/ml_profiling.ML |
61715
5dc95d957569
speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
5dc95d957569
speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff
changeset
|
3 |
|
73840 | 4 |
ML profiling (via Poly/ML run-time system). |
61715
5dc95d957569
speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff
changeset
|
5 |
*) |
5dc95d957569
speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff
changeset
|
6 |
|
73840 | 7 |
signature BASIC_ML_PROFILING = |
62824 | 8 |
sig |
73834 | 9 |
val profile_time: ('a -> 'b) -> 'a -> 'b |
10 |
val profile_time_thread: ('a -> 'b) -> 'a -> 'b |
|
11 |
val profile_allocations: ('a -> 'b) -> 'a -> 'b |
|
62824 | 12 |
end; |
13 |
||
73840 | 14 |
signature ML_PROFILING = |
15 |
sig |
|
16 |
val check_mode: string -> unit |
|
17 |
val profile: string -> ('a -> 'b) -> 'a -> 'b |
|
18 |
include BASIC_ML_PROFILING |
|
19 |
end; |
|
20 |
||
62824 | 21 |
structure ML_Profiling: ML_PROFILING = |
61885
acdfc76a6c33
more explicit ML profiling, with official Isabelle output;
wenzelm
parents:
61715
diff
changeset
|
22 |
struct |
acdfc76a6c33
more explicit ML profiling, with official Isabelle output;
wenzelm
parents:
61715
diff
changeset
|
23 |
|
73840 | 24 |
(* mode *) |
25 |
||
26 |
val modes = |
|
27 |
Symtab.make |
|
28 |
[("time", PolyML.Profiling.ProfileTime), |
|
29 |
("time_thread", PolyML.Profiling.ProfileTimeThisThread), |
|
30 |
("allocations", PolyML.Profiling.ProfileAllocations)]; |
|
31 |
||
32 |
fun get_mode kind = |
|
33 |
(case Symtab.lookup modes kind of |
|
34 |
SOME mode => mode |
|
35 |
| NONE => error ("Bad profiling mode: " ^ quote kind)); |
|
36 |
||
37 |
fun check_mode "" = () |
|
38 |
| check_mode kind = ignore (get_mode kind); |
|
39 |
||
40 |
||
41 |
(* profile *) |
|
42 |
||
73835 | 43 |
fun print_entry count name = |
44 |
let |
|
45 |
val c = string_of_int count; |
|
73843 | 46 |
val prefix = Symbol.spaces (Int.max (0, 12 - size c)); |
73835 | 47 |
in prefix ^ c ^ " " ^ name end; |
48 |
||
73840 | 49 |
fun profile "" f x = f x |
50 |
| profile kind f x = |
|
51 |
let |
|
52 |
val mode = get_mode kind; |
|
53 |
fun output entries = |
|
54 |
(case fold (curry (op +) o fst) entries 0 of |
|
55 |
0 => () |
|
56 |
| total => |
|
57 |
let |
|
58 |
val body = entries |
|
59 |
|> sort (int_ord o apply2 fst) |
|
60 |
|> map (fn (count, name) => |
|
61 |
let val markup = Markup.ML_profiling_entry {name = name, count = count} |
|
62 |
in XML.Elem (markup, [XML.Text (print_entry count name ^ "\n")]) end); |
|
63 |
val head = XML.Text ("profile_" ^ kind ^ ":\n"); |
|
64 |
val foot = XML.Text (print_entry total "TOTAL"); |
|
65 |
val msg = XML.Elem (Markup.ML_profiling kind, head :: body @ [foot]); |
|
66 |
in tracing (YXML.string_of msg) end); |
|
67 |
in PolyML.Profiling.profileStream output mode f x end; |
|
61885
acdfc76a6c33
more explicit ML profiling, with official Isabelle output;
wenzelm
parents:
61715
diff
changeset
|
68 |
|
73840 | 69 |
fun profile_time f = profile "time" f; |
70 |
fun profile_time_thread f = profile "time_thread" f; |
|
71 |
fun profile_allocations f = profile "allocations" f; |
|
61885
acdfc76a6c33
more explicit ML profiling, with official Isabelle output;
wenzelm
parents:
61715
diff
changeset
|
72 |
|
acdfc76a6c33
more explicit ML profiling, with official Isabelle output;
wenzelm
parents:
61715
diff
changeset
|
73 |
end; |
73834 | 74 |
|
73840 | 75 |
structure Basic_ML_Profiling: BASIC_ML_PROFILING = ML_Profiling; |
76 |
open Basic_ML_Profiling; |