/** * Dump live informations from extender process (if in daemon mode and pcntl installed) * * @param int $timestamp_absolute * @param itn $parent_pid * @param int $completed_processes * @param itn $failed_processes */ public static final function dump($timestamp_absolute, $parent_pid, $completed_processes, $failed_processes, $paused) { $statusfile = EXTENDER_CACHE_FOLDER . self::$statusfile; $data = array("RUNNING" => $paused ? 0 : 1, "STARTED" => $timestamp_absolute, "TIME" => microtime(true) - $timestamp_absolute, "PARENTPID" => $parent_pid, "COMPLETED" => $completed_processes, "FAILED" => $failed_processes, "CPUAVG" => sys_getloadavg(), "MEM" => memory_get_usage(true), "MEMPEAK" => memory_get_peak_usage(true), "USER" => get_current_user(), "NICENESS" => function_exists('pcntl_getpriority') ? pcntl_getpriority() : "UNDEFINED"); $content = serialize($data); return file_put_contents($statusfile, $content); }
/** * Get Priority * * @param int $pid If not specified, the pid of the current process is used. * @param int $processIdentifier One of PRIO_PGRP, PRIO_USER or PRIO_PROCESS. * * @return int The priority of the process or FALSE on error. A lower numerical value causes more favorable scheduling. */ public function getPriority($pid = null, $processIdentifier = PRIO_PROCESS) { if (is_null($pid)) { $pid = getmypid(); } return pcntl_getpriority($pid, $processIdentifier); }
public function testGetPriority() { $p = \pcntl_getpriority(\posix_getpid()); $this->assertTrue(is_integer($p)); //returns null in HHVM $p = @\pcntl_getpriority('xxx'); $err = \error_get_last(); $this->assertTrue(is_null($p) || $p === false); }
function exit_if_already_running($pidfile) { # No pidfile, probably no daemon present if (!file_exists($pidfile)) { return 1; } $pid = intval(file_get_contents($pidfile)); # No pid, probably no daemon present if (!$pid || @pcntl_getpriority($pid) === false) { return 1; } exit(1); }
protected function getLocks() { if (is_null($this->laraext_state)) { $list = $this->laravel['files']->files(storage_path('locks')); $this->laraext_state = []; foreach ($list as $file) { if (preg_match("#.+laraext\\.(\\d+)\$#", $file, $matches)) { $job = json_decode(trim(file($file)[0]), true); $job['pid'] = $matches[1]; $job['state'] = @pcntl_getpriority($job['pid']) === false ? "KILLED" : "ALIVE"; $job['lock'] = $file; $this->laraext_state[] = $job; } } } return $this->laraext_state; }
/** * daemon is active? * @return boolean */ public function isActive() { try { $pid = $this->_getPidFromFile(); } catch (\Exception $e) { return false; } if (false === $pid) { return false; } if (false === ($active = @pcntl_getpriority($pid))) { return false; } else { return true; } }
/** * Set process priority * * @return int */ public function getPriority() { return pcntl_getpriority(posix_getpid()); }
/** * Gets the fork's scheduling priority as a percentage. * * The priority is a float between 0 and 1 that indicates the relative priority for the forked process, where 0 is * very low priority, 1 is very high priority, and 0.5 is considered a "normal" priority. The value is based on the * forked process's "nice" value. The priority affects the operating system's scheduling of processes. How much the * priority actually affects the amount of CPU time the process gets is ultimately system-specific. * * @return float A priority value between 0 and 1. * * @throws ForkException If the operation failed. * * @see Fork::setPriority() * @see http://linux.die.net/man/2/getpriority */ public function getPriority() : float { if (($nice = pcntl_getpriority($this->pid)) === false) { throw new ForkException('Failed to get the fork\'s priority.'); } return (19 - $nice) / 39; }
echo "Failed: {$y}\n"; echo "Got: {$x}\n"; var_dump(debug_backtrace()); } } function VERIFY($x) { VS($x != false, true); } ////////////////////////////////////////////////////////////////////// $pid = pcntl_fork(); if ($pid == 0) { exit(123); } pcntl_wait($status); VS(pcntl_getpriority(), 0); VERIFY(pcntl_setpriority(0)); $pid = pcntl_fork(); if ($pid == 0) { exit(0x12); } pcntl_wait($status); VS($status, 0x1200); $pid = pcntl_fork(); if ($pid == 0) { exit(0x12); } pcntl_waitpid(0, $status); VS($status, 0x1200); $pid = pcntl_fork(); if ($pid == 0) {
/** * Combination getter/setter for the $loop_interval property. * @param boolean $set_value * @return int|null */ protected function loop_interval($set_value = null) { if ($set_value === null) { return $this->loop_interval; } if (!is_numeric($set_value)) { throw new Exception(__METHOD__ . ' Failed. Could not set loop interval. Number Expected. Given: ' . $set_value); } $this->loop_interval = $set_value; $priority = -1; if ($set_value >= 5.0 || $set_value <= 0.0) { $priority = 0; } if ($priority == pcntl_getpriority()) { return; } @pcntl_setpriority($priority); if (pcntl_getpriority() == $priority) { $this->log('Adjusting Process Priority to ' . $priority); } else { $this->log("Warning: At configured loop_interval a process priorty of `{$priority}` is suggested but this process does not have setpriority privileges.\n" . " Consider running the daemon with `CAP_SYS_RESOURCE` privileges or set it manually using `sudo renice -n {$priority} -p {$this->pid}`"); } }
/** * Checks if the creator of the heavy thread is alive. * @return bool Returns true if the parent is alive otherwise returns false. */ public function isParentAlive() { // {{{ return @pcntl_getpriority(@posix_getppid(), PRIO_PROCESS) !== false; }
/** * @memcheck */ public function testGetPriority() { $this->assertSame(pcntl_getpriority(), Process::getPriority()); $this->assertSame(pcntl_getpriority(posix_getppid()), Process::getPriority(posix_getppid())); }
/** * @return integer * * @throws ProcessException If an error occurs. */ public function getPriority() { if (false === ($priority = pcntl_getpriority($this->pid))) { throw ProcessException::getPriorityError(); } return $priority; }