diff --git a/source/lib/core/config.cpp b/source/lib/core/config.cpp index 1e4ff5b6b..a08c8a71d 100644 --- a/source/lib/core/config.cpp +++ b/source/lib/core/config.cpp @@ -608,6 +608,11 @@ configure_settings(bool _init) "Enable HIP API tracing support", true, "roctracer", "rocm", "advanced"); + OMNITRACE_CONFIG_SETTING( + bool, "OMNITRACE_ROCTRACER_HIP_API_BACKTRACE", + "Enable annotating the perfetto debug annotation with backtraces", false, + "roctracer", "rocm", "perfetto", "advanced"); + OMNITRACE_CONFIG_SETTING(bool, "OMNITRACE_ROCTRACER_HIP_ACTIVITY", "Enable HIP activity tracing support", true, "roctracer", "rocm", "advanced"); diff --git a/source/lib/omnitrace/library/roctracer.cpp b/source/lib/omnitrace/library/roctracer.cpp index 7ce7bf144..8a4632622 100644 --- a/source/lib/omnitrace/library/roctracer.cpp +++ b/source/lib/omnitrace/library/roctracer.cpp @@ -21,6 +21,7 @@ // SOFTWARE. #include "library/roctracer.hpp" +#include "binary/analysis.hpp" #include "core/components/fwd.hpp" #include "core/concepts.hpp" #include "core/config.hpp" @@ -736,6 +737,35 @@ hip_api_callback(uint32_t domain, uint32_t cid, const void* callback_data, void* "OMNITRACE_PERFETTO_COMPACT_ROCTRACER_ANNOTATIONS") .value_or(false); + static auto _enable_backtraces = + config::get_setting_value("OMNITRACE_ROCTRACER_HIP_API_BACKTRACE") + .value_or(false); + + constexpr size_t bt_stack_depth = 16; + constexpr size_t bt_ignore_depth = 3; + constexpr bool bt_with_signal_frame = true; + + using backtrace_entry_vec_t = std::vector; + auto _bt_data = std::optional{}; + if(_enable_backtraces && config::get_perfetto_annotations()) + { + auto _backtrace = tim::get_unw_stack(); + _bt_data = backtrace_entry_vec_t{}; + _bt_data->reserve(_backtrace.size()); + for(auto itr : _backtrace) + { + if(itr) + { + if(auto _val = binary::lookup_ipaddr_entry(itr->address()); + _val) + { + _bt_data->emplace_back(std::move(*_val)); + } + } + } + } + auto _api_id = static_cast(cid); tracing::push_perfetto_ts( category::rocm_hip{}, op_name, _ts, @@ -775,6 +805,36 @@ hip_api_callback(uint32_t domain, uint32_t cid, const void* callback_data, void* } } } + + if(_enable_backtraces && _bt_data && !_bt_data->empty()) + { + const std::string _unk = "??"; + size_t _bt_cnt = 0; + for(const auto& itr : *_bt_data) + { + const auto* _func = + (itr.name.empty()) ? &_unk : &itr.name; + const auto* _loc = + (itr.location.empty()) ? &_unk : &itr.location; + auto _line = (itr.lineno == 0) ? std::string{ "?" } + : join("", itr.lineno); + auto _entry = join("", demangle(*_func), " @ ", + join(':', *_loc, _line)); + if(_bt_cnt < 10) + { + // Prepend zero for better ordering in UI. + // Only one zero is ever necessary since stack depth + // is limited to 16. + tracing::add_perfetto_annotation( + ctx, join("", "frame#0", _bt_cnt++), _entry); + } + else + { + tracing::add_perfetto_annotation( + ctx, join("", "frame#", _bt_cnt++), _entry); + } + } + } } }); }