From 29961c68646f4e49027ad2f291c3243811d1f2d4 Mon Sep 17 00:00:00 2001 From: Michael O'Farrell Date: Mon, 6 Mar 2017 13:31:49 -0800 Subject: [PATCH] Add processor affinity setting "--cpu-fraction". --- base/DatabaseInstaller.php | 12 +++++++++++- base/HHVMDaemon.php | 3 +++ base/NginxDaemon.php | 3 +++ base/PHP5Daemon.php | 3 +++ base/PerfOptions.php | 16 ++++++++++++++++ base/Process.php | 6 ++++++ base/Siege.php | 3 +++ batch-run.json.example | 6 +++++- batch-run.php | 7 +++++++ scripts/setup.sh | 2 +- 10 files changed, 58 insertions(+), 3 deletions(-) diff --git a/base/DatabaseInstaller.php b/base/DatabaseInstaller.php index 3271d33..3cb21b0 100644 --- a/base/DatabaseInstaller.php +++ b/base/DatabaseInstaller.php @@ -15,7 +15,9 @@ final class DatabaseInstaller { private ?string $username; private ?string $password = null; - public function __construct(private PerfOptions $options): void {} + public function __construct(private PerfOptions $options): void { + $this->configureMysqlAffinity(); + } public function getUsername(): ?string { return $this->username ? $this->username : $this->databaseName; @@ -35,6 +37,14 @@ public function setDumpFile(string $dump_file): this { return $this; } + public function configureMysqlAffinity(): void { + if ($this->options->cpuBind) { + exec("sudo taskset -acp ".$this->options->helperProcessors." `pgrep mysqld`"); + print "You need to restart mysql after the benchmarks to remove the "; + print "processor affinity.\n"; + } + } + public function installDatabase(): bool { $db = $this->databaseName; $dump = $this->dumpFile; diff --git a/base/HHVMDaemon.php b/base/HHVMDaemon.php index ec539ba..7dde3e7 100644 --- a/base/HHVMDaemon.php +++ b/base/HHVMDaemon.php @@ -64,6 +64,9 @@ protected function getTarget(): PerfTarget { <<__Override>> protected function getArguments(): Vector { + if ($this->options->cpuBind) { + $this->cpuRange = $this->options->daemonProcessors; + } $args = Vector { '-m', 'server', diff --git a/base/NginxDaemon.php b/base/NginxDaemon.php index 5b0ee87..5980276 100644 --- a/base/NginxDaemon.php +++ b/base/NginxDaemon.php @@ -110,6 +110,9 @@ protected function getPidFilePath(): string { <<__Override>> protected function getArguments(): Vector { + if ($this->options->cpuBind) { + $this->cpuRange = $this->options->helperProcessors; + } return Vector { '-c', $this->getGeneratedConfigFile(), diff --git a/base/PHP5Daemon.php b/base/PHP5Daemon.php index 52a07ca..48ab27f 100644 --- a/base/PHP5Daemon.php +++ b/base/PHP5Daemon.php @@ -72,6 +72,9 @@ public function start(): void { } protected function getArguments(): Vector { + if ($this->options->cpuBind) { + $this->cpuRange = $this->options->daemonProcessors; + } if ($this->options->fpm) { echo 'Creating PHP FPM config'; $path = $this->options->tempDir.'/php-fpm.conf'; diff --git a/base/PerfOptions.php b/base/PerfOptions.php index dfca5e5..48a236a 100644 --- a/base/PerfOptions.php +++ b/base/PerfOptions.php @@ -42,6 +42,10 @@ final class PerfOptions { public ?string $dbUsername; public ?string $dbPassword; + public bool $cpuBind = false; + public ?string $daemonProcessors; + public ?string $helperProcessors; + public bool $fetchResources = false; public bool $forceInnodb = false; public bool $skipSanityCheck = false; @@ -146,6 +150,7 @@ public function __construct(Vector $argv) { 'setUpTest:', 'db-username:', 'db-password:', + 'cpu-fraction:', 'tearDownTest:', 'i-am-not-benchmarking', 'hhvm-extra-arguments:', @@ -255,6 +260,17 @@ public function __construct(Vector $argv) { $this->proxygen = !$this->getBool('no-proxygen'); $this->applyPatches = $this->getBool('apply-patches'); + $fraction = $this->getFloat('cpu-fraction', 1.0); + if ($fraction !== 1.0) { + $this->cpuBind = true; + $output = []; + exec('nproc', $output); + $numProcessors = (int)($output[0]); + $numDaemonProcessors = (int)($numProcessors * $fraction); + $this->helperProcessors = "$numDaemonProcessors-$numProcessors"; + $this->daemonProcessors = "0-$numDaemonProcessors"; + } + $this->precompile = !$this->getBool('no-repo-auth'); $this->filecache = $this->precompile && !$this->getBool('no-file-cache'); $this->pcreCache = $this->getNullableString('pcre-cache'); diff --git a/base/Process.php b/base/Process.php index 755a853..24eed2d 100644 --- a/base/Process.php +++ b/base/Process.php @@ -14,6 +14,7 @@ abstract class Process { protected ?resource $stdin; protected ?resource $stdout; protected ?string $command; + protected ?string $cpuRange = null; protected bool $suppress_stdout = false; private static Vector $processes = Vector {}; @@ -54,6 +55,11 @@ public function startWorker( if ($this->suppress_stdout) { $this->command .= ' >/dev/null'; } + + if ($this->cpuRange !== null) { + $this->command = 'taskset -c '.$this->cpuRange.' '.$this->command; + } + $use_pipe = ($outputFileName === null); $spec = [ diff --git a/base/Siege.php b/base/Siege.php index e915f4e..89ab14d 100644 --- a/base/Siege.php +++ b/base/Siege.php @@ -84,6 +84,9 @@ public function getExecutablePath(): string { <<__Override>> protected function getArguments(): Vector { + if ($this->options->cpuBind) { + $this->cpuRange = $this->options->helperProcessors; + } $urls_file = tempnam($this->options->tempDir, 'urls'); $urls = file_get_contents($this->target->getURLsFile()); $urls = diff --git a/batch-run.json.example b/batch-run.json.example index 1dea0bd..b39aac3 100644 --- a/batch-run.json.example +++ b/batch-run.json.example @@ -44,6 +44,10 @@ }, "settings": { "username": "root", - "password": "root" + "password": "root", + "options": [ + "cpu-fraction=0.5", + "i-am-not-benchmarking" + ] } } diff --git a/batch-run.php b/batch-run.php index 4757f02..c8990a1 100644 --- a/batch-run.php +++ b/batch-run.php @@ -143,6 +143,13 @@ function batch_get_single_run( $argv->addAll($runtime['args']); $argv[] = '--'.$target['name']; + foreach ($target['settings'] as $name => $value) { + if ($name === 'options') { + foreach ((array)$value as $v) { + $argv[] = '--'.$v; + } + } + } $options = new PerfOptions($argv); switch ($runtime['type']) { case BatchRuntimeType::HHVM: diff --git a/scripts/setup.sh b/scripts/setup.sh index 46d0ecc..02e328d 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,5 +1,5 @@ #!/bin/sh -sudo apt-get -y install nginx unzip mysql-server +sudo apt-get -y install nginx unzip mysql-server util-linux coreutils sudo apt-get -y install autotools-dev sudo apt-get -y install autoconf sudo apt-get -y install software-properties-common build-essential