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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Pierre Muller,
member of the Free Pascal development team.
Profiling support for Go32V2
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************
}
{$S- do not use stackcheck here .. PM }
{$ifdef FPC_PROFILE}
{$error }
{$message you can not compile profile unit with profiling}
{$endif FPC_PROFILE}
Unit profile;
interface
type
header = record
low,high,nbytes : longint;
end;
{ entry of a GPROF type file }
ppMTABE = ^pMTABE;
pMTABE = ^MTABE;
MTABE = record
from,_to,count : longint;
end;
{ internal form - sizeof(MTAB) is 4096 for efficiency }
PMTAB = ^M_TAB;
M_TAB = record
calls : array [0..340] of MTABE;
prev : PMTAB;
end;
const
mcount_skip : longint = 1;
mtab : PMTAB = nil;
var
h : header;
histogram : ^integer;
histlen : longint;
oldexitproc : pointer;
{ called by functions. Use the pointer it provides to cache the last used
MTABE, so that repeated calls to/from the same pair works quickly -
no lookup. }
procedure mcount;
implementation
{$asmmode ATT}
uses
go32,dpmiexcp;
type
plongint = ^longint;
const
cache : pMTABE = nil;
var
djgpp_old_timer : tseginfo;external name '___djgpp_old_timer';
start : longint;external name 'start';
_etext : longint;external name '_etext';
starttext : longint;
endtext : longint;
procedure djgpp_timer_hdlr;external name '___djgpp_timer_hdlr';
procedure sbrk_getmem(var p : pointer;size : longint);
begin
system.getmem(p,size);
end;
{ problem how to avoid mcount calling itself !! }
procedure mcount; [public, alias : 'MCOUNT'];
{
ebp contains the frame of mcount (ebp) the frame of calling (to_)
((ebp)) the frame of from
}
var
m : pmtab;
i,to_,ebp,from,mtabi : longint;
begin
{ optimisation !! }
asm
pushal
movl 4(%ebp),%eax
movl %eax,to_
movl (%ebp),%eax
movl 4(%eax),%eax
movl %eax,from
end;
if endtext=0 then
asm
popal
leave
ret
end;
mcount_skip := 1;
if (to_ > endtext) or (from > endtext) then
runerror(255);
if ((cache<>nil) and (cache^.from=from) and (cache^._to=to_)) then
begin
{ cache paid off - works quickly }
inc(cache^.count);
mcount_skip:=0;
asm
popal
leave
ret
end;
end;
{ no cache hit - search all mtab tables for a match, or an empty slot }
mtabi := -1;
m:=mtab;
while m<>nil do
begin
for i:=0 to 340 do
begin
if m^.calls[i].from=0 then
begin
{ empty slot - end of table }
mtabi := i;
break;
end;
if ((m^.calls[i].from = from) and (m^.calls[i]._to = to_)) then
begin
{ found a match - bump count and return }
inc(m^.calls[i].count);
cache:=@(m^.calls[i]);
mcount_skip:=0;
asm
popal
leave
ret
end;
end;
end;
m:=m^.prev;
end;
if (mtabi<>-1) then
begin
{ found an empty - fill it in }
mtab^.calls[mtabi].from := from;
mtab^.calls[mtabi]._to := to_;
mtab^.calls[mtabi].count := 1;
cache := @(mtab^.calls[mtabi]);
mcount_skip := 0;
asm
popal
leave
ret
end;
end;
{ lob off another page of memory and initialize the new table }
{ problem here : getmem is not reentrant yet !! PM }
{ lets hope that a direct call to sbrk correct this }
sbrk_getmem(m,sizeof(M_TAB));
fillchar(m^, sizeof(M_TAB),#0);
m^.prev := mtab;
mtab := m;
m^.calls[0].from := from;
m^.calls[0]._to := to_;
m^.calls[0].count := 1;
cache := @(m^.calls[0]);
mcount_skip := 0;
asm
popal
leave
ret
end;
end;
var
new_timer,
old_timer : tseginfo;
invalid_mcount_call,
mcount_nb,
doublecall,
reload : longint; {=0}
function mcount_tick(x : longint) : longint;cdecl;
var
bin : longint;
begin
if mcount_skip=0 then
begin
bin := djgpp_exception_state^.__eip;
if (djgpp_exception_state^.__cs=get_cs) and (bin >= starttext) and (bin <= endtext) then
begin
bin := (bin - starttext) div 16;
inc(histogram[bin]);
end
else
inc(invalid_mcount_call);
inc(mcount_nb);
end
else
inc(doublecall);
mcount_tick:=0;
end;
var
___djgpp_timer_countdown:longint;external name '___djgpp_timer_countdown';
function timer(x : longint) : longint;cdecl;
begin
if reload>0 then
___djgpp_timer_countdown:=RELOAD;
timer:=mcount_tick(x);
{ _raise(SIGPROF); }
end;
procedure mcount_write;
{
this is called during program exit
}
var
m : PMTAB;
i : longint;
f : file;
begin
mcount_skip:=1;
signal(SIGTIMR,@SIG_IGN);
signal(SIGPROF,@SIG_IGN);
set_pm_interrupt($8,old_timer);
reload:=0;
exitproc:=oldexitproc;
writeln(stderr,'Writing profile output');
writeln(stderr,'histogram length = ',histlen);
writeln(stderr,'Nb of double calls = ',doublecall);
if invalid_mcount_call>0 then
writeln(stderr,'nb of invalid mcount : ',invalid_mcount_call,'/',mcount_nb)
else
writeln(stderr,'nb of mcount : ',mcount_nb);
assign(f,'gmon.out');
rewrite(f,1);
blockwrite(f, h, sizeof(header));
blockwrite(f, histogram^, histlen);
m:=mtab;
while m<>nil do
begin
for i:=0 to 340 do
begin
if (m^.calls[i].from = 0) then
break;
blockwrite(f, m^.calls[i],sizeof(MTABE));
{$ifdef DEBUG}
if m^.calls[i].count>0 then
writeln(stderr,' 0x',hexstr(m^.calls[i]._to,8),' called from ',hexstr(m^.calls[i].from,8),
' ',m^.calls[i].count,' times');
{$endif DEBUG}
end;
m:=m^.prev;
end;
close(f);
end;
procedure mcount_init;
{
this is called to initialize profiling before the program starts
}
procedure set_old_timer_handler;
begin
djgpp_old_timer:=Old_Timer;
end;
begin
starttext:=longint(@start);
endtext:=longint(@_etext);
h.low := starttext;
h.high := endtext;
histlen := ((h.high-h.low) div 16) * 2; { must be even }
h.nbytes := sizeof(header) + histlen;
getmem(histogram,histlen);
fillchar(histogram^, histlen,#0);
oldexitproc:=exitproc;
exitproc:=@mcount_write;
{ here, do whatever it takes to initialize the timer interrupt }
signal(SIGPROF,@mcount_tick);
signal(SIGTIMR,@timer);
get_pm_interrupt($8,old_timer);
set_old_timer_handler;
{$ifdef DEBUG}
writeln(stderr,'ori pm int8 '+hexstr(old_timer.segment,4)+':'+hexstr(longint(old_timer.offset),8));
flush(stderr);
{$endif DEBUG}
new_timer.segment:=get_cs;
new_timer.offset:=@djgpp_timer_hdlr;
reload:=3;
{$ifdef DEBUG}
writeln(stderr,'new pm int8 '+hexstr(new_timer.segment,4)+':'+hexstr(longint(new_timer.offset),8));
flush(stderr);
{$endif DEBUG}
set_pm_interrupt($8,new_timer);
reload:=1;
___djgpp_timer_countdown:=RELOAD;
mcount_skip := 0;
end;
begin
mcount_init;
end.
|