Skip to content

Commit efe0747

Browse files
authored
Use read_proc_file helper from granulate-utils (#473)
1 parent 949319d commit efe0747

File tree

5 files changed

+15
-27
lines changed

5 files changed

+15
-27
lines changed

gprofiler/profilers/java.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
)
6666
from gprofiler.utils.fs import is_rw_exec_dir, safe_copy
6767
from gprofiler.utils.perf import can_i_use_perf_events
68-
from gprofiler.utils.process import is_process_basename_matching, process_comm, read_proc_file
68+
from gprofiler.utils.process import is_process_basename_matching, process_comm, search_proc_maps
6969

7070
logger = get_logger_adapter(__name__)
7171

@@ -980,7 +980,7 @@ def _select_processes_to_profile(self) -> List[Process]:
980980
return pgrep_maps(DETECTED_JAVA_PROCESSES_REGEX)
981981

982982
def _should_profile_process(self, process: Process) -> bool:
983-
return re.search(DETECTED_JAVA_PROCESSES_REGEX, read_proc_file(process, "maps"), re.MULTILINE) is not None
983+
return search_proc_maps(process, DETECTED_JAVA_PROCESSES_REGEX) is not None
984984

985985
def start(self) -> None:
986986
super().start()

gprofiler/profilers/python.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
wait_event,
5252
wait_for_file_by_prefix,
5353
)
54-
from gprofiler.utils.process import is_process_basename_matching, process_comm, read_proc_file
54+
from gprofiler.utils.process import is_process_basename_matching, process_comm, search_proc_maps
5555

5656
logger = get_logger_adapter(__name__)
5757

@@ -258,8 +258,9 @@ def _select_processes_to_profile(self) -> List[Process]:
258258
return filtered_procs
259259

260260
def _should_profile_process(self, process: Process) -> bool:
261-
match = re.search(DETECTED_PYTHON_PROCESSES_REGEX, read_proc_file(process, "maps"), re.MULTILINE) is not None
262-
return match and not self._should_skip_process(process)
261+
return search_proc_maps(process, DETECTED_PYTHON_PROCESSES_REGEX) is not None and not self._should_skip_process(
262+
process
263+
)
263264

264265
def _should_skip_process(self, process: Process) -> bool:
265266
if process.pid == os.getpid():

gprofiler/profilers/ruby.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import functools
77
import os
8-
import re
98
import signal
109
from pathlib import Path
1110
from threading import Event
@@ -23,7 +22,7 @@
2322
from gprofiler.profilers.profiler_base import SpawningProcessProfilerBase
2423
from gprofiler.profilers.registry import register_profiler
2524
from gprofiler.utils import pgrep_maps, random_prefix, removed_path, resource_path, run_process
26-
from gprofiler.utils.process import is_process_basename_matching, process_comm, read_proc_file
25+
from gprofiler.utils.process import is_process_basename_matching, process_comm, search_proc_maps
2726

2827
logger = get_logger_adapter(__name__)
2928

@@ -128,4 +127,4 @@ def _select_processes_to_profile(self) -> List[Process]:
128127
return pgrep_maps(self.DETECTED_RUBY_PROCESSES_REGEX)
129128

130129
def _should_profile_process(self, process: Process) -> bool:
131-
return re.search(self.DETECTED_RUBY_PROCESSES_REGEX, read_proc_file(process, "maps"), re.MULTILINE) is not None
130+
return search_proc_maps(process, self.DETECTED_RUBY_PROCESSES_REGEX) is not None

gprofiler/utils/process.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,18 @@
66
import os
77
import re
88
from functools import lru_cache
9-
from pathlib import Path
9+
from typing import Match, Optional
1010

11-
from granulate_utils.linux.process import is_process_running, process_exe
12-
from psutil import NoSuchProcess, Process
11+
from granulate_utils.linux.process import process_exe, read_proc_file
12+
from psutil import Process
1313

1414

15-
def ensure_running(process: Process) -> None:
16-
if not is_process_running(process, allow_zombie=True):
17-
raise NoSuchProcess(process.pid)
18-
19-
20-
def read_proc_file(process: Process, file: str) -> str:
21-
try:
22-
data = Path(f"/proc/{process.pid}/{file}").read_text()
23-
except FileNotFoundError as e:
24-
raise NoSuchProcess(process.pid) from e
25-
else:
26-
# ensures we read the right file (i.e PID was not reused)
27-
ensure_running(process)
28-
return data
15+
def search_proc_maps(process: Process, pattern: str) -> Optional[Match[str]]:
16+
return re.search(pattern, read_proc_file(process, "maps").decode(), re.MULTILINE)
2917

3018

3119
def process_comm(process: Process) -> str:
32-
status = read_proc_file(process, "status")
20+
status = read_proc_file(process, "status").decode()
3321
name_line = status.splitlines()[0]
3422
assert name_line.startswith("Name:\t")
3523
return name_line.split("\t", 1)[1]

granulate-utils

0 commit comments

Comments
 (0)