Details: - Previously, we used the LLVM_BUILD_TELEMETRY flag to control whether any Telemetry code will be built. This has proven to cause more nuisance to both users of the Telemetry and any further extension of it. (Eg., we needed to put #ifdef around caller/user code) - So the new approach is to: + Remove this flag and introduce LLVM_ENABLE_TELEMETRY which would be true by default. + If LLVM_ENABLE_TELEMETRY is set to FALSE (at buildtime), the library would still be built BUT Telemetry cannot be enabled. And no data can be collected. The benefit of this is that it simplifies user (and extension) code since we just need to put the check on Config::EnableTelemetry. Besides, the Telemetry library itself is very small, hence the additional code to be built would not cause any difference in build performance. --------- Co-authored-by: Pavel Labath <pavel@labath.sk>
77 lines
2.5 KiB
C++
77 lines
2.5 KiB
C++
//===-- 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 <chrono>
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace lldb_private {
|
|
namespace telemetry {
|
|
|
|
using namespace llvm::telemetry;
|
|
|
|
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()));
|
|
}
|
|
|
|
[[maybe_unused]] static std::string MakeUUID(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 UUID(random_bytes).GetAsString();
|
|
}
|
|
|
|
TelemetryManager::TelemetryManager(std::unique_ptr<Config> 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 llvm::Error::success();
|
|
}
|
|
|
|
std::unique_ptr<TelemetryManager> TelemetryManager::g_instance = nullptr;
|
|
TelemetryManager *TelemetryManager::GetInstance() {
|
|
if (!Config::BuildTimeEnableTelemetry)
|
|
return nullptr;
|
|
return g_instance.get();
|
|
}
|
|
|
|
void TelemetryManager::SetInstance(std::unique_ptr<TelemetryManager> manager) {
|
|
g_instance = std::move(manager);
|
|
}
|
|
|
|
} // namespace telemetry
|
|
} // namespace lldb_private
|