-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPTraceDocument.m
516 lines (432 loc) · 14.9 KB
/
RPTraceDocument.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//
// MyDocument.m
// Rampler
//
// Copyright 2010-2012 Fotonauts. All rights reserved.
//
#import "RPTraceDocument.h"
#import "RPCallTree.h"
#import "RPSampleSession.h"
#import "RPRubyTraceLogReader.h"
#import "RPOutlineView.h"
#import "RPTableHeaderView.h"
#define COLUMN_INFO_VERSION 5
@interface RPTraceDocument()
@property (nonatomic) RPSampleSession* root;
@property (nonatomic) RPSampleSession* displayRoot;
@property (nonatomic, weak) RPOutlineView* mainOutlineView;;
@property (nonatomic) RPTraceDocument *mainDocument;
@property (nonatomic) NSString *version;
@property (nonatomic) NSURL* url;
@property (nonatomic, assign) double interval;
@property (nonatomic, assign) double duration;
@end
@implementation RPTraceDocument
+ (NSArray *)defaultOutlineColumnList
{
NSMutableArray *result;
NSMutableArray *defaultValue;
defaultValue = [[NSMutableArray alloc] initWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Thread", @"title", @"thread", @"identifier", @52.0f, @"width", @NO, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Total", @"title", @"totalTime", @"identifier", @101.0f, @"width", @YES, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Self", @"title", @"selfTime", @"identifier", @61.0f, @"width", @YES, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Tick count", @"title", @"tickCount", @"identifier", @52.0f, @"width", @NO, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Blocked ticks", @"title", @"blockedTicks", @"identifier", @52.0f, @"width", @YES, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"File", @"title", @"file", @"identifier", @232.0f, @"width", @NO, @"enabled", nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Symbol", @"title", @"symbol", @"identifier", @300.0f, @"width", @YES, @"enabled", nil],
nil
];
result = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"columns"] mutableCopy];
if (!result || ![result[0] isKindOfClass:[NSNumber class]] || [result[0] intValue] != COLUMN_INFO_VERSION) {
result = defaultValue;
} else {
int ii, count;
[result removeObjectAtIndex:0];
count = [result count];
for (ii = 0; ii < count; ii++) {
NSMutableDictionary *column;
column = [result[ii] mutableCopy];
result[ii] = column;
}
}
return result;
}
- (id)init
{
self = [super init];
if (self) {
self.displayTimeUnitAsPercentOfTotal = YES;
}
return self;
}
- (void)dealloc
{
self.root = nil;
}
- (void)loadWithMainDocument:(RPTraceDocument *)document root:(RPSampleSession *)newRoot version:(NSString *)newVersion
{
self.mainDocument = document;
self.root = newRoot;
self.version = newVersion;
self.interval = document.interval;
self.url = document.url;
self.duration = document.duration;
}
- (NSString *)windowNibName
{
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
- (void)setRoot:(RPSampleSession *)newRoot
{
if (newRoot != _root) {
_root = newRoot;
[self updateDisplayRoot];
}
}
- (void) updateDisplayRoot
{
RPSampleSession* newRoot = self.root;
if (self.flattenRecursion) {
newRoot = [newRoot sessionByFlatteningRecursion];
}
self.displayRoot = newRoot;
if (self.displayRoot) [_mainOutlineView reloadData];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
id<RPLogReader> reader = nil;
if ([typeName isEqualToString:@"Ruby trace"]) {
reader = [[RPRubyTraceLogReader alloc] initWithData:data];
}
self.root = [reader sampleSession];
self.version = [reader version];
self.url = [reader url];
self.interval = [reader interval];
self.duration = [reader duration];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
- (void) setDisplayTimeUnitAsPercentOfTotal:(BOOL)percentEnabled
{
_displayTimeUnitAsPercentOfTotal = percentEnabled;
[self updateTimeFormatter];
[self.mainOutlineView reloadData];
[self.mainOutlineView setNeedsDisplay];
}
- (void)_updateOutlineViewColumn
{
int ii = 0;
updatingColumns = YES;
for (NSDictionary *info in columnInfo) {
NSTableColumn *column;
column = [_mainOutlineView tableColumnWithIdentifier:info[@"identifier"]];
if (!column && [info[@"enabled"] boolValue]) {
column = [[NSTableColumn alloc] initWithIdentifier:info[@"identifier"]];
[_mainOutlineView addTableColumn:column];
}
if ([info[@"enabled"] boolValue]) {
[_mainOutlineView moveColumn:[_mainOutlineView columnWithIdentifier:info[@"identifier"]] toColumn:ii];
[column setWidth:[info[@"width"] floatValue]];
[[column headerCell] setTitle:info[@"title"]];
ii++;
} else if (column) {
[_mainOutlineView removeTableColumn:column];
}
}
updatingColumns = NO;
}
- (void)_saveColumnInfo
{
NSMutableArray *copy;
copy = [columnInfo mutableCopy];
[copy insertObject:@COLUMN_INFO_VERSION atIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:copy forKey:@"columns"];
}
- (void)awakeFromNib
{
[self updateTimeFormatter];
if (self.mainDocument) {
[focusDownFunctionButton setHidden:YES];
}
[urlTextField setAction:@selector(urlTextFieldClicked:)];
[totalTimeTextField setStringValue:[NSString stringWithFormat:@"%.2fs", self.root.totalTime]];
[intervalTextField setStringValue:[NSString stringWithFormat:@"%.2fms", self.interval * 1000]];
[realIntervalTextField setStringValue:[NSString stringWithFormat:@"%.2fms", (self.root.totalTime / self.root.sampleCount) * 1000]];
[tickCountTextField setStringValue:[NSString stringWithFormat:@"%ld", self.root.sampleCount]];
[stackCountTextField setStringValue:[NSString stringWithFormat:@"%ld", self.root.stackTraceCount]];
[versionTextField setStringValue:self.version];
[urlTextField setStringValue:self.url ? [self.url absoluteString] : @""];
_mainOutlineView.columnIdentifierForCopy = @"file";
[_mainOutlineView setDoubleAction:@selector(outlineDoubleAction:)];
columnInfo = [[[self class] defaultOutlineColumnList] mutableCopy];
[self _updateOutlineViewColumn];
}
- (void)updateTimeFormatter
{
if (_displayTimeUnitAsPercentOfTotal) {
self.percentFormatter.multiplier = @(100.0 / self.displayRoot.totalTime);
self.percentFormatter.positiveSuffix = @"%";
self.percentFormatter.maximumFractionDigits = 2;
} else {
self.percentFormatter.multiplier = @1000.0;
self.percentFormatter.positiveSuffix = @"ms";
self.percentFormatter.maximumFractionDigits = 2;
}
}
- (NSArray *)childrenForCallTree:(RPCallTree *)callTree
{
NSArray *result;
if (!hideInsignificantCalls) {
result = [callTree children];
} else {
while (YES) {
result = [callTree children];
if ([result count] == 0) {
break;
} else if ([result count] == 1) {
callTree = result[0];
if (callTree.totalTime * 5 / 100 < callTree.selfTime) {
break;
}
} else {
NSMutableArray *significantChildren;
NSInteger ii, count;
significantChildren = [result mutableCopy];
count = [significantChildren count];
for (ii = 0; ii < count; ii++) {
RPCallTree *current = significantChildren[ii];
if (callTree.totalTime * 5 / 100 > current.totalTime) {
[significantChildren removeObjectAtIndex:ii];
count--;
ii--;
}
}
result = significantChildren;
break;
}
}
}
return result;
}
- (void)expandAndSelectCallTree:(RPCallTree *)node
{
NSMutableArray *parents;
NSInteger selectedRow;
selectedRow = -1;
parents = [[NSMutableArray alloc] init];
while (node) {
[parents insertObject:node atIndex:0];
node = node.parent;
}
for (RPCallTree *node in parents) {
NSInteger row;
[_mainOutlineView expandItem:node];
row = [_mainOutlineView rowForItem:node];
if (row != -1) {
selectedRow = row;
}
}
if (selectedRow != -1) {
[_mainOutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
} else {
[_mainOutlineView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
}
}
- (void) focusOnSelectedRow
{
RPCallTree *callTreeToSelect = [self selectedCallTree];
if (!callTreeToSelect) return;
[self openDerivedDocumentWithRoot:[self.displayRoot sessionByFocussingOnSubTree:callTreeToSelect]];
}
#pragma mark -
#pragma mark IBAction
- (void)openDerivedDocumentWithRoot:(RPSampleSession*)newRoot
{
RPTraceDocument* newDocument = [[RPTraceDocument alloc] initWithType:@"Ruby trace" error:nil];
if (self.mainDocument) {
[newDocument loadWithMainDocument:self.mainDocument root:newRoot version:self.version];
} else {
[newDocument loadWithMainDocument:self root:newRoot version:self.version];
}
[[NSDocumentController sharedDocumentController] addDocument:newDocument];
[newDocument makeWindowControllers];
[newDocument showWindows];
}
- (RPCallTree*)selectedCallTree
{
NSInteger selectedRow = [_mainOutlineView selectedRow];
return selectedRow == -1 ? nil : [_mainOutlineView itemAtRow:selectedRow];
}
- (IBAction)followHottestSubpath:(id)sender
{
NSInteger selectedRow = [self.mainOutlineView selectedRow];
RPCallTree* selectedNode = [self.mainOutlineView itemAtRow:selectedRow];
RPCallTree* hottestNode = selectedNode;
while ([[self childrenForCallTree:hottestNode] count] > 0) {
[self.mainOutlineView expandItem:hottestNode];
hottestNode = [self childrenForCallTree:hottestNode][0];
}
}
- (void)outlineDoubleAction:(id)sender
{
[self focusOnSelectedRow];
}
- (IBAction)focusButtonAction:(id)sender
{
[self focusOnSelectedRow];
}
- (IBAction)focusDownFunctionButtonAction:(id)sender;
{
RPCallTree* selectedCall = [self selectedCallTree];
if (!selectedCall) return;
[self openDerivedDocumentWithRoot:[self.root callTreeByMergingDownIdenticalCalls:selectedCall]];
}
- (IBAction)flattenRecursionButtonAction:(id)sender
{
self.flattenRecursion = ([flattenRecursionButton state] == NSOnState);
[self updateDisplayRoot];
}
#pragma mark -
#pragma mark Outline view delegation
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if (!item) {
item = self.displayRoot;
}
return [self childrenForCallTree:item][index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
return [[self childrenForCallTree:item] count];
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (!item) {
item = self.displayRoot;
}
return [[self childrenForCallTree:item] count];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
id result = nil;
if ([[tableColumn identifier] isEqualToString:@"thread"]) {
result = @([item thread]);
} else if ([[tableColumn identifier] isEqualToString:@"totalTime"]) {
result = @([item totalTime]);
} else if ([[tableColumn identifier] isEqualToString:@"selfTime"]) {
result = @([item selfTime]);
} else if ([[tableColumn identifier] isEqualToString:@"tickCount"]) {
result = @([item sampleCount]);
} else if ([[tableColumn identifier] isEqualToString:@"file"]) {
result = [item file];
} else if ([[tableColumn identifier] isEqualToString:@"namespace"]) {
result = [item moduleOrClass];
} else if ([[tableColumn identifier] isEqualToString:@"symbol"]) {
result = [NSString stringWithFormat:@"%@::%@", [item moduleOrClass], [item method]];
} else if ([[tableColumn identifier] isEqualToString:@"blockedTicks"]) {
result = @([item blockedTicks]);
}
return result;
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification;
{
BOOL allowSelectionDependentActions = [_mainOutlineView selectedRow] != -1;
[focusButton setEnabled:allowSelectionDependentActions];
[focusDownFunctionButton setEnabled:allowSelectionDependentActions];
[hottestSubpathButton setEnabled:allowSelectionDependentActions];
[unfocusButton setEnabled:self.root != self.displayRoot];
}
- (void)setHideInsignificantCalls:(BOOL)value
{
NSInteger selectedRow = [self.mainOutlineView selectedRow];
RPCallTree* selectedNode = nil;
if (selectedRow != -1) {
selectedNode = [self.mainOutlineView itemAtRow:selectedRow];
}
hideInsignificantCalls = value;
[_mainOutlineView reloadData];
if (selectedNode) {
[self expandAndSelectCallTree:selectedNode];
}
}
- (NSMenu *)headerMenuForTableView:(NSTableView *)tableView event:(NSEvent *)event
{
NSMenu *result;
NSInteger tag = 0;
result = [[NSMenu alloc] init];
[result setDelegate:self];
for (NSDictionary *column in columnInfo) {
NSMenuItem *item;
item = [result addItemWithTitle:column[@"title"] action:@selector(headerMenuAction:) keyEquivalent:@""];
[item setState:[column[@"enabled"] boolValue]?NSOnState:NSOffState];
[item setTag:tag];
if ([column[@"identifier"] isEqualToString:@"symbol"]) {
[item setEnabled:NO];
}
tag++;
}
return result;
}
- (void)headerMenuAction:(NSMenuItem *)item
{
NSMutableDictionary *info;
info = columnInfo[[item tag]];
info[@"enabled"] = [NSNumber numberWithBool:![info[@"enabled"] boolValue]];
[self _updateOutlineViewColumn];
[self _saveColumnInfo];
}
- (void)outlineViewColumnDidMove:(NSNotification *)notification
{
if (!updatingColumns) {
NSInteger newColumn = [[notification userInfo][@"NSNewColumn"] intValue];
NSInteger oldColumn = [[notification userInfo][@"NSOldColumn"] intValue];
NSDictionary *info;
NSInteger ii = 0;
for (info in columnInfo) {
if (![info[@"enabled"] boolValue]) {
if (ii <= newColumn) {
newColumn++;
}
if (ii <= oldColumn) {
oldColumn++;
}
}
ii++;
}
info = columnInfo[oldColumn];
[columnInfo removeObjectAtIndex:oldColumn];
[columnInfo insertObject:info atIndex:newColumn];
[self _saveColumnInfo];
}
}
- (void)outlineViewColumnDidResize:(NSNotification *)notification
{
if (!updatingColumns) {
NSTableColumn *column;
column = [notification userInfo][@"NSTableColumn"];
for (NSMutableDictionary *info in columnInfo) {
if ([info[@"identifier"] isEqualToString:[column identifier]]) {
info[@"width"] = [NSNumber numberWithFloat:[column width]];
}
}
[self _saveColumnInfo];
}
}
- (BOOL)hideInsignificantCalls
{
return hideInsignificantCalls;
}
@end