aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/tools/hipe_profile.erl
blob: 7566acb8f43ebc8e29c35397b02898d6df23a68b (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
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2002-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%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2001 by Erik Johansson.  All Rights Reserved 
%% Time-stamp: <2008-04-20 14:53:42 richard>
%% ====================================================================
%%  Module   :	hipe_profile
%%  Purpose  :  
%%  History  :	* 2001-07-12 Erik Johansson (happi@it.uu.se): Created.
%% ====================================================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(hipe_profile).

-export([%% profile/1, mods_profile/1,
	 prof/0, prof_off/0, clear/0, res/0,
	 mods_res/0,
	 %% clear_module/1, res_module/1,
	 prof_module/1, prof_module_off/1]).

%% %% @spec mods_profile(F) -> [{mod(),calls()}]
%% %%       F = () -> term()
%% %%       mod()  = atom()
%% %%       calls()= integer()
%% %%
%% %% @doc  Returns the number of calls per module generated by
%% %%       applying F().
%% %%       The resulting lists is sorted with the most called
%% %%       module first.
%% mods_profile(F) ->
%%   F(),
%%   prof(),
%%   clear(), 
%%   F(), 
%%   R = mods_res(),
%%   prof_off(), 
%%   R.

-spec mods_res() -> [{atom(), non_neg_integer()}].
%% @doc  Returns the number of calls per module currently 
%%       recordeed since hipe_bifs:call_count_on().
%%       The resulting list is sorted with the most called
%%       module first.
mods_res() ->
  lists:reverse(lists:keysort(2, calls())).

-spec calls() -> [{atom(), non_neg_integer()}].
%% @doc  Returns the number of calls per module currently 
%%       recordeed since hipe_bifs:call_count_on().
calls() ->
  [{Mod, total_calls(Mod)} || Mod <- mods(),
			      total_calls(Mod) > 1,
			      Mod =/= hipe_profile].

%% %% @spec profile(F) -> [{mfa(),calls()}]
%% %%       F = ()    -> term()
%% %%       mfa()      = {mod(),function(),arity()}
%% %%       function() = atom()
%% %%       arity()    = intger()
%% %%
%% %% @doc  Returns the number of calls per module generated by
%% %%       applying F().
%% %%       The resulting lists is sorted with the most called
%% %%       module first.
%% profile(F) ->
%%   %% Make sure all code is loaded.
%%   F(),
%%   %% Turn profiling on.
%%   prof(),
%%   clear(), 
%%   %% Apply the closure to profile.
%%   F(), 
%%   %% Get result.
%%   R = res(),
%%   %% Turn of profiling.
%%   prof_off(),
%%   R.

-spec prof() -> 'ok'.
%% @doc Turns on profiling of all loaded modules.
prof() ->
  lists:foreach(fun prof_module/1, mods()).

-spec prof_off() -> 'ok'.
%% @doc Turns off profiling of all loaded modules.
 prof_off() ->
  lists:foreach(fun prof_module_off/1, mods()).

-spec clear() -> 'ok'.
%% @doc Clears all counters.
clear() ->
  lists:foreach(fun clear_module/1, mods()).

-spec res() -> [{mfa(), non_neg_integer()}].
%% @doc Returns a list of the numbers of calls to each profiled function.
%%      The list is sorted with the most called function first.
res() ->
  lists:reverse(lists:keysort(2, lists:flatten([res_module(M) || M <- mods()]))).

%% --------------------------------------------------------------------
-spec mods() -> [atom()].
%% @doc	 Returns a list of all loaded modules. 
%@ --------------------------------------------------------------------

mods() ->
  [Mod || {Mod,_} <- code:all_loaded()].

%% --------------------------------------------------------------------
-spec prof_module(atom()) -> 'ok'.
%% @doc	 Turns on profiling for given module. 
%@ ____________________________________________________________________

prof_module(Mod) ->
  Funs = Mod:module_info(functions),
  lists:foreach(fun ({F,A}) -> catch hipe_bifs:call_count_on({Mod,F,A}) end,
		Funs),
  ok.

%% --------------------------------------------------------------------
-spec prof_module_off(atom()) -> 'ok'.
%% @doc	 Turns off profiling of the module Mod. 
%@ --------------------------------------------------------------------

prof_module_off(Mod) ->
  Funs = Mod:module_info(functions),
  lists:foreach(fun ({F,A}) -> catch hipe_bifs:call_count_off({Mod,F,A}) end,
		Funs),
  ok.

%% --------------------------------------------------------------------
-spec clear_module(atom()) -> 'ok'.
%% @doc  Clears the call counters for all functions in module Mod.
%@ --------------------------------------------------------------------

clear_module(Mod) ->
  Funs = Mod:module_info(functions),
  lists:foreach(fun ({F,A}) -> catch hipe_bifs:call_count_clear({Mod,F,A}) end,
		Funs),
  ok.

%% --------------------------------------------------------------------
-spec res_module(atom()) -> [{mfa(), non_neg_integer()}].
%% @doc	  Returns the number of profiled calls to each function (MFA) 
%%        in the module Mod.
%@ --------------------------------------------------------------------

res_module(Mod) ->
  Fun = fun ({F,A}) when is_atom(F), is_integer(A) ->
	    MFA = {Mod,F,A},
	    {MFA, try hipe_bifs:call_count_get(MFA) of 
		    N when is_integer(N) -> N; 
		    false -> 0
		  catch
		    _:_ -> 0
		  end
	    }
	end,
  lists:reverse(lists:keysort(2, [Fun(FA) || FA <- Mod:module_info(functions)])).

-spec total_calls(atom()) -> non_neg_integer().

total_calls(Mod) ->
  Funs = Mod:module_info(functions),
  SumF = fun ({F,A}, Acc) -> 
	    MFA = {Mod,F,A},
	    try hipe_bifs:call_count_get(MFA) of 
	      N when is_integer(N) -> N+Acc; 
	      false -> Acc
	    catch
	      _:_ -> Acc
	    end;
	     (_, Acc) -> Acc
	end,
  lists:foldl(SumF, 0, Funs).