Index: lldb/trunk/include/lldb/Core/Timer.h =================================================================== --- lldb/trunk/include/lldb/Core/Timer.h +++ lldb/trunk/include/lldb/Core/Timer.h @@ -1,91 +0,0 @@ -//===-- Timer.h -------------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_Timer_h_ -#define liblldb_Timer_h_ - -#include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN -#include "llvm/Support/Chrono.h" - -#include - -#include // for uint32_t - -namespace lldb_private { -class Stream; -} - -namespace lldb_private { - -//---------------------------------------------------------------------- -/// @class Timer Timer.h "lldb/Core/Timer.h" -/// @brief A timer class that simplifies common timing metrics. -/// -/// A scoped timer class that allows a variety of pthread mutex -/// objects to have a mutex locked when a Timer::Locker -/// object is created, and unlocked when it goes out of scope or -/// when the Timer::Locker::Reset(pthread_mutex_t *) -/// is called. This provides an exception safe way to lock a mutex -/// in a scope. -//---------------------------------------------------------------------- - -class Timer { -public: - class Category { - public: - explicit Category(const char *category_name); - - private: - friend class Timer; - const char *m_name; - std::atomic m_nanos; - std::atomic m_next; - - DISALLOW_COPY_AND_ASSIGN(Category); - }; - - //-------------------------------------------------------------- - /// Default constructor. - //-------------------------------------------------------------- - Timer(Category &category, const char *format, ...) - __attribute__((format(printf, 3, 4))); - - //-------------------------------------------------------------- - /// Destructor - //-------------------------------------------------------------- - ~Timer(); - - void Dump(); - - static void SetDisplayDepth(uint32_t depth); - - static void SetQuiet(bool value); - - static void DumpCategoryTimes(Stream *s); - - static void ResetCategoryTimes(); - -protected: - using TimePoint = std::chrono::steady_clock::time_point; - void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; } - - Category &m_category; - TimePoint m_total_start; - TimePoint::duration m_child_duration{0}; - - static std::atomic g_quiet; - static std::atomic g_display_depth; - -private: - DISALLOW_COPY_AND_ASSIGN(Timer); -}; - -} // namespace lldb_private - -#endif // liblldb_Timer_h_ Index: lldb/trunk/include/lldb/Core/TraceOptions.h =================================================================== --- lldb/trunk/include/lldb/Core/TraceOptions.h +++ lldb/trunk/include/lldb/Core/TraceOptions.h @@ -1,61 +0,0 @@ -//===-- TraceOptions.h ------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_TraceOptions_h_ -#define liblldb_TraceOptions_h_ - -#include "lldb/lldb-defines.h" -#include "lldb/lldb-enumerations.h" - -#include "lldb/Utility/StructuredData.h" - -namespace lldb_private { -class TraceOptions { -public: - TraceOptions() : m_trace_params(new StructuredData::Dictionary()) {} - - const StructuredData::DictionarySP &getTraceParams() const { - return m_trace_params; - } - - lldb::TraceType getType() const { return m_type; } - - uint64_t getTraceBufferSize() const { return m_trace_buffer_size; } - - uint64_t getMetaDataBufferSize() const { return m_meta_data_buffer_size; } - - void setTraceParams(const StructuredData::DictionarySP &dict_obj) { - m_trace_params = dict_obj; - } - - void setType(lldb::TraceType type) { m_type = type; } - - void setTraceBufferSize(uint64_t size) { m_trace_buffer_size = size; } - - void setMetaDataBufferSize(uint64_t size) { m_meta_data_buffer_size = size; } - - void setThreadID(lldb::tid_t thread_id) { m_thread_id = thread_id; } - - lldb::tid_t getThreadID() const { return m_thread_id; } - -private: - lldb::TraceType m_type; - uint64_t m_trace_buffer_size; - uint64_t m_meta_data_buffer_size; - lldb::tid_t m_thread_id; - - /// m_trace_params is meant to hold any custom parameters - /// apart from meta buffer size and trace size. - /// The interpretation of such parameters is left to - /// the lldb-server. - StructuredData::DictionarySP m_trace_params; -}; -} - -#endif // liblldb_TraceOptions_h_ Index: lldb/trunk/include/lldb/Host/common/NativeProcessProtocol.h =================================================================== --- lldb/trunk/include/lldb/Host/common/NativeProcessProtocol.h +++ lldb/trunk/include/lldb/Host/common/NativeProcessProtocol.h @@ -10,10 +10,10 @@ #ifndef liblldb_NativeProcessProtocol_h_ #define liblldb_NativeProcessProtocol_h_ -#include "lldb/Core/TraceOptions.h" #include "lldb/Host/Host.h" #include "lldb/Host/MainLoop.h" #include "lldb/Utility/Status.h" +#include "lldb/Utility/TraceOptions.h" #include "lldb/lldb-private-forward.h" #include "lldb/lldb-types.h" #include "llvm/ADT/ArrayRef.h" Index: lldb/trunk/include/lldb/Target/Process.h =================================================================== --- lldb/trunk/include/lldb/Target/Process.h +++ lldb/trunk/include/lldb/Target/Process.h @@ -35,7 +35,6 @@ #include "lldb/Core/LoadedModuleInfoList.h" #include "lldb/Core/PluginInterface.h" #include "lldb/Core/ThreadSafeValue.h" -#include "lldb/Core/TraceOptions.h" #include "lldb/Core/UserSettingsController.h" #include "lldb/Host/HostThread.h" #include "lldb/Host/ProcessRunLock.h" @@ -50,6 +49,7 @@ #include "lldb/Utility/NameMatches.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StructuredData.h" +#include "lldb/Utility/TraceOptions.h" #include "lldb/lldb-private.h" #include "llvm/ADT/ArrayRef.h" Index: lldb/trunk/include/lldb/Utility/Timer.h =================================================================== --- lldb/trunk/include/lldb/Utility/Timer.h +++ lldb/trunk/include/lldb/Utility/Timer.h @@ -0,0 +1,79 @@ +//===-- Timer.h -------------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Timer_h_ +#define liblldb_Timer_h_ + +#include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN +#include "llvm/Support/Chrono.h" +#include +#include // for uint32_t + +namespace lldb_private { +class Stream; + +//---------------------------------------------------------------------- +/// @class Timer Timer.h "lldb/Utility/Timer.h" +/// @brief A timer class that simplifies common timing metrics. +//---------------------------------------------------------------------- + +class Timer { +public: + class Category { + public: + explicit Category(const char *category_name); + + private: + friend class Timer; + const char *m_name; + std::atomic m_nanos; + std::atomic m_next; + + DISALLOW_COPY_AND_ASSIGN(Category); + }; + + //-------------------------------------------------------------- + /// Default constructor. + //-------------------------------------------------------------- + Timer(Category &category, const char *format, ...) + __attribute__((format(printf, 3, 4))); + + //-------------------------------------------------------------- + /// Destructor + //-------------------------------------------------------------- + ~Timer(); + + void Dump(); + + static void SetDisplayDepth(uint32_t depth); + + static void SetQuiet(bool value); + + static void DumpCategoryTimes(Stream *s); + + static void ResetCategoryTimes(); + +protected: + using TimePoint = std::chrono::steady_clock::time_point; + void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; } + + Category &m_category; + TimePoint m_total_start; + TimePoint::duration m_child_duration{0}; + + static std::atomic g_quiet; + static std::atomic g_display_depth; + +private: + DISALLOW_COPY_AND_ASSIGN(Timer); +}; + +} // namespace lldb_private + +#endif // liblldb_Timer_h_ Index: lldb/trunk/include/lldb/Utility/TraceOptions.h =================================================================== --- lldb/trunk/include/lldb/Utility/TraceOptions.h +++ lldb/trunk/include/lldb/Utility/TraceOptions.h @@ -0,0 +1,61 @@ +//===-- TraceOptions.h ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_TraceOptions_h_ +#define liblldb_TraceOptions_h_ + +#include "lldb/lldb-defines.h" +#include "lldb/lldb-enumerations.h" + +#include "lldb/Utility/StructuredData.h" + +namespace lldb_private { +class TraceOptions { +public: + TraceOptions() : m_trace_params(new StructuredData::Dictionary()) {} + + const StructuredData::DictionarySP &getTraceParams() const { + return m_trace_params; + } + + lldb::TraceType getType() const { return m_type; } + + uint64_t getTraceBufferSize() const { return m_trace_buffer_size; } + + uint64_t getMetaDataBufferSize() const { return m_meta_data_buffer_size; } + + void setTraceParams(const StructuredData::DictionarySP &dict_obj) { + m_trace_params = dict_obj; + } + + void setType(lldb::TraceType type) { m_type = type; } + + void setTraceBufferSize(uint64_t size) { m_trace_buffer_size = size; } + + void setMetaDataBufferSize(uint64_t size) { m_meta_data_buffer_size = size; } + + void setThreadID(lldb::tid_t thread_id) { m_thread_id = thread_id; } + + lldb::tid_t getThreadID() const { return m_thread_id; } + +private: + lldb::TraceType m_type; + uint64_t m_trace_buffer_size; + uint64_t m_meta_data_buffer_size; + lldb::tid_t m_thread_id; + + /// m_trace_params is meant to hold any custom parameters + /// apart from meta buffer size and trace size. + /// The interpretation of such parameters is left to + /// the lldb-server. + StructuredData::DictionarySP m_trace_params; +}; +} + +#endif // liblldb_TraceOptions_h_ Index: lldb/trunk/source/API/SBTraceOptions.cpp =================================================================== --- lldb/trunk/source/API/SBTraceOptions.cpp +++ lldb/trunk/source/API/SBTraceOptions.cpp @@ -10,9 +10,9 @@ #include "lldb/API/SBTraceOptions.h" #include "lldb/API/SBError.h" #include "lldb/API/SBStructuredData.h" -#include "lldb/Utility/Log.h" #include "lldb/Core/StructuredDataImpl.h" -#include "lldb/Core/TraceOptions.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/TraceOptions.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/API/SystemInitializerFull.cpp =================================================================== --- lldb/trunk/source/API/SystemInitializerFull.cpp +++ lldb/trunk/source/API/SystemInitializerFull.cpp @@ -20,7 +20,6 @@ #endif #include "lldb/Core/Debugger.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Initialization/SystemInitializerCommon.h" #include "lldb/Interpreter/CommandInterpreter.h" @@ -28,6 +27,7 @@ #include "lldb/Symbol/GoASTContext.h" #include "lldb/Symbol/JavaASTContext.h" #include "lldb/Symbol/OCamlASTContext.h" +#include "lldb/Utility/Timer.h" #include "Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h" #include "Plugins/ABI/MacOSX-arm64/ABIMacOSX_arm64.h" Index: lldb/trunk/source/Commands/CommandObjectFrame.cpp =================================================================== --- lldb/trunk/source/Commands/CommandObjectFrame.cpp +++ lldb/trunk/source/Commands/CommandObjectFrame.cpp @@ -17,7 +17,6 @@ #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/Value.h" #include "lldb/Core/ValueObject.h" #include "lldb/Core/ValueObjectVariable.h" @@ -47,6 +46,7 @@ #include "lldb/Target/Thread.h" #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Commands/CommandObjectLog.cpp =================================================================== --- lldb/trunk/source/Commands/CommandObjectLog.cpp +++ lldb/trunk/source/Commands/CommandObjectLog.cpp @@ -15,7 +15,6 @@ #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/OptionParser.h" #include "lldb/Interpreter/Args.h" #include "lldb/Interpreter/CommandInterpreter.h" @@ -31,6 +30,7 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Commands/CommandObjectTarget.cpp =================================================================== --- lldb/trunk/source/Commands/CommandObjectTarget.cpp +++ lldb/trunk/source/Commands/CommandObjectTarget.cpp @@ -16,7 +16,6 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/Section.h" #include "lldb/Core/State.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/ValueObjectVariable.h" #include "lldb/DataFormatters/ValueObjectPrinter.h" #include "lldb/Host/OptionParser.h" @@ -50,6 +49,7 @@ #include "lldb/Target/StackFrame.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadSpec.h" +#include "lldb/Utility/Timer.h" #include "llvm/Support/FileSystem.h" Index: lldb/trunk/source/Core/CMakeLists.txt =================================================================== --- lldb/trunk/source/Core/CMakeLists.txt +++ lldb/trunk/source/Core/CMakeLists.txt @@ -32,7 +32,6 @@ State.cpp StreamAsynchronousIO.cpp StreamFile.cpp - Timer.cpp UserSettingsController.cpp Value.cpp ValueObject.cpp Index: lldb/trunk/source/Core/Disassembler.cpp =================================================================== --- lldb/trunk/source/Core/Disassembler.cpp +++ lldb/trunk/source/Core/Disassembler.cpp @@ -17,7 +17,6 @@ #include "lldb/Core/ModuleList.h" // for ModuleList #include "lldb/Core/PluginManager.h" #include "lldb/Core/SourceManager.h" // for SourceManager -#include "lldb/Core/Timer.h" #include "lldb/Host/FileSystem.h" #include "lldb/Interpreter/OptionValue.h" #include "lldb/Interpreter/OptionValueArray.h" @@ -37,8 +36,9 @@ #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Status.h" -#include "lldb/Utility/Stream.h" // for Stream -#include "lldb/Utility/StreamString.h" // for StreamString +#include "lldb/Utility/Stream.h" // for Stream +#include "lldb/Utility/StreamString.h" // for StreamString +#include "lldb/Utility/Timer.h" #include "lldb/lldb-private-enumerations.h" // for InstructionType:... #include "lldb/lldb-private-interfaces.h" // for DisassemblerCrea... #include "lldb/lldb-private-types.h" // for RegisterInfo Index: lldb/trunk/source/Core/Mangled.cpp =================================================================== --- lldb/trunk/source/Core/Mangled.cpp +++ lldb/trunk/source/Core/Mangled.cpp @@ -27,12 +27,12 @@ #include #endif -#include "lldb/Core/Timer.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Logging.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #include "lldb/lldb-enumerations.h" // for LanguageType #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" Index: lldb/trunk/source/Core/Module.cpp =================================================================== --- lldb/trunk/source/Core/Module.cpp +++ lldb/trunk/source/Core/Module.cpp @@ -17,7 +17,6 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/SearchFilter.h" // for SearchFilt... #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/Host.h" #include "lldb/Interpreter/CommandInterpreter.h" @@ -45,6 +44,7 @@ #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" // for Stream #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #if defined(LLVM_ON_WIN32) #include "lldb/Host/windows/PosixApi.h" // for PATH_MAX Index: lldb/trunk/source/Core/Timer.cpp =================================================================== --- lldb/trunk/source/Core/Timer.cpp +++ lldb/trunk/source/Core/Timer.cpp @@ -1,135 +0,0 @@ -//===-- Timer.cpp -----------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -#include "lldb/Core/Timer.h" - -#include "lldb/Host/Host.h" -#include "lldb/Utility/Stream.h" -#include "lldb/lldb-types.h" // for thread_key_t - -#include -#include -#include -#include // for pair -#include - -#include // for assert -#include // for va_end, va_list, va_start -#include - -using namespace lldb_private; - -#define TIMER_INDENT_AMOUNT 2 - -namespace { -typedef std::vector TimerStack; -static std::atomic g_categories; -} // end of anonymous namespace - -std::atomic Timer::g_quiet(true); -std::atomic Timer::g_display_depth(0); -static std::mutex &GetFileMutex() { - static std::mutex *g_file_mutex_ptr = new std::mutex(); - return *g_file_mutex_ptr; -} - -static TimerStack &GetTimerStackForCurrentThread() { - static thread_local TimerStack g_stack; - return g_stack; -} - -Timer::Category::Category(const char *cat) : m_name(cat) { - m_nanos.store(0, std::memory_order_release); - Category *expected = g_categories; - do { - m_next = expected; - } while (!g_categories.compare_exchange_weak(expected, this)); -} - -void Timer::SetQuiet(bool value) { g_quiet = value; } - -Timer::Timer(Timer::Category &category, const char *format, ...) - : m_category(category), m_total_start(std::chrono::steady_clock::now()) { - TimerStack &stack = GetTimerStackForCurrentThread(); - - stack.push_back(this); - if (g_quiet && stack.size() <= g_display_depth) { - std::lock_guard lock(GetFileMutex()); - - // Indent - ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, ""); - // Print formatted string - va_list args; - va_start(args, format); - ::vfprintf(stdout, format, args); - va_end(args); - - // Newline - ::fprintf(stdout, "\n"); - } -} - -Timer::~Timer() { - using namespace std::chrono; - - auto stop_time = steady_clock::now(); - auto total_dur = stop_time - m_total_start; - auto timer_dur = total_dur - m_child_duration; - - TimerStack &stack = GetTimerStackForCurrentThread(); - if (g_quiet && stack.size() <= g_display_depth) { - std::lock_guard lock(GetFileMutex()); - ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", - int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "", - duration(total_dur).count(), - duration(timer_dur).count()); - } - - assert(stack.back() == this); - stack.pop_back(); - if (!stack.empty()) - stack.back()->ChildDuration(total_dur); - - // Keep total results for each category so we can dump results. - m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count(); -} - -void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } - -/* binary function predicate: - * - returns whether a person is less than another person - */ - -typedef std::pair TimerEntry; - -static bool CategoryMapIteratorSortCriterion(const TimerEntry &lhs, - const TimerEntry &rhs) { - return lhs.second > rhs.second; -} - -void Timer::ResetCategoryTimes() { - for (Category *i = g_categories; i; i = i->m_next) - i->m_nanos.store(0, std::memory_order_release); -} - -void Timer::DumpCategoryTimes(Stream *s) { - std::vector sorted; - for (Category *i = g_categories; i; i = i->m_next) { - uint64_t nanos = i->m_nanos.load(std::memory_order_acquire); - if (nanos) - sorted.push_back(std::make_pair(i->m_name, nanos)); - } - if (sorted.empty()) - return; // Later code will break without any elements. - - // Sort by time - std::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion); - - for (const auto &timer : sorted) - s->Printf("%.9f sec for %s\n", timer.second / 1000000000., timer.first); -} Index: lldb/trunk/source/Host/common/Symbols.cpp =================================================================== --- lldb/trunk/source/Host/common/Symbols.cpp +++ lldb/trunk/source/Host/common/Symbols.cpp @@ -11,7 +11,6 @@ #include "lldb/Core/ArchSpec.h" #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/Timer.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBuffer.h" @@ -19,6 +18,7 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/SafeMachO.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "lldb/Utility/UUID.h" #include "llvm/Support/FileSystem.h" Index: lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp =================================================================== --- lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp +++ lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp @@ -42,12 +42,12 @@ #include "llvm/ADT/SmallVector.h" #endif // Project includes -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Host/Socket.h" #include "lldb/Host/common/TCPSocket.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Initialization/SystemInitializerCommon.cpp =================================================================== --- lldb/trunk/source/Initialization/SystemInitializerCommon.cpp +++ lldb/trunk/source/Initialization/SystemInitializerCommon.cpp @@ -17,10 +17,10 @@ #include "Plugins/ObjectFile/ELF/ObjectFileELF.h" #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h" #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/Timer.h" #if defined(__APPLE__) #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" Index: lldb/trunk/source/Interpreter/CommandInterpreter.cpp =================================================================== --- lldb/trunk/source/Interpreter/CommandInterpreter.cpp +++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp @@ -45,9 +45,9 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Core/State.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #ifndef LLDB_DISABLE_LIBEDIT #include "lldb/Host/Editline.h" Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp @@ -30,7 +30,6 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Core/Scalar.h" #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/ValueObjectVariable.h" #include "lldb/Expression/DiagnosticManager.h" #include "lldb/Expression/FunctionCaller.h" @@ -55,6 +54,7 @@ #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "AppleObjCClassDescriptorV2.h" #include "AppleObjCDeclVendor.h" Index: lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp =================================================================== --- lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp +++ lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp @@ -31,11 +31,11 @@ #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/PluginManager.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/FileSystem.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #include "llvm/Support/MemoryBuffer.h" Index: lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp =================================================================== --- lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp +++ lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp @@ -19,7 +19,6 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Symbol/DWARFCallFrameInfo.h" #include "lldb/Symbol/SymbolContext.h" #include "lldb/Target/SectionLoadList.h" @@ -28,6 +27,7 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/StringRef.h" Index: lldb/trunk/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp =================================================================== --- lldb/trunk/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp +++ lldb/trunk/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp @@ -20,7 +20,6 @@ #include "lldb/Core/RangeMap.h" #include "lldb/Core/Section.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/Platform.h" @@ -32,6 +31,7 @@ #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "lldb/Utility/UUID.h" #ifndef __APPLE__ Index: lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp =================================================================== --- lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp +++ lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp @@ -27,7 +27,6 @@ #include "lldb/Core/RegisterValue.h" #include "lldb/Core/Section.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Symbol/DWARFCallFrameInfo.h" #include "lldb/Symbol/ObjectFile.h" @@ -44,6 +43,7 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "lldb/Utility/UUID.h" #include "lldb/Utility/SafeMachO.h" Index: lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp =================================================================== --- lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp +++ lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp @@ -19,7 +19,6 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/Process.h" #include "lldb/Target/SectionLoadList.h" @@ -28,6 +27,7 @@ #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "lldb/Utility/UUID.h" #include "llvm/Support/MemoryBuffer.h" Index: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -24,7 +24,6 @@ #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/Symbols.h" @@ -39,6 +38,7 @@ #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" +#include "lldb/Utility/Timer.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Threading.h" Index: lldb/trunk/source/Plugins/Process/Linux/ProcessorTrace.h =================================================================== --- lldb/trunk/source/Plugins/Process/Linux/ProcessorTrace.h +++ lldb/trunk/source/Plugins/Process/Linux/ProcessorTrace.h @@ -10,12 +10,11 @@ #ifndef liblldb_ProcessorTrace_H_ #define liblldb_ProcessorTrace_H_ -#include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/DenseSet.h" - -#include "lldb/Core/TraceOptions.h" #include "lldb/Utility/Status.h" +#include "lldb/Utility/TraceOptions.h" #include "lldb/lldb-types.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" #include #include @@ -138,4 +137,4 @@ }; } // namespace process_linux } // namespace lldb_private -#endif \ No newline at end of file +#endif Index: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp =================================================================== --- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp +++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -35,7 +35,6 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Core/State.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/Value.h" #include "lldb/DataFormatters/FormatManager.h" #include "lldb/Host/ConnectionFileDescriptor.h" @@ -66,6 +65,7 @@ #include "lldb/Utility/CleanUp.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" // Project includes #include "GDBRemoteRegisterContext.h" Index: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp =================================================================== --- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -33,7 +33,6 @@ #include "lldb/Core/Communication.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/PluginManager.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/ValueObject.h" #include "lldb/DataFormatters/TypeSummary.h" #include "lldb/Host/ConnectionFileDescriptor.h" @@ -44,6 +43,7 @@ #include "lldb/Interpreter/CommandReturnObject.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadPlan.h" +#include "lldb/Utility/Timer.h" #if defined(_WIN32) #include "lldb/Host/windows/ConnectionGenericFileWindows.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp @@ -13,13 +13,13 @@ #include "lldb/Core/DumpDataExtractor.h" #include "lldb/Core/Mangled.h" #include "lldb/Core/Module.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/StringConvert.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/LineTable.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "DWARFDIECollection.h" #include "DWARFDebugAbbrev.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugAranges.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugAranges.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugAranges.cpp @@ -14,9 +14,9 @@ #include -#include "lldb/Core/Timer.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #include "DWARFCompileUnit.h" #include "DWARFDebugInfo.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugLine.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugLine.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugLine.cpp @@ -14,9 +14,9 @@ #include "lldb/Core/FileSpecList.h" #include "lldb/Core/Module.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/Timer.h" #include "LogChannelDWARF.h" #include "SymbolFileDWARF.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugPubnames.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugPubnames.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugPubnames.cpp @@ -9,8 +9,8 @@ #include "DWARFDebugPubnames.h" -#include "lldb/Core/Timer.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" #include "DWARFCompileUnit.h" #include "DWARFDIECollection.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -21,10 +21,10 @@ #include "lldb/Core/Scalar.h" #include "lldb/Core/Section.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/Value.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h" Index: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp @@ -22,12 +22,12 @@ #include "lldb/Core/Section.h" #include "lldb/Host/FileSystem.h" #include "lldb/Utility/RegularExpression.h" +#include "lldb/Utility/Timer.h" //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT #if defined(DEBUG_OSO_DMAP) #include "lldb/Core/StreamFile.h" #endif -#include "lldb/Core/Timer.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/LineTable.h" Index: lldb/trunk/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp +++ lldb/trunk/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp @@ -10,7 +10,6 @@ #include "SymbolFileSymtab.h" #include "lldb/Core/Module.h" #include "lldb/Core/PluginManager.h" -#include "lldb/Core/Timer.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/Function.h" #include "lldb/Symbol/ObjectFile.h" @@ -19,6 +18,7 @@ #include "lldb/Symbol/Symtab.h" #include "lldb/Symbol/TypeList.h" #include "lldb/Utility/RegularExpression.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Plugins/SymbolVendor/ELF/SymbolVendorELF.cpp =================================================================== --- lldb/trunk/source/Plugins/SymbolVendor/ELF/SymbolVendorELF.cpp +++ lldb/trunk/source/Plugins/SymbolVendor/ELF/SymbolVendorELF.cpp @@ -15,11 +15,11 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Host/Symbols.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Symbol/DWARFCallFrameInfo.cpp =================================================================== --- lldb/trunk/source/Symbol/DWARFCallFrameInfo.cpp +++ lldb/trunk/source/Symbol/DWARFCallFrameInfo.cpp @@ -14,7 +14,6 @@ #include "lldb/Core/ArchSpec.h" #include "lldb/Core/Module.h" #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/dwarf.h" #include "lldb/Host/Host.h" #include "lldb/Symbol/DWARFCallFrameInfo.h" @@ -23,6 +22,7 @@ #include "lldb/Target/RegisterContext.h" #include "lldb/Target/Thread.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Symbol/ObjectFile.cpp =================================================================== --- lldb/trunk/source/Symbol/ObjectFile.cpp +++ lldb/trunk/source/Symbol/ObjectFile.cpp @@ -13,7 +13,6 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" -#include "lldb/Core/Timer.h" #include "lldb/Symbol/ObjectContainer.h" #include "lldb/Symbol/SymbolFile.h" #include "lldb/Target/Process.h" @@ -25,6 +24,7 @@ #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/RegularExpression.h" +#include "lldb/Utility/Timer.h" #include "lldb/lldb-private.h" using namespace lldb; Index: lldb/trunk/source/Symbol/Symtab.cpp =================================================================== --- lldb/trunk/source/Symbol/Symtab.cpp +++ lldb/trunk/source/Symbol/Symtab.cpp @@ -13,15 +13,15 @@ #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" #include "Plugins/Language/ObjC/ObjCLanguage.h" #include "lldb/Core/Module.h" -#include "lldb/Core/Section.h" #include "lldb/Core/STLUtils.h" -#include "lldb/Core/Timer.h" +#include "lldb/Core/Section.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Symbol/Symbol.h" #include "lldb/Symbol/SymbolContext.h" #include "lldb/Symbol/Symtab.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Target/ObjCLanguageRuntime.cpp =================================================================== --- lldb/trunk/source/Target/ObjCLanguageRuntime.cpp +++ lldb/trunk/source/Target/ObjCLanguageRuntime.cpp @@ -11,7 +11,6 @@ #include "lldb/Core/MappedHash.h" #include "lldb/Core/Module.h" #include "lldb/Core/PluginManager.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/ValueObject.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/SymbolContext.h" @@ -21,6 +20,7 @@ #include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Target.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/Timer.h" #include "llvm/ADT/StringRef.h" Index: lldb/trunk/source/Target/Target.cpp =================================================================== --- lldb/trunk/source/Target/Target.cpp +++ lldb/trunk/source/Target/Target.cpp @@ -30,7 +30,6 @@ #include "lldb/Core/SourceManager.h" #include "lldb/Core/State.h" #include "lldb/Core/StreamFile.h" -#include "lldb/Core/Timer.h" #include "lldb/Core/ValueObject.h" #include "lldb/Expression/REPL.h" #include "lldb/Expression/UserExpression.h" @@ -58,6 +57,7 @@ #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Target/TargetList.cpp =================================================================== --- lldb/trunk/source/Target/TargetList.cpp +++ lldb/trunk/source/Target/TargetList.cpp @@ -15,7 +15,6 @@ #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/State.h" -#include "lldb/Core/Timer.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Interpreter/CommandInterpreter.h" @@ -24,6 +23,7 @@ #include "lldb/Target/Platform.h" #include "lldb/Target/Process.h" #include "lldb/Utility/TildeExpressionResolver.h" +#include "lldb/Utility/Timer.h" // Other libraries and framework includes #include "llvm/ADT/SmallString.h" Index: lldb/trunk/source/Utility/CMakeLists.txt =================================================================== --- lldb/trunk/source/Utility/CMakeLists.txt +++ lldb/trunk/source/Utility/CMakeLists.txt @@ -31,6 +31,7 @@ StructuredData.cpp TaskPool.cpp TildeExpressionResolver.cpp + Timer.cpp UserID.cpp UriParser.cpp UUID.cpp Index: lldb/trunk/source/Utility/Timer.cpp =================================================================== --- lldb/trunk/source/Utility/Timer.cpp +++ lldb/trunk/source/Utility/Timer.cpp @@ -0,0 +1,132 @@ +//===-- Timer.cpp -----------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "lldb/Utility/Timer.h" +#include "lldb/Utility/Stream.h" + +#include +#include +#include +#include // for pair +#include + +#include // for assert +#include // for va_end, va_list, va_start +#include + +using namespace lldb_private; + +#define TIMER_INDENT_AMOUNT 2 + +namespace { +typedef std::vector TimerStack; +static std::atomic g_categories; +} // end of anonymous namespace + +std::atomic Timer::g_quiet(true); +std::atomic Timer::g_display_depth(0); +static std::mutex &GetFileMutex() { + static std::mutex *g_file_mutex_ptr = new std::mutex(); + return *g_file_mutex_ptr; +} + +static TimerStack &GetTimerStackForCurrentThread() { + static thread_local TimerStack g_stack; + return g_stack; +} + +Timer::Category::Category(const char *cat) : m_name(cat) { + m_nanos.store(0, std::memory_order_release); + Category *expected = g_categories; + do { + m_next = expected; + } while (!g_categories.compare_exchange_weak(expected, this)); +} + +void Timer::SetQuiet(bool value) { g_quiet = value; } + +Timer::Timer(Timer::Category &category, const char *format, ...) + : m_category(category), m_total_start(std::chrono::steady_clock::now()) { + TimerStack &stack = GetTimerStackForCurrentThread(); + + stack.push_back(this); + if (g_quiet && stack.size() <= g_display_depth) { + std::lock_guard lock(GetFileMutex()); + + // Indent + ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, ""); + // Print formatted string + va_list args; + va_start(args, format); + ::vfprintf(stdout, format, args); + va_end(args); + + // Newline + ::fprintf(stdout, "\n"); + } +} + +Timer::~Timer() { + using namespace std::chrono; + + auto stop_time = steady_clock::now(); + auto total_dur = stop_time - m_total_start; + auto timer_dur = total_dur - m_child_duration; + + TimerStack &stack = GetTimerStackForCurrentThread(); + if (g_quiet && stack.size() <= g_display_depth) { + std::lock_guard lock(GetFileMutex()); + ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", + int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "", + duration(total_dur).count(), + duration(timer_dur).count()); + } + + assert(stack.back() == this); + stack.pop_back(); + if (!stack.empty()) + stack.back()->ChildDuration(total_dur); + + // Keep total results for each category so we can dump results. + m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count(); +} + +void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } + +/* binary function predicate: + * - returns whether a person is less than another person + */ + +typedef std::pair TimerEntry; + +static bool CategoryMapIteratorSortCriterion(const TimerEntry &lhs, + const TimerEntry &rhs) { + return lhs.second > rhs.second; +} + +void Timer::ResetCategoryTimes() { + for (Category *i = g_categories; i; i = i->m_next) + i->m_nanos.store(0, std::memory_order_release); +} + +void Timer::DumpCategoryTimes(Stream *s) { + std::vector sorted; + for (Category *i = g_categories; i; i = i->m_next) { + uint64_t nanos = i->m_nanos.load(std::memory_order_acquire); + if (nanos) + sorted.push_back(std::make_pair(i->m_name, nanos)); + } + if (sorted.empty()) + return; // Later code will break without any elements. + + // Sort by time + std::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion); + + for (const auto &timer : sorted) + s->Printf("%.9f sec for %s\n", timer.second / 1000000000., timer.first); +} Index: lldb/trunk/unittests/Core/CMakeLists.txt =================================================================== --- lldb/trunk/unittests/Core/CMakeLists.txt +++ lldb/trunk/unittests/Core/CMakeLists.txt @@ -6,7 +6,6 @@ ScalarTest.cpp StateTest.cpp StreamCallbackTest.cpp - TimerTest.cpp LINK_LIBS lldbCore Index: lldb/trunk/unittests/Core/TimerTest.cpp =================================================================== --- lldb/trunk/unittests/Core/TimerTest.cpp +++ lldb/trunk/unittests/Core/TimerTest.cpp @@ -1,73 +0,0 @@ -//===-- TimerTest.cpp -------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Core/Timer.h" -#include "gtest/gtest.h" - -#include "lldb/Utility/StreamString.h" -#include - -using namespace lldb_private; - -TEST(TimerTest, CategoryTimes) { - Timer::ResetCategoryTimes(); - { - static Timer::Category tcat("CAT1"); - Timer t(tcat, ""); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - StreamString ss; - Timer::DumpCategoryTimes(&ss); - double seconds; - ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds)); - EXPECT_LT(0.001, seconds); - EXPECT_GT(0.1, seconds); -} - -TEST(TimerTest, CategoryTimesNested) { - Timer::ResetCategoryTimes(); - { - static Timer::Category tcat1("CAT1"); - Timer t1(tcat1, ""); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - // Explicitly testing the same category as above. - Timer t2(tcat1, ""); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - StreamString ss; - Timer::DumpCategoryTimes(&ss); - double seconds; - // It should only appear once. - ASSERT_EQ(ss.GetString().count("CAT1"), 1U); - ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds)); - EXPECT_LT(0.002, seconds); - EXPECT_GT(0.2, seconds); -} - -TEST(TimerTest, CategoryTimes2) { - Timer::ResetCategoryTimes(); - { - static Timer::Category tcat1("CAT1"); - Timer t1(tcat1, ""); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - static Timer::Category tcat2("CAT2"); - Timer t2(tcat2, ""); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - StreamString ss; - Timer::DumpCategoryTimes(&ss); - double seconds1, seconds2; - ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2", - &seconds1, &seconds2)) - << "String: " << ss.GetData(); - EXPECT_LT(0.01, seconds1); - EXPECT_GT(1, seconds1); - EXPECT_LT(0.001, seconds2); - EXPECT_GT(0.1, seconds2); -} Index: lldb/trunk/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp =================================================================== --- lldb/trunk/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp +++ lldb/trunk/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp @@ -12,10 +12,10 @@ #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h" #include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/TraceOptions.h" #include "lldb/Target/MemoryRegionInfo.h" #include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/StructuredData.h" +#include "lldb/Utility/TraceOptions.h" #include "lldb/lldb-enumerations.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/Testing/Support/Error.h" Index: lldb/trunk/unittests/Utility/CMakeLists.txt =================================================================== --- lldb/trunk/unittests/Utility/CMakeLists.txt +++ lldb/trunk/unittests/Utility/CMakeLists.txt @@ -10,6 +10,7 @@ TaskPoolTest.cpp TildeExpressionResolverTest.cpp TimeoutTest.cpp + TimerTest.cpp UriParserTest.cpp VASprintfTest.cpp Index: lldb/trunk/unittests/Utility/TimerTest.cpp =================================================================== --- lldb/trunk/unittests/Utility/TimerTest.cpp +++ lldb/trunk/unittests/Utility/TimerTest.cpp @@ -0,0 +1,72 @@ +//===-- TimerTest.cpp -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Utility/StreamString.h" +#include "lldb/Utility/Timer.h" +#include "gtest/gtest.h" +#include + +using namespace lldb_private; + +TEST(TimerTest, CategoryTimes) { + Timer::ResetCategoryTimes(); + { + static Timer::Category tcat("CAT1"); + Timer t(tcat, ""); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + StreamString ss; + Timer::DumpCategoryTimes(&ss); + double seconds; + ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds)); + EXPECT_LT(0.001, seconds); + EXPECT_GT(0.1, seconds); +} + +TEST(TimerTest, CategoryTimesNested) { + Timer::ResetCategoryTimes(); + { + static Timer::Category tcat1("CAT1"); + Timer t1(tcat1, ""); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + // Explicitly testing the same category as above. + Timer t2(tcat1, ""); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + StreamString ss; + Timer::DumpCategoryTimes(&ss); + double seconds; + // It should only appear once. + ASSERT_EQ(ss.GetString().count("CAT1"), 1U); + ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds)); + EXPECT_LT(0.002, seconds); + EXPECT_GT(0.2, seconds); +} + +TEST(TimerTest, CategoryTimes2) { + Timer::ResetCategoryTimes(); + { + static Timer::Category tcat1("CAT1"); + Timer t1(tcat1, ""); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + static Timer::Category tcat2("CAT2"); + Timer t2(tcat2, ""); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + StreamString ss; + Timer::DumpCategoryTimes(&ss); + double seconds1, seconds2; + ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2", + &seconds1, &seconds2)) + << "String: " << ss.GetData(); + EXPECT_LT(0.01, seconds1); + EXPECT_GT(1, seconds1); + EXPECT_LT(0.001, seconds2); + EXPECT_GT(0.1, seconds2); +}