Revert "Allow signposts to take advantage of deferred string substitution"

This reverts commits f9aba9a5af and
035217ff51.

As explained in the original commit message, this didn't have the
intended effect of improving the common LLDB use case, but still
provided a marginal improvement for the places where LLDB creates a
scoped time with a string literal.

The reason for the revert is that this change pulls in the os/signpost.h
header in Signposts.h. The former transitively includes loader.h, which
contains a series of macro defines that conflict with MachO.h. There are
ways to work around that, but Adrian and I concluded that  none of them
are worth the trade-off in complicating Signposts.h even further.
This commit is contained in:
Jonas Devlieghere 2021-10-11 09:59:21 -07:00
parent d409048201
commit 070315d04c
9 changed files with 67 additions and 104 deletions

View File

@ -9,16 +9,11 @@
#ifndef LLDB_UTILITY_TIMER_H #ifndef LLDB_UTILITY_TIMER_H
#define LLDB_UTILITY_TIMER_H #define LLDB_UTILITY_TIMER_H
#include "llvm/ADT/ScopeExit.h" #include "lldb/lldb-defines.h"
#include "llvm/Support/Chrono.h" #include "llvm/Support/Chrono.h"
#include "llvm/Support/Signposts.h"
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
namespace llvm {
class SignpostEmitter;
}
namespace lldb_private { namespace lldb_private {
class Stream; class Stream;
@ -81,28 +76,15 @@ private:
const Timer &operator=(const Timer &) = delete; const Timer &operator=(const Timer &) = delete;
}; };
llvm::SignpostEmitter &GetSignposts();
} // namespace lldb_private } // namespace lldb_private
// Use a format string because LLVM_PRETTY_FUNCTION might not be a string // Use a format string because LLVM_PRETTY_FUNCTION might not be a string
// literal. // literal.
#define LLDB_SCOPED_TIMER() \ #define LLDB_SCOPED_TIMER() \
static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \ static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION); \ ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
SIGNPOST_EMITTER_START_INTERVAL(::lldb_private::GetSignposts(), \ #define LLDB_SCOPED_TIMERF(...) \
&_scoped_timer, "%s", LLVM_PRETTY_FUNCTION); \
auto _scoped_signpost = llvm::make_scope_exit([&_scoped_timer]() { \
::lldb_private::GetSignposts().endInterval(&_scoped_timer); \
})
#define LLDB_SCOPED_TIMERF(FMT, ...) \
static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \ static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
::lldb_private::Timer _scoped_timer(_cat, FMT, __VA_ARGS__); \ ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
SIGNPOST_EMITTER_START_INTERVAL(::lldb_private::GetSignposts(), \
&_scoped_timer, FMT, __VA_ARGS__); \
auto _scoped_signpost = llvm::make_scope_exit([&_scoped_timer]() { \
::lldb_private::GetSignposts().endInterval(&_scoped_timer); \
})
#endif // LLDB_UTILITY_TIMER_H #endif // LLDB_UTILITY_TIMER_H

View File

