View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2017, VU University Amsterdam
    7			 CWI Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_jiti,
   37          [ jiti_list/0,
   38            jiti_list/1                         % +Spec
   39          ]).   40:- use_module(library(apply)).   41:- use_module(library(dcg/basics)).   42
   43:- meta_predicate
   44    jiti_list(:).

Just In Time Indexing (JITI) utilities

This module provides utilities to examine just-in-time indexes created by the system and can help diagnosing space and performance issues.

To be done
- Use print_message/2 and dynamically figure out the column width. */
 jiti_list is det
 jiti_list(:Spec) is det
List the JITI (Just In Time Indexes) of selected predicates. The predicate jiti_list/0 list all just-in-time indexed predicates. The predicate jiti_list/1 takes one of the patterns below. All parts except for Name can be variables. The last pattern takes an arbitrary number of arguments.

The columns use the following notation:

   82jiti_list :-
   83    jiti_list(_:_).
   84
   85jiti_list(Module:Name/Arity) :-
   86    atom(Name),
   87    integer(Arity),
   88    !,
   89    functor(Head, Name, Arity),
   90    jiti_list(Module:Head).
   91jiti_list(Module:Name/Arity) :-
   92    atom(Name),
   93    var(Arity),
   94    !,
   95    freeze(Head, functor(Head, Name, _)),
   96    jiti_list(Module:Head).
   97jiti_list(Module:Name) :-
   98    atom(Name),
   99    !,
  100    freeze(Head, functor(Head, Name, _)),
  101    jiti_list(Module:Head).
  102jiti_list(Head) :-
  103    findall(Head-Indexed,
  104            (   predicate_property(Head, indexed(Indexed)),
  105                \+ predicate_property(Head, imported_from(_))
  106            ), Pairs),
  107    format('Predicate~46|~w ~t~8+ ~t~w~6+ ~t~w~6+ ~t~w~5+~n',
  108           ['Indexed','Buckets','Speedup','Flags']),
  109    format('~`=t~76|~n'),
  110    maplist(print_indexed, Pairs).
  111
  112print_indexed((M:Head)-[Args-hash(Buckets,Speedup,_Size,List)|More]) :-
  113    functor(Head, Name, Arity),
  114    phrase(iarg_spec(Args), ArgsS),
  115    phrase(iflags(List), Flags),
  116    format('~q ~t~48|~s ~t~8+ ~t~D~6+ ~t~1f~8+ ~t~s~3+~n',
  117           [M:Name/Arity, ArgsS,Buckets,Speedup,Flags]),
  118    maplist(print_secondary_index, More),
  119    !.
  120print_indexed(Pair) :-
  121    format('Failed: ~p~n', [Pair]).
  122
  123print_secondary_index(Args-hash(Buckets,Speedup,_Size,List)) :-
  124    phrase(iarg_spec(Args), ArgsS),
  125    phrase(iflags(List), Flags),
  126    format('~t~48|~s ~t~8+ ~t~D~6+ ~t~1f~8+ ~t~s~3+~n',
  127           [ArgsS,Buckets,Speedup,Flags]),
  128    !.
  129print_secondary_index(Pair) :-
  130    format('Secondary failed: ~p~n', [Pair]).
  131
  132iarg_spec(single(N)) -->
  133    number(N).
  134iarg_spec(multi(L)) -->
  135    plus_list(L).
  136iarg_spec(deep(List)) -->
  137    deep_list(List).
  138
  139plus_list([H|T]) -->
  140    number(H),
  141    (   {T==[]}
  142    ->  []
  143    ;   "+",
  144        plus_list(T)
  145    ).
  146
  147deep_list([Last]) -->
  148    !,
  149    iarg_spec(Last).
  150deep_list([H|T]) -->
  151    number(H),
  152    "/",
  153    deep_list(T).
  154
  155
  156iflags(true)  --> "L".
  157iflags(false) --> ""