2019-02-20 02:18:31 +08:00
|
|
|
//===-- Signposts.cpp - Interval debug annotations ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Signposts.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
|
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2021-01-20 03:37:45 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2019-02-20 02:18:31 +08:00
|
|
|
#include <os/signpost.h>
|
|
|
|
#endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
namespace {
|
|
|
|
os_log_t *LogCreator() {
|
|
|
|
os_log_t *X = new os_log_t;
|
|
|
|
*X = os_log_create("org.llvm.signposts", OS_LOG_CATEGORY_POINTS_OF_INTEREST);
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
void LogDeleter(os_log_t *X) {
|
|
|
|
os_release(*X);
|
|
|
|
delete X;
|
|
|
|
}
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class SignpostEmitterImpl {
|
2021-01-07 06:35:07 +08:00
|
|
|
using LogPtrTy = std::unique_ptr<os_log_t, std::function<void(os_log_t *)>>;
|
2019-02-20 02:18:31 +08:00
|
|
|
using LogTy = LogPtrTy::element_type;
|
|
|
|
|
|
|
|
LogPtrTy SignpostLog;
|
2021-01-07 06:35:07 +08:00
|
|
|
DenseMap<const void *, os_signpost_id_t> Signposts;
|
2021-01-20 03:37:45 +08:00
|
|
|
sys::SmartMutex<true> Mutex;
|
2019-02-20 02:18:31 +08:00
|
|
|
|
|
|
|
LogTy &getLogger() const { return *SignpostLog; }
|
2021-01-07 06:35:07 +08:00
|
|
|
os_signpost_id_t getSignpostForObject(const void *O) {
|
2021-01-20 03:37:45 +08:00
|
|
|
sys::SmartScopedLock<true> Lock(Mutex);
|
2021-01-07 06:35:07 +08:00
|
|
|
const auto &I = Signposts.find(O);
|
2019-02-20 02:18:31 +08:00
|
|
|
if (I != Signposts.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
const auto &Inserted = Signposts.insert(
|
2021-01-07 06:35:07 +08:00
|
|
|
std::make_pair(O, os_signpost_id_make_with_pointer(getLogger(), O)));
|
2019-02-20 02:18:31 +08:00
|
|
|
return Inserted.first->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
SignpostEmitterImpl() : SignpostLog(LogCreator(), LogDeleter), Signposts() {}
|
|
|
|
|
|
|
|
bool isEnabled() const { return os_signpost_enabled(*SignpostLog); }
|
|
|
|
|
2021-01-07 06:35:07 +08:00
|
|
|
void startInterval(const void *O, llvm::StringRef Name) {
|
2019-02-20 02:18:31 +08:00
|
|
|
if (isEnabled()) {
|
2021-01-07 06:35:07 +08:00
|
|
|
// Both strings used here are required to be constant literal strings.
|
|
|
|
os_signpost_interval_begin(getLogger(), getSignpostForObject(O),
|
|
|
|
"LLVM Timers", "Begin %s", Name.data());
|
2019-02-20 02:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:35:07 +08:00
|
|
|
void endInterval(const void *O, llvm::StringRef Name) {
|
2019-02-20 02:18:31 +08:00
|
|
|
if (isEnabled()) {
|
2021-01-07 06:35:07 +08:00
|
|
|
// Both strings used here are required to be constant literal strings.
|
|
|
|
os_signpost_interval_end(getLogger(), getSignpostForObject(O),
|
|
|
|
"LLVM Timers", "End %s", Name.data());
|
2019-02-20 02:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
|
|
|
|
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
#define HAVE_ANY_SIGNPOST_IMPL 1
|
2019-08-15 18:05:22 +08:00
|
|
|
#else
|
|
|
|
#define HAVE_ANY_SIGNPOST_IMPL 0
|
2019-02-20 02:18:31 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SignpostEmitter::SignpostEmitter() {
|
|
|
|
#if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
Impl = new SignpostEmitterImpl();
|
2021-01-07 06:35:07 +08:00
|
|
|
#else // if HAVE_ANY_SIGNPOST_IMPL
|
2019-02-20 02:18:31 +08:00
|
|
|
Impl = nullptr;
|
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|
|
|
|
|
|
|
|
SignpostEmitter::~SignpostEmitter() {
|
|
|
|
#if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
delete Impl;
|
|
|
|
#endif // if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SignpostEmitter::isEnabled() const {
|
|
|
|
#if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
return Impl->isEnabled();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:35:07 +08:00
|
|
|
void SignpostEmitter::startInterval(const void *O, StringRef Name) {
|
2019-02-20 02:18:31 +08:00
|
|
|
#if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
if (Impl == nullptr)
|
|
|
|
return;
|
2021-01-07 06:35:07 +08:00
|
|
|
return Impl->startInterval(O, Name);
|
2019-02-20 02:18:31 +08:00
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:35:07 +08:00
|
|
|
void SignpostEmitter::endInterval(const void *O, StringRef Name) {
|
2019-02-20 02:18:31 +08:00
|
|
|
#if HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
if (Impl == nullptr)
|
|
|
|
return;
|
2021-01-07 06:35:07 +08:00
|
|
|
Impl->endInterval(O, Name);
|
2019-02-20 02:18:31 +08:00
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|