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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2009. 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%
%%
%% Purpose: Profile a system in order to figure out where the
%% time goes.
%%
-module(eprof).
-behaviour(gen_server).
-export([start/0,
stop/0,
dump/0,
start_profiling/1, start_profiling/2,
profile/1, profile/2, profile/3, profile/4, profile/5,
stop_profiling/0,
analyze/0, analyze/1, analyze/2,
log/1]).
%% Internal exports
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-record(bpd, {
n = 0, % number of total calls
us = 0, % sum of uS for all calls
p = gb_trees:empty(), % tree of {Pid, {Mfa, {Count, Us}}}
mfa = [] % list of {Mfa, {Count, Us}}
}).
-record(state, {
profiling = false,
pattern = {'_','_','_'},
rootset = [],
fd = undefined,
start_ts = undefined,
reply = undefined,
bpd = #bpd{}
}).
%% -------------------------------------------------------------------- %%
%%
%% API
%%
%% -------------------------------------------------------------------- %%
start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []).
stop() -> gen_server:call(?MODULE, stop, infinity).
profile(Fun) when is_function(Fun) ->
profile([], Fun);
profile(Rs) when is_list(Rs) ->
start_profiling(Rs).
profile(Pids, Fun) ->
profile(Pids, Fun, {'_','_','_'}).
profile(Pids, Fun, Pattern) ->
profile(Pids, Pattern, erlang, apply, [Fun,[]]).
profile(Pids, M, F, A) ->
profile(Pids, {'_','_','_'}, M, F, A).
profile(Pids, Pattern, M, F, A) ->
start(),
gen_server:call(?MODULE, {profile,Pids,Pattern, M,F,A},infinity).
dump() ->
gen_server:call(?MODULE, dump, infinity).
analyze() ->
analyze(procs).
analyze(Type) when is_atom(Type) ->
analyze(Type, []);
analyze(Opts) when is_list(Opts) ->
analyze(procs, Opts).
analyze(Type, Opts) when is_list(Opts) ->
gen_server:call(?MODULE, {analyze, Type, Opts}, infinity).
log(File) ->
gen_server:call(?MODULE, {logfile, File}, infinity).
start_profiling(Rootset) ->
start_profiling(Rootset, {'_','_','_'}).
start_profiling(Rootset, Pattern) ->
start(),
gen_server:call(?MODULE, {profile, Rootset, Pattern}, infinity).
stop_profiling() ->
gen_server:call(?MODULE, stop_profiling, infinity).
%% -------------------------------------------------------------------- %%
%%
%% init
%%
%% -------------------------------------------------------------------- %%
init([]) ->
process_flag(trap_exit, true),
{ok, #state{}}.
%% -------------------------------------------------------------------- %%
%%
%% handle_call
%%
%% -------------------------------------------------------------------- %%
%% analyze
sum_bp_total_n_us(Mfas) ->
lists:foldl(fun ({_, {Ci,Usi}}, {Co, Uso}) -> {Co + Ci, Uso + Usi} end, {0,0}, Mfas).
print_bp_mfa(Mfas, {_Tn, Tus}, Fd, Opts) ->
print(Fd, ["FUNCTION", "CALLS", " %", "TIME", "uS / CALLS"]),
print(Fd, ["--------", "-----", "---", "----", "----------"]),
lists:foreach(fun
({_, {Count, Time}}) when Count =:= 0; Time < 1 ->
ok;
({Mfa, {Count, Time}}) ->
print(Fd, [s(Mfa), s(Count), s("~.2f", [100*(Time/Tus)]), s(Time), s("~.2f", [Time/Count])]),
ok
end, filter_mfa(sort_mfa(Mfas, proplists:get_value(sort, Opts)), proplists:get_value(thresholds, Opts))),
ok.
handle_call({analyze, _, _}, _, #state{ bpd = #bpd{ p = {0,nil}, us = 0, n = 0} = Bpd } = S) when is_record(Bpd, bpd) ->
{reply, nothing_to_analyze, S};
handle_call({analyze, procs, Opts}, _, #state{ bpd = #bpd{ p = Ps, us = Tus} = Bpd, fd = Fd} = S) when is_record(Bpd, bpd) ->
lists:foreach(fun
({Pid, Mfas}) ->
{Pn, Pus} = sum_bp_total_n_us(Mfas),
format(Fd, "~n****** Process ~w -- ~s % of profiled time *** ~n", [Pid, s("~.2f", [100.0*(Pus/Tus)])]),
print_bp_mfa(Mfas, {Pn,Pus}, Fd, Opts),
ok
end, gb_trees:to_list(Ps)),
{reply, ok, S};
handle_call({analyze, total, Opts}, _, #state{ bpd = #bpd{ mfa = Mfas, n = Tn, us = Tus} = Bpd, fd = Fd} = S) when is_record(Bpd, bpd) ->
print_bp_mfa(Mfas, {Tn, Tus}, Fd, Opts),
{reply, ok, S};
handle_call({analyze, Type, _Opts}, _, S) ->
{reply, {error, {undefined, Type}}, S};
%% profile
handle_call({profile, _Rootset, _Pattern, _M,_F,_A}, _From, #state{ profiling = true } = S)->
{reply, {error, already_profiling}, S};
handle_call({profile, Rootset, Pattern, M,F,A}, From, #state{fd = Fd } = S) ->
set_pattern_trace(false, S#state.pattern),
set_process_trace(false, S#state.rootset),
Pid = setup_profiling(M,F,A),
case set_process_trace(true, [Pid|Rootset]) of
true ->
set_pattern_trace(true, Pattern),
T0 = now(),
execute_profiling(Pid),
{noreply, #state{
profiling = true,
rootset = [Pid|Rootset],
start_ts = T0,
reply = From,
fd = Fd,
pattern = Pattern
}};
false ->
exit(Pid, kill),
{reply, error, #state{ fd = Fd}}
end;
handle_call({profile, Rootset, Pattern}, From, #state{ fd = Fd } = S) ->
set_pattern_trace(false, S#state.pattern),
set_process_trace(false, S#state.rootset),
case set_process_trace(true, Rootset) of
true ->
T0 = now(),
set_pattern_trace(true, Pattern),
{reply, profiling, #state{
profiling = true,
rootset = Rootset,
start_ts = T0,
reply = From,
fd = Fd,
pattern = Pattern
}};
false ->
{reply, error, #state{ fd = Fd }}
end;
handle_call(stop_profiling, _From, #state{ profiling = false } = S) ->
{reply, profiling_already_stopped, S};
handle_call(stop_profiling, _From, #state{ profiling = true } = S) ->
set_pattern_trace(pause, S#state.pattern),
Bpd = collect_bpd(),
set_process_trace(false, S#state.rootset),
set_pattern_trace(false, S#state.pattern),
{reply, profiling_stopped, S#state{
profiling = false,
rootset = [],
pattern = {'_','_','_'},
bpd = Bpd
}};
%% logfile
handle_call({logfile, File}, _From, #state{ fd = OldFd } = S) ->
case file:open(File, [write]) of
{ok, Fd} ->
case OldFd of
undefined -> ok;
OldFd -> file:close(OldFd)
end,
{reply, ok, S#state{ fd = Fd}};
Error ->
{reply, Error, S}
end;
handle_call(dump, _From, #state{ bpd = Bpd } = S) when is_record(Bpd, bpd) ->
{reply, gb_trees:to_list(Bpd#bpd.p), S};
handle_call(stop, _FromTag, S) ->
{stop, normal, stopped, S}.
%% -------------------------------------------------------------------- %%
%%
%% handle_cast
%%
%% -------------------------------------------------------------------- %%
handle_cast(_Msg, State) ->
{noreply, State}.
%% -------------------------------------------------------------------- %%
%%
%% handle_info
%%
%% -------------------------------------------------------------------- %%
handle_info({trace, _Pid, _Cmd, _Type}, S) ->
{noreply, S};
handle_info({trace, _Parent, spawn, _Pid, _Mfa}, S) ->
{noreply, S};
handle_info({'EXIT', _, normal}, S) ->
{noreply, S};
handle_info({'EXIT', _, Reason}, #state{ reply = FromTag } = S) ->
set_process_trace(false, S#state.rootset),
set_pattern_trace(false, S#state.pattern),
gen_server:reply(FromTag, {error, Reason}),
{noreply, S#state{
profiling = false,
rootset = [],
pattern = {'_','_','_'}
}};
% check if Pid is spawned process?
handle_info({_Pid, {answer, Result}}, #state{ reply = {From,_} = FromTag} = S) ->
set_pattern_trace(pause, S#state.pattern),
Bpd = collect_bpd(),
set_process_trace(false, S#state.rootset),
set_pattern_trace(false, S#state.pattern),
catch unlink(From),
gen_server:reply(FromTag, {ok, Result}),
{noreply, S#state{
profiling = false,
rootset = [],
pattern = {'_','_','_'},
bpd = Bpd
}}.
%% -------------------------------------------------------------------- %%
%%
%% termination
%%
%% -------------------------------------------------------------------- %%
terminate(_Reason, #state{ fd = undefined }) ->
set_pattern_trace(false, {'_','_','_'}),
ok;
terminate(_Reason, #state{ fd = Fd }) ->
file:close(Fd),
set_pattern_trace(false, {'_','_','_'}),
ok.
%% -------------------------------------------------------------------- %%
%%
%% code_change
%%
%% -------------------------------------------------------------------- %%
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% -------------------------------------------------------------------- %%
%%
%% AUX Functions
%%
%% -------------------------------------------------------------------- %%
setup_profiling(M,F,A) ->
spawn_link(fun() -> spin_profile(M,F,A) end).
spin_profile(M, F, A) ->
receive
{Pid, execute} ->
Pid ! {self(), {answer, erlang:apply(M,F,A)}}
end.
execute_profiling(Pid) ->
Pid ! {self(), execute}.
set_pattern_trace(Flag, Pattern) ->
erlang:system_flag(multi_scheduling, block),
erlang:trace_pattern(on_load, Flag, [call_time]),
erlang:trace_pattern(Pattern, Flag, [call_time]),
erlang:system_flag(multi_scheduling, unblock),
ok.
set_process_trace(Flag, Pids) ->
% do we need procs for meta info?
% could be useful
set_process_trace(Flag, Pids, [call, set_on_spawn]).
set_process_trace(_, [], _) -> true;
set_process_trace(Flag, [Pid|Pids], Options) when is_pid(Pid) ->
try
erlang:trace(Pid, Flag, Options)
catch
_:_ ->
false
end,
set_process_trace(Flag, Pids, Options);
set_process_trace(Flag, [Name|Pids], Options) when is_atom(Name) ->
case whereis(Name) of
undefined ->
set_process_trace(Flag, Pids, Options);
Pid ->
set_process_trace(Flag, [Pid|Pids], Options)
end.
collect_bpd() ->
collect_bpd([M || M <- [element(1, Mi) || Mi <- code:all_loaded()], M =/= ?MODULE]).
collect_bpd(Ms) when is_list(Ms) ->
collect_bpdf(collect_mfas(Ms) ++ erlang:system_info(snifs)).
collect_mfas(Ms) ->
lists:foldl(fun
(M, Mfas) ->
Mfas ++ [{M, F, A} || {F, A} <- M:module_info(functions)]
end, [], Ms).
collect_bpdf(Mfas) ->
collect_bpdf(Mfas, #bpd{}).
collect_bpdf([], Bpd) ->
Bpd;
collect_bpdf([Mfa|Mfas], #bpd{n = N, us = Us, p = Tree, mfa = Code } = Bpd) ->
case erlang:trace_info(Mfa, call_time) of
{call_time, []} ->
collect_bpdf(Mfas, Bpd);
{call_time, Data} when is_list(Data) ->
{CTn, CTus, CTree} = collect_bpdfp(Mfa, Tree, Data),
collect_bpdf(Mfas, Bpd#bpd{
n = CTn + N,
us = CTus + Us,
p = CTree,
mfa = [{Mfa, {CTn, CTus}}|Code]
});
{call_time, false} ->
collect_bpdf(Mfas, Bpd);
{call_time, _Other} ->
collect_bpdf(Mfas, Bpd)
end.
collect_bpdfp(Mfa, Tree, Data) ->
lists:foldl(fun
({Pid, Ni, Si, Usi}, {PTno, PTuso, To}) ->
Time = Si * 1000000 + Usi,
Ti1 = case gb_trees:lookup(Pid, To) of
none ->
gb_trees:enter(Pid, [{Mfa, {Ni, Time}}], To);
{value, Pmfas} ->
gb_trees:enter(Pid, [{Mfa, {Ni, Time}}|Pmfas], To)
end,
{PTno + Ni, PTuso + Time, Ti1}
end, {0,0, Tree}, Data).
%% manipulators
sort_mfa(Bpfs, ascending) when is_list(Bpfs) ->
lists:sort(fun
({_,{_,A}}, {_,{_,B}}) when A > B -> true;
(_, _) -> false
end, Bpfs);
sort_mfa(Bpfs, descending) when is_list(Bpfs) ->
lists:sort(fun
({_,{_,A}}, {_,{_,B}}) when A < B -> true;
(_, _) -> false
end, Bpfs);
sort_mfa(Bpfs, _) when is_list(Bpfs) -> sort_mfa(Bpfs, ascending).
filter_mfa(Bpfs, Ts) when is_list(Ts) ->
filter_mfa(Bpfs, [], proplists:get_value(calls, Ts, 0), proplists:get_value(time, Ts, 0));
filter_mfa(Bpfs, _) -> Bpfs.
filter_mfa([], Out, _, _) -> lists:reverse(Out);
filter_mfa([{_, {C, T}}=Bpf|Bpfs], Out, Ct, Tt) when C >= Ct, T >= Tt -> filter_mfa(Bpfs, [Bpf|Out], Ct, Tt);
filter_mfa([_|Bpfs], Out, Ct, Tt) -> filter_mfa(Bpfs, Out, Ct, Tt).
%% strings and format
s({M,F,A}) -> s("~w:~w/~w",[M,F,A]);
s(Term) -> s("~p", [Term]).
s(Format, Terms) -> lists:flatten(io_lib:format(Format, Terms)).
print(Fd, [_,_,_] = Strings) ->
print(Fd, "~.44s ~14s ~14s~n", Strings);
print(Fd, [_,_,_,_] = Strings) ->
print(Fd, "~.44s ~14s ~14s ~7s ~n", Strings);
print(Fd, Strings) ->
print(Fd, "~.44s ~14s ~14s ~7s [~7s]~n", Strings).
print(Fd, Format, Strings) ->
format(Fd, Format, Strings).
format(undefined, Format, Strings) ->
io:format(Format, Strings),
ok;
format(Fd, Format, Strings) ->
io:format(Fd, Format, Strings),
io:format(Format, Strings),
ok.
|