From dff3f45d22b65e8fb03ca684ad2edc0f80a3f2d5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 13 Jan 2023 16:29:09 +0000 Subject: [PATCH] Constify more tick-related things --- src/MemoryManager.php | 10 +++++++--- src/Server.php | 23 +++++++++++++++-------- src/timings/TimingsRecord.php | 5 +++-- src/world/WorldManager.php | 8 +++++--- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/MemoryManager.php b/src/MemoryManager.php index 14ecc03cd..0162f0be0 100644 --- a/src/MemoryManager.php +++ b/src/MemoryManager.php @@ -69,6 +69,10 @@ use const JSON_UNESCAPED_SLASHES; use const SORT_NUMERIC; class MemoryManager{ + private const DEFAULT_CHECK_RATE = Server::TARGET_TICKS_PER_SECOND; + private const DEFAULT_CONTINUOUS_TRIGGER_RATE = Server::TARGET_TICKS_PER_SECOND * 2; + private const DEEFAULT_TICKS_PER_GC = 30 * 60 * Server::TARGET_TICKS_PER_SECOND; + private int $memoryLimit; private int $globalMemoryLimit; private int $checkRate; @@ -131,11 +135,11 @@ class MemoryManager{ } $this->globalMemoryLimit = $config->getPropertyInt("memory.global-limit", 0) * 1024 * 1024; - $this->checkRate = $config->getPropertyInt("memory.check-rate", 20); + $this->checkRate = $config->getPropertyInt("memory.check-rate", self::DEFAULT_CHECK_RATE); $this->continuousTrigger = $config->getPropertyBool("memory.continuous-trigger", true); - $this->continuousTriggerRate = $config->getPropertyInt("memory.continuous-trigger-rate", 30); + $this->continuousTriggerRate = $config->getPropertyInt("memory.continuous-trigger-rate", self::DEFAULT_CONTINUOUS_TRIGGER_RATE); - $this->garbageCollectionPeriod = $config->getPropertyInt("memory.garbage-collection.period", 36000); + $this->garbageCollectionPeriod = $config->getPropertyInt("memory.garbage-collection.period", self::DEEFAULT_TICKS_PER_GC); $this->garbageCollectionTrigger = $config->getPropertyBool("memory.garbage-collection.low-memory-trigger", true); $this->garbageCollectionAsync = $config->getPropertyBool("memory.garbage-collection.collect-async-worker", true); diff --git a/src/Server.php b/src/Server.php index 3b1c22a32..8f6d732e6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -117,6 +117,7 @@ use pocketmine\world\WorldCreationOptions; use pocketmine\world\WorldManager; use Ramsey\Uuid\UuidInterface; use Symfony\Component\Filesystem\Path; +use function array_fill; use function array_sum; use function base64_encode; use function cli_set_process_title; @@ -191,10 +192,16 @@ class Server{ * The average time between ticks, in seconds. */ public const TARGET_SECONDS_PER_TICK = 1 / self::TARGET_TICKS_PER_SECOND; + public const TARGET_NANOSECONDS_PER_TICK = 1_000_000_000 / self::TARGET_TICKS_PER_SECOND; + /** * The TPS threshold below which the server will generate log warnings. */ - public const TPS_OVERLOAD_WARNING_THRESHOLD = self::TARGET_TICKS_PER_SECOND * 0.6; + private const TPS_OVERLOAD_WARNING_THRESHOLD = self::TARGET_TICKS_PER_SECOND * 0.6; + + private const TICKS_PER_WORLD_CACHE_CLEAR = 5 * self::TARGET_TICKS_PER_SECOND; + private const TICKS_PER_TPS_OVERLOAD_WARNING = 5 * self::TARGET_TICKS_PER_SECOND; + private const TICKS_PER_STATS_REPORT = 300 * self::TARGET_TICKS_PER_SECOND; private static ?Server $instance = null; @@ -988,7 +995,7 @@ class Server{ $this->worldManager = new WorldManager($this, Path::join($this->dataPath, "worlds"), $providerManager); $this->worldManager->setAutoSave($this->configGroup->getConfigBool("auto-save", $this->worldManager->getAutoSave())); - $this->worldManager->setAutoSaveInterval($this->configGroup->getPropertyInt("ticks-per.autosave", 6000)); + $this->worldManager->setAutoSaveInterval($this->configGroup->getPropertyInt("ticks-per.autosave", $this->worldManager->getAutoSaveInterval())); $this->updater = new UpdateChecker($this, $this->configGroup->getPropertyString("auto-updater.host", "update.pmmp.io")); @@ -1028,7 +1035,7 @@ class Server{ } if($this->configGroup->getPropertyBool("anonymous-statistics.enabled", true)){ - $this->sendUsageTicker = 6000; + $this->sendUsageTicker = self::TICKS_PER_STATS_REPORT; $this->sendUsage(SendUsageTask::TYPE_OPEN); } @@ -1826,18 +1833,18 @@ class Server{ } if($this->sendUsageTicker > 0 && --$this->sendUsageTicker === 0){ - $this->sendUsageTicker = 6000; + $this->sendUsageTicker = self::TICKS_PER_STATS_REPORT; $this->sendUsage(SendUsageTask::TYPE_STATUS); } - if(($this->tickCounter % 100) === 0){ + if(($this->tickCounter % self::TICKS_PER_WORLD_CACHE_CLEAR) === 0){ foreach($this->worldManager->getWorlds() as $world){ $world->clearCache(); } + } - if($this->getTicksPerSecondAverage() < self::TPS_OVERLOAD_WARNING_THRESHOLD){ - $this->logger->warning($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_tickOverload())); - } + if(($this->tickCounter % self::TICKS_PER_TPS_OVERLOAD_WARNING) === 0 && $this->getTicksPerSecondAverage() < self::TPS_OVERLOAD_WARNING_THRESHOLD){ + $this->logger->warning($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_tickOverload())); } $this->getMemoryManager()->check(); diff --git a/src/timings/TimingsRecord.php b/src/timings/TimingsRecord.php index 0ba03d522..8ee752ae2 100644 --- a/src/timings/TimingsRecord.php +++ b/src/timings/TimingsRecord.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\timings; +use pocketmine\Server; use function round; use function spl_object_id; @@ -54,8 +55,8 @@ final class TimingsRecord{ public static function tick(bool $measure = true) : void{ if($measure){ foreach(self::$records as $record){ - if($record->curTickTotal > 50000000){ - $record->violations += (int) round($record->curTickTotal / 50000000); + if($record->curTickTotal > Server::TARGET_NANOSECONDS_PER_TICK){ + $record->violations += (int) round($record->curTickTotal / Server::TARGET_NANOSECONDS_PER_TICK); } $record->curTickTotal = 0; $record->curCount = 0; diff --git a/src/world/WorldManager.php b/src/world/WorldManager.php index 80561298d..9ee68cc0e 100644 --- a/src/world/WorldManager.php +++ b/src/world/WorldManager.php @@ -55,12 +55,14 @@ use function strval; use function trim; class WorldManager{ + public const TICKS_PER_AUTOSAVE = 300 * Server::TARGET_TICKS_PER_SECOND; + /** @var World[] */ private array $worlds = []; private ?World $defaultWorld = null; private bool $autoSave = true; - private int $autoSaveTicks = 6000; + private int $autoSaveTicks = self::TICKS_PER_AUTOSAVE; private int $autoSaveTicker = 0; public function __construct( @@ -348,8 +350,8 @@ class WorldManager{ $world->doTick($currentTick); $tickMs = (microtime(true) - $worldTime) * 1000; $world->tickRateTime = $tickMs; - if($tickMs >= 50){ - $world->getLogger()->debug(sprintf("Tick took too long: %gms (%g ticks)", $tickMs, round($tickMs / 50, 2))); + if($tickMs >= Server::TARGET_SECONDS_PER_TICK){ + $world->getLogger()->debug(sprintf("Tick took too long: %gms (%g ticks)", $tickMs, round($tickMs / Server::TARGET_SECONDS_PER_TICK, 2))); } }