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

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

-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2, init_per_testcase/2, fin_per_testcase/2]).
-export([equal/1, many_low/1, few_low/1, max/1, high/1]).

-define(default_timeout, ?t:minutes(11)).

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

all() -> 
case catch erlang:system_info(modified_timing_level) of
  Level when is_integer(Level) ->
      {skipped,
       "Modified timing (level " ++
	 integer_to_list(Level) ++
	   ") is enabled. Testcases gets messed "
	   "up by modfied timing."};
  _ -> [equal, many_low, few_low, max, high]
end.

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
	Config.

end_per_group(_GroupName, Config) ->
	Config.


%%-----------------------------------------------------------------------------------
%% TEST SUITE DESCRIPTION
%%
%% The test case function spawns two controlling processes: Starter and Receiver.
%% Starter spawns a number of prio A and a number of prio B test processes. Each
%% test process loops for a number of times, sends a report to the Receiver, then
%% loops again. For each report, the Receiver increases a counter that corresponds
%% to the priority of the sender. After a certain amount of time, the Receiver
%% sends the collected data to the main test process and waits for the test case
%% to terminate. From this data, it's possible to calculate the average run time
%% relationship between the prio A and B test processes.
%%
%% Note that in order to be able to run tests with high or max prio test processes, 
%% the main test process and the Receiver needs to run at max prio, or they will
%% be starved by the test processes. The controlling processes must not wait for
%% messages from a normal (or low) prio process while max or high prio test processes
%% are running (which happens e.g. if an io function is called).
%%-----------------------------------------------------------------------------------

init_per_testcase(_Case, Config) ->
    ?line Dog = test_server:timetrap(?default_timeout),
    %% main test process needs max prio
    ?line Prio = process_flag(priority, max),
    ?line MS = erlang:system_flag(multi_scheduling, block),
    [{prio,Prio},{watchdog,Dog},{multi_scheduling, MS}|Config].

fin_per_testcase(_Case, Config) ->
    erlang:system_flag(multi_scheduling, unblock),
    Dog=?config(watchdog, Config),
    Prio=?config(prio, Config),
    process_flag(priority, Prio),
    test_server:timetrap_cancel(Dog),
    ok.

ok(Config) when is_list(Config) ->
    case ?config(multi_scheduling, Config) of
	blocked ->
	    {comment,
	     "Multi-scheduling blocked during test. This testcase was not "
	     "written to work with multiple schedulers."};
	_ -> ok
    end.

%% Run equal number of low and normal prio processes.

equal(suite) -> [];
equal(doc) -> [];
equal(Config) when is_list(Config) ->
    ?line Self = self(),

    %% specify number of test processes to run
    Normal = {normal,500},
    Low    = {low,500},

    %% specify time of test (in seconds)
    Time = 30,

    %% start controllers
    ?line Receiver = 
	spawn(fun() -> receiver(now(), Time, Self, Normal, Low) end),
    ?line Starter =
	spawn(fun() -> starter(Normal, Low, Receiver) end),

    %% receive test data from Receiver
    ?line {NRs,NAvg,LRs,LAvg,Ratio} = 
	receive
	    {Receiver,Res} -> Res
	end,

    %% stop controllers and test processes
    ?line exit(Starter, kill),
    ?line exit(Receiver, kill),

    io:format("Reports: ~w normal (~w/proc), ~w low (~w/proc). Ratio: ~w~n", 
	      [NRs,NAvg,LRs,LAvg,Ratio]),

    %% runtime ratio between normal and low should be ~8
    if Ratio < 7.5 ; Ratio > 8.5 ->	
	    ?t:fail({bad_ratio,Ratio});
       true ->
	    ok(Config)
    end.


%% Run many low and few normal prio processes.

many_low(suite) -> [];
many_low(doc) -> [];
many_low(Config) when is_list(Config) ->
    ?line Self = self(),
    Normal = {normal,1},
    Low    = {low,1000},

    %% specify time of test (in seconds)
    Time = 30,

    ?line Receiver = 
	spawn(fun() -> receiver(now(), Time, Self, Normal, Low) end),
    ?line Starter =
	spawn(fun() -> starter(Normal, Low, Receiver) end),
    ?line {NRs,NAvg,LRs,LAvg,Ratio} = 
	receive
	    {Receiver,Res} -> Res
	end,
    ?line exit(Starter, kill),
    ?line exit(Receiver, kill),
    io:format("Reports: ~w normal (~w/proc), ~w low (~w/proc). Ratio: ~w~n", 
	      [NRs,NAvg,LRs,LAvg,Ratio]),
    if Ratio < 7.5 ; Ratio > 8.5 ->
	    ?t:fail({bad_ratio,Ratio});
       true ->
	    ok(Config)
    end.


