aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/test/global_SUITE_data/global_trace.erl
blob: 00bacf8f549d335fcd15fcf6a3514cc93a7f68c6 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
%% -*- coding: utf-8 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2007-2013. 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(global_trace).

%%%
%%% Inspection of High Level Trace created by global.erl.
%%%

%%% A few handy functions when running the test_server
%%% 

d() ->
    lists:foreach(fun(F) -> dd(F, []) end, last()).

d(Testcase) ->
    d(Testcase, []).

%% Skip "global_" from T.
d(Testcase, Options) ->
    [Filename] = tmp_files(Testcase),
    dd(Filename, Options).

dd(Filename, Options) ->
    io:format("\n======= ~s \n", [Filename]), 
    t(Filename, Options).

last() ->
    tmp_files("*").

%% global_groups_change: one node is restarted
%% global_mass_death: nodes are stopped
%% global_lock_die: two spurious (trying to remove locks taken by other pid)
%% global_otp_5640: 4 spurious (names registered again &c)
tmp_files(A) when is_atom(A) ->
    tmp_files(atom_to_list(A));
tmp_files(T) when is_list(T) ->
    Logs = logdir(),
    Dir = lists:last(filelib:wildcard(filename:join(Logs, "*"))),
    filelib:wildcard(filename:join([Dir, log_private, "global_" ++ T])).

%logdir() ->
%    "/net/yoshi/ldisk/daily_build/otp_norel_linux_r11b.2007-02-18_19/"
%        "test/test_server/global_SUITE.logs";
%logdir() ->
%    "/ldisk/daily_build/otp_norel_linux_suse_r11b.2007-02-07_19/test/"
%        "test_server/global_SUITE.logs";
logdir() ->
    "/tmp/tests/test_server/global_SUITE.logs".



%%% The contents of this file is by no means fixed; the printouts are
%%% updated depending on the problems at hand. Not everything has been
%%% designed very carefully :)
%%% 
%%% For one thing, the trace from all nodes are written onto the file
%%% as one single term. One term per node would be better. &c.

-compile(export_all).

-record(state, {connect_all, known = [], synced = [],
		resolvers = [], syncers = [], node_name = node(),
		the_locker, the_deleter, the_registrar, trace = [],
                global_lock_down
               }).

%% Compatible with different versions.
state(#state{}=S) ->
    S;
state({state, ConnectAll, Known, Synced, LockersResolvers, Syncers,
       NodeName, TheLocker, TheDeleter}) ->
    %% r10b: Lockers, r10b_patched, r11b: Resolvers
    #state{connect_all = ConnectAll, known = Known, synced = Synced,
           resolvers = LockersResolvers, syncers = Syncers, 
           node_name = NodeName, the_locker = TheLocker, 
           the_deleter = TheDeleter, the_registrar = undefined, trace = []};
state({state, ConnectAll, Known, Synced, Resolvers, Syncers,
       NodeName, TheLocker, TheDeleter, Trace}) ->
    %% r11b, some time before r11b-3
    #state{connect_all = ConnectAll, known = Known, synced = Synced,
           resolvers = Resolvers, syncers = Syncers, 
           node_name = NodeName, the_locker = TheLocker, 
           the_deleter = TheDeleter, the_registrar = undefined, 
           trace = Trace};
state({state, ConnectAll, Known, Synced, Resolvers, Syncers,
       NodeName, TheLocker, TheDeleter, TheRegistrar, Trace}) ->
    %% r11b, some time after r11b-3
    #state{connect_all = ConnectAll, known = Known, synced = Synced,
           resolvers = Resolvers, syncers = Syncers, 
           node_name = NodeName, the_locker = TheLocker, 
           the_deleter = TheDeleter, the_registrar = TheRegistrar, 
           trace = Trace, global_lock_down = false};
state(Else) ->
    Else.

%%% Trace tuples look like {Node, Now, Message, Nodes, Extra}.
%%% Nodes is the list as returned by nodes().
%%% Extra is [] most of the time.
%%%
%%% init
%%% {nodedown,DownNode}
%%% {extra_nodedown,DownNode}
%%% {nodeup, UpNode}
%%% {added,AddedNodes}, Extra = [{new_nodes, NewNodes}, 
%%%                              {abcast, Known},
%%%                              {ops,Ops}]
%%%    NewKnown = Known ++ AddedNodes
%%%    AddedNodes = NewNodes -- Known
%%%    NewNodes är här den man förhandlat med plus de noder den känner till.
%%% {added, AddedNodes}, Extra = [{ops,Ops}]
%%%    NewKnown = Known ++ AddedNodes
%%%    Den (passiva) noden får Nodes som är NewNodes
%%%    hos den förhandlande. Sedan: AddedNodes = (Nodes -- Known) -- [node()].
%%%    Det är som hos förhandlaren.
%%% {nodes_changed, {New,Old}}
%%%    Every now and then the list [node() | nodes()] is checked for updates.
%%%    New are the nodes that global does not know of (yet).
%%% {new_node_name, NewNode}
%%%    Ignored. Someone changes the nodename dynamically.
%%% {ins_name, Node}, Extra = [Name, Pid]
%%%    Node = node(Pid)
%%% {ins_name_ext, Node}, Extra = [Name, Pid]
%%%    Node = node(Pid)
%%% {del_name, Node}, Extra = [Name, Pid]
%%%    Node = node(Pid)
%%% {ins_lock, Node}, Extra = [Id, Pid]
%%%    Node = node(Pid)
%%% {rem_lock, Node}, Extra = [Id, Pid]
%%%    Node = node(Pid)
%%% {locker_succeeded, node()}, Extra = {First, Known}
%%% {locker_failed, node()}, Extra = {Tried, SoFar}
%%%    The nodes in SoFar have been locked, could not lock Tried.
%%%
%%% Also trace of the creation and deletion of resolvers
%%% (this kind of resolvers are created when nodeup arrives from
%%%  unknown nodes (there are also name resolvers...)).
%%% {new_resolver, Node}, Extra = [Tag, ResolverPid]
%%% {kill_resolver, Node}, Extra = [Tag, ResolverPid]
%%% {exit_resolver, Node}, Extra = [Tag]

