Skip to content

Commit fd9d68a

Browse files
kevin85421Drice1999
authored andcommitted
[core] Use psutil.process_iter to replace psutil.pids (ray-project#51123)
* Use `psutil.process_iter` to replace `psutil.pids`. * Use `proc.info["name"]` instead of `proc.name()`. * I'm not sure whether `proc.name()` uses the cache set by `process_iter`, but I'm certain that using info is correct since the official docs frequently use `proc.info[...]` with `process_iter`. * I asked a question on giampaolo/psutil#2518, but I'm not sure how long it will take to get an answer from the community. For now, I think we can merge it, and I'll update the use of psutil if the maintainers have any suggestions. --------- Signed-off-by: kaihsun <[email protected]>
1 parent 409e109 commit fd9d68a

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

python/ray/_private/memory_monitor.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ def get_top_n_memory_usage(n: int = 10):
3737
Returns:
3838
(str) The formatted string of top n process memory usage.
3939
"""
40-
pids = psutil.pids()
4140
proc_stats = []
42-
for pid in pids:
41+
for proc in psutil.process_iter(["memory_info", "cmdline"]):
4342
try:
44-
proc = psutil.Process(pid)
45-
proc_stats.append((get_rss(proc.memory_info()), pid, proc.cmdline()))
43+
proc_stats.append(
44+
(get_rss(proc.info["memory_info"]), proc.pid, proc.info["cmdline"])
45+
)
4646
except psutil.NoSuchProcess:
4747
# We should skip the process that has exited. Refer this
4848
# issue for more detail:

python/ray/_private/services.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,21 @@ def _find_address_from_flag(flag: str):
340340

341341
# The --redis-address here is what is now called the --address, but it
342342
# appears in the default_worker.py and agent.py calls as --redis-address.
343-
pids = psutil.pids()
344343
addresses = set()
345-
for pid in pids:
344+
for proc in psutil.process_iter(["cmdline"]):
346345
try:
347-
proc = psutil.Process(pid)
348346
# HACK: Workaround for UNIX idiosyncrasy
349347
# Normally, cmdline() is supposed to return the argument list.
350348
# But it in some cases (such as when setproctitle is called),
351349
# an arbitrary string resembling a command-line is stored in
352350
# the first argument.
353351
# Explanation: https://unix.stackexchange.com/a/432681
354352
# More info: https://github.com/giampaolo/psutil/issues/1179
355-
cmdline = proc.cmdline()
353+
cmdline = proc.info["cmdline"]
356354
# NOTE(kfstorm): To support Windows, we can't use
357355
# `os.path.basename(cmdline[0]) == "raylet"` here.
358356

359-
if len(cmdline) > 0 and "raylet" in os.path.basename(cmdline[0]):
357+
if _is_raylet_process(cmdline):
360358
for arglist in cmdline:
361359
# Given we're merely seeking --redis-address, we just split
362360
# every argument on spaces for now.
@@ -2289,3 +2287,19 @@ def start_ray_client_server(
22892287
fate_share=fate_share,
22902288
)
22912289
return process_info
2290+
2291+
2292+
def _is_raylet_process(cmdline: Optional[List[str]]) -> bool:
2293+
"""Check if the command line belongs to a raylet process.
2294+
2295+
Args:
2296+
cmdline: List of command line arguments or None
2297+
2298+
Returns:
2299+
bool: True if this is a raylet process, False otherwise
2300+
"""
2301+
if cmdline is None or len(cmdline) == 0:
2302+
return False
2303+
2304+
executable = os.path.basename(cmdline[0])
2305+
return "raylet" in executable

python/ray/_private/test_utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2130,11 +2130,11 @@ def get_gcs_memory_used():
21302130
import psutil
21312131

21322132
m = {
2133-
process.name(): process.memory_info().rss
2134-
for process in psutil.process_iter()
2133+
proc.info["name"]: proc.info["memory_info"].rss
2134+
for proc in psutil.process_iter(["status", "name", "memory_info"])
21352135
if (
2136-
process.status() not in (psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD)
2137-
and process.name() in ("gcs_server", "redis-server")
2136+
proc.info["status"] not in (psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD)
2137+
and proc.info["name"] in ("gcs_server", "redis-server")
21382138
)
21392139
}
21402140
assert "gcs_server" in m

0 commit comments

Comments
 (0)