aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/src/dialyzer_worker.erl
blob: 0efa5d9b01441609ae31800e4be51d47c02177ae (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%% -*- erlang-indent-level: 2 -*-
%%-----------------------------------------------------------------------
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2006-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

-module(dialyzer_worker).

-export([launch/4]).

-export_type([worker/0]).

-type worker() :: pid(). %%opaque

-type mode() :: dialyzer_coordinator:mode().
-type coordinator() :: dialyzer_coordinator:coordinator().
-type servers() :: dialyzer_succ_typings:servers() |
		   dialyzer_analysis_callgraph:servers().
-type data()    :: dialyzer_succ_typings:scc_data() |
		   dialyzer_succ_typings:scc_refine_data().

-record(state, {
	  mode             :: mode(),
	  job              :: mfa_or_funlbl() | file:filename(),
	  coordinator      :: coordinator(),
	  servers          :: servers(),
	  depends_on  = [] :: list(),
	  scc_data         :: data()
	 }).

-include("dialyzer.hrl").

%% -define(DEBUG, true).

-ifdef(DEBUG).
-define(debug(X__, Y__), io:format(X__, Y__)).
-else.
-define(debug(X__, Y__), ok).
-endif.

%%--------------------------------------------------------------------

-spec launch(mode(), [mfa_or_funlbl()], servers(), coordinator()) -> worker().

launch(Mode, Job, Servers, Coordinator) ->
  State = #state{mode        = Mode,
		 job         = Job,
		 servers     = Servers,
		 coordinator = Coordinator},
  InitState =
    case Mode of
      X when X =:= 'typesig'; X =:= 'dataflow' -> initializing;
      X when X =:= 'compile'; X =:= 'warnings' -> running
    end,
  spawn(fun() -> loop(InitState, State) end).

%%--------------------------------------------------------------------

loop(updating, State) ->
  ?debug("Update: ~p\n",[State#state.job]),
  NextStatus =
    case waits_more_success_typings(State) of
      true -> waiting;
      Other ->
	case has_data(State) of
	  false -> getting_data;
	  true ->
	    case Other of
	      imminent -> waiting;
	      false -> running
	    end
	end
    end,
  loop(NextStatus, State);
loop(initializing, #state{job = SCC, servers = Servers} = State) ->
  DependsOn = dialyzer_succ_typings:find_depends_on(SCC, Servers),
  ?debug("Deps ~p: ~p\n",[State#state.job, DependsOn]),
  loop(updating, State#state{depends_on = DependsOn});
loop(waiting, State) ->
  ?debug("Wait: ~p\n",[State#state.job]),
  NewState = wait_for_success_typings(State),
  loop(updating, NewState);
loop(getting_data, State) ->
  ?debug("Data: ~p\n",[State#state.job]),
  loop(updating, get_data(State));
loop(running, #state{mode = 'compile'} = State) ->
  ?debug("Compile: ~s\n",[State#state.job]),
  Result =
    case start_compilation(State) of
      {ok, EstimatedSize, Data} ->
	Label = ask_coordinator_for_label(EstimatedSize, State),
	continue_compilation(Label, Data);
      {error, _Reason} = Error ->
	Error
    end,
  report_to_coordinator(Result, State);
loop(running, #state{mode = 'warnings'} = State) ->
  ?debug("Warning: ~s\n",[State#state.job]),
  Result = collect_warnings(State),
  report_to_coordinator(Result, State);
loop(running, #state{mode = Mode} = State) when
    Mode =:= 'typesig'; Mode =:= 'dataflow' ->
  ?debug("Run: ~p\n",[State#state.job]),
  ok = ask_coordinator_for_callers(State),
  NotFixpoint = do_work(State),
  ok = broadcast_done(State),
  report_to_coordinator(NotFixpoint, State).

waits_more_success_typings(#state{depends_on = Depends}) ->
  case Depends of
    [] -> false;
    [_] -> imminent;
    _ -> true
  end.

has_data(#state{scc_data = Data}) ->
  case Data of
    undefined -> false;
    _ -> true
  end.

get_data(#state{mode = Mode, job = SCC, servers = Servers} = State) ->
  Data =
    case Mode of
      typesig -> dialyzer_succ_typings:collect_scc_data(SCC, Servers);
      dataflow -> dialyzer_succ_typings:collect_refine_scc_data(SCC, Servers)
    end,
  State#state{scc_data = Data}.

ask_coordinator_for_callers(#state{job = SCC,
				   servers = Servers,
				   coordinator = Coordinator}) ->
  RequiredBy = dialyzer_succ_typings:find_required_by(SCC, Servers),
  ?debug("Waiting for me~p: ~p\n",[SCC, RequiredBy]),
  dialyzer_coordinator:sccs_to_pids_request(RequiredBy, Coordinator).

broadcast_done(#state{job = SCC, coordinator = Coordinator}) ->
  {Callers, Unknown} = dialyzer_coordinator:sccs_to_pids_reply(),
  send_done(Callers, SCC),
  continue_broadcast_done(Unknown, SCC, Coordinator).

send_done(Callers, SCC) ->
  ?debug("Sending ~p: ~p\n",[SCC, Callers]),
  SendSTFun = fun(PID) -> PID ! {done, SCC} end,
  lists:foreach(SendSTFun, Callers).

continue_broadcast_done([], _SCC, _Coordinator) -> ok;
continue_broadcast_done(Rest, SCC, Coordinator) ->
  dialyzer_coordinator:sccs_to_pids_request(Rest, Coordinator),
  {Callers, Unknown} = dialyzer_coordinator:sccs_to_pids_reply(),
  send_done(Callers, SCC),
  continue_broadcast_done(Unknown, SCC, Coordinator).

wait_for_success_typings(#state{depends_on = DependsOn} = State) ->
  receive
    {done, SCC} ->
      ?debug("GOT ~p: ~p\n",[State#state.job, SCC]),
      State#state{depends_on = DependsOn -- [SCC]}
  after
    5000 ->
      ?debug("Still Waiting ~p: ~p\n",[State#state.job, DependsOn]),
      State
  end.

do_work(#state{mode = Mode, scc_data = SCCData}) ->
  case Mode of
    typesig -> dialyzer_succ_typings:find_succ_types_for_scc(SCCData);
    dataflow -> dialyzer_succ_typings:refine_one_module(SCCData)
  end.

report_to_coordinator(Result, #state{job = Job, coordinator = Coordinator}) ->
  ?debug("Done: ~p\n",[Job]),
  dialyzer_coordinator:job_done(Job, Result, Coordinator).

start_compilation(#state{job = Job, servers = Servers}) ->
  dialyzer_analysis_callgraph:start_compilation(Job, Servers).

ask_coordinator_for_label(EstimatedSize, #state{coordinator = Coordinator}) ->
  dialyzer_coordinator:get_next_label(EstimatedSize, Coordinator).

continue_compilation(Label, Data) ->
  dialyzer_analysis_callgraph:continue_compilation(Label, Data).

collect_warnings(#state{job = Job, servers = Servers}) ->
  dialyzer_succ_typings:collect_warnings(Job, Servers).