Trying to read a Xcode Instruments .trace file. What is the file format of a .trace file?
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...)
I am trying to parse the .trace document using the undocumented frameworks shipped with Instruments itself. It is now working with Time Profiler and it shouldn't be hard to get it working with other instrument templates as well, with a little more reverse engineering work.
There are quite a few frameworks bundled with Instruments as you can see in /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/Frameworks
.
However we only need to link against these two:
DVTInstrumentsFoundation.framework
InstrumentsPlugIn.framework
Another thing you should know before starting is that instrument templates are actually plugins in /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns
.
For example, SamplerPlugin.xrplugin
is for Time Profiler.
The code is short and commented out: https://github.com/Qusic/TraceUtility