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 of 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.
   85malloc_info(Info) :-
   86    '$malloc_info'(XML),
   87    setup_call_cleanup(
   88        open_string(XML, In),
   89        load_xml(In, DOM, [space(remove)]),
   90        close(In)),
   91    malloc_dom_prolog(DOM, Info).
   92
   93malloc_dom_prolog([element(malloc, _, DOM)], Info) :-
   94    maplist(malloc_prolog, DOM, List),
   95    partition(is_dict, List, Heaps, Rest),
   96    dict_create(Info, malloc, [heaps:Heaps|Rest]).
   97
   98malloc_prolog(element(heap, [nr=NRA], DOM), Heap) :-
   99    !,
  100    atom_number(NRA, NR),
  101    maplist(heap_prolog, DOM, HeapProperties),
  102    dict_create(Heap, heap, [nr-NR|HeapProperties]).
  103malloc_prolog(Element, Pair) :-
  104    misc_field(Element, Pair).
  105
  106heap_prolog(element(sizes, _, DOM), sizes-Sizes) :-
  107    !,
  108    maplist(chunk_size, DOM, Sizes).
  109heap_prolog(Element, Pair) :-
  110    misc_field(Element, Pair).
  111
  112misc_field(element(Name, Attrs0, []), Key-Value) :-
  113    selectchk(type=Type, Attrs0, Attrs1),
  114    atomic_list_concat([Name, '_', Type], Key),
  115    maplist(attr_value, Attrs1, Attrs),
  116    (   Attrs = [_=Value]
  117    ->  true
  118    ;   dict_create(Value, Name, Attrs)
  119    ).
  120
  121chunk_size(element(size, Attrs0, []), Dict) :-
  122    !,
  123    maplist(attr_value, Attrs0, Attrs),
  124    dict_create(Dict, size, Attrs).
  125chunk_size(element(unsorted, Attrs0, []), Dict) :-
  126    maplist(attr_value, Attrs0, Attrs),
  127    dict_create(Dict, unsorted, Attrs).
  128
  129attr_value(Name=In, Name=Out) :-
  130    atom_number(In, Out),
  131    !.
  132attr_value(Name=In, Name=Out) :-
  133    atom_string(In, Out),
  134    !.
  135
  136:- endif.