Daij-Djan
Daij-Djan

Reputation: 50089

Measure time a thread spends waiting for a Lock (locking by coredata)

We are using coredata in some threads... (too many concurrent contexts are bad - I know, I experience it)

Now around every fetch/save coredata wraps a lock.

I'd now like to measure the time one thread spends being blocked waiting to aquire that lock.


I thought I could just use time profiler or even thread state instrument or the sampler. but: - time profiler just ignores waiting (likely because it isnt a CPU call) - sampler as well (even though he isntin cpu mode) - the thread states instrument doesnt show me correct callstack either :( [but maybe (and that's always a possibility) I did overlook an easy solution]


here I have a very simple app that also has a lock and for which I also fail to get the wait time... maybe you can help me get the time the main thread spends waiting in this example -- using some profiling technique I can then transfer to the coredata case:

@implementation DDAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLock *l = [[NSLock alloc] init];
    NSLog(@"made");
    [NSThread detachNewThreadSelector:@selector(hogTheLog:) toTarget:self withObject:l];
    NSLog(@"wait");
    [l lock];
    NSLog(@"got");
    [l unlock];
    NSLog(@"terminate");
    [NSApp terminate:nil];
}

- (void)hogTheLog:(NSLock*)l {
    [l lock];
    sleep(3);
    [l unlock];
}
@end

Upvotes: 3

Views: 3125

Answers (2)

Roland
Roland

Reputation: 854

I'm not sure how long Xcode has this feature, I only noticed it in 5.1. In the "Time Profiler" instrument configuration there is now a checkbox "Record Waiting Threads". In this mode it records how much time exactly did the application spend on each method/function etc (instead of measuring just the CPU time).

enter image description here

I set sample interval to 20 ms, because otherwise overall will be too slow.

UPD: also, set "Sample Perspective" (in the bottom left corner on the screenshot) to "Running Sample Times" to discard samples when thread is just idle from the picture.

Upvotes: 8

James Bedford
James Bedford

Reputation: 28962

Use the "System Calls" instrument in Instruments. You get this instrument if you select the "System Trace" template when launching Instruments. This will give you all sorts of scheduling information which should allow you to find your answer.

Upvotes: 1

Related Questions