summaryrefslogtreecommitdiff
path: root/compiler/cprofile.pas
blob: 6835ba440ba86255ea1cd8fae11794257677465f (plain)
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
{
    Copyright (c) 2018 by Florian Klaempfl

    Basic infrastructure for measuring timings of different compilation steps

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit cprofile;

{ to use this profile intrastructure, the epiktimer sources must be available, the official repository is currently:

  https://github.com/graemeg/epiktimer.git

  clone it into the same base directory fpc is checked out/cloned, i.e.
  <base dir>/fpc/compiler
  <base dir>/epiktimer

  As the offical branch requires the use of the classes units, I recommend to
  use my modified version of epiktimer which allows to disable the use of the classes unit,
  this is done automatically by the compiler sources through a define. You can get my epiktimer source from

  https://github.com/FPK/epiktimer.git

  clone them into the same base directory fpc is checked out/cloned, i.e.
  <base dir>/fpc/compiler
  <base dir>/epiktimer

                                                                           (FK)
}
{ $define COMPILER_TIMINGS}

{$i fpcdefs.inc}

interface

  uses
    globals;

  type
    TCTimer = (
      ct_none,
      ct_aopt);

  procedure ResumeTimer(t : TCTimer);
  procedure StopTimer;

implementation

{$ifndef COMPILER_TIMINGS}
  procedure ResumeTimer(t : TCTimer);
    begin
    end;


  procedure StopTimer;
    begin
    end;
{$else COMPILER_TIMINGS}
  uses
    cepiktimer;

  var
    currenttimer : TCTimer;
    timers : array[TCTimer] of TEpikTimer;


  procedure ResumeTimer(t : TCTimer);
    begin
      timers[currenttimer].Stop;
      timers[t].Start;
      currenttimer:=t;
    end;


  procedure StopTimer;
    begin
      timers[currenttimer].Stop;
      currenttimer:=ct_none;
      timers[currenttimer].Start;
    end;

var
  t : TCTimer;

initialization
  for t:=low(timers) to high(timers) do
    timers[t]:=TEpikTimer.Create;
  currenttimer:=ct_none;
finalization
  writeln(StdErr,'Compiler profiler results:');
  timers[currenttimer].Stop;
  for t:=low(timers) to high(timers) do
    writeln(StdErr,'  ',t,' ',timers[t].Elapsed:0:9,' s');
  for t:=low(timers) to high(timers) do
    timers[t].Free;
{$endif COMPILER_TIMINGS}
end.