From 66eb30e5dd96d90bc0d3b56fa1df79dbfb784d37 Mon Sep 17 00:00:00 2001 From: "Jonathan R. Madsen" Date: Wed, 13 Dec 2023 14:18:01 -0600 Subject: [PATCH 1/3] Update lib/core/config.cpp - Add OMNITRACE_ROCTRACER_HIP_API_BACKTRACE option --- source/lib/core/config.cpp | 5 +++++ 1 file changed, 5 insertions(+) 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"); From c8f66c4e0008e86cc7b0e8cd0b943c58e84162a6 Mon Sep 17 00:00:00 2001 From: "Jonathan R. Madsen" Date: Wed, 13 Dec 2023 14:19:45 -0600 Subject: [PATCH 2/3] Update lib/omnitrace/library/roctracer.cpp - support perfetto debug annotation of backtrace in HIP API call --- source/lib/omnitrace/library/roctracer.cpp | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/source/lib/omnitrace/library/roctracer.cpp b/source/lib/omnitrace/library/roctracer.cpp index 7ce7bf144..16fa040a7 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,25 @@ 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); + tracing::add_perfetto_annotation( + ctx, join("", "frame#", _bt_cnt++), + join("", demangle(*_func), " @ ", + join(':', *_loc, _line))); + } + } } }); } From a6b169d37c86cc4adc81872aeed5276427820be9 Mon Sep 17 00:00:00 2001 From: Tal Ben-Nun Date: Wed, 10 Jan 2024 01:20:44 -0800 Subject: [PATCH 3/3] Fix backtrace resolution and ordering in UI (#1) * Fix backtrace resolution for non-omnitrace libraries * Nicer Perfetto UI on long backtraces * Make Perfetto annotation consistent * clang-format --- source/lib/omnitrace/library/roctracer.cpp | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/source/lib/omnitrace/library/roctracer.cpp b/source/lib/omnitrace/library/roctracer.cpp index 16fa040a7..8a4632622 100644 --- a/source/lib/omnitrace/library/roctracer.cpp +++ b/source/lib/omnitrace/library/roctracer.cpp @@ -757,7 +757,7 @@ hip_api_callback(uint32_t domain, uint32_t cid, const void* callback_data, void* { if(itr) { - if(auto _val = binary::lookup_ipaddr_entry(itr->address()); + if(auto _val = binary::lookup_ipaddr_entry(itr->address()); _val) { _bt_data->emplace_back(std::move(*_val)); @@ -816,12 +816,23 @@ hip_api_callback(uint32_t domain, uint32_t cid, const void* callback_data, void* (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); - tracing::add_perfetto_annotation( - ctx, join("", "frame#", _bt_cnt++), - join("", demangle(*_func), " @ ", - join(':', *_loc, _line))); + 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); + } } } }