Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions ext/standard/VmMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,24 @@ private static function readEmallocApprox(): int
return $rss;
}

/**
* RSS via /proc/self/statm only (issue #7287, #4862).
*
* JIT/AOT use the same source in MemoryRuntime::__phpc_memory_read_rss_bytes.
*/
private static function readRssBytes(): int
{
if ('Linux' === \PHP_OS_FAMILY && is_readable('/proc/self/statm')) {
$statm = @file_get_contents('/proc/self/statm');
if (false !== $statm && '' !== $statm) {
$parts = preg_split('/\s+/', trim($statm));
$rssPages = (int) ($parts[1] ?? 0);

return $rssPages * self::pageSize();
}
if ('Linux' !== \PHP_OS_FAMILY || !is_readable('/proc/self/statm')) {
return 0;
}

if (\function_exists('getrusage')) {
$ru = getrusage();
if (\is_array($ru) && isset($ru['ru_maxrss'])) {
// Linux: kilobytes; BSD often bytes — treat as KiB (php-src ZEND_SYS_VMEM).
return (int) $ru['ru_maxrss'] * 1024;
}
$statm = @file_get_contents('/proc/self/statm');
if (false === $statm || '' === $statm) {
return 0;
}
$parts = preg_split('/\s+/', trim($statm));
$rssPages = (int) ($parts[1] ?? 0);

return 0;
return $rssPages * self::pageSize();
}

private static function pageSize(): int
Expand Down
29 changes: 29 additions & 0 deletions test/unit/VmMemoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\Test\Unit;

use PHPCompiler\ext\standard\VmMemory;
use PHPUnit\Framework\TestCase;

/** VmMemory RSS path without host getrusage() delegation (issue #7287, #4862). */
final class VmMemoryTest extends TestCase
{
public function testSourceDoesNotDelegateToHostGetrusage(): void
{
$source = (string) file_get_contents(__DIR__.'/../../ext/standard/VmMemory.php');
$this->assertDoesNotMatchRegularExpression('/function_exists\\s*\\(\\s*[\'"]getrusage/', $source);
$this->assertDoesNotMatchRegularExpression('/\\\\getrusage\\s*\\(/', $source);
$this->assertStringContainsString('/proc/self/statm', $source);
}

public function testRealUsageReadsProcStatmOnLinux(): void
{
if ('Linux' !== \PHP_OS_FAMILY || !is_readable('/proc/self/statm')) {
$this->markTestSkipped('Linux /proc/self/statm required for RSS probe');
}

$this->assertGreaterThan(0, VmMemory::getUsage(true));
}
}