22declare(strict_types=1);
34use Symfony\Component\Filesystem\Path;
38use
function file_exists;
39use
function file_put_contents;
42use
function gc_collect_cycles;
43use
function gc_disable;
44use
function gc_enable;
45use
function gc_mem_caches;
46use
function get_class;
47use
function get_declared_classes;
48use
function get_defined_functions;
54use
function is_object;
55use
function is_resource;
56use
function is_string;
57use
function json_encode;
58use
function mb_strtoupper;
61use
function preg_match;
64use
function spl_object_hash;
68use
const JSON_PRETTY_PRINT;
69use
const JSON_THROW_ON_ERROR;
70use
const JSON_UNESCAPED_SLASHES;
71use
const SORT_NUMERIC;
74 private const DEFAULT_CHECK_RATE = Server::TARGET_TICKS_PER_SECOND;
75 private const DEFAULT_CONTINUOUS_TRIGGER_RATE = Server::TARGET_TICKS_PER_SECOND * 2;
76 private const DEFAULT_TICKS_PER_GC = 30 * 60 * Server::TARGET_TICKS_PER_SECOND;
78 private int $memoryLimit;
79 private int $globalMemoryLimit;
80 private int $checkRate;
81 private int $checkTicker = 0;
82 private bool $lowMemory =
false;
84 private bool $continuousTrigger =
true;
85 private int $continuousTriggerRate;
86 private int $continuousTriggerCount = 0;
87 private int $continuousTriggerTicker = 0;
89 private int $garbageCollectionPeriod;
90 private int $garbageCollectionTicker = 0;
91 private bool $garbageCollectionTrigger;
92 private bool $garbageCollectionAsync;
94 private int $lowMemChunkRadiusOverride;
95 private bool $lowMemChunkGC;
97 private bool $lowMemDisableChunkCache;
98 private bool $lowMemClearWorldCache;
100 private bool $dumpWorkers =
true;
102 private \Logger $logger;
104 public function __construct(
107 $this->logger = new \PrefixedLogger($server->getLogger(),
"Memory Manager");
109 $this->init($server->getConfigGroup());
113 $this->memoryLimit = $config->getPropertyInt(Yml::MEMORY_MAIN_LIMIT, 0) * 1024 * 1024;
115 $defaultMemory = 1024;
117 if(preg_match(
"/([0-9]+)([KMGkmg])/", $config->getConfigString(
"memory-limit",
""), $matches) > 0){
118 $m = (int) $matches[1];
122 $defaultMemory = match(mb_strtoupper($matches[2])){
123 "K" => intdiv($m, 1024),
131 $hardLimit = $config->getPropertyInt(Yml::MEMORY_MAIN_HARD_LIMIT, $defaultMemory);
134 ini_set(
"memory_limit",
'-1');
136 ini_set(
"memory_limit", $hardLimit .
"M");
139 $this->globalMemoryLimit = $config->getPropertyInt(Yml::MEMORY_GLOBAL_LIMIT, 0) * 1024 * 1024;
140 $this->checkRate = $config->getPropertyInt(Yml::MEMORY_CHECK_RATE, self::DEFAULT_CHECK_RATE);
141 $this->continuousTrigger = $config->getPropertyBool(Yml::MEMORY_CONTINUOUS_TRIGGER,
true);
142 $this->continuousTriggerRate = $config->getPropertyInt(Yml::MEMORY_CONTINUOUS_TRIGGER_RATE, self::DEFAULT_CONTINUOUS_TRIGGER_RATE);
144 $this->garbageCollectionPeriod = $config->getPropertyInt(Yml::MEMORY_GARBAGE_COLLECTION_PERIOD, self::DEFAULT_TICKS_PER_GC);
145 $this->garbageCollectionTrigger = $config->getPropertyBool(Yml::MEMORY_GARBAGE_COLLECTION_LOW_MEMORY_TRIGGER,
true);
146 $this->garbageCollectionAsync = $config->getPropertyBool(Yml::MEMORY_GARBAGE_COLLECTION_COLLECT_ASYNC_WORKER,
true);
148 $this->lowMemChunkRadiusOverride = $config->getPropertyInt(Yml::MEMORY_MAX_CHUNKS_CHUNK_RADIUS, 4);
149 $this->lowMemChunkGC = $config->getPropertyBool(Yml::MEMORY_MAX_CHUNKS_TRIGGER_CHUNK_COLLECT,
true);
151 $this->lowMemDisableChunkCache = $config->getPropertyBool(Yml::MEMORY_WORLD_CACHES_DISABLE_CHUNK_CACHE,
true);
152 $this->lowMemClearWorldCache = $config->getPropertyBool(Yml::MEMORY_WORLD_CACHES_LOW_MEMORY_TRIGGER,
true);
154 $this->dumpWorkers = $config->getPropertyBool(Yml::MEMORY_MEMORY_DUMP_DUMP_ASYNC_WORKER,
true);
158 public function isLowMemory() :
bool{
159 return $this->lowMemory;
162 public function getGlobalMemoryLimit() :
int{
163 return $this->globalMemoryLimit;
166 public function canUseChunkCache() :
bool{
167 return !$this->lowMemory || !$this->lowMemDisableChunkCache;
174 return ($this->lowMemory && $this->lowMemChunkRadiusOverride > 0) ? min($this->lowMemChunkRadiusOverride, $distance) : $distance;
180 public function trigger(
int $memory,
int $limit,
bool $global =
false,
int $triggerCount = 0) : void{
181 $this->logger->debug(sprintf(
"%sLow memory triggered, limit %gMB, using %gMB",
182 $global ?
"Global " :
"", round(($limit / 1024) / 1024, 2), round(($memory / 1024) / 1024, 2)));
183 if($this->lowMemClearWorldCache){
184 foreach($this->
server->getWorldManager()->getWorlds() as $world){
185 $world->clearCache(
true);
187 ChunkCache::pruneCaches();
190 if($this->lowMemChunkGC){
191 foreach($this->
server->getWorldManager()->getWorlds() as $world){
192 $world->doChunkGarbageCollection();
196 $ev =
new LowMemoryEvent($memory, $limit, $global, $triggerCount);
200 if($this->garbageCollectionTrigger){
201 $cycles = $this->triggerGarbageCollector();
204 $this->logger->debug(sprintf(
"Freed %gMB, $cycles cycles", round(($ev->getMemoryFreed() / 1024) / 1024, 2)));
211 Timings::$memoryManager->startTiming();
213 if(($this->memoryLimit > 0 || $this->globalMemoryLimit > 0) && ++$this->checkTicker >= $this->checkRate){
214 $this->checkTicker = 0;
215 $memory = Process::getAdvancedMemoryUsage();
217 if($this->memoryLimit > 0 && $memory[0] > $this->memoryLimit){
219 }elseif($this->globalMemoryLimit > 0 && $memory[1] > $this->globalMemoryLimit){
223 if($trigger !==
false){
224 if($this->lowMemory && $this->continuousTrigger){
225 if(++$this->continuousTriggerTicker >= $this->continuousTriggerRate){
226 $this->continuousTriggerTicker = 0;
227 $this->trigger($memory[$trigger], $this->memoryLimit, $trigger > 0, ++$this->continuousTriggerCount);
230 $this->lowMemory =
true;
231 $this->continuousTriggerCount = 0;
232 $this->trigger($memory[$trigger], $this->memoryLimit, $trigger > 0);
235 $this->lowMemory =
false;
239 if($this->garbageCollectionPeriod > 0 && ++$this->garbageCollectionTicker >= $this->garbageCollectionPeriod){
240 $this->garbageCollectionTicker = 0;
241 $this->triggerGarbageCollector();
244 Timings::$memoryManager->stopTiming();
247 public function triggerGarbageCollector() : int{
248 Timings::$garbageCollector->startTiming();
250 if($this->garbageCollectionAsync){
251 $pool = $this->
server->getAsyncPool();
252 if(($w = $pool->shutdownUnusedWorkers()) > 0){
253 $this->logger->debug(
"Shut down $w idle async pool workers");
255 foreach($pool->getRunningWorkers() as $i){
256 $pool->submitTaskToWorker(
new GarbageCollectionTask(), $i);
260 $cycles = gc_collect_cycles();
263 Timings::$garbageCollector->stopTiming();
271 public function dumpServerMemory(
string $outputFolder,
int $maxNesting,
int $maxStringSize) : void{
273 $logger->
notice(
"After the memory dump is done, the server might crash");
274 self::dumpMemory($this->server, $outputFolder, $maxNesting, $maxStringSize, $logger);
276 if($this->dumpWorkers){
277 $pool = $this->
server->getAsyncPool();
278 foreach($pool->getRunningWorkers() as $i){
279 $pool->submitTaskToWorker(
new DumpWorkerMemoryTask($outputFolder, $maxNesting, $maxStringSize), $i);
287 public static function dumpMemory(mixed $startingObject,
string $outputFolder,
int $maxNesting,
int $maxStringSize, \
Logger $logger) : void{
288 $hardLimit =
Utils::assumeNotFalse(ini_get(
'memory_limit'),
"memory_limit INI directive should always exist");
289 ini_set(
'memory_limit',
'-1');
292 if(!file_exists($outputFolder)){
293 mkdir($outputFolder, 0777,
true);
296 $obData = Utils::assumeNotFalse(fopen(Path::join($outputFolder,
"objects.js"),
"wb+"));
302 $instanceCounts = [];
304 $staticProperties = [];
307 $functionStaticVars = [];
308 $functionStaticVarsCount = 0;
310 foreach(get_declared_classes() as $className){
311 $reflection = new \ReflectionClass($className);
312 $staticProperties[$className] = [];
313 foreach($reflection->getProperties() as $property){
314 if(!$property->isStatic() || $property->getDeclaringClass()->getName() !== $className){
318 if(!$property->isInitialized()){
323 $staticProperties[$className][$property->getName()] = self::continueDump($property->getValue(), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
326 if(count($staticProperties[$className]) === 0){
327 unset($staticProperties[$className]);
330 foreach($reflection->getMethods() as $method){
331 if($method->getDeclaringClass()->getName() !== $reflection->getName()){
335 foreach($method->getStaticVariables() as $name => $variable){
336 $methodStatics[$name] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
338 if(count($methodStatics) > 0){
339 $functionStaticVars[$className .
"::" . $method->getName()] = $methodStatics;
340 $functionStaticVarsCount += count($functionStaticVars);
345 file_put_contents(Path::join($outputFolder,
"staticProperties.js"), json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
346 $logger->
info(
"Wrote $staticCount static properties");
348 $globalVariables = [];
363 foreach($GLOBALS as $varName => $value){
364 if(isset($ignoredGlobals[$varName])){
369 $globalVariables[$varName] = self::continueDump($value, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
372 file_put_contents(Path::join($outputFolder,
"globalVariables.js"), json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
373 $logger->
info(
"Wrote $globalCount global variables");
375 foreach(get_defined_functions()[
"user"] as $function){
376 $reflect = new \ReflectionFunction($function);
379 foreach($reflect->getStaticVariables() as $varName => $variable){
380 $vars[$varName] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
382 if(count($vars) > 0){
383 $functionStaticVars[$function] = $vars;
384 $functionStaticVarsCount += count($vars);
387 file_put_contents(Path::join($outputFolder,
'functionStaticVars.js'), json_encode($functionStaticVars, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
388 $logger->
info(
"Wrote $functionStaticVarsCount function static variables");
390 $data = self::continueDump($startingObject, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
394 foreach(Utils::stringifyKeys($objects) as $hash => $object){
395 if(!is_object($object)){
400 $className = get_class($object);
401 if(!isset($instanceCounts[$className])){
402 $instanceCounts[$className] = 1;
404 $instanceCounts[$className]++;
407 $objects[$hash] =
true;
409 "information" =>
"$hash@$className",
411 if($object instanceof \Closure){
412 $info[
"definition"] = Utils::getNiceClosureName($object);
413 $info[
"referencedVars"] = [];
414 $reflect = new \ReflectionFunction($object);
415 if(($closureThis = $reflect->getClosureThis()) !==
null){
416 $info[
"this"] = self::continueDump($closureThis, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
419 foreach($reflect->getStaticVariables() as $name => $variable){
420 $info[
"referencedVars"][$name] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
423 $reflection = new \ReflectionObject($object);
425 $info[
"properties"] = [];
427 for($original = $reflection; $reflection !==
false; $reflection = $reflection->getParentClass()){
428 foreach($reflection->getProperties() as $property){
429 if($property->isStatic()){
433 $name = $property->getName();
434 if($reflection !== $original){
435 if($property->isPrivate()){
436 $name = $reflection->getName() .
":" . $name;
441 if(!$property->isInitialized($object)){
445 $info[
"properties"][$name] = self::continueDump($property->getValue($object), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
450 fwrite($obData, json_encode($info, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) .
"\n");
455 $logger->
info(
"Wrote " . count($objects) .
" objects");
459 file_put_contents(Path::join($outputFolder,
"serverEntry.js"), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
460 file_put_contents(Path::join($outputFolder,
"referenceCounts.js"), json_encode($refCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
462 arsort($instanceCounts, SORT_NUMERIC);
463 file_put_contents(Path::join($outputFolder,
"instanceCounts.js"), json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
465 $logger->
info(
"Finished!");
467 ini_set(
'memory_limit', $hardLimit);
480 private static function continueDump(mixed $from, array &$objects, array &$refCounts,
int $recursion,
int $maxNesting,
int $maxStringSize) : mixed{
481 if($maxNesting <= 0){
482 return "(error) NESTING LIMIT REACHED";
487 if(is_object($from)){
488 if(!isset($objects[$hash = spl_object_hash($from)])){
489 $objects[$hash] = $from;
490 $refCounts[$hash] = 0;
495 $data =
"(object) $hash";
496 }elseif(is_array($from)){
498 return "(error) ARRAY RECURSION LIMIT REACHED";
502 foreach($from as $key => $value){
504 "k" => self::continueDump($key, $objects, $refCounts, $recursion + 1, $maxNesting, $maxStringSize),
505 "v" => self::continueDump($value, $objects, $refCounts, $recursion + 1, $maxNesting, $maxStringSize),
509 }elseif(is_string($from)){
510 $data =
"(string) len(" . strlen($from) .
") " . substr(Utils::printable($from), 0, $maxStringSize);
511 }elseif(is_resource($from)){
512 $data =
"(resource) " . print_r($from,
true);
513 }elseif(is_float($from)){
514 $data =
"(float) $from";
dumpServerMemory(string $outputFolder, int $maxNesting, int $maxStringSize)
static dumpMemory(mixed $startingObject, string $outputFolder, int $maxNesting, int $maxStringSize, \Logger $logger)
getViewDistance(int $distance)
trigger(int $memory, int $limit, bool $global=false, int $triggerCount=0)