aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/save_calls_SUITE.erl
blob: ae1a9e0b4317da820f56a618bf5bcd3d1cba7f02 (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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1999-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(save_calls_SUITE).

-include_lib("test_server/include/test_server.hrl").

-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2]).

-export([save_calls_1/1,dont_break_reductions/1]).

-export([do_bopp/1, do_bipp/0, do_bepp/0]).

suite() -> [{suite_callbacks,[ts_install_scb]}].

all() -> 
[save_calls_1, dont_break_reductions].

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
	Config.

end_per_group(_GroupName, Config) ->
	Config.


dont_break_reductions(suite) ->
    [];
dont_break_reductions(doc) ->
    ["Check that save_calls dont break reduction-based scheduling"];
dont_break_reductions(Config) when is_list(Config) ->
    ?line RPS1 = reds_per_sched(0),
    ?line RPS2 = reds_per_sched(20),
    ?line Diff = abs(RPS1 - RPS2),
    ?line true = (Diff < (0.05 * RPS1)),
    ok.


reds_per_sched(SaveCalls) ->
    ?line Parent = self(),
    ?line HowMany = 10000,
    ?line Pid = spawn(fun() -> 
			process_flag(save_calls,SaveCalls), 
			receive 
			    go -> 
				carmichaels_below(HowMany), 
				Parent ! erlang:process_info(self(),reductions)
			end 
		end),
    ?line TH = spawn(fun() -> trace_handler(0,Parent,Pid) end),
    ?line erlang:trace(Pid, true,[running,procs,{tracer,TH}]),
    ?line Pid ! go,
    ?line {Sched,Reds} = receive 
		       {accumulated,X} -> 
			   receive {reductions,Y} -> 
				   {X,Y} 
			   after 30000 -> 
				   timeout 
			   end 
		   after 30000 -> 
			   timeout 
		   end,
    ?line Reds div Sched.



trace_handler(Acc,Parent,Client) ->
    receive
	{trace,Client,out,_} ->
	    trace_handler(Acc+1,Parent,Client);
	{trace,Client,exit,_} ->
	    Parent ! {accumulated, Acc};
	_ ->
	    trace_handler(Acc,Parent,Client)
    after 10000 ->
	    ok
    end.

save_calls_1(doc) -> "Test call saving.";
save_calls_1(Config) when is_list(Config) ->
    case test_server:is_native(?MODULE) of
	true -> {skipped,"Native code"};
	false -> save_calls_1()
    end.
	    
save_calls_1() ->
    ?line erlang:process_flag(self(), save_calls, 0),
    ?line {last_calls, false} = process_info(self(), last_calls),

    ?line erlang:process_flag(self(), save_calls, 10),
    ?line {last_calls, _L1} = process_info(self(), last_calls),
    ?line ?MODULE:do_bipp(),
    ?line {last_calls, L2} = process_info(self(), last_calls),
    ?line L21 = lists:filter(fun is_local_function/1, L2),
    ?line case L21 of
	      [{?MODULE,do_bipp,0},
	       timeout,
	       'send',
	       {?MODULE,do_bopp,1},
	       'receive',
	       timeout,
	       {?MODULE,do_bepp,0}] ->
		  ok;
	      X ->
		  test_server:fail({l21, X})
	  end,

    ?line erlang:process_flag(self(), save_calls, 10),
    ?line {last_calls, L3} = process_info(self(), last_calls),
    ?line L31 = lists:filter(fun is_local_function/1, L3),
    ?line [] = L31,
    ok.

do_bipp() ->
    do_bopp(0),
    do_bapp(),
    ?MODULE:do_bopp(0),
    do_bopp(3),
    apply(?MODULE, do_bepp, []).

do_bapp() ->
    self() ! heffaklump.

do_bopp(T) ->
    receive
	X -> X
    after T -> ok
    end.

do_bepp() ->
    ok.

is_local_function({?MODULE, _, _}) ->
    true;
is_local_function({_, _, _}) ->
    false;
is_local_function(_) ->
    true.


% Number crunching for reds test.
carmichaels_below(N) ->
    random:seed(3172,9814,20125),
    carmichaels_below(1,N).

carmichaels_below(N,N2) when N >= N2 ->
    0;
carmichaels_below(N,N2) ->
    X = case fast_prime(N,10) of
	false -> 0;
	true ->
	    case fast_prime2(N,10) of
		true ->
		    %io:format("Prime: ~p~n",[N]),
		    0;
		false ->
		    io:format("Carmichael: ~p (dividable by ~p)~n",
			      [N,smallest_divisor(N)]),
		    1
	    end
    end,
    X+carmichaels_below(N+2,N2).

expmod(_,E,_) when E == 0 ->
    1;
expmod(Base,Exp,Mod) when (Exp rem 2) == 0 ->
    X = expmod(Base,Exp div 2,Mod),
    (X*X) rem Mod;
expmod(Base,Exp,Mod) -> 
    (Base * expmod(Base,Exp - 1,Mod)) rem Mod.

uniform(N) ->
    random:uniform(N-1).

fermat(N) ->    
    R = uniform(N),
    expmod(R,N,N) == R.

do_fast_prime(1,_) ->
    true;
do_fast_prime(_N,0) ->
    true;
do_fast_prime(N,Times) ->
    case fermat(N) of
	true ->
	    do_fast_prime(N,Times-1);
	false ->
	    false
    end.
    
fast_prime(N,T) ->
    do_fast_prime(N,T).

expmod2(_,E,_) when E == 0 ->
    1;
expmod2(Base,Exp,Mod) when (Exp rem 2) == 0 ->
%% Uncomment the code below to simulate scheduling bug!
%     case erlang:process_info(self(),last_calls) of
% 	{last_calls,false} -> ok;
% 	_ -> erlang:yield()
%     end,
    X = expmod2(Base,Exp div 2,Mod),
    Y=(X*X) rem Mod,
    if 
	Y == 1, X =/= 1, X =/= (Mod - 1) ->
	    0;
	true ->
	    Y rem Mod
    end;
expmod2(Base,Exp,Mod) -> 
    (Base * expmod2(Base,Exp - 1,Mod)) rem Mod.

miller_rabbin(N) ->
    R = uniform(N),
    expmod2(R,N,N) == R.

do_fast_prime2(1,_) ->
    true;
do_fast_prime2(_N,0) ->
    true;
do_fast_prime2(N,Times) ->
    case miller_rabbin(N) of
	true ->
	    do_fast_prime2(N,Times-1);
	false ->
	    false
    end.
    
fast_prime2(N,T) ->
    do_fast_prime2(N,T).

smallest_divisor(N) ->
    find_divisor(N,2).

find_divisor(N,TD) ->
    if 
	TD*TD > N ->
	    N;
	true ->
	    case divides(TD,N) of
		true ->
		    TD;
		false ->
		    find_divisor(N,TD+1)
	    end
    end.

divides(A,B) ->
    (B rem A) == 0.