View source with formatted 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:- autoload(library(apply),[maplist/2]).   41:- autoload(library(dcg/basics),[number/3]).   42
   43
   44:- meta_predicate
   45    jiti_list(:).   46
   47/** <module> Just In Time Indexing (JITI) utilities
   48
   49This module provides utilities to   examine just-in-time indexes created
   50by the system and can help diagnosing space and performance issues.
   51
   52@tbd	Use print_message/2 and dynamically figure out the column width.
   53*/
   54
   55
   56%!  jiti_list is det.
   57%!  jiti_list(:Spec) is det.
   58%
   59%   List the JITI (Just In  Time   Indexes)  of selected predicates. The
   60%   predicate jiti_list/0 list all just-in-time  indexed predicates. The
   61%   predicate jiti_list/1 takes one of  the   patterns  below. All parts
   62%   except for Name  can  be  variables.   The  last  pattern  takes  an
   63%   arbitrary number of arguments.
   64%
   65%     - Module:Head
   66%     - Module:Name/Arity
   67%     - Module:Name
   68%
   69%   The columns use the following notation:
   70%
   71%     - The _Indexed_ column describes the argument(s) indexed:
   72%       - A plain integer refers to a 1-based argument number
   73%       - _|A+B|_ is a multi-argument index on the arguments _A_ and _B_.
   74%       - _|A/B|_ is a deep-index on sub-argument _B_ of argument _A_.
   75%     - The _Buckets_ specifies the number of buckets of the hash table
   76%     - The _Speedup_ specifies the selectivity of the index
   77%     - The _Flags_ describes additional properties, currently:
   78%       - =L= denotes that the index contains multiple compound
   79%         terms with the same name/arity that may be used to create
   80%         deep indexes.  The deep indexes themselves are created
   81%         as just-in-time indexes.
   82
   83jiti_list :-
   84    jiti_list(_:_).
   85
   86jiti_list(Module:Name/Arity) :-
   87    atom(Name),
   88    integer(Arity),
   89    !,
   90    functor(Head, Name, Arity),
   91    jiti_list(Module:Head).
   92jiti_list(Module:Name/Arity) :-
   93    atom(Name),
   94    var(Arity),
   95    !,
   96    freeze(Head, functor(Head, Name, _)),
   97    jiti_list(Module:Head).
   98jiti_list(Module:Name) :-
   99    atom(Name),
  100    !,
  101    freeze(Head, functor(Head, Name, _)),
  102    jiti_list(Module:Head).
  103jiti_list(Head) :-
  104    findall(Head-Indexed,
  105            (   predicate_property(Head, indexed(Indexed)),
  106                \+ predicate_property(Head, imported_from(_))
  107            ), Pairs),
  108    format('Predicate~46|~w ~t~8+ ~t~w~6+ ~t~w~6+ ~t~w~5+~n',
  109           ['Indexed','Buckets','Speedup','Flags']),
  110    format('~`=t~76|~n'),
  111    maplist(print_indexed, Pairs).
  112
  113print_indexed((M:Head)-[Args-hash(Buckets,Speedup,_Size,List)|More]) :-
  114    functor(Head, Name, Arity),
  115    phrase(iarg_spec(Args), ArgsS),
  116    phrase(iflags(List), Flags),
  117    format('~q ~t~48|~s ~t~8+ ~t~D~6+ ~t~1f~8+ ~t~s~3+~n',
  118           [M:Name/Arity, ArgsS,Buckets,Speedup,Flags]),
  119    maplist(print_secondary_index, More),
  120    !.
  121print_indexed(Pair) :-
  122    format('Failed: ~p~n', [Pair]).
  123
  124print_secondary_index(Args-hash(Buckets,Speedup,_Size,List)) :-
  125    phrase(iarg_spec(Args), ArgsS),
  126    phrase(iflags(List), Flags),
  127    format('~t~48|~s ~t~8+ ~t~D~6+ ~t~1f~8+ ~t~s~3+~n',
  128           [ArgsS,Buckets,Speedup,Flags]),
  129    !.
  130print_secondary_index(Pair) :-
  131    format('Secondary failed: ~p~n', [Pair]).
  132
  133iarg_spec(single(N)) -->
  134    number(N).
  135iarg_spec(multi(L)) -->
  136    plus_list(L).
  137iarg_spec(deep(List)) -->
  138    deep_list(List).
  139
  140plus_list([H|T]) -->
  141    number(H),
  142    (   {T==[]}
  143    ->  []
  144    ;   "+",
  145        plus_list(T)
  146    ).
  147
  148deep_list([Last]) -->
  149    !,
  150    iarg_spec(Last).
  151deep_list([H|T]) -->
  152    number(H),
  153    "/",
  154    deep_list(T).
  155
  156
  157iflags(true)  --> "L".
  158iflags(false) --> ""