Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:11

0001 /* Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
0002  *
0003  * Redistribution and use in source and binary forms, with or without
0004  * modification, are permitted provided that the following conditions
0005  * are met:
0006  *  * Redistributions of source code must retain the above copyright
0007  *    notice, this list of conditions and the following disclaimer.
0008  *  * Redistributions in binary form must reproduce the above copyright
0009  *    notice, this list of conditions and the following disclaimer in the
0010  *    documentation and/or other materials provided with the distribution.
0011  *  * Neither the name of NVIDIA CORPORATION nor the names of its
0012  *    contributors may be used to endorse or promote products derived
0013  *    from this software without specific prior written permission.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
0016  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0018  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0023  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }  // namespace
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 }