Skip to content

Commit 5123ab4

Browse files
authored
java: Upgrade to async-profiler v2.10 and use asprof-exported jattach (#776)
See Granulate/async-profiler#7 Closes: #745
1 parent 02c6d58 commit 5123ab4

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ COPY --from=phpspy-builder /tmp/phpspy/phpspy gprofiler/resources/php/phpspy
184184
COPY --from=phpspy-builder /tmp/binutils/binutils-2.25/bin/bin/objdump gprofiler/resources/php/objdump
185185
COPY --from=phpspy-builder /tmp/binutils/binutils-2.25/bin/bin/strings gprofiler/resources/php/strings
186186

187-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/jattach gprofiler/resources/java/jattach
187+
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/bin/asprof gprofiler/resources/java/asprof
188188
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/async-profiler-version gprofiler/resources/java/async-profiler-version
189-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/libasyncProfiler.so gprofiler/resources/java/glibc/libasyncProfiler.so
190-
COPY --from=async-profiler-builder-musl /tmp/async-profiler/build/libasyncProfiler.so gprofiler/resources/java/musl/libasyncProfiler.so
191-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/fdtransfer gprofiler/resources/java/fdtransfer
189+
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/lib/libasyncProfiler.so gprofiler/resources/java/glibc/libasyncProfiler.so
190+
COPY --from=async-profiler-builder-musl /tmp/async-profiler/build/lib/libasyncProfiler.so gprofiler/resources/java/musl/libasyncProfiler.so
192191
COPY --from=node-package-builder-musl /tmp/module_build gprofiler/resources/node/module/musl
193192
COPY --from=node-package-builder-glibc /tmp/module_build gprofiler/resources/node/module/glibc
194193

gprofiler/profilers/java.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ def __init__(self, stop_event: Event, jattach_timeout: int):
237237

238238
def run(self, process: Process, cmd: str) -> str:
239239
return run_process(
240-
[jattach_path(), str(process.pid), "jcmd", cmd], stop_event=self.stop_event, timeout=self.jattach_timeout
240+
[asprof_path(), "jcmd", "--jattach-cmd", cmd, str(process.pid)],
241+
stop_event=self.stop_event,
242+
timeout=self.jattach_timeout,
241243
).stdout.decode()
242244

243245

@@ -390,8 +392,8 @@ def get_supported_jvm_flags(self, process: Process) -> Iterable[JvmFlag]:
390392

391393

392394
@functools.lru_cache(maxsize=1)
393-
def jattach_path() -> str:
394-
return resource_path("java/jattach")
395+
def asprof_path() -> str:
396+
return resource_path("java/asprof")
395397

396398

397399
@functools.lru_cache(maxsize=1)
@@ -589,11 +591,11 @@ def _check_disk_requirements(self) -> None:
589591

590592
def _get_base_cmd(self) -> List[str]:
591593
return [
592-
jattach_path(),
593-
str(self.process.pid),
594-
"load",
594+
asprof_path(),
595+
"jattach",
596+
"-L",
595597
self._libap_path_process,
596-
"true",
598+
"--jattach-cmd",
597599
]
598600

599601
def _get_extra_ap_args(self) -> str:
@@ -644,7 +646,7 @@ def _run_async_profiler(self, cmd: List[str]) -> str:
644646
try:
645647
# kill jattach with SIGTERM if it hangs. it will go down
646648
run_process(
647-
cmd,
649+
cmd + [str(self.process.pid)],
648650
stop_event=self._profiler_state.stop_event,
649651
timeout=self._jattach_timeout,
650652
kill_signal=signal.SIGTERM,
@@ -683,7 +685,15 @@ def _run_fdtransfer(self) -> None:
683685
# run fdtransfer with accept timeout that's slightly greater than the jattach timeout - to make
684686
# sure that fdtransfer is still around for the full duration of jattach, in case the application
685687
# takes a while to accept & handle the connection.
686-
[fdtransfer_path(), self._fdtransfer_path, str(self.process.pid), str(self._jattach_timeout + 5)],
688+
[
689+
asprof_path(),
690+
"fdtransfer",
691+
"--fd-path",
692+
self._fdtransfer_path,
693+
"--fdtransfer-timeout",
694+
str(self._jattach_timeout + 5),
695+
str(self.process.pid),
696+
],
687697
stop_event=self._profiler_state.stop_event,
688698
timeout=self._FDTRANSFER_TIMEOUT,
689699
)

pyi.Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ RUN ./async_profiler_build_shared.sh
8484
# a build step to ensure the minimum CentOS version that we require can "ldd" our libasyncProfiler.so file.
8585
FROM centos${AP_CENTOS_MIN} AS async-profiler-centos-min-test-glibc
8686
SHELL ["/bin/bash", "-c", "-euo", "pipefail"]
87-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/libasyncProfiler.so /libasyncProfiler.so
87+
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/lib/libasyncProfiler.so /libasyncProfiler.so
8888
RUN if ldd /libasyncProfiler.so 2>&1 | grep -q "not found" ; then echo "libasyncProfiler.so is not compatible with minimum CentOS!"; readelf -Ws /libasyncProfiler.so; ldd /libasyncProfiler.so; exit 1; fi
8989

9090
# async-profiler musl
@@ -246,11 +246,10 @@ COPY --from=phpspy-builder /tmp/binutils/binutils-2.25/bin/bin/strings gprofiler
246246
COPY --from=async-profiler-builder-glibc /usr/bin/awk gprofiler/resources/php/awk
247247
COPY --from=async-profiler-builder-glibc /usr/bin/xargs gprofiler/resources/php/xargs
248248

249-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/jattach gprofiler/resources/java/jattach
249+
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/bin/asprof gprofiler/resources/java/asprof
250250
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/async-profiler-version gprofiler/resources/java/async-profiler-version
251251
COPY --from=async-profiler-centos-min-test-glibc /libasyncProfiler.so gprofiler/resources/java/glibc/libasyncProfiler.so
252-
COPY --from=async-profiler-builder-musl /tmp/async-profiler/build/libasyncProfiler.so gprofiler/resources/java/musl/libasyncProfiler.so
253-
COPY --from=async-profiler-builder-glibc /tmp/async-profiler/build/fdtransfer gprofiler/resources/java/fdtransfer
252+
COPY --from=async-profiler-builder-musl /tmp/async-profiler/build/lib/libasyncProfiler.so gprofiler/resources/java/musl/libasyncProfiler.so
254253
COPY --from=node-package-builder-musl /tmp/module_build gprofiler/resources/node/module/musl
255254
COPY --from=node-package-builder-glibc /tmp/module_build gprofiler/resources/node/module/glibc
256255

scripts/async_profiler_build_shared.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#
66
set -euo pipefail
77

8-
VERSION=v2.9g6
9-
GIT_REV="4938ce815be597fafc6a7a185a16ff394b9d7f41"
8+
VERSION=v2.10g1
9+
GIT_REV="b3baf6010e31378679a28fe1cd30168f51cc1ade"
1010

1111
git clone --depth 1 -b "$VERSION" https://github.com/Granulate/async-profiler.git && cd async-profiler && git reset --hard "$GIT_REV"
1212
make all

tests/test_java.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,13 @@ def test_java_deleted_libjvm(
429429
def filter_jattach_load_records(records: List[LogRecord]) -> List[LogRecord]:
430430
def _filter_record(r: LogRecord) -> bool:
431431
# find the log record of
432-
# Running command (command=['/app/gprofiler/resources/java/jattach', 'xxx', 'load', ....])
432+
# Running command (command=['/app/gprofiler/resources/java/apsprof', 'jattach',
433+
# '-L', '/path/to/libasyncProfiler.so', "--jattach-cmd", "start,..."])
433434
return (
434435
r.message == "Running command"
435-
and "/jattach" in log_record_extra(r)["command"][0]
436-
and "load" in log_record_extra(r)["command"][2]
436+
and len(log_record_extra(r)["command"]) >= 6
437+
and log_record_extra(r)["command"][1] == "jattach"
438+
and any(map(lambda k: k in log_record_extra(r)["command"][5], ["start,", "stop,"]))
437439
)
438440

439441
return list(filter(_filter_record, records))

0 commit comments

Comments
 (0)