profiling

Henry Cejtin henry@sourcelight.com
Tue, 10 Apr 2001 00:46:05 -0500


It  seems to work perfectly.  I used some fancyness so that to turn profiling
on you just have to link it with prof.o.  Nothing  else  changes.   The  only
cost  of  this is that initSignalStack() has one extra test in it of a (link-
time) constant against 0.

The  change  to   gc.c   consists   of   the   following   new   version   of
initSignalStack():

	static void initSignalStack(GC_state s) {
		extern void	startProf(void)
					__attribute__((weak));
	        static stack_t altstack;
		size_t ss_size = roundPage(SIGSTKSZ);
		size_t psize = getpagesize();

		void *ss_sp = ssmmap(2 * ss_size, psize, psize);
		altstack.ss_sp = ss_sp + ss_size;
		altstack.ss_size = ss_size;
		altstack.ss_flags = 0;

		sigaltstack(&altstack, NULL);
		unless (startProf == NULL)
			startProf();
	}

The only change is the funny declaration of startProf() and the call to it if
it isn't NULL.

Next comes the new C file prof.c:


Finally,  the  Makefile  in  src/runtime  has  to be fixed to compile prof.o.
Note, this .o must NOT go in any of the libraries built.  It  ends  up  going
directly  in  /usr/local/lib/mlton/lib.   (It  is essentially the analogue of
/usr/lib/gcrt1.o.)

Now in the mlton executable, for the -p case, we still want  to  set  keepCps
true.   We only need to set keepGenerated to true if information still has to
be extracted from the C files, and I don't think that that needs  to  be  the
case  if  Matthew  encodes what he needs in the static symbol names.  Also we
shouldn't need to turn on -g if profile is true any more.  On the other hand,
if   profile   is   true   and   you   are   linking,   then   you  must  add
/usr/local/lib/mlton/lib/prof.o onto the list of things.

Note, at the moment there still doesn't seem to be any way to, for  instance,
keep the .c file that gets generated.  I often want to keep this, but NOT the
.s or .o that gets generated from that.

Matthew, if there seems to be something wrong with this, I'm open to changes.
Also if you don't want to write the thing that scans the resulting mlprof.out
file, tell me what symbols you are including in your generated code and I can
do that part.

One  thing  I should point out that I discovered the hard way: If the call to
sigaction does NOT specify the SA_ONSTACK flag, then even if you have  called
sigaltstack(),  it  will NOT switch stacks, so you will probably die.  Worse,
if the call to sigaction  DOES  have  SA_ONSTACK  and  you  ahve  NOT  called
sigaltstack(),  it  still  switches  stacks  (to location 0) and you die of a
SEGV.  Thus the sigaction() call MUST occur after the call to  sigaltstack(),
and  in  order  to  have  profiling cover as much as possible, you want it to
occur right after the sigaltstack() call.


begin 600 prof.c.tgz
M'XL(`**:TCH"`^U76V_;-A3.J_PK#CRLE3/'<;(L!=9V0!>T1;"V*=H,>^BV
MC)8HBZM$"B3EU-WZW_<=TO>Y0_>P/@PZ0&+S\/#<;VZL*4;9P7\*)^/Q^=D9
M'1#1V>DX?)XLS@SW3L_OA?]GWYQ\<_KU&+=CG`YH?/`9H'5>6**#4FH[_T<Z
M:=W!_PZ.#WMT2->E)*.K.<EW7EHM*GR=*:<FE:2BU9E71I/2Y$OEJ%#`*H=W
MR<RH/&$'^I?(HY2/`^9W6ZJLI'#AJ&VHP:W22D])Z)R$M4)/I:-TI@0)+]\I
MGPX&Y`W=6N6A2NLA2C(G*UU;@<DMXL,X9C6UHB9^Y$9$EYYJHY4WUFW=9Z;5
ML`6Y-&8^7M40V`#A9&98"?PIG5E92PW^0A,^[9RM9/WF9`IJE0XRKLN6S5WR
M=)0)K8VGQCCV$6AGTA:5N:7"6#K]]>M3.F;!"UF.@!4T43X0,J<3NGCY(\VE
ML",^7NE@3S`@N+>NV&4C=@0\#BWAI3QHL@P`&#OEH+DION50"#HYI<D<WJO%
M5&4T$U4K*>WWGS_S"!ZS^UGW.3H)2X&RTGD2>0X/0X*Q^&R@+`<)@6!U-1/_
MC@*A1KBH7*FFY:>]:_5;;6X1,Y6]=5$J7[EENOVV17"7S131P>QYEJ7;>H)X
M\2D$SY<B*K&(M\R9UVZ\;X4CCHV*Z1(RC7D$Q98>%%5M8$4FK1<*>1^#NS3%
ML2%19D@[!(:Y-@;I$'+8DRL%Y%.E)E8@:3*3RQ#(1U45/>\0T$PV/F0$*X+T
M]TI4*`H;>%A)+2(XU9+ST,LIYU5P731>>#7C6&:ETI*YU!";GH40NR$D>U_)
M(PG'"ST(LE\8+X><XJ5H7"Q5U@NZFK;*"0V,ZEH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