Disk I/O Debugging

Fresh apps are usually able to startup in the time it takes for backboardd to animate from the icon to your first controller. But over the lifetime of an app, you can build up technical debt and slow things down.

One of the biggest offenders to slow startup, and slowness in general, is disk I/O. Even worse, doing I/O on the main thread.

With apps that have been around for a while, it can be daunting to be given a task to "make it faster". Here's a neat debugging tip I found that logs app-wide disk I/O and can help get you started.

Start by adding a symbolic breakpoint in Xcode. Do this before running your app.

Adding a Symbolic Breakpoint

In the Symbol field add -[NSData initWithContentsOfFile:]. This method is used by a lot of the disk I/O APIs that iOS developers end up using, so it should catch most of what you need.

You will want to print the first parameter of the method -initWithContentsOfFile:, the string path. Since this method is part of Foundation and already compiled, you can't just drop a breakpoint in the implementation.

Instead, you just need to print the first argument. You can find the parameter in the registers. The first two are self and _cmd, so you just need to print $esp+12 for 32 bit architectures or $rdx for 64 bit.

Add a Debugger Command action with the following command:

// 32 bit:
po *(int*)($esp+12)

// 64 bit
po $rdx

For more info on debugging and registers check out these articles by Ari Grant and Peter Steinberger.

In addition to the file path, it would also be great if we knew what thread the file was being accessed on. To do this, add another Debugger Command action with thread info.

Breakpoint Setup

You'll probably want to setup another symbolic breakpoint identical to the previous one except using the symbol:

-[NSData initWithContentsOfFile:options:error:]

Oddly enough this isn't the designated initializer, so you can't capture everything with just -initWithContentsOfFile:.

Try running your app. If you're running on an iOS 8 device or simulator, you should see a bunch of output when the app launches.

Using a small hobby app and some regex to cleanup my logs, I get the following:

Library/Application Support/iPhone Simulator/com.apple.iphonesimulator/extended_display.plist
thread #1

// what's this??
thread #1

thread #1

thread #1

thread #1

thread #1

thread #1

thread #1

thread #1

There is a lot of system disk I/O happening, and unfortunately all on the main thread. That's pretty much out of my control.

However, I can see that I'm reading the file Documents/latest.feed from the main thread (that's thread #1)! That's enough to give me a clue to track down where that file is read and maybe toss it off to a background queue.

If you want more detail, you could replace the thread info action with bt to print out a backtrace at each disk I/O event. That will output a ton of logs that you can then sift through.

If you wanted to go deeper, you could use tools like Facebook's chisel and fishhook. And if you wanted to get even lower, you could setup a breakpoint on fopen.

Hopefully this post helps track down pesky I/O issues. Happy hunting!

Visual Tests with Xcode Playgrounds

Recently I discovered a neat way to use Xcode 6 Playgrounds as a kind of unit test for a framework and walks through the problem I solved with it. You can jump directly to the project here if you'd like.

I've been tinkering around with a tiny library to do some math regarding aviation and flight paths. I'm more interested in creating a basic Air Traffic Control simulator app.

As a pilot, it's fun to learn more about the mechanics of aviation traffic.

I ran into an interesting problem when writing code to maneuver a plane between points in a 2-dimensional space.

Say you're flying near San Francisco at 120kts (in aviation, speed is in knots) with a heading of 360° (due North). Air Traffic Control asks you to make a standard rate turn towards the Palo Alto airport.

San Francisco Sectional

Flying a plane is actually pretty easy. To do this maneuver: turn the yoke, push the pedals, and when you're lined up with your destination, level off.


But in an iPad app there is only data: speed, heading angle, turn rate, coordinates, time.

Example Vector

I had to dust off my trigonometry and dig into a little bit of aviation math to build a tiny library that made vector, circle, and velocity calculations a bit simpler.

Since these equations are the foundation of my simulation, I wanted to add unit tests so that as I add more features or refactor, the base project is solid.

But doing traditional unit tests in this case meant writing math to prove math. I'd like to actually see this stuff in action before I try to build an app around it.

That's when I remembered you can use layers and views in Xcode 6 Playgrounds. Maybe I could visualize my equations?

I created a new playground in my library called VectorKit.playground to try it out.

For starters, I needed to import my library. Unlike with Swift files, a playground does not automatically import your stuff.

import VectorKit

I created a simple scene with a blank UIView. I had to slim down my units to points instead of nautical miles because playgrounds don't scale to fit.

let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let view = UIView(frame: bounds)
view.backgroundColor = UIColor.lightGrayColor()

In order to get the view to show up in the assistant editor, import XCPlayground and call this method:

XCPShowView("view", view)
Playground Setup

I'm going to be adding a lot of dots to the scene, so I created a dot function to generate little red CALayers.

