MemoryManager: use prefixed loggers

This commit is contained in:
Dylan K. Taylor 2019-06-04 13:49:07 +01:00
parent 3569f8dfbe
commit e69ab60034
2 changed files with 15 additions and 10 deletions

View File

@ -110,8 +110,12 @@ class MemoryManager{
/** @var bool */ /** @var bool */
private $dumpWorkers = true; private $dumpWorkers = true;
/** @var \Logger */
private $logger;
public function __construct(Server $server){ public function __construct(Server $server){
$this->server = $server; $this->server = $server;
$this->logger = new \PrefixedLogger($server->getLogger(), "Memory Manager");
$this->init(); $this->init();
} }
@ -211,7 +215,7 @@ class MemoryManager{
* @param int $triggerCount * @param int $triggerCount
*/ */
public function trigger(int $memory, int $limit, bool $global = false, int $triggerCount = 0) : void{ public function trigger(int $memory, int $limit, bool $global = false, int $triggerCount = 0) : void{
$this->server->getLogger()->debug(sprintf("[Memory Manager] %sLow memory triggered, limit %gMB, using %gMB", $this->logger->debug(sprintf("%sLow memory triggered, limit %gMB, using %gMB",
$global ? "Global " : "", round(($limit / 1024) / 1024, 2), round(($memory / 1024) / 1024, 2))); $global ? "Global " : "", round(($limit / 1024) / 1024, 2), round(($memory / 1024) / 1024, 2)));
if($this->lowMemClearWorldCache){ if($this->lowMemClearWorldCache){
foreach($this->server->getWorldManager()->getWorlds() as $world){ foreach($this->server->getWorldManager()->getWorlds() as $world){
@ -233,7 +237,7 @@ class MemoryManager{
$cycles = $this->triggerGarbageCollector(); $cycles = $this->triggerGarbageCollector();
} }
$this->server->getLogger()->debug(sprintf("[Memory Manager] Freed %gMB, $cycles cycles", round(($ev->getMemoryFreed() / 1024) / 1024, 2))); $this->logger->debug(sprintf("Freed %gMB, $cycles cycles", round(($ev->getMemoryFreed() / 1024) / 1024, 2)));
} }
/** /**
@ -285,7 +289,7 @@ class MemoryManager{
if($this->garbageCollectionAsync){ if($this->garbageCollectionAsync){
$pool = $this->server->getAsyncPool(); $pool = $this->server->getAsyncPool();
if(($w = $pool->shutdownUnusedWorkers()) > 0){ if(($w = $pool->shutdownUnusedWorkers()) > 0){
$this->server->getLogger()->debug("Shut down $w idle async pool workers"); $this->logger->debug("Shut down $w idle async pool workers");
} }
foreach($pool->getRunningWorkers() as $i){ foreach($pool->getRunningWorkers() as $i){
$pool->submitTaskToWorker(new GarbageCollectionTask(), $i); $pool->submitTaskToWorker(new GarbageCollectionTask(), $i);
@ -307,8 +311,9 @@ class MemoryManager{
* @param int $maxStringSize * @param int $maxStringSize
*/ */
public function dumpServerMemory(string $outputFolder, int $maxNesting, int $maxStringSize) : void{ public function dumpServerMemory(string $outputFolder, int $maxNesting, int $maxStringSize) : void{
$this->server->getLogger()->notice("[Dump] After the memory dump is done, the server might crash"); $logger = new \PrefixedLogger($this->server->getLogger(), "Memory Dump");
self::dumpMemory($this->server, $outputFolder, $maxNesting, $maxStringSize, $this->server->getLogger()); $logger->notice("After the memory dump is done, the server might crash");
self::dumpMemory($this->server, $outputFolder, $maxNesting, $maxStringSize, $logger);
if($this->dumpWorkers){ if($this->dumpWorkers){
$pool = $this->server->getAsyncPool(); $pool = $this->server->getAsyncPool();
@ -373,7 +378,7 @@ class MemoryManager{
} }
file_put_contents($outputFolder . "/staticProperties.js", json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/staticProperties.js", json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
$logger->info("[Dump] Wrote $staticCount static properties"); $logger->info("Wrote $staticCount static properties");
if(isset($GLOBALS)){ //This might be null if we're on a different thread if(isset($GLOBALS)){ //This might be null if we're on a different thread
$globalVariables = []; $globalVariables = [];
@ -401,7 +406,7 @@ class MemoryManager{
} }
file_put_contents($outputFolder . "/globalVariables.js", json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/globalVariables.js", json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
$logger->info("[Dump] Wrote $globalCount global variables"); $logger->info("Wrote $globalCount global variables");
} }
self::continueDump($startingObject, $data, $objects, $refCounts, 0, $maxNesting, $maxStringSize); self::continueDump($startingObject, $data, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
@ -462,7 +467,7 @@ class MemoryManager{
}while($continue); }while($continue);
$logger->info("[Dump] Wrote " . count($objects) . " objects"); $logger->info("Wrote " . count($objects) . " objects");
fclose($obData); fclose($obData);
@ -472,7 +477,7 @@ class MemoryManager{
arsort($instanceCounts, SORT_NUMERIC); arsort($instanceCounts, SORT_NUMERIC);
file_put_contents($outputFolder . "/instanceCounts.js", json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/instanceCounts.js", json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
$logger->info("[Dump] Finished!"); $logger->info("Finished!");
ini_set('memory_limit', $hardLimit); ini_set('memory_limit', $hardLimit);
gc_enable(); gc_enable();

View File

@ -54,7 +54,7 @@ class DumpWorkerMemoryTask extends AsyncTask{
$this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(), $this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(),
$this->maxNesting, $this->maxNesting,
$this->maxStringSize, $this->maxStringSize,
$this->worker->getLogger() new \PrefixedLogger($this->worker->getLogger(), "Memory Dump")
); );
} }
} }