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:- autoload(library(apply),[maplist/3,partition/4]). 39:- autoload(library(lists),[selectchk/3]). 40:- autoload(library(sgml),[load_xml/3]). 41 42:- use_foreign_library(foreign(mallocinfo)). 43 44/** <module> Memory allocation details 45 46This library is provided if the clib package is compiled on a _glibc_ 47based system, typically Linux. It provides access to the glibc ptmalloc 48informational functions for diagnosing memory usage. This library 49exports 50 51 * mallinfo/1 52 * malloc_info/1 53*/ 54 55:- if(current_predicate('$mallinfo'/1)). 56:- export(mallinfo/1). 57 58%! mallinfo(-Info:dict) is det. 59% 60% Return the content of the =|struct mallinfo|= returned by 61% =|mallinfo()|= as a dict. See =|man mallinfo|= for an 62% explanation of the fields. 63% 64% @bug The =|struct mallinfo|= contains =int= fields and is thus 65% incapable of expressing the memory sizes of 64-bit 66% machines. The fields are interpreted as _unsigned_ and 67% thus represent the true value modulo 2**32 (4Gb). 68 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). 76 77%! malloc_info(-Info:dict) is det. 78% 79% Interface to =|malloc_info()|=, which provides an XML document 80% describing the status of the GNU glibc malloc implementation. 81% The XML document is parsed and translated into a dict with a 82% similar structure. The malloc_info() XML is supposed to be 83% self-explanatory. 84% 85% @see [Understanding glibc malloc](https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/) 86 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.