Commit 0bbf7ba9 by Antonio Maiorano

Cache callstack results to avoid expensive lookup

This works well as our use-case is to lookup the same addresses multiple times (loops, helper functions, etc.). With a simple timing of ReactorUnitTests.exe, with this optimization enabled, total time dropped from 16.72 seconds to 2.24 seconds (~7.5X faster). This optimization is especially important for Mac where resolving stack addresses takes much longer than on other platforms. With this optimization, running a specific gles-unittests test, it was able to output the callstack trace in about 30 seconds; without, it was still emitting jit code after 10 minutes at which point I gave up. Bug: b/131425026 Change-Id: I97e76256220f73fba0a1fc4099532e833f561551 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/32369 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarAntonio Maiorano <amaiorano@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com>
parent aae33735
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
# include "boost/stacktrace.hpp" # include "boost/stacktrace.hpp"
# include <algorithm> # include <algorithm>
# include <unordered_map>
namespace rr { namespace rr {
...@@ -58,21 +59,35 @@ Backtrace getCallerBacktrace(size_t limit /* = 0 */) ...@@ -58,21 +59,35 @@ Backtrace getCallerBacktrace(size_t limit /* = 0 */)
std::vector<Location> locations; std::vector<Location> locations;
// Note that bs::stacktrace() effectively returns a vector of addresses; bs::frame construction is where
// the heavy lifting is done: resolving the function name, file and line number.
namespace bs = boost::stacktrace; namespace bs = boost::stacktrace;
// Cache to avoid expensive stacktrace lookups, especially since our use-case results in looking up the
// same call stack addresses many times.
static std::unordered_map<bs::frame::native_frame_ptr_t, Location> cache;
for(bs::frame frame : bs::stacktrace()) for(bs::frame frame : bs::stacktrace())
{ {
if(shouldSkipFile(frame.source_file()))
{
continue;
}
Location location; Location location;
auto iter = cache.find(frame.address());
if(iter == cache.end())
{
location.function.file = frame.source_file(); location.function.file = frame.source_file();
location.function.name = frame.name(); location.function.name = frame.name();
location.line = frame.source_line(); location.line = frame.source_line();
locations.push_back(location); cache[frame.address()] = location;
}
else
{
location = iter->second;
}
if(shouldSkipFile(location.function.file))
{
continue;
}
locations.push_back(std::move(location));
if(limit > 0 && locations.size() >= limit) if(limit > 0 && locations.size() >= limit)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment