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
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2001 by Erik Johansson. All Rights Reserved
%% Time-stamp: <2002-09-13 15:32:45 richardc>
%% ====================================================================
%% Filename : hipe_profile.erl
%% Module : hipe_profile
%% Purpose :
%% History : * 2001-07-12 Erik Johansson (happi@csd.uu.se):
%% Created.
%% CVS :
%% $Author: kostis $
%% $Date: 2005/12/12 21:58:04 $
%% $Revision: 1.4 $
%% ====================================================================
%% Exports :
%%
%% @doc
%% Notes :
%@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-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() -> [{Mod,Calls}]
%% @doc Returns the number of calls per module currently
%% recordeed since hipe_bifs:call_count_on().
%% The resulting lists is sorted with the most called
%% module first.
mods_res() ->
lists:reverse(lists:keysort(2,calls())).
%% @spec () -> [{mod,calls}]
%% @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 () -> ok
%% @doc Turns on profiling of all loaded modules.
prof() ->
mods(fun prof_module/1),ok.
%% @spec () -> ok
%% @doc Turns off profiling of all loaded modules.
prof_off() ->
mods(fun prof_module_off/1),ok.
%% @spec () -> ok
%% @doc Clears all counters.
clear()->
mods(fun clear_module/1),ok.
%% @spec () -> [{mfa(),calls()}]
%% @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(mods(fun res_module/1)))).
%% ____________________________________________________________________
%% @spec mods(F) -> [T]
%% F = (mod()) -> T
%% T = term()
%% @doc Applies F to all loaded modules.
%@ ____________________________________________________________________
mods(F) ->
[F(X) || X <- mods()].
%% ____________________________________________________________________
%% @spec () -> [mod()]
%% @doc Returns a list of all loaded modules.
%@ ____________________________________________________________________
mods() ->
[element(1,X) || X <- code:all_loaded()].
%% ____________________________________________________________________
%% @spec (mod()) -> 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 (Mod) -> ok
%% Mod = mod()
%% @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 (Mod)->ok
%% Mod = mod()
%% @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 (Mod)->[{MFA,calls()}]
%% Mod = mod()
%% MFA = mfa()
%% @doc Returns the number of profiled calls to each function (MFA)
%% in the module Mod.
%@ ____________________________________________________________________
res_module(Mod) ->
Funs = Mod:module_info(functions),
lists:reverse(lists:keysort(2,lists:map(
fun ({F,A}) ->
MFA = {Mod,F,A},
{MFA,case catch hipe_bifs:call_count_get(MFA) of
N when is_integer(N) -> N;
_ -> 0 end
}
end,
Funs))).
total_calls(Mod) ->
Funs = Mod:module_info(functions),
Sum = fun({F,A},Acc) ->
MFA = {Mod,F,A},
case catch hipe_bifs:call_count_get(MFA) of
N when is_integer(N) -> N+Acc;
_ -> Acc
end;
(_,Acc) -> Acc
end,
lists:foldl(Sum,0,Funs).
|