10b57cec5SDimitry Andric //===-- Signposts.cpp - Interval debug annotations ------------------------===//
20b57cec5SDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/Support/Signposts.h"
1081ad6265SDimitry Andric #include "llvm/ADT/StringRef.h"
11349cc55cSDimitry Andric #include "llvm/Config/config.h"
1281ad6265SDimitry Andric 
130b57cec5SDimitry Andric #if LLVM_SUPPORT_XCODE_SIGNPOSTS
140b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
15e8d8bef9SDimitry Andric #include "llvm/Support/Mutex.h"
16349cc55cSDimitry Andric #include <Availability.h>
17349cc55cSDimitry Andric #include <os/signpost.h>
18349cc55cSDimitry Andric #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #if LLVM_SUPPORT_XCODE_SIGNPOSTS
23349cc55cSDimitry Andric #define SIGNPOSTS_AVAILABLE()                                                  \
24349cc55cSDimitry Andric   __builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *)
250b57cec5SDimitry Andric namespace {
LogCreator()260b57cec5SDimitry Andric os_log_t *LogCreator() {
270b57cec5SDimitry Andric   os_log_t *X = new os_log_t;
2881ad6265SDimitry Andric   *X = os_log_create("org.llvm.signposts", "toolchain");
290b57cec5SDimitry Andric   return X;
300b57cec5SDimitry Andric }
31fe6060f1SDimitry Andric struct LogDeleter {
operator ()__anonea4767ce0111::LogDeleter32fe6060f1SDimitry Andric   void operator()(os_log_t *X) const {
330b57cec5SDimitry Andric     os_release(*X);
340b57cec5SDimitry Andric     delete X;
350b57cec5SDimitry Andric   }
36fe6060f1SDimitry Andric };
370b57cec5SDimitry Andric } // end anonymous namespace
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace llvm {
400b57cec5SDimitry Andric class SignpostEmitterImpl {
41fe6060f1SDimitry Andric   using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>;
42349cc55cSDimitry Andric   using LogTy = LogPtrTy::element_type;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   LogPtrTy SignpostLog;
45e8d8bef9SDimitry Andric   DenseMap<const void *, os_signpost_id_t> Signposts;
46e8d8bef9SDimitry Andric   sys::SmartMutex<true> Mutex;
470b57cec5SDimitry Andric 
getLogger() const48349cc55cSDimitry Andric   LogTy &getLogger() const { return *SignpostLog; }
getSignpostForObject(const void * O)49e8d8bef9SDimitry Andric   os_signpost_id_t getSignpostForObject(const void *O) {
50e8d8bef9SDimitry Andric     sys::SmartScopedLock<true> Lock(Mutex);
51e8d8bef9SDimitry Andric     const auto &I = Signposts.find(O);
520b57cec5SDimitry Andric     if (I != Signposts.end())
530b57cec5SDimitry Andric       return I->second;
54fe6060f1SDimitry Andric     os_signpost_id_t ID = {};
55fe6060f1SDimitry Andric     if (SIGNPOSTS_AVAILABLE()) {
56fe6060f1SDimitry Andric       ID = os_signpost_id_make_with_pointer(getLogger(), O);
57fe6060f1SDimitry Andric     }
58fe6060f1SDimitry Andric     const auto &Inserted = Signposts.insert(std::make_pair(O, ID));
590b57cec5SDimitry Andric     return Inserted.first->second;
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
62349cc55cSDimitry Andric public:
SignpostEmitterImpl()63fe6060f1SDimitry Andric   SignpostEmitterImpl() : SignpostLog(LogCreator()) {}
640b57cec5SDimitry Andric 
isEnabled() const65fe6060f1SDimitry Andric   bool isEnabled() const {
66fe6060f1SDimitry Andric     if (SIGNPOSTS_AVAILABLE())
67fe6060f1SDimitry Andric       return os_signpost_enabled(*SignpostLog);
68fe6060f1SDimitry Andric     return false;
69fe6060f1SDimitry Andric   }
700b57cec5SDimitry Andric 
startInterval(const void * O,llvm::StringRef Name)71e8d8bef9SDimitry Andric   void startInterval(const void *O, llvm::StringRef Name) {
720b57cec5SDimitry Andric     if (isEnabled()) {
73fe6060f1SDimitry Andric       if (SIGNPOSTS_AVAILABLE()) {
74e8d8bef9SDimitry Andric         // Both strings used here are required to be constant literal strings.
75e8d8bef9SDimitry Andric         os_signpost_interval_begin(getLogger(), getSignpostForObject(O),
76fe6060f1SDimitry Andric                                    "LLVM Timers", "%s", Name.data());
77fe6060f1SDimitry Andric       }
780b57cec5SDimitry Andric     }
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
endInterval(const void * O,llvm::StringRef Name)81349cc55cSDimitry Andric   void endInterval(const void *O, llvm::StringRef Name) {
820b57cec5SDimitry Andric     if (isEnabled()) {
83fe6060f1SDimitry Andric       if (SIGNPOSTS_AVAILABLE()) {
84e8d8bef9SDimitry Andric         // Both strings used here are required to be constant literal strings.
85e8d8bef9SDimitry Andric         os_signpost_interval_end(getLogger(), getSignpostForObject(O),
86fe6060f1SDimitry Andric                                  "LLVM Timers", "");
87fe6060f1SDimitry Andric       }
880b57cec5SDimitry Andric     }
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric };
910b57cec5SDimitry Andric } // end namespace llvm
92fe6060f1SDimitry Andric #else
93fe6060f1SDimitry Andric /// Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl.
94fe6060f1SDimitry Andric class llvm::SignpostEmitterImpl {};
950b57cec5SDimitry Andric #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric #if LLVM_SUPPORT_XCODE_SIGNPOSTS
980b57cec5SDimitry Andric #define HAVE_ANY_SIGNPOST_IMPL 1
998bcb0991SDimitry Andric #else
1008bcb0991SDimitry Andric #define HAVE_ANY_SIGNPOST_IMPL 0
1010b57cec5SDimitry Andric #endif
1020b57cec5SDimitry Andric 
SignpostEmitter()1030b57cec5SDimitry Andric SignpostEmitter::SignpostEmitter() {
1040b57cec5SDimitry Andric #if HAVE_ANY_SIGNPOST_IMPL
105fe6060f1SDimitry Andric   Impl = std::make_unique<SignpostEmitterImpl>();
1060b57cec5SDimitry Andric #endif // if !HAVE_ANY_SIGNPOST_IMPL
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
109fe6060f1SDimitry Andric SignpostEmitter::~SignpostEmitter() = default;
1100b57cec5SDimitry Andric 
isEnabled() const1110b57cec5SDimitry Andric bool SignpostEmitter::isEnabled() const {
1120b57cec5SDimitry Andric #if HAVE_ANY_SIGNPOST_IMPL
1130b57cec5SDimitry Andric   return Impl->isEnabled();
1140b57cec5SDimitry Andric #else
1150b57cec5SDimitry Andric   return false;
1160b57cec5SDimitry Andric #endif // if !HAVE_ANY_SIGNPOST_IMPL
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
startInterval(const void * O,StringRef Name)119e8d8bef9SDimitry Andric void SignpostEmitter::startInterval(const void *O, StringRef Name) {
1200b57cec5SDimitry Andric #if HAVE_ANY_SIGNPOST_IMPL
1210b57cec5SDimitry Andric   if (Impl == nullptr)
1220b57cec5SDimitry Andric     return;
123e8d8bef9SDimitry Andric   return Impl->startInterval(O, Name);
1240b57cec5SDimitry Andric #endif // if !HAVE_ANY_SIGNPOST_IMPL
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric 
endInterval(const void * O,StringRef Name)127349cc55cSDimitry Andric void SignpostEmitter::endInterval(const void *O, StringRef Name) {
1280b57cec5SDimitry Andric #if HAVE_ANY_SIGNPOST_IMPL
1290b57cec5SDimitry Andric   if (Impl == nullptr)
1300b57cec5SDimitry Andric     return;
131349cc55cSDimitry Andric   Impl->endInterval(O, Name);
1320b57cec5SDimitry Andric #endif // if !HAVE_ANY_SIGNPOST_IMPL
1330b57cec5SDimitry Andric }
134