-record(node, {
          node, 
          known = [],         % #state.known (derived)
          nodes = [],         % nodes()
          locks = [],         % [{Id, [Pid, node(Pid)]}] (derived)
          names = [],         % [{Name, Pid, node(Pid)}] (derived)
          resolvers = [],     % [{Node, Tag, ResolverPid}]
          n_locks = {0,       % OK
                     0,       % Tried to lock the boss
                     0,       % Tried to lock other boss
                     0},      % Tried to lock known
          rejected = 0        % Lock OK, but later rejected
         }).

-record(w, {nodes = [], % [#node{}]
            n = []}).

t(File) ->
    t(File, []).

%%% What to search for in the output of t/2?
%%% - 'NEGOTIATIONS': this is a list of the name negotiations 
%%%   (the big picture);
%%% - '###' signals a possibly strange event;
%%% - 'spurious' is used for "tagging" such events;
%%% - 'resol ' could mean that some resolver process has not been removed;
%%% ...

%% Options:
%% {show_state, From, To} 
%%    From = To = integer() | {integer(), integer()}
%%      Examples: {7, 8} (show states between seconds 7.0 and 8.0);
%%                {{1,431234},{2,432}} (between 1.431234 and 2.000432)
%%      The state of a node includes locks, names, nodes, known, ...
%%      Default is {{0,0}, {0,0}}, that is, do not show state.
%% show_state
%%      same as {show_state, 0, 1 bsl 28}, that is, show every state
%% {show_trace, bool()
%%      Show the complete trace as one list and per node pair.
%%      Default is true.
t(File, Options) ->
    {StateFun, ShowTrace} = 
        case options(Options, [show_state, show_trace]) of
            [{From,To}, ST] ->
                {fun(T, S) ->
                         Time =  element(2, T),
                         if 
                             Time >= From, Time =< To ->
                                 io:format("===> ~p~n", [T]),
                                 display_nodes("After", Time, S#w.nodes, T);
                             true ->
                                 ok
                         end
                 end, ST};
            _ ->
                erlang:error(badarg, [File, Options])
        end,
    D1 = try
             %% All nodes' trace is put on the file as one binary.
             %% This could (and should?) be improved upon.
             {ok, Bin} = file:read_file(File),
             io:format("Size of trace file is ~p bytes~n", [size(Bin)]),
             binary_to_term(Bin)
         catch _:_ ->
             {ok, [D0]} = file:consult(File),
             D0
         end,
    {D2, End} = case D1 of
                    {high_level_trace, ET, D3} ->
                        {D3, ET};
                    _ ->
                        {D1, now()}
                end,
    D = adjust_nodes(D2),
    {NodeNodeTrace, _NodeTrace, Trace, Base} = get_trace(D, End),
    messages(D, Base, End),

    %io:format("NET~n ~p~n", [net_kernel_nodes(NodeTrace)]),

    io:format("NEGOTIATIONS:~n ~p~n", [negotiations(Trace)]),

    io:format("*** Complete trace ***~n"),
    if 
        ShowTrace ->
            show_trace(Trace),
            io:format("--- End of complete trace ---~n"),
            lists:map(fun({{Node,ActionNode},Ts}) ->
                              io:format("*** Trace for ~p on node ~p ***~n", 
                                        [ActionNode, Node]),
                              show_trace(lists:keysort(2, Ts)),
                              io:format("--- End of trace for ~p on node ~p ---~n", 
                                        [ActionNode, Node])
                      end, NodeNodeTrace);
        true -> ok
    end,
    io:format("*** Evaluation ***~n"),
    {Fini, Spurious} = eval(Trace, StateFun),
    io:format("*** End of evaluation ***~n"),
    show_spurious(NodeNodeTrace, Spurious),
    display_nodes("FINI", '', Fini),
    ok.

% show_trace(Trace) ->
%     lists:foreach(fun({Node, {S,Mu}, Message, Nodes, Extra}) ->
%                           io:format("~2w.~6..0w ~w~n", [S, Mu, Node]),
%                           io:format("     ~p~n", [Message]),
%                           io:format("          Nodes: ~p~n", [Nodes]),
%                           case Extra of 
%                               [] -> ok;
%                               _ -> io:format("          Extra: ~p~n", [Extra])
%                           end
%                   end, Trace);
show_trace(Trace) ->
    lists:map(fun(T) -> io:format("~p~n", [T]) end, Trace).

get_trace(D, EndTime0) ->
    NodeTrace0 = [{Node,lists:keysort(2, (state(State))#state.trace)} || 
                     {Node,{info,State}} <- D,
                     case state(State) of
                         #state{trace = no_trace} ->
                             io:format("No trace for ~p~n", [Node]),
                             false;
                         #state{} ->
                             true;
                         Else ->
                             io:format("Bad state for ~p: ~p~n", 
                                       [Node, Else]),
                             false
                     end],
    Trace0 = lists:keysort(2, lists:append([T || {_Node, T} <- NodeTrace0])),
    Trace1 = sort_nodes(Trace0),
    {Base, Trace2} = adjust_times(Trace1),
    EndTime = adjust_time(EndTime0, Base),
    io:format("The trace was generated at ~p~n", [EndTime]),
    Trace = [T || T <- Trace2, element(2, T) < EndTime],
    NodeTrace = [{Node, adjust_times(Ts, Base)} ||
                    {Node, Ts} <- NodeTrace0],
    NodeNodeTrace = 
        [{{Node,ActionNode}, T} || {Node, Ts} <- NodeTrace,
                                   T <- Ts,
                                   ActionNode <- action_nodes(T)],
    {family(NodeNodeTrace), NodeTrace, Trace, Base}.

adjust_nodes([E | Es]) ->
    [adjust_nodes(E) | adjust_nodes(Es)];
adjust_nodes(T) when is_tuple(T) ->
    list_to_tuple(adjust_nodes(tuple_to_list(T)));
adjust_nodes(A) when is_atom(A) ->
    adjust_node(A);
adjust_nodes(E) ->
    E.

sort_nodes(Ts) ->
    [setelement(4, T, lists:sort(element(4, T))) || T <- Ts].

adjust_times([]) ->
    {0, []};
adjust_times([T1 | _]=Ts) ->
    Base = element(2, T1),
    {Base, adjust_times(Ts, Base)}.

adjust_times(Ts, Base) ->
    [setelement(2, adj_tag(T, Base), adjust_time(element(2, T), Base)) || 
        T <- Ts].

adj_tag({Node, Time, {M, Node2}, Nodes, Extra}=T, Base) ->
    if
        M =:= new_resolver;
        M =:= kill_resolver;
        M =:= exit_resolver ->
            {Node, Time, {M, Node2}, Nodes, 
             [adjust_time(hd(Extra), Base) | tl(Extra)]};
        true ->
            T
    end.

adjust_time(Time, Base) ->
    musec2sec(timer:now_diff(Time, Base)).

action_nodes({_Node, _Time, {_, Nodes}, _, _}) when is_list(Nodes) ->
    Nodes;
action_nodes({_Node, _Time, {_, Node}, _, _}) ->
    [Node].

%% Some of the names in global_SUITE.erl are recognized.
adjust_node(Node) ->
    case atom_to_list(Node) of
        "cp" ++ L ->
            list_to_atom([$c, $p | lists:takewhile(fun is_digit/1, L)]);
        "test_server" ++ _ ->
            test_server;
        "a_2" ++ _ ->
            a_2;
        "n_1" ++ _ ->
            n_1;
        "n_2" ++ _ ->
            n_2;
        "z_2" ++ _ ->
            z_2;
        "z_" ++ _ ->
            z;
        "b_" ++ _ ->
            b;
        "c_external_nodes" ++ _ ->
            c_external_nodes;
        _ ->
            Node
    end.

is_digit(C) ->
    (C >= $0) and (C =< $9).

eval(Trace, Fun) ->
    eval(Trace, {0, 0}, #w{}, Fun).

eval([T | Ts], Time0, S0, Fun) ->
    Time1 = element(2, T),
    case is_fresh(S0#w.nodes) of
        true ->
            io:format("~p ***************** FRESH *****************~n",
                      [Time1]);
        false -> 
            ok
    end,
    case time_diff(Time1, Time0) > 0 of 
        true ->
            display_nodes("PAUS", Time1, S0#w.nodes, T);
        false ->
            ok
    end,
    S = eval_trace(T, S0),
    Fun(T, S),
    eval(Ts, Time1, S, Fun);
eval([], _, S, _Fun) ->
    {S#w.nodes, lists:usort(S#w.n)}.

%% Old.
eval_trace({Node, Time, {added,Added}, _Nodes, [_NewNodes,_Abc]}, S0) ->
    added(Node, Added, Time, S0);
eval_trace({Node, Time, {added,Added}, _Nodes, []}, S0) ->
    added(Node, Added, Time, S0);


eval_trace({Node, Time, {init, Node}, Nodes, []}, S0) ->
    init(Node, Nodes, Time, S0);
eval_trace({Node, Time, {nodedown, DownNode}, Nodes, []}, S0) ->
    node_down(Node, DownNode, Nodes, Time, S0);
eval_trace({Node, Time, {extra_nodedown, DownNode}, Nodes, []}, S0) ->
    node_down(Node, DownNode, Nodes, Time, S0);
eval_trace({Node, Time, {nodeup, UpNode}, Nodes, []}, S0) ->
    node_up(Node, UpNode, Nodes, Time, S0);
eval_trace({Node, Time, {added,Added}, _Nodes, [_NewNodes,_Abc,_Ops]}, S0) ->
    added(Node, Added, Time, S0);
eval_trace({Node, Time, {added,Added}, _Nodes, [_Ops]}, S0) ->
    added(Node, Added, Time, S0);
eval_trace({Node, Time, {nodes_changed, {New,Old}}, _Nodes, []}, S0) ->
    nodes_changed(Node, New, Old, Time, S0);
eval_trace({Node, Time, {ins_name, PNode}, _Nodes, [Name, Pid]}, S0) ->
    insert_name(Node, PNode, Time, Name, Pid, S0);
eval_trace({Node, Time, {del_name, PNode}, _Nodes, [Name, Pid]}, S0) ->
    delete_name(Node, PNode, Time, Name, Pid, S0);
eval_trace({Node, Time, {ins_name_ext, PNode}, _Nodes, [Name, Pid]}, S0) ->
    insert_external_name(Node, PNode, Time, Name, Pid, S0);
eval_trace({Node, Time, {ins_lock, PNode}, _Nodes, [Id, Pid]}, S0) ->
    insert_lock(Node, PNode, Time, Id, Pid, S0);
eval_trace({Node, Time, {rem_lock, PNode}, _Nodes, [Id, Pid]}, S0) ->
    remove_lock(Node, PNode, Time, Id, Pid, S0);
eval_trace({Node, Time, {locker_succeeded, _}, _Nodes,{_First,_Known}}, S0) ->
    locker_succeeded(Node, Time, S0);
eval_trace({Node, Time, {lock_rejected, _}, _Nodes, Known}, S0) ->
    lock_rejected(Node, Time, Known, S0);
eval_trace({Node, Time, {locker_failed, _}, _Nodes, {Tried,SoFar}}, S0) ->
    locker_failed(Node, Time, Tried, SoFar, S0);
eval_trace({Node, Time, {new_resolver, RNode}, _Nodes, [Tag, ResPid]}, S0) ->
    new_resolver(Node, Time, RNode, Tag, ResPid, S0);
eval_trace({Node, Time, {kill_resolver, RNode}, _Nodes, [Tag,_ResPid]}, S0) ->
    stop_resolver(Node, Time, RNode, Tag, kill, S0);
eval_trace({Node, Time, {exit_resolver, RNode}, _Nodes, [Tag]}, S0) ->
    stop_resolver(Node, Time, RNode, Tag, exit, S0);
eval_trace(_Ignored, S) ->
io:format("ignored ~p~n", [_Ignored]),
    S.

init(_Node, [], _Time, S) ->
    S;
init(Node, NodesList, Time, S) ->
    io:format("### ~p ~p: already in nodes(): ~p~n", [Node, Time, NodesList]),
    S.

node_down(Node, DownNode, NodesList, Time, S0) ->
    case get_node(Node, S0) of
        {ok, #node{known = Known, nodes = Nodes}=N} ->
            case lists:member(DownNode, Nodes) of
                true ->
                    S1 = case lists:member(DownNode, Known) of
                             true -> 
                                 S0;
                             false ->
                                 io:format("### ~p ~p:~n   "
                                           "nodedown but unknown ~p~n",
                                           [Node, Time, DownNode]),
                                 case lists:member(DownNode, Nodes) of
                                     true ->
                                         io:format("(but note that ~p"
                                                   " is member of nodes())~n",
                                                   [DownNode]);
                                     false ->
                                         ok
                                 end,
                                 add_spurious(Node, DownNode, S0, Time)
                         end,
                    NewKnown = lists:delete(DownNode, Known),
                    NewNodes = lists:delete(DownNode, Nodes),
                    put_node(N#node{known = NewKnown, nodes = NewNodes}, S1);
                false ->
                    io:format("### ~p ~p:~n   spurious nodedown from ~p~n  "
                              "~p~n", [Node, Time, DownNode, NodesList]),
                    NewKnown = lists:delete(DownNode, Known),
                    S1 = put_node(N#node{known = NewKnown,nodes = Nodes}, S0),
                    add_spurious(Node, DownNode, S1, Time)
            end;
        not_ok ->
            io:format("### ~p ~p:~n   unknown node got nodedown from ~p~n", 
                      [Node, Time, DownNode]),
            add_spurious(Node, DownNode, S0, Time)
    end.

node_up(Node, UpNode, NodesList, Time, S) ->
    case get_node(Node, S) of
        {ok, #node{nodes = Nodes}=N} ->
            case lists:member(UpNode, Nodes) of
                true ->
                    io:format("### ~p ~p:~n   spurious nodeup from ~p~n  "
                              "~p~n", [Node, Time, UpNode, NodesList]),
                    add_spurious(Node, UpNode, S, Time);
                false ->
                    put_node(N#node{nodes = lists:sort([UpNode | Nodes])}, S)
            end;
        not_ok ->
            S#w{nodes = [#node{node = Node, nodes = [UpNode]} | S#w.nodes]}
    end.

added(Node, Added, Time,  S0) ->
    case get_node(Node, S0) of
        {ok, #node{known = Known, nodes = Nodes}=N} ->
            case Known -- (Known -- Added) of
                [] ->
                    S1 = put_node(N#node{known = lists:sort(Added ++ Known), 
                                         nodes = Nodes}, S0),
                    case lists:member(Node, Added) of
                        true ->
                            io:format("### ~p ~p:~n   adding node()"
                                      " to known (~p)~n", [Node, Time,Added]),
                            add_spurious(Node, Added, S1, Time);
                        false ->
                            S1
                    end;
                AK ->
                    io:format("### ~p ~p:~n   added already known ~p~n", 
                              [Node, Time, AK]),
                    S1 = put_node(N#node{known = lists:usort(Added ++ Known), 
                                         nodes = Nodes}, S0),
                    add_spurious(Node, AK, S1, Time)
            end;
        not_ok ->
            io:format("### ~p ~p:~n   unknown node got added ~p~n", 
                      [Node, Time, Added]),
            S1 = S0#w{nodes = [#node{node = Node, known = Added} | 
                               S0#w.nodes]},
            add_spurious(Node, Added, S1, Time)
    end.

nodes_changed(Node, New, Old, Time, S) ->
    io:format("### ~p ~p:~n   nodes changed, new are ~p, old are ~p~n",
              [Node, Time, New, Old]),
    S.

insert_external_name(Node, PNode, Time, Name, Pid, S) ->
    insert_name(Node, PNode, Time, Name, Pid, S).

insert_name(Node, PNode, Time, Name, Pid, S0) ->
    RegName = {Name, Pid, PNode},
    case get_node(Node, S0) of
        {ok, #node{names = Names}=N} ->
            case lists:keysearch(Name, 1, Names) of
                {value, {Name, OldPid, OldPNode}} ->
                    io:format("### ~p ~p:~n   name ~p already registered "
                              "for ~p on ~p~n", 
                              [Node, Time, Name, OldPid, OldPNode]),
                    add_spurious(Node, [PNode], S0, Time);
                false ->
                    case lists:keysearch(Pid, 2, Names) of
                        {value, {OldName, Pid, OldPNode}} ->
                            io:format("### ~p ~p:~n   pid ~p already "
                                      "registered as ~p on ~p~n", 
                                      [Node, Time, Pid, OldName, OldPNode]),
                            add_spurious(Node, [PNode], S0, Time);
                        false -> 
                            put_node(N#node{names = [RegName | Names]}, S0)
                    end
            end;
        not_ok ->
            io:format("### ~p ~p:~n   unknown node registered ~p for ~p "
                      "on ~p~n", [Node, Time, Name, Pid, PNode]),
            Known = add_to_known(Node, PNode, []),
            N = #node{node = Node, known = Known, names = [RegName]},
            S1 = S0#w{nodes = [N | S0#w.nodes]},
            add_spurious(Node, [PNode], S1, Time)
    end.

delete_name(Node, PNode, Time, Name, Pid, S0) ->
    case get_node(Node, S0) of
        {ok, #node{names = Names}=N} ->
            case lists:keysearch(Name, 1, Names) of
                {value, {Name, Pid, PNode}} ->
                    NewNames = lists:keydelete(Name, 1, Names),
                    put_node(N#node{names = NewNames}, S0);
                {value, {Name, Pid2, PNode2}} -> % bad log
                    io:format("### ~p ~p:~n   name ~p not registered "
                              "for ~p on ~p but for ~p on ~p~n", 
                              [Node, Time, Name, Pid, PNode, Pid2, PNode2]),
                    add_spurious(Node, [PNode], S0, Time);
                false -> 
                    io:format("### ~p ~p:~n   name ~p not registered "
                              "for ~p on ~p~n", 
                              [Node, Time, Name, Pid, PNode]),
                    add_spurious(Node, [PNode], S0, Time)
            end;
        not_ok ->
            io:format("### ~p ~p:~n   unknown node deleted ~p for ~p on ~p~n",
                      [Node, Time, Name, Pid, PNode]),
            Known = add_to_known(Node, PNode, []),
            N = #node{node = Node, known = Known},
            S1 = S0#w{nodes = [N | S0#w.nodes]},
            add_spurious(Node, [PNode], S1, Time)
    end.

insert_lock(Node, PNode, Time, Id, Pid, S0) ->
    Lock = {Pid, PNode},
    case get_node(Node, S0) of
        {ok, #node{locks = NLocks}=N} ->
            case lists:keysearch(Id, 1, NLocks) of
                {value, {Id, OldLocks}} ->
                    case lists:member(Lock, OldLocks) of
                        true ->
                            io:format("### ~p ~p:~n   lock ~p already set "
                                      "for ~p on ~p~n", 
                                      [Node, Time, Id, Pid, PNode]),
                            %% This is not so strange, actually. 
                            add_spurious(Node, [PNode], S0, Time);
                        false ->
                            NewLocks = {Id, [Lock | OldLocks]},
                            Ls = lists:keyreplace(Id, 1, NLocks, NewLocks),
                            put_node(N#node{locks = Ls}, S0)
                    end;
                false ->
                    put_node(N#node{locks = [{Id,[Lock]}|N#node.locks]}, S0)
            end;
        not_ok ->
            Known = add_to_known(Node, PNode, []),
            N = #node{node = Node, known = Known, locks = [{Id, [Lock]}]},
            S1 = S0#w{nodes = [N | S0#w.nodes]},
            if 
                Node =/= PNode ->
                    io:format("### ~p ~p:~n   unknown pid ~p locked ~p on "
                              "~p~n", [Node, Time, Pid, Id, PNode]),
                    add_spurious(Node, [PNode], S1, Time);
                true ->
                    S1
            end
    end.

remove_lock(Node, PNode, Time, Id, Pid, S0) ->
    Lock = {Pid, PNode},
    case get_node(Node, S0) of
        {ok, #node{locks = NLocks}=N} ->
            case lists:keysearch(Id, 1, NLocks) of
                {value, {Id, OldLocks}} ->
                    case lists:member(Lock, OldLocks) of
                        true ->
                            NewLocks = lists:delete(Lock, OldLocks),
                            Ls = case NewLocks of
                                     [] ->
                                         lists:keydelete(Id, 1, NLocks);
                                     _ ->
                                         lists:keyreplace(Id, 1, NLocks, 
                                                          {Id, NewLocks})
                                 end,
                            put_node(N#node{locks = Ls}, S0);
                        false ->
                            io:format("### ~p ~p:~n   lock ~p not set "
                                      "by ~p on ~p~n", 
                                      [Node, Time, Id, Pid, PNode]),
                            add_spurious(Node, [PNode], S0, Time)
                    end;
                false ->
                    io:format("### ~p ~p:~n   lock ~p not set "
                              "by ~p on ~p~n", 
                              [Node, Time, Id, Pid, PNode]),
                    add_spurious(Node, [PNode], S0, Time)
            end;
        not_ok ->
            io:format("### ~p ~p:~n   ~p unlocked ~p on unknown node ~p~n",
                      [Node, Time, Pid, Id, PNode]),
            Known = add_to_known(Node, PNode, []),
            N = #node{node = Node, known = Known},
            S1 = S0#w{nodes = [N | S0#w.nodes]},
            add_spurious(Node, [PNode], S1, Time)
    end.
    
%% This is just statistics...
locker_succeeded(Node, Time, S0) ->
    case get_node(Node, S0) of
        {ok, #node{n_locks = {Ok,Boss,NodeX,Bad}}=N} ->
            put_node(N#node{n_locks = {Ok+1,Boss,NodeX,Bad}}, S0);
        not_ok ->
            io:format("### ~p ~p:~n   unknown node's locker succeeded~n", 
                      [Node, Time]),
            add_spurious(Node, [Node], S0, Time)
    end.

lock_rejected(Node, Time, _Known, S0) ->
    case get_node(Node, S0) of
        {ok, #node{rejected = Rej}=N} ->
            put_node(N#node{rejected = Rej+1}, S0);
        not_ok ->
            io:format("### ~p ~p:~n   unknown node's lock rejected~n", 
                      [Node, Time]),
            add_spurious(Node, [Node], S0, Time)
    end.

locker_failed(Node, Time, Tried, SoFar, S0) ->
    case get_node(Node, S0) of
        {ok, #node{known = Known, n_locks = {Ok,Boss,NodeX,Bad}}=N} ->
            TheBoss = lists:max([Node | Known]),
            Cheap = (Tried =:= [TheBoss]),
            RatherCheap = ((SoFar -- [Node, TheBoss]) =:= []) and
                          ((Tried -- [Node, TheBoss]) =/= []),
            if
                Cheap ->
                    put_node(N#node{n_locks = {Ok,Boss+1,NodeX,Bad}}, S0);
                RatherCheap ->
                    put_node(N#node{n_locks = {Ok,Boss,NodeX+1,Bad}}, S0);
                true ->
                    put_node(N#node{n_locks = {Ok,Boss,NodeX,Bad+1}}, S0)
            end;
        not_ok ->
            io:format("### ~p ~p:~n   unknown node's locker failed~n", 
                      [Node, Time]),
            add_spurious(Node, [Node], S0, Time)
    end.

new_resolver(Node, Time, ResNode, Tag, ResPid, S0) ->
    case get_node(Node, S0) of
        {ok, #node{resolvers = Rs}=N} ->
            put_node(N#node{resolvers = [{ResNode, Tag, ResPid} | Rs]}, S0);
        not_ok ->
            io:format("### ~p ~p:~n   resolver created for unknown node~n", 
                      [Node, Time]),
            add_spurious(Node, [Node], S0, Time)
    end.

stop_resolver(Node, Time, ResNode, Tag, How, S0) ->
    case get_node(Node, S0) of
        {ok, #node{resolvers = Rs}=N} ->
            case lists:keysearch(Tag, 2, Rs) of
                {value, {ResNode, Tag, _ResPid}} ->
                    NewRs = lists:keydelete(Tag, 2, Rs),
                    put_node(N#node{resolvers = NewRs}, S0);
                false ->
                    case lists:keysearch(ResNode, 1, Rs) of
                        {value, {ResNode, _Tag2, _ResPid2}} ->
                            NewRs = lists:keydelete(ResNode, 1, Rs),
                            put_node(N#node{resolvers = NewRs}, S0);
                        false when How =:= exit -> 
                            io:format("### ~p ~p:~n   there is no resolver "
                                      "with tag ~p on node ~p~n", 
                                      [Node, Time, Tag, ResNode]),
                            add_spurious(Node, [ResNode], S0, Time);
                        false when How =:= kill ->
                            S0
                    end
            end;
        not_ok ->
            io:format("### ~p ~p:~n   resolver stopped for unknown node~n", 
                      [Node, Time]),
            add_spurious(Node, [Node], S0, Time)
    end.

add_to_known(Node, NodeToAdd, Known) ->
    if
        Node =:= NodeToAdd ->
            Known;
        true ->
            lists:sort([NodeToAdd | Known])
    end.

get_node(Node, S) ->
    case lists:keysearch(Node, #node.node, S#w.nodes) of
        {value, N} ->
            {ok, N};
        false ->
            not_ok
    end.

put_node(#node{node = Node, known = [], nodes = [], locks = [], names = [], 
               n_locks = {0,0,0,0}}, 
         S) ->
    S#w{nodes = lists:keydelete(Node, #node.node, S#w.nodes)};
put_node(N, S) ->
    S#w{nodes = lists:keyreplace(N#node.node, #node.node, S#w.nodes, N)}.

is_fresh(#node{known = [], nodes = [], locks = [], names = []}) ->
    true;
is_fresh(#node{}) ->
    false;
is_fresh([]) ->
    true;
is_fresh([N | Ns]) ->
    is_fresh(N) andalso is_fresh(Ns).

add_spurious(Node, ActionNodes, S, Time) when is_list(ActionNodes) ->
    S#w{n = [{{Node,N},Time}|| N <- ActionNodes] ++ S#w.n};
add_spurious(Node, ActionNode, S, Time) ->
    add_spurious(Node, [ActionNode], S, Time).

messages(D, Base, End) ->
    messages1(no_info(D), no_info), 
    messages1(resolvers(D, Base, End), resolvers),
    messages1(syncers(D), syncers).

messages1(M, ST) ->
    [foo || {Node, T} <- M,
            ok =:= io:format(ms(ST), [Node, T])].

ms(no_info) ->
    "~p: ~p~n";    
ms(resolvers) ->
    "~p: resolvers ~p~n";
ms(syncers) ->
    "~p: syncers ~p~n".

no_info(D) ->
    [{Node,no_info} || {Node, no_info} <- D].

resolvers(D, Base, End) ->
    [{Node,
      [{N,adjust_time(T, Base),P} || {N, T, P} <- Rs, T < End]} || 
        {Node, {info,State}} <- D,
        is_record(State, state),
        [] =/= (Rs = (state(State))#state.resolvers)].

syncers(D) ->
    [{Node,Ss} || {Node, {info,State}} <- D,
                  is_record(State, state),
                  [] =/= (Ss = (state(State))#state.syncers)].

net_kernel_nodes(NodeTrace) ->
    [{Node, nkn(Trace, [])} || {Node, Trace} <- NodeTrace].

nkn([], _Nodes) ->
    [];
nkn([{Node, Time, _Message, Ns, _X} | Ts], Nodes) ->
    {NewS, _, OldS} = sofs:symmetric_partition(sofs:set(Ns), sofs:set(Nodes)),
    New = sofs:to_external(NewS),
    Old = sofs:to_external(OldS),
    [{Node, Time, {newnode, N}, []} || N <- New] ++
    [{Node, Time, {oldnode, N}, []} || N <- Old] ++
    nkn(Ts, (Nodes -- Old) ++ New).

negotiations(Trace) ->
    Ns = [{Node,T,Added,X} || 
             {Node,T,{added,Added},_Nodes,X} <- Trace],
    Pass = [{passive,Node,T,Added} || 
               {Node,T,Added,[_Ops]} <- Ns],
    Act = [{active,Node,T,Other,Added,NewNodes} ||
              {Node,T,Added,[{new_nodes,[Other|_]=NewNodes},_Abcast,_Ops]} <- Ns],
    Act ++ Pass.

show_spurious(NodeTrace, Spurious) ->
    Pairs = [{Node,ActionNode} || {{Node,ActionNode}, _Time} <- Spurious],
    S = sofs:restriction(sofs:relation(NodeTrace), sofs:set(Pairs)),
    [foo || 
        {{{Node,ANode},Times},
         {{Node,ANode},Ts}} <- lists:zip(family(Spurious), 
                                         sofs:to_external(S)),
        show_spurious(Node, ANode, Times, lists:keysort(2, Ts))].

show_spurious(Node, ActionNode, Times, Ts) ->
    io:format("** Actions for ~p on node ~p **~n", [ActionNode, Node]),
    lists:map(fun(T) -> spurious(Node, T, Times) end, Ts),
    io:format("-- End of actions for ~p on node ~p --~n", [ActionNode, Node]),
    true.

spurious(Node, Trace, Times) ->
    As = case Trace of
             {Node, _T0, {init, Node}, _Nodes, _} ->
                 init; % should not happen, I guess
             {Node, _T0, {nodedown, _ActionNode}, _Nodes, _} ->   
                 nodedown;
             {Node, _T0, {extra_nodedown, _ActionNode}, _Nodes, _} ->
                 extra_nodedown;
             {Node, _T0, {nodeup, _ActionNode}, _Nodes, _} ->
                 nodeup;
             {Node, _T0, {added, Added}, _Nodes, [_Ops]} ->
                 {passive, Added};
             {Node, _T0, {added, Added}, _Nodes, [_NewNodes,_AbCast,_Ops]} ->
                 {negotiator, Added};
             {Node, _T0, {ins_lock, PNode}, _Nodes, [Id, Pid]} ->
                 {insert_lock, [Id, Pid, PNode]};
             {Node, _T0, {rem_lock, PNode}, _Nodes, [Id, Pid]} ->
                 {remove_lock, [Id, Pid, PNode]};
             {Node, _T0, {ins_name, PNode}, _Nodes, [Name, Pid]} ->
                 {insert_name, [Name, Pid, PNode]};
             {Node, _T0, {del_name, PNode}, _Nodes, [Name, Pid]} ->
                 {insert_name, [Name, Pid, PNode]};
             {Node, _T0, {nodes_changed, CNode}, _Nodes, []} ->
                 {nodes_changed, [CNode]};
             {Node, _T0, {Any, Some}, _Nodes, X} ->
                 {Any, [Some | X]}
        end,
    T = element(2, Trace),
    _Nodes2 = element(4, Trace),
    TS = ["(spurious)" || lists:member(T, Times)],
    io:format("~p: ~p ~s~n", [T, As, TS]),
%    io:format("  ~w~n", [_Nodes2]),
    ok.

display_nodes(Why, Time, Nodes) ->
    display_nodes(Why, Time, Nodes, none).

display_nodes(Why, Time, Nodes, LastTrace) ->
    io:format("~p **** ~s ****~n", [Time, Why]),
    {OkL, BossL, NodeXL, BadL} = unzip4([L || #node{n_locks = L} <- Nodes]),
    [NOk, NBoss, NNodeX, NBad] = 
        [lists:sum(L) || L <- [OkL, BossL, NodeXL, BadL]],
    Rejected = lists:sum([Rej || #node{rejected = Rej} <- Nodes]),
    io:format("Locks: (~w+~w+~w=~w)/~w, ~w of ~w rejected~n", 
              [NOk, NBoss, NNodeX, NOk+NBoss+NNodeX, NOk+NBoss+NNodeX+NBad,
               Rejected, NOk]),
    lists:foreach(fun(#node{node = Node, known = Known, nodes = Ns,
                            locks = Locks, names = Names, 
                            n_locks = {Ok, Boss, NodeX, Bad},
                            resolvers = Resolvers0,
                            rejected = Rej}) ->
                          NodeL = io_lib:format("~p: ",[Node]),
                          io:format("~sknown ~p~n", [NodeL, Known]),
                          Sp = spaces(NodeL),
                          case Ns =:= Known of
                              true -> ok;
                              false -> display_list(Sp, nodes, Ns)
                          end,
                          display_list(Sp, locks, Locks),
                          display_list(Sp, names, lists:sort(Names)),
                          Resolvers = lists:sort(Resolvers0),
                          _ResNs = [R || {R,_,_} <- Resolvers],
                          %% Should check trace on this node (Node) only:
                          New = [N || {_,_,{nodeup,N},_,_} <- [LastTrace]],
                          _ResAllowed = (Ns -- New) -- Known,
%% Displays too much junk.
%                           case ResAllowed =:= ResNs of
%                               true -> ok;
%                               false -> display_list(Sp, resol, Resolvers)
%                           end,
                          %% This is less bulky:
                          case Known =:= Ns of
                              true -> display_list(Sp, resol, Resolvers);
                              false -> ok
                          end,
                          case {Ok, Boss, NodeX, Bad} of
                              {0, 0, 0, 0} -> ok;
                              _ -> io:format("~slocks (~w+~w+~w=~w)/~w, "
                                             "~w of ~w rejected~n", 
                                           [Sp, Ok, Boss, NodeX,
                                            Ok+Boss+NodeX,Ok+Boss+NodeX+Bad,
                                            Rej, Ok])
                          end
                  end, lists:keysort(#node.node, Nodes)),
    io:format("\n").

display_list(_S, _What, []) ->
    ok;
display_list(S, What, L) ->
    io:format("~s~p ~p~n", [S, What, L]).

spaces(Iolist) ->
    lists:duplicate(iolist_size(Iolist), $\s).

family(R) ->
    sofs:to_external(sofs:relation_to_family(sofs:relation(R))).

time_diff({S1,MyS1}, {S0,MyS0}) ->
    ((S1*1000000+MyS1) - (S0*1000000+MyS0)) div 1000000.

musec2sec(T) ->
    S = T div 1000000,
    M = (T - S * 1000000),
    {S, M}.

%%% Options

options(Options, Keys) when is_list(Options) ->
    options(Options, Keys, []);
options(Option, Keys) ->
    options([Option], Keys, []).

options(Options0, [Key | Keys], L) when is_list(Options0) ->
    Options = case lists:member(Key, Options0) of
                  true -> 
                      [atom_option(Key) | lists:delete(Key, Options0)];
                  false ->
                      Options0
              end,
    V = case lists:keysearch(Key, 1, Options) of
            {value, {show_state, From, To}} when is_integer(From), From >= 0,
                                                 is_integer(To), To >= From ->
                {ok, {{From,0}, {To,0}}};
            {value, {show_state, {From, FromMusec}, 
                                  {To, ToMusec}}} when is_integer(From), 
                                                       From >= 0,
                                                       is_integer(To), 
                                                       To >= From,
                                                       FromMusec >= 0,
                                                       FromMusec =< 999999,
                                                       ToMusec >= 0,
                                                       ToMusec =< 999999 ->
                {ok, {{From,FromMusec}, {To,ToMusec}}};
            {value, {show_state, false}} ->
                {value, default_option(show_state)};
            {value, {show_trace, Bool}} when Bool; not Bool ->
                {ok, Bool};
            {value, {Key, _}} ->
                badarg;
            false ->
                Default = default_option(Key),
                {ok, Default}
        end,
    case V of
        badarg ->
            badarg;
        {ok, Value} ->
            NewOptions = lists:keydelete(Key, 1, Options),
            options(NewOptions, Keys, [Value | L])
    end;
options([], [], L) ->
    lists:reverse(L);
options(_Options, _, _L) ->
    badarg.

default_option(show_state) -> {{0,0}, {0,0}};
default_option(show_trace) -> true.

atom_option(show_state) ->
    {show_state, 0, 1 bsl 28};
atom_option(show_trace) ->
    {show_trace, true};
atom_option(_) ->
    erlang:error(program_error, []).

unzip4(Ts) -> unzip4(Ts, [], [], [], []).

unzip4([{X, Y, Z, W} | Ts], Xs, Ys, Zs, Ws) ->
    unzip4(Ts, [X | Xs], [Y | Ys], [Z | Zs], [W | Ws]);
unzip4([], Xs, Ys, Zs,  Ws) ->
    {lists:reverse(Xs), lists:reverse(Ys), 
     lists:reverse(Zs), lists:reverse(Ws)}.