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)).
55:- if(current_predicate('$mallinfo'/1)). 56:- export(mallinfo/1).
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).
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.
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
*/