Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Add new profling options for Facebook/hhvm #28

Merged
merged 1 commit into from
Apr 21, 2015
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
153 changes: 148 additions & 5 deletions base/HHVMDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,104 @@ protected function getArguments(): Vector<string> {
'-v', 'Eval.Jit=1',
'-v', 'AdminServer.Port='.PerfSettings::FastCGIAdminPort(),
'-c', OSS_PERFORMANCE_ROOT.'/conf/php.ini',
'-v', 'Eval.PCRECacheType=' . $this->options->pcreCache,
'-v', 'Eval.PCRETableSize=' . $this->options->pcreSize,
'-v', 'Eval.PCREExpireInterval=' . $this->options->pcreExpire,
};
if (count($this->options->hhvmExtraArguments) > 0) {
$args->addAll($this->options->hhvmExtraArguments);
}
if ($this->options->precompile) {
$bcRepo = $this->options->tempDir . '/hhvm.hhbc';
$args->add('-v'); $args->add('Repo.Authoritative=true');
$args->add('-v'); $args->add('Repo.Central.Path=' . $bcRepo);
}
if ($this->options->filecache) {
$sourceRoot = $this->getTarget()->getSourceRoot();
$staticContent = $this->options->tempDir . '/static.content';
$args->add('-v'); $args->add('Server.FileCache=' . $staticContent);
$args->add('-v'); $args->add('Server.SourceRoot=' . $sourceRoot);
}
if ($this->options->tcprint !== null) {
$args->add('-v'); $args->add('Eval.JitTransCounters=true');
$args->add('-v'); $args->add('Eval.DumpTC=true');
}
if ($this->options->profBC) {
$args->add('-v'); $args->add('Eval.ProfileBC=true');
}
if ($this->options->interpPseudomains) {
$args->add('-v'); $args->add('Eval.JitPseudomain=false');
}
if ($this->options->allVolatile) {
$args->add('-v'); $args->add('Eval.AllVolatile=true');
}
return $args;
}

<<__Override>>
public function start(): void {
if ($this->options->precompile) {
$sourceRoot = $this->getTarget()->getSourceRoot();
$fileList = $this->options->tempDir . '/files.txt';
$hhvm = $this->options->hhvm;
invariant(!is_null($hhvm), "Must have hhvm path");
$args = Vector {
$hhvm,
'--hphp',
'--input-list', $fileList,
'--target', 'hhbc',
'--output-dir', $this->options->tempDir,
'--input-dir', $sourceRoot,
'--module', $sourceRoot,
'-l3',
'-k1',
};

if ($this->options->allVolatile) {
$args->add('-v'); $args->add('AllVolatile=true');
}

invariant(is_dir($sourceRoot), 'Could not find valid source root');

$files = Vector{};
$dir_iter = new RecursiveDirectoryIterator($sourceRoot);
$iter = new RecursiveIteratorIterator($dir_iter);
foreach ($iter as $path => $_) {
if (is_file($path)) {
$files->add(ltrim(substr($path, strlen($sourceRoot)), '/'));
}
}
file_put_contents($fileList, implode("\n", $files));

$bcRepo = $this->options->tempDir . '/hhvm.hhbc';
$staticContent = $this->options->tempDir . '/static.content';
if (file_exists($bcRepo)) {
unlink($bcRepo);
}

if ($this->options->filecache) {
if (file_exists($staticContent)) {
unlink($staticContent);
}
$args->add('--file-cache');
$args->add($this->options->tempDir . '/static.content');
}

Utils::RunCommand($args);

invariant(file_exists($bcRepo), 'Failed to create bytecode repo');
invariant(
!$this->options->filecache || file_exists($staticContent),
'Failed to create static content cache'
);
}

if ($this->options->pcredump) {
if (file_exists('/tmp/pcre_cache')) {
unlink('/tmp/pcre_cache');
}
}

parent::startWorker(
$this->options->daemonOutputFileName('hhvm'),
$this->options->delayProcessLaunch,
Expand All @@ -92,13 +181,14 @@ public function start(): void {
continue;
}
$health = json_decode($health, /* assoc array = */ true);
if (array_key_exists('tc-size', $health) && $health['tc-size'] > 0) {
if (array_key_exists('tc-size', $health) &&
($health['tc-size'] > 0 || $health['tc-hotsize'] > 0)) {
return;
}
}
$this->stop();
return;
}
// Whoops...
$this->stop();
}

