Search code examples
iphoneiosxcodeopengl-esinstruments

Trying to read a Xcode Instruments .trace file. What is the file format of a .trace file?


I am writing an automated profiling system, to profile different GPU intensive screens in my App. I have been trying to use 'XCode Instruments' for this, with the 'OpenGL ES Driver' instrument that captures the gpu usage data.

My automated system runs Xcode Instruments from the command line which runs the App, profiles and captures the data, and writes the data to a ".trace" file.

I now want to be able to open the trace file, and read the trace data using my automated profiling system, so that I can inform App developers of how the various parts of the App perform.

I cannot however find any way of reading the trace file. It seems to be package which contain various directories, and buried in there is a .zip file which seems to contain some binary data. How is the data in this file parsed?

The Instruments system seems fairly sosphisticated, and I've been suprised how hard it has been to access the trace data that it produces.

Does anyone know how to parse the trace file?

I am currently using XCode 4.6.1


Solution

  • Alright, so to answer the main question: The data in the .zip archive is a blob of data that was serialized with the NSArchiver class (they have a fairly distinctive header when being opened with a hex tool (I used hex fiend), so that was the first clue). It's fairly straight forward to read, all you have to do is making a call to NSUnarchiver, at least that's the theory. Before I go in into the details, here is a very simple example app that dumps a few infos: https://github.com/JustSid/Traced

    So, the problem with NSArchiver, and NSUnarchiver, is that you first of all need to have all the classes that were archived, and second of all you have to read the data out in the order in that it was archived (that was the tricky bit, I used class-dump to dump the interface for a few of the required classes and then tried to unarchive the data object by object and looking at what I got returned. Luckily, NSArchiver dies with descriptive error messages, if there is a class missing, it will tell you what its name is). The biggest problem that I had was that the Instruments binary and the used frameworks don't contain all the classes that I needed, in particular the archive contains serialized data of a class named XRVideoCardRun. I have the assumption that the .template file inside the .trace bundle contains a dynamic library with the required class (I mean, it's over 300kb in size and contains a lot of blobs (it's btw a binary plist)). I was too lazy to extract the binary data out of it and run class-dump against it, and I was lucky enough that most of the data that came out of the archive was consistent with what I was expecting to see for the superclass, XRRun (which I found in one of the Instruments frameworks), with the exception of an array containing dictionaries, which content looked like the sample data.

    So, the rest was just combining everything together. If you look into the sample app, the most interesting part should be the XRRun.m and .h file. They contain a bit of documentation, and some pieces on how to extract the data from the samples, although you probably want to replace this with your own logic for your automation. Hope it helps.

    The app thrown agains your sample file outputs this:

    Run 1, starting at 24.05.13 17:42:16, running until 24.05.13 17:42:28
    Sample 0: FPS: 27 Device: 0% Renderer: 0% Tiler: 0% Timestamp: 1.012740
    Sample 1: FPS: 35 Device: 11% Renderer: 10% Tiler: 2% Timestamp: 2.018574
    Sample 2: FPS: 34 Device: 33% Renderer: 32% Tiler: 7% Timestamp: 3.026101
    Sample 3: FPS: 59 Device: 59% Renderer: 59% Tiler: 16% Timestamp: 4.032030
    Sample 4: FPS: 60 Device: 59% Renderer: 58% Tiler: 16% Timestamp: 5.038990
    Sample 5: FPS: 59 Device: 59% Renderer: 58% Tiler: 16% Timestamp: 6.046022
    Sample 6: FPS: 59 Device: 57% Renderer: 53% Tiler: 17% Timestamp: 7.051187
    Sample 7: FPS: 60 Device: 67% Renderer: 66% Tiler: 14% Timestamp: 8.057343
    Sample 8: FPS: 59 Device: 64% Renderer: 64% Tiler: 11% Timestamp: 9.064914
    Sample 9: FPS: 60 Device: 67% Renderer: 67% Tiler: 11% Timestamp: 10.072592
    Sample 10: FPS: 59 Device: 65% Renderer: 65% Tiler: 15% Timestamp: 11.080248
    

    (PS: If the format changes, the app will break as well...)