How to start and stop sampling (time profile) programmatically?

I have a long running process and I'm trying to profile a specific task. I'd like to be able to start Instruments collecting performance data when the task starts, then stop it again as soon as it is finished.


Googling, I found the DTPerformanceSession framework, which looked like exactly what I need, but it appears to be obsolete (added in Instruments 4 and no longer documented anywhere).


Is there something equivelent for Instruments 9?

Replies

Is the "instruments" command line an option? Or do you want control the UI that's currently recording?

I'm not picky (UI vs. comand-line vs. whatever).


I just want to turn on Instrument sampling at one point in my code and stop it when it gets to another.


Specifically, my first job involves a long running process (18 to 30 hours long), and I'm trying to improve the performance of one phase, which might execute one, two, or maybe three times during this process. Sampling the whole process is burdonsome and makes it really difficult to zoom in on the code I'm trying to tune. I've even had Instruments crash because the sample set got too big.

The "instruments" command line tool is available, if you can shell out to it. Also, there is a macOS service for starting and stopping the active trace document called "Toggle Instruments Recording". To invoke that, just call NSPerformService("Toggle Instruments Recording", pboard).


There is currently no way on a device to start/stop recording, but those two should work for the host.

Now that I know how to programmatically toggle recording, is there a way to select a target for system trace or use the front active one?