profile.pp 7.4 KB

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