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

Disable opcache.preload if it's enabled #9388

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 52 additions & 14 deletions src/Psalm/Internal/Fork/PsalmRestarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use function in_array;
use function ini_get;
use function preg_replace;
use function strlen;
use function strtolower;

use const PHP_VERSION_ID;

Expand All @@ -22,6 +24,14 @@
*/
class PsalmRestarter extends XdebugHandler
{
private const REQUIRED_OPCACHE_SETTINGS = [
'enable_cli' => true,
'jit' => 1205,
'jit_buffer_size' => 512 * 1024 * 1024,
'optimization_level' => '0x7FFEBFFF',
'preload' => '',
];

private bool $required = false;

/**
Expand Down Expand Up @@ -53,28 +63,54 @@ protected function requiresRestart($default): bool
static fn(string $extension): bool => extension_loaded($extension)
);

if (PHP_VERSION_ID >= 8_00_00 && (extension_loaded('opcache') || extension_loaded('Zend OPcache'))) {
$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');

if (PHP_VERSION_ID >= 8_00_00 && $opcache_loaded) {
// restart to enable JIT if it's not configured in the optimal way
if (!in_array(ini_get('opcache.enable_cli'), ['1', 'true', true, 1])) {
return true;
}
$opcache_settings = [
'enable_cli' => in_array(ini_get('opcache.enable_cli'), ['1', 'true', true, 1]),
'jit' => (int) ini_get('opcache.jit'),
'optimization_level' => (string) ini_get('opcache.optimization_level'),
'preload' => (string) ini_get('opcache.preload'),
'jit_buffer_size' => self::toBytes(ini_get('opcache.jit_buffer_size')),
];

if (((int) ini_get('opcache.jit')) !== 1205) {
return true;
foreach (self::REQUIRED_OPCACHE_SETTINGS as $ini_name => $required_value) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't this be just an if (self::REQUIRED_OPCACHE_SETTINGS !== $opcache_settings) { return true; }?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps, but that would require the keys in both arrays to be in the same order, which could easily be overlooked when adding/removing items.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense!

if ($opcache_settings[$ini_name] !== $required_value) {
return true;
}
}
}

if (((int) ini_get('opcache.jit')) === 0) {
return true;
}
return $default || $this->required;
}

if (ini_get('opcache.optimization_level') !== '0x7FFEBFFF') {
return true;
}
private static function toBytes(string $value): int
{
$unit = strtolower($value[strlen($value) - 1]);

if (in_array($unit, ['g', 'm', 'k'], true)) {
$value = (int) $value;
} else {
$unit = '';
$value = (int) $value;
}

return $default || $this->required;
switch ($unit) {
case 'g':
$value *= 1024;
// no break
case 'm':
$value *= 1024;
// no break
case 'k':
$value *= 1024;
}

return $value;
}


/**
* No type hint to allow xdebug-handler v1 and v2 usage
*
Expand All @@ -93,17 +129,19 @@ protected function restart($command): void
}

$additional_options = [];
$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');

// executed in the parent process (before restart)
// if it wasn't loaded then we apparently don't have opcache installed and there's no point trying
// to tweak it
// If we're running on 7.4 there's no JIT available
if (PHP_VERSION_ID >= 8_00_00 && (extension_loaded('opcache') || extension_loaded('Zend OPcache'))) {
if (PHP_VERSION_ID >= 8_00_00 && $opcache_loaded) {
$additional_options = [
'-dopcache.enable_cli=true',
'-dopcache.jit_buffer_size=512M',
'-dopcache.jit=1205',
'-dopcache.optimization_level=0x7FFEBFFF',
'-dopcache.preload=',
];
}

Expand Down