1 /* Copyright 2020 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 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_GPU_NVTX_UTILS_H_ 17 #define TENSORFLOW_CORE_PROFILER_INTERNAL_GPU_NVTX_UTILS_H_ 18 19 #include <stack> 20 21 #include "absl/strings/string_view.h" 22 #include "tensorflow/core/platform/macros.h" 23 24 namespace tensorflow { 25 namespace profiler { 26 27 /*** 28 * We have no intention to use NVTX in tensorflow right now, we use this class 29 * to track NVTX instrumentation inside NVIDIA libraries (such as TensorRT). 30 * This bears a lot of resemblance to ScopedAnnotation for now. In the future, 31 * we will use TraceMe to keep track trace context within a thread. 32 */ 33 class NVTXRangeTracker { 34 public: EnterRange(const std::string & range)35 static void EnterRange(const std::string& range) { 36 auto& range_stack = GetRangeStack(); 37 range_stack.push(range); 38 } ExitRange()39 static void ExitRange() { 40 auto& range_stack = GetRangeStack(); 41 if (!range_stack.empty()) range_stack.pop(); 42 } CurrentRange()43 static const absl::string_view CurrentRange() { 44 auto& range_stack = GetRangeStack(); 45 if (!range_stack.empty()) return range_stack.top(); 46 return ""; 47 } 48 49 private: 50 static std::stack<std::string>& GetRangeStack(); 51 52 TF_DISALLOW_COPY_AND_ASSIGN(NVTXRangeTracker); 53 }; 54 55 } // namespace profiler 56 } // namespace tensorflow 57 58 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_GPU_NVTX_UTILS_H_ 59