summaryrefslogtreecommitdiff
path: root/lib/tools/test/cprof_SUITE.erl
blob: 158a4c696a3d5900cae997daedd1a2ad4d783993 (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
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2002-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%
%%

%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% Define to run outside of test server
%%%
%%% -define(STANDALONE,1).
%%%
%%%
%%% Define for debug output
%%%
%%% -define(debug,1).

-module(cprof_SUITE).

%% Exported end user tests
-export([basic_test/0, on_load_test/1, modules_test/1]).

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Test server related stuff
%%

-ifdef(STANDALONE).
-define(config(A,B),config(A,B)).
-export([config/2]).
-else.
-include_lib("test_server/include/test_server.hrl").
-endif.
 
-ifdef(debug).
-ifdef(STANDALONE).
-define(line, erlang:display({?MODULE,?LINE}), ).
-endif.
-define(dbgformat(A,B),io:format(A,B)).
-else.
-ifdef(STANDALONE).
-define(line, noop, ).
-endif.
-define(dbgformat(A,B),noop).
-endif.
 
-ifdef(STANDALONE).
config(priv_dir, _) ->
    ".";
config(data_dir, _) ->
    "cprof_SUITE_data".
-else.
%% When run in test server.
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2, 
	 init_per_testcase/2, end_per_testcase/2, 
	 not_run/1]).
-export([basic/1, on_load/1, modules/1]).
	 
init_per_testcase(_Case, Config) ->
    ?line Dog=test_server:timetrap(test_server:seconds(30)),
    [{watchdog, Dog}|Config].

end_per_testcase(_Case, Config) ->
    erlang:trace_pattern({'_','_','_'}, false, [local,meta,call_count]),
    erlang:trace_pattern(on_load, false, [local,meta,call_count]),
    erlang:trace(all, false, [all]),
    Dog=?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.

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

all() -> 
    case test_server:is_native(cprof_SUITE) of
	true -> [not_run];
	false -> [basic, on_load, modules]
    end.

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


not_run(Config) when is_list(Config) -> 
    {skipped,"Native code"}.

basic(suite) ->
    [];
basic(doc) ->
    ["Tests basic profiling"];
basic(Config) when is_list(Config) ->
    basic_test().

on_load(suite) ->
    [];
on_load(doc) ->
    ["Tests profiling of unloaded module"];
on_load(Config) when is_list(Config) ->
    on_load_test(Config).

modules(suite) ->
    [];
modules(doc) ->
    ["Tests profiling of several modules"];
modules(Config) when is_list(Config) ->
    modules_test(Config).

-endif. %-ifdef(STANDALONE). ... -else.

%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The Tests
%%%

basic_test() ->
    ?line M = 1000,
    %%
    ?line M2 = M*2,
    ?line M3 = M*3,
    ?line M2__1 = M2 + 1,
    ?line M3__1 = M3 + 1,
    ?line N = cprof:stop(),
    %%
    ?line 2 = cprof:start(?MODULE, seq_r),
    ?line 1 = cprof:start(?MODULE, seq, 3),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    ?line L = lists:reverse(Lr),
    %%
    ?line io:format("~p~n~p~n~p~n", 
		    [erlang:trace_info({?MODULE,sec_r,3}, all),
		     erlang:trace_info({?MODULE,sec_r,4}, all),
		     erlang:trace_info({?MODULE,sec,3}, all)]),
    %%
    ?line ModAna1 = {?MODULE,M2__1,[{{?MODULE,seq_r,4},M},
				   {{?MODULE,seq,3},M},
				   {{?MODULE,seq_r,3},1}]},
    ?line ModAna1 = cprof:analyse(?MODULE,0),
    ?line {M2__1, [ModAna1]} = cprof:analyse(),
    ?line ModAna1 = cprof:analyse(?MODULE, 1),
    ?line {M2__1, [ModAna1]} = cprof:analyse(1),
    %%
    ?line ModAna2 = {?MODULE,M2__1,[{{?MODULE,seq_r,4},M},
				   {{?MODULE,seq,3},M}]},
    ?line ModAna2 = cprof:analyse(?MODULE, 2),
    ?line {M2__1, [ModAna2]} = cprof:analyse(2),
    %%
    2 = cprof:pause(?MODULE, seq_r),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    %%
    ?line ModAna3 = {?MODULE,M3__1,[{{?MODULE,seq,3},M2},
				   {{?MODULE,seq_r,4},M},
				   {{?MODULE,seq_r,3},1}]},
    ?line ModAna3 = cprof:analyse(?MODULE),
    %%
    ?line N = cprof:pause(),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    %%
    ?line {M3__1, [ModAna3]} = cprof:analyse(),
    %%
    ?line N = cprof:restart(),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    %%
    ?line ModAna1 = cprof:analyse(?MODULE),
    %%
    ?line N = cprof:stop(),
    ?line {?MODULE,0,[]} = cprof:analyse(?MODULE),
    ?line {0,[]} = cprof:analyse(),
    ok.

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

