Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Use psutil.process_iter to replace psutil.pids #51123

Merged
merged 3 commits into from
Mar 7, 2025

Conversation

kevin85421
Copy link
Member

@kevin85421 kevin85421 commented Mar 6, 2025

Why are these changes needed?

image

  • 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 [Linux] proc.info is slower than proc.name() 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.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: kaihsun <kaihsun@anyscale.com>
@kevin85421 kevin85421 added the go add ONLY when ready to merge, run all tests label Mar 6, 2025
Signed-off-by: kaihsun <kaihsun@anyscale.com>
@@ -2289,3 +2287,19 @@ def start_ray_client_server(
fate_share=fate_share,
)
return process_info


def _is_raylet_process(cmdline: Optional[List[str]]) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about taking a proc object directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may prefer to use cmdline to avoid passing the whole Process object into this function. This makes it easier for us to write mock data for unit tests if needed. Another reason is to avoid using proc.info["cmdline"] in this function so that we can clearly determine that cmdline is used in the loop above; and we can easily know why do we use psutil.process_iter(["cmdline"]).

Signed-off-by: kaihsun <kaihsun@anyscale.com>
proc = psutil.Process(pid)
proc_stats.append((get_rss(proc.memory_info()), pid, proc.cmdline()))
proc_stats.append(
(get_rss(proc.info["memory_info"]), proc.pid, proc.info["cmdline"])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proc.pid is set when Process is constructed. No need to specify it in process_iter.

@kevin85421 kevin85421 marked this pull request as ready for review March 7, 2025 01:12
Copy link
Collaborator

@edoakes edoakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. Is this covered by existing tests?

@kevin85421
Copy link
Member Author

Looks fine. Is this covered by existing tests?

Yes

  1. get_top_n_memory_usage is used in monitor_memory_usage, and monitor_memory_usage is used by many tests.
  2. _find_address_from_flag is used by tests liketest_actor_advanced.
  3. get_gcs_memory_used is used by tests like test_advanced_4 and test_advanced_9.

@edoakes edoakes merged commit 4c28d71 into ray-project:master Mar 7, 2025
5 checks passed
elimelt pushed a commit to elimelt/ray that referenced this pull request Mar 9, 2025
…t#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 <kaihsun@anyscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants