Constify more tick-related things

This commit is contained in:
Dylan K. Taylor 2023-01-13 16:29:09 +00:00
parent 1e17d86421
commit dff3f45d22
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 30 additions and 16 deletions

View File

@ -69,6 +69,10 @@ use const JSON_UNESCAPED_SLASHES;
use const SORT_NUMERIC; use const SORT_NUMERIC;
class MemoryManager{ 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 $memoryLimit;
private int $globalMemoryLimit; private int $globalMemoryLimit;
private int $checkRate; private int $checkRate;
@ -131,11 +135,11 @@ class MemoryManager{
} }
$this->globalMemoryLimit = $config->getPropertyInt("memory.global-limit", 0) * 1024 * 1024; $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->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->garbageCollectionTrigger = $config->getPropertyBool("memory.garbage-collection.low-memory-trigger", true);
$this->garbageCollectionAsync = $config->getPropertyBool("memory.garbage-collection.collect-async-worker", true); $this->garbageCollectionAsync = $config->getPropertyBool("memory.garbage-collection.collect-async-worker", true);

View File

@ -117,6 +117,7 @@ use pocketmine\world\WorldCreationOptions;
use pocketmine\world\WorldManager; use pocketmine\world\WorldManager;
use Ramsey\Uuid\UuidInterface; use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function array_fill;
use function array_sum; use function array_sum;
use function base64_encode; use function base64_encode;
use function cli_set_process_title; use function cli_set_process_title;
@ -191,10 +192,16 @@ class Server{
* The average time between ticks, in seconds. * The average time between ticks, in seconds.
*/ */
public const TARGET_SECONDS_PER_TICK = 1 / self::TARGET_TICKS_PER_SECOND; 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. * 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; private static ?Server $instance = null;
@ -988,7 +995,7 @@ class Server{
$this->worldManager = new WorldManager($this, Path::join($this->dataPath, "worlds"), $providerManager); $this->worldManager = new WorldManager($this, Path::join($this->dataPath, "worlds"), $providerManager);
$this->worldManager->setAutoSave($this->configGroup->getConfigBool("auto-save", $this->worldManager->getAutoSave())); $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")); $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)){ if($this->configGroup->getPropertyBool("anonymous-statistics.enabled", true)){
$this->sendUsageTicker = 6000; $this->sendUsageTicker = self::TICKS_PER_STATS_REPORT;
$this->sendUsage(SendUsageTask::TYPE_OPEN); $this->sendUsage(SendUsageTask::TYPE_OPEN);
} }
@ -1826,18 +1833,18 @@ class Server{
} }
if($this->sendUsageTicker > 0 && --$this->sendUsageTicker === 0){ if($this->sendUsageTicker > 0 && --$this->sendUsageTicker === 0){
$this->sendUsageTicker = 6000; $this->sendUsageTicker = self::TICKS_PER_STATS_REPORT;
$this->sendUsage(SendUsageTask::TYPE_STATUS); $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){ foreach($this->worldManager->getWorlds() as $world){
$world->clearCache(); $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(); $this->getMemoryManager()->check();

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\timings; namespace pocketmine\timings;
use pocketmine\Server;
use function round; use function round;
use function spl_object_id; use function spl_object_id;
@ -54,8 +55,8 @@ final class TimingsRecord{
public static function tick(bool $measure = true) : void{ public static function tick(bool $measure = true) : void{
if($measure){ if($measure){
foreach(self::$records as $record){ foreach(self::$records as $record){
if($record->curTickTotal > 50000000){ if($record->curTickTotal > Server::TARGET_NANOSECONDS_PER_TICK){
$record->violations += (int) round($record->curTickTotal / 50000000); $record->violations += (int) round($record->curTickTotal / Server::TARGET_NANOSECONDS_PER_TICK);
} }
$record->curTickTotal = 0; $record->curTickTotal = 0;
$record->curCount = 0; $record->curCount = 0;

View File

@ -55,12 +55,14 @@ use function strval;
use function trim; use function trim;
class WorldManager{ class WorldManager{
public const TICKS_PER_AUTOSAVE = 300 * Server::TARGET_TICKS_PER_SECOND;
/** @var World[] */ /** @var World[] */
private array $worlds = []; private array $worlds = [];
private ?World $defaultWorld = null; private ?World $defaultWorld = null;
private bool $autoSave = true; private bool $autoSave = true;
private int $autoSaveTicks = 6000; private int $autoSaveTicks = self::TICKS_PER_AUTOSAVE;
private int $autoSaveTicker = 0; private int $autoSaveTicker = 0;
public function __construct( public function __construct(
@ -348,8 +350,8 @@ class WorldManager{
$world->doTick($currentTick); $world->doTick($currentTick);
$tickMs = (microtime(true) - $worldTime) * 1000; $tickMs = (microtime(true) - $worldTime) * 1000;
$world->tickRateTime = $tickMs; $world->tickRateTime = $tickMs;
if($tickMs >= 50){ if($tickMs >= Server::TARGET_SECONDS_PER_TICK){
$world->getLogger()->debug(sprintf("Tick took too long: %gms (%g ticks)", $tickMs, round($tickMs / 50, 2))); $world->getLogger()->debug(sprintf("Tick took too long: %gms (%g ticks)", $tickMs, round($tickMs / Server::TARGET_SECONDS_PER_TICK, 2)));
} }
} }