summaryrefslogtreecommitdiff
path: root/lib/runtime_tools/test/erts_alloc_config_SUITE.erl
blob: 10a9560b7344d17b65fe9cba2e9d5e2c96c26850 (plain)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2022. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

-module(erts_alloc_config_SUITE).

%-define(line_trace, 1).

-include_lib("common_test/include/ct.hrl").

%-compile(export_all).
-export([all/0, suite/0,
         init_per_suite/1, end_per_suite/1,
         init_per_testcase/2, end_per_testcase/2]).

%% Testcases
-export([basic/1]).

%% internal export
-export([make_basic_config/1]).

suite() ->
    [{ct_hooks,[ts_install_cth]},
     {timetrap, {minutes, 2}}].

all() -> 
    [basic].

init_per_suite(Config) ->
    case test_server:is_asan() of
	true ->
	    %% No point testing own allocators under address sanitizer.
	    {skip, "Address sanitizer"};
	false ->
	    Config
    end.

end_per_suite(_Config) ->
    ok.

init_per_testcase(Case, Config) when is_list(Config) ->
    [{testcase, Case},
     {erl_flags_env, save_env()} | Config].

end_per_testcase(_Case, Config) when is_list(Config) ->
    restore_env(proplists:get_value(erl_flags_env, Config)),
    ok.

%%%
%%% The test cases ------------------------------------------------------------
%%%

basic(Config) when is_list(Config) ->
    ErtsAllocConfig = privfile("generated", Config),

    SbctMod = " +MBsbct 1024 +MHsbct 4096",

    %% Make sure we have enabled allocators
    ZFlgs = os:getenv("ERL_ZFLAGS", "") ++ " +Mea max +Mea config",

    os:putenv("ERL_ZFLAGS", ZFlgs ++ SbctMod),

    {ok, Peer1, Node1} = ?CT_PEER(),
    ok = rpc:call(Node1, ?MODULE, make_basic_config, [ErtsAllocConfig]),
    peer:stop(Peer1),

    display_file(ErtsAllocConfig),

    ManualConfig = privfile("manual", Config),
    {ok, IOD} = file:open(ManualConfig, [write]),
    io:format(IOD, "~s", ["+MBsbct 2048"]),
    file:close(IOD),
    display_file(ManualConfig),

    os:putenv("ERL_ZFLAGS", ZFlgs),

    {ok, Peer2, Node2} = ?CT_PEER(["-args_file", ErtsAllocConfig,
                             "-args_file", ManualConfig]),

    {_, _, _, Cfg} = rpc:call(Node2, erlang, system_info, [allocator]),

    peer:stop(Peer2),

    {value,{binary_alloc, BCfg}} = lists:keysearch(binary_alloc, 1, Cfg),
    {value,{sbct, 2097152}} = lists:keysearch(sbct, 1, BCfg),
    {value,{eheap_alloc, HCfg}} = lists:keysearch(eheap_alloc, 1, Cfg),
    {value,{sbct, 4194304}} = lists:keysearch(sbct, 1, HCfg),

    ok.

make_basic_config(ErtsAllocConfig) ->
    %% Save some different scenarios
    Tester = self(),
    SSBegun = make_ref(),
    SSDone = make_ref(),
    SSFun = fun (F) ->
                    receive
                        SSDone ->
                            ok = erts_alloc_config:save_scenario(),
                            Tester ! SSDone
                    after 500 ->
                              ok = erts_alloc_config:save_scenario(),
                              F(F)
                    end
            end,
    SS = spawn_link(fun () ->
                            ok = erts_alloc_config:save_scenario(),
                            Tester ! SSBegun,
                            SSFun(SSFun)
                    end),
    receive SSBegun -> ok end,
    Ref = make_ref(),
    Tab = ets:new(?MODULE, [bag, public]),
    Ps = lists:map(
           fun (_) ->
                   spawn_link(
                     fun () ->
                             ets:insert(Tab,
                                        {self(),
                                         lists:seq(1, 1000)}),
                             receive after 1000 -> ok end,
                             Tester ! {Ref, self()}
                     end)
           end,
           lists:seq(1, 10000)),
    lists:foreach(fun (P) -> receive {Ref, P} -> ok end end, Ps),
    ets:delete(Tab),
    SS ! SSDone,
    receive SSDone -> ok end,

    ok = erts_alloc_config:make_config(ErtsAllocConfig).



%%
%% Utils ----------------------------------------------------------------------
%%

display_file(FileName) ->
    io:format("filename: ~s~n", [FileName]),
    {ok, Bin} = file:read_file(FileName),
    io:format("~s", [binary_to_list(Bin)]),
    io:format("eof: ~s~n", [FileName]),
    ok.

privfile(Name, Config) ->
    filename:join([proplists:get_value(priv_dir, Config),
                   atom_to_list(proplists:get_value(testcase, Config)) ++ "." ++ Name]).

save_env() ->
    {erl_flags,
     os:getenv("ERL_AFLAGS"),
     os:getenv("ERL_FLAGS"),
     os:getenv("ERL_"++erlang:system_info(otp_release)++"_FLAGS"),
     os:getenv("ERL_ZFLAGS")}.

restore_env(EVar, false) when is_list(EVar) ->
    restore_env(EVar, "");
restore_env(EVar, "") when is_list(EVar) ->
    case os:getenv(EVar) of
        false -> ok;
        "" -> ok;
        " " -> ok;
        _ -> os:putenv(EVar, " ")
    end;
restore_env(EVar, Value) when is_list(EVar), is_list(Value) ->
    case os:getenv(EVar) of
        Value -> ok;
        _ -> os:putenv(EVar, Value)
    end.

restore_env({erl_flags, AFlgs, Flgs, RFlgs, ZFlgs}) ->
    restore_env("ERL_AFLAGS", AFlgs),
    restore_env("ERL_FLAGS", Flgs),
    restore_env("ERL_"++erlang:system_info(otp_release)++"_FLAGS", RFlgs),
    restore_env("ERL_ZFLAGS", ZFlgs),
    ok.