aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/src/dialyzer_timing.erl
blob: d5e6d1476dc1379f75030f26fbed1ddc483263b7 (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
%% -*- erlang-indent-level: 2 -*-
%%-------------------------------------------------------------------
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2006-2012. 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%
%%

%%%-------------------------------------------------------------------
%%% File    : dialyzer_timing.erl
%%% Authors : Stavros Aronis <[email protected]>
%%% Description : Timing reports for Dialyzer
%%%-------------------------------------------------------------------

-module(dialyzer_timing).

-export([init/1, start_stamp/1, send_size_info/2, end_stamp/0, stop/0]).

-spec init(boolean()) -> ok.

init(Active) ->
  Pid = spawn_link(fun() -> loop_init(Active) end),
  register(?MODULE, Pid),
  ok.

loop_init(Active) ->
  case Active of
    true ->
      io:format("\n"),
      loop(now(), 0, "");
    false -> dummy_loop()
  end.

dummy_loop() ->
  receive
    {Pid, stop, _Now} -> Pid ! ok;
    _ -> dummy_loop()
  end.

loop(LastNow, Size, Unit) ->
  receive
    {stamp, Msg, Now} ->
      io:format("    ~-10s (+~4.2fs):", [Msg, diff(Now, LastNow)]),
      loop(Now, 0, "");
    {stamp, Now} ->
      SizeStr =
	case Size of
	  0 -> "";
	  _ ->
	    Data = io_lib:format("~p ~s",[Size, Unit]),
	    io_lib:format(" (~12s)",[Data])
	end,
      io:format("~7.2fs~s\n", [diff(Now, LastNow), SizeStr]),
      loop(Now, 0, "");
    {size, NewSize, NewUnit} ->
      loop(LastNow, NewSize, NewUnit);
    {Pid, stop, Now} ->
      io:format("    ~-9s (+~5.2fs)\n", ["",diff(Now, LastNow)]),
      Pid ! ok;
    {Pid, stop} ->
      Pid ! ok
  end.

-spec start_stamp(string()) -> ok.

start_stamp(Msg) ->
  ?MODULE ! {stamp, Msg, now()},
  ok.

-spec end_stamp() -> ok.

end_stamp() ->
  ?MODULE ! {stamp, now()},
  ok.

-spec send_size_info(integer(), string()) -> ok.

send_size_info(Size, Unit) ->
  ?MODULE ! {size, Size, Unit},
  ok.

-spec stop() -> ok.

stop() ->
  ?MODULE ! {self(), stop, now()},
  receive ok -> ok end.

diff(T2, T1) ->
  timer:now_diff(T2,T1) / 1000000.