Skip to content

Commit bfa5fb0

Browse files
gkdncopybara-github
authored andcommitted
Enable profiling for GwtIncompatibleStripper and improve profiling output.
This change extends the profiling support to the GwtIncompatibleStripper by using the shared `compiler_java_binary` macro. The profiling logic in `j2cl_common.bzl` is refactored to be reusable and now includes the action mnemonic in the profile output filename for better clarity. Additionally we now print the matched targets and corresponding pprof commands for convenience. PiperOrigin-RevId: 887081179
1 parent e6364f3 commit bfa5fb0

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ bool_flag(
9797
# Flag to enable profiling for particular targets.
9898
# Example usage:
9999
# blaze build <my_binary> --//:profiling_filter=<target_pattern>
100-
# pprof --flame blaze-bin/<target>.profile
100+
# pprof --flame blaze-bin/<target>_<mnemonic>.profile
101+
# You should also see the matched targets & corresponding pprof command in the blaze output.
101102
string_flag(
102103
name = "profiling_filter",
103104
build_setting_default = "<disabled>",

build_defs/internal_do_not_use/j2cl_common.bzl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,24 @@ def get_jdk_system(java_toolchain, javac_opts):
266266
# TODO(b/197211878): Switch to a public API when available.
267267
return java_toolchain._bootclasspath_info._system_inputs.to_list() if not jdk_system_already_set else []
268268

269+
def _add_profiling_support(ctx, mnemonic, outputs, args):
270+
if ctx.attr._profiling_filter[BuildSettingInfo].value in str(ctx.label):
271+
profile_output = ctx.actions.declare_file(ctx.label.name + "_" + mnemonic + ".profile")
272+
outputs.append(profile_output)
273+
args.add("-profileOutput", profile_output)
274+
print("Profiling %s %s" % (ctx.label, mnemonic)) # buildifier: disable=print
275+
print("pprof --flame %s" % profile_output.path) # buildifier: disable=print
276+
269277
def _strip_incompatible_annotation(ctx, name, java_srcs, mnemonic, strip_annotations):
270278
# Paths are matched by Kythe to identify generated J2CL sources.
271279
output_file = ctx.actions.declare_file(name + "_j2cl_stripped-src.jar")
272280

281+
mnemonic = mnemonic + "Strip"
282+
outputs = [output_file]
273283
args = ctx.actions.args()
274284
args.use_param_file("@%s", use_always = True)
275285
args.set_param_file_format("multiline")
286+
_add_profiling_support(ctx, mnemonic, outputs, args)
276287
args.add("-d", output_file)
277288
args.add_all(strip_annotations, format_each = "-annotation=%s")
278289
args.add_all(java_srcs)
@@ -281,15 +292,15 @@ def _strip_incompatible_annotation(ctx, name, java_srcs, mnemonic, strip_annotat
281292
ctx.actions.run(
282293
progress_message = "Stripping %s from %s" % (formatted_annotations, name),
283294
inputs = java_srcs,
284-
outputs = [output_file],
295+
outputs = outputs,
285296
executable = ctx.executable._j2cl_stripper,
286297
arguments = [args],
287298
env = dict(LANG = "en_US.UTF-8"),
288299
execution_requirements = {
289300
"supports-multiplex-workers": "1",
290301
"supports-multiplex-sandboxing": "1",
291302
},
292-
mnemonic = mnemonic + "Strip",
303+
mnemonic = mnemonic,
293304
)
294305

295306
return output_file
@@ -355,6 +366,7 @@ def _j2cl_transpile(
355366
klib_provider,
356367
klib_friends):
357368
""" Takes Java provider and translates it into Closure style JS in a zip bundle."""
369+
mnemonic = "J2cl" if backend == "CLOSURE" else "J2wasm"
358370
is_klibs_enabled = klib_common.is_klibs_experiment_enabled(ctx) and (kt_srcs or kt_common_srcs)
359371

360372
if "-Xstdlib-compilation" in kotlincopts:
@@ -387,6 +399,7 @@ def _j2cl_transpile(
387399
args = ctx.actions.args()
388400
args.use_param_file("@%s", use_always = True)
389401
args.set_param_file_format("multiline")
402+
_add_profiling_support(ctx, mnemonic, outputs, args)
390403
args.add_joined("-classpath", classpath, join_with = ctx.configuration.host_path_separator)
391404
args.add_all("-system", jdk_system, expand_directories = False)
392405

@@ -411,11 +424,6 @@ def _j2cl_transpile(
411424

412425
args.add("-experimentalBackend", backend)
413426

414-
if ctx.attr._profiling_filter[BuildSettingInfo].value in str(ctx.label):
415-
profile_output = ctx.actions.declare_file(ctx.label.name + ".profile")
416-
outputs.append(profile_output)
417-
args.add("-profileOutput", profile_output)
418-
419427
if backend == "WASM":
420428
# Add a prefix to where the Java source files will be located relative to the source map.
421429
args.add(output_dir.short_path, format = "-sourceMappingPathPrefix=%s/")
@@ -465,7 +473,7 @@ def _j2cl_transpile(
465473
"supports-multiplex-sandboxing": "1",
466474
"supports-worker-cancellation": "1",
467475
},
468-
mnemonic = "J2cl" if backend == "CLOSURE" else "J2wasm",
476+
mnemonic = mnemonic,
469477
)
470478

471479
def _create_js_lib_struct(j2cl_info, extra_providers = []):
@@ -511,6 +519,9 @@ J2CL_JAVA_TOOLCHAIN_ATTRS = {
511519
cfg = "exec",
512520
executable = True,
513521
),
522+
"_profiling_filter": attr.label(
523+
default = Label("//:profiling_filter"),
524+
),
514525
}
515526

516527
J2CL_TOOLCHAIN_ATTRS = {
@@ -522,9 +533,6 @@ J2CL_TOOLCHAIN_ATTRS = {
522533
"_java_frontend": attr.label(
523534
default = Label("//:experimental_java_frontend"),
524535
),
525-
"_profiling_filter": attr.label(
526-
default = Label("//:profiling_filter"),
527-
),
528536
"_zip": attr.label(
529537
executable = True,
530538
cfg = "exec",

tools/java/com/google/j2cl/tools/gwtincompatible/BUILD

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# J2CL.
55

66
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
7-
load("//transpiler/java/com/google/j2cl/common/bazel:jvm_flags.bzl", "JVM_FLAGS")
7+
load("//transpiler/java/com/google/j2cl/common/bazel:compiler_java_binary.bzl", "compiler_java_binary")
88

99
package(
1010
default_applicable_licenses = ["//:j2cl_license"],
@@ -45,11 +45,8 @@ java_library(
4545
],
4646
)
4747

48-
java_binary(
48+
compiler_java_binary(
4949
name = "GwtIncompatibleStripper_worker",
50-
jvm_flags = JVM_FLAGS,
5150
main_class = "com.google.j2cl.tools.gwtincompatible.BazelGwtIncompatibleStripper",
52-
use_launcher = False,
53-
visibility = ["//build_defs:toolchain_users"],
5451
runtime_deps = [":gwtincompatible_worker_lib"],
5552
)

transpiler/java/com/google/j2cl/common/bazel/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ java_library(
2121
"//transpiler/java/com/google/j2cl/common/bazel/profiler",
2222
],
2323
)
24+
25+
config_setting(
26+
name = "profiling_disabled",
27+
flag_values = {"//:profiling_filter": "<disabled>"},
28+
)

transpiler/java/com/google/j2cl/transpiler/compiler_java_binary.bzl renamed to transpiler/java/com/google/j2cl/common/bazel/compiler_java_binary.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
load("@rules_java//java:java_binary.bzl", "java_binary")
2-
load("//transpiler/java/com/google/j2cl/common/bazel:jvm_flags.bzl", "JVM_FLAGS")
2+
load(":jvm_flags.bzl", "JVM_FLAGS")
33

4-
visibility("private")
4+
visibility(["//..."])
55

66
def compiler_java_binary(name, runtime_deps = [], extra_jvm_flags = [], **kwargs):
77
java_binary(
88
name = name,
99
jvm_flags = JVM_FLAGS + extra_jvm_flags + select({
10-
":profiling_disabled": [],
10+
"//transpiler/java/com/google/j2cl/common/bazel:profiling_disabled": [],
1111
"//conditions:default": [
1212
"-XX:GoogleAgentFlags=-contentionz,-codez,-histogram,-heapz,-native_heapz," +
1313
"cpu_profile_stack_limit:256,cpu_samples:200",
1414
],
1515
}),
1616
runtime_deps = runtime_deps + select({
17-
":profiling_disabled": [],
17+
"//transpiler/java/com/google/j2cl/common/bazel:profiling_disabled": [],
1818
"//conditions:default": [
1919
"//transpiler/java/com/google/j2cl/common/bazel/profiler:profiler_impl",
2020
],
2121
}),
2222
launcher = select({
23-
":profiling_disabled": None,
23+
"//transpiler/java/com/google/j2cl/common/bazel:profiling_disabled": None,
2424
"//conditions:default": "//devtools/java/launcher:run_java",
2525
}),
2626
visibility = ["//build_defs:toolchain_users"],

transpiler/java/com/google/j2cl/transpiler/BUILD

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
2-
load(":compiler_java_binary.bzl", "compiler_java_binary")
2+
load("//transpiler/java/com/google/j2cl/common/bazel:compiler_java_binary.bzl", "compiler_java_binary")
33

44
package(
55
default_applicable_licenses = ["//:j2cl_license"],
@@ -125,8 +125,3 @@ compiler_java_binary(
125125
main_class = "com.google.j2cl.transpiler.BazelJ2wasmBundler",
126126
runtime_deps = [":bazelj2wasmbundler_lib"],
127127
)
128-
129-
config_setting(
130-
name = "profiling_disabled",
131-
flag_values = {"//:profiling_filter": "<disabled>"},
132-
)

0 commit comments

Comments
 (0)