%% Run few low and many normal prio processes.

few_low(suite) -> [];
few_low(doc) -> [];
few_low(Config) when is_list(Config) ->
    ?line Self = self(),
    Normal = {normal,1000},
    Low    = {low,1},

    %% specify time of test (in seconds)
    Time = 30,

    ?line Receiver = 
	spawn(fun() -> receiver(now(), Time, Self, Normal, Low) end),
    ?line Starter =
	spawn(fun() -> starter(Normal, Low, Receiver) end),
    ?line {NRs,NAvg,LRs,LAvg,Ratio} = 
	receive
	    {Receiver,Res} -> Res
	end,
    ?line exit(Starter, kill),
    ?line exit(Receiver, kill),
    io:format("Reports: ~w normal (~w/proc), ~w low (~w/proc). Ratio: ~w~n", 
	      [NRs,NAvg,LRs,LAvg,Ratio]),
    if Ratio < 7.0 ; Ratio > 8.5 ->
	    ?t:fail({bad_ratio,Ratio});
       true ->
	    ok(Config)
    end.


%% Run max prio processes and verify they get at least as much 
%% runtime as high, normal and low.

max(suite) -> [];
max(doc) -> [];
max(Config) when is_list(Config) ->
    max = process_flag(priority, max),		% should already be max (init_per_tc)
    ?line Self = self(),
    Max    = {max,2},
    High   = {high,2},
    Normal = {normal,100},
    Low    = {low,100},

    %% specify time of test (in seconds)
    Time = 30,

    ?line Receiver1 = 
	spawn(fun() -> receiver(now(), Time, Self, Max, High) end),
    ?line Starter1 =
	spawn(fun() -> starter(Max, High, Receiver1) end),
    ?line {M1Rs,M1Avg,HRs,HAvg,Ratio1} = 
	receive
	    {Receiver1,Res1} -> Res1
	end,
    ?line exit(Starter1, kill),
    ?line exit(Receiver1, kill),
    io:format("Reports: ~w max (~w/proc), ~w high (~w/proc). Ratio: ~w~n", 
	      [M1Rs,M1Avg,HRs,HAvg,Ratio1]),
    if Ratio1 < 1.0 ->
	    ?t:fail({bad_ratio,Ratio1});
       true ->
	    ok(Config)
    end,

    ?line Receiver2 = 
	spawn(fun() -> receiver(now(), Time, Self, Max, Normal) end),
    ?line Starter2 =
	spawn(fun() -> starter(Max, Normal, Receiver2) end),
    ?line {M2Rs,M2Avg,NRs,NAvg,Ratio2} = 
	receive
	    {Receiver2,Res2} -> Res2
	end,
    ?line exit(Starter2, kill),
    ?line exit(Receiver2, kill),
    io:format("Reports: ~w max (~w/proc), ~w normal (~w/proc). Ratio: ~w~n", 
	      [M2Rs,M2Avg,NRs,NAvg,Ratio2]),
    if Ratio2 < 1.0 ->
	    ?t:fail({bad_ratio,Ratio2});
       true ->
	    ok
    end,

    ?line Receiver3 = 
	spawn(fun() -> receiver(now(), Time, Self, Max, Low) end),
    ?line Starter3 =
	spawn(fun() -> starter(Max, Low, Receiver3) end),
    ?line {M3Rs,M3Avg,LRs,LAvg,Ratio3} = 
	receive
	    {Receiver3,Res3} -> Res3
	end,
    ?line exit(Starter3, kill),
    ?line exit(Receiver3, kill),
    io:format("Reports: ~w max (~w/proc), ~w low (~w/proc). Ratio: ~w~n", 
	      [M3Rs,M3Avg,LRs,LAvg,Ratio3]),
    if Ratio3 < 1.0 ->
	    ?t:fail({bad_ratio,Ratio3});
       true ->
	    ok(Config)
    end.


%% Run high prio processes and verify they get at least as much 
%% runtime as normal and low.

