File: mallocinfo.pl

package info (click to toggle)
swi-prolog 8.0.2+dfsg-3+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (136 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2015, VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(malloc_info,
          [
          ]).
:- use_module(library(apply)).
:- use_module(library(lists)).
:- use_module(library(sgml)).

:- use_foreign_library(foreign(mallocinfo)).

/** <module> 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

  * mallinfo/1
  * malloc_info/1
*/

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

mallinfo(Info) :-
    '$mallinfo'(List),
    dict_create(Info, malinfo, List).
:- endif.

:- if(current_predicate('$malloc_info'/1)).
:- 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.

malloc_info(Info) :-
    '$malloc_info'(XML),
    setup_call_cleanup(
        open_string(XML, In),
        load_xml(In, DOM, [space(remove)]),
        close(In)),
    malloc_dom_prolog(DOM, Info).

malloc_dom_prolog([element(malloc, _, DOM)], Info) :-
    maplist(malloc_prolog, DOM, List),
    partition(is_dict, List, Heaps, Rest),
    dict_create(Info, malloc, [heaps:Heaps|Rest]).

malloc_prolog(element(heap, [nr=NRA], DOM), Heap) :-
    !,
    atom_number(NRA, NR),
    maplist(heap_prolog, DOM, HeapProperties),
    dict_create(Heap, heap, [nr-NR|HeapProperties]).
malloc_prolog(Element, Pair) :-
    misc_field(Element, Pair).

heap_prolog(element(sizes, _, DOM), sizes-Sizes) :-
    !,
    maplist(chunk_size, DOM, Sizes).
heap_prolog(Element, Pair) :-
    misc_field(Element, Pair).

misc_field(element(Name, Attrs0, []), Key-Value) :-
    selectchk(type=Type, Attrs0, Attrs1),
    atomic_list_concat([Name, '_', Type], Key),
    maplist(attr_value, Attrs1, Attrs),
    (   Attrs = [_=Value]
    ->  true
    ;   dict_create(Value, Name, Attrs)
    ).

chunk_size(element(size, Attrs0, []), Dict) :-
    !,
    maplist(attr_value, Attrs0, Attrs),
    dict_create(Dict, size, Attrs).
chunk_size(element(unsorted, Attrs0, []), Dict) :-
    maplist(attr_value, Attrs0, Attrs),
    dict_create(Dict, unsorted, Attrs).

attr_value(Name=In, Name=Out) :-
    atom_number(In, Out),
    !.
attr_value(Name=In, Name=Out) :-
    atom_string(In, Out),
    !.

:- endif.