1 /* Copyright 2019 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 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_CPU_ANNOTATION_STACK_H_ 16 #define TENSORFLOW_CORE_PROFILER_INTERNAL_CPU_ANNOTATION_STACK_H_ 17 18 #include <stddef.h> 19 20 #include <atomic> 21 #include <utility> 22 23 #include "absl/strings/str_cat.h" 24 #include "absl/strings/string_view.h" 25 #include "tensorflow/core/platform/macros.h" 26 #include "tensorflow/core/platform/types.h" 27 28 namespace tensorflow { 29 namespace profiler { 30 namespace internal { 31 32 // Whether annotations are enabled. 33 // Static atomic so Annotation::IsEnabled can be fast and non-blocking. 34 TF_EXPORT extern std::atomic<int> g_annotation_enabled; 35 36 } // namespace internal 37 38 // Backend for ScopedAnnotation. 39 class AnnotationStack { 40 public: 41 // Appends name to the annotation for the current thread and returns the 42 // original length of the annotation. 43 // Append name to the current annotation, separated by "::". 44 // The choice of separator "::" is based on characters not used by 45 // TensorFlow for its TensorOps. PushAnnotation(absl::string_view name)46 static size_t PushAnnotation(absl::string_view name) { 47 string* annotation_stack = ThreadAnnotationStack(); 48 size_t old_length = annotation_stack->size(); 49 if (old_length != 0) { 50 absl::StrAppend(annotation_stack, "::", name); 51 } else { 52 *annotation_stack = string(name); 53 } 54 return old_length; 55 } 56 PushAnnotation(string && name)57 static size_t PushAnnotation(string&& name) { 58 string* annotation_stack = ThreadAnnotationStack(); 59 size_t old_length = annotation_stack->size(); 60 if (old_length != 0) { 61 absl::StrAppend(annotation_stack, "::", name); 62 } else { 63 *annotation_stack = std::move(name); 64 } 65 return old_length; 66 } 67 68 // Returns the annotation stack for the current thread. Get()69 static const string& Get() { return *ThreadAnnotationStack(); } 70 71 // Resizes the annotation stack for the current thread to its old length. PopAnnotation(size_t old_length)72 static void PopAnnotation(size_t old_length) { 73 ThreadAnnotationStack()->resize(old_length); 74 } 75 Enable(bool enable)76 static void Enable(bool enable) { 77 internal::g_annotation_enabled.store(enable, std::memory_order_release); 78 } 79 IsEnabled()80 static bool IsEnabled() { 81 return internal::g_annotation_enabled.load(std::memory_order_acquire); 82 } 83 84 private: 85 AnnotationStack() = default; 86 87 TF_DISALLOW_COPY_AND_ASSIGN(AnnotationStack); 88 89 // Returns a reference to the annotation for the current thread. 90 static string* ThreadAnnotationStack(); 91 }; 92 93 } // namespace profiler 94 } // namespace tensorflow 95 96 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_CPU_ANNOTATION_STACK_H_ 97