I'm developing a plugin for a third-party host application on OSX, using C++. It is compiled as a .dylib. I wish to profile my plugin as it runs in the host application.
Unfortunately the host calls the plugin code at a rate that varies depending on the plugin's (last) execution time. This means that the process's overall time can vary considerably relative to real time. Therefore with a sampling profiler the 'time spent' within the plugin isn't really grounded to anything useful, as it's only compared to stack frames that fall within the process. If I improve the performance of the plugin, then the pattern of the host's execution of the plugin will change accordingly and it will be very difficult to measure improvements within the plugin.
I am able to use Instruments but as far as I can tell I can only get relative time against the process's CPU time.
I've used dtrace to obtain a user stack histogram of the host process:
#!/usr/sbin/dtrace -s
#pragma ustackframes 100
#pragma D option quiet
/* $1 is pid */
/* $2 is sample rate in Hz (e.g. 100) */
/* $3 is duration (e.g. '20s') */
profile-$2
/pid == $1 && arg1/
{
@[ustack()] = count();
}
tick-$3
{
exit(0);
}
This works, but it still only provides samples relative to the process time, as the predicate is only matched then the process is in user-space. Even removing the && arg1
condition to trigger it during the process's kernel calls doesn't really help.
What I really want to know is how many profile-n
samples resulted in the process not running at all. Then I can compare the number within my plugin against the total number of samples, and get absolute sample values for my plugin's functions. This makes me wonder - is it safe to assume that the requested profile-n
sample rate is honoured? Can I simply take time * sample rate and use that to calculate the 'off-process' time? I had assumed that at, say, 1500Hz, it was dropping samples and running at some other, unknown, rate, but if I can be sure it's sampling at 1500Hz then I can work out the 'off-process' time from that.
Alternatively, is there a known way to do wall-clock profiling with dtrace?