From 1e17d86421ed26b71cced6ab4126540fb562dbcb Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 13 Jan 2023 16:03:15 +0000 Subject: [PATCH] Constify server TPS and server tick time this makes it significantly easier to perform experiments involving the server TPS. --- src/Server.php | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Server.php b/src/Server.php index 6d762d538..3b1c22a32 100644 --- a/src/Server.php +++ b/src/Server.php @@ -181,6 +181,21 @@ class Server{ public const DEFAULT_PORT_IPV6 = 19133; public const DEFAULT_MAX_VIEW_DISTANCE = 16; + /** + * Worlds, network, commands and most other things are polled this many times per second on average. + * Between ticks, the server will sleep to ensure that the average tick rate is maintained. + * It may wake up between ticks if a Snooze notification source is triggered (e.g. to process network packets). + */ + public const TARGET_TICKS_PER_SECOND = 20; + /** + * The average time between ticks, in seconds. + */ + public const TARGET_SECONDS_PER_TICK = 1 / 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 static ?Server $instance = null; private SleeperHandler $tickSleeper; @@ -199,7 +214,7 @@ class Server{ private PluginManager $pluginManager; - private float $profilingTickRate = 20; + private float $profilingTickRate = self::TARGET_TICKS_PER_SECOND; private UpdateChecker $updater; @@ -209,10 +224,10 @@ class Server{ private int $tickCounter = 0; private float $nextTick = 0; /** @var float[] */ - private array $tickAverage = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]; + private array $tickAverage; /** @var float[] */ - private array $useAverage = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - private float $currentTPS = 20; + private array $useAverage; + private float $currentTPS = self::TARGET_TICKS_PER_SECOND; private float $currentUse = 0; private float $startTime; @@ -750,6 +765,8 @@ class Server{ } self::$instance = $this; $this->startTime = microtime(true); + $this->tickAverage = array_fill(0, self::TARGET_TICKS_PER_SECOND, self::TARGET_TICKS_PER_SECOND); + $this->useAverage = array_fill(0, self::TARGET_TICKS_PER_SECOND, 0); $this->tickSleeper = new SleeperHandler(); @@ -933,7 +950,7 @@ class Server{ Timings::init(); TimingsHandler::setEnabled($this->configGroup->getPropertyBool("settings.enable-profiling", false)); - $this->profilingTickRate = $this->configGroup->getPropertyInt("settings.profile-report-trigger", 20); + $this->profilingTickRate = $this->configGroup->getPropertyInt("settings.profile-report-trigger", self::TARGET_TICKS_PER_SECOND); DefaultPermissions::registerCorePermissions(); @@ -1793,11 +1810,11 @@ class Server{ $this->network->tick(); Timings::$connection->stopTiming(); - if(($this->tickCounter % 20) === 0){ + if(($this->tickCounter % self::TARGET_TICKS_PER_SECOND) === 0){ if($this->doTitleTick){ $this->titleTick(); } - $this->currentTPS = 20; + $this->currentTPS = self::TARGET_TICKS_PER_SECOND; $this->currentUse = 0; $queryRegenerateEvent = new QueryRegenerateEvent(new QueryInfo($this)); @@ -1818,7 +1835,7 @@ class Server{ $world->clearCache(); } - if($this->getTicksPerSecondAverage() < 12){ + if($this->getTicksPerSecondAverage() < self::TPS_OVERLOAD_WARNING_THRESHOLD){ $this->logger->warning($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_tickOverload())); } } @@ -1837,19 +1854,19 @@ class Server{ Timings::$serverTick->stopTiming(); $now = microtime(true); - $this->currentTPS = min(20, 1 / max(0.001, $now - $tickTime)); - $this->currentUse = min(1, ($now - $tickTime) / 0.05); + $this->currentTPS = min(self::TARGET_TICKS_PER_SECOND, 1 / max(0.001, $now - $tickTime)); + $this->currentUse = min(1, ($now - $tickTime) / self::TARGET_SECONDS_PER_TICK); TimingsHandler::tick($this->currentTPS <= $this->profilingTickRate); - $idx = $this->tickCounter % 20; + $idx = $this->tickCounter % self::TARGET_TICKS_PER_SECOND; $this->tickAverage[$idx] = $this->currentTPS; $this->useAverage[$idx] = $this->currentUse; if(($this->nextTick - $tickTime) < -1){ $this->nextTick = $tickTime; }else{ - $this->nextTick += 0.05; + $this->nextTick += self::TARGET_SECONDS_PER_TICK; } } }