profile.pp 7.3 KB

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