Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Instruments API?

Is it possible to setup an Instruments run programmatically from my code? For instance, I'd like to structure my code something like this where startTrace might setup a specific probe for the current thread and start recording while stopTrace would stop recording. I would be writing the content of those routines using the Instruments API that is the subject of this question.

-(void)myInterestingMethod
{
    [self startTrace];

    // do something interesting and performance critical

    [self stopTrace];
}

If the above isn't available, is setting up my own DTrace probe a viable alternative?

like image 546
nall Avatar asked Dec 20 '10 01:12

nall


People also ask

What is API instrumentation?

API instrumentation identifies the time that elapsed during application activities. It is used for applications and products that use the IBM Spectrum Protect API. By default, instrumentation data is automatically collected by the API during backup or restore processing.

What is data instrumentation?

Instrumentation refers to the selection or development and the later use of tools to make observations about variables in a research study. The observations are collected, recorded, and used as primary data.


1 Answers

Doesn't look like there's anything straight-forward, but there is an instruments command-line tool. Here's some quick+dirty code that will invoke it and sample CPU usage for the calling process

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
}

After invocation a file named instrumentscli0.trace will be in your home directory.

Update: Instruments 4.0 offers DTSendSignalFlag in the DTPerformanceSession for iOS apps.

like image 159
rentzsch Avatar answered Oct 03 '22 14:10

rentzsch