Skip to content

Commit 600fe77

Browse files
authored
Merge pull request #11218 from vimeo/vm_overcommit_warning
Add VM overcommit warning
2 parents 03ae62f + bcb25cc commit 600fe77

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Psalm/Internal/Cli/Psalm.php

+31
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use ReflectionClass;
3838
use RuntimeException;
3939
use Symfony\Component\Filesystem\Path;
40+
use Throwable;
4041

4142
use function array_filter;
4243
use function array_key_exists;
@@ -51,6 +52,7 @@
5152
use function defined;
5253
use function extension_loaded;
5354
use function file_exists;
55+
use function file_get_contents;
5456
use function file_put_contents;
5557
use function function_exists;
5658
use function fwrite;
@@ -79,6 +81,7 @@
7981
use function str_starts_with;
8082
use function strlen;
8183
use function substr;
84+
use function trim;
8285
use function wordwrap;
8386

8487
use const DIRECTORY_SEPARATOR;
@@ -985,6 +988,34 @@ private static function restart(array $options, int $threads, int $scanThreads,
985988
$progress->write('Exiting because JIT was requested but is not available.' . PHP_EOL . PHP_EOL);
986989
exit(1);
987990
}
991+
992+
$overcommit = null;
993+
try {
994+
$overcommit = trim(file_get_contents('/proc/sys/vm/overcommit_memory'));
995+
} catch (Throwable) {
996+
}
997+
998+
if ($overcommit === '2') {
999+
$err = 'ERROR: VM overcommiting is disabled.' . PHP_EOL . PHP_EOL
1000+
. "TL;DR: to fix, run these two commands:" . PHP_EOL . PHP_EOL
1001+
. "echo 1 | sudo tee /proc/sys/vm/overcommit_memory" . PHP_EOL
1002+
. "echo vm.overcommit_memory=1 | sudo tee /etc/sysctl.d/40-psalm.conf # For persistence" . PHP_EOL
1003+
. PHP_EOL
1004+
. "Explanation: disabling VM overcommitting *WILL* cause failures when running Psalm "
1005+
. "in multithreaded mode during analysis," . PHP_EOL
1006+
. 'as Psalm relies very heavily on the copy-on-write semantics of fork(), which are currently disabled.'
1007+
. PHP_EOL . PHP_EOL . PHP_EOL
1008+
. "Please enable VM overcommitting to greatly speed up Psalm and avoid crashes in multithreaded mode."
1009+
. PHP_EOL . PHP_EOL . PHP_EOL
1010+
. "This warning may be ignored by setting the PSALM_IGNORE_NO_OVERCOMMIT=1 environment variable "
1011+
. "(not recommended)."
1012+
. PHP_EOL . PHP_EOL;
1013+
1014+
fwrite(STDERR, $err);
1015+
if (getenv('PSALM_IGNORE_NO_OVERCOMMIT') !== '1') {
1016+
exit(1);
1017+
}
1018+
}
9881019
}
9891020

9901021
/** @psalm-suppress UnusedParam $argv is being reported as unused */

0 commit comments

Comments
 (0)