[MLton] profiling question

Stephen Weeks MLton@mlton.org
Tue, 16 Mar 2004 19:36:17 -0800


> I decided to just use my own counters and create variables that I append
> into the program. The only thing I am not sure about is how to send my
> counters to stdout or a file.

It may be easier to create a single array on the C side with one
element for each coercion point, and have each coercion point
increment the appropriate array element.  You can allocate the array
with an FFI call that you insert somewhere near program startup.  You
can also make an FFI call at each coercion point to bump the counter,
by passing the array index to the C function.  Finally, you can add a
line to the runtime function GC_done that writes out the counts to a
file.  

You do need to be aware that with this approach, the inserted code may
substantially affect subsequent optimization.  If you want to avoid
that, you need to modify the profiling infrastructure.  To do that,
you could add a new profile variant to the Control structure, say,
ProfileFlatten.  You could then insert Profile Enter and Profile Leave
statements around the points you want to count, with source info that
somehow indicates the point in the SSA IL that you want to look at.
You'll then need to modify backend/profile.fun to handle this new
profile variant so that it inserts calls to C functions to increment
the profile counts, very much like is done with ProfileAlloc, but
presumably with increment of 1 rather than bytes allocated.

Once you've done that, you get all the profiling infrastructure
working for you.  That is, the runtime will allocate the array for the
counts, maintain the dynamic counts, and spit out the mlmon.out file.
You can also use mlprof to look at the data.