Skip to content

Commit

Permalink
Remove allocation instrumenter jar and update version in docs
Browse files Browse the repository at this point in the history
The jar can be supplied separately as it needs to be passed to the `-javaagent` JVM arg anyway.

Also update the recommended version for compatibility with Java 21.

Fixes bazelbuild#24782

Closes bazelbuild#24783.

PiperOrigin-RevId: 714057306
Change-Id: Ic8d47562f15344ed16d0af5d3242937d2ee53b22
  • Loading branch information
fmeum authored and copybara-github committed Jan 10, 2025
1 parent edb2b06 commit b59fbee
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 53 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,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.10.1",
"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.36.0",
"com.google.errorprone:error_prone_annotations:2.36.0",
"com.google.errorprone:error_prone_check_api:2.36.0",
Expand Down
51 changes: 14 additions & 37 deletions maven_install.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": -44489190,
"__RESOLVED_ARTIFACTS_HASH": 1526283480,
"__INPUT_ARTIFACTS_HASH": -1897763682,
"__RESOLVED_ARTIFACTS_HASH": -165154966,
"conflict_resolution": {
"com.google.api.grpc:proto-google-common-protos:2.29.0": "com.google.api.grpc:proto-google-common-protos:2.41.0",
"com.google.auth:google-auth-library-credentials:1.6.0": "com.google.auth:google-auth-library-credentials:1.23.0",
Expand Down Expand Up @@ -189,9 +189,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.dagger:dagger": {
"shasums": {
Expand Down Expand Up @@ -1128,15 +1128,6 @@
"com.google.code.gson:gson": [
"com.google.errorprone:error_prone_annotations"
],
"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.dagger:dagger": [
"javax.inject:javax.inject"
],
Expand Down Expand Up @@ -1981,30 +1972,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.dagger:dagger": [
"dagger",
Expand Down
12 changes: 6 additions & 6 deletions site/en/docs/user-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -2178,30 +2178,30 @@ 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.

Example:

<pre>
% 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 &lt;targets&gt;

# 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
Expand Down
4 changes: 2 additions & 2 deletions site/en/rules/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ You must pass these two startup flags to *every* Bazel invocation:

```
STARTUP_FLAGS=\
--host_jvm_args=-javaagent:<path to java-allocation-instrumenter-3.3.0.jar> \
--host_jvm_args=-javaagent:<path to java-allocation-instrumenter-3.3.4.jar> \
--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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +38,7 @@
* <li>--host_jvm_args=-javaagent:(path to Google's java agent jar)
* <ul>
* <li>For Bazel use <a
* href="https://github.com/bazelbuild/bazel/tree/master/third_party/allocation_instrumenter">java-allocation-instrumenter-3.3.0.jar</a>
* href="https://github.com/bazelbuild/bazel/tree/master/third_party/allocation_instrumenter">java-allocation-instrumenter-3.3.4.jar</a>
* </ul>
* <li>--host_jvm_args=-DRULE_MEMORY_TRACKER=1
* </ol>
Expand All @@ -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(
Expand All @@ -65,21 +69,43 @@ 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);
}
}

@Override
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:<path to"
+ " java-allocation-instrumenter-3.3.4.jar>"));
}
}

private static boolean isRequested() {
String memoryTrackerProperty = System.getProperty("RULE_MEMORY_TRACKER");
return memoryTrackerProperty != null && memoryTrackerProperty.equals("1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public static List<OptionDefinition> getAllProdOptionDefinitions() {

List<OptionDefinition> 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))
Expand Down
3 changes: 3 additions & 0 deletions src/test/shell/bazel/jdeps_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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|/.*||' | \
Expand Down
9 changes: 9 additions & 0 deletions third_party/allocation_instrumenter/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_java//java:java_library.bzl", "java_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0
Expand All @@ -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"],
)

0 comments on commit b59fbee

Please sign in to comment.