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)  2015, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(malloc_info,
   36          [
   37          ]).   38:- use_module(library(apply)).   39:- use_module(library(lists)).   40:- use_module(library(sgml)).   41
   42:- use_foreign_library(foreign(mallocinfo)).

Memory allocation details

This library is provided if the clib package is compiled on a glibc based system, typically Linux. It provides access to the glibc ptmalloc informational functions for diagnosing memory usage. This library exports

*/

   55:- if(current_predicate('$mallinfo'/1)).   56:- export(mallinfo/1).
 mallinfo(-Info:dict) is det
Return the content of the struct mallinfo returned by mallinfo() as a dict. See man mallinfo for an explanation of the fields.
bug
- The struct mallinfo contains int fields and is thus incapable of expressing the memory sizes of 64-bit machines. The fields are interpreted as unsigned and thus represent the true value modulo 2**32 (4Gb).
   69mallinfo(Info) :-
   70    '$mallinfo'(List),
   71    dict_create(Info, malinfo, List).
   72:- endif.   73
   74:- if(current_predicate('$malloc_info'/1)).   75:- export(malloc_info/1).
 malloc_info(-Info:dict) is det
Interface to malloc_info(), which provides an XML document describing the status of the GNU glibc malloc implementation. The XML document is parsed and translated into a dict with a similar structure. The malloc_info() XML is supposed to be self-explanatory.
See also
- Understanding glibc malloc
   87malloc_info(Info) :-
   88    '$malloc_info'(XML),
   89    setup_call_cleanup(
   90        open_string(XML, In),
   91        load_xml(In, DOM, [space(remove)]),
   92        close(In)),
   93    malloc_dom_prolog(DOM, Info).
   94
   95malloc_dom_prolog([element(malloc, _, DOM)], Info) :-
   96    maplist(malloc_prolog, DOM, List),
   97    partition(is_dict, List, Heaps, Rest),
   98    dict_create(Info, malloc, [heaps:Heaps|Rest]).
   99
  100malloc_prolog(element(heap, [nr=NRA], DOM), Heap) :-
  101    !,
  102    atom_number(NRA, NR),
  103    maplist(heap_prolog, DOM, HeapProperties),
  104    dict_create(Heap, heap, [nr-NR|HeapProperties]).
  105malloc_prolog(Element, Pair) :-
  106    misc_field(Element, Pair).
  107
  108heap_prolog(element(sizes, _, DOM), sizes-Sizes) :-
  109    !,
  110    maplist(chunk_size, DOM, Sizes).
  111heap_prolog(Element, Pair) :-
  112    misc_field(Element, Pair).
  113
  114misc_field(element(Name, Attrs0, []), Key-Value) :-
  115    selectchk(type=Type, Attrs0, Attrs1),
  116    atomic_list_concat([Name, '_', Type], Key),
  117    maplist(attr_value, Attrs1, Attrs),
  118    (   Attrs = [_=Value]
  119    ->  true
  120    ;   dict_create(Value, Name, Attrs)
  121    ).
  122
  123chunk_size(element(size, Attrs0, []), Dict) :-
  124    !,
  125    maplist(attr_value, Attrs0, Attrs),
  126    dict_create(Dict, size, Attrs).
  127chunk_size(element(unsorted, Attrs0, []), Dict) :-
  128    maplist(attr_value, Attrs0, Attrs),
  129    dict_create(Dict, unsorted, Attrs).
  130
  131attr_value(Name=In, Name=Out) :-
  132    atom_number(In, Out),
  133    !.
  134attr_value(Name=In, Name=Out) :-
  135    atom_string(In, Out),
  136    !.
  137
  138:- endif.