Skip to content

Commit 159b872

Browse files
oontvoolabath
andauthored
[llvm][telemetry]Change Telemetry-disabling mechanism. (#128534)
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>
1 parent 317461e commit 159b872

File tree

14 files changed

+40
-42
lines changed

14 files changed

+40
-42
lines changed

lldb/source/Core/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ if (LLDB_ENABLE_CURSES)
1616
endif()
1717
endif()
1818

19-
if (LLVM_BUILD_TELEMETRY)
20-
set(TELEMETRY_DEPS Telemetry)
21-
endif()
22-
2319
# TODO: Add property `NO_PLUGIN_DEPENDENCIES` to lldbCore
2420
add_lldb_library(lldbCore
2521
Address.cpp
@@ -84,7 +80,7 @@ add_lldb_library(lldbCore
8480
Support
8581
Demangle
8682
TargetParser
87-
${TELEMETRY_DEPS}
83+
Telemetry
8884
)
8985

9086
add_dependencies(lldbCore

lldb/source/Core/Telemetry.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
9-
#include "llvm/Config/llvm-config.h"
10-
11-
#ifdef LLVM_BUILD_TELEMETRY
12-
138
#include "lldb/Core/Telemetry.h"
149
#include "lldb/Core/Debugger.h"
1510
#include "lldb/Utility/LLDBLog.h"
@@ -67,13 +62,15 @@ llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) {
6762
}
6863

6964
std::unique_ptr<TelemetryManager> TelemetryManager::g_instance = nullptr;
70-
TelemetryManager *TelemetryManager::GetInstance() { return g_instance.get(); }
65+
TelemetryManager *TelemetryManager::GetInstance() {
66+
if (!Config::BuildTimeEnableTelemetry)
67+
return nullptr;
68+
return g_instance.get();
69+
}
7170

7271
void TelemetryManager::SetInstance(std::unique_ptr<TelemetryManager> manager) {
7372
g_instance = std::move(manager);
7473
}
7574

7675
} // namespace telemetry
7776
} // namespace lldb_private
78-
79-
#endif // LLVM_BUILD_TELEMETRY

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
if (LLVM_BUILD_TELEMETRY)
2-
set(TELEMETRY_DEPS Telemetry)
3-
endif()
41

