1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2013-2017. 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_ets_cb).
20
21-export([col_to_elem/1,
22	 col_spec/0,
23	 get_info/1,
24	 get_details/2,
25	 get_detail_cols/1,
26	 detail_pages/0
27	]).
28
29-include_lib("wx/include/wx.hrl").
30-include("crashdump_viewer.hrl").
31
32%% Defines
33-define(COL_NAME,  0).
34-define(COL_IS_NAMED,  ?COL_NAME+1).
35-define(COL_OWNER, ?COL_IS_NAMED+1).
36-define(COL_OBJ,   ?COL_OWNER+1).
37-define(COL_MEM,   ?COL_OBJ+1).
38
39%% Callbacks for cdv_virtual_list_wx
40col_to_elem(id) -> col_to_elem(?COL_NAME);
41col_to_elem(?COL_IS_NAMED) -> #ets_table.is_named;
42col_to_elem(?COL_NAME)  -> #ets_table.name;
43col_to_elem(?COL_OWNER) -> #ets_table.pid;
44col_to_elem(?COL_OBJ)   -> #ets_table.size;
45col_to_elem(?COL_MEM)   -> #ets_table.memory.
46
47col_spec() ->
48    [{"Name",    ?wxLIST_FORMAT_LEFT,   200},
49     {"Is Named", ?wxLIST_FORMAT_CENTRE, 70},
50     {"Owner",   ?wxLIST_FORMAT_CENTRE, 120},
51     {"Objects", ?wxLIST_FORMAT_RIGHT,  80},
52     {"Memory",  ?wxLIST_FORMAT_RIGHT,  80}
53    ].
54
55get_info(Owner) ->
56    {ok,Info,TW} = crashdump_viewer:ets_tables(Owner),
57    {Info,TW}.
58
59%% Callbacks for cdv_detail_wx
60get_details(_Id, not_found) ->
61    Info = "The table you are searching for could not be found.",
62    {info,Info};
63get_details(Id, Data) ->
64    Proplist = crashdump_viewer:to_proplist(record_info(fields,ets_table),Data),
65    {ok,{"Table:" ++ Id,Proplist,""}}.
66
67get_detail_cols(all) ->
68    {[{ets, ?COL_NAME}, {process, ?COL_OWNER}],true};
69get_detail_cols(_W) ->
70    {[],true}.
71
72
73%%%%%%%%%%%%%%%%%%%%%%%%
74
75detail_pages() ->
76    [{"Table Information",   fun init_gen_page/2}].
77
78init_gen_page(Parent, Info0) ->
79    Fields = info_fields(),
80    Details = proplists:get_value(details, Info0),
81    Info = if is_map(Details) -> Info0 ++ maps:to_list(Details);
82	      true -> Info0
83	   end,
84    cdv_info_wx:start_link(Parent,{Fields,Info,[]}).
85
86%%% Internal
87info_fields() ->
88    [{"Overview",
89      [{"Id",             id},
90       {"Name",           name},
91       {"Slot",           slot},
92       {"Owner",          pid},
93       {"Data Structure", data_type}
94      ]},
95     {"Settings",
96      [{"Type",           type},
97       {"Protection",     protection},
98       {"Compressed",     compressed},
99       {"Fixed",          fixed},
100       {"Lock write concurrency", write_c},
101       {"Lock read concurrency", read_c}
102      ]},
103     {"Memory Usage",
104      [{"Buckets",        buckets},
105       {"Size",           size},
106       {"Memory",         memory},
107       {"Min Chain Length",  chain_min},
108       {"Avg Chain Length",  chain_avg},
109       {"Max Chain Length",  chain_max},
110       {"Chain Length Std Dev",  chain_stddev},
111       {"Chain Length Expected Std Dev",  chain_exp_stddev}
112      ]}
113    ].
114