This file is indexed.

/usr/lib/swi-prolog/library/statistics.pl is in swi-prolog-nox 7.6.4+dfsg-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  1999-2016, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(prolog_statistics,
          [ statistics/0,
            statistics/1,               % -Stats
            thread_statistics/2,        % ?Thread, -Stats
            time/1,                     % :Goal
            profile/1,                  % :Goal
            profile/2,                  % :Goal, +Options
            show_profile/1              % +Options
          ]).
:- use_module(library(lists)).
:- use_module(library(pairs)).
:- use_module(library(option)).
:- use_module(library(error)).
:- set_prolog_flag(generate_debug_info, false).

:- meta_predicate
    time(0),
    profile(0),
    profile(0, +).

/** <module> Get information about resource usage

This library provides predicates to   obtain  information about resource
usage by your program. The predicates of  this library are for human use
at the toplevel: information is _printed_.   All predicates obtain their
information using public low-level primitives.   These primitives can be
use to obtain selective statistics during execution.
*/

%!  statistics is det.
%
%   Print information about resource usage using print_message/2.
%
%   @see    All statistics printed are obtained through statistics/2.

statistics :-
    phrase(collect_stats, Stats),
    print_message(information, statistics(Stats)).

%!  statistics(-Stats:dict) is det.
%
%   Stats  is  a  dict   representing    the   same  information  as
%   statistics/0. This convience function is   primarily intended to
%   pass  statistical  information  to  e.g.,  a  web  client.  Time
%   critical code that wishes to   collect statistics typically only
%   need a small subset  and  should   use  statistics/2  to  obtain
%   exactly the data they need.

statistics(Stats) :-
    phrase(collect_stats, [CoreStats|StatList]),
    dict_pairs(CoreStats, _, CorePairs),
    map_list_to_pairs(dict_key, StatList, ExtraPairs),
    append(CorePairs, ExtraPairs, Pairs),
    dict_pairs(Stats, statistics, Pairs).

dict_key(Dict, Key) :-
    gc{type:atom} :< Dict,
    !,
    Key = agc.
dict_key(Dict, Key) :-
    gc{type:clause} :< Dict,
    !,
    Key = cgc.
dict_key(Dict, Key) :-
    is_dict(Dict, Key).

collect_stats -->
    core_statistics,
    gc_statistics,
    agc_statistics,
    cgc_statistics,
    shift_statistics,
    thread_counts,
    engine_counts.

core_statistics -->
    { statistics(process_cputime, Cputime),
      statistics(process_epoch, Epoch),
      statistics(inferences, Inferences),
      statistics(atoms, Atoms),
      statistics(functors, Functors),
      statistics(predicates, Predicates),
      statistics(modules, Modules),
      statistics(codes, Codes),
      thread_self(Me),
      thread_stack_statistics(Me, Stacks)
    },
    [ core{ time:time{cpu:Cputime, inferences:Inferences, epoch:Epoch},
            data:counts{atoms:Atoms, functors:Functors,
                        predicates:Predicates, modules:Modules,
                        vm_codes:Codes},
            stacks:Stacks
          }
    ].

:- if(\+current_predicate(thread_statistics/3)).
thread_statistics(_Thread, Key, Value) :-
    statistics(Key, Value).
:- endif.

thread_stack_statistics(Thread,
                  stacks{local:stack{name:local,
                                    limit:LocalLimit,
                                     allocated:Local,
                                     usage:LocalUsed},
                         global:stack{name:global,
                                      limit:GlobalLimit,
                                      allocated:Global,
                                      usage:GlobalUsed},
                         trail:stack{name:trail,
                                     limit:TrailLimit,
                                     allocated:Trail,
                                     usage:TrailUsed},
                         total:stack{name:stacks,
                                     limit:StackLimit,
                                     allocated:StackAllocated,
                                     usage:StackUsed}
                        }) :-
    thread_statistics(Thread, trail,       Trail),
    thread_statistics(Thread, trailused,   TrailUsed),
    thread_statistics(Thread, local,       Local),
    thread_statistics(Thread, localused,   LocalUsed),
    thread_statistics(Thread, global,      Global),
    thread_statistics(Thread, globalused,  GlobalUsed),
    thread_statistics(Thread, locallimit,  LocalLimit),
    thread_statistics(Thread, globallimit, GlobalLimit),
    thread_statistics(Thread, traillimit,  TrailLimit),
    StackUsed is LocalUsed+GlobalUsed+TrailUsed,
    StackAllocated is Local+Global+Trail,
    StackLimit is LocalLimit+GlobalLimit+TrailLimit.

