1 //===-- Signposts.cpp - Interval debug annotations ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Signposts.h" 11 #include "llvm/Support/Timer.h" 12 13 #include "llvm/Config/config.h" 14 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 15 #include "llvm/ADT/DenseMap.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 namespace { 23 os_log_t *LogCreator() { 24 os_log_t *X = new os_log_t; 25 *X = os_log_create("org.llvm.signposts", OS_LOG_CATEGORY_POINTS_OF_INTEREST); 26 return X; 27 } 28 void LogDeleter(os_log_t *X) { 29 os_release(*X); 30 delete X; 31 } 32 } // end anonymous namespace 33 34 namespace llvm { 35 class SignpostEmitterImpl { 36 using LogPtrTy = 37 std::unique_ptr<os_log_t, std::function<void(os_log_t *)>>; 38 using LogTy = LogPtrTy::element_type; 39 40 LogPtrTy SignpostLog; 41 DenseMap<const Timer *, os_signpost_id_t> Signposts; 42 43 LogTy &getLogger() const { return *SignpostLog; } 44 os_signpost_id_t getSignpostForTimer(const Timer *T) { 45 const auto &I = Signposts.find(T); 46 if (I != Signposts.end()) 47 return I->second; 48 49 const auto &Inserted = Signposts.insert( 50 std::make_pair(T, os_signpost_id_make_with_pointer(getLogger(), T))); 51 return Inserted.first->second; 52 } 53 54 public: 55 SignpostEmitterImpl() : SignpostLog(LogCreator(), LogDeleter), Signposts() {} 56 57 bool isEnabled() const { return os_signpost_enabled(*SignpostLog); } 58 59 void startTimerInterval(Timer *T) { 60 if (isEnabled()) { 61 // Both strings used here are required to be constant literal strings 62 os_signpost_interval_begin(getLogger(), getSignpostForTimer(T), 63 "Pass Timers", "Begin %s", 64 T->getName().c_str()); 65 } 66 } 67 68 void endTimerInterval(Timer *T) { 69 if (isEnabled()) { 70 // Both strings used here are required to be constant literal strings 71 os_signpost_interval_end(getLogger(), getSignpostForTimer(T), 72 "Pass Timers", "End %s", T->getName().c_str()); 73 } 74 } 75 }; 76 } // end namespace llvm 77 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS 78 79 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 80 #define HAVE_ANY_SIGNPOST_IMPL 1 81 #endif 82 83 SignpostEmitter::SignpostEmitter() { 84 #if HAVE_ANY_SIGNPOST_IMPL 85 Impl = new SignpostEmitterImpl(); 86 #else // if HAVE_ANY_SIGNPOST_IMPL 87 Impl = nullptr; 88 #endif // if !HAVE_ANY_SIGNPOST_IMPL 89 } 90 91 SignpostEmitter::~SignpostEmitter() { 92 #if HAVE_ANY_SIGNPOST_IMPL 93 delete Impl; 94 #endif // if HAVE_ANY_SIGNPOST_IMPL 95 } 96 97 bool SignpostEmitter::isEnabled() const { 98 #if HAVE_ANY_SIGNPOST_IMPL 99 return Impl->isEnabled(); 100 #else 101 return false; 102 #endif // if !HAVE_ANY_SIGNPOST_IMPL 103 } 104 105 void SignpostEmitter::startTimerInterval(Timer *T) { 106 #if HAVE_ANY_SIGNPOST_IMPL 107 if (Impl == nullptr) 108 return; 109 return Impl->startTimerInterval(T); 110 #endif // if !HAVE_ANY_SIGNPOST_IMPL 111 } 112 113 void SignpostEmitter::endTimerInterval(Timer *T) { 114 #if HAVE_ANY_SIGNPOST_IMPL 115 if (Impl == nullptr) 116 return; 117 Impl->endTimerInterval(T); 118 #endif // if !HAVE_ANY_SIGNPOST_IMPL 119 } 120