/** * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('PHPMetrics by Jean-François Lépine <https://twitter.com/Halleck45>'); $output->writeln(''); // config $configFactory = new ConfigFactory(); $config = $configFactory->factory($input); // files if (null === $config->getPath()->getBasePath()) { throw new \LogicException('Please provide a path to analyze'); } // files to analyze $finder = new Finder($config->getPath()->getExtensions(), $config->getPath()->getExcludedDirs(), $config->getPath()->isFollowSymlinks() ? Finder::FOLLOW_SYMLINKS : null); // prepare structures $bounds = new Bounds(); $collection = new \Hal\Component\Result\ResultCollection(); $aggregatedResults = new \Hal\Component\Result\ResultCollection(); // execute analyze $queueFactory = new QueueFactory($input, $output, $config); $queue = $queueFactory->factory($finder, $bounds); gc_disable(); $queue->execute($collection, $aggregatedResults); gc_enable(); $output->writeln('<info>done</info>'); // evaluation of success $evaluator = new Evaluator($collection, $aggregatedResults, $bounds); $result = $evaluator->evaluate($config->getFailureCondition()); // fail if failure-condition is realized return $result->isValid() ? 1 : 0; }
function yay_parse(string $source, Directives $directives = null, BlueContext $blueContext = null) : string { if ($gc = gc_enabled()) { gc_disable(); } // important optimization! static $globalDirectives = null; if (null === $globalDirectives) { $globalDirectives = new ArrayObject(); } $directives = $directives ?: new Directives(); $blueContext = $blueContext ?: new BlueContext(); $cg = (object) ['ts' => TokenStream::fromSource($source), 'directives' => $directives, 'cycle' => new Cycle($source), 'globalDirectives' => $globalDirectives, 'blueContext' => $blueContext]; foreach ($cg->globalDirectives as $d) { $cg->directives->add($d); } traverse(midrule(function (TokenStream $ts) use($directives, $blueContext) { $token = $ts->current(); tail_call: if (null === $token) { return; } // skip when something looks like a new macro to be parsed if ('macro' === (string) $token) { return; } // here we do the 'magic' to match and expand userland macros $directives->apply($ts, $token, $blueContext); $token = $ts->next(); goto tail_call; }), consume(chain(token(T_STRING, 'macro')->as('declaration'), optional(repeat(rtoken('/^·\\w+$/')))->as('tags'), lookahead(token('{')), commit(chain(braces()->as('pattern'), operator('>>'), braces()->as('expansion')))->as('body'), optional(token(';'))), CONSUME_DO_TRIM)->onCommit(function (Ast $macroAst) use($cg) { $scope = Map::fromEmpty(); $tags = Map::fromValues(array_map('strval', $macroAst->{'tags'})); $pattern = new Pattern($macroAst->{'declaration'}->line(), $macroAst->{'body pattern'}, $tags, $scope); $expansion = new Expansion($macroAst->{'body expansion'}, $tags, $scope); $macro = new Macro($tags, $pattern, $expansion, $cg->cycle); $cg->directives->add($macro); // allocate the userland macro // allocate the userland macro globally if it's declared as global if ($macro->tags()->contains('·global')) { $cg->globalDirectives[] = $macro; } }))->parse($cg->ts); $expansion = (string) $cg->ts; if ($gc) { gc_enable(); } return $expansion; }
/** * @inheritdoc * @throws ContentProcessorException */ public function processContent(File $asset) { $path = $asset->getPath(); try { $parser = new \Less_Parser(['relativeUrls' => false, 'compress' => $this->appState->getMode() !== State::MODE_DEVELOPER]); $content = $this->assetSource->getContent($asset); if (trim($content) === '') { return ''; } $tmpFilePath = $this->temporaryFile->createFile($path, $content); gc_disable(); $parser->parseFile($tmpFilePath, ''); $content = $parser->getCss(); gc_enable(); if (trim($content) === '') { $errorMessage = PHP_EOL . self::ERROR_MESSAGE_PREFIX . PHP_EOL . $path; $this->logger->critical($errorMessage); throw new ContentProcessorException(new Phrase($errorMessage)); } return $content; } catch (\Exception $e) { $errorMessage = PHP_EOL . self::ERROR_MESSAGE_PREFIX . PHP_EOL . $path . PHP_EOL . $e->getMessage(); $this->logger->critical($errorMessage); throw new ContentProcessorException(new Phrase($errorMessage)); } }
/** * Runtime of Worker process. * @return void */ protected function run() { if (Daemon::$process instanceof Master) { Daemon::$process->unregisterSignals(); } EventLoop::init(); Daemon::$process = $this; if (Daemon::$logpointerAsync) { Daemon::$logpointerAsync->fd = null; Daemon::$logpointerAsync = null; } class_exists('Timer'); if (Daemon::$config->autogc->value > 0) { gc_enable(); } else { gc_disable(); } $this->prepareSystemEnv(); $this->registerEventSignals(); FileSystem::init(); // re-init FileSystem::initEvent(); Daemon::openLogs(); $this->fileWatcher = new FileWatcher(); $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager'); if (!$this->IPCManager) { $this->log('cannot instantiate IPCManager'); } EventLoop::$instance->run(); }
private static function huffman_lookup_init() { gc_disable(); $encodingAccess = []; $terminals = []; foreach (self::HUFFMAN_CODE as $chr => $bits) { $len = self::HUFFMAN_CODE_LENGTHS[$chr]; for ($bit = 0; $bit < 8; $bit++) { $offlen = $len + $bit; $next =& $encodingAccess[$bit]; for ($byte = (int) (($offlen - 1) / 8); $byte > 0; $byte--) { $cur = \str_pad(\decbin($bits >> $byte * 8 - (0x30 - $offlen) % 8 & 0xff), 8, "0", STR_PAD_LEFT); if (isset($next[$cur]) && $next[$cur][0] != $encodingAccess[0]) { $next =& $next[$cur][0]; } else { $tmp =& $next; unset($next); $tmp[$cur] = [&$next, null]; } } $key = \str_pad(\decbin($bits & (1 << ($offlen - 1) % 8 + 1) - 1), ($offlen - 1) % 8 + 1, "0", STR_PAD_LEFT); $next[$key] = [null, $chr > 0xff ? "" : \chr($chr)]; if ($offlen % 8) { $terminals[$offlen % 8][] = [$key, &$next]; } else { $next[$key][0] =& $encodingAccess[0]; } } } $memoize = []; for ($off = 7; $off > 0; $off--) { foreach ($terminals[$off] as &$terminal) { $key = $terminal[0]; $next =& $terminal[1]; if ($next[$key][0] === null) { foreach ($encodingAccess[$off] as $chr => &$cur) { $next[($memoize[$key] ?? ($memoize[$key] = \str_pad($key, 8, "0", STR_PAD_RIGHT))) | $chr] = [&$cur[0], $next[$key][1] != "" ? $next[$key][1] . $cur[1] : ""]; } unset($next[$key]); } } } $memoize = []; for ($off = 7; $off > 0; $off--) { foreach ($terminals[$off] as &$terminal) { $next =& $terminal[1]; foreach ($next as $k => $v) { if (\strlen($k) != 1) { $next[$memoize[$k] ?? ($memoize[$k] = \chr(\bindec($k)))] = $v; unset($next[$k]); } } } } unset($tmp, $cur, $next, $terminals, $terminal); gc_enable(); gc_collect_cycles(); return $encodingAccess[0]; }
public function __construct($bridgeName = null, $appBootstrap, array $config = []) { gc_disable(); $this->config = $config; $this->appBootstrap = $appBootstrap; $this->bridgeName = $bridgeName; $this->run(); }
/** * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('PHPMetrics by Jean-François Lépine <https://twitter.com/Halleck45>'); $output->writeln(''); // config $configFactory = new ConfigFactory(); $config = $configFactory->factory($input); // files if (null === $config->getPath()->getBasePath()) { throw new \LogicException('Please provide a path to analyze'); } // files to analyze $finder = new Finder($config->getPath()->getExtensions(), $config->getPath()->getExcludedDirs(), $config->getPath()->isFollowSymlinks() ? Finder::FOLLOW_SYMLINKS : null); // prepare plugins $repository = new Repository(); foreach ($config->getExtensions()->getExtensions() as $filename) { if (!file_exists($filename) || !is_readable($filename)) { $output->writeln(sprintf('<error>Plugin %s skipped: not found</error>', $filename)); continue; } $plugin = (require_once $filename); $repository->attach($plugin); } $extensionService = new ExtensionService($repository); // prepare structures $bounds = new Bounds(); $collection = new ResultCollection(); $aggregatedResults = new ResultCollection(); // execute analyze $queueFactory = new QueueAnalyzeFactory($input, $output, $config, $extensionService); $queue = $queueFactory->factory($finder, $bounds); gc_disable(); $queue->execute($collection, $aggregatedResults); gc_enable(); $output->writeln(''); // provide data to extensions if (($n = sizeof($repository->all())) > 0) { $output->writeln(sprintf('%d %s. Executing analyzis', $n, $n > 1 ? 'plugins are enabled' : 'plugin is enabled')); $extensionService->receive($config, $collection, $aggregatedResults, $bounds); } // generating reports $output->writeln("Generating reports..."); $queueFactory = new QueueReportFactory($input, $output, $config, $extensionService); $queue = $queueFactory->factory($finder, $bounds); $queue->execute($collection, $aggregatedResults); $output->writeln('<info>Done</info>'); // evaluation of success $rule = $config->getFailureCondition(); if (null !== $rule) { $evaluator = new Evaluator($collection, $aggregatedResults, $bounds); $result = $evaluator->evaluate($rule); // fail if failure-condition is realized return $result->isValid() ? 1 : 0; } return 0; }
protected function setUp() { parent::setUp(); // disable GC while https://bugs.php.net/bug.php?id=63677 is still open // If GC enabled, Gmagick unit tests fail gc_disable(); if (!class_exists('Gmagick')) { $this->markTestSkipped('Gmagick is not installed'); } }
/** * Gc */ private function gc() { if (gc_enabled()) { gc_collect_cycles(); } else { gc_enable(); gc_collect_cycles(); gc_disable(); } }
public static function clean($print = false) { gc_enable(); // Enable Garbage Collector if ($print) { AppLog::log(gc_collect_cycles() . " garbage cycles cleaned"); // # of elements cleaned up } gc_disable(); // Disable Garbage Collector }
public function formatCode($source = '') { gc_enable(); $passes = array_map(function ($pass) { return clone $pass; }, $this->passes); while ($pass = array_shift($passes)) { $source = $pass->format($source); gc_collect_cycles(); } gc_disable(); return $source; }
private static function doBench(callable $func, array $args) { gc_collect_cycles(); gc_disable(); usleep(0); $t = microtime(true); for ($i = self::$iterations; $i >= 0; --$i) { call_user_func_array($func, $args); } $t = microtime(true) - $t; gc_enable(); gc_collect_cycles(); usleep(0); return round($t * 1000000 / self::$iterations, 3); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { gc_disable(); $this->output = $output; $this->router->getContext()->setScheme($this->config['scheme']); $this->router->getContext()->setHost($this->config['host']); $start = time(); $name = $input->getOption('name'); if ($name && !$this->runSingleSitemap($name)) { return 1; } else { $this->runMultiSitemaps(); } $this->output->writeln($this->getHelper('formatter')->formatBlock(['[Info]', sprintf('Mission Accomplished in %d s', time() - $start)], ConsoleLogger::INFO)); return 0; }
protected static function timeExec(callable $code, $rounds) { $gc_enabled = gc_enabled(); if ($gc_enabled) { gc_disable(); } $start = microtime(TRUE); for ($i = 0; $i < $rounds; $i++) { $code(); } $end = microtime(TRUE); if ($gc_enabled) { gc_enable(); } return $end - $start; }
function install($diff) { if (!file_exists($diff)) { return; } gc_enable(); $root = __DIR__ . "/../../"; $archive = new ZipArchive(); $archive->open($diff); $bower = $archive->locateName("bower.json"); $webDir = $root . "web"; if (!file_exists($webDir)) { mkdir($webDir); } $privateDir = $root . "private"; if (!file_exists($privateDir)) { mkdir($privateDir); } if ($bower) { $bowerDir = $root . "web/vendor"; if (file_exists($bowerDir)) { $this->rrmdir($bowerDir); } mkdir($bowerDir); } $composer = $archive->locateName("composer.json"); if ($composer) { $composerDir = $root . "private/vendor"; if (file_exists($composerDir)) { $this->rrmdir($composerDir); } mkdir($composerDir); } $tempDir = $root . "private/temp"; $this->rrmdir($tempDir); mkdir($tempDir); $archive->extractTo($root); @unlink($diff); unset($archive); gc_collect_cycles(); gc_disable(); }
public function generateProjectIndex(Project $project) { // You know what this mean gc_disable(); $index = $project->getIndex(); $globalTime = 0; $done = 0; $files = $this->filesFinder->getProjectFiles($project); $all = count($files); foreach ($files as $file) { $start = microtime(1); $this->processFile($index, $file, false, false); $end = microtime(1) - $start; $this->getLogger()->debug("Indexing: [{$end}]s"); $this->getLogger()->debug("Memory: " . memory_get_usage()); $globalTime += $end; ++$done; $process = floor($done / $all * 100); $this->getLogger()->info("Progress: {$process}%"); } $this->getLogger()->info("[ {$globalTime} ]"); gc_enable(); }
/** * Metodo Magico * __Destruct() * * Proceso de liberacion de memoria, se genera el proceso del garbage collector para una eliminacion de datos mas eficiente * @access private */ function __Destruct() { gc_enable(); $this->MatrizAccesos; $this->MatrizErrores; gc_collect_cycles(); gc_disable(); }
/** * Run installation (or update) * * @throws \Exception * @return int 0 on success or a positive error code on failure */ public function run() { // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands // of PHP objects, the GC can spend quite some time walking the tree of references looking // for stuff to collect while there is nothing to collect. This slows things down dramatically // and turning it off results in much better performance. Do not try this at home however. gc_collect_cycles(); gc_disable(); // Force update if there is no lock file present if (!$this->update && !$this->locker->isLocked()) { $this->update = true; } if ($this->dryRun) { $this->verbose = true; $this->runScripts = false; $this->installationManager->addInstaller(new NoopInstaller()); $this->mockLocalRepositories($this->repositoryManager); } if ($this->runScripts) { // dispatch pre event $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $this->downloadManager->setPreferSource($this->preferSource); $this->downloadManager->setPreferDist($this->preferDist); // create installed repo, this contains all local packages + platform packages (php & extensions) $localRepo = $this->repositoryManager->getLocalRepository(); if ($this->update) { $platformOverrides = $this->config->get('platform') ?: array(); } else { $platformOverrides = $this->locker->getPlatformOverrides(); } $platformRepo = new PlatformRepository(array(), $platformOverrides); $installedRepo = $this->createInstalledRepo($localRepo, $platformRepo); $aliases = $this->getRootAliases(); $this->aliasPlatformPackages($platformRepo, $aliases); if (!$this->suggestedPackagesReporter) { $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io); } try { list($res, $devPackages) = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases); if ($res !== 0) { return $res; } } catch (\Exception $e) { if (!$this->dryRun) { $this->installationManager->notifyInstalls($this->io); } throw $e; } if (!$this->dryRun) { $this->installationManager->notifyInstalls($this->io); } // output suggestions if we're in dev mode if ($this->devMode && !$this->skipSuggest) { $this->suggestedPackagesReporter->output($installedRepo); } # Find abandoned packages and warn user foreach ($localRepo->getPackages() as $package) { if (!$package instanceof CompletePackage || !$package->isAbandoned()) { continue; } $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested'; $this->io->writeError(sprintf("<warning>Package %s is abandoned, you should avoid using it. %s.</warning>", $package->getPrettyName(), $replacement)); } if (!$this->dryRun) { // write lock if ($this->update) { $localRepo->reload(); $platformReqs = $this->extractPlatformRequirements($this->package->getRequires()); $platformDevReqs = $this->extractPlatformRequirements($this->package->getDevRequires()); $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: array()); if ($updatedLock) { $this->io->writeError('<info>Writing lock file</info>'); } } if ($this->dumpAutoloader) { // write autoloader if ($this->optimizeAutoloader) { $this->io->writeError('<info>Generating optimized autoload files</info>'); } else { $this->io->writeError('<info>Generating autoload files</info>'); } $this->autoloadGenerator->setDevMode($this->devMode); $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative); $this->autoloadGenerator->setRunScripts($this->runScripts); $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); } if ($this->runScripts) { $devMode = (int) $this->devMode; putenv("COMPOSER_DEV_MODE={$devMode}"); // dispatch post event $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } // force binaries re-generation in case they are missing foreach ($localRepo->getPackages() as $package) { $this->installationManager->ensureBinariesPresence($package); } $vendorDir = $this->config->get('vendor-dir'); if (is_dir($vendorDir)) { // suppress errors as this fails sometimes on OSX for no apparent reason // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 @touch($vendorDir); } } // re-enable GC except on HHVM which triggers a warning here if (!defined('HHVM_VERSION')) { gc_enable(); } return 0; }
/** * Starts the main loop. Blocks. */ public function run() { Debug::enable(); gc_disable(); //necessary, since connections will be dropped without reasons after several hundred connections. $this->loop = \React\EventLoop\Factory::create(); $this->controller = new \React\Socket\Server($this->loop); $this->controller->on('connection', array($this, 'onSlaveConnection')); $this->controller->listen(5500); $this->web = new \React\Socket\Server($this->loop); $this->web->on('connection', array($this, 'onWeb')); $this->web->listen($this->port, $this->host); $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop); $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop); $pcntl->on(SIGTERM, [$this, 'shutdown']); $pcntl->on(SIGINT, [$this, 'shutdown']); if ($this->isDebug()) { $this->loop->addPeriodicTimer(0.5, function () { $this->checkChangedFiles(); }); } $this->isRunning = true; $loopClass = (new \ReflectionClass($this->loop))->getShortName(); $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>"); for ($i = 0; $i < $this->slaveCount; $i++) { $this->newInstance(5501 + $i); } $this->loop->run(); }
/** * Run installation (or update) * * @throws \Exception * @return int 0 on success or a positive error code on failure */ public function run() { // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands // of PHP objects, the GC can spend quite some time walking the tree of references looking // for stuff to collect while there is nothing to collect. This slows things down dramatically // and turning it off results in much better performance. Do not try this at home however. gc_collect_cycles(); gc_disable(); if ($this->dryRun) { $this->verbose = true; $this->runScripts = false; $this->installationManager->addInstaller(new NoopInstaller()); $this->mockLocalRepositories($this->repositoryManager); } if ($this->runScripts) { // dispatch pre event $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $this->downloadManager->setPreferSource($this->preferSource); $this->downloadManager->setPreferDist($this->preferDist); // clone root package to have one in the installed repo that does not require anything // we don't want it to be uninstallable, but its requirements should not conflict // with the lock file for example $installedRootPackage = clone $this->package; $installedRootPackage->setRequires(array()); $installedRootPackage->setDevRequires(array()); // create installed repo, this contains all local packages + platform packages (php & extensions) $localRepo = $this->repositoryManager->getLocalRepository(); if (!$this->update && $this->locker->isLocked()) { $platformOverrides = $this->locker->getPlatformOverrides(); } else { $platformOverrides = $this->config->get('platform') ?: array(); } $platformRepo = new PlatformRepository(array(), $platformOverrides); $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo); $installedRepo = new CompositeRepository($repos); if ($this->additionalInstalledRepository) { $installedRepo->addRepository($this->additionalInstalledRepository); } $aliases = $this->getRootAliases(); $this->aliasPlatformPackages($platformRepo, $aliases); try { $this->suggestedPackages = array(); $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode); if ($res !== 0) { return $res; } } catch (\Exception $e) { if (!$this->dryRun) { $this->installationManager->notifyInstalls($this->io); } throw $e; } if (!$this->dryRun) { $this->installationManager->notifyInstalls($this->io); } // output suggestions if we're in dev mode if ($this->devMode) { foreach ($this->suggestedPackages as $suggestion) { $target = $suggestion['target']; foreach ($installedRepo->getPackages() as $package) { if (in_array($target, $package->getNames())) { continue 2; } } $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')'); } } # Find abandoned packages and warn user foreach ($localRepo->getPackages() as $package) { if (!$package instanceof CompletePackage || !$package->isAbandoned()) { continue; } $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested'; $this->io->writeError(sprintf("<warning>Package %s is abandoned, you should avoid using it. %s.</warning>", $package->getPrettyName(), $replacement)); } if (!$this->dryRun) { // write lock if ($this->update || !$this->locker->isLocked()) { $localRepo->reload(); // if this is not run in dev mode and the root has dev requires, the lock must // contain null to prevent dev installs from a non-dev lock $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null; // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements if ($this->devMode && $this->package->getDevRequires()) { $policy = $this->createPolicy(); $pool = $this->createPool(true); $pool->addRepository($installedRepo, $aliases); // creating requirements request $request = $this->createRequest($this->package, $platformRepo); $request->updateAll(); foreach ($this->package->getRequires() as $link) { $request->install($link->getTarget(), $link->getConstraint()); } $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request); $solver = new Solver($policy, $pool, $installedRepo); $ops = $solver->solve($request, $this->ignorePlatformReqs); $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops); foreach ($ops as $op) { if ($op->getJobType() === 'uninstall') { $devPackages[] = $op->getPackage(); } } } $platformReqs = $this->extractPlatformRequirements($this->package->getRequires()); $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array(); $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: array()); if ($updatedLock) { $this->io->writeError('<info>Writing lock file</info>'); } } if ($this->dumpAutoloader) { // write autoloader if ($this->optimizeAutoloader) { $this->io->writeError('<info>Generating optimized autoload files</info>'); } else { $this->io->writeError('<info>Generating autoload files</info>'); } $this->autoloadGenerator->setDevMode($this->devMode); $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative); $this->autoloadGenerator->setRunScripts($this->runScripts); $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); } if ($this->runScripts) { // dispatch post event $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $vendorDir = $this->config->get('vendor-dir'); if (is_dir($vendorDir)) { // suppress errors as this fails sometimes on OSX for no apparent reason // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 @touch($vendorDir); } } // re-enable GC except on HHVM which triggers a warning here if (!defined('HHVM_VERSION')) { gc_enable(); } return 0; }
public function run() { proc_nice(Daemon::$settings['workerpriority']); Daemon::$worker = $this; $this->microsleep = Daemon::$settings['microsleep']; $this->autoReloadLast = time(); $this->reloadDelay = Daemon::$parsedSettings['mpmdelay'] + 2; $this->setStatus(4); Thread::setproctitle(Daemon::$runName . ': worker process' . (Daemon::$settings['pidfile'] !== Daemon::$settings['defaultpidfile'] ? ' (' . Daemon::$settings['pidfile'] . ')' : '')); register_shutdown_function(array($this, 'shutdown')); if (Daemon::$settings['autogc'] > 0) { gc_enable(); } else { gc_disable(); } if (isset(Daemon::$settings['group'])) { $sg = posix_getgrnam(Daemon::$settings['group']); } if (isset(Daemon::$settings['user'])) { $su = posix_getpwnam(Daemon::$settings['user']); } if (Daemon::$settings['chroot'] !== '/') { if (posix_getuid() != 0) { Daemon::log('You must have the root privileges to change root.'); exit(0); } elseif (!chroot(Daemon::$settings['chroot'])) { Daemon::log('Couldn\'t change root to \'' . Daemon::$settings['chroot'] . '\'.'); exit(0); } } if (isset(Daemon::$settings['group'])) { if ($sg === FALSE) { Daemon::log('Couldn\'t change group to \'' . Daemon::$settings['group'] . '\'. You must replace config-variable \'group\' with existing group.'); exit(0); } elseif ($sg['gid'] != posix_getgid() && !posix_setgid($sg['gid'])) { Daemon::log('Couldn\'t change group to \'' . Daemon::$settings['group'] . "'. Error (" . ($errno = posix_get_last_error()) . '): ' . posix_strerror($errno)); exit(0); } } if (isset(Daemon::$settings['user'])) { if ($su === FALSE) { Daemon::log('Couldn\'t change user to \'' . Daemon::$settings['user'] . '\', user not found. You must replace config-variable \'user\' with existing username.'); exit(0); } elseif ($su['uid'] != posix_getuid() && !posix_setuid($su['uid'])) { Daemon::log('Couldn\'t change user to \'' . Daemon::$settings['user'] . "'. Error (" . ($errno = posix_get_last_error()) . '): ' . posix_strerror($errno)); exit(0); } } if (Daemon::$settings['cwd'] !== '.') { if (!@chdir(Daemon::$settings['cwd'])) { Daemon::log('WORKER ' . $this->pid . '] Couldn\'t change directory to \'' . Daemon::$settings['cwd'] . '.'); } } $this->setStatus(6); $this->eventBase = event_base_new(); Daemon::$appResolver->preload(); foreach (Daemon::$appInstances as $app) { foreach ($app as $appInstance) { if (!$appInstance->ready) { $this->ready = TRUE; $appInstance->onReady(); } } } $this->setStatus(1); $ev = event_new(); event_set($ev, STDIN, EV_TIMEOUT, function () { }, array()); event_base_set($ev, $this->eventBase); $this->timeoutEvent = $ev; while (TRUE) { pcntl_signal_dispatch(); if (($s = $this->checkState()) !== TRUE) { $this->closeSockets(); if (sizeof($this->queue) === 0) { return $s; } } event_add($this->timeoutEvent, $this->microsleep); event_base_loop($this->eventBase, EVLOOP_ONCE); do { for ($i = 0, $s = sizeof($this->eventsToAdd); $i < $s; ++$i) { event_add($this->eventsToAdd[$i]); unset($this->eventsToAdd[$i]); } $this->readPool(); $processed = $this->runQueue(); } while ($processed || $this->readPoolState || $this->eventsToAdd); } }
/** * Runtime of Worker process. * @return void */ protected function run() { $this->lambdaCache = new CappedStorageHits(); $this->lambdaCache->setMaxCacheSize(Daemon::$config->lambdacachemaxsize->value); $this->lambdaCache->setCapWindow(Daemon::$config->lambdacachecapwindow->value); $this->callbacks = new StackCallbacks(); if (Daemon::$process instanceof Master) { Daemon::$process->unregisterSignals(); } if (Daemon::$process && Daemon::$process->eventBase) { Daemon::$process->eventBase->reinit(); $this->eventBase = Daemon::$process->eventBase; } else { $this->eventBase = new \EventBase(); } Daemon::$process = $this; if (Daemon::$logpointerAsync) { $oldfd = Daemon::$logpointerAsync->fd; Daemon::$logpointerAsync->fd = null; Daemon::$logpointerAsync = null; } class_exists('Timer'); $this->autoReloadLast = time(); $this->reloadDelay = Daemon::$config->mpmdelay->value + 2; $this->setState(Daemon::WSTATE_PREINIT); if (Daemon::$config->autogc->value > 0) { gc_enable(); gc_collect_cycles(); } else { gc_disable(); } if (Daemon::$runworkerMode) { if (!Daemon::$config->verbosetty->value) { fclose(STDIN); fclose(STDOUT); fclose(STDERR); } Daemon::$appResolver->preload(true); } $this->prepareSystemEnv(); $this->overrideNativeFuncs(); $this->setState(Daemon::WSTATE_INIT); $this->dnsBase = new \EventDnsBase($this->eventBase, false); // @TODO: test with true $this->registerEventSignals(); FileSystem::init(); FileSystem::initEvent(); Daemon::openLogs(); $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager'); if (!$this->IPCManager) { $this->log('cannot instantiate IPCManager'); } Daemon::$appResolver->preload(); foreach (Daemon::$appInstances as $app) { foreach ($app as $appInstance) { if (!$appInstance->ready) { $appInstance->ready = true; $appInstance->onReady(); } } } $this->setState(Daemon::WSTATE_IDLE); Timer::add(function ($event) { if (!Daemon::$runworkerMode) { if ($this->IPCManager) { $this->IPCManager->ensureConnection(); } } $this->breakMainLoopCheck(); if ($this->breakMainLoop) { $this->eventBase->exit(); return; } if (Daemon::checkAutoGC()) { $this->callbacks->push(function ($thread) { gc_collect_cycles(); }); $this->eventBase->exit(); } $event->timeout(); }, 1000000.0 * 1, 'breakMainLoopCheck'); if (Daemon::$config->autoreload->value > 0) { Timer::add(function ($event) { static $n = 0; $list = get_included_files(); $s = sizeof($list); if ($s > $n) { $slice = array_map('realpath', array_slice($list, $n)); Daemon::$process->IPCManager->sendPacket(['op' => 'addIncludedFiles', 'files' => $slice]); $n = $s; } $event->timeout(); }, 1000000.0 * Daemon::$config->autoreload->value, 'watchIncludedFiles'); } while (!$this->breakMainLoop) { $this->callbacks->executeAll($this); if (!$this->eventBase->dispatch()) { break; } } $this->shutdown(); }
/** * fixing GC segmentation fault (disable GC before test) */ protected function setUpGcFix() { gc_collect_cycles(); gc_disable(); }
/** * Forced cleaning memory */ function memory_clear() { if (function_exists('gc_collect_cycles')) { gc_enable(); $_ = gc_collect_cycles(); gc_disable(); return (int) $_; } return 0; }
public function dumpServerMemory($outputFolder, $maxNesting, $maxStringSize) { gc_disable(); if (!file_exists($outputFolder)) { mkdir($outputFolder, 0777, true); } $this->server->getLogger()->notice("[Dump] After the memory dump is done, the server might crash"); $obData = fopen($outputFolder . "/objects.js", "wb+"); $staticProperties = []; $data = []; $objects = []; $refCounts = []; $this->continueDump($this->server, $data, $objects, $refCounts, 0, $maxNesting, $maxStringSize); do { $continue = false; foreach ($objects as $hash => $object) { if (!is_object($object)) { continue; } $continue = true; $className = get_class($object); $objects[$hash] = true; $reflection = new \ReflectionObject($object); $info = ["information" => "{$hash}@{$className}", "properties" => []]; if ($reflection->getParentClass()) { $info["parent"] = $reflection->getParentClass()->getName(); } if (count($reflection->getInterfaceNames()) > 0) { $info["implements"] = implode(", ", $reflection->getInterfaceNames()); } foreach ($reflection->getProperties() as $property) { if ($property->isStatic()) { continue; } if (!$property->isPublic()) { $property->setAccessible(true); } $this->continueDump($property->getValue($object), $info["properties"][$property->getName()], $objects, $refCounts, 0, $maxNesting, $maxStringSize); } fwrite($obData, "{$hash}@{$className}: " . json_encode($info, JSON_UNESCAPED_SLASHES) . "\n"); if (!isset($objects["staticProperties"][$className])) { $staticProperties[$className] = []; foreach ($reflection->getProperties() as $property) { if (!$property->isStatic() or $property->getDeclaringClass()->getName() !== $className) { continue; } if (!$property->isPublic()) { $property->setAccessible(true); } $this->continueDump($property->getValue($object), $staticProperties[$className][$property->getName()], $objects, $refCounts, 0, $maxNesting, $maxStringSize); } } } echo "[Dump] Wrote " . count($objects) . " objects\n"; } while ($continue); file_put_contents($outputFolder . "/staticProperties.js", json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/serverEntry.js", json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/referenceCounts.js", json_encode($refCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); echo "[Dump] Finished!\n"; gc_enable(); $this->server->forceShutdown(); }
$testNames = getenv('MP_BENCH_TESTS') ?: '-16-bit array #2, -32-bit array, -16-bit map #2, -32-bit map'; $benchmark = getenv('MP_BENCH_DURATION') ? new DurationBenchmark(getenv('MP_BENCH_DURATION')) : new IterationBenchmark(getenv('MP_BENCH_ITERATIONS') ?: 100000); if ($rounds) { $benchmark = new AverageableBenchmark($benchmark, $rounds); } if ($testNames) { $filter = '/' === $testNames[0] ? new RegexpFilter($testNames) : new ListFilter(explode(',', $testNames)); $benchmark = new FilterableBenchmark($benchmark, $filter); } $targetFactories = ['pecl_p' => function () { return new PeclFunctionPackTarget(); }, 'pecl_u' => function () { return new PeclFunctionUnpackTarget(); }, 'pure_p' => function () { return new PackerTarget('Packer'); }, 'pure_ps' => function () { return new PackerTarget('Packer (str)', new Packer(Packer::FORCE_STR)); }, 'pure_pa' => function () { return new PackerTarget('Packer (arr)', new Packer(Packer::FORCE_ARR)); }, 'pure_psa' => function () { return new PackerTarget('Packer (str|arr)', new Packer(Packer::FORCE_STR | Packer::FORCE_ARR)); }, 'pure_bu' => function () { return new BufferUnpackerTarget('BufferUnpacker'); }]; $targets = []; foreach (explode(',', $targetAliases) as $alias) { $targets[] = $targetFactories[trim($alias)](); } $runner = new Runner(DataProvider::provideData()); gc_disable(); $runner->run($benchmark, $targets);
/** * Run installation (or update) * * @return int 0 on success or a positive error code on failure * * @throws \Exception */ public function run() { gc_collect_cycles(); gc_disable(); if ($this->dryRun) { $this->verbose = true; $this->runScripts = false; $this->installationManager->addInstaller(new NoopInstaller()); $this->mockLocalRepositories($this->repositoryManager); } // TODO remove this BC feature at some point // purge old require-dev packages to avoid conflicts with the new way of handling dev requirements $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir') . '/composer/installed_dev.json')); if ($devRepo->getPackages()) { $this->io->writeError('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>'); foreach ($devRepo->getPackages() as $package) { if ($this->installationManager->isPackageInstalled($devRepo, $package)) { $this->installationManager->uninstall($devRepo, new UninstallOperation($package)); } } unlink($this->config->get('vendor-dir') . '/composer/installed_dev.json'); } unset($devRepo, $package); // end BC if ($this->runScripts) { // dispatch pre event $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $this->downloadManager->setPreferSource($this->preferSource); $this->downloadManager->setPreferDist($this->preferDist); // clone root package to have one in the installed repo that does not require anything // we don't want it to be uninstallable, but its requirements should not conflict // with the lock file for example $installedRootPackage = clone $this->package; $installedRootPackage->setRequires(array()); $installedRootPackage->setDevRequires(array()); // create installed repo, this contains all local packages + platform packages (php & extensions) $localRepo = $this->repositoryManager->getLocalRepository(); $platformRepo = new PlatformRepository(); $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo); $installedRepo = new CompositeRepository($repos); if ($this->additionalInstalledRepository) { $installedRepo->addRepository($this->additionalInstalledRepository); } $aliases = $this->getRootAliases(); $this->aliasPlatformPackages($platformRepo, $aliases); try { $this->suggestedPackages = array(); $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode); if ($res !== 0) { return $res; } } catch (\Exception $e) { $this->installationManager->notifyInstalls(); throw $e; } $this->installationManager->notifyInstalls(); // output suggestions if we're in dev mode if ($this->devMode) { foreach ($this->suggestedPackages as $suggestion) { $target = $suggestion['target']; foreach ($installedRepo->getPackages() as $package) { if (in_array($target, $package->getNames())) { continue 2; } } $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')'); } } # Find abandoned packages and warn user foreach ($localRepo->getPackages() as $package) { if (!$package instanceof CompletePackage || !$package->isAbandoned()) { continue; } $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested'; $this->io->writeError(sprintf("<error>Package %s is abandoned, you should avoid using it. %s.</error>", $package->getPrettyName(), $replacement)); } if (!$this->dryRun) { // write lock if ($this->update || !$this->locker->isLocked()) { $localRepo->reload(); // if this is not run in dev mode and the root has dev requires, the lock must // contain null to prevent dev installs from a non-dev lock $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null; // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements if ($this->devMode && $this->package->getDevRequires()) { $policy = $this->createPolicy(); $pool = $this->createPool(true); $pool->addRepository($installedRepo, $aliases); // creating requirements request $request = $this->createRequest($pool, $this->package, $platformRepo); $request->updateAll(); foreach ($this->package->getRequires() as $link) { $request->install($link->getTarget(), $link->getConstraint()); } $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request); $solver = new Solver($policy, $pool, $installedRepo); $ops = $solver->solve($request, $this->ignorePlatformReqs); $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops); foreach ($ops as $op) { if ($op->getJobType() === 'uninstall') { $devPackages[] = $op->getPackage(); } } } $platformReqs = $this->extractPlatformRequirements($this->package->getRequires()); $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array(); $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest); if ($updatedLock) { $this->io->writeError('<info>Writing lock file</info>'); } } if ($this->dumpAutoloader) { // write autoloader if ($this->optimizeAutoloader) { $this->io->writeError('<info>Generating optimized autoload files</info>'); } else { $this->io->writeError('<info>Generating autoload files</info>'); } $this->autoloadGenerator->setDevMode($this->devMode); $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); } if ($this->runScripts) { // dispatch post event $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $vendorDir = $this->config->get('vendor-dir'); if (is_dir($vendorDir)) { touch($vendorDir); } } return 0; }
/** * @param Worker $worker * @param GearmanWorker $gmworker * @param OutputInterface $output * @return void * @throws RuntimeException */ public function registerWorker(Worker $worker, GearmanWorker $gmworker, OutputInterface $output) { $pointsOfEntry = $this->readPointsOfEntry($worker, $output); if (empty($pointsOfEntry)) { throw new \RuntimeException(sprintf('No "PointOfEntry" annotations found in public methods of "%s".', get_class($worker))); } foreach ($pointsOfEntry as $entryPointName => $entryPoint) { $gmworker->addFunction($entryPointName, function (GearmanJob $gearmanJob) use($entryPoint, $entryPointName, $output) { gc_enable(); // Add a new job $job = $this->createJob(); $job->setName($entryPointName); $job->setStartTime(new \DateTime()); $job->setWorkload($gearmanJob->workload()); // Launch event, like refreshing doctrine $beforeEvent = new JobEvent($gearmanJob, $job); $this->getEventDispatcher()->dispatch(Events::JOB_STARTED, $beforeEvent); $manager = $this->getManager(); $manager->persist($job); $manager->flush($job); try { ob_start(); $taskReturnStatus = call_user_func_array(array($entryPoint[0], $entryPoint[1]), array($gearmanJob, $output)); $output = ob_get_clean(); } catch (\Exception $e) { $output = ob_get_clean(); $taskReturnStatus = false; $job->setErrorOutput($e->getMessage()); } // GOTCHA: null means success false === $taskReturnStatus ? $gearmanJob->sendFail() : $gearmanJob->sendComplete($taskReturnStatus); $job->setEndTime(new \DateTime()); $job->setReturnStatus($taskReturnStatus); $job->setOutput($output); // After event $afterEvent = new JobEvent($gearmanJob, $job); $this->getEventDispatcher()->dispatch(Events::JOB_FINISHED, $afterEvent); $manager->flush($job); gc_collect_cycles(); gc_disable(); return $taskReturnStatus; }); $entryPointTarget = implode('::', array(get_class($entryPoint[0]), $entryPoint[1])); $output->writeln(sprintf('Registered "%s" function pointing to "%s".', $entryPointName, $entryPointTarget)); } }
private function preventMemoryLeak() { gc_enable(); gc_collect_cycles(); gc_disable(); }
private static function huffman_lookup_init() { $encodingAccess = []; $terminals = []; gc_disable(); foreach (self::HUFFMAN_CODE as $chr => $bits) { $len = self::HUFFMAN_CODE_LENGTHS[$chr]; for ($bit = 0; $bit < 8; $bit++) { $offlen = $len + $bit; $next =& $encodingAccess[$bit]; for ($byte = (int) (($offlen - 1) / 8); $byte > 0; $byte--) { $cur = str_pad(decbin($bits >> $byte * 8 - (0x30 - $offlen) % 8 & 0xff), 8, "0", STR_PAD_LEFT); if (isset($next[$cur]) && $next[$cur][0] != $encodingAccess[0]) { $next =& $next[$cur][0]; } else { $tmp =& $next; unset($next); $tmp[$cur] = [&$next, null]; } } $key = str_pad(decbin($bits & (1 << ($offlen - 1) % 8 + 1) - 1), ($offlen - 1) % 8 + 1, "0", STR_PAD_LEFT); $next[$key] = [null, $chr > 0xff ? "" : chr($chr)]; if ($offlen % 8) { $terminals[$offlen % 8][] = [$key, &$next]; } else { $next[$key][0] =& $encodingAccess[0]; } } } $memoize = []; for ($off = 7; $off > 0; $off--) { foreach ($terminals[$off] as &$terminal) { $key = $terminal[0]; $next =& $terminal[1]; if ($next[$key][0] === null) { foreach ($encodingAccess[$off] as $chr => &$cur) { $next[($memoize[$key] ?? ($memoize[$key] = str_pad($key, 8, "0", STR_PAD_RIGHT))) | $chr] = [&$cur[0], $next[$key][1] != "" ? $next[$key][1] . $cur[1] : ""]; } unset($next[$key]); } } } $memoize = []; ($fn = function (&$arr) use(&$fn, &$encodingAccess, &$memoize) { ksort($arr, SORT_STRING); // this line is really not performant... foreach ($arr as $k => $v) { $arr[$memoize[$k] ?? ($memoize[$k] = chr(bindec($k)))] = $v; unset($arr[$k]); } foreach ($arr as $k => $v) { if (\strlen(key($v[0])) == 8) { // prevent infinite recursion $fn($v[0]); } } })($encodingAccess[0]); gc_enable(); gc_collect_cycles(); return $encodingAccess[0]; }