gc_statistics -->
    { statistics(collections, Collections),
      Collections > 0,
      !,
      statistics(collected, Collected),
      statistics(gctime, GcTime)
    },
    [ gc{type:stack, unit:byte,
         count:Collections, time:GcTime, gained:Collected } ].
gc_statistics --> [].

agc_statistics -->
    { catch(statistics(agc, Agc), _, fail),
      Agc > 0,
      !,
      statistics(agc_gained, Gained),
      statistics(agc_time, Time)
    },
    [ gc{type:atom, unit:atom,
         count:Agc, time:Time, gained:Gained} ].
agc_statistics --> [].

cgc_statistics -->
    { catch(statistics(cgc, Cgc), _, fail),
      Cgc > 0,
      !,
      statistics(cgc_gained, Gained),
      statistics(cgc_time, Time)
    },
    [ gc{type:clause, unit:clause,
         count:Cgc, time:Time, gained:Gained} ].
cgc_statistics --> [].

shift_statistics -->
    { statistics(local_shifts, LS),
      statistics(global_shifts, GS),
      statistics(trail_shifts, TS),
      (   LS > 0
      ;   GS > 0
      ;   TS > 0
      ),
      !,
      statistics(shift_time, Time)
    },
    [ shift{local:LS, global:GS, trail:TS, time:Time} ].
shift_statistics --> [].

thread_counts -->
    { current_prolog_flag(threads, true),
      statistics(threads, Active),
      statistics(threads_created, Created),
      Created > 1,
      !,
      statistics(thread_cputime, CpuTime),
      Finished is Created - Active
    },
    [ thread{count:Active, finished:Finished, time:CpuTime} ].
thread_counts --> [].

engine_counts -->
    { current_prolog_flag(threads, true),
      statistics(engines, Active),
      statistics(engines_created, Created),
      Created > 0,
      !,
      Finished is Created - Active
    },
    [ engine{count:Active, finished:Finished} ].
engine_counts --> [].


%!  thread_statistics(?Thread, -Stats:dict) is nondet.
%
%   Obtain statistical information about a single thread.  Fails
%   silently of the Thread is no longer alive.
%
%   @arg    Stats is a dict containing status, time and stack-size
%           information about Thread.

thread_statistics(Thread, Stats) :-
    thread_property(Thread, status(Status)),
    human_thread_id(Thread, Id),
    (   catch(thread_stats(Thread, Stacks, Time), _, fail)
    ->  Stats = thread{id:Id,
                       status:Status,
                       time:Time,
                       stacks:Stacks}
    ;   Stats = thread{id:Thread,
                       status:Status}
    ).

human_thread_id(Thread, Id) :-
    atom(Thread),
    !,
    Id = Thread.
human_thread_id(Thread, Id) :-
    thread_property(Thread, id(Id)).

thread_stats(Thread, Stacks,
             time{cpu:CpuTime,
                  inferences:Inferences,
                  epoch:Epoch
                 }) :-
    thread_statistics(Thread, cputime, CpuTime),
    thread_statistics(Thread, inferences, Inferences),
    thread_statistics(Thread, epoch, Epoch),
    thread_stack_statistics(Thread, Stacks).


%!  time(:Goal) is nondet.
%
%   Execute Goal, reporting statistics to the user. If Goal succeeds
%   non-deterministically,  retrying  reports  the   statistics  for
%   providing the next answer.
%
%   Statistics  are  retrieved  using   thread_statistics/3  on  the
%   calling   thread.   Note   that   not    all   systems   support
%   thread-specific CPU time. Notable, this is lacking on MacOS X.
%
%   @bug Inference statistics are often a few off.
%   @see statistics/2 for obtaining statistics in your program and
%        understanding the reported values.

time(Goal) :-
    time_state(State0),
    (   call_cleanup(catch(Goal, E, (report(State0,10), throw(E))),
                     Det = true),
        time_true(State0),
        (   Det == true
        ->  !
        ;   true
        )
    ;   report(State0, 11),
        fail
    ).