@ -21,7 +21,6 @@
#include "lldb/Core/Section.h" #include "lldb/Core/Section.h"
#include "lldb/Core/StreamFile.h" #include "lldb/Core/StreamFile.h"
#include "lldb/Host/Host.h" #include "lldb/Host/Host.h"
#include "lldb/Host/SafeMachO.h"
#include "lldb/Symbol/DWARFCallFrameInfo.h" #include "lldb/Symbol/DWARFCallFrameInfo.h"
#include "lldb/Symbol/LocateSymbolFile.h" #include "lldb/Symbol/LocateSymbolFile.h"
#include "lldb/Symbol/ObjectFile.h" #include "lldb/Symbol/ObjectFile.h"
@ -44,6 +43,8 @@
#include "lldb/Utility/Timer.h" #include "lldb/Utility/Timer.h"
#include "lldb/Utility/UUID.h" #include "lldb/Utility/UUID.h"
#include "lldb/Host/SafeMachO.h"
#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/DenseSet.h"
#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
@ -65,48 +66,65 @@
#include <bitset> #include <bitset>
#include <memory> #include <memory>
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
// Unfortunately the signpost header pulls in the system MachO header, too. // Unfortunately the signpost header pulls in the system MachO header, too.
#ifdef CPU_TYPE_ARM
#undef CPU_TYPE_ARM #undef CPU_TYPE_ARM
#endif
#ifdef CPU_TYPE_ARM64
#undef CPU_TYPE_ARM64 #undef CPU_TYPE_ARM64
#endif
#ifdef CPU_TYPE_ARM64_32
#undef CPU_TYPE_ARM64_32 #undef CPU_TYPE_ARM64_32
#endif
#ifdef CPU_TYPE_I386
#undef CPU_TYPE_I386 #undef CPU_TYPE_I386
#endif
#ifdef CPU_TYPE_X86_64
#undef CPU_TYPE_X86_64 #undef CPU_TYPE_X86_64
#undef MH_BINDATLOAD #endif
#undef MH_BUNDLE #ifdef MH_DYLINKER
#undef MH_CIGAM
#undef MH_CIGAM_64
#undef MH_CORE
#undef MH_DSYM
#undef MH_DYLDLINK
#undef MH_DYLIB
#undef MH_DYLIB_STUB
#undef MH_DYLINKER #undef MH_DYLINKER
#undef MH_DYLINKER #endif
#undef MH_EXECUTE #ifdef MH_OBJECT
#undef MH_FVMLIB
#undef MH_INCRLINK
#undef MH_KEXT_BUNDLE
#undef MH_MAGIC
#undef MH_MAGIC_64
#undef MH_NOUNDEFS
#undef MH_OBJECT #undef MH_OBJECT
#undef MH_OBJECT #endif
#undef MH_PRELOAD #ifdef LC_VERSION_MIN_MACOSX
#undef LC_BUILD_VERSION
#undef LC_VERSION_MIN_MACOSX #undef LC_VERSION_MIN_MACOSX
#endif
#ifdef LC_VERSION_MIN_IPHONEOS
#undef LC_VERSION_MIN_IPHONEOS #undef LC_VERSION_MIN_IPHONEOS
#endif
#ifdef LC_VERSION_MIN_TVOS
#undef LC_VERSION_MIN_TVOS #undef LC_VERSION_MIN_TVOS
#endif
#ifdef LC_VERSION_MIN_WATCHOS
#undef LC_VERSION_MIN_WATCHOS #undef LC_VERSION_MIN_WATCHOS
#endif
#ifdef LC_BUILD_VERSION
#undef LC_BUILD_VERSION
#endif
#ifdef PLATFORM_MACOS
#undef PLATFORM_MACOS #undef PLATFORM_MACOS
#endif
#ifdef PLATFORM_MACCATALYST
#undef PLATFORM_MACCATALYST #undef PLATFORM_MACCATALYST
#endif
#ifdef PLATFORM_IOS
#undef PLATFORM_IOS #undef PLATFORM_IOS
#endif
#ifdef PLATFORM_IOSSIMULATOR
#undef PLATFORM_IOSSIMULATOR #undef PLATFORM_IOSSIMULATOR
#endif
#ifdef PLATFORM_TVOS
#undef PLATFORM_TVOS #undef PLATFORM_TVOS
#endif
#ifdef PLATFORM_TVOSSIMULATOR
#undef PLATFORM_TVOSSIMULATOR #undef PLATFORM_TVOSSIMULATOR
#endif
#ifdef PLATFORM_WATCHOS
#undef PLATFORM_WATCHOS #undef PLATFORM_WATCHOS
#endif
#ifdef PLATFORM_WATCHOSSIMULATOR
#undef PLATFORM_WATCHOSSIMULATOR #undef PLATFORM_WATCHOSSIMULATOR
#endif #endif

View File

@ -16,6 +16,7 @@
#include "lldb/Host/FileSystem.h" #include "lldb/Host/FileSystem.h"
#include "lldb/Utility/RangeMap.h" #include "lldb/Utility/RangeMap.h"
#include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Timer.h"
//#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
#if defined(DEBUG_OSO_DMAP) #if defined(DEBUG_OSO_DMAP)
@ -33,9 +34,6 @@
#include "LogChannelDWARF.h" #include "LogChannelDWARF.h"
#include "SymbolFileDWARF.h" #include "SymbolFileDWARF.h"
// Work around the fact that Timer.h pulls in the system Mach-O headers.
#include "lldb/Utility/Timer.h"
#include <memory> #include <memory>
using namespace lldb; using namespace lldb;

View File

