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
|
%% -*- erlang-indent-level: 2 -*-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2001 by Erik Johansson. All Rights Reserved
%% Time-stamp: <2008-04-20 14:53:42 richard>
%% ====================================================================
%% Filename : hipe_profile.erl
%% Module : hipe_profile
%% Purpose :
%% History : * 2001-07-12 Erik Johansson (happi@it.uu.se): Created.
%% CVS :
%% $Author: richardc $
%% $Date: 2008/04/20 13:01:14 $
%% $Revision: 1.9 $
%% ====================================================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-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/0 :: () -> [{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/0 :: () -> [{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/0 :: () -> 'ok').
%% @doc Turns on profiling of all loaded modules.
prof() ->
lists:foreach(fun prof_module/1, mods()).
-spec(prof_off/0 :: () -> 'ok').
%% @doc Turns off profiling of all loaded modules.
prof_off() ->
lists:foreach(fun prof_module_off/1, mods()).
-spec(clear/0 :: () -> 'ok').
%% @doc Clears all counters.
clear() ->
lists:foreach(fun clear_module/1, mods()).
-spec(res/0 :: () -> [{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(lists:map(fun res_module/1,
mods())))).
%% --------------------------------------------------------------------
-spec(mods/0 :: () -> [atom()]).
%% @doc Returns a list of all loaded modules.
%@ --------------------------------------------------------------------
mods() ->
[Mod || {Mod,_} <- code:all_loaded()].
%% --------------------------------------------------------------------
-spec(prof_module/1 :: (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/1 :: (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/1 :: (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/1 :: (atom()) -> [{mfa(), non_neg_integer()}]).
%% @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}) when is_atom(F), is_integer(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))).
-spec(total_calls/1 :: (atom()) -> non_neg_integer()).
total_calls(Mod) ->
Funs = Mod:module_info(functions),
SumF = 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(SumF, 0, Funs).
|