report(t(OldWall, OldTime, OldInferences), Sub) :-
    time_state(t(NewWall, NewTime, NewInferences)),
    UsedTime is NewTime - OldTime,
    UsedInf  is NewInferences - OldInferences - Sub,
    Wall     is NewWall - OldWall,
    (   UsedTime =:= 0
    ->  Lips = 'Infinite'
    ;   Lips is integer(UsedInf / UsedTime)
    ),
    print_message(information, time(UsedInf, UsedTime, Wall, Lips)).

time_state(t(Wall, Time, Inferences)) :-
    get_time(Wall),
    statistics(cputime, Time),
    statistics(inferences, Inferences).

time_true(State0) :-
    report(State0, 12).             % leave choice-point
time_true(State) :-
    get_time(Wall),
    statistics(cputime, Time),
    statistics(inferences, Inferences0),
    plus(Inferences0, -3, Inferences),
    nb_setarg(1, State, Wall),
    nb_setarg(2, State, Time),
    nb_setarg(3, State, Inferences),
    fail.


                 /*******************************
                 *     EXECUTION PROFILING      *
                 *******************************/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This module provides a simple backward compatibility frontend on the new
(in version 5.1.10) execution profiler  with  a   hook  to  the  new GUI
visualiser for profiling results defined in library('swi/pce_profile').

Later we will add a proper textual report-generator.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- multifile
    prolog:show_profile_hook/1.

%!  profile(:Goal).
%!  profile(:Goal, +Options).
%
%   Run Goal under the execution profiler.  Defined options are:
%
%     * time(Which)
%     Profile =cpu= or =wall= time.  The default is CPU time.
%     * top(N)
%     When generating a textual report, show the top N predicates.
%     * cumulative(Bool)
%     If =true= (default =false=), show cumulative output in
%     a textual report.

profile(Goal) :-
    profile(Goal, []).

profile(Goal0, Options) :-
    option(time(Which), Options, cpu),
    time_name(Which, How),
    expand_goal(Goal0, Goal),
    call_cleanup('$profile'(Goal, How),
                 prolog_statistics:show_profile(Options)).

time_name(cpu,      cputime)  :- !.
time_name(wall,     walltime) :- !.
time_name(cputime,  cputime)  :- !.
time_name(walltime, walltime) :- !.
time_name(Time, _) :-
    must_be(oneof([cpu,wall]), Time).

%!  show_profile(+Options)
%
%   Display last collected profiling data.  Options are
%
%     * top(N)
%     When generating a textual report, show the top N predicates.
%     * cumulative(Bool)
%     If =true= (default =false=), show cumulative output in
%     a textual report.

show_profile(N) :-
    integer(N),
    !,
    show_profile([top(N)]).
show_profile(Options) :-
    profiler(Old, false),
    show_profile_(Options),
    profiler(_, Old).

show_profile_(Options) :-
    prolog:show_profile_hook(Options),
    !.
show_profile_(Options) :-
    prof_statistics(Stat),
    prof_statistics(time, Stat, Time),
    sort_on(Options, SortKey),
    findall(KeyedNode, prof_node(SortKey, KeyedNode), Nodes),
    sort(1, >=, Nodes, Sorted),
    format('~`=t~69|~n'),
    format('Total time: ~3f seconds~n', [Time]),
    format('~`=t~69|~n'),
    format('~w~t~w =~45|~t~w~60|~t~w~69|~n',
           [ 'Predicate', 'Box Entries', 'Calls+Redos', 'Time'
           ]),
    format('~`=t~69|~n'),
    option(top(N), Options, 25),
    show_plain(Sorted, N, Stat, SortKey).

sort_on(Options, ticks_self) :-
    option(cumulative(false), Options, false),
    !.
sort_on(_, ticks).

show_plain([], _, _, _).
show_plain(_, 0, _, _) :- !.
show_plain([_-H|T], N, Stat, Key) :-
    show_plain(H, Stat, Key),
    N2 is N - 1,
    show_plain(T, N2, Stat, Key).

