mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Added MemoryManager, new memory properties, improved performance, updated RakLib, fixed misc. bugs
This commit is contained in:
@ -47,7 +47,7 @@ class AsyncPool{
|
||||
for($i = 0; $i < $this->size; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
$this->workers[$i] = new AsyncWorker;
|
||||
$this->workers[$i]->setClassLoader($server->getLoader());
|
||||
$this->workers[$i]->setClassLoader($this->server->getLoader());
|
||||
$this->workers[$i]->start();
|
||||
}
|
||||
}
|
||||
@ -56,13 +56,41 @@ class AsyncPool{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
public function submitTask(AsyncTask $task){
|
||||
public function increaseSize($newSize){
|
||||
$newSize = (int) $newSize;
|
||||
if($newSize > $this->size){
|
||||
$this->size = $newSize;
|
||||
for($i = $this->size; $i < $newSize; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
$this->workers[$i] = new AsyncWorker;
|
||||
$this->workers[$i]->setClassLoader($this->server->getLoader());
|
||||
$this->workers[$i]->start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function submitTaskToWorker(AsyncTask $task, $worker){
|
||||
if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){
|
||||
return;
|
||||
}
|
||||
|
||||
$worker = (int) $worker;
|
||||
if($worker < 0 or $worker >= $this->size){
|
||||
throw new \InvalidArgumentException("Invalid worker $worker");
|
||||
}
|
||||
|
||||
$this->tasks[$task->getTaskId()] = $task;
|
||||
|
||||
$this->workers[$worker]->stack($task);
|
||||
$this->workerUsage[$worker]++;
|
||||
$this->taskWorkers[$task->getTaskId()] = $worker;
|
||||
}
|
||||
|
||||
public function submitTask(AsyncTask $task){
|
||||
if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedWorker = mt_rand(0, $this->size - 1);
|
||||
$selectedTasks = $this->workerUsage[$selectedWorker];
|
||||
for($i = 0; $i < $this->size; ++$i){
|
||||
@ -72,9 +100,7 @@ class AsyncPool{
|
||||
}
|
||||
}
|
||||
|
||||
$this->workers[$selectedWorker]->stack($task);
|
||||
$this->workerUsage[$selectedWorker]++;
|
||||
$this->taskWorkers[$task->getTaskId()] = $selectedWorker;
|
||||
$this->submitTaskToWorker($task, $selectedWorker);
|
||||
}
|
||||
|
||||
private function removeTask(AsyncTask $task){
|
||||
@ -85,6 +111,8 @@ class AsyncPool{
|
||||
|
||||
unset($this->tasks[$task->getTaskId()]);
|
||||
unset($this->taskWorkers[$task->getTaskId()]);
|
||||
|
||||
$task->cleanObject();
|
||||
}
|
||||
|
||||
public function removeTasks(){
|
||||
|
@ -131,4 +131,10 @@ abstract class AsyncTask extends \Collectable{
|
||||
|
||||
}
|
||||
|
||||
public function cleanObject(){
|
||||
foreach($this as $p => $v){
|
||||
$this->{$p} = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,9 +28,11 @@ class AsyncWorker extends Worker{
|
||||
public function run(){
|
||||
$this->registerClassLoader();
|
||||
gc_enable();
|
||||
ini_set("memory_limit", -1);
|
||||
|
||||
global $store;
|
||||
$store = [];
|
||||
|
||||
}
|
||||
|
||||
public function start($options = PTHREADS_INHERIT_NONE){
|
||||
|
30
src/pocketmine/scheduler/GarbageCollectionTask.php
Normal file
30
src/pocketmine/scheduler/GarbageCollectionTask.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
class GarbageCollectionTask extends AsyncTask{
|
||||
|
||||
public function onRun(){
|
||||
gc_enable();
|
||||
gc_collect_cycles();
|
||||
}
|
||||
}
|
@ -78,6 +78,28 @@ class ServerScheduler{
|
||||
$this->asyncPool->submitTask($task);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits an asynchronous task to a specific Worker in the Pool
|
||||
*
|
||||
* @param AsyncTask $task
|
||||
* @param int $worker
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function scheduleAsyncTaskToWorker(AsyncTask $task, $worker){
|
||||
$id = $this->nextId();
|
||||
$task->setTaskId($id);
|
||||
$this->asyncPool->submitTaskToWorker($task, $worker);
|
||||
}
|
||||
|
||||
public function getAsyncTaskPoolSize(){
|
||||
return $this->asyncPool->getSize();
|
||||
}
|
||||
|
||||
public function increaseAsyncTaskPoolSize($newSize){
|
||||
$this->asyncPool->increaseSize($newSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $delay
|
||||
|
Reference in New Issue
Block a user