Skip to content

Commit 166c291

Browse files
authored
Better java app id resolving (#311)
* Better java app id resolving * CR fix
1 parent 0e8c495 commit 166c291

File tree

7 files changed

+24
-16
lines changed

7 files changed

+24
-16
lines changed

gprofiler/merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from gprofiler.containers_client import ContainerNamesClient
1414
from gprofiler.gprofiler_types import ProcessToStackSampleCounters, StackToSampleCount
1515
from gprofiler.log import get_logger_adapter
16-
from gprofiler.metadata.application_identifiers import get_application_name
16+
from gprofiler.metadata import application_identifiers
1717
from gprofiler.metadata.metadata_type import Metadata
1818
from gprofiler.system_metrics import Metrics
1919

@@ -256,7 +256,7 @@ def concatenate_profiles(
256256

257257
for pid, stacks in process_profiles.items():
258258
container_name = _get_container_name(pid, container_names_client, add_container_names)
259-
application_name = get_application_name(pid) if identify_applications else None
259+
application_name = application_identifiers.get_application_name(pid) if identify_applications else None
260260
if application_name is not None:
261261
application_name = f"appid: {application_name}"
262262
prefix = (container_name + ";") if add_container_names else ""

gprofiler/metadata/application_identifiers.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
from granulate_utils.linux.ns import resolve_host_path, resolve_proc_root_links
1212
from psutil import NoSuchProcess, Process
1313

14+
from gprofiler.exceptions import CalledProcessError
1415
from gprofiler.log import get_logger_adapter
16+
from gprofiler.profilers.java import jattach_path
17+
from gprofiler.utils import run_process
1518

1619
_logger = get_logger_adapter(__name__)
1720

@@ -244,11 +247,16 @@ def get_application_name(self, process: Process) -> Optional[str]:
244247
if not any("libjvm.so" in m.path for m in process.memory_maps()):
245248
return None
246249

247-
jar_arg = _get_cli_arg_by_name(process.cmdline(), "-jar")
248-
if jar_arg is _NON_AVAILABLE_ARG:
249-
return None
250+
try:
251+
java_properties = run_process([jattach_path(), str(process.pid), "properties"]).stdout.decode()
252+
for line in java_properties.splitlines():
253+
if line.startswith("sun.java.command"):
254+
app_id = line[line.find("=") + 1 :]
255+
return f"java: {app_id} ({_append_file_to_proc_wd(process, app_id)})"
256+
except CalledProcessError as e:
257+
_logger.warning(f"Couldn't get Java properties for process {process.pid}: {e.stderr}")
250258

251-
return f"java: {jar_arg} ({_append_file_to_proc_wd(process, jar_arg)})"
259+
return None
252260

253261

254262
# Please note that the order matter, because the FIRST matching identifier will be used.

gprofiler/profilers/java.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
from packaging.version import Version
3636
from psutil import Process
3737

38+
from gprofiler import merge
3839
from gprofiler.exceptions import CalledProcessError
3940
from gprofiler.gprofiler_types import ProcessToStackSampleCounters, StackToSampleCount
4041
from gprofiler.kernel_messages import get_kernel_messages_provider
4142
from gprofiler.log import get_logger_adapter
42-
from gprofiler.merge import parse_one_collapsed
4343
from gprofiler.profilers.profiler_base import ProcessProfilerBase
4444
from gprofiler.profilers.registry import ProfilerArgument, register_profiler
4545
from gprofiler.utils import (
@@ -795,7 +795,7 @@ def _profile_ap_process(self, ap_proc: AsyncProfiledProcess, comm: str) -> Stack
795795
return self._profiling_error_stack("error", "process exited before reading the output", comm)
796796
else:
797797
logger.info(f"Finished profiling process {ap_proc.process.pid}")
798-
return parse_one_collapsed(output, comm)
798+
return merge.parse_one_collapsed(output, comm)
799799

800800
def _check_hotspot_error(self, ap_proc: AsyncProfiledProcess) -> None:
801801
pid = ap_proc.process.pid

gprofiler/profilers/perf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from threading import Event
1010
from typing import List, Optional
1111

12+
from gprofiler import merge
1213
from gprofiler.exceptions import StopEventSetException
1314
from gprofiler.gprofiler_types import ProcessToStackSampleCounters
1415
from gprofiler.log import get_logger_adapter
15-
from gprofiler.merge import merge_global_perfs
1616
from gprofiler.profilers.profiler_base import ProfilerBase
1717
from gprofiler.profilers.registry import ProfilerArgument, register_profiler
1818
from gprofiler.utils import run_process, start_process, wait_event, wait_for_file_by_prefix
@@ -202,7 +202,7 @@ def snapshot(self) -> ProcessToStackSampleCounters:
202202
for perf in self._perfs:
203203
perf.switch_output()
204204

205-
return merge_global_perfs(
205+
return merge.merge_global_perfs(
206206
self._perf_fp.wait_and_script() if self._perf_fp is not None else None,
207207
self._perf_dwarf.wait_and_script() if self._perf_dwarf is not None else None,
208208
)

gprofiler/profilers/python.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from granulate_utils.python import _BLACKLISTED_PYTHON_PROCS, DETECTED_PYTHON_PROCESSES_REGEX
1818
from psutil import NoSuchProcess, Process
1919

20+
from gprofiler import merge
2021
from gprofiler.exceptions import (
2122
CalledProcessError,
2223
CalledProcessTimeoutError,
@@ -25,7 +26,6 @@
2526
)
2627
from gprofiler.gprofiler_types import ProcessToStackSampleCounters, StackToSampleCount, nonnegative_integer
2728
from gprofiler.log import get_logger_adapter
28-
from gprofiler.merge import parse_many_collapsed, parse_one_collapsed_file
2929
from gprofiler.metadata.py_module_version import get_modules_versions
3030
from gprofiler.metadata.system_metadata import get_arch
3131
from gprofiler.profilers.profiler_base import ProcessProfilerBase, ProfilerBase, ProfilerInterface
@@ -147,7 +147,7 @@ def _profile_process(self, process: Process) -> StackToSampleCount:
147147
raise
148148

149149
logger.info(f"Finished profiling process {process.pid} with py-spy")
150-
parsed = parse_one_collapsed_file(Path(local_output_path), comm)
150+
parsed = merge.parse_one_collapsed_file(Path(local_output_path), comm)
151151
if self.add_versions:
152152
parsed = _add_versions_to_process_stacks(process, parsed)
153153
return parsed
@@ -368,7 +368,7 @@ def snapshot(self) -> ProcessToStackSampleCounters:
368368
finally:
369369
# always remove, even if we get read/decode errors
370370
collapsed_path.unlink()
371-
parsed = parse_many_collapsed(collapsed_text)
371+
parsed = merge.parse_many_collapsed(collapsed_text)
372372
if self.add_versions:
373373
parsed = _add_versions_to_stacks(parsed)
374374
return parsed

gprofiler/profilers/ruby.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
from psutil import Process
1212

13+
from gprofiler import merge
1314
from gprofiler.exceptions import ProcessStoppedException, StopEventSetException
1415
from gprofiler.gprofiler_types import StackToSampleCount
1516
from gprofiler.log import get_logger_adapter
16-
from gprofiler.merge import parse_one_collapsed_file
1717
from gprofiler.profilers.profiler_base import ProcessProfilerBase
1818
from gprofiler.profilers.registry import register_profiler
1919
from gprofiler.utils import pgrep_maps, random_prefix, removed_path, resource_path, run_process
@@ -75,7 +75,7 @@ def _profile_process(self, process: Process) -> StackToSampleCount:
7575
raise StopEventSetException
7676

7777
logger.info(f"Finished profiling process {process.pid} with rbspy")
78-
return parse_one_collapsed_file(Path(local_output_path), process_comm(process))
78+
return merge.parse_one_collapsed_file(Path(local_output_path), process_comm(process))
7979

8080
def _select_processes_to_profile(self) -> List[Process]:
8181
return pgrep_maps(r"(?:^.+/ruby[^/]*$)")

lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ if [[ "$1" = "--ci" ]]; then
1111
fi
1212

1313
isort --settings-path .isort.cfg --skip granulate-utils .
14-
black --line-length 120 $check_arg --exclude granulate-utils .
14+
black --line-length 120 $check_arg --exclude "granulate-utils|\.venv" .
1515
flake8 --config .flake8 .
1616
mypy .

0 commit comments

Comments
 (0)