3

Apologies of this has been asked before/elsewhere but I cannot find the answer.

We have some issues in the minute following a deploy and we think they are Opcache related. On our live setup we have the following Opcache settings:

opcache.revalidate_freq=60
opcache.validate_timestamps=1

Which of the following does PHP do?

  1. When PHP needs a file, does it at that point check if it has been 60 seconds since it last generated a new cache of the file and if it has been more then generate a new one for this request?

  2. Or does it run on some form of timer (or something else) where the 60 seconds is unrelated to when it last needed the file?

I'd expect option 1 but that wouldn't explain our 60 seconds or so of problems as the file path for the files is different as we deploy to an alternating A or B directory each time.

I hope that makes sense? Thanks for your help.

2 Answers 2

4

Based on my analysis of the PHP source code, something similar to option 1 is what happens. When PHP needs a file, it checks if it has been opcache.revalidate_freq seconds since it last compiled or revalidated the file. If not, it skips the revalidation (timestamp check).

In other words, the opcache.revalidate_freq setting specifies the maximum frequency of timestamp checks. A file will not be revalidated until it is requested, even if it has been a year since the last revalidation.

Source

Here's the C function in ZendAccelerator.c that handles cache validation:

int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle)
{
    if (persistent_script->timestamp == 0) {
        return SUCCESS; /* Don't check timestamps of preloaded scripts */
    } else if (ZCG(accel_directives).revalidate_freq &&
        persistent_script->dynamic_members.revalidate >= ZCG(request_time)) {
        return SUCCESS;
    } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) {
        return FAILURE;
    } else {
        persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
        return SUCCESS;
    }
}

When a script is successfully revalidated – that is, when PHP checks the timestamp of a file and sees that it has not changed since the file was put in the cache, it will mark the file as "fresh" for another opcache.revalidate_freq seconds (the .revalidate property). This will prevent further timestamp checks in that time period (PHP will assume the file is fresh).

If the revalidation is unsuccessful – i.e. the timestamp is newer than what's in the cache, this leads to a recompilation, which also sets the same .revalidate property (this is not shown in the above code), again exempting the file from revalidation for the same period.

The above function appears to be called only from persistent_compile_file(), which is a function that is called whenever a script is executed. I couldn't find any other references to it that would indicate another trigger is used, such as a timer.

1

So, from the PHP manual

How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request.

So what's happening is you're updating the file, but your directive says that it's up to 60 seconds between the time you update the file and the time opcache builds a new opcode.

validate_timestamps has the answer. Because you want to manually revalidate the file you can do this

When this directive is disabled, you must reset OPcache manually via opcache_reset(), opcache_invalidate() or by restarting the Web server for changes to the filesystem to take effect.

1
  • 1
    The phrasing you quoted does not actually answer the OP's question. Jan 26, 2019 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.