4.10.1.2 Displaying Statistics

Statistics relating to memory usage, run time, and garbage collection, including information about which areas of memory have overflowed and how much time has been spent expanding them, can be displayed by calling statistics/0.

The output from statistics/0 looks like this:

     memory (total)       1718512 bytes
        global stack       185472 bytes:       1784 in use,    183688 free
        local stack         10496 bytes:        308 in use,     10188 free
        trail stack         92750 bytes:        244 in use,     92506 free
        control stack       92722 bytes:        216 in use,     92506 free
        atoms              109849 bytes:       3698 in use,   1044877 free
        program space     1337072 bytes:    1048136 in use,    288936 free
        program space breakdown:
                 compiled code               469800 bytes
                 predicate                   134624 bytes
                 try_node                    129632 bytes
                 atom                        123064 bytes
                 sw_on_key                    92592 bytes
                 incore_info                  43392 bytes
                 atom table                   36864 bytes
                 interpreted code              7656 bytes
                 atom buffer                   2560 bytes
                 miscellaneous                 2096 bytes
                 BDD hash table                1560 bytes
                 numstack                      1024 bytes
                 FLI stack                     1024 bytes
                 SP_malloc                      952 bytes
                 int_info                       688 bytes
                 source info (itable)           368 bytes
                 module                         128 bytes
                 source info (lheap)             80 bytes
                 foreign resource                32 bytes
         No memory resource errors
     
            0.000 sec. for 3 global, 21 local, and 0 control space overflows
            0.000 sec. for 0 garbage collections which collected 0 bytes
            0.000 sec. for 0 atom garbage collections which collected 0 atoms 0 bytes
            0.010 sec. for 1 memory defragmentations
            0.420 sec. runtime
           26.100 sec. elapsed time

Note the use of indentation to indicate sub-areas. That is, memory contains the program space, global space, and local stack, and the global space contains the global stack and trail.

The memory (total) figure shown as “in use” is the sum of the spaces for the program space and stacks. The “free” figures for the stacks are for free space within those areas. However, this free space is considered used as far as the memory (total) area is concerned, because it has been allocated to the stacks. The program space is not considered to have its own free space. It always allocates new space from the general memory (total) free area.

If a memory resource error has occurred previously in the execution, the memory area for which memory could not be allocated is displayed.

Individual statistics can be obtained by statistics/2, which accepts a keyword and returns a list of statistics related to that keyword.

The keys and values for statistics(Keyword, List) are summarized below. The keywords core and heap are included to retain compatibility with other Prologs. Times are given in milliseconds and sizes are given in bytes.

Keyword
List
global_stack
[size used,free]
This refers to the global stack, where compound terms are stored. The values are gathered before the list holding the answers is allocated.
local_stack
[size used,free]
This refers to the local stack, where recursive predicate environments are stored.
trail
[size used,free]
This refers to the trail stack, where conditional variable bindings are recorded.
choice
[size used,free]
This refers to the choicepoint stack, where partial states are stored for backtracking purposes.
core
memory
[size used,0]
These refer to the amount of memory actually allocated by the Prolog engine. The zero is there for compatibility with other Prolog implementations.
heap
program
[size used,size free]
These refer to the amount of memory allocated for the database, symbol tables, and the like.
runtime
[since start of Prolog,since previous statistics] These refer to CPU time used while executing, excluding time spent garbage collecting, stack shifting, or in system calls. The second element is the time since the last call to statistics/2 with this key. It is not affected by calls to statistics/0.
total_runtime
[since start of Prolog,since previous statistics] These refer to total CPU time used while executing, including memory management such as garbage collection but excluding system calls. The second element is the time since the last call to statistics/2 with this key. It is not affected by calls to statistics/0.
walltime
[since start of Prolog,since previous statistics] These refer to absolute time elapsed. The second element is the time since the last call to statistics/2 with this key. It is not affected by calls to statistics/0.
garbage_collection
[no. of GCs,bytes freed,time spent]
stack_shifts
[no. of global shifts,no. of local/trailtrail shifts,time spent]
atoms
[no. of atoms,bytes used,atoms free] The number of atoms free is the number of atoms allocated (the first element in the list) subtracted from the maximum number of atoms, i.e. 262143 (33554431) on 32-bit (64-bit) architectures. Note that atom garbage collection may be able to reclaim some of the allocated atoms.
atom_garbage_collection
[no. of AGCs,bytes freed,time spent]
defragmentation
[no. of defragmentations,time spent]

To see an example of the use of each of these keywords, type

     | ?- statistics(K, L).

and then repeatedly type `;' to backtrack through all the possible keywords. As an additional example, to report information on the runtime of a predicate p/0, add the following to your program:

     :- statistics(runtime, [T0| _]),
        p,
        statistics(runtime, [T1|_]),
        T is T1 - T0,
        format('p/0 took ~3d sec.~n', [T]).

Send feedback on this subject.