From ee2909204c15d7ecf70cb02271283d4da14accfd Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 00:24:50 +0900 Subject: [PATCH 01/11] Saving memories in operation processing --- composer.json | 3 ++- src/Core/JVM/Invoker/Invokable.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ee23d02d..509fc4b5 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php-java/java-util-package": "*", "php-java/java-io-package": "*", "php-java/java-net-package": "*", - "php-java/java-nio-package": "*" + "php-java/java-nio-package": "*", + "gabrielelana/byte-units": "dev-master" }, "autoload": { "psr-4": { diff --git a/src/Core/JVM/Invoker/Invokable.php b/src/Core/JVM/Invoker/Invokable.php index 070e17be..2b158698 100644 --- a/src/Core/JVM/Invoker/Invokable.php +++ b/src/Core/JVM/Invoker/Invokable.php @@ -1,6 +1,7 @@ debugTool->getLogger()->debug( + vsprintf( + 'Used Memory: %s, Used Memory Peak: %s', + [ + Metric::bytes(memory_get_usage())->format(), + Metric::bytes(memory_get_peak_usage())->format(), + ] + ) + ); + while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) { if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) { throw new RuntimeException( @@ -248,6 +259,16 @@ function ($item) { $debugTraces['mnemonic_indexes'][] = $pointer; } + $this->debugTool->getLogger()->debug( + vsprintf( + 'Used Memory: %s, Used Memory Peak: %s', + [ + Metric::bytes(memory_get_usage())->format(), + Metric::bytes(memory_get_peak_usage())->format(), + ] + ) + ); + $this->debugTool->getLogger()->debug( vsprintf( 'OpCode: 0x%02X, Mnemonic: %s, Stacks: %d, PC: %d', From bb636ac98327334ff92f90ce8302bf7765b52063 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 13:42:54 +0900 Subject: [PATCH 02/11] Disable trace --- src/Core/JVM/Cache/Cache.php | 15 +++++++++++++++ src/Core/JVM/Cache/OperationCache.php | 7 +++++++ src/Core/JVM/ConstantPool.php | 4 ++-- src/Core/JVM/Invoker/Invokable.php | 17 ++++++++++++++--- src/Core/JVM/Parameters/Runtime.php | 2 +- src/Core/JavaClass.php | 9 +++++++++ .../DebugTraceIsDisabledException.php | 6 ++++++ 7 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/Core/JVM/Cache/Cache.php create mode 100644 src/Core/JVM/Cache/OperationCache.php create mode 100644 src/Exceptions/DebugTraceIsDisabledException.php diff --git a/src/Core/JVM/Cache/Cache.php b/src/Core/JVM/Cache/Cache.php new file mode 100644 index 00000000..a9de8501 --- /dev/null +++ b/src/Core/JVM/Cache/Cache.php @@ -0,0 +1,15 @@ +items[$key])) { + return $this->items[$key]; + } + return $this->items[$key] = $pushFunction(); + } +} diff --git a/src/Core/JVM/Cache/OperationCache.php b/src/Core/JVM/Cache/OperationCache.php new file mode 100644 index 00000000..8c2468b8 --- /dev/null +++ b/src/Core/JVM/Cache/OperationCache.php @@ -0,0 +1,7 @@ +getOffset() < $codeAttribute->getOpCodeLength()) { if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) { throw new RuntimeException( @@ -295,6 +296,12 @@ function ($item) { /** * @var OperationInterface|Accumulator|ConstantPool $executor */ + $executor = $operationCache->fetchOrPush( + $fullName, + function () use ($fullName) { + return new $fullName(); + } + ); $executor = new $fullName(); $executor->setConstantPool($currentConstantPool); $executor->setParameters( @@ -319,13 +326,17 @@ function ($item) { } if ($returnValue !== null) { - $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); + if ($isEnabledTrace === true) { + $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); + } $this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified); return $returnValue; } } - $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); + if ($isEnabledTrace === true) { + $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); + } $this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified); return null; } diff --git a/src/Core/JVM/Parameters/Runtime.php b/src/Core/JVM/Parameters/Runtime.php index 9d1fa198..98b45679 100644 --- a/src/Core/JVM/Parameters/Runtime.php +++ b/src/Core/JVM/Parameters/Runtime.php @@ -13,7 +13,7 @@ final class Runtime const PRELOAD = false; const DRY_RUN_ATTRIBUTE = false; - const OPERATIONS_ENABLE_TRACE = true; + const OPERATIONS_ENABLE_TRACE = false; const OPERATIONS_TEMPORARY_CODE_STREAM = 'php://memory'; const VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY = false; diff --git a/src/Core/JavaClass.php b/src/Core/JavaClass.php index 66331157..93d46272 100644 --- a/src/Core/JavaClass.php +++ b/src/Core/JavaClass.php @@ -6,8 +6,11 @@ use PHPJava\Core\JVM\ActiveInterface; use PHPJava\Core\JVM\ActiveMethods; use PHPJava\Core\JVM\ConstantPool; +use PHPJava\Core\JVM\Parameters\GlobalOptions; +use PHPJava\Core\JVM\Parameters\Runtime; use PHPJava\Core\JVM\Validations\MagicByte; use PHPJava\Core\Stream\Reader\ReaderInterface; +use PHPJava\Exceptions\DebugTraceIsDisabledException; use PHPJava\Exceptions\ValidatorException; use PHPJava\Kernel\Attributes\AttributeInterface; use PHPJava\Kernel\Attributes\InnerClassesAttribute; @@ -302,6 +305,12 @@ public function getSuperClass() public function debug(): void { + $isEnabledTrace = $this->options['operations']['enable_trace'] ?? GlobalOptions::get('operations.enable_trace') ?? Runtime::OPERATIONS_ENABLE_TRACE; + if (!$isEnabledTrace) { + throw new DebugTraceIsDisabledException( + "Debug trace is disabled. If you want to show debug trace then enable to `enable_trace` option." + ); + } $cpInfo = $this->getConstantPool(); foreach ($this->debugTraces as $debugTraces) { printf("[method]\n"); diff --git a/src/Exceptions/DebugTraceIsDisabledException.php b/src/Exceptions/DebugTraceIsDisabledException.php new file mode 100644 index 00000000..5b7c59a0 --- /dev/null +++ b/src/Exceptions/DebugTraceIsDisabledException.php @@ -0,0 +1,6 @@ + Date: Sun, 28 Apr 2019 13:44:38 +0900 Subject: [PATCH 03/11] Fix refactoring missed --- src/Core/JVM/ConstantPool.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/JVM/ConstantPool.php b/src/Core/JVM/ConstantPool.php index 3290b734..fdf4d0a9 100644 --- a/src/Core/JVM/ConstantPool.php +++ b/src/Core/JVM/ConstantPool.php @@ -104,11 +104,11 @@ public function offsetGet($offset) public function offsetSet($offset, $value) { - throw new ReadOnlyException('You cannot rewrite datum. The Constant Cache is read-only.'); + throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.'); } public function offsetUnset($offset) { - throw new ReadOnlyException('You cannot rewrite datum. The Constant Cache is read-only.'); + throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.'); } } From 1f74cbe3bdd4313f75049ef0e57c102756eb2d78 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 14:45:48 +0900 Subject: [PATCH 04/11] Update --- src/Core/JVM/Cache/Cache.php | 10 +++++----- src/Core/JVM/Cache/HeapCache.php | 6 ++++++ src/Core/JVM/Invoker/Invokable.php | 30 +++++++++++++++--------------- src/Kernel/Core/Accumulator.php | 10 +++++----- src/Kernel/Types/Type.php | 1 + 5 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 src/Core/JVM/Cache/HeapCache.php diff --git a/src/Core/JVM/Cache/Cache.php b/src/Core/JVM/Cache/Cache.php index a9de8501..c25fde3a 100644 --- a/src/Core/JVM/Cache/Cache.php +++ b/src/Core/JVM/Cache/Cache.php @@ -3,13 +3,13 @@ class Cache { - private $items = []; + private static $items = []; - public function fetchOrPush(string $key, callable $pushFunction) + public static function fetchOrPush(string $key, callable $pushFunction, ...$parameters) { - if (isset($this->items[$key])) { - return $this->items[$key]; + if (isset(self::$items[$key])) { + return self::$items[$key]; } - return $this->items[$key] = $pushFunction(); + return self::$items[$key] = $pushFunction(...$parameters); } } diff --git a/src/Core/JVM/Cache/HeapCache.php b/src/Core/JVM/Cache/HeapCache.php new file mode 100644 index 00000000..626eaf8d --- /dev/null +++ b/src/Core/JVM/Cache/HeapCache.php @@ -0,0 +1,6 @@ +getOffset() < $codeAttribute->getOpCodeLength()) { if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) { throw new RuntimeException( @@ -296,23 +296,23 @@ function ($item) { /** * @var OperationInterface|Accumulator|ConstantPool $executor */ - $executor = $operationCache->fetchOrPush( + $executor = OperationCache::fetchOrPush( $fullName, - function () use ($fullName) { - return new $fullName(); + function () use ($fullName, &$currentConstantPool) { + return (new $fullName()) + ->setConstantPool($currentConstantPool); } ); - $executor = new $fullName(); - $executor->setConstantPool($currentConstantPool); - $executor->setParameters( - $method->getAttributes(), - $this->javaClassInvoker, - $reader, - $localStorage, - $stacks, - $pointer - ); - $returnValue = $executor->execute(); + $returnValue = $executor + ->setParameters( + $method->getAttributes(), + $this->javaClassInvoker, + $reader, + $localStorage, + $stacks, + $pointer + ) + ->execute(); $afterTrigger = $this->options['operations']['injections']['after'] ?? GlobalOptions::get('operations.injections.after'); if (is_callable($afterTrigger)) { diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index 648aaea5..eef331be 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -37,16 +37,16 @@ public function setParameters( \PHPJava\Core\JVM\Stream\BinaryReader $reader, array &$localStorage, array &$stacks, - int $pointer + int &$pointer ): self { - $this->attributes = $attributes; - $this->javaClassInvoker = $javaClassInvoker; + $this->attributes = &$attributes; + $this->javaClassInvoker = &$javaClassInvoker; $this->javaClass = $javaClassInvoker->getJavaClass(); $this->options = $this->javaClass->getOptions(); - $this->reader = $reader; + $this->reader = &$reader; $this->localStorage = &$localStorage; $this->stacks = &$stacks; - $this->pointer = $pointer; + $this->pointer = &$pointer; return $this; } diff --git a/src/Kernel/Types/Type.php b/src/Kernel/Types/Type.php index 13cf5974..4c5d5ef1 100644 --- a/src/Kernel/Types/Type.php +++ b/src/Kernel/Types/Type.php @@ -1,6 +1,7 @@ Date: Sun, 28 Apr 2019 14:48:54 +0900 Subject: [PATCH 05/11] Versioning --- composer.json | 2 +- src/Core/PHPJava.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 509fc4b5..48248e36 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "JVM emulator by PHP", "type": "library", "license": "MIT", - "version": "0.0.6.4-dev", + "version": "0.0.6.5-dev", "authors": [ { "name": "memory" diff --git a/src/Core/PHPJava.php b/src/Core/PHPJava.php index 4d42bf86..aad57825 100644 --- a/src/Core/PHPJava.php +++ b/src/Core/PHPJava.php @@ -12,5 +12,5 @@ final class PHPJava /** * As same as composer version. */ - const VERSION = 0x000064; + const VERSION = 0x000065; } From 0d81009f29a6e68d6f48e0ec741e6fcf20fa6b8b Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 14:51:13 +0900 Subject: [PATCH 06/11] Update tests --- src/Core/JVM/Parameters/GlobalOptions.php | 2 +- tests/OutputDebugTraceTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Core/JVM/Parameters/GlobalOptions.php b/src/Core/JVM/Parameters/GlobalOptions.php index bcd7cc2a..92860f3d 100644 --- a/src/Core/JVM/Parameters/GlobalOptions.php +++ b/src/Core/JVM/Parameters/GlobalOptions.php @@ -25,7 +25,7 @@ public static function get($name) return $ref; } - public function reset() + public static function reset() { static::$settings = []; } diff --git a/tests/OutputDebugTraceTest.php b/tests/OutputDebugTraceTest.php index 02ebf1c1..911824e6 100644 --- a/tests/OutputDebugTraceTest.php +++ b/tests/OutputDebugTraceTest.php @@ -1,6 +1,7 @@ [ + 'enable_trace' => true, + ], + ]); + $calculatedValue = $this->initiatedJavaClasses['OutputDebugTraceTest'] ->getInvoker() ->getStatic() @@ -30,5 +37,7 @@ public function testCallMain() file_get_contents(__DIR__ . '/templates/DebugTraceTest.txt'), $result ); + + GlobalOptions::reset(); } } From f41a975c9a0e66cfe513f2d4fb0e22ed4184089c Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 15:06:42 +0900 Subject: [PATCH 07/11] Fix constant pool --- src/Core/JVM/Invoker/Invokable.php | 6 +++--- src/Kernel/Core/Accumulator.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Core/JVM/Invoker/Invokable.php b/src/Core/JVM/Invoker/Invokable.php index 42c882ac..608a605e 100644 --- a/src/Core/JVM/Invoker/Invokable.php +++ b/src/Core/JVM/Invoker/Invokable.php @@ -298,12 +298,12 @@ function ($item) { */ $executor = OperationCache::fetchOrPush( $fullName, - function () use ($fullName, &$currentConstantPool) { - return (new $fullName()) - ->setConstantPool($currentConstantPool); + function () use ($fullName) { + return new $fullName(); } ); $returnValue = $executor + ->setConstantPool($currentConstantPool) ->setParameters( $method->getAttributes(), $this->javaClassInvoker, diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index eef331be..0e4f4dc1 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -37,7 +37,7 @@ public function setParameters( \PHPJava\Core\JVM\Stream\BinaryReader $reader, array &$localStorage, array &$stacks, - int &$pointer + int $pointer ): self { $this->attributes = &$attributes; $this->javaClassInvoker = &$javaClassInvoker; @@ -46,7 +46,7 @@ public function setParameters( $this->reader = &$reader; $this->localStorage = &$localStorage; $this->stacks = &$stacks; - $this->pointer = &$pointer; + $this->pointer = $pointer; return $this; } From e3894bf44fbbe6bf5d4e17218977881294b16857 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 15:11:56 +0900 Subject: [PATCH 08/11] Fix --- src/Kernel/Core/Accumulator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index 0e4f4dc1..2dbdc213 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -43,7 +43,7 @@ public function setParameters( $this->javaClassInvoker = &$javaClassInvoker; $this->javaClass = $javaClassInvoker->getJavaClass(); $this->options = $this->javaClass->getOptions(); - $this->reader = &$reader; + $this->reader = $reader; $this->localStorage = &$localStorage; $this->stacks = &$stacks; $this->pointer = $pointer; From 49dddc1505196ff38472ba87cf81eb9237dd76f4 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 17:00:19 +0900 Subject: [PATCH 09/11] Fix --- src/Core/JVM/Invoker/Invokable.php | 4 ++-- src/Kernel/Core/Accumulator.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core/JVM/Invoker/Invokable.php b/src/Core/JVM/Invoker/Invokable.php index 608a605e..9c366a5b 100644 --- a/src/Core/JVM/Invoker/Invokable.php +++ b/src/Core/JVM/Invoker/Invokable.php @@ -326,7 +326,7 @@ function () use ($fullName) { } if ($returnValue !== null) { - if ($isEnabledTrace === true) { + if ($isEnabledTrace) { $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); } $this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified); @@ -334,7 +334,7 @@ function () use ($fullName) { } } - if ($isEnabledTrace === true) { + if ($isEnabledTrace) { $this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces); } $this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified); diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index 2dbdc213..648aaea5 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -39,8 +39,8 @@ public function setParameters( array &$stacks, int $pointer ): self { - $this->attributes = &$attributes; - $this->javaClassInvoker = &$javaClassInvoker; + $this->attributes = $attributes; + $this->javaClassInvoker = $javaClassInvoker; $this->javaClass = $javaClassInvoker->getJavaClass(); $this->options = $this->javaClass->getOptions(); $this->reader = $reader; From 9b91d4443a94645635779df08258b09537c7d07b Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 17:11:47 +0900 Subject: [PATCH 10/11] Update --- src/Core/JVM/Cache/Cache.php | 10 +++++----- src/Core/JVM/Invoker/Invokable.php | 3 ++- src/Kernel/Core/Accumulator.php | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Core/JVM/Cache/Cache.php b/src/Core/JVM/Cache/Cache.php index c25fde3a..73cbbea0 100644 --- a/src/Core/JVM/Cache/Cache.php +++ b/src/Core/JVM/Cache/Cache.php @@ -3,13 +3,13 @@ class Cache { - private static $items = []; + private $items = []; - public static function fetchOrPush(string $key, callable $pushFunction, ...$parameters) + public function fetchOrPush(string $key, callable $pushFunction, ...$parameters) { - if (isset(self::$items[$key])) { - return self::$items[$key]; + if (isset($this->items[$key])) { + return $this->items[$key]; } - return self::$items[$key] = $pushFunction(...$parameters); + return $this->items[$key] = $pushFunction(...$parameters); } } diff --git a/src/Core/JVM/Invoker/Invokable.php b/src/Core/JVM/Invoker/Invokable.php index 9c366a5b..09fb1d17 100644 --- a/src/Core/JVM/Invoker/Invokable.php +++ b/src/Core/JVM/Invoker/Invokable.php @@ -230,6 +230,7 @@ function ($item) { ) ); + $operationCache = new OperationCache(); while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) { if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) { throw new RuntimeException( @@ -296,7 +297,7 @@ function ($item) { /** * @var OperationInterface|Accumulator|ConstantPool $executor */ - $executor = OperationCache::fetchOrPush( + $executor = $operationCache->fetchOrPush( $fullName, function () use ($fullName) { return new $fullName(); diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index 648aaea5..9dab9f54 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -33,8 +33,8 @@ trait Accumulator public function setParameters( array $attributes, - JavaClassInvoker $javaClassInvoker, - \PHPJava\Core\JVM\Stream\BinaryReader $reader, + JavaClassInvoker &$javaClassInvoker, + \PHPJava\Core\JVM\Stream\BinaryReader &$reader, array &$localStorage, array &$stacks, int $pointer From d418be55311587e1fbae9588a329008402dec953 Mon Sep 17 00:00:00 2001 From: memory-agape Date: Sun, 28 Apr 2019 17:15:52 +0900 Subject: [PATCH 11/11] Fix --- src/Kernel/Core/Accumulator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kernel/Core/Accumulator.php b/src/Kernel/Core/Accumulator.php index 9dab9f54..648aaea5 100644 --- a/src/Kernel/Core/Accumulator.php +++ b/src/Kernel/Core/Accumulator.php @@ -33,8 +33,8 @@ trait Accumulator public function setParameters( array $attributes, - JavaClassInvoker &$javaClassInvoker, - \PHPJava\Core\JVM\Stream\BinaryReader &$reader, + JavaClassInvoker $javaClassInvoker, + \PHPJava\Core\JVM\Stream\BinaryReader $reader, array &$localStorage, array &$stacks, int $pointer