YAP 7.1.0
callcount.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: callcount.yap *
12* Last rev: 8/2/02 *
13* mods: *
14* comments: Some profiling predicates available in yap *
15* *
16*************************************************************************/
17
18%% @{
19
20/**
21 @file callcount.yap
22 @short support call counting.
23
24 @defgroup Profiling Profiling Prolog Programs
25 @ingroup extensions
26
27YAP includes two profilers. The count profiler keeps information on the
28number of times a predicate was called. This information can be used to
29detect what are the most commonly called predicates in the program. The
30count profiler can be compiled by setting YAP's flag profiling
31to `on`. The time-profiler is a `gprof` profiler, and counts
32how many ticks are being spent on specific predicates, or on other
33system functions such as internal data-base accesses or garbage collects.
34
35The YAP profiling sub-system is currently under
36development. Functionality for this sub-system will increase with newer
37implementation.
38
39
40 */
41
42%% @{
43
44/** @defgroup Call_Counting Counting Calls
45@ingroup Profiling
46
47Predicates compiled with YAP's flag call_counting set to
48`on` update counters on the numbers of calls and of
49retries. Counters are actually decreasing counters, so that they can be
50used as timers. Three counters are available:
51
52+ `calls`: number of predicate calls since execution started or since
53system was reset;
54+ `retries`: number of retries for predicates called since
55execution started or since counters were reset;
56+ `calls_and_retries`: count both on predicate calls and
57retries.
58
59These counters can be used to find out how many calls a certain
60goal takes to execute. They can also be used as timers.
61
62The code for the call counters piggybacks on the profiling
63code. Therefore, activating the call counters also activates the profiling
64counters.
65
66These are the predicates that access and manipulate the call counters.
67*/
68
69:- system_module( '$_callcount', [call_count/3,
71 call_count_reset/0], []).
72
73:- '$do_error'/2use_system_module( '$_errors', []).
74
75
76/** @pred call_count_data(- _Calls_, - _Retries_, - _CallsAndRetries_)
77
78
79Give current call count data. The first argument gives the current value
80for the _Calls_ counter, next the _Retries_ counter, and last
81the _CallsAndRetries_ counter.
82
83*/
84call_count_data(Calls, Retries, Both) :-
85 '$call_count_info'(Calls, Retries, Both).
86
87/** @pred call_count_reset
88
89
90Reset call count counters. All timers are also reset.
91
92*/
93'$call_count_info' :-
94 '$call_count_reset'.
95
96/** @pred call_count(? _CallsMax_, ? _RetriesMax_, ? _CallsAndRetriesMax_)
97
98
99Set call counters as timers. YAP will generate an exception
100if one of the instantiated call counters decreases to 0:
101
102+ _CallsMax_
103
104 throw the exception `call_counter` when the
105counter `calls` reaches 0;
106
107+ _RetriesMax_
108
109 throw the exception `retry_counter` when the
110counter `retries` reaches 0;
111
112+ _CallsAndRetriesMax_
113
114 throw the exception
115`call_and_retry_counter` when the counter `calls_and_retries`
116reaches 0.
117
118 YAP will ignore counters that are called with unbound arguments.
119
120Next, we show a simple example of how to use call counters:
121
122```
123 ?- prolog_flag(call_counting,on), [-user]. l :- l. end_of_file. yap_flag(call_counting,off).
124
125yes
126
127yes
128 ?- catch((call_count(10000,_,_),l),call_counter,format("limit_exceeded.~n",[])).
129
130limit_exceeded.
131
132yes
133```
134Notice that we first compile the looping predicate `l/0` with
135call_counting `on`. Next, we catch/3 to handle an
136exception when `l/0` performs more than 10000 reductions.
137
138
139 */
140call_count(Calls, Retries, Both) :-
141 '$check_if_call_count_on'(Calls, CallsOn),
142 '$check_if_call_count_on'(Retries, RetriesOn),
143 '$check_if_call_count_on'(Both, BothOn),
144 '$call_count_set'(Calls, CallsOn, Retries, RetriesOn, Both, BothOn).
145
146'$check_if_call_count_on'(Calls, 1) :- integer(Calls), integer.
147'$check_if_call_count_on'(Calls, 0) :- var(Calls), var.
148'$check_if_call_count_on'(Calls, A) :-
149 '$do_error'(type_error(integer,Calls),call_count(A)).
150
151%% @}
152
153/**
154@}
155*/
156
157
call_count(? CallsMax, ? RetriesMax, ? CallsAndRetriesMax)
Definition: callcount.yap:93
call_count_data(- Calls, - Retries, - CallsAndRetries)
integer( T)
var( T)