1 //===-- Signposts.cpp - Interval debug annotations ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Signposts.h"
10 #include "llvm/Support/Timer.h"
12 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/Support/Mutex.h"
19 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
21 os_log_t
*LogCreator() {
22 os_log_t
*X
= new os_log_t
;
23 *X
= os_log_create("org.llvm.signposts", OS_LOG_CATEGORY_POINTS_OF_INTEREST
);
27 void operator()(os_log_t
*X
) const {
32 } // end anonymous namespace
35 class SignpostEmitterImpl
{
36 using LogPtrTy
= std::unique_ptr
<os_log_t
, LogDeleter
>;
39 DenseMap
<const void *, os_signpost_id_t
> Signposts
;
40 sys::SmartMutex
<true> Mutex
;
43 os_log_t
&getLogger() const { return *SignpostLog
; }
44 os_signpost_id_t
getSignpostForObject(const void *O
) {
45 sys::SmartScopedLock
<true> Lock(Mutex
);
46 const auto &I
= Signposts
.find(O
);
47 if (I
!= Signposts
.end())
49 os_signpost_id_t ID
= {};
50 if (SIGNPOSTS_AVAILABLE()) {
51 ID
= os_signpost_id_make_with_pointer(getLogger(), O
);
53 const auto &Inserted
= Signposts
.insert(std::make_pair(O
, ID
));
54 return Inserted
.first
->second
;
57 SignpostEmitterImpl() : SignpostLog(LogCreator()) {}
59 bool isEnabled() const {
60 if (SIGNPOSTS_AVAILABLE())
61 return os_signpost_enabled(*SignpostLog
);
65 void startInterval(const void *O
, llvm::StringRef Name
) {
67 if (SIGNPOSTS_AVAILABLE()) {
68 // Both strings used here are required to be constant literal strings.
69 os_signpost_interval_begin(getLogger(), getSignpostForObject(O
),
70 "LLVM Timers", "%s", Name
.data());
75 void endInterval(const void *O
) {
77 if (SIGNPOSTS_AVAILABLE()) {
78 // Both strings used here are required to be constant literal strings.
79 os_signpost_interval_end(getLogger(), getSignpostForObject(O
),
85 } // end namespace llvm
87 /// Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl.
88 class llvm::SignpostEmitterImpl
{};
89 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
91 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
92 #define HAVE_ANY_SIGNPOST_IMPL 1
94 #define HAVE_ANY_SIGNPOST_IMPL 0
97 SignpostEmitter::SignpostEmitter() {
98 #if HAVE_ANY_SIGNPOST_IMPL
99 Impl
= std::make_unique
<SignpostEmitterImpl
>();
100 #endif // if !HAVE_ANY_SIGNPOST_IMPL
103 SignpostEmitter::~SignpostEmitter() = default;
105 bool SignpostEmitter::isEnabled() const {
106 #if HAVE_ANY_SIGNPOST_IMPL
107 return Impl
->isEnabled();
110 #endif // if !HAVE_ANY_SIGNPOST_IMPL
113 void SignpostEmitter::startInterval(const void *O
, StringRef Name
) {
114 #if HAVE_ANY_SIGNPOST_IMPL
117 return Impl
->startInterval(O
, Name
);
118 #endif // if !HAVE_ANY_SIGNPOST_IMPL
121 #if HAVE_ANY_SIGNPOST_IMPL
122 os_log_t
&SignpostEmitter::getLogger() const { return Impl
->getLogger(); }
123 os_signpost_id_t
SignpostEmitter::getSignpostForObject(const void *O
) {
124 return Impl
->getSignpostForObject(O
);
128 void SignpostEmitter::endInterval(const void *O
) {
129 #if HAVE_ANY_SIGNPOST_IMPL
132 Impl
->endInterval(O
);
133 #endif // if !HAVE_ANY_SIGNPOST_IMPL