show_plain(Node, Stat, Key) :-
    value(label,                       Node, Pred),
    value(call,                        Node, Call),
    value(redo,                        Node, Redo),
    value(time(Key, percentage, Stat), Node, Percent),
    IntPercent is round(Percent*10),
    Entry is Call + Redo,
    format('~w~t~D =~45|~t~D+~55|~D ~t~1d%~69|~n',
           [Pred, Entry, Call, Redo, IntPercent]).


                 /*******************************
                 *         DATA GATHERING       *
                 *******************************/

%!  prof_statistics(-Node) is det.
%
%   Get overall statistics
%
%   @param Node     term of the format prof(Ticks, Account, Time, Nodes)

prof_statistics(prof(Samples, Ticks, Account, Time, Nodes)) :-
    '$prof_statistics'(Samples, Ticks, Account, Time, Nodes).

prof_statistics(samples, Term, Samples) :-
    arg(1, Term, Samples).
prof_statistics(ticks, Term, Ticks) :-
    arg(2, Term, Ticks).
prof_statistics(accounting, Term, Ticks) :-
    arg(3, Term, Ticks).
prof_statistics(time, Term, Ticks) :-
    arg(4, Term, Ticks).
prof_statistics(nodes, Term, Ticks) :-
    arg(5, Term, Ticks).


%!  prof_node(+Field, -Pairs) is nondet.
%
%   Collect data for each of the interesting predicate.
%
%   @param Field specifies the field to use as key in each pair.
%   @param Pair is a term of the following format:
%
%     ==
%     KeyValue-node(Pred,
%                   TimeSelf, TimeSiblings,
%                   Calls, Redo, Recursive,
%                   Parents)
%     ==
%

prof_node(KeyOn, Node) :-
    setup_call_cleanup(
        ( current_prolog_flag(access_level, Old),
          set_prolog_flag(access_level, system)
        ),
        get_prof_node(KeyOn, Node),
        set_prolog_flag(access_level, Old)).

get_prof_node(KeyOn, Key-Node) :-
    Node = node(M:H,
                TicksSelf, TicksSiblings,
                Call, Redo,
                Parents, Siblings),
    current_predicate(_, M:H),
    \+ predicate_property(M:H, imported_from(_)),
    '$prof_procedure_data'(M:H,
                           TicksSelf, TicksSiblings,
                           Call, Redo,
                           Parents, Siblings),
    value(KeyOn, Node, Key).

key(predicate,      1).
key(ticks_self,     2).
key(ticks_siblings, 3).
key(call,           4).
key(redo,           5).
key(callers,        6).
key(callees,        7).

value(name, Data, Name) :-
    !,
    arg(1, Data, Pred),
    predicate_functor_name(Pred, Name).
value(label, Data, Label) :-
    !,
    arg(1, Data, Pred),
    predicate_label(Pred, Label).
value(ticks, Data, Ticks) :-
    !,
    arg(2, Data, Self),
    arg(3, Data, Siblings),
    Ticks is Self + Siblings.
value(time(Key, percentage, Stat), Data, Percent) :-
    !,
    value(Key, Data, Ticks),
    prof_statistics(ticks, Stat, Total),
    prof_statistics(accounting, Stat, Account),
    (   Total-Account > 0
    ->  Percent is 100 * (Ticks/(Total-Account))
    ;   Percent is 0.0
    ).
value(Name, Data, Value) :-
    key(Name, Arg),
    arg(Arg, Data, Value).

%!  predicate_label(+Head, -Label)
%
%   Create a human-readable label for the given head

predicate_label(M:H, Label) :-
    !,
    functor(H, Name, Arity),
    (   hidden_module(M, H)
    ->  atomic_list_concat([Name, /, Arity], Label)
    ;   atomic_list_concat([M, :, Name, /, Arity], Label)
    ).
predicate_label(H, Label) :-
    !,
    functor(H, Name, Arity),
    atomic_list_concat([Name, /, Arity], Label).

hidden_module(system, _).
hidden_module(user, _).
hidden_module(M, H) :-
    predicate_property(system:H, imported_from(M)).

%!  predicate_functor_name(+Head, -Name)
%
%   Return the (module-free) name of the predicate for sorting
%   purposes.

predicate_functor_name(_:H, Name) :-
    !,
    predicate_functor_name(H, Name).
predicate_functor_name(H, Name) :-
    functor(H, Name, _Arity).


                 /*******************************
                 *            MESSAGES          *
                 *******************************/

:- multifile
    prolog:message/3.

