From 33e312c7d05909ef271c2c3122759317e7f36507 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 13 Jun 2015 15:37:02 +0200 Subject: [PATCH] Added extra memory reportings (heap, stack), stop tasks faster, added extra timeouts fro ServerKiller --- src/pocketmine/PocketMine.php | 2 +- src/pocketmine/Server.php | 10 ++++++++- .../command/defaults/StatusCommand.php | 5 ++++- src/pocketmine/level/Level.php | 2 +- src/pocketmine/scheduler/AsyncPool.php | 5 ++++- src/pocketmine/scheduler/AsyncTask.php | 13 ++++++++++- src/pocketmine/scheduler/SendUsageTask.php | 2 +- src/pocketmine/utils/ServerKiller.php | 8 ++++++- src/pocketmine/utils/Utils.php | 22 +++++++++++++++++++ 9 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index d31211328..2e260c0ee 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -470,7 +470,7 @@ namespace pocketmine { $thread->quit(); } - $killer = new ServerKiller(); + $killer = new ServerKiller(8); $killer->start(); $killer->detach(); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 2c309db85..e1a23d5f8 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -106,6 +106,7 @@ use pocketmine\utils\Config; use pocketmine\utils\LevelException; use pocketmine\utils\MainLogger; use pocketmine\utils\ServerException; +use pocketmine\utils\ServerKiller; use pocketmine\utils\Terminal; use pocketmine\utils\TextFormat; use pocketmine\utils\TextWrapper; @@ -2069,6 +2070,11 @@ class Server{ * Shutdowns the server correctly */ public function shutdown(){ + if($this->isRunning){ + $killer = new ServerKiller(90); + $killer->start(); + $killer->detach(); + } $this->isRunning = false; } @@ -2423,9 +2429,11 @@ class Server{ if(!Terminal::hasFormattingCodes()){ return; } + + $d = Utils::getRealMemoryUsage(); $u = Utils::getMemoryUsage(true); - $usage = round(($u[0] / 1024) / 1024, 2) . "/" . round(($u[1] / 1024) / 1024, 2) . "/".round(($u[2] / 1024) / 1024, 2)." MB @ " . Utils::getThreadCount() . " threads"; + $usage = round(($u[0] / 1024) / 1024, 2) . "/" . round(($d[0] / 1024) / 1024, 2) . "/" . round(($u[1] / 1024) / 1024, 2) . "/".round(($u[2] / 1024) / 1024, 2)." MB @ " . Utils::getThreadCount() . " threads"; echo "\x1b]0;" . $this->getName() . " " . $this->getPocketMineVersion() . diff --git a/src/pocketmine/command/defaults/StatusCommand.php b/src/pocketmine/command/defaults/StatusCommand.php index 91bf2d1f0..393f6a2db 100644 --- a/src/pocketmine/command/defaults/StatusCommand.php +++ b/src/pocketmine/command/defaults/StatusCommand.php @@ -41,6 +41,7 @@ class StatusCommand extends VanillaCommand{ return true; } + $rUsage = Utils::getRealMemoryUsage(true); $mUsage = Utils::getMemoryUsage(true); $server = $sender->getServer(); @@ -88,7 +89,9 @@ class StatusCommand extends VanillaCommand{ $sender->sendMessage(TextFormat::GOLD . "Thread count: " . TextFormat::RED . Utils::getThreadCount()); $sender->sendMessage(TextFormat::GOLD . "Main thread memory: " . TextFormat::RED . number_format(round(($mUsage[0] / 1024) / 1024, 2)) . " MB."); - $sender->sendMessage(TextFormat::GOLD . "Allocated memory: " . TextFormat::RED . number_format(round(($mUsage[1] / 1024) / 1024, 2)) . " MB."); + $sender->sendMessage(TextFormat::GOLD . "Total memory: " . TextFormat::RED . number_format(round(($mUsage[1] / 1024) / 1024, 2)) . " MB."); + $sender->sendMessage(TextFormat::GOLD . "Total virtual memory: " . TextFormat::RED . number_format(round(($mUsage[2] / 1024) / 1024, 2)) . " MB."); + $sender->sendMessage(TextFormat::GOLD . "Heap memory: " . TextFormat::RED . number_format(round(($rUsage[0] / 1024) / 1024, 2)) . " MB."); $sender->sendMessage(TextFormat::GOLD . "Maximum memory (system): " . TextFormat::RED . number_format(round(($mUsage[2] / 1024) / 1024, 2)) . " MB."); if($server->getProperty("memory.global-limit") > 0){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 5631d52d8..bc236432e 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -2544,7 +2544,7 @@ class Level implements ChunkManager, Metadatable{ ++$y; } } - + $v->y = $y; } diff --git a/src/pocketmine/scheduler/AsyncPool.php b/src/pocketmine/scheduler/AsyncPool.php index 20b3e3216..eded14c0d 100644 --- a/src/pocketmine/scheduler/AsyncPool.php +++ b/src/pocketmine/scheduler/AsyncPool.php @@ -122,6 +122,7 @@ class AsyncPool{ public function removeTasks(){ do{ foreach($this->tasks as $task){ + $task->cancelRun(); $this->removeTask($task); } @@ -144,7 +145,9 @@ class AsyncPool{ foreach($this->tasks as $task){ if($task->isGarbage() and !$task->isRunning()){ - $task->onCompletion($this->server); + if(!$task->hasCancelledRun()){ + $task->onCompletion($this->server); + } $this->removeTask($task); }elseif($task->isTerminated()){ diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index b54d43485..48bcb3bc4 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -35,13 +35,16 @@ abstract class AsyncTask extends \Collectable{ private $result = null; private $serialized = false; + private $cancelRun = false; /** @var int */ private $taskId = null; public function run(){ $this->result = null; - $this->onRun(); + if($this->cancelRun !== true){ + $this->onRun(); + } $this->setGarbage(); } @@ -62,6 +65,14 @@ abstract class AsyncTask extends \Collectable{ return $this->serialized ? unserialize($this->result) : $this->result; } + public function cancelRun(){ + $this->cancelRun = true; + } + + public function hasCancelledRun(){ + return $this->cancelRun === true; + } + /** * @return bool */ diff --git a/src/pocketmine/scheduler/SendUsageTask.php b/src/pocketmine/scheduler/SendUsageTask.php index ded254b19..d303872aa 100644 --- a/src/pocketmine/scheduler/SendUsageTask.php +++ b/src/pocketmine/scheduler/SendUsageTask.php @@ -139,7 +139,7 @@ class SendUsageTask extends AsyncTask{ public function onRun(){ try{ - Utils::postURL($this->endpoint, $this->data, 10, [ + Utils::postURL($this->endpoint, $this->data, 5, [ "Content-Type: application/json", "Content-Length: ". strlen($this->data) ]); diff --git a/src/pocketmine/utils/ServerKiller.php b/src/pocketmine/utils/ServerKiller.php index cc2a8babb..d5c710f07 100644 --- a/src/pocketmine/utils/ServerKiller.php +++ b/src/pocketmine/utils/ServerKiller.php @@ -25,8 +25,14 @@ use pocketmine\Thread; class ServerKiller extends Thread{ + public $time; + + public function __construct($time = 15){ + $this->time = $time; + } + public function run(){ - sleep(15); + sleep($this->time); echo "\nTook to long to stop, server was killed forcefully!\n"; @\pocketmine\kill(getmypid()); } diff --git a/src/pocketmine/utils/Utils.php b/src/pocketmine/utils/Utils.php index fce99403d..91cf1ca37 100644 --- a/src/pocketmine/utils/Utils.php +++ b/src/pocketmine/utils/Utils.php @@ -216,6 +216,26 @@ class Utils{ } + public static function getRealMemoryUsage(){ + $stack = 0; + $heap = 0; + + if(Utils::getOS() === "linux" or Utils::getOS() === "android"){ + $mappings = file("/proc/self/maps"); + foreach($mappings as $line){ + if(preg_match("#([a-z0-9]+)\\-([a-z0-9]+) [rwxp\\-]{4} [a-z0-9]+ [^\\[]*\\[([a-zA-z0-9]+)\\]#", trim($line), $matches) > 0){ + if(strpos($matches[3], "heap") === 0){ + $heap += hexdec($matches[2]) - hexdec($matches[1]); + }elseif(strpos($matches[3], "stack") === 0){ + $stack += hexdec($matches[2]) - hexdec($matches[1]); + } + } + } + } + + return [$heap, $stack]; + } + public static function getMemoryUsage($advanced = false){ $reserved = memory_get_usage(); $VmSize = null; @@ -482,6 +502,7 @@ class Utils{ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); $ret = curl_exec($ch); curl_close($ch); @@ -515,6 +536,7 @@ class Utils{ curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"], $extraHeaders)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); $ret = curl_exec($ch); curl_close($ch);