52
add_lldb_unittest(LLDBCoreTests
63
CommunicationTest.cpp
@@ -31,5 +28,5 @@ add_lldb_unittest(LLDBCoreTests
3128
LLVMTestingSupport
3229
LINK_COMPONENTS
3330
Support
34-
${TELEMETRY_DEPS}
31+
Telemetry
3532
)

lldb/unittests/Core/TelemetryTest.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
9-
#include "llvm/Config/llvm-config.h"
10-
11-
#ifdef LLVM_BUILD_TELEMETRY
12-
138
#include "lldb/Core/PluginInterface.h"
149
#include "lldb/Core/PluginManager.h"
1510
#include "lldb/Core/Telemetry.h"
@@ -71,7 +66,13 @@ class FakePlugin : public telemetry::TelemetryManager {
7166

7267
} // namespace lldb_private
7368

74-
TEST(TelemetryTest, PluginTest) {
69+
#if LLVM_ENABLE_TELEMETRY
70+
#define TELEMETRY_TEST(suite, test) TEST(suite, test)
71+
#else
72+
#define TELEMETRY_TEST(suite, test) TEST(DISABLED_##suite, test)
73+
#endif
74+
75+
TELEMETRY_TEST(TelemetryTest, PluginTest) {
7576
// This would have been called by the plugin reg in a "real" plugin
7677
// For tests, we just call it directly.
7778
lldb_private::FakePlugin::Initialize();
@@ -94,5 +95,3 @@ TEST(TelemetryTest, PluginTest) {
9495

9596
ASSERT_EQ("FakeTelemetryPlugin", ins->GetInstanceName());
9697
}
97-
98-
#endif // LLVM_BUILD_TELEMETRY

llvm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ option (LLVM_ENABLE_DOXYGEN "Use doxygen to generate llvm API documentation." OF
835835
option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF)
836836
option (LLVM_ENABLE_OCAMLDOC "Build OCaml bindings documentation." ON)
837837
option (LLVM_ENABLE_BINDINGS "Build bindings." ON)
838-
option (LLVM_BUILD_TELEMETRY "Build the telemetry library. This does not enable telemetry." ON)
838+
option (LLVM_ENABLE_TELEMETRY "Enable the telemetry library. If set to OFF, library cannot be enabled after build (eg., at runtime)" ON)
839839

840840
set(LLVM_INSTALL_DOXYGEN_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html"
841841
CACHE STRING "Doxygen-generated HTML documentation install directory")

llvm/cmake/modules/LLVMConfig.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ set(LLVM_ENABLE_PIC @LLVM_ENABLE_PIC@)
100100

101101
set(LLVM_BUILD_32_BITS @LLVM_BUILD_32_BITS@)
102102

103-
set(LLVM_BUILD_TELEMETRY @LLVM_BUILD_TELEMETRY@)
103+
set(LLVM_ENABLE_TELEMETRY @LLVM_ENABLE_TELEMETRY@)
104104

105105
if (NOT "@LLVM_PTHREAD_LIB@" STREQUAL "")
106106
set(LLVM_PTHREAD_LIB "@LLVM_PTHREAD_LIB@")

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
/* Define if logf128 is available */
202202
#cmakedefine LLVM_HAS_LOGF128
203203

204-
/* Define if building LLVM with LLVM_BUILD_TELEMETRY */
205-
#cmakedefine LLVM_BUILD_TELEMETRY ${LLVM_BUILD_TELEMETRY}
204+
/* Define if building LLVM with LLVM_ENABLE_TELEMETRY */
205+
#cmakedefine01 LLVM_ENABLE_TELEMETRY
206206

207207
#endif

llvm/include/llvm/Telemetry/Telemetry.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ class Serializer {
6464
/// This struct can be extended as needed to add additional configuration
6565
/// points specific to a vendor's implementation.
6666
struct Config {
67-
virtual ~Config() = default;
67+
static constexpr bool BuildTimeEnableTelemetry = LLVM_ENABLE_TELEMETRY;
6868

6969
// If true, telemetry will be enabled.
7070
const bool EnableTelemetry;
71-
Config(bool E) : EnableTelemetry(E) {}
71+
72+
explicit Config() : EnableTelemetry(BuildTimeEnableTelemetry) {}
73+
74+
// Telemetry can only be enabled if both the runtime and buildtime flag
75+
// are set.
76+
explicit Config(bool E) : EnableTelemetry(E && BuildTimeEnableTelemetry) {}
7277

7378
virtual std::optional<std::string> makeSessionId() { return std::nullopt; }
7479
};

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ add_subdirectory(ProfileData)
4141
add_subdirectory(Passes)
4242
add_subdirectory(TargetParser)
4343
add_subdirectory(TextAPI)
44-
if (LLVM_BUILD_TELEMETRY)
45-
add_subdirectory(Telemetry)
46-
endif()
44+
add_subdirectory(Telemetry)
4745
add_subdirectory(ToolDrivers)
4846
add_subdirectory(XRay)
4947
if (LLVM_INCLUDE_TESTS)

llvm/lib/Telemetry/Telemetry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void TelemetryInfo::serialize(Serializer &serializer) const {
2121
}
2222

2323
Error Manager::dispatch(TelemetryInfo *Entry) {
24+
assert(Config::BuildTimeEnableTelemetry &&
25+
"Telemetry should have been enabled");
2426
if (Error Err = preDispatch(Entry))
2527
return Err;
2628

llvm/unittests/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ add_subdirectory(Support)
6363
add_subdirectory(TableGen)
6464
add_subdirectory(Target)
6565
add_subdirectory(TargetParser)
66-
if (LLVM_BUILD_TELEMETRY)
67-
add_subdirectory(Telemetry)
68-
endif()
66+
add_subdirectory(Telemetry)
6967
add_subdirectory(Testing)
7068
add_subdirectory(TextAPI)
7169
add_subdirectory(Transforms)

llvm/unittests/Telemetry/TelemetryTest.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ std::shared_ptr<Config> getTelemetryConfig(const TestContext &Ctxt) {
212212
return std::make_shared<Config>(false);
213213
}
214214

215-
TEST(TelemetryTest, TelemetryDisabled) {
215+
#if LLVM_ENABLE_TELEMETRY
216+
#define TELEMETRY_TEST(suite, test) TEST(suite, test)
217+
#else
218+
#define TELEMETRY_TEST(suite, test) TEST(DISABLED_##suite, test)
219+
#endif
220+
221+
TELEMETRY_TEST(TelemetryTest, TelemetryDisabled) {
216222
TestContext Context;
217223
Context.HasVendorPlugin = false;
218224

@@ -221,7 +227,7 @@ TEST(TelemetryTest, TelemetryDisabled) {
221227
EXPECT_EQ(nullptr, Manager);
222228
}
223229

224-
TEST(TelemetryTest, TelemetryEnabled) {
230+
TELEMETRY_TEST(TelemetryTest, TelemetryEnabled) {
225231
const std::string ToolName = "TelemetryTestTool";
226232

227233
// Preset some params.

llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ write_cmake_config("llvm-config") {
292292
values = [
293293
"LLVM_BUILD_LLVM_DYLIB=",
294294
"LLVM_BUILD_SHARED_LIBS=",
295-
"LLVM_BUILD_TELEMETRY=",
295+
"LLVM_ENABLE_TELEMETRY=",
296296
"LLVM_DEFAULT_TARGET_TRIPLE=$llvm_target_triple",
297297
"LLVM_ENABLE_DUMP=",
298298
"LLVM_ENABLE_HTTPLIB=",

utils/bazel/llvm_configs/llvm-config.h.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
/* Define if logf128 is available */
202202
#cmakedefine LLVM_HAS_LOGF128
203203

204-
/* Define if building LLVM with LLVM_BUILD_TELEMETRY */
205-
#cmakedefine LLVM_BUILD_TELEMETRY ${LLVM_BUILD_TELEMETRY}
204+
/* Define if building LLVM with LLVM_ENABLE_TELEMETRY */
205+
#cmakedefine01 LLVM_ENABLE_TELEMETRY
206206

207207
#endif

0 commit comments

Comments
 (0)