Include Paulo Moura's Logtalk OO LP system
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@53 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
19
Logtalk/examples/profiling/NOTES
Normal file
19
Logtalk/examples/profiling/NOTES
Normal file
@@ -0,0 +1,19 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
To load all objects in this example consult the profiling.loader utility
|
||||
file.
|
||||
|
||||
You will also need to load the following files in the library directory:
|
||||
types.loader, events.loader, metapredicates.loader, and dates.loader (note
|
||||
that the *.loader files are Prolog files).
|
||||
|
||||
This is a very simple example of the use of events and monitors to make
|
||||
profilers for an application. It's easy to modify to make it do much more.
|
||||
For instance, most Prolog compilers give you access to data concerning space
|
||||
usage (stacks, heap, etc).
|
104
Logtalk/examples/profiling/SCRIPT
Normal file
104
Logtalk/examples/profiling/SCRIPT
Normal file
@@ -0,0 +1,104 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
%% message_counter example
|
||||
|
||||
|
||||
% choose an object to spy:
|
||||
|
||||
| ?- message_counter::set_spy_point(_, list, _, _).
|
||||
yes
|
||||
|
||||
|
||||
% activate the monitor:
|
||||
|
||||
| ?- message_counter::activate_monitor.
|
||||
yes
|
||||
|
||||
|
||||
% send some messages to the spied object; get all the answers for ancestor/1:
|
||||
|
||||
| ?- list::empty([]).
|
||||
yes
|
||||
|
||||
| ?- list::member(X, [1, 2, 3]).
|
||||
X = 1 ? ;
|
||||
X = 2 ? ;
|
||||
X = 3 ? ;
|
||||
no
|
||||
|
||||
|
||||
% print a report of the data collected by the monitor:
|
||||
|
||||
| ?- message_counter::report.
|
||||
list
|
||||
total of calls: 2
|
||||
total of exits: 4
|
||||
|
||||
empty/1
|
||||
calls: 1
|
||||
exits: 1
|
||||
|
||||
member/2
|
||||
calls: 1
|
||||
exits: 3
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% stop and reset the message counter monitor:
|
||||
|
||||
| ?- message_counter::stop.
|
||||
yes
|
||||
|
||||
|
||||
|
||||
%% stop_watch example
|
||||
|
||||
|
||||
% choose a pair object/message to spy:
|
||||
|
||||
| ?- stop_watch::set_spy_point(_, list, length(_, _), _).
|
||||
yes
|
||||
|
||||
|
||||
% activate the monitor:
|
||||
|
||||
| ?- stop_watch::activate_monitor.
|
||||
yes
|
||||
|
||||
|
||||
% send a message to the spied object:
|
||||
|
||||
| ?- list::length([1, 2, 3], Length).
|
||||
list <-- length([1,2,3],_277) from user
|
||||
STARTING at 755.75999999999999 seconds
|
||||
list <-- length([1,2,3],3) from user
|
||||
ENDING at 755.69000000000005 seconds
|
||||
|
||||
Length = 3
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% clean up:
|
||||
|
||||
| ?- stop_watch::reset_monitor.
|
||||
yes
|
||||
|
||||
|
||||
|
||||
%% timer example
|
||||
|
||||
|
||||
% try a message 1000 times and return the average time:
|
||||
|
||||
| ?- timer::timer(list::length([1, 2, 3], _), 1000, Time).
|
||||
Time = 0.00070799999999996994
|
||||
|
||||
yes
|
104
Logtalk/examples/profiling/message_counter.lgt
Normal file
104
Logtalk/examples/profiling/message_counter.lgt
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
:- object(message_counter,
|
||||
implements(event_handlersp),
|
||||
imports(monitor)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
authors is 'Paulo Moura',
|
||||
date is 1998/3/23,
|
||||
comment is 'Message counter monitor.']).
|
||||
|
||||
|
||||
:- public(report/0).
|
||||
|
||||
:- mode(report, one).
|
||||
|
||||
:- info(report/0,
|
||||
[comment is 'Reports current calls and exits message counts.']).
|
||||
|
||||
|
||||
:- public(stop/0).
|
||||
|
||||
:- mode(stop, one).
|
||||
|
||||
:- info(stop/0,
|
||||
[comment is 'Stops message counting.']).
|
||||
|
||||
|
||||
:- private(calls/2).
|
||||
:- dynamic(calls/2).
|
||||
:- mode(calls(?object, ?integer), zero_or_more).
|
||||
|
||||
:- private(calls/3).
|
||||
:- dynamic(calls/3).
|
||||
:- mode(calls(?object, ?predicate_indicator,?integer), zero_or_more).
|
||||
|
||||
:- private(exits/2).
|
||||
:- dynamic(exits/2).
|
||||
:- mode(exits(?object, ?integer), zero_or_more).
|
||||
|
||||
:- private(exits/3).
|
||||
:- dynamic(exits/3).
|
||||
:- mode(exits(?object, ?predicate_indicator,?integer), zero_or_more).
|
||||
|
||||
|
||||
report :-
|
||||
forall(
|
||||
::calls(Object, Calls),
|
||||
(writeq(Object), nl,
|
||||
write(' total of calls: '), write(Calls), nl,
|
||||
write(' total of exits: '),
|
||||
(::exits(Object, Exits) ->
|
||||
write(Exits), nl, nl
|
||||
;
|
||||
write(0), nl, nl),
|
||||
forall(
|
||||
::calls(Object, Functor/Arity, Calls2),
|
||||
(write(' '), writeq(Functor/Arity), nl,
|
||||
write(' calls: '), write(Calls2), nl,
|
||||
write(' exits: '),
|
||||
(::exits(Object, Functor/Arity, Exits2) ->
|
||||
write(Exits2), nl, nl
|
||||
;
|
||||
write(0), nl, nl))))).
|
||||
|
||||
|
||||
stop :-
|
||||
::retractall(calls(_, _)),
|
||||
::retractall(exits(_, _)),
|
||||
::retractall(calls(_, _, _)),
|
||||
::retractall(exits(_, _, _)),
|
||||
::reset_monitor.
|
||||
|
||||
|
||||
before(Object, Message, _) :-
|
||||
(::retract(calls(Object, Old)) ->
|
||||
New is Old + 1
|
||||
;
|
||||
New = 1),
|
||||
::assertz(calls(Object, New)),
|
||||
functor(Message, Functor, Arity),
|
||||
(::retract(calls(Object, Functor/Arity, Old2)) ->
|
||||
New2 is Old2 + 1
|
||||
;
|
||||
New2 = 1 ),
|
||||
::assertz(calls(Object, Functor/Arity, New2)).
|
||||
|
||||
|
||||
after(Object, Message, _) :-
|
||||
(::retract(exits(Object, Old)) ->
|
||||
New is Old + 1
|
||||
;
|
||||
New = 1),
|
||||
::assertz(exits(Object, New)),
|
||||
functor(Message, Functor, Arity),
|
||||
(::retract(exits(Object, Functor/Arity, Old2)) ->
|
||||
New2 is Old2 + 1
|
||||
;
|
||||
New2 = 1),
|
||||
::assertz(exits(Object, Functor/Arity, New2)).
|
||||
|
||||
|
||||
:- end_object.
|
6
Logtalk/examples/profiling/profiling.loader
Normal file
6
Logtalk/examples/profiling/profiling.loader
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
timer,
|
||||
message_counter,
|
||||
stop_watch])).
|
28
Logtalk/examples/profiling/stop_watch.lgt
Normal file
28
Logtalk/examples/profiling/stop_watch.lgt
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
:- object(stop_watch,
|
||||
implements(event_handlersp),
|
||||
imports(monitor)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
authors is 'Paulo Moura',
|
||||
date is 1998/3/23,
|
||||
comment is 'Message executing time monitor.']).
|
||||
|
||||
|
||||
:- uses(time).
|
||||
|
||||
|
||||
before(Object, Message, Sender) :-
|
||||
write(Object), write(' <-- '), writeq(Message),
|
||||
write(' from '), write(Sender), nl, write('STARTING at '),
|
||||
time::cpu_time(Seconds), write(Seconds), write(' seconds'), nl.
|
||||
|
||||
after(Object, Message, Sender) :-
|
||||
write(Object), write(' <-- '), writeq(Message),
|
||||
write(' from '), write(Sender), nl, write('ENDING at '),
|
||||
time::cpu_time(Seconds), write(Seconds), write(' seconds'), nl.
|
||||
|
||||
|
||||
:- end_object.
|
50
Logtalk/examples/profiling/timer.lgt
Normal file
50
Logtalk/examples/profiling/timer.lgt
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
:- object(timer).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
authors is 'Paulo Moura',
|
||||
date is 1998/3/23,
|
||||
comment is 'Call executing time profiler.']).
|
||||
|
||||
|
||||
:- uses(time).
|
||||
:- uses(loop).
|
||||
|
||||
|
||||
:- public(timer/2).
|
||||
:- metapredicate(timer(::, *)).
|
||||
|
||||
:- mode(timer(+callable, -number), one).
|
||||
|
||||
:- info(timer/2,
|
||||
[comment is 'Returns time to execute a call.',
|
||||
argnames is ['Call', 'Time']]).
|
||||
|
||||
|
||||
:- public(timer/3).
|
||||
:- metapredicate(timer(::, *, *)).
|
||||
|
||||
:- mode(timer(+callable, +integer, -float), one).
|
||||
|
||||
:- info(timer/3,
|
||||
[comment is 'Returns the average time needed to to execute a call.',
|
||||
argnames is ['Call', 'Times', 'Time']]).
|
||||
|
||||
|
||||
timer(Call, Time) :-
|
||||
time::cpu_time(Start),
|
||||
(call(Call) -> true; true),
|
||||
time::cpu_time(End),
|
||||
Time is End - Start.
|
||||
|
||||
|
||||
timer(Call, Times, Time) :-
|
||||
time::cpu_time(Start),
|
||||
loop::forto(1, Times, Call),
|
||||
time::cpu_time(End),
|
||||
Time is (End - Start) / Times.
|
||||
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user