profile.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. {$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. uses
  51. go32,dpmiexcp;
  52. type
  53. plongint = ^longint;
  54. var
  55. starttext, endtext : longint;
  56. const
  57. cache : pMTABE = nil;
  58. (*
  59. {$ASMMODE DIRECT}
  60. procedure sbrk_getmem(var p : pointer;size : longint);assembler;
  61. asm
  62. movl size,%eax
  63. pushl %eax
  64. call ___sbrk
  65. addl $4,%esp
  66. movl %eax,p
  67. end;
  68. this nice piece of code make serious problems !!! PM *)
  69. procedure sbrk_getmem(var p : pointer;size : longint);
  70. begin
  71. system.getmem(p,size);
  72. end;
  73. {$ASMMODE ATT}
  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;
  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. {$ASMMODE DIRECT}
  201. function timer(x : longint) : longint;
  202. begin
  203. if reload>0 then
  204. asm
  205. movl _RELOAD,%eax
  206. movl %eax,___djgpp_timer_countdown
  207. end;
  208. timer:=mcount_tick(x);
  209. { _raise(SIGPROF); }
  210. end;
  211. procedure mcount_write;
  212. {
  213. this is called during program exit
  214. }
  215. var
  216. m : PMTAB;
  217. i : longint;
  218. f : file;
  219. begin
  220. mcount_skip:=1;
  221. signal(SIGTIMR,@SIG_IGN);
  222. signal(SIGPROF,@SIG_IGN);
  223. set_pm_interrupt($8,old_timer);
  224. reload:=0;
  225. exitproc:=oldexitproc;
  226. writeln(stderr,'Writing profile output');
  227. writeln(stderr,'histogram length = ',histlen);
  228. writeln(stderr,'Nb of double calls = ',doublecall);
  229. if invalid_mcount_call>0 then
  230. writeln(stderr,'nb of invalid mcount : ',invalid_mcount_call,'/',mcount_nb)
  231. else
  232. writeln(stderr,'nb of mcount : ',mcount_nb);
  233. assign(f,'gmon.out');
  234. rewrite(f,1);
  235. blockwrite(f, h, sizeof(header));
  236. blockwrite(f, histogram^, histlen);
  237. m:=mtab;
  238. while m<>nil do
  239. begin
  240. for i:=0 to 340 do
  241. begin
  242. if (m^.calls[i].from = 0) then
  243. break;
  244. blockwrite(f, m^.calls[i],sizeof(MTABE));
  245. {$ifdef DEBUG}
  246. if m^.calls[i].count>0 then
  247. writeln(stderr,' 0x',hexstr(m^.calls[i]._to,8),' called from ',hexstr(m^.calls[i].from,8),
  248. ' ',m^.calls[i].count,' times');
  249. {$endif DEBUG}
  250. end;
  251. m:=m^.prev;
  252. end;
  253. close(f);
  254. end;
  255. procedure mcount_init;
  256. {
  257. this is called to initialize profiling before the program starts
  258. }
  259. function djgpp_timer_hdlr : pointer;
  260. begin
  261. asm
  262. movl $___djgpp_timer_hdlr,%eax
  263. movl %eax,__RESULT
  264. end;
  265. end;
  266. procedure set_old_timer_handler;
  267. begin
  268. asm
  269. movl $_OLD_TIMER,%eax
  270. movl $___djgpp_old_timer,%ebx
  271. movl (%eax),%ecx
  272. movl %ecx,(%ebx)
  273. movw 4(%eax),%ax
  274. movw %ax,4(%ebx)
  275. end;
  276. end;
  277. begin
  278. asm
  279. movl $_etext,_ENDTEXT
  280. movl $start,_STARTTEXT
  281. end;
  282. h.low := starttext;
  283. h.high := endtext;
  284. histlen := ((h.high-h.low) div 16) * 2; { must be even }
  285. h.nbytes := sizeof(header) + histlen;
  286. getmem(histogram,histlen);
  287. fillchar(histogram^, histlen,#0);
  288. oldexitproc:=exitproc;
  289. exitproc:=@mcount_write;
  290. { here, do whatever it takes to initialize the timer interrupt }
  291. signal(SIGPROF,@mcount_tick);
  292. signal(SIGTIMR,@timer);
  293. get_pm_interrupt($8,old_timer);
  294. set_old_timer_handler;
  295. {$ifdef DEBUG}
  296. writeln(stderr,'ori pm int8 '+hexstr(old_timer.segment,4)+':'+hexstr(longint(old_timer.offset),8));
  297. flush(stderr);
  298. {$endif DEBUG}
  299. new_timer.segment:=get_cs;
  300. new_timer.offset:=djgpp_timer_hdlr;
  301. reload:=3;
  302. {$ifdef DEBUG}
  303. writeln(stderr,'new pm int8 '+hexstr(new_timer.segment,4)+':'+hexstr(longint(new_timer.offset),8));
  304. flush(stderr);
  305. {$endif DEBUG}
  306. set_pm_interrupt($8,new_timer);
  307. reload:=1;
  308. asm
  309. movl _RELOAD,%eax
  310. movl %eax,___djgpp_timer_countdown
  311. end;
  312. mcount_skip := 0;
  313. end;
  314. {$ASMMODE ATT}
  315. begin
  316. mcount_init;
  317. end.
  318. {
  319. $Log$
  320. Revision 1.4 1998-11-18 09:22:10 pierre
  321. + added $error if compiled with -pg
  322. + all output to stderr
  323. Revision 1.3 1998/11/17 09:43:22 pierre
  324. + No stack check
  325. Revision 1.2 1998/05/31 14:18:28 peter
  326. * force att or direct assembling
  327. * cleanup of some files
  328. }