LLVM  14.0.0git
Signposts.cpp
Go to the documentation of this file.
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 
10 #include "llvm/Support/Timer.h"
11 
12 #include "llvm/Config/config.h"
13 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/Support/Mutex.h"
16 #include <Availability.h>
17 #include <os/signpost.h>
18 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
19 
20 using namespace llvm;
21 
22 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
23 #define SIGNPOSTS_AVAILABLE() \
24  __builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *)
25 namespace {
26 os_log_t *LogCreator() {
27  os_log_t *X = new os_log_t;
28  *X = os_log_create("org.llvm.signposts", OS_LOG_CATEGORY_POINTS_OF_INTEREST);
29  return X;
30 }
31 struct LogDeleter {
32  void operator()(os_log_t *X) const {
33  os_release(*X);
34  delete X;
35  }
36 };
37 } // end anonymous namespace
38 
39 namespace llvm {
40 class SignpostEmitterImpl {
41  using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>;
42  using LogTy = LogPtrTy::element_type;
43 
44  LogPtrTy SignpostLog;
47 
48  LogTy &getLogger() const { return *SignpostLog; }
49  os_signpost_id_t getSignpostForObject(const void *O) {
51  const auto &I = Signposts.find(O);
52  if (I != Signposts.end())
53  return I->second;
54  os_signpost_id_t ID = {};
55  if (SIGNPOSTS_AVAILABLE()) {
56  ID = os_signpost_id_make_with_pointer(getLogger(), O);
57  }
58  const auto &Inserted = Signposts.insert(std::make_pair(O, ID));
59  return Inserted.first->second;
60  }
61 
62 public:
63  SignpostEmitterImpl() : SignpostLog(LogCreator()) {}
64 
65  bool isEnabled() const {
66  if (SIGNPOSTS_AVAILABLE())
67  return os_signpost_enabled(*SignpostLog);
68  return false;
69  }
70 
71  void startInterval(const void *O, llvm::StringRef Name) {
72  if (isEnabled()) {
73  if (SIGNPOSTS_AVAILABLE()) {
74  // Both strings used here are required to be constant literal strings.
75  os_signpost_interval_begin(getLogger(), getSignpostForObject(O),
76  "LLVM Timers", "%s", Name.data());
77  }
78  }
79  }
80 
81  void endInterval(const void *O, llvm::StringRef Name) {
82  if (isEnabled()) {
83  if (SIGNPOSTS_AVAILABLE()) {
84  // Both strings used here are required to be constant literal strings.
85  os_signpost_interval_end(getLogger(), getSignpostForObject(O),
86  "LLVM Timers", "");
87  }
88  }
89  }
90 };
91 } // end namespace llvm
92 #else
93 /// Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl.
95 #endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
96 
97 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
98 #define HAVE_ANY_SIGNPOST_IMPL 1
99 #else
100 #define HAVE_ANY_SIGNPOST_IMPL 0
101 #endif
102 
104 #if HAVE_ANY_SIGNPOST_IMPL
105  Impl = std::make_unique<SignpostEmitterImpl>();
106 #endif // if !HAVE_ANY_SIGNPOST_IMPL
107 }
108 
110 
112 #if HAVE_ANY_SIGNPOST_IMPL
113  return Impl->isEnabled();
114 #else
115  return false;
116 #endif // if !HAVE_ANY_SIGNPOST_IMPL
117 }
118 
120 #if HAVE_ANY_SIGNPOST_IMPL
121  if (Impl == nullptr)
122  return;
123  return Impl->startInterval(O, Name);
124 #endif // if !HAVE_ANY_SIGNPOST_IMPL
125 }
126 
128 #if HAVE_ANY_SIGNPOST_IMPL
129  if (Impl == nullptr)
130  return;
131  Impl->endInterval(O, Name);
132 #endif // if !HAVE_ANY_SIGNPOST_IMPL
133 }
Signposts
static ManagedStatic< SignpostEmitter > Signposts
Allows llvm::Timer to emit signposts when supported.
Definition: Timer.cpp:56
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
DenseMap.h
llvm::SignpostEmitter::~SignpostEmitter
~SignpostEmitter()
llvm::SignpostEmitter::startInterval
void startInterval(const void *O, StringRef Name)
Begin a signposted interval for a given object.
Definition: Signposts.cpp:119
llvm::SignpostEmitter::isEnabled
bool isEnabled() const
Definition: Signposts.cpp:111
llvm::Lock
static sys::Mutex Lock
Definition: NVPTXUtilities.cpp:39
llvm::SignpostEmitterImpl
Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl.
Definition: Signposts.cpp:94
Signposts.h
llvm::sys::SmartMutex
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Timer.h
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::sys::SmartScopedLock
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:71
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Mutex.h
llvm::sys::Mutex
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition: Mutex.h:68
llvm::SignpostEmitter::endInterval
void endInterval(const void *O, StringRef Name)
End a signposted interval for a given object.
Definition: Signposts.cpp:127
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::SignpostEmitter::SignpostEmitter
SignpostEmitter()
Definition: Signposts.cpp:103
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37