profile.pp 7.5 KB

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