有仪器 API 吗?

nal*_*all 5 xcode cocoa objective-c instruments

是否可以设置从我的代码以编程方式运行的仪器?例如,我想构造这样的代码,其中startTrace可能为当前线程设置特定的探测器并开始记录,同时stopTrace会停止记录。我将使用 Instruments API 编写这些例程的内容,这是这个问题的主题。

-(void)myInterestingMethod
{
    [self startTrace];

    // do something interesting and performance critical

    [self stopTrace];
}
Run Code Online (Sandbox Code Playgroud)

如果上述内容不可用,设置我自己的 DTrace 探针是否是一个可行的替代方案?

ren*_*sch 5

看起来没有什么直接的东西,但有一个instruments命令行工具。下面是一些快速+脏代码,将调用它并示例调用进程的 CPU 使用情况

static void sampleMe() {
    // instruments -t '/Developer/Applications/Instruments.app/Contents/Resources/templates/CPU Sampler.tracetemplate' -p 26838 -l 5000

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/usr/bin/instruments"];
    [task setArguments:[NSArray arrayWithObjects:
                        @"-t",
                        @"/Developer/Applications/Instruments.app/Contents/Resources/templates/CPU Sampler.tracetemplate",
                        @"-p",
                        [NSString stringWithFormat:@"%ld", getpid()],
                        @"-l",
                        @"5000",
                        nil]];
    [task setCurrentDirectoryPath:NSHomeDirectory()];
    [task setStandardInput:[NSPipe pipe]];
    [task setStandardOutput:[NSPipe pipe]];
    [task setStandardError:[NSPipe pipe]];
    [task launch];
    // purposely leak everything since I can't be bothered to figure out lifetimes
}
Run Code Online (Sandbox Code Playgroud)

instrumentscli0.trace调用后,您的主目录中将出现一个名为的文件。

更新: Instruments 4.0在 iOS 应用程序的 DTPerformanceSession 中提供了DTSendSignalFlag 。