Add debug loggin

This commit is contained in:
Kresten Krab Thorup 2012-04-26 00:58:16 +02:00
parent 6c8492c7d0
commit 5d95070669

View file

@ -55,6 +55,21 @@
opts = [], owner, work_done=0 opts = [], owner, work_done=0
}). }).
-ifdef(DEBUG).
-define(log(Fmt,Args), debug_log(State,Fmt,Args)).
debug_log(State,Fmt,Args) ->
Files = [if State#state.c == undefined -> $ ; true -> $C end,
if State#state.b == undefined -> $ ; true -> $B end,
if State#state.a == undefined -> $ ; true -> $A end ],
io:format("~s~p[~s]: " ++ Fmt, [if State#state.level < 10 -> "0"; true -> "" end,
State#state.level,
Files] ++ Args),
ok.
-else.
-define(log(Fmt,Args),ok).
-endif.
%%%%% PUBLIC OPERATIONS %%%%% PUBLIC OPERATIONS
open(Dir,Level,Next,Opts,Owner) when Level>0 -> open(Dir,Level,Next,Opts,Owner) when Level>0 ->
@ -218,11 +233,27 @@ main_loop(State = #state{ next=Next }) ->
ToFileName = filename("C",State), ToFileName = filename("C",State),
SetPos = #state.c SetPos = #state.c
end, end,
ok = file:rename(FileName, ToFileName),
{ok, BT} = hanoi_reader:open(ToFileName, random),
reply(From, ok), ?log("inject ~s~n", [ToFileName]),
case file:rename(FileName, ToFileName) of
ok -> ok;
E -> ?log("rename failed ~p -> ~p :: ~p~n", [FileName, ToFileName, E]),
error(E)
end,
plain_rpc:send_reply(From, ok),
case hanoi_reader:open(ToFileName, random) of
{ok, BT} ->
if SetPos == #state.b ->
check_begin_merge_then_loop(setelement(SetPos, State, BT)); check_begin_merge_then_loop(setelement(SetPos, State, BT));
true ->
main_loop(setelement(SetPos, State, BT))
end;
E2 -> ?log("open failed ~p :: ~p~n", [ToFileName, E2]),
error(E2)
end;
?CALL(From, unmerged_count) -> ?CALL(From, unmerged_count) ->
@ -252,6 +283,8 @@ main_loop(State = #state{ next=Next }) ->
WorkToDoHere = min(WorkLeftHere, HowMuch), WorkToDoHere = min(WorkLeftHere, HowMuch),
DelegateWork = max(0,HowMuch - WorkToDoHere), DelegateWork = max(0,HowMuch - WorkToDoHere),
?log("step:~p, do:~p, left:~p ~n", [HowMuch, WorkToDoHere, WorkLeftHere]),
%% delegate the step request to the next level %% delegate the step request to the next level
if Next =:= undefined; DelegateWork == 0 -> if Next =:= undefined; DelegateWork == 0 ->
DelegateRef = undefined; DelegateRef = undefined;
@ -464,6 +497,9 @@ main_loop(State = #state{ next=Next }) ->
%% We need to push the output of merging to the next level %% We need to push the output of merging to the next level
%% %%
?CAST(_,{merge_done, _Count, OutFileName}) -> ?CAST(_,{merge_done, _Count, OutFileName}) ->
?log("merge_done, out:~w, next:~p~n", [_Count,Next]),
State1 = State1 =
if Next =:= undefined -> if Next =:= undefined ->
{ok, PID} = ?MODULE:open(State#state.dir, State#state.level + 1, undefined, {ok, PID} = ?MODULE:open(State#state.dir, State#state.level + 1, undefined,
@ -516,6 +552,8 @@ main_loop(State = #state{ next=Next }) ->
%% Probably from a merger_pid - which we may have forgotten in the meantime. %% Probably from a merger_pid - which we may have forgotten in the meantime.
main_loop(State); main_loop(State);
{'EXIT', Pid, Reason} when Pid == State#state.merge_pid -> {'EXIT', Pid, Reason} when Pid == State#state.merge_pid ->
?log("*** merge_died: ~p~n", [Reason]),
restart_merge_then_loop(State#state{merge_pid=undefined}, Reason); restart_merge_then_loop(State#state{merge_pid=undefined}, Reason);
{'EXIT', PID, _} when [PID] == tl(State#state.folding); {'EXIT', PID, _} when [PID] == tl(State#state.folding);
hd(State#state.folding) == PID -> hd(State#state.folding) == PID ->
@ -523,6 +561,7 @@ main_loop(State = #state{ next=Next }) ->
{'EXIT', PID, Reason} -> {'EXIT', PID, Reason} ->
error_logger:info_msg("got unexpected exit ~p from ~p~n", [Reason, PID]) error_logger:info_msg("got unexpected exit ~p from ~p~n", [Reason, PID])
end. end.
reply_step_ok(State) -> reply_step_ok(State) ->
@ -577,15 +616,18 @@ begin_merge(State) ->
XFileName = filename("X",State), XFileName = filename("X",State),
Owner = self(), Owner = self(),
?log("starting merge~n", []),
file:delete(XFileName), file:delete(XFileName),
MergePID = proc_lib:spawn_link(fun() -> MergePID = proc_lib:spawn_link(fun() ->
try try
?log("merge begun", []),
{ok, OutCount} = hanoi_merger:merge(AFileName, BFileName, XFileName, {ok, OutCount} = hanoi_merger:merge(AFileName, BFileName, XFileName,
?BTREE_SIZE(State#state.level + 1), ?BTREE_SIZE(State#state.level + 1),
State#state.next =:= undefined, State#state.next =:= undefined,
State#state.opts ), State#state.opts ),
% error_logger:info_msg("merge done ~p,~p -> ~p~n", [AFileName, BFileName, XFileName]),
Owner ! ?CAST(self(),{merge_done, OutCount, XFileName}) Owner ! ?CAST(self(),{merge_done, OutCount, XFileName})
catch catch