Skip to content

Instantly share code, notes, and snippets.

@BenjaminPoulain
Created September 2, 2011 11:48
Show Gist options
  • Save BenjaminPoulain/1188424 to your computer and use it in GitHub Desktop.
Save BenjaminPoulain/1188424 to your computer and use it in GitHub Desktop.
From b23bd097bd114389754f91f89a0a22098f790fed Mon Sep 17 00:00:00 2001
From: Benjamin Poulain <benjamin@webkit.org>
Date: Thu, 1 Sep 2011 21:56:43 +0200
Subject: [PATCH 2/7] Pass the information loaded by the FileLoader to the
document and update the UI accordingly
When the file loader has finished loading the data, pass the information back to the client. Currently,
only the command name is read and passed to NSDocument. The display name is update accordingly
so that the window title show the command that was being profiled by Valgrind.
---
Callgrind Viewer.xcodeproj/project.pbxproj | 6 +++++
Callgrind Viewer/CallgrindOutputDocument.h | 2 +
Callgrind Viewer/CallgrindOutputDocument.m | 21 +++++++++++++++++++-
Callgrind Viewer/FileLoader.h | 7 +++++-
Callgrind Viewer/FileLoader.m | 16 +++++++++++---
Callgrind Viewer/Profile.h | 29 ++++++++++++++++++++++++++++
Callgrind Viewer/Profile.m | 29 ++++++++++++++++++++++++++++
7 files changed, 104 insertions(+), 6 deletions(-)
create mode 100644 Callgrind Viewer/Profile.h
create mode 100644 Callgrind Viewer/Profile.m
diff --git a/Callgrind Viewer.xcodeproj/project.pbxproj b/Callgrind Viewer.xcodeproj/project.pbxproj
index 793f885..085dbcc 100644
--- a/Callgrind Viewer.xcodeproj/project.pbxproj
+++ b/Callgrind Viewer.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 268A56AF140FFC790066652C /* Profile.m in Sources */ = {isa = PBXBuildFile; fileRef = 268A56AE140FFC790066652C /* Profile.m */; };
268DF3C5140D242000A961F2 /* CallgrindOutputWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 268DF3C4140D242000A961F2 /* CallgrindOutputWindowController.m */; };
268DF3C8140E78AC00A961F2 /* FileLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 268DF3C7140E78AA00A961F2 /* FileLoader.m */; };
26ACF3A5140684FB008653A5 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26ACF3A4140684FB008653A5 /* Cocoa.framework */; };
@@ -19,6 +20,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 268A56AD140FFC790066652C /* Profile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Profile.h; sourceTree = "<group>"; };
+ 268A56AE140FFC790066652C /* Profile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Profile.m; sourceTree = "<group>"; };
268DF3C3140D242000A961F2 /* CallgrindOutputWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CallgrindOutputWindowController.h; sourceTree = "<group>"; };
268DF3C4140D242000A961F2 /* CallgrindOutputWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CallgrindOutputWindowController.m; sourceTree = "<group>"; };
268DF3C6140E78A900A961F2 /* FileLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileLoader.h; sourceTree = "<group>"; };
@@ -97,6 +100,8 @@
26ACF3B9140684FB008653A5 /* CallgrindOutputWindow.xib */,
268DF3C6140E78A900A961F2 /* FileLoader.h */,
268DF3C7140E78AA00A961F2 /* FileLoader.m */,
+ 268A56AD140FFC790066652C /* Profile.h */,
+ 268A56AE140FFC790066652C /* Profile.m */,
26ACF3BC140684FB008653A5 /* MainMenu.xib */,
26ACF3AB140684FB008653A5 /* Supporting Files */,
);
@@ -180,6 +185,7 @@
26ACF3B8140684FB008653A5 /* CallgrindOutputDocument.m in Sources */,
268DF3C5140D242000A961F2 /* CallgrindOutputWindowController.m in Sources */,
268DF3C8140E78AC00A961F2 /* FileLoader.m in Sources */,
+ 268A56AF140FFC790066652C /* Profile.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/Callgrind Viewer/CallgrindOutputDocument.h b/Callgrind Viewer/CallgrindOutputDocument.h
index a8b94ac..aa234ca 100644
--- a/Callgrind Viewer/CallgrindOutputDocument.h
+++ b/Callgrind Viewer/CallgrindOutputDocument.h
@@ -18,10 +18,12 @@
#import <Cocoa/Cocoa.h>
@class FileLoader;
+@class Profile;
@interface CallgrindOutputDocument : NSDocument {
@private
FileLoader *_fileLoader;
+ Profile *_profile;
}
@end
diff --git a/Callgrind Viewer/CallgrindOutputDocument.m b/Callgrind Viewer/CallgrindOutputDocument.m
index b083a61..b7adacc 100644
--- a/Callgrind Viewer/CallgrindOutputDocument.m
+++ b/Callgrind Viewer/CallgrindOutputDocument.m
@@ -19,6 +19,7 @@
#import "CallgrindOutputWindowController.h"
#import "FileLoader.h"
+#import "Profile.h"
@implementation CallgrindOutputDocument
@@ -36,9 +37,24 @@
{
[_fileLoader cancel];
[_fileLoader release];
+ [_profile release];
[super dealloc];
}
+- (void)profileLoaded:(Profile *)profile
+{
+ assert(!_profile);
+ [profile retain];
+ _profile = profile;
+
+ NSURL *fileURL = [self fileURL];
+ NSString *fileName = [fileURL lastPathComponent];
+ [self setDisplayName:[NSString stringWithFormat:@"%@ - %@", fileName, _profile.command]];
+
+ for (NSWindowController *controller in [self windowControllers])
+ [controller synchronizeWindowTitleWithDocumentName];
+}
+
- (void)makeWindowControllers
{
CallgrindOutputWindowController *windowController = [[[CallgrindOutputWindowController alloc] init] autorelease];
@@ -48,7 +64,10 @@
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
assert(!_fileLoader);
- _fileLoader = [[FileLoader alloc] initWithURL:absoluteURL];
+ _fileLoader = [[FileLoader alloc] initWithURL:absoluteURL
+ fileReadCallback:^(Profile *profile) {
+ [self profileLoaded:profile];
+ }];
return YES;
}
diff --git a/Callgrind Viewer/FileLoader.h b/Callgrind Viewer/FileLoader.h
index e82134e..e53ed94 100644
--- a/Callgrind Viewer/FileLoader.h
+++ b/Callgrind Viewer/FileLoader.h
@@ -18,9 +18,14 @@
#import <Foundation/Foundation.h>
@class NSMutableData;
+@class Profile;
+
+typedef void (^SuccessCallback)(Profile *);
@interface FileLoader : NSObject {
@private
+ __block Profile *_profile;
+
__block dispatch_io_t _ioChannel;
NSMutableData *_pendingDataBuffer;
@@ -32,7 +37,7 @@
} _readingStage;
}
-- (id)initWithURL:(NSURL *)absoluteURL;
+- (id)initWithURL:(NSURL *)absoluteURL fileReadCallback:(SuccessCallback)successCallback;
- (void)cancel;
@end
diff --git a/Callgrind Viewer/FileLoader.m b/Callgrind Viewer/FileLoader.m
index e7559fc..f578464 100644
--- a/Callgrind Viewer/FileLoader.m
+++ b/Callgrind Viewer/FileLoader.m
@@ -18,6 +18,7 @@
#import "FileLoader.h"
#import "Foundation/NSData.h"
+#import "Profile.h"
static const size_t maxBufferSize = 4096;
@@ -55,8 +56,7 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
if (match) {
NSRange commandRange = [match rangeAtIndex: 1];
NSString *commandName = [string substringWithRange: commandRange];
- // FIXME: store the command in a nice object representing the profile.
- NSLog(@"command = %@", commandName);
+ _profile.command = commandName;
return YES;
}
}
@@ -163,10 +163,12 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
}
}
-- (id)initWithURL:(NSURL *)absoluteURL
+- (id)initWithURL:(NSURL *)absoluteURL fileReadCallback:(SuccessCallback)successCallback
{
self = [super init];
if (self) {
+ _profile = [[Profile alloc] init];
+
_pendingDataBuffer = [[NSMutableData alloc] initWithCapacity:0];
_readingStage = FormatVersion;
@@ -178,10 +180,15 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
/* mode */ 0,
/* queue */ dispatch_get_main_queue(),
/* completion callback */ ^(int error) {
- if (error != 0) {
+ if (!error && [_profile isValid]) {
+ successCallback(_profile);
+ } else {
// FIXME: error callback to the clients of the class.
NSLog(@"Error while reading the file or reading interrupted.");
}
+ [_profile release];
+ _profile = nil;
+
dispatch_release(_ioChannel);
_ioChannel = 0;
});
@@ -222,6 +229,7 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
- (void)dealloc
{
assert(!_ioChannel);
+ assert(!_profile);
[_pendingDataBuffer release];
[super dealloc];
}
diff --git a/Callgrind Viewer/Profile.h b/Callgrind Viewer/Profile.h
new file mode 100644
index 0000000..6568290
--- /dev/null
+++ b/Callgrind Viewer/Profile.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2011 Benjamin Poulain
+ *
+ * This program is free software: you can redistribute it and or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface Profile : NSObject {
+@private
+ NSString *_command;
+}
+
+@property (nonatomic, copy) NSString *command;
+
+- (BOOL)isValid;
+
+@end
diff --git a/Callgrind Viewer/Profile.m b/Callgrind Viewer/Profile.m
new file mode 100644
index 0000000..abb6304
--- /dev/null
+++ b/Callgrind Viewer/Profile.m
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2011 Benjamin Poulain
+ *
+ * This program is free software: you can redistribute it and or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#import "Profile.h"
+
+@implementation Profile
+
+@synthesize command = _command;
+
+- (BOOL)isValid
+{
+ return !!_command;
+}
+
+@end
--
1.7.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment