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

Cleanup of entries should not be skipped when using default settings #536

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions apc_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,7 @@ PHP_APCU_API void apc_cache_clear(apc_cache_t* cache)
PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
{
time_t t;
size_t suitable = 0L;
size_t available = 0L;
size_t i;

if (!cache) {
return;
Expand All @@ -751,43 +750,38 @@ PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
return;
}

/* make suitable selection */
suitable = (cache->smart > 0L) ? (size_t) (cache->smart * size) : (size_t) (cache->sma->size/2);

/* gc */
apc_cache_wlocked_gc(cache);

/* get available */
available = apc_sma_get_avail_mem(cache->sma);

/* check that expunge is necessary */
if (available < suitable) {
size_t i;

/* look for junk */
for (i = 0; i < cache->nslots; i++) {
apc_cache_entry_t **entry = &cache->slots[i];
while (*entry) {
if (apc_cache_entry_expired(cache, *entry, t)) {
apc_cache_wlocked_remove_entry(cache, entry);
continue;
}
if (cache->smart > 0L && apc_sma_get_avail_mem(cache->sma) >= (size_t) (cache->smart * size)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this even do anything useful? We're only going to call expunge if there is not enough space for size, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah no, this is checking for total free size, not available single block. But then doesn't setting smart > 0 still result in expunge being skipped sometimes even though it may have been possible to free up consecutive space?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it does and I'd like to change how smart works to address this issue as well. I made a suggestion how smart could be changed in #533. Then I opened this pr (#536) because i wasn't sure if it is a good idea to change the behavior of smart. But finally i think we should do it, because the current implementation seems to be not very useful and will cause unexpected behavior.

If you think it makes sense to change the behavior of smart, I could make the entire change (including smart) here or open a new PR for it. Which do you prefer?

apc_cache_wunlock(cache);
return;
}

/* grab next entry */
entry = &(*entry)->next;
/* look for junk */
for (i = 0; i < cache->nslots; i++) {
apc_cache_entry_t **entry = &cache->slots[i];
while (*entry) {
if (apc_cache_entry_expired(cache, *entry, t)) {
apc_cache_wlocked_remove_entry(cache, entry);
continue;
}
}

/* if the cache now has space, then reset last key */
if (apc_sma_get_avail_size(cache->sma, size)) {
/* wipe lastkey */
memset(&cache->header->lastkey, 0, sizeof(apc_cache_slam_key_t));
} else {
/* with not enough space left in cache, we are forced to expunge */
apc_cache_wlocked_real_expunge(cache);
/* grab next entry */
entry = &(*entry)->next;
}
}

/* if the cache now has space, then reset last key */
if (apc_sma_get_avail_size(cache->sma, size)) {
/* wipe lastkey */
memset(&cache->header->lastkey, 0, sizeof(apc_cache_slam_key_t));
} else {
/* with not enough space left in cache, we are forced to expunge */
apc_cache_wlocked_real_expunge(cache);
}

apc_cache_wunlock(cache);
}
/* }}} */
Expand Down