func dot() -> CALayer {
  let layer = CALayer()
  layer.bounds = CGRect(x: 0, y: 0, width: 4, height: 4)
  layer.cornerRadius = 2
  layer.backgroundColor = UIColor.redColor().CGColor
  return layer

I needed some data about my aircraft, as well as the coordinates of where I was going to send it:

let bank = 30.0 * M_PI / 180.0
let velocity = 15.0
let initial = (x: 100.0, y: 100.0)
let vector = (x: 300.0, y: 120.0)
let heading = 25.0 * M_PI / 180.0
let left = false

Using the dot function, I can easily see where my plane currently is.

let plane = dot()
plane.position = CGPoint(x: initial.x, y: initial.y)

As well as a dot for my destination.

let vectorLayer = dot()
vectorLayer.position = CGPoint(x: vector.x, y: vector.y)

To and Fro

Since I'm using XCPShowView(...) the view is updated in real time. Pretty cool!

Now that the scene is set, I can start using my small library's functions.

let r = VectorKit.turnRadius(velocity, bank)
let center = VectorKit.turnCenter(r, heading, left, initial)

I don't really need to use the VectorKit. prefix, but it helps me remember what parts the framework is doing. Plus, I avoid naming collisions (which I screwed up a lot at first).

Time to draw the full circle of the turn:

let circleBounds = CGRect(x: 0, y: 0, width: r * 2, height: r * 2)
let path = UIBezierPath(roundedRect: circleBounds, cornerRadius: circleBounds.size.height / 2.0)
let circle = CAShapeLayer()
circle.bounds = circleBounds
circle.position = CGPoint(x: center.x, y: center.y)
circle.strokeColor = UIColor.redColor().CGColor
circle.fillColor = nil
circle.lineWidth = 0.5
circle.path = path.CGPath

Turn Circle

I can also figure out the final heading towards the destination coordinate using an equation that finds the line that is tangent to a circle and an external point. And of course, display it!

let tangent = VectorKit.tangentHeading(r, left, center, vector)

let line = CALayer()
line.backgroundColor = UIColor.yellowColor().CGColor
line.bounds = CGRect(x: 0, y: 0, width: 800, height: 0.5)
line.position = vectorLayer.position
line.transform = CATransform3DMakeRotation(CGFloat(tangent), 0, 0, 1.0)

Last, but not least, get the coordinate where we finish our turn and are pointed at the destination.

let tp = VectorKit.tangentPoint(center, tangent, r, left)

let intersect = dot()
intersect.position = CGPoint(x: tp.x, y: tp.y)

Finished Vector and Route

Furthermore, I can tweak my variables, like heading and the turn direction, and double check my math in real time.

Change in Direction

What Have We Got?

In the end, I now have my own visual test that my equations work out. If I ever revisit any of the math, or maybe refactor the library, I can quickly look at this playground to see how things look.

If you're into TDD you can use playgrounds too! Imagine building out each step and then writing functions to satisfy them. Pretty cool.

Is this a replacement for traditional unit tests? Hell no. But I'm a visual person, and this lets me see that things line up and my math is correct.

You can check out the playground and math on the Github repo.

A NSURLSessionTask Debug Story

While working on AsyncDisplayKit, we ran into a strange iOS compatibility bug. Our Kittens example project was working just fine for iOS 8, but when we ran the project on iOS 7 it instacrashed.

There was an unrecognized selector being sent to a NSURLSessionTask object. We were using the Objective-C runtime headers to slap on a metadata object for some convenience properties:

@implementation NSURLSessionTask (ASBasicImageDownloader)
static const char *kMetadataKey = NSStringFromClass(ASBasicImageDownloaderMetadata.class).UTF8String;
- (void)setAsyncdisplaykit_metadata:(ASBasicImageDownloaderMetadata *)asyncdisplaykit_metadata
  objc_setAssociatedObject(self, kMetadataKey, asyncdisplaykit_metadata, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (ASBasicImageDownloader *)asyncdisplaykit_metadata
  return objc_getAssociatedObject(self, kMetadataKey);

Nothing special.

In order to debug the crash, I placed a breakpoint just before the setter that was crashing. I fired up the app on my iPhone 4 running iOS 7.1 and waited for the breakpoint to hit.

As soon as the Debugger window popped up, I used our chisel lldb extensions to inspect the class hierarchy of the object with pclass:

   | __NSCFLocalSessionTask
   |    | __NSCFURLSessionTask
   |    |    | NSObject

That's odd. We are creating a NSURLSessionTask object. That class doesn't even show up in the dump.

Just to be safe, I printed the class hierarchy of the same object when running on iOS 8.1:

   | __NSCFLocalSessionTask
   |    | NSURLSessionTask
   |    |    | NSObject

It appears that the NSURLSessionTask factory method downloadTaskWithURL: has some black-box magic happening on iOS 7.

Since __NSCFLocalDownloadTask doesn't inherit from NSURLSessionTask on iOS 7, that means that our associated object wont exist on our task object.

All we had to do was change the class to NSURLSessionDownloadTask and everything worked just fine. My hunch is that NSURLSessionTask is some sort of class-cluster similar to UIButton.