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?
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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With