Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@max-au
Copy link
Contributor

@max-au max-au commented Dec 18, 2022

When writable binaries were moved to a separate list proc->wrt_bins, they weren't added to the output of process_info(Pid, binary). It led to interesting effects of binaries suddenly disappearing after running implicit GC caused by BIF calls, including process_info itself. Effectively, running process_info(self(), binary) twice in a row resulted in different lists, because first call triggers GC clearing out original binary from the off-heap list:

start() ->
    OffHeapBin = crypto:strong_rand_bytes(1024 * 1024),
    Bin = <<OffHeapBin/binary, "a">>,
    Before = erlang:process_info(self(), [binary]),
    After = erlang:process_info(self(), [binary]),
    Before =/= After andalso erlang:display({wtf, Before, After}),
    Bin.

Fixes #6573

@github-actions
Copy link
Contributor

github-actions bot commented Dec 19, 2022

CT Test Results

No tests were run for this PR. This is either because the build failed, or the PR is based on a branch without GH actions tests configured.

Results for commit b0ad453

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

@bjorng bjorng added team:VM Assigned to OTP team VM fix labels Dec 19, 2022
@sverker
Copy link
Contributor

sverker commented Dec 19, 2022

@max-au Thanks for the PR. Looks good, test case and all.

One little nitpick. Writable binaries were not introduced by #5195, it's a quite old performance feature.
However, #5195 moved them into a separate list proc->wrt_bins as an optimization of the GC. Can you update the commit message, not to confuse people.

@sverker sverker self-requested a review December 19, 2022 13:35
@sverker sverker added the testing currently being tested, tag is used by OTP internal CI label Dec 19, 2022
When writable binaries were moved to a separate list
`proc->wrt_bins`, they weren't added to the output of
`process_info(Pid, binary)`. It led to interesting effects of
binaries suddenly disappearing after running implicit GC caused
by BIF calls, including `process_info` itself. Effectively,
running `process_info(self(), binary)` twice in a row resulted
in different lists, because first call triggers GC clearing out
original binary from the off-heap list:

    start() ->
        OffHeapBin = crypto:strong_rand_bytes(1024 * 1024),
        Bin = <<OffHeapBin/binary, "a">>,
        Before = erlang:process_info(self(), [binary]),
        After = erlang:process_info(self(), [binary]),
        Before =/= After andalso erlang:display({wtf, Before, After}),
        Bin.

Fixes erlang#6573
@max-au max-au force-pushed the max-au/fix-process-info-binary branch from 7996bf4 to b0ad453 Compare December 19, 2022 15:58
@max-au
Copy link
Contributor Author

max-au commented Dec 19, 2022

Updated, thanks for the correction!

While at it, I wonder if there is any way to take a peek into what's stored in these binaries. Something like process_info(Pid, binaries) that returns actual binaries (with their content) rather than a reference (VM internal pointer). Sometimes I'm getting curious why a process keeps off-heap binaries way longer than I expect. Knowing what those binaries contain can help.

@sverker
Copy link
Contributor

sverker commented Dec 20, 2022

While at it, I wonder if there is any way to take a peek into what's stored in these binaries. Something like process_info(Pid, binaries)

There is nothing like that, but it would be easy to implement. However, then you probably also want to know which parts of large shared binaries that are actually referred by the process. That is a bit trickier, as you have to scan the heap to find all ErlSubBin terms.

@markmeeus
Copy link

Thanks @max-au !

Just one thing I would like to add:

While it is totally correct to report orig_size for writable ProcBins, it is a bit unexpected to see the total size there. If there was more info in the tuple, (maybe a used_size and an is_writable) it would be more transparent.

Just mentioning this because I think it relates to your 'peeking into binaries' question.

@sverker sverker merged commit 43ad41d into erlang:maint Dec 21, 2022
@sverker
Copy link
Contributor

sverker commented Dec 21, 2022

Merged regression fix to maint for OTP 25.3.

@max-au max-au deleted the max-au/fix-process-info-binary branch December 23, 2022 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix 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