Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiler changes #95

Merged
merged 8 commits into from
Oct 21, 2024
Merged

Profiler changes #95

merged 8 commits into from
Oct 21, 2024

Conversation

michaeleisel
Copy link
Contributor

  • Separate C++ largely into its own class to minimize Objective-C++ and associated weirdness
  • Use more idiomatic, fast C++ (e.g., use objects directly instead of new pointers which also avoids memory leaks)
  • Make performance more consistent (use deque over vector for O(1) worst-case insertion)
  • Only suspend threads one at a time
  • Fix current/potential issues: avoid static initialization order fiasco, minimize reentrant section

As far as performance goes though, it's largely bottlenecked both before and after this PR by FIRCLSReadMemory, which calls vm_read_overwrite. I imagine it's because it doesn't trust the address to be safe to directly dereference without causing a segfault. It currently takes about 0.1 ms for my test case for a single sampling of the main thread, although it's largely dependent on stack depth I imagine. Replacing it with a memcpy makes it take about 0.04ms, at which point thread_get_state is the next-biggest culprit.

@@ -49,25 +39,23 @@ + (BOOL)isRecording {
+ (void)stopRecording:(void (^)(NSDictionary *))stopped {
[sStackRecordingThread cancel];
sStackRecordingThread = nil;
NSLog(@"exiting");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this?


auto emplaceResult = threadsMap.try_emplace(threads[i], threads[i], mainMachThread);
size_t startIndex = addressStorage.size();
// TODO: previously, we caught an std::length_error here. why was that happening?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we didn't handle that case very well in the catch block, so I'm fine with removing it

#import <vector>
#import <unordered_map>
#import <mach/mach.h>
#import <QuartzCore/QuartzCore.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing but I think you can remove this import since CACurrentMediaTime is only used in the implementation file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need something for CFTimeInterval

}

void EMGStackTraceRecorder::recordStackForAllThreads(bool recordAllThreads, thread_t mainMachThread, thread_t etTraceThread) {
std::lock_guard<std::mutex> lockGuard(threadsLock);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you also want to remove this by ensuring the collectThreadSummaries was always called on the same recording thread? Or is that not a bottleneck so not worth doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was less of a performance thing, more of a logic thing (e.g., not accidentally putting code above the lock in the future). However, doing it the other way has its own complications, so I just stuck with the mutex approach

@michaeleisel
Copy link
Contributor Author

testing looks good. i will say, profiling in general (with or without my change) seems off on real devices (not seeing my own functions in the profiling result), but maybe i'm doing something wrong

@michaeleisel
Copy link
Contributor Author

@noahsmartin i can't merge, can you do it

@noahsmartin noahsmartin merged commit 8df984f into EmergeTools:main Oct 21, 2024
1 check passed
@noahsmartin noahsmartin mentioned this pull request Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants