Skip to content

Commit

Permalink
kram-profile - breakup CBA call
Browse files Browse the repository at this point in the history
  • Loading branch information
alecazam committed Mar 25, 2024
1 parent 29a5840 commit deafe97
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
10 changes: 9 additions & 1 deletion kram-profile/CBA/CBA.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@

// TODO: move to header
@interface CBA : NSObject
+ (NSString* _Nonnull)RunCBA:(NSArray<NSData*> * _Nonnull)files filenames:(NSArray<NSString*> * _Nonnull)filenames;

- (_Nonnull instancetype)init;
- (void)deinit;

- (void)parse:(NSData* _Nonnull)file filename:(NSString* _Nonnull)filename;
- (void)parseAll:(NSArray<NSData*> * _Nonnull)files filenames:(NSArray<NSString*> * _Nonnull)filenames;

- (NSString* _Nonnull)analyze;

@end
45 changes: 33 additions & 12 deletions kram-profile/CBA/CBA.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,51 @@


@implementation CBA {
BuildEventsParser* parser;
}

+ (NSString* _Nonnull)RunCBA:(NSArray<NSData*> * _Nonnull)files filenames:(NSArray<NSString*> * _Nonnull)filenames
{
- (_Nonnull instancetype)init {
ArenaInitialize();

BuildEventsParser* parser = CreateBuildEventsParser();

for (uint32_t i = 0; i < files.count; ++i)
{
NSData* data = files[i];
ParseBuildEvents(parser, (const uint8_t*)data.bytes, data.length, [filenames[i] UTF8String]);
parser = CreateBuildEventsParser();

return self;
}

- (void)deinit {
// Shutdown the parser
DeleteBuildEventsParser(parser);
parser = nullptr;

ArenaDelete();
}

// This is bad because it runs single-threaded, and doesn't cache anything across builds.
// TODO: restructure, so parser is built once
// feed files to it individually, and then request analysis on a few of the events/names
// TODO: reformat output to Perfetto json, can then display it visually.
- (void)parseAll:(NSArray<NSData*> * _Nonnull)files filenames:(NSArray<NSString*> * _Nonnull)filenames
{
for (uint32_t i = 0; i < files.count; ++i) {
[self parse:files[i] filename:filenames[i]];
}
}

- (void)parse:(NSData* _Nonnull)file filename:(NSString* _Nonnull)filename {
const char* filename_ = [filename UTF8String];
ParseBuildEvents(parser, (const uint8_t*)file.bytes, file.length, filename_);
}


- (NSString* _Nonnull)analyze {
// Run the analysis on data from the parser.
std::string out;
DoAnalysis(GetBuildEvents(*parser), GetBuildNames(*parser), out);

// Shutdown the parser
DeleteBuildEventsParser(parser);
ArenaDelete();

return [NSString stringWithUTF8String:out.c_str()];
}



@end

5 changes: 2 additions & 3 deletions kram-profile/CBA/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ bool utils::BeginsWith(const std::string& str, const std::string& prefix)
}
return true;
}
*/

bool utils::EndsWith(const std::string_view& str, const std::string& suffix)
{
if (str.size() < suffix.size())
Expand All @@ -45,8 +46,6 @@ bool utils::EndsWith(const std::string_view& str, const std::string& suffix)
}
return true;
}
*/


bool utils::IsHeader(std::string_view path)
{
Expand Down
3 changes: 2 additions & 1 deletion kram-profile/CBA/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace utils
void Lowercase(std::string& path);
[[nodiscard]] bool BeginsWith(const std::string& str, const std::string& prefix);
*/
[[nodiscard]] bool EndsWith(const std::string_view& str, const std::string& suffix);
*/

}
3 changes: 3 additions & 0 deletions kram-profile/kram-profile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "-ftime-trace";
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_OBJC_INTEROP_MODE = objc;
Expand Down Expand Up @@ -365,6 +366,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand All @@ -381,6 +383,7 @@
MACOSX_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-ftime-trace";
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "";
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down
6 changes: 4 additions & 2 deletions kram-profile/kram-profile/kram_profileApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2407,8 +2407,10 @@ A tool to help profile mem, perf, and builds.
}
// Extract the fileContent and names. This avoids CBA needing to do IO.
// But CBA is reparsing all of the json in C++ to build up its tables.
// Also demangling names again..
let cbaReport = CBA.run(fileDatas, filenames: filenames)
// Also demangling names again.
let cba = CBA()
cba.parseAll(fileDatas, filenames: filenames)
let cbaReport = cba.analyze()

// Can't use log here, since it's not setup to chop up long
// strings by newlines yet. Print doesn't go to console
Expand Down

0 comments on commit deafe97

Please sign in to comment.