1 //===-- Signposts.cpp - Interval debug annotations ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/Signposts.h" 10 11 #include "llvm/Config/config.h" 12 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/Support/Mutex.h" 15 #include <Availability.h> 16 #include <os/signpost.h> 17 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS 18 19 using namespace llvm; 20 21 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 22 #define SIGNPOSTS_AVAILABLE() \ 23 __builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *) 24 namespace { 25 os_log_t *LogCreator() { 26 os_log_t *X = new os_log_t; 27 *X = os_log_create("org.llvm.signposts", OS_LOG_CATEGORY_POINTS_OF_INTEREST); 28 return X; 29 } 30 struct LogDeleter { 31 void operator()(os_log_t *X) const { 32 os_release(*X); 33 delete X; 34 } 35 }; 36 } // end anonymous namespace 37 38 namespace llvm { 39 class SignpostEmitterImpl { 40 using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>; 41 using LogTy = LogPtrTy::element_type; 42 43 LogPtrTy SignpostLog; 44 DenseMap<const void *, os_signpost_id_t> Signposts; 45 sys::SmartMutex<true> Mutex; 46 47 LogTy &getLogger() const { return *SignpostLog; } 48 os_signpost_id_t getSignpostForObject(const void *O) { 49 sys::SmartScopedLock<true> Lock(Mutex); 50 const auto &I = Signposts.find(O); 51 if (I != Signposts.end()) 52 return I->second; 53 os_signpost_id_t ID = {}; 54 if (SIGNPOSTS_AVAILABLE()) { 55 ID = os_signpost_id_make_with_pointer(getLogger(), O); 56 } 57 const auto &Inserted = Signposts.insert(std::make_pair(O, ID)); 58 return Inserted.first->second; 59 } 60 61 public: 62 SignpostEmitterImpl() : SignpostLog(LogCreator()) {} 63 64 bool isEnabled() const { 65 if (SIGNPOSTS_AVAILABLE()) 66 return os_signpost_enabled(*SignpostLog); 67 return false; 68 } 69 70 void startInterval(const void *O, llvm::StringRef Name) { 71 if (isEnabled()) { 72 if (SIGNPOSTS_AVAILABLE()) { 73 // Both strings used here are required to be constant literal strings. 74 os_signpost_interval_begin(getLogger(), getSignpostForObject(O), 75 "LLVM Timers", "%s", Name.data()); 76 } 77 } 78 } 79 80 void endInterval(const void *O, llvm::StringRef Name) { 81 if (isEnabled()) { 82 if (SIGNPOSTS_AVAILABLE()) { 83 // Both strings used here are required to be constant literal strings. 84 os_signpost_interval_end(getLogger(), getSignpostForObject(O), 85 "LLVM Timers", ""); 86 } 87 } 88 } 89 }; 90 } // end namespace llvm 91 #else 92 /// Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl. 93 class llvm::SignpostEmitterImpl {}; 94 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS 95 96 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 97 #define HAVE_ANY_SIGNPOST_IMPL 1 98 #else 99 #define HAVE_ANY_SIGNPOST_IMPL 0 100 #endif 101 102 SignpostEmitter::SignpostEmitter() { 103 #if HAVE_ANY_SIGNPOST_IMPL 104 Impl = std::make_unique<SignpostEmitterImpl>(); 105 #endif // if !HAVE_ANY_SIGNPOST_IMPL 106 } 107 108 SignpostEmitter::~SignpostEmitter() = default; 109 110 bool SignpostEmitter::isEnabled() const { 111 #if HAVE_ANY_SIGNPOST_IMPL 112 return Impl->isEnabled(); 113 #else 114 return false; 115 #endif // if !HAVE_ANY_SIGNPOST_IMPL 116 } 117 118 void SignpostEmitter::startInterval(const void *O, StringRef Name) { 119 #if HAVE_ANY_SIGNPOST_IMPL 120 if (Impl == nullptr) 121 return; 122 return Impl->startInterval(O, Name); 123 #endif // if !HAVE_ANY_SIGNPOST_IMPL 124 } 125 126 void SignpostEmitter::endInterval(const void *O, StringRef Name) { 127 #if HAVE_ANY_SIGNPOST_IMPL 128 if (Impl == nullptr) 129 return; 130 Impl->endInterval(O, Name); 131 #endif // if !HAVE_ANY_SIGNPOST_IMPL 132 } 133