diff --git a/MODULE.bazel b/MODULE.bazel index d9ce1fb15b7afe..84e08db9388696 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -98,7 +98,7 @@ maven.install( "com.google.auto.value:auto-value-annotations:1.11.0", "com.google.code.findbugs:jsr305:3.0.2", "com.google.code.gson:gson:2.9.0", - "com.google.code.java-allocation-instrumenter:java-allocation-instrumenter:3.3.0", + "com.google.code.java-allocation-instrumenter:java-allocation-instrumenter:3.3.4", "com.google.errorprone:error_prone_annotation:2.22.0", "com.google.errorprone:error_prone_annotations:2.22.0", "com.google.errorprone:error_prone_check_api:2.22.0", diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 20dc7954ef598c..4fb07dda777e33 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -1290,11 +1290,11 @@ "@@rules_jvm_external~//:extensions.bzl%maven": { "general": { "bzlTransitiveDigest": "6HjpYwTb4aw3ujkkpdKEvg9geToiH1ebaT6hmsvF5xw=", - "usagesDigest": "GDnun3+UYCfO0VT0ashmwTVdZp5HrtUpu+S9LHigYpQ=", + "usagesDigest": "tau7Qd3JFn6zPkH7rMjkFN0UpoTsDcXLcHUMlE6517s=", "recordedFileInputs": { "@@//src/tools/android/maven_android_install.json": "09bff3e33d291336046f7c9201630fb5e014f0e60b78b6f09b84e4f5f73ed04f", "@@rules_jvm_external~//rules_jvm_external_deps_install.json": "cafb5d2d8119391eb2b322ce3840d3352ea82d496bdb8cbd4b6779ec4d044dda", - "@@//maven_install.json": "f1b04e79794fe014666c5ad52eba53f924fab0865f3ae755bb34cb951af39976" + "@@//maven_install.json": "94d5ed6992789187930f2fb021e93a0ef7671cac798adb860fd78eece2364fc2" }, "recordedDirentsInputs": {}, "envVariables": {}, @@ -1938,7 +1938,7 @@ "{ \"group\": \"com.google.auto.value\", \"artifact\": \"auto-value-annotations\", \"version\": \"1.11.0\" }", "{ \"group\": \"com.google.code.findbugs\", \"artifact\": \"jsr305\", \"version\": \"3.0.2\" }", "{ \"group\": \"com.google.code.gson\", \"artifact\": \"gson\", \"version\": \"2.9.0\" }", - "{ \"group\": \"com.google.code.java-allocation-instrumenter\", \"artifact\": \"java-allocation-instrumenter\", \"version\": \"3.3.0\" }", + "{ \"group\": \"com.google.code.java-allocation-instrumenter\", \"artifact\": \"java-allocation-instrumenter\", \"version\": \"3.3.4\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_annotation\", \"version\": \"2.22.0\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_annotations\", \"version\": \"2.22.0\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_check_api\", \"version\": \"2.22.0\" }", @@ -2836,17 +2836,6 @@ "downloaded_file_path": "v1/io/grpc/grpc-auth/1.56.1/grpc-auth-1.56.1.jar" } }, - "com_google_code_java_allocation_instrumenter_java_allocation_instrumenter_3_3_0": { - "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", - "ruleClassName": "http_file", - "attributes": { - "sha256": "1ef5535a8bd41cf3072469f381b9ee6ab28275311a7499f53d6e52adf976fef0", - "urls": [ - "https://repo1.maven.org/maven2/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.0/java-allocation-instrumenter-3.3.0.jar" - ], - "downloaded_file_path": "v1/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.0/java-allocation-instrumenter-3.3.0.jar" - } - }, "com_google_turbine_turbine_0_6_0": { "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", "ruleClassName": "http_file", @@ -3065,6 +3054,17 @@ "downloaded_file_path": "v1/com/google/guava/guava-testlib/31.1-jre/guava-testlib-31.1-jre.jar" } }, + "com_google_code_java_allocation_instrumenter_java_allocation_instrumenter_3_3_4": { + "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", + "ruleClassName": "http_file", + "attributes": { + "sha256": "44f8cddec129520b2532fa9ff25f9572d7566307d660635ba32bf409f06ae336", + "urls": [ + "https://repo1.maven.org/maven2/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.4/java-allocation-instrumenter-3.3.4.jar" + ], + "downloaded_file_path": "v1/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.4/java-allocation-instrumenter-3.3.4.jar" + } + }, "com_google_http_client_google_http_client_1_42_0": { "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", "ruleClassName": "http_file", @@ -4595,7 +4595,7 @@ "{ \"group\": \"com.google.auto.value\", \"artifact\": \"auto-value-annotations\", \"version\": \"1.11.0\" }", "{ \"group\": \"com.google.code.findbugs\", \"artifact\": \"jsr305\", \"version\": \"3.0.2\" }", "{ \"group\": \"com.google.code.gson\", \"artifact\": \"gson\", \"version\": \"2.9.0\" }", - "{ \"group\": \"com.google.code.java-allocation-instrumenter\", \"artifact\": \"java-allocation-instrumenter\", \"version\": \"3.3.0\" }", + "{ \"group\": \"com.google.code.java-allocation-instrumenter\", \"artifact\": \"java-allocation-instrumenter\", \"version\": \"3.3.4\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_annotation\", \"version\": \"2.22.0\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_annotations\", \"version\": \"2.22.0\" }", "{ \"group\": \"com.google.errorprone\", \"artifact\": \"error_prone_check_api\", \"version\": \"2.22.0\" }", diff --git a/maven_install.json b/maven_install.json index 16ea13422c6b5d..9ceef7ae97af86 100644 --- a/maven_install.json +++ b/maven_install.json @@ -1,7 +1,7 @@ { "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", - "__INPUT_ARTIFACTS_HASH": 1071623342, - "__RESOLVED_ARTIFACTS_HASH": 1286859829, + "__INPUT_ARTIFACTS_HASH": -669893718, + "__RESOLVED_ARTIFACTS_HASH": -1421116745, "conflict_resolution": { "com.google.code.gson:gson:2.8.9": "com.google.code.gson:gson:2.9.0", "com.google.errorprone:error_prone_annotations:2.3.2": "com.google.errorprone:error_prone_annotations:2.22.0", @@ -122,9 +122,9 @@ }, "com.google.code.java-allocation-instrumenter:java-allocation-instrumenter": { "shasums": { - "jar": "1ef5535a8bd41cf3072469f381b9ee6ab28275311a7499f53d6e52adf976fef0" + "jar": "44f8cddec129520b2532fa9ff25f9572d7566307d660635ba32bf409f06ae336" }, - "version": "3.3.0" + "version": "3.3.4" }, "com.google.errorprone:error_prone_annotation": { "shasums": { @@ -824,15 +824,6 @@ "com.google.auto:auto-common": [ "com.google.guava:guava" ], - "com.google.code.java-allocation-instrumenter:java-allocation-instrumenter": [ - "com.google.guava:guava", - "javax.annotation:javax.annotation-api", - "org.ow2.asm:asm", - "org.ow2.asm:asm-analysis", - "org.ow2.asm:asm-commons", - "org.ow2.asm:asm-tree", - "org.ow2.asm:asm-util" - ], "com.google.errorprone:error_prone_annotation": [ "com.google.guava:guava" ], @@ -1417,30 +1408,16 @@ "com.google.monitoring.runtime.instrumentation.asm.commons", "com.google.monitoring.runtime.instrumentation.asm.signature", "com.google.monitoring.runtime.instrumentation.asm.tree", - "com.google.monitoring.runtime.instrumentation.asm.tree.analysis", - "com.google.monitoring.runtime.instrumentation.asm.util", - "com.google.monitoring.runtime.instrumentation.checker.nullness.compatqual", - "com.google.monitoring.runtime.instrumentation.common.annotations", - "com.google.monitoring.runtime.instrumentation.common.base", - "com.google.monitoring.runtime.instrumentation.common.base.internal", - "com.google.monitoring.runtime.instrumentation.common.cache", - "com.google.monitoring.runtime.instrumentation.common.collect", - "com.google.monitoring.runtime.instrumentation.common.escape", - "com.google.monitoring.runtime.instrumentation.common.eventbus", - "com.google.monitoring.runtime.instrumentation.common.graph", - "com.google.monitoring.runtime.instrumentation.common.hash", - "com.google.monitoring.runtime.instrumentation.common.html", - "com.google.monitoring.runtime.instrumentation.common.io", - "com.google.monitoring.runtime.instrumentation.common.math", - "com.google.monitoring.runtime.instrumentation.common.net", - "com.google.monitoring.runtime.instrumentation.common.primitives", - "com.google.monitoring.runtime.instrumentation.common.reflect", - "com.google.monitoring.runtime.instrumentation.common.util.concurrent", - "com.google.monitoring.runtime.instrumentation.common.xml", - "com.google.monitoring.runtime.instrumentation.errorprone.annotations", - "com.google.monitoring.runtime.instrumentation.errorprone.annotations.concurrent", - "com.google.monitoring.runtime.instrumentation.j2objc.annotations", - "com.google.thirdparty.publicsuffix" + "com.google.monitoring.runtime.instrumentation.common.com.google.common.annotations", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.base", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.cache", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.collect", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.math", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.primitives", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.util.concurrent", + "com.google.monitoring.runtime.instrumentation.common.com.google.common.util.concurrent.internal", + "javax.annotation", + "javax.annotation.security" ], "com.google.errorprone:error_prone_annotation": [ "com.google.errorprone" diff --git a/site/en/docs/user-manual.md b/site/en/docs/user-manual.md index e37f87339ed742..579f82a30e4ef0 100644 --- a/site/en/docs/user-manual.md +++ b/site/en/docs/user-manual.md @@ -2182,12 +2182,12 @@ Following options are supported: Some `dump` commands require memory tracking. To turn this on, you have to pass startup flags to Bazel: -* `--host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar` +* `--host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.4.jar` * `--host_jvm_args=-DRULE_MEMORY_TRACKER=1` The java-agent is checked into Bazel at -`third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar`, so make -sure you adjust `$BAZEL` for where you keep your Bazel repository. +`third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.4.jar`, so +make sure you adjust `$BAZEL` for where you keep your Bazel repository. Do not forget to keep passing these options to Bazel for every command or the server will restart. @@ -2195,17 +2195,17 @@ restart. Example:
- % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar \ + % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.4.jar \ --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \ build --nobuild <targets> # Dump rules - % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar \ + % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.4.jar \ --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \ dump --rules # Dump Starlark heap and analyze it with pprof - % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar \ + % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.4.jar \ --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \ dump --skylark_memory=$HOME/prof.gz % pprof -flame $HOME/prof.gz diff --git a/site/en/rules/performance.md b/site/en/rules/performance.md index f2aec17efe3192..308b4c040dfa65 100644 --- a/site/en/rules/performance.md +++ b/site/en/rules/performance.md @@ -218,13 +218,13 @@ You must pass these two startup flags to *every* Bazel invocation: ``` STARTUP_FLAGS=\ - --host_jvm_args=-javaagent:\ + --host_jvm_args=-javaagent: \ --host_jvm_args=-DRULE_MEMORY_TRACKER=1 ``` Note: You can download the allocation instrumenter jar file from [Maven Central Repository][allocation-instrumenter-link]. -[allocation-instrumenter-link]: https://repo1.maven.org/maven2/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.0 +[allocation-instrumenter-link]: https://repo1.maven.org/maven2/com/google/code/java-allocation-instrumenter/java-allocation-instrumenter/3.3.4 These start the server in memory tracking mode. If you forget these for even one Bazel invocation the server will restart and you will have to start over. diff --git a/src/main/java/com/google/devtools/build/lib/profiler/memory/AllocationTrackerModule.java b/src/main/java/com/google/devtools/build/lib/profiler/memory/AllocationTrackerModule.java index 2c6034d137d9f1..8b0465cef207cc 100644 --- a/src/main/java/com/google/devtools/build/lib/profiler/memory/AllocationTrackerModule.java +++ b/src/main/java/com/google/devtools/build/lib/profiler/memory/AllocationTrackerModule.java @@ -18,8 +18,10 @@ import com.google.devtools.build.lib.analysis.BlazeVersionInfo; import com.google.devtools.build.lib.analysis.ServerDirectories; import com.google.devtools.build.lib.clock.Clock; +import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.runtime.BlazeModule; import com.google.devtools.build.lib.runtime.BlazeRuntime; +import com.google.devtools.build.lib.runtime.CommandEnvironment; import com.google.devtools.build.lib.runtime.WorkspaceBuilder; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.common.options.OptionsParsingResult; @@ -36,7 +38,7 @@ * --host_jvm_args=-javaagent:(path to Google's java agent jar) * *
*- For Bazel use java-allocation-instrumenter-3.3.0.jar + * href="https://github.com/bazelbuild/bazel/tree/master/third_party/allocation_instrumenter">java-allocation-instrumenter-3.3.4.jar *
--host_jvm_args=-DRULE_MEMORY_TRACKER=1 * @@ -55,7 +57,9 @@ public class AllocationTrackerModule extends BlazeModule { private static final int VARIANCE = 100; private boolean enabled; - private AllocationTracker tracker = null; + // Always AllocationTracker, but we don't refer to the type as it is supplied manually via a Java + // agent. + private Object tracker = null; @Override public void blazeStartup( @@ -65,13 +69,18 @@ public void blazeStartup( FileSystem fileSystem, ServerDirectories directories, Clock clock) { - String memoryTrackerPropery = System.getProperty("RULE_MEMORY_TRACKER"); - enabled = memoryTrackerPropery != null && memoryTrackerPropery.equals("1"); + enabled = isRequested(); if (enabled) { + try { + Class.forName("com.google.monitoring.runtime.instrumentation.Sampler"); + } catch (ClassNotFoundException e) { + enabled = false; + return; + } tracker = new AllocationTracker(SAMPLE_SIZE, VARIANCE); - Debug.setThreadHook(tracker); + Debug.setThreadHook((AllocationTracker) tracker); CurrentRuleTracker.setEnabled(true); - AllocationTrackerInstaller.installAllocationTracker(tracker); + AllocationTrackerInstaller.installAllocationTracker((AllocationTracker) tracker); } } @@ -79,7 +88,24 @@ public void blazeStartup( public void workspaceInit( BlazeRuntime runtime, BlazeDirectories directories, WorkspaceBuilder builder) { if (enabled) { - builder.setAllocationTracker(tracker); + builder.setAllocationTracker((AllocationTracker) tracker); } } + + @Override + public void beforeCommand(CommandEnvironment env) { + if (!enabled && isRequested()) { + env.getReporter() + .handle( + Event.error( + "Failed to enable memory tracking, ensure that you set" + + " --host_jvm_args=-javaagent: ")); + } + } + + private static boolean isRequested() { + String memoryTrackerProperty = System.getProperty("RULE_MEMORY_TRACKER"); + return memoryTrackerProperty != null && memoryTrackerProperty.equals("1"); + } } diff --git a/src/main/java/com/google/devtools/build/lib/profiler/memory/BUILD b/src/main/java/com/google/devtools/build/lib/profiler/memory/BUILD index 6d6bb68d84b7ed..21ed8260723cae 100644 --- a/src/main/java/com/google/devtools/build/lib/profiler/memory/BUILD +++ b/src/main/java/com/google/devtools/build/lib/profiler/memory/BUILD @@ -25,6 +25,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/analysis:blaze_version_info", "//src/main/java/com/google/devtools/build/lib/analysis:server_directories", "//src/main/java/com/google/devtools/build/lib/clock", + "//src/main/java/com/google/devtools/build/lib/events", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/common/options", "//src/main/java/net/starlark/java/eval", diff --git a/src/test/java/com/google/devtools/build/lib/profiler/memory/BUILD b/src/test/java/com/google/devtools/build/lib/profiler/memory/BUILD index a8590632c3fe0a..fe13688f870cf4 100644 --- a/src/test/java/com/google/devtools/build/lib/profiler/memory/BUILD +++ b/src/test/java/com/google/devtools/build/lib/profiler/memory/BUILD @@ -16,6 +16,7 @@ filegroup( java_test( name = "AllocationTrackerTest", srcs = ["AllocationTrackerTest.java"], + runtime_deps = ["//third_party/allocation_instrumenter:allocation_instrumenter_runtime"], deps = [ "//src/main/java/com/google/devtools/build/lib/packages", "//src/main/java/com/google/devtools/build/lib/profiler/memory:allocationtracker", diff --git a/src/test/java/com/google/devtools/common/options/OptionDefaultValueConversionTest.java b/src/test/java/com/google/devtools/common/options/OptionDefaultValueConversionTest.java index 41e397f3d21bef..94cec5d77bd8b7 100644 --- a/src/test/java/com/google/devtools/common/options/OptionDefaultValueConversionTest.java +++ b/src/test/java/com/google/devtools/common/options/OptionDefaultValueConversionTest.java @@ -63,6 +63,10 @@ public static List getAllProdOptionDefinitions() { List optionDefinitions = allClasses.stream() + // This package contains classes that reference other classes that aren't available + // without manual setup. + .filter( + c -> !c.getPackageName().equals("com.google.devtools.build.lib.profiler.memory")) .filter(c -> !isTestClass(c)) .flatMap(c -> Arrays.stream(c.getFields())) .filter(f -> f.isAnnotationPresent(Option.class)) diff --git a/src/test/shell/bazel/jdeps_test.sh b/src/test/shell/bazel/jdeps_test.sh index ec58c651d3a114..a7693c843f3ecb 100755 --- a/src/test/shell/bazel/jdeps_test.sh +++ b/src/test/shell/bazel/jdeps_test.sh @@ -78,6 +78,9 @@ function test_jdeps() { || fail "Failed to run jdeps on non denylisted class files." cd .. + # Keep java.instrument for allocation_instrumenter, which is supplied by the user. + echo "java.instrument" >> jdeps + # Make the list sorted and unique and compare it with expected results. cat jdeps | \ sed -e 's|[[:space:]]*||g' -e 's|/.*||' | \ diff --git a/third_party/allocation_instrumenter/BUILD b/third_party/allocation_instrumenter/BUILD index 96f3345ba0e9b8..7b3255cb47c252 100644 --- a/third_party/allocation_instrumenter/BUILD +++ b/third_party/allocation_instrumenter/BUILD @@ -1,3 +1,5 @@ +load("@rules_java//java:java_library.bzl", "java_library") + package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache 2.0 @@ -9,5 +11,12 @@ filegroup( java_library( name = "allocation_instrumenter", + neverlink = True, + exports = ["@maven//:com_google_code_java_allocation_instrumenter_java_allocation_instrumenter"], +) + +java_library( + name = "allocation_instrumenter_runtime", + testonly = True, exports = ["@maven//:com_google_code_java_allocation_instrumenter_java_allocation_instrumenter"], )