| author | desharna |
| Thu, 02 Oct 2025 11:02:15 +0000 | |
| changeset 83240 | dfa14d921fd2 |
| parent 82979 | 98faa43a2505 |
| permissions | -rw-r--r-- |
| 82978 | 1 |
(* Title: Tools/profiling.thy |
| 78315 | 2 |
Author: Makarius |
|
78314
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
3 |
|
| 78315 | 4 |
Session profiling based on loaded ML image. |
|
78314
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
5 |
*) |
|
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
6 |
|
| 78315 | 7 |
theory Profiling |
|
78314
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
8 |
imports Pure |
|
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
9 |
begin |
|
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
10 |
|
| 82978 | 11 |
ML \<open> |
12 |
signature PROFILING = |
|
13 |
sig |
|
14 |
val locales: theory -> string list |
|
15 |
val locale_thms: theory -> string -> thm list |
|
16 |
val locales_thms: theory -> thm list |
|
17 |
val global_thms: theory -> thm list |
|
18 |
val all_thms: theory -> thm list |
|
19 |
type session_statistics = |
|
20 |
{theories: int,
|
|
21 |
garbage_theories: int, |
|
22 |
locales: int, |
|
23 |
locale_thms: int, |
|
24 |
global_thms: int, |
|
25 |
sizeof_thys_id: int, |
|
26 |
sizeof_thms_id: int, |
|
27 |
sizeof_terms: int, |
|
28 |
sizeof_types: int, |
|
29 |
sizeof_names: int, |
|
30 |
sizeof_spaces: int} |
|
31 |
val session_statistics: string list -> session_statistics |
|
32 |
val main: unit -> unit |
|
33 |
end; |
|
34 |
||
35 |
structure Profiling: PROFILING = |
|
36 |
struct |
|
37 |
||
38 |
(* theory content *) |
|
39 |
||
40 |
fun locales thy = |
|
41 |
let |
|
42 |
val parent_spaces = map Locale.locale_space (Theory.parents_of thy); |
|
43 |
fun add name = |
|
44 |
if exists (fn space => Name_Space.declared space name) parent_spaces then I |
|
45 |
else cons name; |
|
46 |
in fold add (Locale.get_locales thy) [] end; |
|
47 |
||
48 |
fun proper_thm thm = not (Thm.eq_thm_prop (Drule.dummy_thm, thm)); |
|
49 |
||
50 |
fun locale_thms thy loc = |
|
51 |
(Locale.locale_notes thy loc, []) |-> |
|
52 |
(fold (#2 #> fold (#2 #> (fold (#1 #> fold (fn thm => proper_thm thm ? cons thm )))))); |
|
53 |
||
54 |
fun locales_thms thy = |
|
55 |
maps (locale_thms thy) (locales thy); |
|
56 |
||
57 |
fun global_thms thy = |
|
58 |
Facts.dest_static true |
|
59 |
(map Global_Theory.facts_of (Theory.parents_of thy)) (Global_Theory.facts_of thy) |
|
60 |
|> maps #2; |
|
61 |
||
62 |
fun all_thms thy = locales_thms thy @ global_thms thy; |
|
63 |
||
64 |
||
65 |
(* session content *) |
|
66 |
||
67 |
fun session_content thys = |
|
68 |
let |
|
69 |
val thms = maps all_thms thys; |
|
70 |
val thys_id = map Context.theory_id thys; |
|
71 |
val thms_id = map Thm.theory_id thms; |
|
72 |
val terms = |
|
73 |
Termset.dest ((fold o Thm.fold_terms {hyps = true}) Termset.insert thms Termset.empty);
|
|
74 |
val types = Typset.dest ((fold o fold_types) Typset.insert terms Typset.empty); |
|
75 |
val spaces = |
|
76 |
maps (fn f => map f thys) |
|
77 |
[Sign.class_space, |
|
78 |
Sign.type_space, |
|
79 |
Sign.const_space, |
|
80 |
Theory.axiom_space, |
|
81 |
Thm.oracle_space, |
|
82 |
Global_Theory.fact_space, |
|
83 |
Locale.locale_space, |
|
84 |
Attrib.attribute_space o Context.Theory, |
|
85 |
Method.method_space o Context.Theory]; |
|
86 |
val names = Symset.dest (Symset.merges (map (Symset.make o Name_Space.get_names) spaces)); |
|
87 |
in |
|
88 |
{thys_id = thys_id, thms_id = thms_id, terms = terms,
|
|
89 |
types = types, names = names, spaces = spaces} |
|
90 |
end; |
|
91 |
||
92 |
fun sizeof1_diff (a, b) = ML_Heap.sizeof1 a - ML_Heap.sizeof1 b; |
|
93 |
fun sizeof_list_diff (a, b) = ML_Heap.sizeof_list a - ML_Heap.sizeof_list b; |
|
94 |
fun sizeof_diff (a, b) = ML_Heap.sizeof a - ML_Heap.sizeof b; |
|
95 |
||
96 |
||
97 |
(* session statistics *) |
|
98 |
||
99 |
type session_statistics = |
|
100 |
{theories: int,
|
|
101 |
garbage_theories: int, |
|
102 |
locales: int, |
|
103 |
locale_thms: int, |
|
104 |
global_thms: int, |
|
105 |
sizeof_thys_id: int, |
|
106 |
sizeof_thms_id: int, |
|
107 |
sizeof_terms: int, |
|
108 |
sizeof_types: int, |
|
109 |
sizeof_names: int, |
|
110 |
sizeof_spaces: int}; |
|
111 |
||
112 |
fun session_statistics theories : session_statistics = |
|
113 |
let |
|
114 |
val theories_member = Symtab.defined (Symtab.make_set theories) o Context.theory_long_name; |
|
115 |
||
116 |
val context_thys = |
|
117 |
#contexts (Context.finish_tracing ()) |
|
118 |
|> map_filter (fn (Context.Theory thy, _) => SOME thy | _ => NONE); |
|
119 |
||
120 |
val loader_thys = map Thy_Info.get_theory (Thy_Info.get_names ()); |
|
121 |
val loaded_thys = filter theories_member loader_thys; |
|
122 |
val loaded_context_thys = filter theories_member context_thys; |
|
123 |
||
124 |
val all_thys = loader_thys @ context_thys; |
|
125 |
val base_thys = filter_out theories_member all_thys; |
|
126 |
||
127 |
val {thys_id = all_thys_id, thms_id = all_thms_id, terms = all_terms,
|
|
128 |
types = all_types, names = all_names, spaces = all_spaces} = session_content all_thys; |
|
129 |
val {thys_id = base_thys_id, thms_id = base_thms_id, terms = base_terms,
|
|
130 |
types = base_types, names = base_names, spaces = base_spaces} = session_content base_thys; |
|
131 |
||
132 |
val n = length loaded_thys; |
|
133 |
val m = (case length loaded_context_thys of 0 => 0 | l => l - n); |
|
134 |
in |
|
135 |
{theories = n,
|
|
136 |
garbage_theories = m, |
|
137 |
locales = Integer.sum (map (length o locales) loaded_thys), |
|
138 |
locale_thms = Integer.sum (map (length o locales_thms) loaded_thys), |
|
139 |
global_thms = Integer.sum (map (length o global_thms) loaded_thys), |
|
140 |
sizeof_thys_id = |
|
141 |
sizeof1_diff ((all_thys_id, all_thms_id), (base_thys_id, all_thms_id)) - |
|
142 |
sizeof_list_diff (all_thys_id, base_thys_id), |
|
143 |
sizeof_thms_id = |
|
144 |
sizeof1_diff ((all_thms_id, all_thys_id), (base_thms_id, all_thys_id)) - |
|
145 |
sizeof_list_diff (all_thms_id, base_thms_id), |
|
146 |
sizeof_terms = |
|
147 |
sizeof1_diff ((all_terms, all_types, all_names), (base_terms, all_types, all_names)) - |
|
148 |
sizeof_list_diff (all_terms, base_terms), |
|
149 |
sizeof_types = |
|
150 |
sizeof1_diff ((all_types, all_names), (base_types, all_names)) - |
|
151 |
sizeof_list_diff (all_types, base_types), |
|
152 |
sizeof_spaces = |
|
153 |
sizeof1_diff ((all_spaces, all_names), (base_spaces, all_names)) - |
|
154 |
sizeof_list_diff (all_spaces, base_spaces), |
|
155 |
sizeof_names = sizeof_diff (all_names, base_names)} |
|
156 |
end; |
|
157 |
||
158 |
||
159 |
(* main entry point for external process *) |
|
160 |
||
161 |
local |
|
162 |
||
163 |
val decode_args : string list XML.Decode.T = |
|
164 |
let open XML.Decode in list string end; |
|
165 |
||
166 |
val encode_result : session_statistics XML.Encode.T = |
|
167 |
(fn {theories = a,
|
|
168 |
garbage_theories = b, |
|
169 |
locales = c, |
|
170 |
locale_thms = d, |
|
171 |
global_thms = e, |
|
172 |
sizeof_thys_id = f, |
|
173 |
sizeof_thms_id = g, |
|
174 |
sizeof_terms = h, |
|
175 |
sizeof_types = i, |
|
176 |
sizeof_names = j, |
|
177 |
sizeof_spaces = k} => ((a, (b, (c, (d, (e, (f, (g, (h, (i, (j, k)))))))))))) #> |
|
178 |
let open XML.Encode in |
|
179 |
pair int (pair int (pair int (pair int (pair int (pair int (pair int |
|
180 |
(pair int (pair int (pair int int))))))))) |
|
181 |
end; |
|
182 |
||
183 |
in |
|
184 |
||
185 |
fun main () = |
|
|
82979
98faa43a2505
prefer system options, which are easier to change on the spot;
wenzelm
parents:
82978
diff
changeset
|
186 |
(case Options.default_string \<^system_option>\<open>profiling_dir\<close> of |
| 82978 | 187 |
"" => () |
188 |
| dir_name => |
|
189 |
let |
|
190 |
val dir = Path.explode dir_name; |
|
191 |
val args = decode_args (YXML.parse_body (File.read (dir + \<^path>\<open>args.yxml\<close>))); |
|
192 |
val result = session_statistics args; |
|
193 |
in File.write (dir + \<^path>\<open>result.yxml\<close>) (YXML.string_of_body (encode_result result)) end); |
|
194 |
||
195 |
end; |
|
196 |
||
197 |
end; |
|
198 |
\<close> |
|
199 |
||
| 78315 | 200 |
ML_command \<open>Profiling.main ()\<close> |
|
78314
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
201 |
|
|
1588bec693c2
copy/rename files from private autocorres version e45b9b680d3e;
wenzelm
parents:
diff
changeset
|
202 |
end |