xref: /aosp_15_r20/external/tensorflow/tensorflow/stream_executor/rocm/roctracer_wrapper.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // This file wraps roctracer API calls with dso loader so that we don't need to
17 // have explicit linking to libroctracer. All TF hipsarse API usage should route
18 // through this wrapper.
19 
20 #ifndef TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCTRACER_WRAPPER_H_
21 #define TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCTRACER_WRAPPER_H_
22 
23 #include "rocm/include/roctracer/roctracer.h"
24 #include "rocm/include/roctracer/roctracer_hcc.h"
25 #include "rocm/include/roctracer/roctracer_hip.h"
26 #include "tensorflow/stream_executor/lib/env.h"
27 #include "tensorflow/stream_executor/platform/dso_loader.h"
28 #include "tensorflow/stream_executor/platform/port.h"
29 
30 namespace tensorflow {
31 namespace wrap {
32 
33 #ifdef PLATFORM_GOOGLE
34 
35 #define ROCTRACER_API_WRAPPER(API_NAME)                          \
36   template <typename... Args>                                    \
37   auto API_NAME()(Args... args)->decltype(::API_NAME(args...)) { \
38     return ::API_NAME(args...);                                  \
39   }
40 
41 #else
42 
43 #define ROCTRACER_API_WRAPPER(API_NAME)                                       \
44   template <typename... Args>                                                 \
45   auto API_NAME(Args... args)->decltype(::API_NAME(args...)) {                \
46     using FuncPtrT = std::add_pointer<decltype(::API_NAME)>::type;            \
47     static FuncPtrT loaded = []() -> FuncPtrT {                               \
48       static const char* kName = #API_NAME;                                   \
49       void* f;                                                                \
50       auto s = Env::Default()->GetSymbolFromLibrary(                          \
51           stream_executor::internal::CachedDsoLoader::GetRoctracerDsoHandle() \
52               .ValueOrDie(),                                                  \
53           kName, &f);                                                         \
54       CHECK(s.ok()) << "could not find " << kName                             \
55                     << " in roctracer DSO; dlerror: " << s.error_message();   \
56       return reinterpret_cast<FuncPtrT>(f);                                   \
57     }();                                                                      \
58     return loaded(args...);                                                   \
59   }
60 
61 #endif  // PLATFORM_GOOGLE
62 
63 #define FOREACH_ROCTRACER_API(DO_FUNC)           \
64   DO_FUNC(roctracer_default_pool_expl)           \
65   DO_FUNC(roctracer_disable_domain_activity)     \
66   DO_FUNC(roctracer_disable_domain_callback)     \
67   DO_FUNC(roctracer_disable_op_activity)         \
68   DO_FUNC(roctracer_disable_op_callback)         \
69   DO_FUNC(roctracer_enable_domain_activity_expl) \
70   DO_FUNC(roctracer_enable_domain_callback)      \
71   DO_FUNC(roctracer_enable_op_activity_expl)     \
72   DO_FUNC(roctracer_enable_op_callback)          \
73   DO_FUNC(roctracer_error_string)                \
74   DO_FUNC(roctracer_flush_activity_expl)         \
75   DO_FUNC(roctracer_get_timestamp)               \
76   DO_FUNC(roctracer_op_string)                   \
77   DO_FUNC(roctracer_open_pool_expl)              \
78   DO_FUNC(roctracer_set_properties)
79 
80 FOREACH_ROCTRACER_API(ROCTRACER_API_WRAPPER)
81 
82 #undef FOREACH_ROCTRACER_API
83 #undef ROCTRACER_API_WRAPPER
84 
85 }  // namespace wrap
86 }  // namespace tensorflow
87 
88 #endif  // TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCTRACER_WRAPPER_H_
89