File: statistics.pl

package info (click to toggle)
swi-prolog 3.3.0beta9-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,600 kB
  • ctags: 6,554
  • sloc: ansic: 50,797; perl: 12,880; sh: 1,419; makefile: 524; awk: 14
file content (141 lines) | stat: -rw-r--r-- 3,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
/*  $Id: statistics.pl,v 1.2 1999/12/20 14:14:07 jan Exp $

    Part of SWI-Prolog
    Designed and implemented by Jan Wielemaker

    Copyright (C) 1999 SWI, University of Amsterdam. All rights reserved.
*/


:- module(prolog_statistics,
	  [ time/1,
	    profiler/2,
	    show_profile/1,
	    profile/3
	  ]).

:- module_transparent
	time/1.

%	time(:Goal)
%
%	Time the execution of Goal.  Possible choice-points of Goal are
%	removed.

time(Goal) :-
	statistics(cputime, OldTime), 
	statistics(inferences, OldInferences), 
	(   catch(Goal, E, true)
	->  Result = yes
	;   Result = no
	),
	statistics(inferences, NewInferences), 
	statistics(cputime, NewTime), 
	UsedTime is NewTime - OldTime, 
	UsedInf  is NewInferences - OldInferences - 3, 
	(   UsedTime =:= 0
	->  Lips = 'Infinite'
	;   Lips is integer(UsedInf / UsedTime)
	), 
	print_message(informational, time(UsedInf, UsedTime, Lips)),
	(   nonvar(E)
	->  throw(E)
	;   Result == yes
	).

%    profile(-Old, +New)
%    change or query profiling status.

profiler(Old, New) :-
	'$profile'(OldInt, OldInt), 
	map_profile(Old, OldInt), 
	atom(New), 
	map_profile(New, NewInt), !, 
	'$profile'(_, NewInt).
profiler(_, New) :-
	throw(error(domain_error(profile_type, New), _)).


map_profile(off, 	0).
map_profile(cumulative, 1).
map_profile(plain, 	2).


%   show_profile(N)
%   Show the top N functions' profile. Negative numbers or 0 show ALL
%   functions that have been called during profiling.

show_profile(N) :-
	findall( triple(Perc, Calls, Module:Head), 
		 enum_profile_count(Module:Head, Calls, Perc), 
		 List), 
	sort(List, Sorted), 
	reverse(Sorted, HighFirst), 
	format('~w~t~w =~41|~t~w~57| = ~w ~t~w~79|~n',
	       [ 'Predicate', 'Box Entries', 'Calls+Redos'
	       , 'Exits+Fails', 'Time'
	       ]),
	format('~61t~79|~n'),
	show_profile(N, HighFirst).

enum_profile_count(Head, Calls, Perc) :-
	current_predicate(_, Head), 
	\+ predicate_property(Head, imported_from(_)), 
	profile_count(Head, Calls, Perc), 
	Calls \== 0.

show_profile(0, _) :- !.
show_profile(_, []) :- !.
show_profile(N, [triple(Prom, Total, Pred)|Rest]) :-
	predicate_name(Pred, Name),
	profile_box(Pred, Calls, Redos, Exits, Fails),
	format('~w~t~D =~41|~t~D+~D~57| = ~D+~D ~t~1d%~79|~n',
	       [Name, Total, Calls, Redos, Exits, Fails, Prom]), 
	succ(M, N), 
	show_profile(M, Rest).

:- module_transparent
	profile/3.

profile(Goal, Style, N) :-
	profiler(_, off), 
	reset_profiler, 
	profiler(_, Style), 
	(   catch(time(Goal), E, fail)
	->  Rval = true
	;   Rval = fail
	),
	profiler(_, off), 
	show_profile(N), !, 
	(   nonvar(E)
	->  throw(E)
	;   Rval == true
	).


		 /*******************************
		 *	      MESSAGES		*
		 *******************************/

:- multifile
	prolog:message/3.

prolog:message(time(UsedInf, UsedTime, Lips)) -->
	[ '~D inferences in ~2f seconds (~w Lips)'-[UsedInf, UsedTime, Lips] ].


:- module_transparent
	predicate_name/2.

%	predicate_name(+Head, -String)
%	Convert `Head' into a predicate name.

predicate_name(Goal, String) :-
	'$strip_module'(Goal, Module, Head), 
	functor(Head, Name, Arity), 
	(   memberchk(Module, [user, system])
	->  sformat(String, '~w/~w',	[Name, Arity])
	;   sformat(String, '~w:~w/~w',	[Module, Name, Arity])
	).