aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/util/hipe_timing.erl
blob: 191db497e269d129db3e2a14dc71dbd1a6816f3d (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
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-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%
%%
%%====================================================================
%% Note: Uses the process keys:
%%  hipe_time       - Indicates what to time.
%%  hipe_timers     - A stack of timers.
%%  {hipe_timer,T}  - Delata times for named timers. 
%%  T               - Acc times for all named timers T.
%%====================================================================

-module(hipe_timing).
-export([start/2, stop/2,
	 %% start_timer/0, stop_timer/1,
	 %% get_hipe_timer_val/1, set_hipe_timer_val/2,
	 %% start_hipe_timer/1, stop_hipe_timer/1,
	 start_optional_timer/2, stop_optional_timer/2]).

-include("../main/hipe.hrl").

%%=====================================================================

-spec start(string(), atom()) -> 'ok'.

start(Text, Mod) when is_atom(Mod) ->
  Timers = 
    case get(hipe_timers) of
      undefined -> [];
      Ts -> Ts
    end,
  Space = [$| || _ <- Timers],
  Total = start_timer(),
  put(hipe_timers, [Total|Timers]),
  ?msg("[@~7w]" ++ Space ++ "> ~s~n", [Total,Text]).

-spec stop(string(), atom()) -> 'ok'.

stop(Text, Mod) when is_atom(Mod) ->
  {Total,_Last} = erlang:statistics(runtime),
  case get(hipe_timers) of
    [StartTime|Timers] -> 
      Space = [$| || _ <- Timers],
      put(hipe_timers,Timers),
      ?msg("[@~7w]" ++ Space ++ "< ~s: ~w~n", [Total, Text, Total-StartTime]);
    _ ->
      put(hipe_timers, []),
      ?msg("[@~7w]< ~s: ~w~n", [Total, Text, Total])
  end.

-spec start_optional_timer(string(), atom()) -> 'ok'.

start_optional_timer(Text, Mod) ->
  case get(hipe_time) of 
    true -> start(Text, Mod);
    all -> start(Text, Mod);
    Mod -> start(Text, Mod);
    List when is_list(List) ->
      case lists:member(Mod, List) of
	true -> start(Text, Mod);
	false -> ok
      end;
    _ -> ok
  end.

-spec stop_optional_timer(string(), atom()) -> 'ok'.

stop_optional_timer(Text, Mod) ->
  case get(hipe_time) of
    true -> stop(Text, Mod);
    all -> stop(Text, Mod);
    Mod -> stop(Text, Mod);
    List when is_list(List) ->
      case lists:member(Mod, List) of
	true -> stop(Text, Mod);
	false -> ok
      end;
    _ -> ok
  end.

-spec start_timer() -> non_neg_integer().

start_timer() ->
  {Total, _Last} = erlang:statistics(runtime),
  Total.

%% stop_timer(T) ->
%%   {Total, _Last} = erlang:statistics(runtime),
%%   Total - T.
%% 
%% start_hipe_timer(Timer) ->
%%   Time = erlang:statistics(runtime),
%%   put({hipe_timer,Timer}, Time).
%% 
%% stop_hipe_timer(Timer) ->
%%   {T2, _} = erlang:statistics(runtime),
%%   T1 =
%%     case get({hipe_timer,Timer}) of
%%       {T0, _} -> T0;
%%       _ -> 0
%%     end,
%%   AccT = 
%%     case get(Timer) of
%%       T when is_integer(T) -> T;
%%       _ -> 0
%%     end,
%%   put(Timer,AccT+T2-T1).
%% 
%% get_hipe_timer_val(Timer) ->
%%   case get(Timer) of
%%     T when is_integer(T) -> T;
%%     _ -> 0
%%   end.
%% 
%% set_hipe_timer_val(Timer, Val) ->
%%   put(Timer, Val).