diff --git a/lldb/include/lldb/Core/Telemetry.h b/lldb/include/lldb/Core/Telemetry.h new file mode 100644 index 0000000000000..60a7097de5eee --- /dev/null +++ b/lldb/include/lldb/Core/Telemetry.h @@ -0,0 +1,74 @@ +//===-- Telemetry.h -------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_CORE_TELEMETRY_H +#define LLDB_CORE_TELEMETRY_H + +#include "lldb/Core/StructuredDataImpl.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Utility/StructuredData.h" +#include "lldb/lldb-forward.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/JSON.h" +#include "llvm/Telemetry/Telemetry.h" +#include +#include +#include +#include +#include +#include + +namespace lldb_private { +namespace telemetry { + +struct LLDBEntryKind : public ::llvm::telemetry::EntryKind { + static const llvm::telemetry::KindType BaseInfo = 0b11000; +}; + +/// Defines a convenient type for timestamp of various events. +using SteadyTimePoint = std::chrono::time_point; +struct LLDBBaseTelemetryInfo : public llvm::telemetry::TelemetryInfo { + /// Start time of an event + SteadyTimePoint start_time; + /// End time of an event - may be empty if not meaningful. + std::optional end_time; + // TBD: could add some memory stats here too? + + Debugger *debugger; + + // For dyn_cast, isa, etc operations. + llvm::telemetry::KindType getKind() const override { + return LLDBEntryKind::BaseInfo; + } + + static bool classof(const llvm::telemetry::TelemetryInfo *t) { + // Subclasses of this is also acceptable. + return (t->getKind() & LLDBEntryKind::BaseInfo) == LLDBEntryKind::BaseInfo; + } + + void serialize(llvm::telemetry::Serializer &serializer) const override; +}; + +/// The base Telemetry manager instance in LLDB +/// This class declares additional instrumentation points +/// applicable to LLDB. +class TelemetryManager : public llvm::telemetry::Manager { +public: + TelemetryManager(std::unique_ptr config); + + llvm::Error preDispatch(llvm::telemetry::TelemetryInfo *entry) override; + +private: + std::unique_ptr m_config; +}; + +} // namespace telemetry +} // namespace lldb_private +#endif // LLDB_CORE_TELEMETRY_H diff --git a/lldb/source/Core/CMakeLists.txt b/lldb/source/Core/CMakeLists.txt index 6d14f7a87764e..4b1f418162016 100644 --- a/lldb/source/Core/CMakeLists.txt +++ b/lldb/source/Core/CMakeLists.txt @@ -16,6 +16,11 @@ if (LLDB_ENABLE_CURSES) endif() endif() +if (LLVM_BUILD_TELEMETRY) + set(TELEMETRY_SOURCES Telemetry.cpp) + set(TELEMETRY_DEPS Telemetry) +endif() + # TODO: Add property `NO_PLUGIN_DEPENDENCIES` to lldbCore add_lldb_library(lldbCore Address.cpp @@ -55,7 +60,8 @@ add_lldb_library(lldbCore ThreadedCommunication.cpp UserSettingsController.cpp Value.cpp - + ${TELEMETRY_SOURCES} + PARTIAL_SOURCES_INTENDED DEPENDS clang-tablegen-targets @@ -80,6 +86,7 @@ add_lldb_library(lldbCore Support Demangle TargetParser + ${TELEMETRY_DEPS} ) add_dependencies(lldbCore diff --git a/lldb/source/Core/Telemetry.cpp b/lldb/source/Core/Telemetry.cpp new file mode 100644 index 0000000000000..a474a7663a018 --- /dev/null +++ b/lldb/source/Core/Telemetry.cpp @@ -0,0 +1,69 @@ +//===-- Telemetry.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +#include "lldb/Core/Telemetry.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/UUID.h" +#include "lldb/lldb-enumerations.h" +#include "lldb/lldb-forward.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/RandomNumberGenerator.h" +#include "llvm/Telemetry/Telemetry.h" +#include +#include +#include +#include +#include + +namespace lldb_private { +namespace telemetry { + +using ::llvm::Error; +using ::llvm::telemetry::Destination; +using ::llvm::telemetry::Serializer; +using ::llvm::telemetry::TelemetryInfo; + +static uint64_t ToNanosec(const SteadyTimePoint Point) { + return std::chrono::nanoseconds(Point.time_since_epoch()).count(); +} + +void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const { + serializer.write("entry_kind", getKind()); + serializer.write("session_id", SessionId); + serializer.write("start_time", ToNanosec(start_time)); + if (end_time.has_value()) + serializer.write("end_time", ToNanosec(end_time.value())); +} + +static std::string MakeUUID(lldb_private::Debugger *debugger) { + uint8_t random_bytes[16]; + if (auto ec = llvm::getRandomBytes(random_bytes, 16)) { + LLDB_LOG(GetLog(LLDBLog::Object), + "Failed to generate random bytes for UUID: {0}", ec.message()); + // fallback to using timestamp + debugger ID. + return llvm::formatv( + "{0}_{1}", std::chrono::steady_clock::now().time_since_epoch().count(), + debugger->GetID()); + } + return lldb_private::UUID(random_bytes).GetAsString(); +} + +TelemetryManager::TelemetryManager( + std::unique_ptr config) + : m_config(std::move(config)) {} + +llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) { + // Do nothing for now. + // In up-coming patch, this would be where the manager + // attach the session_uuid to the entry. + return Error::success(); +} + +} // namespace telemetry +} // namespace lldb_private