1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2011-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19-module(cdv_mem_cb).
20
21-export([get_info/0]).
22
23-include("crashdump_viewer.hrl").
24-include_lib("wx/include/wx.hrl").
25
26get_info() ->
27    observer_lib:report_progress({ok,"Processing memory info"}),
28    MemInfo = get_mem_info(),
29    observer_lib:report_progress({ok,33}),
30    {AllocInfo,AllocTW} = get_alloc_info(),
31    observer_lib:report_progress({ok,66}),
32    AreaInfo = get_area_info(),
33    observer_lib:report_progress({ok,100}),
34    [{"Memory",cdv_info_wx,MemInfo}
35     | [{Title,cdv_table_wx,{Cols,Data,AllocTW}} ||
36	   {Title,Cols,Data} <- AllocInfo]] ++
37	[{"Allocated Areas",cdv_table_wx,AreaInfo}].
38
39
40%%%-----------------------------------------------------------------
41%%% Memory page
42get_mem_info() ->
43    {ok,Info,TW} = crashdump_viewer:memory(),
44    {[{"Memory Information",gen_mem_info_fields(Info)}],Info,TW}.
45
46gen_mem_info_fields([{Key,_}|T]) ->
47    [{upper(atom_to_list(Key)),{bytes,Key}}|gen_mem_info_fields(T)];
48gen_mem_info_fields([]) ->
49    [].
50
51upper(Key) ->
52    lists:join(" ", [string:titlecase(Word) || Word <- string:split(Key, "_", all)]).
53
54%%%-----------------------------------------------------------------
55%%% Allocated areas page
56get_area_info() ->
57    {ok,Info0,TW} = crashdump_viewer:allocated_areas(),
58    Info = [tuple_to_list(R) || R <- Info0],
59    {area_columns(),Info,TW}.
60
61area_columns() ->
62    [{"",                 ?wxLIST_FORMAT_LEFT,  150},
63     {"Allocated (bytes)",?wxLIST_FORMAT_RIGHT, 150},
64     {"Used (bytes)",     ?wxLIST_FORMAT_RIGHT, 150}].
65
66%%%-----------------------------------------------------------------
67%%% Allocator page
68get_alloc_info() ->
69    {ok,Info,TW} = crashdump_viewer:allocator_info(),
70    {fix_alloc(Info),TW}.
71
72fix_alloc([{Title,Columns,Data}|Tables]) ->
73    [{Title,alloc_columns(Columns),
74     [[Key|Values] || {Key,Values} <- Data]} |
75     fix_alloc(Tables)];
76fix_alloc([{Title,[{_,V}|_]=Data}|Tables]) ->
77    fix_alloc([{Title,lists:duplicate(length(V),[]),Data}|Tables]);
78fix_alloc([{"",[]}|Tables]) -> % no name and no data, probably truncated dump
79    fix_alloc(Tables);
80fix_alloc([{Title,[]=Data}|Tables]) -> % no data, probably truncated dump
81    fix_alloc([{Title,[],Data}|Tables]);
82fix_alloc([]) ->
83    [].
84
85alloc_columns(Columns) ->
86    [{"",   ?wxLIST_FORMAT_LEFT,  180} |
87     [{Column, ?wxLIST_FORMAT_RIGHT, 140} || Column <- Columns]].
88