如何以编程方式启动/停止仪器(Time Profiler)? [英] How to start/stop Instruments (Time Profiler) programatically?

查看:91
本文介绍了如何以编程方式启动/停止仪器(Time Profiler)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以以编程方式启动/停止仪器配置文件?我只需要以可靠的方式来分析OS X代码的特定部分,但似乎找不到关于Instruments的任何文档,该文档可能会告诉我如何执行此操作.有了CHUD/Shark,有一个编程API和一个命令行工具来支持此功能,但是我在任何地方都看不到Instruments的等效功能吗? FWIW,我发现2009年左右的一些老论坛帖子都在抱怨这方面缺少Instruments功能,但最近没有.

Is there any way to start/stop Instruments profiling programmatically ? I need to profile just a specific section of my OS X code in a reliable way but I can't seem to find any documentation for Instruments which might tell me how I might do this. With CHUD/Shark there was a programming API and a command line tool to support this but I don't see the equivalent for Instruments anywhere ? FWIW I found some old forum posts from around 2009 bemoaning the lack of Instruments functionality in this area but nothing more recent.

推荐答案

是.寻找DTPerformanceSession. 仪器4.0引入.这是在Instruments 4.1中得到了增强.

Yes. Look for DTPerformanceSession. It was introduced with Instruments 4.0. It was enhanced in Instruments 4.1.

这些文档提供了以下示例代码:

Those documents provide 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);

这篇关于如何以编程方式启动/停止仪器(Time Profiler)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