high(suite) -> [];
high(doc) -> [];
high(Config) when is_list(Config) ->
    max = process_flag(priority, max),		% should already be max (init_per_tc)
    ?line Self = self(),
    High   = {high,2},
    Normal = {normal,100},
    Low    = {low,100},

    %% specify time of test (in seconds)
    Time = 30,

    ?line Receiver1 = 
	spawn(fun() -> receiver(now(), Time, Self, High, Normal) end),
    ?line Starter1 =
	spawn(fun() -> starter(High, Normal, Receiver1) end),
    ?line {H1Rs,H1Avg,NRs,NAvg,Ratio1} = 
	receive
	    {Receiver1,Res1} -> Res1
	end,
    ?line exit(Starter1, kill),
    ?line exit(Receiver1, kill),
    io:format("Reports: ~w high (~w/proc), ~w normal (~w/proc). Ratio: ~w~n", 
	      [H1Rs,H1Avg,NRs,NAvg,Ratio1]),
    if Ratio1 < 1.0 ->
	    ?t:fail({bad_ratio,Ratio1});
       true ->
	    ok
    end,

    ?line Receiver2 = 
	spawn(fun() -> receiver(now(), Time, Self, High, Low) end),
    ?line Starter2 =
	spawn(fun() -> starter(High, Low, Receiver2) end),
    ?line {H2Rs,H2Avg,LRs,LAvg,Ratio2} = 
	receive
	    {Receiver2,Res2} -> Res2
	end,
    ?line exit(Starter2, kill),
    ?line exit(Receiver2, kill),
    io:format("Reports: ~w high (~w/proc), ~w low (~w/proc). Ratio: ~w~n", 
	      [H2Rs,H2Avg,LRs,LAvg,Ratio2]),
    if Ratio2 < 1.0 ->
	    ?t:fail({bad_ratio,Ratio2});
       true ->
	    ok(Config)
    end.


%%-----------------------------------------------------------------------------------
%% Controller processes and help functions
%%-----------------------------------------------------------------------------------

receiver(T0, TimeSec, Main, {P1,P1N}, {P2,P2N}) ->
    %% prio should be max so that mailbox doesn't overflow
    process_flag(priority, max),
    receiver(T0, TimeSec*1000, Main, P1,P1N,0, P2,P2N,0, 100000).

%% uncomment lines below to get life sign (debug)
receiver(T0, Time, Main, P1,P1N,P1Rs, P2,P2N,P2Rs, 0) ->
%    T = elapsed_ms(T0, now()),
%    erlang:display({round(T/1000),P1Rs,P2Rs}),
    receiver(T0, Time, Main, P1,P1N,P1Rs, P2,P2N,P2Rs, 100000);

receiver(T0, Time, Main, P1,P1N,P1Rs, P2,P2N,P2Rs, C) ->
    Remain = Time - elapsed_ms(T0, now()),	% test time remaining
    Remain1 = if Remain < 0 ->
		      0;
		 true ->
		      Remain
	      end,
    {P1Rs1,P2Rs1} = 
	receive
	    {_Pid,P1} ->			% report from a P1 process
		{P1Rs+1,P2Rs};
	    {_Pid,P2} ->			% report from a P2 process
		{P1Rs,P2Rs+1}
	after Remain1 ->
		{P1Rs,P2Rs}
	end,
    if Remain > 0 ->				% keep going
	    receiver(T0, Time, Main, P1,P1N,P1Rs1, P2,P2N,P2Rs1, C-1);
       true ->					% finish
	    %% calculate results and send to main test process
	    P1Avg = P1Rs1/P1N,
	    P2Avg = P2Rs1/P2N,
	    Ratio = if P2Avg < 1.0 -> P1Avg;
		       true -> P1Avg/P2Avg
		    end,
	    Main ! {self(),{P1Rs1,round(P1Avg),P2Rs1,round(P2Avg),Ratio}},
	    flush_loop()
    end.

starter({P1,P1N}, {P2,P2N}, Receiver) ->
    %% start N1 processes with prio P1
    start_p(P1, P1N, Receiver),
    %% start N2 processes with prio P2
    start_p(P2, P2N, Receiver),
    erlang:display({started,P1N+P2N}),
    flush_loop().

start_p(_, 0, _) ->
    ok;
start_p(Prio, N, Receiver) ->
    spawn_link(fun() -> p(Prio, Receiver) end),
    start_p(Prio, N-1, Receiver).

p(Prio, Receiver) ->
    %% set process priority
    process_flag(priority, Prio),
    p_loop(0, Prio, Receiver).

p_loop(100, Prio, Receiver) ->
    receive after 0 -> ok end,
    %% if Receiver gone, we're done
    case is_process_alive(Receiver) of
	false -> exit(bye);
	true -> ok
    end,
    %% send report
    Receiver ! {self(),Prio},
    p_loop(0, Prio, Receiver);

p_loop(N, Prio, Receiver) ->
    p_loop(N+1, Prio, Receiver).
    		       

flush_loop() ->
    receive _ ->
	    ok
    end,
    flush_loop().

elapsed_ms({_MS0,S0,MuS0},{_MS1,S1,MuS1}) ->
    round(((S1-S0)*1000)+((MuS1-MuS0)/1000)).