@ -33,8 +33,6 @@ static std::atomic<Timer::Category *> g_categories;
/// Allows llvm::Timer to emit signposts when supported. /// Allows llvm::Timer to emit signposts when supported.
static llvm::ManagedStatic<llvm::SignpostEmitter> Signposts; static llvm::ManagedStatic<llvm::SignpostEmitter> Signposts;
llvm::SignpostEmitter &lldb_private::GetSignposts() { return *Signposts; }
std::atomic<bool> Timer::g_quiet(true); std::atomic<bool> Timer::g_quiet(true);
std::atomic<unsigned> Timer::g_display_depth(0); std::atomic<unsigned> Timer::g_display_depth(0);
static std::mutex &GetFileMutex() { static std::mutex &GetFileMutex() {
@ -61,6 +59,7 @@ void Timer::SetQuiet(bool value) { g_quiet = value; }
Timer::Timer(Timer::Category &category, const char *format, ...) Timer::Timer(Timer::Category &category, const char *format, ...)
: m_category(category), m_total_start(std::chrono::steady_clock::now()) { : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
Signposts->startInterval(this, m_category.GetName());
TimerStack &stack = GetTimerStackForCurrentThread(); TimerStack &stack = GetTimerStackForCurrentThread();
stack.push_back(this); stack.push_back(this);
@ -87,6 +86,8 @@ Timer::~Timer() {
auto total_dur = stop_time - m_total_start; auto total_dur = stop_time - m_total_start;
auto timer_dur = total_dur - m_child_duration; auto timer_dur = total_dur - m_child_duration;
Signposts->endInterval(this, m_category.GetName());
TimerStack &stack = GetTimerStackForCurrentThread(); TimerStack &stack = GetTimerStackForCurrentThread();
if (g_quiet && stack.size() <= g_display_depth) { if (g_quiet && stack.size() <= g_display_depth) {
std::lock_guard<std::mutex> lock(GetFileMutex()); std::lock_guard<std::mutex> lock(GetFileMutex());

View File

@ -353,6 +353,9 @@
/* Define to the default GlobalISel coverage file prefix */ /* Define to the default GlobalISel coverage file prefix */
#cmakedefine LLVM_GISEL_COV_PREFIX "${LLVM_GISEL_COV_PREFIX}" #cmakedefine LLVM_GISEL_COV_PREFIX "${LLVM_GISEL_COV_PREFIX}"
/* Whether Timers signpost passes in Xcode Instruments */
#cmakedefine01 LLVM_SUPPORT_XCODE_SIGNPOSTS
#cmakedefine HAVE_PROC_PID_RUSAGE 1 #cmakedefine HAVE_PROC_PID_RUSAGE 1
#endif #endif

View File

@ -100,8 +100,4 @@
/* Define if the xar_open() function is supported on this platform. */ /* Define if the xar_open() function is supported on this platform. */
#cmakedefine LLVM_HAVE_LIBXAR ${LLVM_HAVE_LIBXAR} #cmakedefine LLVM_HAVE_LIBXAR ${LLVM_HAVE_LIBXAR}
/* Whether Timers signpost passes in Xcode Instruments */
#cmakedefine01 LLVM_SUPPORT_XCODE_SIGNPOSTS
#endif #endif

View File

@ -17,17 +17,8 @@
#define LLVM_SUPPORT_SIGNPOSTS_H #define LLVM_SUPPORT_SIGNPOSTS_H
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Config/llvm-config.h"
#include <memory> #include <memory>
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
#include <Availability.h>
#include <os/signpost.h>
#endif
#define SIGNPOSTS_AVAILABLE() \
__builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *)
namespace llvm { namespace llvm {
class SignpostEmitterImpl; class SignpostEmitterImpl;
@ -44,33 +35,8 @@ public:
/// Begin a signposted interval for a given object. /// Begin a signposted interval for a given object.
void startInterval(const void *O, StringRef Name); void startInterval(const void *O, StringRef Name);
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
os_log_t &getLogger() const;
os_signpost_id_t getSignpostForObject(const void *O);
#endif
/// A macro to take advantage of the special format string handling
/// in the os_signpost API. The format string substitution is
/// deferred to the log consumer and done outside of the
/// application.
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
#define SIGNPOST_EMITTER_START_INTERVAL(SIGNPOST_EMITTER, O, ...) \
do { \
if ((SIGNPOST_EMITTER).isEnabled()) \
if (SIGNPOSTS_AVAILABLE()) \
os_signpost_interval_begin((SIGNPOST_EMITTER).getLogger(), \
(SIGNPOST_EMITTER).getSignpostForObject(O), \
"LLVM Timers", __VA_ARGS__); \
} while (0)
#else
#define SIGNPOST_EMITTER_START_INTERVAL(SIGNPOST_EMITTER, O, ...) \
do { \
} while (0)
#endif
/// End a signposted interval for a given object. /// End a signposted interval for a given object.
void endInterval(const void *O); void endInterval(const void *O, StringRef Name);
}; };
} // end namespace llvm } // end namespace llvm

View File

@ -9,14 +9,19 @@
#include "llvm/Support/Signposts.h" #include "llvm/Support/Signposts.h"
#include "llvm/Support/Timer.h" #include "llvm/Support/Timer.h"
#include "llvm/Config/config.h"
#if LLVM_SUPPORT_XCODE_SIGNPOSTS #if LLVM_SUPPORT_XCODE_SIGNPOSTS
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/Support/Mutex.h" #include "llvm/Support/Mutex.h"
#endif #include <Availability.h>
#include <os/signpost.h>
#endif // if LLVM_SUPPORT_XCODE_SIGNPOSTS
using namespace llvm; using namespace llvm;
#if LLVM_SUPPORT_XCODE_SIGNPOSTS #if LLVM_SUPPORT_XCODE_SIGNPOSTS
#define SIGNPOSTS_AVAILABLE() \
__builtin_available(macos 10.14, iOS 12, tvOS 12, watchOS 5, *)
namespace { namespace {
os_log_t *LogCreator() { os_log_t *LogCreator() {
os_log_t *X = new os_log_t; os_log_t *X = new os_log_t;
@ -34,13 +39,13 @@ struct LogDeleter {
namespace llvm { namespace llvm {
class SignpostEmitterImpl { class SignpostEmitterImpl {
using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>; using LogPtrTy = std::unique_ptr<os_log_t, LogDeleter>;
using LogTy = LogPtrTy::element_type;
LogPtrTy SignpostLog; LogPtrTy SignpostLog;
DenseMap<const void *, os_signpost_id_t> Signposts; DenseMap<const void *, os_signpost_id_t> Signposts;
sys::SmartMutex<true> Mutex; sys::SmartMutex<true> Mutex;
public: LogTy &getLogger() const { return *SignpostLog; }
os_log_t &getLogger() const { return *SignpostLog; }
os_signpost_id_t getSignpostForObject(const void *O) { os_signpost_id_t getSignpostForObject(const void *O) {
sys::SmartScopedLock<true> Lock(Mutex); sys::SmartScopedLock<true> Lock(Mutex);
const auto &I = Signposts.find(O); const auto &I = Signposts.find(O);
@ -54,6 +59,7 @@ public:
return Inserted.first->second; return Inserted.first->second;
} }
public:
SignpostEmitterImpl() : SignpostLog(LogCreator()) {} SignpostEmitterImpl() : SignpostLog(LogCreator()) {}
bool isEnabled() const { bool isEnabled() const {
@ -72,7 +78,7 @@ public:
} }
} }
void endInterval(const void *O) { void endInterval(const void *O, llvm::StringRef Name) {
if (isEnabled()) { if (isEnabled()) {
if (SIGNPOSTS_AVAILABLE()) { if (SIGNPOSTS_AVAILABLE()) {
// Both strings used here are required to be constant literal strings. // Both strings used here are required to be constant literal strings.
@ -118,17 +124,10 @@ void SignpostEmitter::startInterval(const void *O, StringRef Name) {
#endif // if !HAVE_ANY_SIGNPOST_IMPL #endif // if !HAVE_ANY_SIGNPOST_IMPL
} }
#if HAVE_ANY_SIGNPOST_IMPL void SignpostEmitter::endInterval(const void *O, StringRef Name) {
os_log_t &SignpostEmitter::getLogger() const { return Impl->getLogger(); }
os_signpost_id_t SignpostEmitter::getSignpostForObject(const void *O) {
return Impl->getSignpostForObject(O);
}
#endif
void SignpostEmitter::endInterval(const void *O) {
#if HAVE_ANY_SIGNPOST_IMPL #if HAVE_ANY_SIGNPOST_IMPL
if (Impl == nullptr) if (Impl == nullptr)
return; return;
Impl->endInterval(O); Impl->endInterval(O, Name);
#endif // if !HAVE_ANY_SIGNPOST_IMPL #endif // if !HAVE_ANY_SIGNPOST_IMPL
} }

View File

@ -199,7 +199,7 @@ void Timer::stopTimer() {
Running = false; Running = false;
Time += TimeRecord::getCurrentTime(false); Time += TimeRecord::getCurrentTime(false);
Time -= StartTime; Time -= StartTime;
Signposts->endInterval(this); Signposts->endInterval(this, getName());
} }
void Timer::clear() { void Timer::clear() {