on_load_test(Config) ->
    ?line Priv = ?config(priv_dir, Config),
    ?line Data = ?config(data_dir, Config),
    ?line File = filename:join(Data, "cprof_SUITE_test"),
    ?line Module = cprof_SUITE_test,
    ?line M = 1000,
    %%
    ?line M2 = M*2,
    ?line M2__1 = M2 + 1,
    ?line N1 = cprof:start(),

    ?line {ok,Module} = c:c(File, [{outdir,Priv}]),

    %% If this system is hipe-enabled, the loader may have called module_info/1
    %% when Module was loaded above. Reset the call count to avoid seeing
    %% the call in the analysis below.

    ?line 1 = cprof:restart(Module, module_info, 1),

    ?line L = Module:seq(1, M, fun succ/1),
    ?line Lr = Module:seq_r(1, M, fun succ/1),
    ?line Lr = lists:reverse(L),
    ?line N2 = cprof:pause(),
    ?line N3 = cprof:pause(Module),
    ?line {Module,M2__1,[{{Module,seq_r,4},M},
			 {{Module,seq,3},M},
			 {{Module,seq_r,3},1}]} = cprof:analyse(Module),
    ?line io:format("~p ~p ~p~n", [N1, N2, N3]),
    ?line code:purge(Module),
    ?line code:delete(Module),
    ?line N4 = N2 - N3,
    %%
    ?line N4 = cprof:restart(),
    ?line {ok,Module} = c:c(File, [{outdir,Priv}]),
    ?line L = Module:seq(1, M, fun succ/1),
    ?line Lr = Module:seq_r(1, M, fun succ/1),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    ?line N2 = cprof:pause(),
    ?line {Module,0,[]} = cprof:analyse(Module),
    ?line M_1 = M - 1,
    ?line M4__4 = M*4 - 4,
    ?line M10_7 = M*10 - 7,
    ?line {?MODULE,M10_7,[{{?MODULE,succ,1},M4__4},
			  {{?MODULE,seq_r,4},M},
			  {{?MODULE,seq,3},M},
			  {{?MODULE,'-on_load_test/1-fun-5-',1},M_1},
			  {{?MODULE,'-on_load_test/1-fun-4-',1},M_1},
			  {{?MODULE,'-on_load_test/1-fun-3-',1},M_1},
			  {{?MODULE,'-on_load_test/1-fun-2-',1},M_1},
			  {{?MODULE,seq_r,3},1}]} 
	= cprof:analyse(?MODULE),
    ?line N2 = cprof:stop(),
    ok.

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

modules_test(Config) ->
    ?line Priv = ?config(priv_dir, Config),
    ?line Data = ?config(data_dir, Config),
    ?line File = filename:join(Data, "cprof_SUITE_test"),
    ?line Module = cprof_SUITE_test,
    ?line {ok,Module} = c:c(File, [{outdir,Priv}]),
    ?line M = 10,
    %%
    ?line M2 = M*2,
    ?line M2__1 = M2 + 1,
    ?line erlang:yield(),
    ?line N = cprof:start(),
    ?line L = Module:seq(1, M, fun succ/1),
    ?line Lr = Module:seq_r(1, M, fun succ/1),
    ?line L = seq(1, M, fun succ/1),
    ?line Lr = seq_r(1, M, fun succ/1),
    ?line N = cprof:pause(),
    ?line Lr = lists:reverse(L),
    ?line M_1 = M - 1,
    ?line M4_4 = M*4 - 4,
    ?line M10_7 = M*10 - 7,
    ?line M2__1 = M*2 + 1,
    ?line {Tot,ModList} = cprof:analyse(),
    ?line {value,{?MODULE,M10_7,[{{?MODULE,succ,1},M4_4},
				 {{?MODULE,seq_r,4},M},
				 {{?MODULE,seq,3},M},
				 {{?MODULE,'-modules_test/1-fun-3-',1},M_1},
				 {{?MODULE,'-modules_test/1-fun-2-',1},M_1},
				 {{?MODULE,'-modules_test/1-fun-1-',1},M_1},
				 {{?MODULE,'-modules_test/1-fun-0-',1},M_1},
				 {{?MODULE,seq_r,3},1}]}} =
	lists:keysearch(?MODULE, 1, ModList),
    ?line {value,{Module,M2__1,[{{Module,seq_r,4},M},
				{{Module,seq,3},M},
				{{Module,seq_r,3},1}]}} = 
	lists:keysearch(Module, 1, ModList),
    ?line Tot = lists:foldl(fun ({_,C,_}, A) -> C+A end, 0, ModList),
    ?line {cprof,_,Prof} = cprof:analyse(cprof),
    ?line {value,{{cprof,pause,0},1}} = 
	lists:keysearch({cprof,pause,0}, 1, Prof),
    ?line N = cprof:stop(),
    ok.



%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local helpers



%% Stack recursive seq
seq(Stop, Stop, Succ) when is_function(Succ) ->
    [Stop];
seq(Start, Stop, Succ) when is_function(Succ) ->
    [Start | seq(Succ(Start), Stop, Succ)].



%% Tail recursive seq, result list is reversed
seq_r(Start, Stop, Succ) when is_function(Succ) ->
    seq_r(Start, Stop, Succ, []).

seq_r(Stop, Stop, _, R) ->
    [Stop | R];
seq_r(Start, Stop, Succ, R) ->
    seq_r(Succ(Start), Stop, Succ, [Start | R]).



%% Successor
succ(X) -> X+1.