File indexing completed on 2026-07-26 08:22:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #if __has_include(<cxxabi.h>)
0029 #define HAVE_CXXABI 1
0030 #include <cxxabi.h>
0031 #endif
0032
0033 #include <cstring>
0034 #include <memory>
0035 #include <optional>
0036 #include <string>
0037
0038 #include <dlfcn.h>
0039
0040 #include "nvtx3/nvToolsExt.h"
0041
0042 namespace {
0043 uint32_t __attribute__((no_instrument_function)) djb2(const std::string &str) {
0044 uint32_t hash = 5381u;
0045
0046 for (auto &chr : str) {
0047 hash = 33u * hash + static_cast<uint32_t>(chr);
0048 }
0049
0050 return hash;
0051 }
0052
0053 void __attribute__((no_instrument_function)) nvtxRangePushWrapper(
0054 const std::optional<std::string> &name) {
0055 nvtxEventAttributes_t eventAttrib;
0056 std::memset(&eventAttrib, 0, sizeof(nvtxEventAttributes_t));
0057
0058 eventAttrib.version = NVTX_VERSION;
0059 eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
0060
0061 if (name) {
0062 eventAttrib.colorType = NVTX_COLOR_ARGB;
0063 eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
0064 eventAttrib.color = 0xFF000000 | (djb2(*name) & 0x00FFFFFF);
0065 eventAttrib.message.ascii = name->c_str();
0066 } else {
0067 eventAttrib.colorType = NVTX_COLOR_UNKNOWN;
0068 eventAttrib.messageType = NVTX_MESSAGE_UNKNOWN;
0069 }
0070
0071 nvtxRangePushEx(&eventAttrib);
0072 }
0073 }
0074
0075 extern "C" {
0076 void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
0077 [[maybe_unused]] void *this_fn, void *) {
0078 Dl_info this_fn_info;
0079
0080 if (dladdr(this_fn, &this_fn_info)) {
0081 #ifdef HAVE_CXXABI
0082 int status = 0;
0083 std::unique_ptr<char[]> fname(
0084 abi::__cxa_demangle(this_fn_info.dli_sname, nullptr, nullptr, &status));
0085
0086 if (status == 0 && fname) {
0087 nvtxRangePushWrapper(fname.get());
0088 } else {
0089 nvtxRangePushWrapper({});
0090 }
0091 #else
0092 nvtxRangePushWrapper(this_fn_info.dli_sname);
0093 #endif
0094
0095 } else {
0096 nvtxRangePushWrapper({});
0097 }
0098 }
0099
0100 void __attribute__((no_instrument_function)) __cyg_profile_func_exit(void *,
0101 void *) {
0102 nvtxRangePop();
0103 }
0104 }