[m-rev.] diff: Improve monitor examples
Erwan Jahier
Erwan.Jahier at irisa.fr
Wed Jul 11 21:17:10 AEST 2001
I have made those improvements a long time ago...
--
Estimated hours taken: 2
branches: main.
Improve a little bit the code of monitor examples provided with
morphine.
extras/morphine/source/control_flow.op:
extras/morphine/source/collect__control_flow_graph:
extras/morphine/source/collect__dynamic_call_graph:
Use sets instead of lists, and arc/2 instead of edge/2.
Use post_process/2 to remove useless stuff.
extras/morphine/source/collect__dynamic_call_graph:
Extend arc labels with an integer that counts the number of calls.
Use stack library modules instead of list.
Index: extras/morphine/source/collect__control_flow_graph
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/morphine/source/collect__control_flow_graph,v
retrieving revision 1.2
diff -u -d -u -r1.2 collect__control_flow_graph
--- extras/morphine/source/collect__control_flow_graph 1999/12/16 18:13:41 1.2
+++ extras/morphine/source/collect__control_flow_graph 2001/07/11 11:13:13
@@ -1,37 +1,35 @@
%------------------------------------------------------------------------------%
-% Copyright (C) 1999 INRIA/INSA.
+% Copyright (C) 1999-2001 INRIA/INSA/IFSIC.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file License in the Morphine distribution.
%
% Author : Erwan Jahier <jahier at irisa.fr>
%
-% define a monitor which is used in control_flow.op
+% Computes the control flow graph of an execution.
-:- import_module list.
+:- import_module set.
-:- type my_proc ---> proc_name/arity.
-:- type edge ---> edge(my_proc, my_proc).
-:- type graph == list(edge).
+:- type predicate ---> proc_name/arity.
+:- type arc ---> arc(predicate, predicate).
+:- type graph == set(arc).
-:- type collected_type --->
- collected_type(my_proc, graph).
+:- type accumulator_type ---> ct(predicate, graph).
+:- type collected_type ---> collected_type(graph).
-initialize(collected_type("main"/2, [])).
+initialize(ct("user"/0, set__init)).
-filter(Event, AccIn, AccOut, continue) :-
+filter(Event, Acc0, Acc, continue) :-
Port = port(Event),
- AccIn = collected_type(Proc, Graph),
(
- not (Port = call ; Port = exit ; Port = redo ; Port = fail)
+ (Port = call ; Port = exit ; Port = fail ; Port = redo)
->
- AccOut = AccIn
+ Acc0 = ct(PreviousPred, Graph0),
+ CurrentPred = proc_name(Event) / proc_arity(Event),
+ Arc = arc(PreviousPred, CurrentPred),
+ set__insert(Graph0, Arc, Graph),
+ Acc = ct(CurrentPred, Graph)
;
- Proc2 = proc_name(Event) / proc_arity(Event),
- Edge = edge(Proc, Proc2),
- ( member(Edge, Graph) ->
- AccOut = AccIn
- ;
- AccOut = collected_type(Proc2, [Edge|Graph])
- )
+ Acc = Acc0
).
+post_process(ct(_, Graph), collected_type(Graph)).
\ No newline at end of file
Index: extras/morphine/source/collect__dynamic_call_graph
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/morphine/source/collect__dynamic_call_graph,v
retrieving revision 1.2
diff -u -d -u -r1.2 collect__dynamic_call_graph
--- extras/morphine/source/collect__dynamic_call_graph 1999/12/16 18:13:41 1.2
+++ extras/morphine/source/collect__dynamic_call_graph 2001/07/11 11:13:13
@@ -1,69 +1,63 @@
%------------------------------------------------------------------------------%
-% Copyright (C) 1999 INRIA/INSA.
+% Copyright (C) 1999-2001 INRIA/INSA/IFSIC.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file License in the Morphine distribution.
%
% Author : Erwan Jahier <jahier at irisa.fr>
%
-% define a monitor which is used in control_flow.op
+% Computes a dynamic call graph, namely, the sub-graph of the (static) call
+% graph that has been exercized during an execution.
-:- import_module list.
+:- import_module set, stack.
-:- type my_proc ---> proc_name/arity.
-:- type edge ---> edge(my_proc, my_proc).
-:- type graph == list(edge).
-:- type stack == list(my_proc).
+:- type predicate ---> proc_name/arity.
+:- type arc ---> arc(predicate, int, predicate).
+:- type graph == set(arc).
-:- type collected_type --->
- collected_type(stack, graph).
+:- type accumulator_type ---> ct(stack(predicate), graph).
+:- type collected_type ---> collected_type(graph).
-initialize(collected_type(["main"/2], [])).
+initialize(ct(Stack, set__init)) :-
+ stack__push(stack__init, "user"/0, Stack).
-filter(Event, AccIn, AccOut, continue) :-
+filter(Event, Acc0, Acc, continue) :-
Port = port(Event),
- AccIn = collected_type(Stack, Graph),
+ Acc0 = ct(Stack0, Graph0),
(
Port = call
->
- Proc = ancestor(Stack),
- Proc2 = proc_name(Event) / proc_arity(Event),
- Edge = edge(Proc, Proc2),
- ( member(Edge, Graph) ->
- AccOut = collected_type([Proc2|Stack], Graph)
- ;
- AccOut = collected_type([Proc2|Stack], [Edge|Graph])
- )
+ PreviousPred = stack__top_det(Stack0),
+ CurrentPred = proc_name(Event) / proc_arity(Event),
+ Graph = promise_only_solution(
+ update_graph(PreviousPred, CurrentPred, Graph0)),
+ Acc = ct(push(Stack0, CurrentPred), Graph)
;
Port = redo
->
- Proc2 = proc_name(Event) / proc_arity(Event),
- AccOut = collected_type([Proc2|Stack], Graph)
- ;
- Port = fail
- ->
- AccOut = collected_type(pop(Stack), Graph)
+ CurrentPred = proc_name(Event) / proc_arity(Event),
+ Acc = ct(push(Stack0, CurrentPred), Graph0)
;
- Port = exit
+ ( Port = fail ; Port = exit)
->
- AccOut = collected_type(pop(Stack), Graph)
+ stack__pop_det(Stack0, _, Stack),
+ Acc = ct(Stack, Graph0)
;
- AccOut = AccIn
+ % Other events
+ Acc = Acc0
).
-:- func ancestor(stack::in) = (my_proc::out) is det.
-ancestor(Stack) = Anc :-
+:- pred update_graph(predicate, predicate, set(arc), set(arc)).
+:- mode update_graph(in, in, in, out) is cc_multi.
+update_graph(Pred0, Pred, Graph0, Graph) :-
(
- Stack = [],
- Anc = "error"/0
+ member(arc(Pred0, N, Pred), Graph0)
+ ->
+ delete(Graph0, arc(Pred0, N, Pred), Graph1),
+ insert(Graph1, arc(Pred0, N+1, Pred), Graph)
;
- Stack = [Anc|_]
+ insert(Graph0, arc(Pred0, 1, Pred), Graph)
).
-:- func pop(stack::in) = (stack::out) is det.
-pop(Stack0) = Stack :-
- (
- Stack0 = [],
- Stack = []
- ;
- Stack0 = [_|Stack]
- ).
+
+post_process(ct(_, Graph), collected_type(Graph)).
\ No newline at end of file
Index: extras/morphine/source/control_flow.op
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/morphine/source/control_flow.op,v
retrieving revision 1.4
diff -u -d -u -r1.4 control_flow.op
--- extras/morphine/source/control_flow.op 1999/12/20 14:44:11 1.4
+++ extras/morphine/source/control_flow.op 2001/07/11 11:13:13
@@ -1,5 +1,5 @@
%------------------------------------------------------------------------------%
-% Copyright (C) 1999 INRIA/INSA de Rennes.
+% Copyright (C) 1999-2001 INRIA/INSA de Rennes/IFSIC.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file License in the Morphine distribution.
%
@@ -60,7 +60,7 @@
display_graph(Graph, DotFile, PsFile).
-% :- type graph ---> list(edge(procedure, procedure)).
+% :- type graph ---> list(arc(procedure, procedure)).
%
%:- pred generate_control_flow_graph(graph).
%:- mode generate_control_flow_graph(out) is det.
@@ -70,7 +70,7 @@
append_strings(MorphineDir, "/source/collect__control_flow_graph",
CollectFile),
collect(CollectFile, Result),
- Result = collected_type(_,G).
+ Result = collected_type(G).
% This is the pure Morphine version of generate_control_flow_graph that
% I originally wrote and which is about 20 times as slow as the collect
@@ -80,7 +80,7 @@
% ( fget_np(port = [call, exit,redo,fail]) ->
% current(proc_name = Name and arity = N),
% CurrentProc = Name/N,
-% G1 = [edge(Proc, CurrentProc)|G0],
+% G1 = [arc(Proc, CurrentProc)|G0],
% generate_control_flow_graph(G1, CurrentProc, G),
% !
% ;
@@ -121,7 +121,7 @@
append_strings(MorphineDir, "/source/collect__dynamic_call_graph",
CollectFile),
collect(CollectFile, Result),
- Result = collected_type(_,G).
+ Result = collected_type(G).
% This is the pure Morphine version:
%
@@ -130,7 +130,7 @@
% current(proc_name = Name and arity = N),
% ancestor(Anc),
% CurrentProc = Name/N,
-% G1 = [edge(Anc, CurrentProc)|G0],
+% G1 = [arc(Anc, CurrentProc)|G0],
% generate_dynamic_call_graph(G1, G),
% !
% ;
@@ -186,9 +186,12 @@
dump_proc2(Xs).
dump_graph([]) :- nl(dotfile).
-dump_graph([edge(X,Y)|CG]) :-
+dump_graph([arc(X,Y)|CG]) :-
printf(dotfile, "\t\"%w\" -> \"%w\";\n", [X,Y]),
dump_graph(CG).
+dump_graph([arc(X, Label, Y)|CG]) :-
+ printf(dotfile, "\t\"%w\" -> \"%w\"[label=%w];\n", [X,Y,Label]),
+ dump_graph(CG).
%------------------------------------------------------------------------------%
opium_primitive(
@@ -237,7 +240,8 @@
reverse(ProcList1, ProcList).
extract_proc_list_from_graph2([], []).
-extract_proc_list_from_graph2([edge(Proc,_)|Graph], [Proc|ProcList]) :-
+extract_proc_list_from_graph2([Arc|Graph], [Proc|ProcList]) :-
+ ( Arc = arc(Proc,_), ! ; Arc = arc(Proc,_,_)),
extract_proc_list_from_graph2(Graph, ProcList).
@@ -251,3 +255,235 @@
;
L = [X|L2]
).
--
R1.
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list