YAP 7.1.0
statistics.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: statistics.yap *
12* Last rev: 8/2/88 *
13* mods: *
14* comments: statistics on Prolog status *
15* *
16*************************************************************************/
17
18/**
19 * @file statistics.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
21 * @date Thu Oct 19 12:13:51 2017
22 *
23 * @brief System Status
24 *
25 * @defgroup Statistics System Status
26 * @ingroup Builtins
27 *
28 *
29*/
30
31:- system_module( '$_statistics', [key_statistics/3,
32 statistics/0,
34 time/1], []).
35
36:- '$do_error'/2use_system_module( '$_errors', []).
37
38%%% User interface for statistics
39
40/** @pred statistics/0
41
42
43Send to the current user error stream general information on space used and time
44spent by the system.
45
46```
47?- statistics.
48memory (total) 4784124 bytes
49 program space 3055616 bytes: 1392224 in use, 1663392 free
50 2228132 max
51 stack space 1531904 bytes: 464 in use, 1531440 free
52 global stack: 96 in use, 616684 max
53 local stack: 368 in use, 546208 max
54 trail stack 196604 bytes: 8 in use, 196596 free
55
56 0.010 sec. for 5 code, 2 stack, and 1 trail space overflows
57 0.130 sec. for 3 garbage collections which collected 421000 bytes
58 0.000 sec. for 0 atom garbage collections which collected 0 bytes
59 0.880 sec. runtime
60 1.020 sec. cputime
61 25.055 sec. elapsed time
62
63```
64The example shows how much memory the system spends. Memory is divided
65into Program Space, Stack Space and Trail. In the example we have 3MB
66allocated for program spaces, with less than half being actually
67used. YAP also shows the maximum amount of heap space having been used
68which was over 2MB.
69
70The stack space is divided into two stacks which grow against each
71other. We are in the top level so very little stack is being used. On
72the other hand, the system did use a lot of global and local stack
73during the previous execution (we refer the reader to a WAM tutorial in
74order to understand what are the global and local stacks).
75
76YAP also shows information on how many memory overflows and garbage
77collections the system executed, and statistics on total execution
78time. Cputime includes all running time, runtime excludes garbage
79collection and stack overflow time.
80
81
82*/
83use_system_module :-
84 '$runtime'(Runtime,_),
85 '$cputime'(CPUtime,_),
86 '$systime'(SYStime,_),
87 '$walltime'(Walltime,_),
88 '$statistics_heap_info'(HpSpa, HpInUse),
89 '$statistics_heap_max'(HpMax),
90 '$statistics_trail_info'(TrlSpa, TrlInUse),
91 '$statistics_trail_max'(TrlMax),
92 '$statistics_stacks_info'(StkSpa, GlobInU, LocInU),
93 '$statistics_global_max'(GlobMax),
94 '$statistics_local_max'(LocMax),
95 '$inform_heap_overflows'(NOfHO,TotHOTime),
96 '$inform_stack_overflows'(NOfSO,TotSOTime),
97 '$inform_trail_overflows'(NOfTO,TotTOTime),
98 '$inform_gc'(NOfGC,TotGCTime,TotGCSize),
99 '$inform_agc'(NOfAGC,TotAGCTime,TotAGCSize),
100 '$statistics'(Runtime,CPUtime,SYStime,Walltime,HpSpa,HpInUse,HpMax,TrlSpa, TrlInUse,TrlMax,StkSpa, GlobInU, LocInU,GlobMax,LocMax,NOfHO,TotHOTime,NOfSO,TotSOTime,NOfTO,TotTOTime,NOfGC,TotGCTime,TotGCSize,NOfAGC,TotAGCTime,TotAGCSize).
101
102'$statistics'(Runtime,CPUtime,SYStime,Walltime,HpSpa,HpInUse,HpMax,TrlSpa, TrlInUse,_TrlMax,StkSpa, GlobInU, LocInU,GlobMax,LocMax,NOfHO,TotHOTime,NOfSO,TotSOTime,NOfTO,TotTOTime,NOfGC,TotGCTime,TotGCSize,NOfAGC,TotAGCTime,TotAGCSize) :-
103 TotalMemory is HpSpa+StkSpa+TrlSpa,
104 format(user_error,'memory (total)~t~d bytes~35+~n', [TotalMemory]),
105 format(user_error,' program space~t~d bytes~35+', [HpSpa]),
106 format(user_error,':~t ~d in use~19+', [HpInUse]),
107 HpFree is HpSpa-HpInUse,
108 format(user_error,',~t ~d free~19+~n', [HpFree]),
109 format(user_error,'~t ~d max~73+~n', [HpMax]),
110 format(user_error,' stack space~t~d bytes~35+', [StkSpa]),
111 StackInUse is GlobInU+LocInU,
112 format(user_error,':~t ~d in use~19+', [StackInUse]),
113 StackFree is StkSpa-StackInUse,
114 format(user_error,',~t ~d free~19+~n', [StackFree]),
115 format(user_error,' global stack:~t~35+', []),
116 format(user_error,' ~t ~d in use~19+', [GlobInU]),
117 format(user_error,',~t ~d max~19+~n', [GlobMax]),
118 format(user_error,' local stack:~t~35+', []),
119 format(user_error,' ~t ~d in use~19+', [LocInU]),
120 format(user_error,',~t ~d max~19+~n', [LocMax]),
121 format(user_error,' trail stack~t~d bytes~35+', [TrlSpa]),
122 format(user_error,':~t ~d in use~19+', [TrlInUse]),
123 TrlFree is TrlSpa-TrlInUse,
124 format(user_error,',~t ~d free~19+~n', [TrlFree]),
125 OvfTime is (TotHOTime+TotSOTime+TotTOTime)/1000,
126 format(user_error,'~n~t~3f~12+ sec. for ~w code, ~w stack, and ~w trail space overflows~n',
127 [OvfTime,NOfHO,NOfSO,NOfTO]),
128 TotGCTimeF is float(TotGCTime)/1000,
129 format(user_error,'~t~3f~12+ sec. for ~w garbage collections which collected ~d bytes~n',
130 [TotGCTimeF,NOfGC,TotGCSize]),
131 TotAGCTimeF is float(TotAGCTime)/1000,
132 format(user_error,'~t~3f~12+ sec. for ~w atom garbage collections which collected ~d bytes~n',
133 [TotAGCTimeF,NOfAGC,TotAGCSize]),
134 RTime is float(Runtime)/1000,
135 format(user_error,'~t~3f~12+ sec. runtime~n', [RTime]),
136 CPUTime is float(CPUtime)/1000,
137 format(user_error,'~t~3f~12+ sec. cputime~n', [CPUTime]),
138 SYSTime is float(SYStime)/1000,
139 format(user_error,'~t~3f~12+ sec. systime~n', [SYSTime]),
140 WallTime is float(Walltime)/1000,
141 format(user_error,'~t~3f~12+ sec. elapsed time~n~n', [WallTime]),
142 format.
143'$statistics'(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_).
144
145/** @pred statistics(? _Param_,- _Info_)
146
147Gives statistical information on the system parameter given by first
148argument:
149
150
151
152+ atoms
153
154`[ _NumberOfAtoms_, _SpaceUsedBy Atoms_]`
155
156
157This gives the total number of atoms `NumberOfAtoms` and how much
158space they require in bytes, _SpaceUsedBy Atoms_.
159
160+ cputime
161
162`[ _Time since Boot_, _Time From Last Call to Cputime_]`
163
164
165This gives the total cputime in milliseconds spent executing Prolog code,
166garbage collection and stack shifts time included.
167
168+ dynamic_code
169
170`[ _Clause Size_, _Index Size_, _Tree Index Size_, _Choice Point Instructions Size_, _Expansion Nodes Size_, _Index Switch Size_]`
171
172
173Size of static code in YAP in bytes: _Clause Size_, the number of
174bytes allocated for clauses, plus
175 _Index Size_, the number of bytes spent in the indexing code. The
176indexing code is divided into main tree, _Tree Index Size_,
177tables that implement choice-point manipulation, _Choice xsPoint Instructions Size_, tables that cache clauses for future expansion of the index
178tree, _Expansion Nodes Size_, and
179tables such as hash tables that select according to value, _Index Switch Size_.
180
181+ garbage_collection
182
183`[ _Number of GCs_, _Total Global Recovered_, _Total Time Spent_]`
184
185
186Number of garbage collections, amount of space recovered in kbytes, and
187total time spent doing garbage collection in milliseconds. More detailed
188information is available using `yap_flag(gc_trace,verbose)`.
189
190+ global_stack
191
192`[ _Global Stack Used_, _Execution Stack Free_]`
193
194
195Space in kbytes currently used in the global stack, and space available for
196expansion by the local and global stacks.
197
198+ local_stack
199
200`[ _Local Stack Used_, _Execution Stack Free_]`
201
202
203Space in kbytes currently used in the local stack, and space available for
204expansion by the local and global stacks.
205
206+ heap
207
208`[ _Heap Used_, _Heap Free_]`
209
210
211Total space in kbytes not recoverable
212in backtracking. It includes the program code, internal data base, and,
213atom symbol table.
214
215+ program
216
217`[ _Program Space Used_, _Program Space Free_]`
218
219
220Equivalent to heap.
221
222+ runtime
223
224`[ _Time since Boot_, _Time From Last Call to Runtime_]`
225
226
227This gives the total cputime in milliseconds spent executing Prolog
228code, not including garbage collections and stack shifts. Note that
229until YAP4.1.2 the runtime statistics would return time spent on
230garbage collection and stack shifting.
231
232+ stack_shifts
233
234`[ _Number of Heap Shifts_, _Number of Stack Shifts_, _Number of Trail Shifts_]`
235
236
237Number of times YAP had to
238expand the heap, the stacks, or the trail. More detailed information is
239available using `yap_flag(gc_trace,verbose)`.
240
241+ static_code
242
243`[ _Clause Size_, _Index Size_, _Tree Index Size_, _Expansion Nodes Size_, _Index Switch Size_]`
244
245
246Size of static code in YAP in bytes: _Clause Size_, the number of
247bytes allocated for clauses, plus
248 _Index Size_, the number of bytes spent in the indexing code. The
249indexing code is divided into a main tree, _Tree Index Size_, table that cache clauses for future expansion of the index
250tree, _Expansion Nodes Size_, and and
251tables such as hash tables that select according to value, _Index Switch Size_.
252
253+ trail
254
255`[ _Trail Used_, _Trail Free_]`
256
257
258Space in kbytes currently being used and still available for the trail.
259
260+ walltime
261
262`[ _Time since Boot_, _Time From Last Call to Walltime_]`
263
264
265This gives the clock time in milliseconds since starting Prolog.
266
267
268
269
270*/
271statistics(runtime,[T,L]) :-
272 '$runtime'(T,L).
273statistics(cputime,[T,L]) :-
274 '$cputime'(T,L).
275statistics(walltime,[T,L]) :-
276 '$walltime'(T,L).
277statistics(threads,NT) :-
278 '$nof_threads'(NT).
279statistics(threads_created,TC) :-
280 '$nof_threads_created'(TC).
281statistics(thread_cputime,TR) :-
282 '$thread_runtime'(TR).
283%statistics(core,[_]).
284%statistics(memory,[_]).
285statistics(heap,[Hp,HpF]) :-
286 '$statistics_heap_info'(HpM, Hp),
287 HpF is HpM-Hp.
288statistics(program,Info) :-
289 statistics(heap,Info).
290statistics(global_stack,[GlobInU,GlobFree]) :-
291 '$statistics_stacks_info'(StkSpa, GlobInU, LocInU),
292 GlobFree is StkSpa-GlobInU-LocInU.
293statistics(local_stack,[LocInU,LocFree]) :-
294 '$statistics_stacks_info'(StkSpa, GlobInU, LocInU),
295 LocFree is StkSpa-GlobInU-LocInU.
296statistics(trail,[TrlInUse,TrlFree]) :-
297 '$statistics_trail_info'(TrlSpa, TrlInUse),
298 TrlFree is TrlSpa-TrlInUse.
299statistics(garbage_collection,[NOfGC,TotGCSize,TotGCTime]) :-
300 '$inform_gc'(NOfGC,TotGCTime,TotGCSize).
301statistics(stack_shifts,[NOfHO,NOfSO,NOfTO]) :-
302 '$inform_heap_overflows'(NOfHO,_),
303 '$inform_stack_overflows'(NOfSO,_),
304 '$inform_trail_overflows'(NOfTO,_).
305statistics(atoms,[NOf,SizeOf]) :-
306 '$statistics_atom_info'(NOf,SizeOf).
307statistics(static_code,[ClauseSize, IndexSize, TreeIndexSize, ExtIndexSize, SWIndexSize]) :-
308 '$statistics_db_size'(ClauseSize, TreeIndexSize, ExtIndexSize, SWIndexSize),
309 IndexSize is TreeIndexSize+ ExtIndexSize+ SWIndexSize.
310statistics(dynamic_code,[ClauseSize,IndexSize, TreeIndexSize, CPIndexSize, ExtIndexSize, SWIndexSize]) :-
311 '$statistics_lu_db_size'(ClauseSize, TreeIndexSize, CPIndexSize, ExtIndexSize, SWIndexSize),
312 IndexSize is TreeIndexSize+CPIndexSize+ ExtIndexSize+ SWIndexSize.
313
314/** @pred key_statistics(+ _K_,- _Entries_,- _TotalSize_)
315
316Returns several statistics for a key _K_. Currently, it says how
317many entries we have for that key, _Entries_, what is the
318total size spent on this key.
319
320
321*/
322key_statistics(Key, NOfEntries, TotalSize) :-
323 key_statistics(Key, NOfEntries, ClSize, IndxSize),
324 TotalSize is ClSize+IndxSize.
325
326
327%% time(:Goal)
328%
329% Time the execution of Goal. Possible choice-points of Goal are removed.
330% Based on the SWI-Prolog definition minus reporting the number of inferences,
331% which YAP does not currently supports
332
333/** @pred time(: _Goal_)
334
335
336Prints the CPU time and the wall time for the execution of _Goal_.
337Possible choice-points of _Goal_ are removed. Based on the SWI-Prolog
338definition (minus reporting the number of inferences, which YAP currently
339does not support).
340
341
342*/
343:- meta_predicate time(0).
344
345time(Goal) :-
346 var(Goal),
347 '$do_error'(instantiation_error,time(Goal)).
348time(_:Goal) :-
349 var(Goal),
350 '$do_error'(instantiation_error,time(Goal)).
351time(Goal) :- \+ must_be_callable(Goal), must_be_callable,
352 '$do_error'(type_error(callable,Goal),time(Goal)).
353time(Goal) :-
354 statistics(walltime, _),
355 statistics(cputime, _),
356 ( catch(Goal, E, true)
357 -> Result = catch
358 ; Result = catch
359 ),
360 statistics(cputime, [_, Time]),
361 statistics(walltime, [_, Wall]),
362 ( Time =:= 0
363 -> CPU = 'Inf'
364 ; CPU is truncate(Time/Wall*100)
365 ),
366 TimeSecs is Time/1000,
367 WallSecs is Wall/1000,
368 format(user_error,'% ~3f CPU in ~3f seconds (~|~t~w~3+% CPU)~n', [TimeSecs, WallSecs, CPU]),
369 ( nonvar(E)
370 -> throw(E)
371 ; Result == throw
372 ).
catch( : Goal,+ Exception,+ Action)
key_statistics(+ K,- Entries,- TotalSize)
must_be_callable( ?_Goal_ )
statistics(? Param,- Info)
Definition: statistics.yap:83
throw(+ Ball)
time(:Goal) % % Time the execution of Goal
key_statistics(+ K,- Entries,- Size,- IndexSize)
float( T)
nonvar( T)
var( T)