OmniSciDB  471d68cefb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nvtx_helpers.cpp
Go to the documentation of this file.
1 #include "Shared/nvtx_helpers.h"
2 
3 #include <array>
4 #include <sstream>
5 
6 #ifdef _WIN32
7 #include <processthreadsapi.h>
8 #else // _WIN32
9 #include <sys/syscall.h>
10 #include <unistd.h>
11 #endif // _WIN32
12 
13 #include <boost/filesystem/path.hpp>
14 
15 #include "Logger/Logger.h"
16 
17 namespace nvtx_helpers {
18 struct CategoryInfo {
19  uint32_t category;
20  uint32_t color;
21 };
22 
23 #define NUM_CATEGORIES 4u
24 static std::array<CategoryInfo, NUM_CATEGORIES> g_category_infos = {};
26 
27 // Control whether or not to set the event category when using the Omnisci domain
28 // Setting the category can clutter the timeline event names. Colors will still be
29 // used though so the visual organization remains
30 #define SET_OMNISCI_EVENT_CATEGORY 0
31 
32 void init() {
33  // The domain will be null if NVTX profiling is not enabled by the nsys launcher
34  g_omnisci_domain = nvtxDomainCreateA("Omnisci");
35  if (g_omnisci_domain) {
36  auto create_category = [](Category c, const char* name, uint32_t color) {
37  auto category_index = static_cast<uint32_t>(c);
38  CHECK_LT(category_index, NUM_CATEGORIES);
39  g_category_infos[category_index] = {category_index, color};
40  nvtxDomainNameCategoryA(g_omnisci_domain, category_index, name);
41  };
42  // skip category 0 (none), as trying to modify it triggers errors during nsight
43  // analysis
44  create_category(Category::kDebugTimer, "DebugTimer", 0xFFB0E0E6);
45  create_category(Category::kQueryStateTimer, "QueryStateTimer", 0xFF98FB98);
46  create_category(Category::kRenderLogger, "RenderLogger", 0xFFEE8AA);
47  }
48 }
49 
50 void shutdown() {
51  if (g_omnisci_domain) {
52  nvtxDomainDestroy(g_omnisci_domain);
53  g_omnisci_domain = nullptr;
54  }
55 }
56 
58  return g_omnisci_domain;
59 }
60 
61 namespace {
62 inline nvtxEventAttributes_t make_omnisci_event(Category c, const char* name) {
63  auto category_index = static_cast<uint32_t>(c);
64  CHECK_LT(category_index, NUM_CATEGORIES);
65  auto const& info = g_category_infos[category_index];
66  nvtxEventAttributes_t event_attrib = {};
67  event_attrib.version = NVTX_VERSION;
68  event_attrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
69  event_attrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
70  event_attrib.message.ascii = name;
71  if (info.color != 0) {
72  event_attrib.colorType = NVTX_COLOR_ARGB;
73  event_attrib.color = info.color;
74  }
75 #if SET_OMNISCI_EVENT_CATEGORY
76  event_attrib.category = info.category;
77 #endif
78  return event_attrib;
79 }
80 
81 // Return last component of path
82 std::string filename(char const* path) {
83  return boost::filesystem::path(path).filename().string();
84 }
85 } // namespace
86 
87 void omnisci_range_push(Category c, const char* name, const char* file) {
88  if (g_omnisci_domain) {
89  if (file) {
90  std::stringstream ss;
91  ss << name;
92  ss << " | " << filename(file);
93  auto str = ss.str();
94  auto event = make_omnisci_event(c, str.c_str());
95  nvtxDomainRangePushEx(g_omnisci_domain, &event);
96  } else {
97  auto event = make_omnisci_event(c, name);
98  nvtxDomainRangePushEx(g_omnisci_domain, &event);
99  }
100  }
101 }
102 
104  if (g_omnisci_domain) {
105  nvtxDomainRangePop(g_omnisci_domain);
106  }
107 }
108 
110  if (g_omnisci_domain) {
111  auto event = make_omnisci_event(c, name);
112  return nvtxDomainRangeStartEx(g_omnisci_domain, &event);
113  } else {
114  return nvtxRangeId_t{};
115  }
116 }
117 
119  if (g_omnisci_domain) {
120  nvtxDomainRangeEnd(g_omnisci_domain, r);
121  }
122 }
123 
124 void omnisci_set_mark(Category c, const char* name) {
125  if (g_omnisci_domain) {
126  auto event = make_omnisci_event(c, name);
127  nvtxDomainMarkEx(g_omnisci_domain, &event);
128  }
129 }
130 
131 void name_current_thread(const char* name) {
132 #ifdef _WIN32
133  uint32_t thread_id = GetCurrentThreadId();
134 #else
135 #ifdef SYS_gettid
136  uint32_t thread_id = static_cast<uint32_t>(syscall(SYS_gettid));
137 #else // SYS_gettid
138 #error "SYS_gettid unavailable on this system"
139 #endif // SYS_gettid
140 #endif // _WIN32
141  nvtxNameOsThreadA(thread_id, name);
142 }
143 
144 } // namespace nvtx_helpers
const nvtxDomainHandle_t get_omnisci_domain()
void omnisci_range_end(nvtxRangeId_t r)
void omnisci_range_pop()
string name
Definition: setup.in.py:72
void omnisci_range_push(Category c, const char *name, const char *file)
static std::array< CategoryInfo, NUM_CATEGORIES > g_category_infos
static nvtxDomainHandle_t g_omnisci_domain
void name_current_thread(const char *name)
#define CHECK_LT(x, y)
Definition: Logger.h:219
void omnisci_set_mark(Category c, const char *name)
uint64_t nvtxRangeId_t
Definition: nvtx_helpers.h:26
ThreadId thread_id()
Definition: Logger.cpp:791
nvtxEventAttributes_t make_omnisci_event(Category c, const char *name)
#define NUM_CATEGORIES
nvtxRangeId_t omnisci_range_start(Category c, const char *name)
void * nvtxDomainHandle_t
Definition: nvtx_helpers.h:25