34
35:- module(prolog_table_utils,
36 [ table_statistics/0, 37 table_statistics/1, 38 table_statistics_by_predicate/0,
39 table_statistics_by_predicate/1, 40 table_statistics/2, 41 table_statistics/3, 42 tstat/2, 43 tstat/3, 44 tdump/0, 45 tdump/1, 46 tdump/2, 47 tidg/0,
48 tidg/1, 49 summarize_idg/0,
50 summarize_idg/1 51 ]). 52:- autoload(library(lists), [member/2]). 53:- autoload(library(aggregate), [aggregate_all/3]). 54:- autoload(library(ansi_term), [ansi_format/3]). 55:- autoload(library(apply), [exclude/3, maplist/2]). 56:- autoload(library(dif), [dif/2]). 57:- autoload(library(error), [domain_error/2]). 58:- autoload(library(option), [option/3, option/2]). 59:- autoload(library(prolog_code), [pi_head/2]). 60:- autoload(library(solution_sequences), [limit/2, order_by/2]). 61:- autoload(library(varnumbers), [numbervars/1]). 62
63:- meta_predicate
64 tdump(:),
65 tdump(:, +),
66 tidg(:),
67 summarize_idg(:),
68 table_statistics(:),
69 table_statistics(:, ?, -),
70 tstat(:, ?, ?). 71
77
78summary_table_width(55).
79
126
127table_statistics(Stat, Value) :-
128 table_statistics(_:_, Stat, Value).
129
130table_statistics(Variant, Stat, Value) :-
131 ( var(Stat)
132 -> table_statistics_(Variant, Stat, Value)
133 ; table_statistics_(Variant, Stat, Value)
134 -> true
135 ).
136
137table_statistics_(Variant, tables, NTables) :-
138 aggregate_all(count, answer_table(Variant, _), NTables),
139 ( NTables > 0
140 -> true
141 ; \+ dependency_node(Variant, _)
142 ).
143table_statistics_(Variant, dependency_nodes, Count) :-
144 aggregate_all(count, dependency_node(Variant, _), Count),
145 Count > 0.
146table_statistics_(Variant, dependency_space, Bytes) :-
147 \+ \+ dependency_node(Variant, _),
148 aggregate_all(sum(Size),
149 ( dependency_node(Variant, Trie),
150 dependency_node_size(Trie, Size)
151 ),
152 Bytes).
153table_statistics_(Variant, Stat, Total) :-
154 variant_trie_stat(Stat, _What),
155 \+ hidden_stat(Stat, Variant),
156 \+ \+ answer_table(Variant, _),
157 ( avg(Stat)
158 -> aggregate_all(sum(Ratio)+count,
159 variant_stat(Stat, Variant, Ratio),
160 Sum+Count),
161 Count > 0,
162 Total is Sum/Count
163 ; aggregate_all(sum(Count), variant_stat(Stat, Variant, Count), Total)
164 ).
165
166hidden_stat(variables, _).
167hidden_stat(lookup, _).
168hidden_stat(invalidated, Variant) :-
169 callable(Variant),
170 \+ predicate_property(Variant, tabled(incremental)).
171hidden_stat(reevaluated, Variant) :-
172 callable(Variant),
173 \+ predicate_property(Variant, tabled(incremental)).
174
175avg(space_ratio).
176avg(duplicate_ratio).
177
183
184table_statistics :-
185 ( ( '$tbl_global_variant_table'(Table),
186 call_table_properties(shared, Table)
187 ; '$tbl_local_variant_table'(Table),
188 call_table_properties(private, Table)
189 ),
190 fail
191 ; true
192 ),
193 ansi_format([bold], 'Summary of answer trie statistics:', []),
194 nl,
195 table_statistics_(_:_, [tables(false)]).
196
201
202table_statistics(Variant) :-
203 table_statistics_(Variant, []).
204
205table_statistics_(Variant, Options) :-
206 table_statistics_dict(Variant, Dict),
207 print_table_statistics(Dict, Options).
208
209table_statistics_dict(Variant, Dict) :-
210 findall(Stat-Value, table_statistics(Variant, Stat, Value), Pairs),
211 dict_create(Dict, table_stat, [variant-Variant|Pairs]).
212
213print_table_statistics(Dict, Options) :-
214 summary_table_width(DefWidth),
215 option(width(Width), Options, DefWidth),
216 ( option(tables(false), Options)
217 -> dif(Stat, tables)
218 ; true
219 ),
220 ( option(header(true), Options)
221 -> print_table_predicate_header(Dict.variant, [width(Width)|Options])
222 ; true
223 ),
224 ( variant_trie_stat0(Stat, What),
225 Value = Dict.get(Stat),
226 ( integer(Value)
227 -> format(' ~w ~`.t ~D~*|~n', [What, Value, Width])
228 ; format(' ~w ~`.t ~2f~*|~n', [What, Value, Width])
229 ),
230 fail
231 ; true
232 ).
233
(Pred, Options) :-
235 option(width(Width), Options),
236 Pred = M:Head,
237 tflags(Pred, Flags),
238 functor(Head, Name, Arity),
239 format('~n~`\u2015t~*|~n', [Width]),
240 format('~t~p~t~w~*|~n', [M:Name/Arity, Flags, Width]),
241 format('~`\u2015t~*|~n', [Width]).
242
243tflags(Pred, Flags) :-
244 findall(F, tflag(Pred, F), List),
245 atomic_list_concat(List, Flags).
246
247tflag(Pred, Flag) :-
248 predicate_property(Pred, tabled(How)),
249 tflag_name(How, Flag).
250
251tflag_name(variant, 'V').
252tflag_name(subsumptive, 'S').
253tflag_name(shared, 'G').
254tflag_name(incremental, 'I').
255
256
257variant_trie_stat0(tables, "Answer tables").
258variant_trie_stat0(Stat, What) :-
259 dif(Stat, tables),
260 variant_trie_stat(Stat, What).
261variant_trie_stat0(dependency_nodes, "Incremental dependency nodes").
262variant_trie_stat0(dependency_space, "Memory for dependency nodes").
263
264call_table_properties(Which, Trie) :-
265 ansi_format([bold], 'Statistics for ~w call trie:', [Which]),
266 nl,
267 summary_table_width(Width),
268 ( call_trie_property_name(P, Label, Value),
269 atrie_prop(Trie, P),
270 ( integer(Value)
271 -> format(' ~w ~`.t ~D~*|~n', [Label, Value, Width])
272 ; format(' ~w ~`.t ~1f~*|~n', [Label, Value, Width])
273 ),
274 fail
275 ; true
276 ).
277
278call_trie_property_name(value_count(N), 'Number of tables', N).
279call_trie_property_name(size(N), 'Memory for call trie', N).
280call_trie_property_name(space_ratio(N), 'Space efficiency', N).
281
297
298table_statistics_by_predicate :-
299 Pred = _:_,
300 summary_table_width(Width),
301 ( tabled_predicate_with_tables(Pred),
302 print_table_predicate_header(Pred, [width(Width)]),
303 table_statistics(Pred),
304 fail
305 ; true
306 ).
307
308table_statistics_by_predicate(Options) :-
309 option(order_by(OrderBy), Options, tables),
310 option(top(Top), Options, infinite),
311 option(module(M), Options, _),
312 Pred = (M:_),
313 findall(Dict,
314 ( tabled_predicate_with_tables(Pred),
315 table_statistics_dict(Pred, Dict)
316 ),
317 Dicts),
318 exclude(has_no_key(OrderBy), Dicts, Dicts1),
319 ( integer(Top), Top < 0
320 -> Order = @=<,
321 TopN is -Top
322 ; Order = @>=,
323 TopN is Top
324 ),
325 sort(OrderBy, Order, Dicts1, Sorted),
326 forall(limit(TopN, member(Dict, Sorted)),
327 print_table_statistics(Dict, [header(true)|Options])).
328
329has_no_key(Key, Dict) :-
330 \+ _ = Dict.get(Key).
331
332tabled_predicate_with_tables(Pred) :-
333 Pred = _:_,
334 predicate_property(Pred, tabled),
335 \+ predicate_property(Pred, imported_from(_)),
336 \+ \+ table(Pred, _).
337
367
377
378tstat(Stat, Top) :-
379 tstat(_:_, Stat, Top).
380tstat(Variant, Stat, Top) :-
381 variant_trie_stat(Stat, What),
382 top(Top, Count, Limit, Dir, Order),
383 findall(Variant-Count,
384 limit(Limit, order_by([Order], variant_stat(Stat, Variant, Count))),
385 Pairs),
386 write_variant_table('~w ~w count per variant'-[Dir, What], Pairs).
387
388top(Top, Var, 10, "Top", desc(Var)) :-
389 var(Top), !.
390top(Top, Var, Top, "Top", desc(Var)) :-
391 Top >= 0, !.
392top(Top, Var, Limit, "Bottom", asc(Var)) :-
393 Limit is -Top.
394
395variant_stat(Stat, V, Count) :-
396 variant_trie_stat(Stat, _, Count, Property),
397 answer_table(V, T),
398 atrie_prop(T, Property).
399
400atrie_prop(T, size(Bytes)) :-
401 '$trie_property'(T, size(Bytes)).
402atrie_prop(T, compiled_size(Bytes)) :-
403 '$trie_property'(T, compiled_size(Bytes)).
404atrie_prop(T, value_count(Count)) :-
405 '$trie_property'(T, value_count(Count)).
406atrie_prop(T, space_ratio(Values/Nodes)) :-
407 '$trie_property'(T, value_count(Values)),
408 Values > 0,
409 \+ '$trie_property'(T, discarded(true)), 410 '$trie_property'(T, node_count(Nodes)).
411atrie_prop(T, lookup_count(Count)) :-
412 '$trie_property'(T, lookup_count(Count)).
413atrie_prop(T, duplicate_ratio(Ratio)) :-
414 '$trie_property'(T, value_count(Values)),
415 Values > 0,
416 '$trie_property'(T, lookup_count(Lookup)),
417 Ratio is (Lookup - Values)/Values.
418atrie_prop(T, gen_call_count(Count)) :-
419 '$trie_property'(T, gen_call_count(Count)).
420atrie_prop(T, invalidated(Count)) :-
421 '$trie_property'(T, invalidated(Count)).
422atrie_prop(T, reevaluated(Count)) :-
423 '$trie_property'(T, reevaluated(Count)).
424atrie_prop(T, deadlock(Count)) :-
425 '$trie_property'(T, deadlock(Count)),
426 Count > 0.
427atrie_prop(T, wait(Count)) :-
428 '$trie_property'(T, wait(Count)),
429 Count > 0.
430atrie_prop(T, variables(Count)) :-
431 '$tbl_table_status'(T, _Status, _Wrapper, Skeleton),
432 functor(Skeleton, ret, Count).
433
434variant_trie_stat(Stat, What) :-
435 ( variant_trie_stat(Stat, What, _, _)
436 *-> true
437 ; domain_error(tstat_key, Stat)
438 ).
439
440variant_trie_stat(answers, "Number of answers",
441 Count, value_count(Count)).
442variant_trie_stat(duplicate_ratio,"Duplicate answer ratio",
443 Ratio, duplicate_ratio(Ratio)).
444variant_trie_stat(space_ratio, "Space efficiency",
445 Ratio, space_ratio(Ratio)).
446variant_trie_stat(complete_call, "Calls to completed tables",
447 Count, gen_call_count(Count)).
448variant_trie_stat(invalidated, "Times the tables were invalidated",
449 Count, invalidated(Count)).
450variant_trie_stat(reevaluated, "Times the tables were reevaluated",
451 Count, reevaluated(Count)).
452variant_trie_stat(space, "Memory usage for answer tables",
453 Bytes, size(Bytes)).
454variant_trie_stat(compiled_space, "Memory usage for compiled answer tables",
455 Bytes, compiled_size(Bytes)).
456variant_trie_stat(variables, "Number of variables in answer skeletons",
457 Count, variables(Count)).
458variant_trie_stat(wait, "Times table was waited for",
459 Count, wait(Count)).
460variant_trie_stat(deadlock, "Times table was involved in a deadlock",
461 Count, deadlock(Count)).
462
464
465write_variant_table(Format-Args, Pairs) :-
466 format(string(Title), Format, Args),
467 tty_size(_, Cols),
468 W is Cols - 8,
469 format('~`\u2015t~*|~n', [W]),
470 format('~t~w~t~*|~n', [Title, W]),
471 format('~`\u2015t~*|~n', [W]),
472 maplist(write_variant_stat(W), Pairs).
473
474write_variant_stat(W, V-Stat) :-
475 \+ \+ ( numbervars(V, 0, _, [singletons(true)]),
476 ( integer(Stat)
477 -> format('~p ~`.t ~D~*|~n', [V, Stat, W])
478 ; format('~p ~`.t ~2f~*|~n', [V, Stat, W])
479 )
480 ).
481
482table(M:Variant, Trie) :-
483 '$tbl_variant_table'(VariantTrie),
484 trie_gen(VariantTrie, M:Variant, Trie).
485
493
494answer_table(Variant, Trie) :-
495 table(Variant, Trie),
496 \+ '$tbl_table_status'(Trie, dynamic, _, _).
497
498dependency_node(Variant, Trie) :-
499 table(Variant, Trie),
500 '$tbl_table_status'(Trie, dynamic, _, _).
501
502dependency_node_size(Trie, Size) :-
503 '$trie_property'(Trie, size(S0)),
504 ( '$trie_property'(Trie, idg_size(S1))
505 -> true
506 ; S1 = 0
507 ),
508 Size is S0+S1.
509
510
511 514
528
529tdump :-
530 tdump(_:_).
531tdump(M:Goal) :-
532 tdump(M:Goal, []).
533
534tdump(M:Goal, Options) :-
535 option(scope(Scope), Options, _),
536 option(limit(Limit), Options, 100),
537 ( table(Scope, M:Goal, Trie),
538 '$tbl_table_status'(Trie, Status, M:Variant, Skeleton),
539 M:'$table_mode'(Head0, Variant, Moded),
540 Head = M:Head0,
541 ( option(reset(true), Options)
542 -> true
543 ; \+ (Scope == global, Status == fresh)
544 ),
545 ansi_format(comment, 'Trie for variant ', []),
546 pflags(Variant, Flags),
547 format('~s ', [Flags]),
548 print_variant(Head),
549 Answer = Head,
550 '$tbl_trienode'(Reserved),
551 ( Moded == Reserved
552 -> findall(Answer-Delay,
553 '$tbl_answer'(Trie, Skeleton, Delay), Pairs),
554 ExtraProp = ''
555 ; findall(Answer-Delay,
556 '$tbl_answer'(Trie, Skeleton, Moded, Delay), Pairs),
557 ExtraProp = 'moded, '
558 ),
559 sort(1, @<, Pairs, Sorted),
560 length(Sorted, Count),
561 status_color(Status, Color),
562 ansi_format(comment, ' (~p,', [Scope]),
563 ansi_format(Color, ' ~p', [Status]),
564 ansi_format(comment, ', ~w~D answers)~n', [ExtraProp, Count]),
565 ( Count == 0
566 -> ansi_format(warning, ' (empty)~n', [])
567 ; forall(limit(Limit, member(Ans, Sorted)),
568 dump_answer(M, Ans))
569 ),
570 fail
571 ; true
572 ).
573
574status_color(invalid, warning) :- !.
575status_color(_, comment).
576
577
578table(local, Variant, Trie) :-
579 '$tbl_local_variant_table'(VariantTrie),
580 trie_gen(VariantTrie, Variant0, Trie),
581 subsumes_term(Variant, Variant0),
582 Variant = Variant0.
583table(global, Variant, Trie) :-
584 '$tbl_global_variant_table'(VariantTrie),
585 trie_gen(VariantTrie, Variant0, Trie),
586 subsumes_term(Variant, Variant0),
587 Variant = Variant0.
588
589print_variant(Head) :-
590 term_attvars(Head, []),
591 !,
592 \+ \+ ( numbervars(Head, 0, _),
593 ansi_format(code, '~p', [Head])
594 ).
595print_variant(Head) :-
596 copy_term(Head, Copy, Constraints),
597 numbervars(Copy+Constraints, 0, _),
598 format('~p', [Copy]),
599 forall(member(C, Constraints),
600 ansi_format(fg(blue), ', ~p', [C])).
601
602dump_answer(M, Answer0-true) :-
603 !,
604 unqualify(Answer0, M, Answer),
605 \+ \+ print_answer(Answer).
606dump_answer(M, Answer0-Condition) :-
607 unqualify(Answer0, M, Answer),
608 unqualify(Condition, M, SimpleCondition),
609 \+ \+ ( numbervars(Answer+SimpleCondition, 0, _),
610 format(' ~p', [Answer]),
611 ansi_format(bold, ' :- ', []),
612 ansi_format(fg(cyan), '~p~n', [SimpleCondition])
613 ).
614
615print_answer(Answer) :-
616 term_attvars(Answer, []),
617 !,
618 numbervars(Answer, 0, _),
619 format(' ~p~n', [Answer]).
620print_answer(Answer) :-
621 copy_term(Answer, Copy, Constraints),
622 numbervars(Copy+Constraints, 0, _),
623 format(' ~p', [Copy]),
624 forall(member(C, Constraints),
625 ansi_format(fg(blue), ', ~p', [C])),
626 nl.
627
628unqualify(Var, _M, Var) :-
629 var(Var),
630 !.
631unqualify((A0,B0), M, (A,B)) :-
632 !,
633 unqualify(A0, M, A),
634 unqualify(B0, M, B).
635unqualify((A0;B0), M, (A;B)) :-
636 !,
637 unqualify(A0, M, A),
638 unqualify(B0, M, B).
639unqualify(tnot(A0), M, tnot(A)) :-
640 !,
641 unqualify(A0, M, A).
642unqualify((M1:Variant)/ModeArgs, M, Goal) :-
643 !,
644 M1:'$table_mode'(G0, Variant, ModeArgs),
645 unqualify(M1:G0, M, Goal).
646unqualify(M:G, M, G) :-
647 !.
648unqualify(G, _, G).
649
655
656tidg :-
657 ansi_format(comment,
658 '% Node1 [falsecount] (affects -->) Node1 [falsecount]~n', []),
659 forall(idg(t(_:From,FFC,FAC), affected, t(_:To,TFC,TAC)),
660 \+ \+ ( numbervars(From),
661 numbervars(To),
662 print_edge(From, FFC,FAC, To, TFC,TAC)
663 )).
664
665tidg(M:Node) :-
666 ansi_format(comment,
667 '% Node1 [falsecount] (affects -->) Node1 [falsecount]~n', []),
668 ansi_format([bold], 'Affected nodes~n', []),
669 forall(idg(t(M:Node,FFC,FAC), affected, t(_:To,TFC,TAC)),
670 \+ \+ ( numbervars(Node),
671 numbervars(To),
672 print_edge(Node, FFC,FAC, To, TFC,TAC)
673 )),
674 ansi_format([bold], 'Dependent nodes~n', []),
675 forall(idg(t(_:From,FFC,FAC), affected, t(M:Node,TFC,TAC)),
676 \+ \+ ( numbervars(From),
677 numbervars(Node),
678 print_edge(From, FFC,FAC, Node, TFC,TAC)
679 )).
680
681
682print_edge(From, FFC,FAC, To, TFC,TAC) :-
683 format(' '),
684 print_node(From, FFC,FAC),
685 format(' --> '),
686 print_node(To, TFC,TAC),
687 nl.
688
689print_node(Variant, Falsecount, AnswerCount) :-
690 pflags(Variant, Flags),
691 format('~s ', [Flags]),
692 ansi_format(code, '~p', [Variant]),
693 format(' '),
694 ( Falsecount == 0
695 -> ansi_format(comment, '[0]', [])
696 ; ansi_format([bg(red),fg(white)], '[~w]', [Falsecount])
697 ),
698 print_answer_count(AnswerCount).
699
700print_answer_count(answers(Count)) =>
701 format(' (~Da)', [Count]).
702print_answer_count(clauses(Count)) =>
703 format(' (~Dc)', [Count]).
704
705pflags(Variant, Flags) :-
706 findall(F, flag(Variant, F), Flags).
707
708flag(Variant, Flag) :-
709 ( pflag(Variant, dynamic, 'D', Flag)
710 ; pflag(Variant, incremental, 'I', Flag)
711 ; pflag(Variant, monotonic, 'M', Flag)
712 ).
713
714pflag(Variant, Property, Char, Flag) :-
715 ( predicate_property(Variant, Property)
716 -> Flag = Char
717 ; Flag = ' '
718 ).
719
720idg(t(FM:From,FFC,FAC), Dir, t(TM:To,TFC,TAC)) :-
721 '$tbl_variant_table'(VTrie),
722 trie_gen(VTrie, FM:FVariant, ATrie),
723 ( FM:'$table_mode'(From1, FVariant, _FModed)
724 -> true
725 ; From1 = FVariant 726 ),
727 subsumes_term(From, From1),
728 From = From1,
729 fc(ATrie, From, FFC,FAC),
730 '$idg_edge'(ATrie, Dir, DepTrie),
731 '$tbl_table_status'(DepTrie, _Status, TM:TVariant, _Return),
732 TM:'$table_mode'(To1, TVariant, _TModed),
733 subsumes_term(To, To1),
734 To = To1,
735 fc(DepTrie, To, TFC,TAC).
736
737fc(ATrie, Variant, FC, AC) :-
738 ( predicate_property(Variant, tabled)
739 -> trie_property(ATrie, value_count(C)),
740 AC = answers(C)
741 ; aggregate_all(count, clause(Variant,_), C),
742 AC = clauses(C)
743 ),
744 ( '$idg_falsecount'(ATrie, FC0)
745 -> ( '$idg_forced'(ATrie)
746 -> FC = FC0/'F'
747 ; FC = FC0
748 )
749 ; FC = 0
750 ).
751
756
757:- module_transparent
758 summarize_idg/0. 759
760summarize_idg :-
761 context_module(M),
762 summarize_idg(infinite, M).
763
764summarize_idg(M:Top) :-
765 summarize_idg(Top, M).
766
767summarize_idg(Top, M) :-
768 tty_width(Width),
769 header('Interior Nodes (Tabled Subgoals)', Width),
770 format('Predicate~t #idg nodes~*|~n', [Width]),
771 format('~`\u2015t~*|~n', [Width]),
772 forall(limit(Top,
773 order_by([desc(Count),asc(PI)],
774 interior_nodes(_:_, M, PI, Count))),
775 format('~q ~`.t ~D~*|~n', [PI, Count, Width])),
776 nl,
777 ColR is Width - 10,
778 header('Leaf Nodes (Calls to Dynamic Predicates)', Width),
779 format('Predicate~t #idg nodes~*|~t#facts~*|~n', [ColR, Width]),
780 format('~`\u2015t~*|~n', [Width]),
781 forall(limit(Top,
782 order_by([desc(Count),desc(Facts),asc(PI)],
783 leaf_nodes(_:_, M, PI, Count, Facts))),
784 format('~q ~`.t ~D~*| ~`.t ~D~*|~n',
785 [PI, Count, ColR, Facts, Width])).
786
787interior_nodes(Variant, M, PI, Count) :-
788 predicate_property(Variant, tabled(incremental)),
789 \+ predicate_property(Variant, imported_from(_)),
790 idg_node_count(Variant, Count),
791 pi_head(QPI, Variant),
792 unqualify_pi(QPI, M, PI).
793
794leaf_nodes(Variant, M, PI, Count, Facts) :-
795 predicate_property(Variant, dynamic),
796 predicate_property(Variant, incremental),
797 \+ predicate_property(Variant, imported_from(_)),
798 predicate_property(Variant, number_of_clauses(Facts)),
799 idg_node_count(Variant, Count),
800 pi_head(QPI, Variant),
801 unqualify_pi(QPI, M, PI).
802
803
804idg_node_count(Variant, Count) :-
805 aggregate_all(count,
806 ( '$tbl_variant_table'(VTrie),
807 trie_gen(VTrie, Variant, _ATrie)
808 ),
809 Count).
810
811unqualify_pi(M:PI, M, PI) :- !.
812unqualify_pi(PI, _, PI).
813
814tty_width(W) :-
815 catch(tty_size(_, TtyW), _, fail),
816 !,
817 W is max(60, TtyW - 8).
818tty_width(60).
819
(Title, Width) :-
821 format('~N~`\u2015t~*|~n', [Width]),
822 ansi_format([bold], '~t~w~t~*|', [Title,Width]),
823 nl