2019-02-20 02:18:31 +08:00
|
|
|
//===-- Signposts.cpp - Interval debug annotations ------------------------===//
|
|
|
|
//
|
2021-08-05 11:16:17 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-02-20 02:18:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Signposts.h"
|
2022-02-02 23:32:19 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2021-10-12 00:59:21 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2022-02-02 23:32:19 +08:00
|
|
|
|
2019-02-20 02:18:31 +08:00
|
|
|
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2021-01-20 03:37:45 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2021-10-12 00:59:21 +08:00
|
|
|
#include <Availability.h>
|
|
|
|
#include <os/signpost.h>
|
|
|
|
#endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
2019-02-20 02:18:31 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
|
2021-10-12 00:59:21 +08:00
|
|
|
#define SIGNPOSTS_AVAILABLE() \
|
|
|
|
__builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *)
|
2019-02-20 02:18:31 +08:00
|
|
|
namespace {
|
|
|
|
os_log_t *LogCreator() {
|
|
|
|
os_log_t *X = new os_log_t;
|
2022-04-06 03:09:21 +08:00
|
|
|
*X = os_log_create("org.llvm.signposts", "toolchain");
|
2019-02-20 02:18:31 +08:00
|
|
|
return X;
|
|
|
|
}
|
2021-04-09 07:20:31 +08:00
|
|
|
struct LogDeleter {
|
|
|
|
void operator()(os_log_t *X) const {
|
|
|
|
os_release(*X);
|
|
|
|
delete X;
|
|
|
|
}
|
|
|
|
};
|
2019-02-20 02:18:31 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class SignpostEmitterImpl {
|
2021-04-09 07:20:31 +08:00
|
|
|
using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>;
|
2021-10-12 00:59:21 +08:00
|
|
|
using LogTy = LogPtrTy::element_type;
|
2019-02-20 02:18:31 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-12 00:59:21 +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;
|
2021-02-25 08:23:12 +08:00
|
|
|
os_signpost_id_t ID = {};
|
|
|
|
if (SIGNPOSTS_AVAILABLE()) {
|
|
|
|
ID = os_signpost_id_make_with_pointer(getLogger(), O);
|
|
|
|
}
|
|
|
|
const auto &Inserted = Signposts.insert(std::make_pair(O, ID));
|
2019-02-20 02:18:31 +08:00
|
|
|
return Inserted.first->second;
|
|
|
|
}
|
|
|
|
|
2021-10-12 00:59:21 +08:00
|
|
|
public:
|
2021-04-09 07:20:31 +08:00
|
|
|
SignpostEmitterImpl() : SignpostLog(LogCreator()) {}
|
2019-02-20 02:18:31 +08:00
|
|
|
|
2021-02-25 08:23:12 +08:00
|
|
|
bool isEnabled() const {
|
|
|
|
if (SIGNPOSTS_AVAILABLE())
|
|
|
|
return os_signpost_enabled(*SignpostLog);
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-20 02:18:31 +08:00
|
|
|
|
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-02-25 08:23:12 +08:00
|
|
|
if (SIGNPOSTS_AVAILABLE()) {
|
|
|
|
// Both strings used here are required to be constant literal strings.
|
|
|
|
os_signpost_interval_begin(getLogger(), getSignpostForObject(O),
|
2021-06-04 02:22:24 +08:00
|
|
|
"LLVM Timers", "%s", Name.data());
|
2021-02-25 08:23:12 +08:00
|
|
|
}
|
2019-02-20 02:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 00:59:21 +08:00
|
|
|
void endInterval(const void *O, llvm::StringRef Name) {
|
2019-02-20 02:18:31 +08:00
|
|
|
if (isEnabled()) {
|
2021-02-25 08:23:12 +08:00
|
|
|
if (SIGNPOSTS_AVAILABLE()) {
|
|
|
|
// Both strings used here are required to be constant literal strings.
|
|
|
|
os_signpost_interval_end(getLogger(), getSignpostForObject(O),
|
2021-06-04 02:22:24 +08:00
|
|
|
"LLVM Timers", "");
|
2021-02-25 08:23:12 +08:00
|
|
|
}
|
2019-02-20 02:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|
2021-04-09 08:00:43 +08:00
|
|
|
#else
|
|
|
|
/// Definition necessary for use of std::unique_ptr in SignpostEmitter::Impl.
|
|
|
|
class llvm::SignpostEmitterImpl {};
|
2019-02-20 02:18:31 +08:00
|
|
|
#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
|
2021-04-09 08:00:43 +08:00
|
|
|
Impl = std::make_unique<SignpostEmitterImpl>();
|
2019-02-20 02:18:31 +08:00
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|
|
|
|
|
2021-04-09 08:00:43 +08:00
|
|
|
SignpostEmitter::~SignpostEmitter() = default;
|
2019-02-20 02:18:31 +08:00
|
|
|
|
|
|
|
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-10-12 00:59:21 +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-10-12 00:59:21 +08:00
|
|
|
Impl->endInterval(O, Name);
|
2019-02-20 02:18:31 +08:00
|
|
|
#endif // if !HAVE_ANY_SIGNPOST_IMPL
|
|
|
|
}
|