-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPRubyTraceLogReader.m
304 lines (231 loc) · 10.1 KB
/
RPRubyTraceLogReader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// RPRubyTraceLogReader.m
// Rampler
//
// Copyright 2010-2012 Fotonauts. All rights reserved.
//
#import "RPRubyTraceLogReader.h"
#import "RPCallTree.h"
#import "RPSampleSession.h"
#import "RPStackTrace.h"
static NSString* kTabSeparator = @"\t";
static NSString* kTracesStartMarker = @"-- Traces --";
static NSString* kFileIndexStartMarker = @"-- File Index --";
static NSString* kClassIndexStartMarker = @"-- Class Index --";
static NSString* kFunctionIndexStartMarker = @"-- Function Index --";
static NSString* kFooterStartMarker = @"-- Footer --";
static NSString* kEmptyLine = @"";
NSString* RPRubyTraceErrorDomain = @"RPRubyTraceErrorDomain";
NSInteger RPRubyTraceParseError = -1;
@interface RPRubyTraceLogReader ()
@property(nonatomic) NSMutableArray* stacks;
@property(nonatomic) NSArray* filesIndex;
@property(nonatomic) NSArray* classesIndex;
@property(nonatomic) NSArray* functionsIndex;
@property(nonatomic) NSString* version;
@property(nonatomic) NSURL* url;
@property(nonatomic, assign)double interval;
@property (nonatomic) NSString *beginningInfoDescription;
@property (nonatomic) NSString *endingInfoDescription;
@property (nonatomic) NSDate* startDate;
@property (nonatomic, assign) double duration;
@property (nonatomic, assign) NSUInteger sampleCount;
@end
@implementation RPRubyTraceLogReader
@synthesize beginningInfoDescription;
@synthesize endingInfoDescription;
- (id) initWithData:(NSData*)d
{
self = [super init];
self.data = d;
self.stacks = [[NSMutableArray alloc] init];
self.beginningInfoDescription = [[NSMutableString alloc] init];
self.endingInfoDescription = [[NSMutableString alloc] init];
BOOL dataOK = [self readData];
if (!dataOK && self.parseError) {
[[NSAlert alertWithError:self.parseError] runModal];
}
return self;
}
- (NSString*)readNextLine
{
NSInteger dataLength = [self.data length];
if (_currentPosition >= dataLength) return nil;
NSInteger eolPos = _currentPosition;
const UInt8* bytes = [self.data bytes];
while ((eolPos < dataLength) && (bytes[eolPos] != '\n')) ++eolPos;
NSString* line = [[NSString alloc] initWithData:[self.data subdataWithRange:NSMakeRange(_currentPosition, eolPos - _currentPosition)] encoding:NSUTF8StringEncoding];
_currentPosition = eolPos + 1;
return line;
}
#define THROW_ON_PARSE_ERROR 0
#define _PARSE_ASSERT(__boolValue__, __reasonString__, __return_value__, __throw__) if (!(__boolValue__)) { \
self.parseError = [NSError errorWithDomain:RPRubyTraceErrorDomain code:RPRubyTraceParseError userInfo:@{NSLocalizedDescriptionKey : (__reasonString__)}];\
if (__throw__) { \
NSAssert(NO, (__reasonString__));\
} \
return NO;\
}
#define PARSE_ASSERT(__boolValue__, __reasonString__, __return_value__) _PARSE_ASSERT(__boolValue__, __reasonString__, __return_value__, THROW_ON_PARSE_ERROR)
#define _ASSERT_CURRENT_LINE(__expected_line__, __return_value__, __throw__) if (![(__expected_line__) isEqualToString:curLine]) { \
NSString* errorString = [NSString stringWithFormat:@"expecting '%@' in file. Found '%@'.", (__expected_line__), curLine]; \
self.parseError = [NSError errorWithDomain:RPRubyTraceErrorDomain code:RPRubyTraceParseError userInfo:@{NSLocalizedDescriptionKey : errorString}];\
if (__throw__) { \
NSAssert(NO, (errorString));\
} \
return NO; \
}
#define ASSERT_CURRENT_LINE(__expected_line__, __return_value__) _ASSERT_CURRENT_LINE(__expected_line__, __return_value__, THROW_ON_PARSE_ERROR)
- (BOOL) parseHeader:(NSString*)curLine
{
NSArray* values = [curLine componentsSeparatedByString:kTabSeparator];
PARSE_ASSERT(values.count >= 4, @"invalid number of element in header line", NO);
self.version = values[0];
self.url = [NSURL URLWithString:values[1]];
self.interval = [values[2] doubleValue] / 1000000.0;
self.startDate = [NSDate dateWithString:values[3]];
if (values.count > 4) {
self.beginningInfoDescription = values[4]; // Fixme: unescape when writer escapes \t and \n
}
return YES;
}
- (RPStackFrame*) parseLine:(NSString*)line
{
NSArray* components = [line componentsSeparatedByString:@"\t"];
PARSE_ASSERT(components.count == 4, @"Invalid index number (index is probably not given in order", nil);
RPStackFrame* parsedLine = [[RPStackFrame alloc] init];
parsedLine.fileId = [components[0] integerValue];
parsedLine.fileLine = [components[1] integerValue];
parsedLine.classId = [components[2] integerValue];
parsedLine.functionId = [components[3] integerValue];
return parsedLine;
}
- (NSArray*) parseSymbolIndexUpTo:(NSString*)endDelimiter
{
NSMutableArray* symbols = [[NSMutableArray alloc] init];
NSInteger indexCheck = 1;
NSString* curLine = [self readNextLine];
while (curLine && ![curLine isEqualToString:endDelimiter]) {
NSArray* indexEntry = [curLine componentsSeparatedByString:kTabSeparator];
if (indexEntry.count != 2) {
self.parseError = [NSError errorWithDomain:RPRubyTraceErrorDomain code:RPRubyTraceParseError userInfo:@{NSLocalizedDescriptionKey : @"Invalid number of elements in index line"}];
return NO;
}
NSInteger index = [indexEntry[0] integerValue];
NSString* value = indexEntry[1];
PARSE_ASSERT(index == indexCheck, @"Invalid index number (index is probably not given in order", nil)
[symbols addObject:value];
indexCheck++;
curLine = [self readNextLine];
}
ASSERT_CURRENT_LINE(endDelimiter, NO);
return symbols;
}
- (BOOL) parseFooter:(NSString*)curLine
{
NSArray* values = [curLine componentsSeparatedByString:kTabSeparator];
PARSE_ASSERT(values.count <= 3, @"invalid number of element in footer line", NO);
self.duration = [values[0] doubleValue];
self.sampleCount = [values[1] integerValue];
if (values.count > 2) {
self.endingInfoDescription = values[2]; // Fixme: unescape when writer escapes \t and \n
}
return YES;
}
- (BOOL) readData
{
NSString* curLine = [self readNextLine];
PARSE_ASSERT(curLine, @"empty file", NO);
BOOL headerOK = [self parseHeader:curLine];
if (!headerOK) return NO;
curLine = [self readNextLine];
ASSERT_CURRENT_LINE(kTracesStartMarker, NO);
curLine = [self readNextLine];
while (curLine && ![curLine isEqualToString:kFileIndexStartMarker]) {
NSArray* stackTraceInfos = [curLine componentsSeparatedByString:kTabSeparator];
PARSE_ASSERT(stackTraceInfos.count == 3, @"invalid number of element in frame header", NO);
int stackDepth = [stackTraceInfos[0] intValue];
RPStackTrace* stackTrace = [[RPStackTrace alloc] init];
stackTrace.sampleCount = [stackTraceInfos[1] integerValue];
stackTrace.duration = [stackTraceInfos[2] doubleValue];
NSMutableArray* stackLines = [[NSMutableArray alloc] initWithCapacity:stackDepth];
for (int i=0; i<stackDepth; ++i) {
curLine = [self readNextLine];
PARSE_ASSERT(curLine, @"unexpected end of file while reading stack trace", NO);
RPStackFrame* parsedLine = [self parseLine:curLine];
if (!parsedLine) return NO;
parsedLine.isLeaf = (i == stackDepth-1);
[stackLines addObject:parsedLine];
}
stackTrace.frames = stackLines;
[self.stacks addObject:stackTrace];
curLine = [self readNextLine]; // eat aesthetic empty line between frames
ASSERT_CURRENT_LINE(kEmptyLine, NO);
curLine = [self readNextLine]; // next stack trace
}
ASSERT_CURRENT_LINE(kFileIndexStartMarker, NO);
self.filesIndex = [self parseSymbolIndexUpTo:kClassIndexStartMarker];
if (!self.filesIndex) return NO;
self.classesIndex = [self parseSymbolIndexUpTo:kFunctionIndexStartMarker];
if (!self.classesIndex) return NO;
self.functionsIndex = [self parseSymbolIndexUpTo:kFooterStartMarker];
if (!self.functionsIndex) return NO;
curLine = [self readNextLine];
PARSE_ASSERT(curLine, @"unexpected end of file reaching footer", NO);
BOOL footerOK = [self parseFooter:curLine];
if (!footerOK) return NO;
NSLog(@"File read successfully");
NSLog(@"stacks %lu", [self.stacks count]);
return YES;
}
- (NSString*) filePathForIndex:(NSInteger)index
{
if (index == 0) return @"-";
return _filesIndex[index-1];
}
- (NSString*) classnameForIndex:(NSInteger)index
{
if (index == 0) return @"-";
return _classesIndex[index-1];
}
- (NSString*) functionForIndex:(NSInteger)index
{
if (index == 0) return @"-";
return _functionsIndex[index-1];
}
- (RPSampleSession*)sampleSession
{
RPSampleSession* session;
int totalSampleCount = 0;
session = [[RPSampleSession alloc] init];
session.thread = 0;
for (RPStackTrace *stackTrace in self.stacks) {
RPCallTree *callTreeFrame;
RPStackFrame *frame;
NSInteger stackTraceSampleCount;
stackTraceSampleCount = [stackTrace sampleCount];
totalSampleCount += stackTraceSampleCount;
callTreeFrame = session;
for (frame in stackTrace.frames) {
NSString* filePath = [self filePathForIndex:frame.fileId];
NSString* className = [self classnameForIndex:frame.classId];
NSString* function = [self functionForIndex:frame.functionId];
callTreeFrame = [callTreeFrame subTreeForMethodId:frame.functionId classId:frame.classId create:YES];
if (frame.isLeaf) {
// last frame. Assign all canonical durations to this one.
callTreeFrame.selfSampleCount += stackTrace.sampleCount;
callTreeFrame.selfBlockedTicks += stackTrace.sampleCount - 1;
callTreeFrame.selfStackTraceCount += 1;
}
callTreeFrame.startLine = frame.logLineNumber;
callTreeFrame.method = function;
callTreeFrame.file = filePath;
callTreeFrame.moduleOrClass = className;
}
}
session.sessionDurationPerTick = self.duration / totalSampleCount;
[session freeze];
NSLog(@"totalSampleCount %d", totalSampleCount);
return session;
}
@end