-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from reliforp/per-php-binary-symbol-cache
Cache symbols per PHP binary
- Loading branch information
Showing
18 changed files
with
302 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Lib\Elf\Process; | ||
|
||
use Reli\Lib\Process\MemoryMap\ProcessModuleMemoryMap; | ||
|
||
final class BinaryFingerprint | ||
{ | ||
public function __construct( | ||
private string $fingerprint | ||
) { | ||
} | ||
|
||
public static function fromProcessModuleMemoryMap( | ||
ProcessModuleMemoryMap $process_module_memory_map | ||
): self { | ||
return new self( | ||
join( | ||
'_', | ||
[ | ||
$process_module_memory_map->getDeviceId(), | ||
$process_module_memory_map->getInodeNumber(), | ||
$process_module_memory_map->getModuleName(), | ||
] | ||
) | ||
); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->fingerprint; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Lib\Elf\Process; | ||
|
||
use Reli\Lib\Elf\Parser\ElfParserException; | ||
use Reli\Lib\Elf\Structure\Elf64\Elf64SymbolTableEntry; | ||
use Reli\Lib\Elf\SymbolResolver\Elf64SymbolResolver; | ||
use Reli\Lib\Elf\SymbolResolver\SymbolResolverCreatorInterface; | ||
use Reli\Lib\Process\MemoryMap\ProcessModuleMemoryMap; | ||
use Reli\Lib\Process\MemoryReader\MemoryReaderInterface; | ||
|
||
final class Elf64LazyParseSymbolResolver implements Elf64SymbolResolver | ||
{ | ||
private ?Elf64SymbolResolver $resolver_cache = null; | ||
|
||
public function __construct( | ||
private string $path, | ||
private MemoryReaderInterface $memory_reader, | ||
private int $pid, | ||
private ProcessModuleMemoryMap $module_memory_map, | ||
private SymbolResolverCreatorInterface $symbol_resolver_creator, | ||
) { | ||
} | ||
|
||
private function loadResolver(): Elf64SymbolResolver | ||
{ | ||
try { | ||
return $this->symbol_resolver_creator->createLinearScanResolverFromPath($this->path); | ||
} catch (ElfParserException $e) { | ||
try { | ||
return $this->symbol_resolver_creator->createDynamicResolverFromPath($this->path); | ||
} catch (ElfParserException $e) { | ||
return $this->symbol_resolver_creator->createDynamicResolverFromProcessMemory( | ||
$this->memory_reader, | ||
$this->pid, | ||
$this->module_memory_map | ||
); | ||
} | ||
} | ||
} | ||
|
||
public function resolve(string $symbol_name): Elf64SymbolTableEntry | ||
{ | ||
if (!isset($this->resolver_cache)) { | ||
$this->resolver_cache = $this->loadResolver(); | ||
} | ||
return $this->resolver_cache->resolve($symbol_name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Lib\Elf\Process; | ||
|
||
use Reli\Lib\Elf\SymbolResolver\Elf64SymbolCache; | ||
|
||
final class PerBinarySymbolCacheRetriever | ||
{ | ||
/** @var array<string, Elf64SymbolCache> */ | ||
private array $cache = []; | ||
|
||
public function get(BinaryFingerprint $binary_fingerprint): Elf64SymbolCache | ||
{ | ||
if (!isset($this->cache[(string)$binary_fingerprint])) { | ||
$this->cache[(string)$binary_fingerprint] = new Elf64SymbolCache(); | ||
} | ||
return $this->cache[(string)$binary_fingerprint]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Lib\Elf\SymbolResolver; | ||
|
||
use Reli\Lib\Elf\Structure\Elf64\Elf64SymbolTableEntry; | ||
|
||
final class Elf64CachedSymbolResolver implements Elf64SymbolResolver | ||
{ | ||
public function __construct( | ||
private Elf64SymbolResolver $resolver, | ||
private Elf64SymbolCache $symbol_cache, | ||
) { | ||
} | ||
|
||
public function resolve(string $symbol_name): Elf64SymbolTableEntry | ||
{ | ||
if (!$this->symbol_cache->has($symbol_name)) { | ||
$this->symbol_cache->set( | ||
$symbol_name, | ||
$this->resolver->resolve($symbol_name), | ||
); | ||
} | ||
return $this->symbol_cache->get($symbol_name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Lib\Elf\SymbolResolver; | ||
|
||
use Reli\Lib\Elf\Structure\Elf64\Elf64SymbolTableEntry; | ||
|
||
final class Elf64SymbolCache | ||
{ | ||
/** @var array<string, Elf64SymbolTableEntry> */ | ||
private array $cache = []; | ||
|
||
public function has(string $symbol_name): bool | ||
{ | ||
return isset($this->cache[$symbol_name]); | ||
} | ||
|
||
public function set(string $symbol_name, Elf64SymbolTableEntry $entry): void | ||
{ | ||
$this->cache[$symbol_name] = $entry; | ||
} | ||
|
||
public function get(string $symbol_name): Elf64SymbolTableEntry | ||
{ | ||
return $this->cache[$symbol_name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.