1 /* Copyright 2015 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 #include "tensorflow/core/platform/default/gpu/cupti_wrapper.h" 17 18 #if GOOGLE_CUDA 19 20 #include <string> 21 22 #include "tensorflow/core/platform/env.h" 23 #include "tensorflow/core/platform/stream_executor.h" 24 25 namespace perftools { 26 namespace gputools { 27 namespace profiler { 28 29 namespace dynload { 30 31 #define LIBCUPTI_WRAP(__name) \ 32 struct DynLoadShim__##__name { \ 33 static const char* kName; \ 34 using FuncPointerT = std::add_pointer<decltype(::__name)>::type; \ 35 static void* GetDsoHandle() { \ 36 static auto status = perftools::gputools::internal::CachedDsoLoader:: \ 37 GetLibcuptiDsoHandle(); \ 38 return status.ValueOrDie(); \ 39 } \ 40 static FuncPointerT DynLoad() { \ 41 static void* f; \ 42 TF_CHECK_OK(::tensorflow::Env::Default()->GetSymbolFromLibrary( \ 43 GetDsoHandle(), kName, &f)) \ 44 << "could not find " << kName << "in libcupti DSO"; \ 45 return reinterpret_cast<FuncPointerT>(f); \ 46 } \ 47 template <typename... Args> \ 48 CUptiResult operator()(Args... args) { \ 49 return DynLoad()(args...); \ 50 } \ 51 } __name; \ 52 const char* DynLoadShim__##__name::kName = #__name; 53 54 LIBCUPTI_WRAP(cuptiActivityDisable); 55 LIBCUPTI_WRAP(cuptiActivityEnable); 56 LIBCUPTI_WRAP(cuptiActivityFlushAll); 57 LIBCUPTI_WRAP(cuptiActivityGetNextRecord); 58 LIBCUPTI_WRAP(cuptiActivityGetNumDroppedRecords); 59 LIBCUPTI_WRAP(cuptiActivityRegisterCallbacks); 60 LIBCUPTI_WRAP(cuptiGetTimestamp); 61 LIBCUPTI_WRAP(cuptiEnableCallback); 62 LIBCUPTI_WRAP(cuptiEnableDomain); 63 LIBCUPTI_WRAP(cuptiSubscribe); 64 LIBCUPTI_WRAP(cuptiUnsubscribe); 65 66 } // namespace dynload 67 68 CUptiResult CuptiWrapper::ActivityDisable(CUpti_ActivityKind kind) { 69 return dynload::cuptiActivityDisable(kind); 70 } 71 72 CUptiResult CuptiWrapper::ActivityEnable(CUpti_ActivityKind kind) { 73 return dynload::cuptiActivityEnable(kind); 74 } 75 76 CUptiResult CuptiWrapper::ActivityFlushAll(uint32_t flag) { 77 return dynload::cuptiActivityFlushAll(flag); 78 } 79 80 CUptiResult CuptiWrapper::ActivityGetNextRecord(uint8_t* buffer, 81 size_t valid_buffer_size_bytes, 82 CUpti_Activity** record) { 83 return dynload::cuptiActivityGetNextRecord(buffer, valid_buffer_size_bytes, 84 record); 85 } 86 87 CUptiResult CuptiWrapper::ActivityGetNumDroppedRecords(CUcontext context, 88 uint32_t stream_id, 89 size_t* dropped) { 90 return dynload::cuptiActivityGetNumDroppedRecords(context, stream_id, 91 dropped); 92 } 93 94 CUptiResult CuptiWrapper::ActivityRegisterCallbacks( 95 CUpti_BuffersCallbackRequestFunc func_buffer_requested, 96 CUpti_BuffersCallbackCompleteFunc func_buffer_completed) { 97 return dynload::cuptiActivityRegisterCallbacks(func_buffer_requested, 98 func_buffer_completed); 99 } 100 101 CUptiResult CuptiWrapper::GetTimestamp(uint64_t* timestamp) { 102 return dynload::cuptiGetTimestamp(timestamp); 103 } 104 105 CUptiResult CuptiWrapper::EnableCallback(uint32_t enable, 106 CUpti_SubscriberHandle subscriber, 107 CUpti_CallbackDomain domain, 108 CUpti_CallbackId cbid) { 109 return dynload::cuptiEnableCallback(enable, subscriber, domain, cbid); 110 } 111 112 CUptiResult CuptiWrapper::EnableDomain(uint32_t enable, 113 CUpti_SubscriberHandle subscriber, 114 CUpti_CallbackDomain domain) { 115 return dynload::cuptiEnableDomain(enable, subscriber, domain); 116 } 117 118 CUptiResult CuptiWrapper::Subscribe(CUpti_SubscriberHandle* subscriber, 119 CUpti_CallbackFunc callback, 120 void* userdata) { 121 return dynload::cuptiSubscribe(subscriber, callback, userdata); 122 } 123 124 CUptiResult CuptiWrapper::Unsubscribe(CUpti_SubscriberHandle subscriber) { 125 return dynload::cuptiUnsubscribe(subscriber); 126 } 127 128 } // namespace profiler 129 } // namespace gputools 130 } // namespace perftools 131 132 #endif // GOOGLE_CUDA 133