public function stop(): void {
Expand All @@ -108,8 +198,61 @@ public function stop(): void {
$this->adminRequest('/stop');
invariant(!$this->isRunning(), 'Failed to stop HHVM');
}
} catch (Exception $e) {
parent::stop();
} catch (Exception $e) { }

parent::stop();
}

public function writeStats(): void {
$tcprint = $this->options->tcprint;
if ($tcprint) {
$conf = $this->options->tempDir . '/conf.hdf';
$args = Vector {};
$hdf = false;
foreach ($this->getArguments() as $arg) {
if ($hdf) $args->add($arg);
$hdf = $arg === '-v';
}
$confData = implode("\n", $args);

file_put_contents($conf, $confData);
$args = Vector { $tcprint, '-c', $conf };

$result = $this->adminRequest('/vm-dump-tc');
invariant(
$result === 'Done' && file_exists('/tmp/tc_dump_a'),
'Failed to dump TC'
);

if ($this->options->tcAlltrans) {
$alltrans = Utils::RunCommand($args);
file_put_contents('tc-all', $alltrans);
}

if ($this->options->tcToptrans) {
$new_args = new Vector($args);
$new_args->add('-t'); $new_args->add('100');
$toptrans = Utils::RunCommand($new_args);
file_put_contents('tc-top-trans', $toptrans);
}

if ($this->options->tcTopfuncs) {
$new_args = new Vector($args);
$new_args->add('-T'); $new_args->add('100');
$topfuncs = Utils::RunCommand($new_args);
file_put_contents('tc-top-funcs', $topfuncs);
}
}

if ($this->options->pcredump) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check /tmp/pcre_cache does not exist first?

$result = $this->adminRequest('/dump-pcre-cache');
invariant(
$result === "OK\n" && file_exists('/tmp/pcre_cache'),
'Failed to dump PCRE cache'
);

// move dump to CWD
rename('/tmp/pcre_cache', getcwd() . '/pcre_cache');
}
}

Expand Down
3 changes: 2 additions & 1 deletion base/NginxDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function getArguments(): Vector<string> {
// Watch out! The -g arguments to nginx do not accumulate.
// The last one wins, and is the only one evaluated by nginx.
//
'-g', 'daemon off; '.'pid '.$this->getPidFilePath().'; ',
'-g', 'daemon off;',
};
}

Expand Down Expand Up @@ -148,6 +148,7 @@ protected function getGeneratedConfigFile(): string {
'__NGINX_FASTCGI_READ_TIMEOUT__' =>
(int)$this->options->maxdelayNginxFastCGI,
'__FRAMEWORK_ROOT__' => $this->target->getSourceRoot(),
'__NGINX_PID_FILE__' => $this->getPidFilePath(),
};