% NOTE: The code below uses get_dict/3 rather than the functional
% notation to make this code work with `swipl --traditional`

prolog:message(time(UsedInf, UsedTime, Wall, Lips)) -->
    [ '~D inferences, ~3f CPU in ~3f seconds (~w% CPU, ~w Lips)'-
      [UsedInf, UsedTime, Wall, Perc, Lips] ],
    {   Wall > 0
    ->  Perc is round(100*UsedTime/Wall)
    ;   Perc = ?
    }.
prolog:message(statistics(List)) -->
    msg_statistics(List).

msg_statistics([]) --> [].
msg_statistics([H|T]) -->
    { is_dict(H, Tag) },
    msg_statistics(Tag, H),
    (   { T == [] }
    ->  []
    ;   [nl], msg_statistics(T)
    ).

msg_statistics(core, S) -->
    { get_dict(time, S, Time),
      get_dict(data, S, Data),
      get_dict(stacks, S, Stacks)
    },
    time_stats(Time), [nl],
    data_stats(Data), [nl,nl],
    stacks_stats(Stacks).
msg_statistics(gc, S) -->
    {   (   get_dict(type, S, stack)
        ->  Label = ''
        ;   get_dict(type, S, Type),
            string_concat(Type, " ", Label)
        ),
        get_dict(count, S, Count),
        get_dict(gained, S, Gained),
        get_dict(unit, S, Unit),
        get_dict(time, S, Time)
    },
    [ '~D ~wgarbage collections gained ~D ~ws in ~3f seconds.'-
      [ Count, Label, Gained, Unit, Time]
    ].
msg_statistics(shift, S) -->
    { get_dict(local, S, Local),
      get_dict(global, S, Global),
      get_dict(trail, S, Trail),
      get_dict(time, S, Time)
    },
    [ 'Stack shifts: ~D local, ~D global, ~D trail in ~3f seconds'-
      [ Local, Global, Trail, Time ]
    ].
msg_statistics(thread, S) -->
    { get_dict(count, S, Count),
      get_dict(finished, S, Finished),
      get_dict(time, S, Time)
    },
    [ '~D threads, ~D finished threads used ~3f seconds'-
      [Count, Finished, Time]
    ].
msg_statistics(engine, S) -->
    { get_dict(count, S, Count),
      get_dict(finished, S, Finished)
    },
    [ '~D engines, ~D finished engines'-
      [Count, Finished]
    ].

time_stats(T) -->
    { get_dict(epoch, T, Epoch),
      format_time(string(EpochS), '%+', Epoch),
      get_dict(cpu, T, CPU),
      get_dict(inferences, T, Inferences)
    },
    [ 'Started at ~s'-[EpochS], nl,
      '~3f seconds cpu time for ~D inferences'-
      [ CPU, Inferences ]
    ].
data_stats(C) -->
    { get_dict(atoms, C, Atoms),
      get_dict(functors, C, Functors),
      get_dict(predicates, C, Predicates),
      get_dict(modules, C, Modules),
      get_dict(vm_codes, C, VMCodes)
    },
    [ '~D atoms, ~D functors, ~D predicates, ~D modules, ~D VM-codes'-
      [ Atoms, Functors, Predicates, Modules, VMCodes]
    ].
stacks_stats(S) -->
    { get_dict(local, S, Local),
      get_dict(global, S, Global),
      get_dict(trail, S, Trail)
    },
    [ '~|~tLimit~28+~tAllocated~13+~tIn use~13+'-[], nl ],
    stack_stats('Local ', Local),  [nl],
    stack_stats('Global', Global), [nl],
    stack_stats('Trail ', Trail),  [nl].

stack_stats(Stack, S) -->
    { get_dict(limit, S, Limit),
      get_dict(allocated, S, Allocated),
      get_dict(usage, S, Usage)
    },
    [ '~|~w stack:~t~D~28+ ~t~D~13+ ~t~D~13+ Bytes'-
      [Stack, Limit, Allocated, Usage]
    ].

:- multifile sandbox:safe_primitive/1.

sandbox:safe_primitive(prolog_statistics:statistics(_)).
sandbox:safe_primitive(prolog_statistics:statistics).
sandbox:safe_meta_predicate(prolog_statistics:profile/1).
sandbox:safe_meta_predicate(prolog_statistics:profile/2).