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

erts: Add BIFs processes_iterator/0 and processes_next/1 #9129

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

lucioleKi
Copy link
Contributor

@lucioleKi lucioleKi commented Nov 29, 2024

This PR adds 2 BIFs to the erlang module.

processes_iterator/0 returns a process iterator that can be used to iterate through the process table.

process_next/1 takes in a process iterator and returns a 2-tuple, consisting of one process identifier and a new process iterator. If the process iterator runs out of processes in the process table, none will be returned.

By using these 2 BIFs instead of processes/0, one can avoid creating a potentially huge list of the pids for all existing processes, at the cost of less consistency guarantees. Process identifiers returned from consecutive calls of process_next/1 may not be a consistent snapshot of all elements existing in the table during any of the calls. The process identifier of a process that is alive before processes_iterator/0 is called and continues to be alive until processes_next/1 returns none is guaranteed to be part of the result returned from one of the calls to processes_next/1.

@lucioleKi lucioleKi added the team:VM Assigned to OTP team VM label Nov 29, 2024
@lucioleKi lucioleKi self-assigned this Nov 29, 2024
Copy link
Contributor

github-actions bot commented Nov 29, 2024

CT Test Results

    5 files    211 suites   1h 54m 20s ⏱️
3 161 tests 2 872 ✅ 289 💤 0 ❌
4 151 runs  3 790 ✅ 361 💤 0 ❌

Results for commit 69f7884.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@lucioleKi lucioleKi force-pushed the isabell/erts/processes_iterator branch from 9b38bf1 to 72b2c22 Compare November 29, 2024 12:48
@lucioleKi lucioleKi added the testing currently being tested, tag is used by OTP internal CI label Nov 29, 2024
@lucioleKi lucioleKi force-pushed the isabell/erts/processes_iterator branch 2 times, most recently from 56cdff4 to 0da4f15 Compare December 2, 2024 11:26
Copy link
Contributor

@rickard-green rickard-green left a comment

Choose a reason for hiding this comment

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

Overall it looks good, but I have a few comments.

erts/preloaded/src/erlang.erl Outdated Show resolved Hide resolved
erts/emulator/beam/erl_ptab.c Outdated Show resolved Hide resolved
erts/preloaded/src/erlang.erl Outdated Show resolved Hide resolved
@lucioleKi lucioleKi force-pushed the isabell/erts/processes_iterator branch from 0da4f15 to ee5776b Compare December 6, 2024 10:36
@lucioleKi lucioleKi changed the title erts: Add BIFs processes_first/0 and processes_next/1 erts: Add BIFs processes_iterator/0 and processes_next/1 Dec 6, 2024
rickard-green
rickard-green previously approved these changes Dec 6, 2024
rickard-green
rickard-green previously approved these changes Dec 6, 2024
This PR adds 2 BIFs to the `erlang` module.

`processes_iterator/0` returns a process iterator that can be used to
iterate through the process table.

`process_next/1` takes in a process iterator and returns a 2-tuple,
consisting of one process identifier and a new process iterator. If the
process iterator runs out of processes in the process table, `none`
will be returned.

By using these 2 BIFs instead of `processes/0`, one can avoid creating
a potentially huge list of the pids for all existing processes, at the
cost of less consistency guarantees. Process identifiers returned from
consecutive calls of `process_next/1` may not be a consistent snapshot
of all elements existing in the table during any of the calls. The
process identifier of a process that is alive before
`processes_iterator/0` is called and continues to be alive until
`processes_next/1` returns `none` is guaranteed to be part of the
result returned from one of the calls to `processes_next/1`.
@josevalim
Copy link
Contributor

This is amazing! We have spotted the process listing in erts_code_purger to be a huge cost when erasing modules, which became much more apparent once we increased the process table. Ideally we would be able to purge modules in batches to alleviate the cost, but reducing the cost of erlang:processes() will definitely be welcome. Do you think this could be used there?

@rickard-green
Copy link
Contributor

Yes, the processes iterator will be used by the code purger process as well as the literal area collector process in OTP 28.

@josevalim
Copy link
Contributor

@rickard-green fantastic! Do you think it would be also possible to purge batches of modules in the future too? Or is it a much more complex change? Or perhaps one that would not yield benefits?

@garazdawi
Copy link
Contributor

Or is it a much more complex change?

It is a much more complex change as all parts of the VM that deals with code purging needs to be tought to handle multiple address ranges instead of just one. I'm uncertain how much work it would be, but definitely more than what is in this PR.

@rickard-green
Copy link
Contributor

The most problematic part as I see it is the literal area collection (which will be done for all modules with a literal area which I suspect is most of the modules). During the collection all copying of all terms in message sends needs to be aware of the literal area being collected and make copies of literals in that area instead of passing references to the literals. As it is now with only one area collected at a time, this is relatively cheap, but having a number of areas to check against might get costly.

@lucioleKi lucioleKi merged commit 0ac5855 into erlang:master Dec 12, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants