1 % (c) 2016-2025 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(performance_messages,
6 [
7 performance_monitoring_on/0,
8 perfmessage/1, perfmessage/2, perfmessage/3, perfmessage/4,
9 perfmessage_wf/5,
10 perfmessage_bexpr/3, perfmessages_bexpr/3, perfmessages_bexpr_call/4,
11 cond_perfmessage/2,
12 perfmessagecall/3, perfmessagecall/4,
13 perf_format/2,
14 perf_format_wf/3,
15 toggle_perfmessages/0
16 ]).
17
18 :- meta_predicate perfmessagecall(*,0,*).
19 :- meta_predicate perfmessagecall(*,*,0,*).
20 :- meta_predicate perfmessages_bexpr_call(*,*,*,0).
21
22 :- use_module(module_information,[module_info/2]).
23 :- module_info(group,profiling).
24 :- module_info(description,'This module allows to generate performance messages.').
25
26 :- use_module(preferences).
27 :- use_module(error_manager).
28 :- use_module(tools_printing,[format_with_colour/4]).
29 :- use_module(tools_strings,[ajoin/2]).
30 :- use_module(translate,[translate_bexpression_with_limit/3]).
31
32 % -------------------------------------------
33
34 :- dynamic performance_monitoring_on/0. % should we use a preference
35 %performance_monitoring_on.
36
37 performance_monitoring_on :- get_preference(performance_monitoring_on,true).
38
39 toggle_perfmessages :- print('TURNING PERF-MESSAGES '),
40 (performance_monitoring_on
41 -> set_preference(performance_monitoring_on,false), print('OFF')
42 ; set_preference(performance_monitoring_on,true), print('ON')),nl.
43
44 perf_format(M,Args) :- preference(performance_monitoring_on,true),!,
45 format_with_colour(user_output,[blue],'==PERF-MESSAGE==> ',[]),
46 format_with_colour(user_output,[blue],M,Args).
47 perf_format(_,_).
48
49 perf_format_wf(M,Args,WF) :- preference(performance_monitoring_on,true),!,
50 format_with_colour(user_output,[blue],'==PERF-MESSAGE==> ',[]),
51 format_with_colour(user_output,[blue],M,Args),
52 kernel_waitflags:portray_call_stack(WF).
53 perf_format_wf(_,_,_).
54
55 perfmessage(M) :- preference(performance_monitoring_on,true),!,
56 format_with_colour(user_output,[blue],'==PERF-MESSAGE==> ~w~n',[M]).
57 perfmessage(_).
58
59 :- use_module(library(lists),[maplist/2]).
60 % conditional perfmessage
61 cond_perfmessage(Prefs,M) :- preference(performance_monitoring_on,true),
62 maplist(prefset,Prefs),
63 !,
64 format_with_colour(user_output,[blue],'==PERF-MESSAGE==> ~w~n',[M]).
65 cond_perfmessage(_,_).
66
67 prefset(P/V) :- get_preference(P,V).
68
69 perfmessage(M,Location) :- preference(performance_monitoring_on,true),!,
70 add_message(performance_message,'==PERF-MESSAGE==> ',M,Location).
71 perfmessage(_,_).
72
73 perfmessage(Category,M,Location) :- preference(performance_monitoring_on,true),
74 show_message(Category),
75 !,
76 add_message(performance_message,'==PERF-MESSAGE==> ',M,Location).
77 perfmessage(_,_,_).
78
79 perfmessage(Category,Msg,Term,Location) :- preference(performance_monitoring_on,true),
80 show_message(Category),
81 !,
82 ajoin(['==PERF-MESSAGE==> ',Msg],M),
83 add_message(performance_message,M,Term,Location).
84 perfmessage(_,_,_,_).
85
86 :- use_module(probsrc(kernel_waitflags),[add_message_wf/5]).
87 perfmessage_wf(Category,Msg,Term,Location,WF) :- preference(performance_monitoring_on,true),
88 show_message(Category),
89 !,
90 ajoin(['==PERF-MESSAGE==> ',Msg],M),
91 add_message_wf(performance_message,M,Term,Location,WF).
92 perfmessage_wf(_,_,_,_,_).
93
94 % perfmessage where we provide a BExpr as location and part of message
95 perfmessage_bexpr(Category,Msg,BExpr) :-
96 (Msg = [_|_] -> List=Msg ; List=[Msg]),
97 perfmessages_bexpr(Category,List,BExpr).
98
99
100 % perfmessage where we provide a BExpr as location and messages to be joined
101 perfmessages_bexpr(Category,Msgs,BExpr) :- preference(performance_monitoring_on,true),
102 show_message(Category),
103 !,
104 translate_bexpression_with_limit(BExpr,100,TS),
105 append(Msgs,[TS],List),
106 ajoin(List,M),
107 add_message(performance_message,'==PERF-MESSAGE==> ',M,BExpr).
108 perfmessages_bexpr(_,_,_).
109
110 % a version of the above performing a call before printing the message
111 perfmessages_bexpr_call(Category,Msgs,BExpr,Call) :- preference(performance_monitoring_on,true),
112 show_message(Category),!,
113 call(Call),
114 perfmessages_bexpr(Category,Msgs,BExpr).
115 perfmessages_bexpr_call(_,_,_,_).
116
117 perfmessagecall(M,Call,Location) :- preference(performance_monitoring_on,true),!,
118 add_message(performance_message,'==PERF-MESSAGE==> ',M,Location),
119 call(Call),nl(user_output).
120 perfmessagecall(_,_,_).
121
122
123 perfmessagecall(Category,M,Call,Location) :- preference(performance_monitoring_on,true),
124 show_message(Category),
125 !,
126 add_message(performance_message,'==PERF-MESSAGE==> ',M,Location),
127 call(Call),nl(user_output).
128 perfmessagecall(_,_,_,_).
129
130 % decide based upon category and current preferences whether to show the message or not
131 show_message(reify) :- !,
132 get_preference(data_validation_mode,false).
133 show_message(good(_)) :- !,
134 get_preference(provide_trace_information,true).
135 show_message(_Category).