How to start / stop Tools (Time Profiler) programmatically?

Is there a way to start / stop profile programming? I need to profile only a specific section of my OS X code in a reliable way, but I cannot find the documentation for the tools that could tell me how I can do this. With CHUD / Shark, there was a programming API and command line tool to support this, but I don't see the equivalent for tools anywhere? FWIW I found several old forum posts from around 2009, fearing the lack of functionality of tools in this area, but nothing more recent.

+1
profiling xcode instruments macos xcode-instruments
source share
1 answer

Yes. Find the DTPerformanceSession. This was presented using tools 4.0 . This has been improved in Tools 4.1 .

These documents contain this sample code:

CFStringRef process = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%d"), getpid()); CFErrorRef error = NULL; DTPerformanceSessionRef session = DTPerformanceSessionCreate(NULL, process, NULL, &error); DTPerformanceSessionAddInstrument(session, (CFStringRef)@DTPerformanceSession_TimeProfiler, NULL, NULL, &error); CFMutableArrayRef instrumentIDs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); CFArrayAppendValue(instrumentIDs, @DTPerformanceSession_TimeProfiler); DTPerformanceSessionStart(session, instrumentIDs, &error); // do something in your app DTPerformanceSessionStop(session, instrumentIDs, &error); DTPerformanceSessionSave(session, (CFStringRef)@"/tmp/myAppProfile", &error); DTPerformanceSessionDispose(session, &error); 
+4
source share

All Articles