Skip to content

Commit 6aead70

Browse files
committed
feat(runtime): run-time variant switch for benchmark builds
-PbenchVariants (CMake NS_BENCH_VARIANTS, off by default) makes the runtime read debug.ns.benchvariant once when the library loads. C++ reads it through BenchVariant() and JS through the read-only __benchVariant global, so one benchmark APK can carry every label of an A/B. The runtime logs NS_BENCH_VARIANT=<n> at startup so a harness can reject a launch that ran the wrong variant. Without the option BenchVariant() is a constant 0, nothing reads the property and there is no JS global. The runtime's ccache switch also accepts -PuseCcache and NS_USE_CCACHE=1.
1 parent 56c7b0b commit 6aead70

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎test-app/runtime/CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ if (CCACHE_FOUND AND (USE_CCACHE))
1313
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
1414
endif (CCACHE_FOUND AND (USE_CCACHE))
1515

16+
# Benchmark builds only (tools/perf): BenchVariant.h reads debug.ns.benchvariant
17+
# at load time so one APK can carry every label of an A/B. Never enable it for a
18+
# build that ships.
19+
option(NS_BENCH_VARIANTS "Read the run-time benchmark variant from debug.ns.benchvariant" OFF)
20+
if (NS_BENCH_VARIANTS)
21+
MESSAGE(STATUS "## NS_BENCH_VARIANTS is ON: benchmark build, do not ship")
22+
add_definitions(-DNS_BENCH_VARIANTS)
23+
endif ()
24+
1625
# Command info: https://cmake.org/cmake/help/v3.4/command/message.html
1726
# we pass the android_ndk_root from gradle because for some reason
1827
# "-DANDROID_STL=c++_static" is just not enough for clang++ to find some libraries in the ndk

‎test-app/runtime/build.gradle‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ if (onlyX86) {
2424
println "OnlyX86 build triggered."
2525
}
2626

27-
def useCCache = project.hasProperty("useCCache")
27+
// -PuseCCache (CI and packaging), its -PuseCcache spelling, or NS_USE_CCACHE=1 in the environment.
28+
def useCCache = project.hasProperty("useCCache") || project.hasProperty("useCcache") ||
29+
System.getenv("NS_USE_CCACHE") == "1"
2830
if (useCCache) {
2931
println "Use CCache build triggered."
3032
}
3133

34+
def benchVariants = project.hasProperty("benchVariants")
35+
if (benchVariants) {
36+
println "Benchmark variant switch (NS_BENCH_VARIANTS) enabled: do not ship this build."
37+
}
38+
3239

3340
def defaultNdkVersion = "29.0.14206865"
3441

@@ -120,6 +127,10 @@ android {
120127
arguments.add("-DUSE_CCACHE=true")
121128
}
122129

130+
if (benchVariants) {
131+
arguments.add("-DNS_BENCH_VARIANTS=ON")
132+
}
133+
123134
cppFlags "-std=c++20"
124135
arguments "-DANDROID_STL=c++_static", "-DANDROID_NDK_ROOT=${NDK_PATH}", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
125136
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef BENCHVARIANT_H_
2+
#define BENCHVARIANT_H_
3+
4+
// Run-time variant switch for benchmark builds (tools/perf/README.md).
5+
//
6+
// Configured with -PbenchVariants (CMake NS_BENCH_VARIANTS=ON), the runtime
7+
// reads the system property debug.ns.benchvariant once, when the library
8+
// loads, so one APK can carry every label of an A/B as
9+
// `if (BenchVariant() == N)` branches. Without the option BenchVariant() is a
10+
// constant 0: the branches fold away and the property is never read.
11+
12+
#ifdef NS_BENCH_VARIANTS
13+
#include <sys/system_properties.h>
14+
15+
#include <cstdlib>
16+
#endif
17+
18+
namespace tns {
19+
20+
#ifdef NS_BENCH_VARIANTS
21+
22+
inline int g_benchVariant = 0;
23+
24+
inline int BenchVariant() { return g_benchVariant; }
25+
26+
// Called once from JNI_OnLoad, before any runtime exists.
27+
inline void InitBenchVariant() {
28+
char value[PROP_VALUE_MAX] = {0};
29+
__system_property_get("debug.ns.benchvariant", value);
30+
g_benchVariant = static_cast<int>(std::strtol(value, nullptr, 10));
31+
}
32+
33+
#else
34+
35+
constexpr int BenchVariant() { return 0; }
36+
37+
inline void InitBenchVariant() {}
38+
39+
#endif
40+
41+
} // namespace tns
42+
43+
#endif // BENCHVARIANT_H_

‎test-app/runtime/src/main/cpp/Runtime.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <thread>
1313

1414
#include "ArgConverter.h"
15+
#include "BenchVariant.h"
1516
#include "BuiltinLoader.h"
1617
#include "CallbackHandlers.h"
1718
#include "Constants.h"
@@ -99,6 +100,13 @@ void Runtime::Init(JavaVM* vm, void* reserved) {
99100
NATIVE_SCRIPT_RUNTIME_VERSION,
100101
NATIVE_SCRIPT_RUNTIME_COMMIT_SHA);
101102

103+
#ifdef NS_BENCH_VARIANTS
104+
InitBenchVariant();
105+
// tools/perf checks this line to reject a launch that ran the wrong variant.
106+
__android_log_print(ANDROID_LOG_INFO, "TNS.Runtime", "NS_BENCH_VARIANT=%d",
107+
BenchVariant());
108+
#endif
109+
102110
if (Runtime::s_jvm == nullptr) {
103111
s_jvm = vm;
104112

@@ -1044,6 +1052,11 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath,
10441052
globalTemplate->Set(
10451053
ArgConverter::ConvertToV8String(isolate, "__time"),
10461054
FunctionTemplate::New(isolate, CallbackHandlers::TimeCallback));
1055+
#ifdef NS_BENCH_VARIANTS
1056+
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__benchVariant"),
1057+
v8::Integer::New(isolate, BenchVariant()),
1058+
readOnlyFlags);
1059+
#endif
10471060

10481061
// queueMicrotask(callback) per spec:
10491062
// https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask

0 commit comments

Comments
 (0)