1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2013-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_sched_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%% Columns
33-define(COL_ID,  0).
34-define(COL_TYPE,  ?COL_ID+1).
35-define(COL_PROC,  ?COL_TYPE+1).
36-define(COL_PORT,  ?COL_PROC+1).
37-define(COL_RQL,   ?COL_PORT+1).
38-define(COL_PQL,   ?COL_RQL+1).
39
40%% Callbacks for cdv_virtual_list_wx
41col_to_elem(id) -> col_to_elem(?COL_ID);
42col_to_elem(?COL_ID)  -> #sched.name;
43col_to_elem(?COL_TYPE)  -> #sched.type;
44col_to_elem(?COL_PROC)  -> #sched.process;
45col_to_elem(?COL_PORT)  -> #sched.port;
46col_to_elem(?COL_RQL)   -> #sched.run_q;
47col_to_elem(?COL_PQL)   -> #sched.port_q.
48
49col_spec() ->
50    [{"Id",                ?wxLIST_FORMAT_RIGHT,   50},
51     {"Type",              ?wxLIST_FORMAT_CENTER, 100},
52     {"Current Process",   ?wxLIST_FORMAT_CENTER, 130},
53     {"Current Port",      ?wxLIST_FORMAT_CENTER, 130},
54     {"Run Queue Length",  ?wxLIST_FORMAT_RIGHT,  180},
55     {"Port Queue Length", ?wxLIST_FORMAT_RIGHT,  180}].
56
57get_info(_) ->
58    {ok,Info,TW} = crashdump_viewer:schedulers(),
59    {Info,TW}.
60
61get_details(_Id, not_found) ->
62    Info = "The scheduler you are searching for could not be found.",
63    {info,Info};
64get_details(Id, Data) ->
65    Proplist = crashdump_viewer:to_proplist(record_info(fields,sched),Data),
66    {ok,{"Scheduler: " ++ Id,Proplist,""}}.
67
68get_detail_cols(all) ->
69    {[{sched, ?COL_ID}, {process, ?COL_PROC}, {process, ?COL_PORT}],true};
70get_detail_cols(_) ->
71    {[],false}.
72
73%%%%%%%%%%%%%%%%%%%%%%%%
74
75detail_pages() ->
76    [{"Scheduler Information", fun init_gen_page/2}].
77
78init_gen_page(Parent, Info0) ->
79    Type = proplists:get_value(type, Info0),
80    Fields = info_fields(Type),
81    Details = proplists:get_value(details, Info0),
82    Info = if is_map(Details) -> Info0 ++ maps:to_list(Details);
83	      true -> Info0
84	   end,
85    cdv_info_wx:start_link(Parent,{Fields,Info,[]}).
86
87%%% Internal
88info_fields(Type) ->
89    [{"Scheduler Overview",
90      [{"Id",             id},
91       {"Type",           type},
92       {"Current Process",process},
93       {"Current Port",   port},
94       {"Sleep Info Flags", sleep_info},
95       {"Sleep Aux Work",   sleep_aux}
96      ]},
97     {run_queues_header(Type),
98      [{"Flags",                   runq_flags},
99       {"Priority Max Length",     runq_max},
100       {"Priority High Length",    runq_high},
101       {"Priority Normal Length",  runq_norm},
102       {"Priority Low Length",     runq_low},
103       {"Port Length",     port_q}
104      ]},
105     {"Current Process",
106      [{"State",           currp_state},
107       {"Internal State",  currp_int_state},
108       {"Program Counter", currp_prg_cnt},
109       {"CP",              currp_cp},
110       {"Stack",           {currp_stack, 0}},
111       {"     ",           {currp_stack, 1}},
112       {"     ",           {currp_stack, 2}},
113       {"     ",           {currp_stack, 3}},
114       {"     ",           {currp_stack, 4}},
115       {"     ",           {currp_stack, 5}},
116       {"     ",           {currp_stack, 6}},
117       {"     ",           {currp_stack, 7}},
118       {"     ",           {currp_stack, 8}},
119       {"     ",           {currp_stack, 9}},
120       {"     ",           {currp_stack, 10}},
121       {"     ",           {currp_stack, 11}}
122      ]}
123    ].
124
125run_queues_header(normal) ->
126    "Run Queues";
127run_queues_header(DirtyX) ->
128    "Run Queues (common for all '" ++ atom_to_list(DirtyX) ++ "' schedulers)".
129