1 //===-- Signposts.cpp - Interval debug annotations ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Signposts.h"
11 #include "llvm/Support/Timer.h"
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
21 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
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
);
28 void LogDeleter(os_log_t
*X
) {
32 } // end anonymous namespace
35 class SignpostEmitterImpl
{
37 std::unique_ptr
<os_log_t
, std::function
<void(os_log_t
*)>>;
38 using LogTy
= LogPtrTy::element_type
;
41 DenseMap
<const Timer
*, os_signpost_id_t
> Signposts
;
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())
49 const auto &Inserted
= Signposts
.insert(
50 std::make_pair(T
, os_signpost_id_make_with_pointer(getLogger(), T
)));
51 return Inserted
.first
->second
;
55 SignpostEmitterImpl() : SignpostLog(LogCreator(), LogDeleter
), Signposts() {}
57 bool isEnabled() const { return os_signpost_enabled(*SignpostLog
); }
59 void startTimerInterval(Timer
*T
) {
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());
68 void endTimerInterval(Timer
*T
) {
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());
76 } // end namespace llvm
77 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
79 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
80 #define HAVE_ANY_SIGNPOST_IMPL 1
83 SignpostEmitter::SignpostEmitter() {
84 #if HAVE_ANY_SIGNPOST_IMPL
85 Impl
= new SignpostEmitterImpl();
86 #else // if HAVE_ANY_SIGNPOST_IMPL
88 #endif // if !HAVE_ANY_SIGNPOST_IMPL
91 SignpostEmitter::~SignpostEmitter() {
92 #if HAVE_ANY_SIGNPOST_IMPL
94 #endif // if HAVE_ANY_SIGNPOST_IMPL
97 bool SignpostEmitter::isEnabled() const {
98 #if HAVE_ANY_SIGNPOST_IMPL
99 return Impl
->isEnabled();
102 #endif // if !HAVE_ANY_SIGNPOST_IMPL
105 void SignpostEmitter::startTimerInterval(Timer
*T
) {
106 #if HAVE_ANY_SIGNPOST_IMPL
109 return Impl
->startTimerInterval(T
);
110 #endif // if !HAVE_ANY_SIGNPOST_IMPL
113 void SignpostEmitter::endTimerInterval(Timer
*T
) {
114 #if HAVE_ANY_SIGNPOST_IMPL
117 Impl
->endTimerInterval(T
);
118 #endif // if !HAVE_ANY_SIGNPOST_IMPL