$config = file_get_contents(
Expand Down
1 change: 1 addition & 0 deletions base/PHPEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

abstract class PHPEngine extends Process {
public abstract function __toString(): string;
public function writeStats(): void {}
}
133 changes: 132 additions & 1 deletion base/PerfOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ final class PerfOptions {
public bool $dumpIsCompressed = true;
public bool $traceSubProcess = false;
public bool $noTimeLimit = false;


// Pause once benchmarking is complete to allow for manual inspection of the
// HHVM or PHP process.
public bool $waitAtEnd = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for adding this!


//
// HHVM specific options to enable RepoAuthoritative mode and the static
// content cache, as well as selecting the PCRE caching mode.
//
public bool $precompile = false;
public bool $filecache = false;
public string $pcreCache = "static";
public int $pcreSize = 98304;
public int $pcreExpire = 7200;
public bool $allVolatile = false;
public bool $interpPseudomains = false;

//
// HHVM specific options for generating performance data and profiling
// information.
//
public ?string $tcprint = null;
public bool $tcAlltrans = false;
public bool $tcToptrans = false;
public bool $tcTopfuncs = false;
public bool $pcredump = false;
public bool $profBC = false;

//
// All times are given in seconds, stored in a float.
// For PHP code, the usleep timer is used, so fractional seconds work fine.
Expand Down Expand Up @@ -78,6 +105,25 @@ public function __construct(Vector<string> $argv) {
'siege:',
'nginx:',

'wait-at-end',

'repo-auth',
'file-cache',
'pcre-cache:',
'pcre-cache-expire:',
'pcre-cache-size:',
'all-volatile',
'interp-pseudomains',

'fbcode::',

'tcprint::',
'dump-top-trans',
'dump-top-funcs',
'dump-all-trans',
'dump-pcre-cache',
'profBC',

'i-am-not-benchmarking',

'hhvm-extra-arguments:',
Expand Down Expand Up @@ -134,6 +180,50 @@ public function __construct(Vector<string> $argv) {
$this->siege = hphp_array_idx($o, 'siege', 'siege');
$this->nginx = hphp_array_idx($o, 'nginx', 'nginx');

$isFacebook = array_key_exists('fbcode', $o);
$fbcode = "";
if ($isFacebook) {
$val = hphp_array_idx($o, 'fbcode', false);
if (is_string($val) && $val !== '') {
$fbcode = $val;
} else {
$fbcode = getenv('HOME') . '/fbcode';
}
}
$this->waitAtEnd = array_key_exists('wait-at-end', $o);

$this->precompile = array_key_exists('repo-auth', $o);
$this->filecache = array_key_exists('file-cache', $o);
$this->pcreCache = (string)hphp_array_idx($o, 'pcre-cache', 'static');
$this->pcreSize = (int)hphp_array_idx($o, 'pcre-cache-size', 98304);
$this->pcreExpire = (int)hphp_array_idx($o, 'pcre-cache-expire', 7200);
$this->allVolatile = array_key_exists('all-volatile', $o);
$this->interpPseudomains = array_key_exists('interp-pseudomains', $o);

if (array_key_exists('tcprint', $o)) {
$tcprint = hphp_array_idx($o, 'tcprint', null);
if (is_string($tcprint) && $tcprint !== '') {
$this->tcprint = $tcprint;
} else if ($isFacebook) {
$this->tcprint =
$fbcode . '/_bin/hphp/facebook/tools/tc-print/tc-print';
}
}
$this->tcAlltrans = array_key_exists('dump-all-trans', $o);
$this->tcToptrans = array_key_exists('dump-top-trans', $o);
$this->tcTopfuncs = array_key_exists('dump-top-funcs', $o);
$this->pcredump = array_key_exists('dump-pcre-cache', $o);
$this->profBC = array_key_exists('profBC', $o);

if ($this->tcprint !== null &&
!$this->tcTopfuncs && !$this->tcToptrans) {
$this->tcAlltrans = true;
}

if ($isFacebook && $this->php5 === null && $this->hhvm === null) {
$this->hhvm = $fbcode . '/_bin/hphp/hhvm/hhvm';
}

$this->traceSubProcess = array_key_exists('trace', $o);

$this->notBenchmarking = array_key_exists('i-am-not-benchmarking', $o);
Expand Down Expand Up @@ -208,6 +298,47 @@ public function validate() {
'Invalid engine: %s',
$engine
);

$tcprint = $this->tcprint;
if ($tcprint !== null) {
invariant(
$tcprint !== '' &&
(shell_exec('which '.escapeshellarg($tcprint)) !== null
|| is_executable($tcprint)),
'Invalid tcprint: %s',
$tcprint
);
}

if ($this->tcAlltrans || $this->tcToptrans || $this->tcTopfuncs) {
invariant(
$tcprint !== null,
'--tcprint=/path/to/tc-print must be specified if --tc-all-trans, ' .
'--tc-top-trans, or --tc-top-funcs are specified'
);
}

if ($this->filecache) {
invariant(
$this->precompile,
'The file cache must be used with --repo-auth'
);
}

if ($this->pcreCache !== null || $this->pcreSize || $this->pcreExpire) {
invariant(
$this->hhvm !== null,
'The PCRE caching scheme can only be tuned for hhvm'
);
}

if ($this->precompile) {
invariant(
$this->hhvm !== null,
'Only hhvm can be used with --repo-auth'
);
}

SystemChecks::CheckAll($this);

// Validates that one was defined
Expand Down
Loading