% Copyright (C) 2003 Fred Mesnard <fred@univ-reunion.fr>
%
% This file is part of cTI.
%
% cTI is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% cTI is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
% for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
% USA.
%
% For the most up-to-date information see the cTI's web site:
% http://www.cs.unipr.it/cTI/

:- module(time,[stat_init/1,add_stat/3,stat_final/1]).

:- use_module(library(lists),[member/2]).

stat_init(E) :-
        E=[global_stack-G,local_stack-L,trail-T,
           choice-C,runtime-R,garbage_collection-GC],
        garbage_collect,
        statistics(global_stack,G),
        statistics(local_stack,L),
        statistics(trail,T),
        statistics(choice,C),
        statistics(runtime,[R,_]),
        garbage_collect,
        statistics(garbage_collection,GC).
 
add_stat(E1,Id,E2) :-
	statistics(runtime,[_,Temps]),
	E2=[Id-Temps|E1].
 
stat_final(EtatFinal) :-
%%%         statistics(global_stack,Gf),
%%%         statistics(local_stack,Lf),
%%%         statistics(trail,Tf),
%%%         statistics(choice,Cf),
%%%         statistics(garbage_collection,GCf),
        statistics(runtime,[Rf,_]),
        member1(runtime-R,EtatFinal),
%%%        write('%  --------'),nl,
%%%        write('%  -- STATS '),nl,
	write('%  ------------------------------'),nl,
        write('%  Timings (ms)'),nl,
        write('%  total: '),Temps is Rf-R,write(Temps),nl,
        stat_int(EtatFinal).
%%%         write('%  -------------------------------'),nl,
%%%         write('%  Memory (bytes)'),nl,
%%%         member1(garbage_collection-GC,EtatFinal),
%%%         GC=[Ni,BFi,_TSi],GCf=[Nf,BFf,_TSf],NbGC is Nf-Ni,
%%%         write('%  nombre de gc  : '),write(NbGC),nl,
%%%         (NbGC=0 ->
%%%             (   write('%                   global stack : '),
%%%                 member1(global_stack-G,EtatFinal),
%%%                 G=[Sui,_],Gf=[Suf,_],GS is Suf-Sui,write(GS),nl,
%%%                 write('%                   local stack  : '),
%%%                 member1(local_stack-L,EtatFinal),
%%%                 L=[Lui,_],Lf=[Luf,_],Lu is Luf-Lui,write(Lu),nl,
%%%                 write('%                   trail stack  : '),
%%%                 member1(trail-T,EtatFinal),
%%%                 T=[Tui,_],Tf=[Tuf,_],Tu is Tuf-Tui,write(Tu),nl,
%%%                 write('%                   choice stack : '),
%%%                 member1(choice-C,EtatFinal),
%%%                 C=[Cui,_],Cf=[Cuf,_],Cu is Cuf-Cui,write(Cu),nl)
%%%         ;
%%%             (   write('%  espace libere par gc          : '),
%%%                 NbOc is BFf-BFi,write(NbOc),nl)).
 
stat_int([global_stack-_|_]) :-!.
stat_int([Info-Temps|Etat]) :-
        stat_int(Etat),write('%  '),
        write(Info), % Info sur 10 caracteres
        write(': '),write(Temps),nl.

member1(X,L) :- once(member(X,L)).
