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

Fix apc_sma_get_avail_size() to look for a contiguous block of memory to make expunge more reliable #532

Conversation

madmajestro
Copy link
Contributor

@madmajestro madmajestro commented Feb 25, 2025

apc_sma_get_avail_size() is used by apc_cache_default_expunge() to check whether enough memory is available to store a new cache entry after expired entries are removed. If there is not enough memory available, a real expunge is performed to be able to store the new entry.

The problem is that apc_sma_get_avail_size() only checks the total amount of available memory, which is usually divided into several fragments of memory. However, we need a contiguous block of memory to store a cache entry.

This pull request fixes apc_sma_get_avail_size() to check if a continuous block of memory is available to store a new entry, which solves the problem.

Prior to this fix, the result was that in situations with fragmentation, new cache entries cannot be saved and PHP functions such as apcu_store() return false, which is not the expected behavior.

@madmajestro
Copy link
Contributor Author

The test failed because before this pull request, the last insert in the loop did not trigger a full_expunge of the whole cache (since apc_sma_get_avail_size() didn't care about contiguous memory blocks). As a result, the last apcu_store() command in the loop failed. With this pull request, the last apcu_store() command is successful.

@madmajestro madmajestro force-pushed the apc_sma_get_avail_size-should-check-for-contiguous-mem branch 2 times, most recently from a9d781e to aafec90 Compare February 26, 2025 10:04
apc_sma_get_avail_size() is used by apc_cache_default_expunge() to check
if enough memory is available to store the next cache entry. Therefore,
it is not enough to check the total amount of available memory. The
memory must be available in one contiguous block.
The memory occupied by “dummy” should be large enough for one of the
entries inserted by the loop to fit into it. This allows the last
inserted record of the loop (which triggers expunge) to replace this
entry, which prevents a real_expunge of the whole cache.
@madmajestro madmajestro force-pushed the apc_sma_get_avail_size-should-check-for-contiguous-mem branch from aafec90 to 12c92ac Compare February 26, 2025 15:24
@madmajestro
Copy link
Contributor Author

I fixed the things from your review and also clarified the comment in the header file. I hope that's okay, otherwise feel free to comment...

@nikic nikic merged commit f26d563 into krakjoe:master Feb 26, 2025
31 checks passed
@madmajestro madmajestro deleted the apc_sma_get_avail_size-should-check-for-contiguous-mem branch March 9, 2025 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants