profile.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Pierre Muller,
  5. member of the Free Pascal development team.
  6. Profiling support for Go32V2
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************
  13. }
  14. {$S- do not use stackcheck here .. PM }
  15. {$ifdef FPC_PROFILE}
  16. {$error }
  17. {$message you can not compile profile unit with profiling}
  18. {$endif FPC_PROFILE}
  19. Unit profile;
  20. interface
  21. type
  22. header = record
  23. low,high,nbytes : longint;
  24. end;
  25. { entry of a GPROF type file }
  26. ppMTABE = ^pMTABE;
  27. pMTABE = ^MTABE;
  28. MTABE = record
  29. from,_to,count : longint;
  30. end;
  31. { internal form - sizeof(MTAB) is 4096 for efficiency }
  32. PMTAB = ^M_TAB;
  33. M_TAB = record
  34. calls : array [0..340] of MTABE;
  35. prev : PMTAB;
  36. end;
  37. const
  38. mcount_skip : longint = 1;
  39. mtab : PMTAB = nil;
  40. var
  41. h : header;
  42. histogram : ^integer;
  43. histlen : longint;
  44. oldexitproc : pointer;
  45. { called by functions. Use the pointer it provides to cache the last used
  46. MTABE, so that repeated calls to/from the same pair works quickly -
  47. no lookup. }
  48. procedure mcount;
  49. implementation
  50. {$asmmode ATT}
  51. uses
  52. go32,dpmiexcp;
  53. type
  54. plongint = ^longint;
  55. const
  56. cache : pMTABE = nil;
  57. var
  58. djgpp_old_timer : tseginfo;external name '___djgpp_old_timer';
  59. start : longint;external name 'start';
  60. _etext : longint;external name '_etext';
  61. starttext : longint;
  62. endtext : longint;
  63. procedure djgpp_timer_hdlr;external name '___djgpp_timer_hdlr';
  64. procedure sbrk_getmem(var p : pointer;size : longint);
  65. begin
  66. system.getmem(p,size);
  67. end;
  68. { problem how to avoid mcount calling itself !! }
  69. procedure mcount; [public, alias : 'MCOUNT'];
  70. {
  71. ebp contains the frame of mcount (ebp) the frame of calling (to_)
  72. ((ebp)) the frame of from
  73. }
  74. var
  75. m : pmtab;
  76. i,to_,ebp,from,mtabi : longint;
  77. begin
  78. { optimisation !! }
  79. asm
  80. pushal
  81. movl 4(%ebp),%eax
  82. movl %eax,to_
  83. movl (%ebp),%eax
  84. movl 4(%eax),%eax
  85. movl %eax,from
  86. end;
  87. if endtext=0 then
  88. asm
  89. popal
  90. leave
  91. ret
  92. end;
  93. mcount_skip := 1;
  94. if (to_ > endtext) or (from > endtext) then
  95. runerror(255);
  96. if ((cache<>nil) and (cache^.from=from) and (cache^._to=to_)) then
  97. begin
  98. { cache paid off - works quickly }
  99. inc(cache^.count);
  100. mcount_skip:=0;
  101. asm
  102. popal
  103. leave
  104. ret
  105. end;
  106. end;
  107. { no cache hit - search all mtab tables for a match, or an empty slot }
  108. mtabi := -1;
  109. m:=mtab;
  110. while m<>nil do
  111. begin
  112. for i:=0 to 340 do
  113. begin
  114. if m^.calls[i].from=0 then
  115. begin
  116. { empty slot - end of table }
  117. mtabi := i;
  118. break;
  119. end;
  120. if ((m^.calls[i].from = from) and (m^.calls[i]._to = to_)) then
  121. begin
  122. { found a match - bump count and return }
  123. inc(m^.calls[i].count);
  124. cache:=@(m^.calls[i]);
  125. mcount_skip:=0;
  126. asm
  127. popal
  128. leave
  129. ret
  130. end;
  131. end;
  132. end;
  133. m:=m^.prev;
  134. end;
  135. if (mtabi<>-1) then
  136. begin
  137. { found an empty - fill it in }
  138. mtab^.calls[mtabi].from := from;
  139. mtab^.calls[mtabi]._to := to_;
  140. mtab^.calls[mtabi].count := 1;
  141. cache := @(mtab^.calls[mtabi]);
  142. mcount_skip := 0;
  143. asm
  144. popal
  145. leave
  146. ret
  147. end;
  148. end;
  149. { lob off another page of memory and initialize the new table }
  150. { problem here : getmem is not reentrant yet !! PM }
  151. { lets hope that a direct call to sbrk correct this }
  152. sbrk_getmem(m,sizeof(M_TAB));
  153. fillchar(m^, sizeof(M_TAB),#0);
  154. m^.prev := mtab;
  155. mtab := m;
  156. m^.calls[0].from := from;
  157. m^.calls[0]._to := to_;
  158. m^.calls[0].count := 1;
  159. cache := @(m^.calls[0]);
  160. mcount_skip := 0;
  161. asm
  162. popal
  163. leave
  164. ret
  165. end;
  166. end;
  167. var
  168. new_timer,
  169. old_timer : tseginfo;
  170. invalid_mcount_call,
  171. mcount_nb,
  172. doublecall,
  173. reload : longint; {=0}
  174. function mcount_tick(x : longint) : longint;
  175. var
  176. bin : longint;
  177. begin
  178. if mcount_skip=0 then
  179. begin
  180. bin := djgpp_exception_state^.__eip;
  181. if (djgpp_exception_state^.__cs=get_cs) and (bin >= starttext) and (bin <= endtext) then
  182. begin
  183. bin := (bin - starttext) div 16;
  184. inc(histogram[bin]);
  185. end
  186. else
  187. inc(invalid_mcount_call);
  188. inc(mcount_nb);
  189. end
  190. else
  191. inc(doublecall);
  192. mcount_tick:=0;
  193. end;
  194. var
  195. ___djgpp_timer_countdown:longint;external name '___djgpp_timer_countdown';
  196. function timer(x : longint) : longint;
  197. begin
  198. if reload>0 then
  199. ___djgpp_timer_countdown:=RELOAD;
  200. timer:=mcount_tick(x);
  201. { _raise(SIGPROF); }
  202. end;
  203. procedure mcount_write;
  204. {
  205. this is called during program exit
  206. }
  207. var
  208. m : PMTAB;
  209. i : longint;
  210. f : file;
  211. begin
  212. mcount_skip:=1;
  213. signal(SIGTIMR,@SIG_IGN);
  214. signal(SIGPROF,@SIG_IGN);
  215. set_pm_interrupt($8,old_timer);
  216. reload:=0;
  217. exitproc:=oldexitproc;
  218. writeln(stderr,'Writing profile output');
  219. writeln(stderr,'histogram length = ',histlen);
  220. writeln(stderr,'Nb of double calls = ',doublecall);
  221. if invalid_mcount_call>0 then
  222. writeln(stderr,'nb of invalid mcount : ',invalid_mcount_call,'/',mcount_nb)
  223. else
  224. writeln(stderr,'nb of mcount : ',mcount_nb);
  225. assign(f,'gmon.out');
  226. rewrite(f,1);
  227. blockwrite(f, h, sizeof(header));
  228. blockwrite(f, histogram^, histlen);
  229. m:=mtab;
  230. while m<>nil do
  231. begin
  232. for i:=0 to 340 do
  233. begin
  234. if (m^.calls[i].from = 0) then
  235. break;
  236. blockwrite(f, m^.calls[i],sizeof(MTABE));
  237. {$ifdef DEBUG}
  238. if m^.calls[i].count>0 then
  239. writeln(stderr,' 0x',hexstr(m^.calls[i]._to,8),' called from ',hexstr(m^.calls[i].from,8),
  240. ' ',m^.calls[i].count,' times');
  241. {$endif DEBUG}
  242. end;
  243. m:=m^.prev;
  244. end;
  245. close(f);
  246. end;
  247. procedure mcount_init;
  248. {
  249. this is called to initialize profiling before the program starts
  250. }
  251. procedure set_old_timer_handler;
  252. begin
  253. djgpp_old_timer:=Old_Timer;
  254. end;
  255. begin
  256. starttext:=longint(@start);
  257. endtext:=longint(@_etext);
  258. h.low := starttext;
  259. h.high := endtext;
  260. histlen := ((h.high-h.low) div 16) * 2; { must be even }
  261. h.nbytes := sizeof(header) + histlen;
  262. getmem(histogram,histlen);
  263. fillchar(histogram^, histlen,#0);
  264. oldexitproc:=exitproc;
  265. exitproc:=@mcount_write;
  266. { here, do whatever it takes to initialize the timer interrupt }
  267. signal(SIGPROF,@mcount_tick);
  268. signal(SIGTIMR,@timer);
  269. get_pm_interrupt($8,old_timer);
  270. set_old_timer_handler;
  271. {$ifdef DEBUG}
  272. writeln(stderr,'ori pm int8 '+hexstr(old_timer.segment,4)+':'+hexstr(longint(old_timer.offset),8));
  273. flush(stderr);
  274. {$endif DEBUG}
  275. new_timer.segment:=get_cs;
  276. new_timer.offset:=@djgpp_timer_hdlr;
  277. reload:=3;
  278. {$ifdef DEBUG}
  279. writeln(stderr,'new pm int8 '+hexstr(new_timer.segment,4)+':'+hexstr(longint(new_timer.offset),8));
  280. flush(stderr);
  281. {$endif DEBUG}
  282. set_pm_interrupt($8,new_timer);
  283. reload:=1;
  284. ___djgpp_timer_countdown:=RELOAD;
  285. mcount_skip := 0;
  286. end;
  287. begin
  288. mcount_init;
  289. end.
  290. {
  291. $Log$
  292. Revision 1.2 2000-07-13 11:33:40 michael
  293. + removed logs
  294. }