profile.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_timer_hdlr : pointer;external name '___djgpp_timer_hdlr';
  59. djgpp_old_timer : tseginfo;external name '___djgpp_old_timer';
  60. endtext : longint;external name '_etext';
  61. starttext : longint;external name 'start';
  62. procedure sbrk_getmem(var p : pointer;size : longint);
  63. begin
  64. system.getmem(p,size);
  65. end;
  66. { problem how to avoid mcount calling itself !! }
  67. procedure mcount; [public, alias : 'MCOUNT'];
  68. {
  69. ebp contains the frame of mcount (ebp) the frame of calling (to_)
  70. ((ebp)) the frame of from
  71. }
  72. var
  73. m : pmtab;
  74. i,to_,ebp,from,mtabi : longint;
  75. begin
  76. { optimisation !! }
  77. asm
  78. pushal
  79. movl 4(%ebp),%eax
  80. movl %eax,to_
  81. movl (%ebp),%eax
  82. movl 4(%eax),%eax
  83. movl %eax,from
  84. end;
  85. if endtext=0 then
  86. asm
  87. popal
  88. leave
  89. ret
  90. end;
  91. mcount_skip := 1;
  92. if (to_ > endtext) or (from > endtext) then
  93. runerror(255);
  94. if ((cache<>nil) and (cache^.from=from) and (cache^._to=to_)) then
  95. begin
  96. { cache paid off - works quickly }
  97. inc(cache^.count);
  98. mcount_skip:=0;
  99. asm
  100. popal
  101. leave
  102. ret
  103. end;
  104. end;
  105. { no cache hit - search all mtab tables for a match, or an empty slot }
  106. mtabi := -1;
  107. m:=mtab;
  108. while m<>nil do
  109. begin
  110. for i:=0 to 340 do
  111. begin
  112. if m^.calls[i].from=0 then
  113. begin
  114. { empty slot - end of table }
  115. mtabi := i;
  116. break;
  117. end;
  118. if ((m^.calls[i].from = from) and (m^.calls[i]._to = to_)) then
  119. begin
  120. { found a match - bump count and return }
  121. inc(m^.calls[i].count);
  122. cache:=@(m^.calls[i]);
  123. mcount_skip:=0;
  124. asm
  125. popal
  126. leave
  127. ret
  128. end;
  129. end;
  130. end;
  131. m:=m^.prev;
  132. end;
  133. if (mtabi<>-1) then
  134. begin
  135. { found an empty - fill it in }
  136. mtab^.calls[mtabi].from := from;
  137. mtab^.calls[mtabi]._to := to_;
  138. mtab^.calls[mtabi].count := 1;
  139. cache := @(mtab^.calls[mtabi]);
  140. mcount_skip := 0;
  141. asm
  142. popal
  143. leave
  144. ret
  145. end;
  146. end;
  147. { lob off another page of memory and initialize the new table }
  148. { problem here : getmem is not reentrant yet !! PM }
  149. { lets hope that a direct call to sbrk correct this }
  150. sbrk_getmem(m,sizeof(M_TAB));
  151. fillchar(m^, sizeof(M_TAB),#0);
  152. m^.prev := mtab;
  153. mtab := m;
  154. m^.calls[0].from := from;
  155. m^.calls[0]._to := to_;
  156. m^.calls[0].count := 1;
  157. cache := @(m^.calls[0]);
  158. mcount_skip := 0;
  159. asm
  160. popal
  161. leave
  162. ret
  163. end;
  164. end;
  165. var
  166. new_timer,
  167. old_timer : tseginfo;
  168. invalid_mcount_call,
  169. mcount_nb,
  170. doublecall,
  171. reload : longint; {=0}
  172. function mcount_tick(x : longint) : longint;
  173. var
  174. bin : longint;
  175. begin
  176. if mcount_skip=0 then
  177. begin
  178. bin := djgpp_exception_state^.__eip;
  179. if (djgpp_exception_state^.__cs=get_cs) and (bin >= starttext) and (bin <= endtext) then
  180. begin
  181. bin := (bin - starttext) div 16;
  182. inc(histogram[bin]);
  183. end
  184. else
  185. inc(invalid_mcount_call);
  186. inc(mcount_nb);
  187. end
  188. else
  189. inc(doublecall);
  190. mcount_tick:=0;
  191. end;
  192. var
  193. ___djgpp_timer_countdown:longint;external name '___djgpp_timer_countdown';
  194. function timer(x : longint) : longint;
  195. begin
  196. if reload>0 then
  197. ___djgpp_timer_countdown:=RELOAD;
  198. timer:=mcount_tick(x);
  199. { _raise(SIGPROF); }
  200. end;
  201. procedure mcount_write;
  202. {
  203. this is called during program exit
  204. }
  205. var
  206. m : PMTAB;
  207. i : longint;
  208. f : file;
  209. begin
  210. mcount_skip:=1;
  211. signal(SIGTIMR,@SIG_IGN);
  212. signal(SIGPROF,@SIG_IGN);
  213. set_pm_interrupt($8,old_timer);
  214. reload:=0;
  215. exitproc:=oldexitproc;
  216. writeln(stderr,'Writing profile output');
  217. writeln(stderr,'histogram length = ',histlen);
  218. writeln(stderr,'Nb of double calls = ',doublecall);
  219. if invalid_mcount_call>0 then
  220. writeln(stderr,'nb of invalid mcount : ',invalid_mcount_call,'/',mcount_nb)
  221. else
  222. writeln(stderr,'nb of mcount : ',mcount_nb);
  223. assign(f,'gmon.out');
  224. rewrite(f,1);
  225. blockwrite(f, h, sizeof(header));
  226. blockwrite(f, histogram^, histlen);
  227. m:=mtab;
  228. while m<>nil do
  229. begin
  230. for i:=0 to 340 do
  231. begin
  232. if (m^.calls[i].from = 0) then
  233. break;
  234. blockwrite(f, m^.calls[i],sizeof(MTABE));
  235. {$ifdef DEBUG}
  236. if m^.calls[i].count>0 then
  237. writeln(stderr,' 0x',hexstr(m^.calls[i]._to,8),' called from ',hexstr(m^.calls[i].from,8),
  238. ' ',m^.calls[i].count,' times');
  239. {$endif DEBUG}
  240. end;
  241. m:=m^.prev;
  242. end;
  243. close(f);
  244. end;
  245. procedure mcount_init;
  246. {
  247. this is called to initialize profiling before the program starts
  248. }
  249. procedure set_old_timer_handler;
  250. begin
  251. djgpp_old_timer:=Old_Timer;
  252. end;
  253. begin
  254. h.low := starttext;
  255. h.high := endtext;
  256. histlen := ((h.high-h.low) div 16) * 2; { must be even }
  257. h.nbytes := sizeof(header) + histlen;
  258. getmem(histogram,histlen);
  259. fillchar(histogram^, histlen,#0);
  260. oldexitproc:=exitproc;
  261. exitproc:=@mcount_write;
  262. { here, do whatever it takes to initialize the timer interrupt }
  263. signal(SIGPROF,@mcount_tick);
  264. signal(SIGTIMR,@timer);
  265. get_pm_interrupt($8,old_timer);
  266. set_old_timer_handler;
  267. {$ifdef DEBUG}
  268. writeln(stderr,'ori pm int8 '+hexstr(old_timer.segment,4)+':'+hexstr(longint(old_timer.offset),8));
  269. flush(stderr);
  270. {$endif DEBUG}
  271. new_timer.segment:=get_cs;
  272. new_timer.offset:=djgpp_timer_hdlr;
  273. reload:=3;
  274. {$ifdef DEBUG}
  275. writeln(stderr,'new pm int8 '+hexstr(new_timer.segment,4)+':'+hexstr(longint(new_timer.offset),8));
  276. flush(stderr);
  277. {$endif DEBUG}
  278. set_pm_interrupt($8,new_timer);
  279. reload:=1;
  280. ___djgpp_timer_countdown:=RELOAD;
  281. mcount_skip := 0;
  282. end;
  283. begin
  284. mcount_init;
  285. end.
  286. {
  287. $Log$
  288. Revision 1.4 2000-01-07 16:41:32 daniel
  289. * copyright 2000
  290. Revision 1.3 2000/01/07 16:32:23 daniel
  291. * copyright 2000 added
  292. Revision 1.2 1999/03/02 13:56:35 peter
  293. * use ATT assembler in profile
  294. * use AS output in graph
  295. Revision 1.1 1998/12/21 13:07:03 peter
  296. * use -FE
  297. Revision 1.4 1998/11/18 09:22:10 pierre
  298. + added $error if compiled with -pg
  299. + all output to stderr
  300. Revision 1.3 1998/11/17 09:43:22 pierre
  301. + No stack check
  302. Revision 1.2 1998/05/31 14:18:28 peter
  303. * force att or direct assembling
  304. * cleanup of some files
  305. }