Process: split getMemoryUsage() into 2 functions

this isn't released yet so it's OK to change.
phpstan level 7 doesn't like these kinds of ambiguous return types because there's no way for it to tell which type is returned without a return type specifying extension, and it's easier to just change the API than to make PHPStan understand it.
This commit is contained in:
Dylan K. Taylor 2020-03-13 17:32:17 +00:00
parent 1171cd2493
commit 3907a2b6ba
7 changed files with 15 additions and 12 deletions

View File

@ -226,7 +226,7 @@ class MemoryManager{
if(($this->memoryLimit > 0 or $this->globalMemoryLimit > 0) and ++$this->checkTicker >= $this->checkRate){ if(($this->memoryLimit > 0 or $this->globalMemoryLimit > 0) and ++$this->checkTicker >= $this->checkRate){
$this->checkTicker = 0; $this->checkTicker = 0;
$memory = Process::getMemoryUsage(true); $memory = Process::getAdvancedMemoryUsage();
$trigger = false; $trigger = false;
if($this->memoryLimit > 0 and $memory[0] > $this->memoryLimit){ if($this->memoryLimit > 0 and $memory[0] > $this->memoryLimit){
$trigger = 0; $trigger = 0;

View File

@ -2325,7 +2325,7 @@ class Server{
Timings::$titleTickTimer->startTiming(); Timings::$titleTickTimer->startTiming();
$d = Process::getRealMemoryUsage(); $d = Process::getRealMemoryUsage();
$u = Process::getMemoryUsage(true); $u = Process::getAdvancedMemoryUsage();
$usage = sprintf("%g/%g/%g/%g MB @ %d threads", round(($u[0] / 1024) / 1024, 2), round(($d[0] / 1024) / 1024, 2), round(($u[1] / 1024) / 1024, 2), round(($u[2] / 1024) / 1024, 2), Process::getThreadCount()); $usage = sprintf("%g/%g/%g/%g MB @ %d threads", round(($u[0] / 1024) / 1024, 2), round(($d[0] / 1024) / 1024, 2), round(($u[1] / 1024) / 1024, 2), round(($u[2] / 1024) / 1024, 2), Process::getThreadCount());
echo "\x1b]0;" . $this->getName() . " " . echo "\x1b]0;" . $this->getName() . " " .

View File

@ -49,7 +49,7 @@ class StatusCommand extends VanillaCommand{
} }
$rUsage = Process::getRealMemoryUsage(); $rUsage = Process::getRealMemoryUsage();
$mUsage = Process::getMemoryUsage(true); $mUsage = Process::getAdvancedMemoryUsage();
$server = $sender->getServer(); $server = $sender->getServer();
$sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::WHITE . "Server status" . TextFormat::GREEN . " ----"); $sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::WHITE . "Server status" . TextFormat::GREEN . " ----");

View File

@ -75,6 +75,7 @@ class LowMemoryEvent extends ServerEvent{
* Amount of memory already freed * Amount of memory already freed
*/ */
public function getMemoryFreed() : int{ public function getMemoryFreed() : int{
return $this->getMemory() - ($this->isGlobal() ? Process::getMemoryUsage(true)[1] : Process::getMemoryUsage(true)[0]); $usage = Process::getAdvancedMemoryUsage();
return $this->getMemory() - ($this->isGlobal() ? $usage[1] : $usage[0]);
} }
} }

View File

@ -136,7 +136,7 @@ class SendUsageTask extends AsyncTask{
"historyList" => array_values($playerList) "historyList" => array_values($playerList)
]; ];
$info = Process::getMemoryUsage(true); $info = Process::getAdvancedMemoryUsage();
$data["system"] = [ $data["system"] = [
"mainMemory" => $info[0], "mainMemory" => $info[0],
"totalMemory" => $info[1], "totalMemory" => $info[1],

View File

@ -47,9 +47,10 @@ final class Process{
} }
/** /**
* @return int[]|int * @return int[]
* @phpstan-return array{int,int,int}
*/ */
public static function getMemoryUsage(bool $advanced = false){ public static function getAdvancedMemoryUsage(){
$reserved = memory_get_usage(); $reserved = memory_get_usage();
$VmSize = null; $VmSize = null;
$VmRSS = null; $VmRSS = null;
@ -70,10 +71,6 @@ final class Process{
$VmRSS = memory_get_usage(); $VmRSS = memory_get_usage();
} }
if(!$advanced){
return $VmRSS;
}
if($VmSize === null){ if($VmSize === null){
$VmSize = memory_get_usage(true); $VmSize = memory_get_usage(true);
} }
@ -81,6 +78,10 @@ final class Process{
return [$reserved, $VmRSS, $VmSize]; return [$reserved, $VmRSS, $VmSize];
} }
public static function getMemoryUsage() : int{
return self::getAdvancedMemoryUsage()[1];
}
/** /**
* @return int[] * @return int[]
*/ */

View File

@ -283,11 +283,12 @@ class Utils{
/** /**
* @deprecated * @deprecated
* @see Process::getMemoryUsage() * @see Process::getMemoryUsage()
* @see Process::getAdvancedMemoryUsage()
* *
* @return int[]|int * @return int[]|int
*/ */
public static function getMemoryUsage(bool $advanced = false){ public static function getMemoryUsage(bool $advanced = false){
return Process::getMemoryUsage($advanced); return $advanced ? Process::getAdvancedMemoryUsage() : Process::getMemoryUsage();
} }
/** /**