X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FCore%2FWorker.php;h=897a80111d67d4d6cc6c7309c8ee645ae3894d02;hb=1b6727fb9c1aa31a606af0b2b7d7153605fb838c;hp=bc45be2d5b75da128966c7d98d56d0cb3dda2315;hpb=00124f9ac09429d311eb7f171cf5356b1d28ce48;p=friendica.git diff --git a/src/Core/Worker.php b/src/Core/Worker.php index bc45be2d5b..897a80111d 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -4,15 +4,11 @@ */ namespace Friendica\Core; -use Friendica\Core\Addon; -use Friendica\Core\Config; -use Friendica\Core\System; +use dba; use Friendica\Database\DBM; use Friendica\Model\Process; use Friendica\Util\DateTimeFormat; -use Friendica\Util\Lock; use Friendica\Util\Network; -use dba; require_once 'include/dba.php'; @@ -45,7 +41,7 @@ class Worker self::$up_start = microtime(true); // At first check the maximum load. We shouldn't continue with a high load - if ($a->maxload_reached()) { + if ($a->isMaxLoadReached()) { logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG); return; } @@ -79,7 +75,7 @@ class Worker } // Possibly there are too much database processes that block the system - if ($a->max_processes_reached()) { + if ($a->isMaxProcessesReached()) { logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG); return; } @@ -108,16 +104,16 @@ class Worker } // If possible we will fetch new jobs for this worker - if (!$refetched && Lock::set('worker_process', 0)) { + if (!$refetched && Lock::acquire('worker_process', 0)) { $stamp = (float)microtime(true); $refetched = self::findWorkerProcesses($passing_slow); self::$db_duration += (microtime(true) - $stamp); - Lock::remove('worker_process'); + Lock::release('worker_process'); } } // To avoid the quitting of multiple workers only one worker at a time will execute the check - if (Lock::set('worker', 0)) { + if (Lock::acquire('worker', 0)) { $stamp = (float)microtime(true); // Count active workers and compare them with a maximum value that depends on the load if (self::tooMuchWorkers()) { @@ -130,7 +126,7 @@ class Worker logger('Memory limit reached, quitting.', LOGGER_DEBUG); return; } - Lock::remove('worker'); + Lock::release('worker'); self::$db_duration += (microtime(true) - $stamp); } @@ -145,7 +141,7 @@ class Worker if (Config::get('system', 'worker_daemon_mode', false)) { self::IPCSetJobState(false); } - logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG); + logger("Couldn't select a workerqueue entry, quitting process " . getmypid() . ".", LOGGER_DEBUG); } /** @@ -207,7 +203,7 @@ class Worker } // Constantly check the number of parallel database processes - if ($a->max_processes_reached()) { + if ($a->isMaxProcessesReached()) { logger("Max processes reached for process ".$mypid, LOGGER_DEBUG); return false; } @@ -313,7 +309,7 @@ class Worker $argc = count($argv); - $new_process_id = uniqid("wrk", true); + $new_process_id = System::processID("wrk"); logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id); @@ -356,7 +352,7 @@ class Worker $a->process_id = $old_process_id; unset($a->queue); - $duration = number_format(microtime(true) - $stamp, 3); + $duration = (microtime(true) - $stamp); self::$up_start = microtime(true); @@ -601,7 +597,7 @@ class Worker ['id' => $entry["id"]] ); } else { - logger("Worker process ".$entry["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG); + logger("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG); } } } @@ -625,34 +621,14 @@ class Worker if ($load) { $maxsysload = intval(Config::get("system", "maxloadavg", 50)); - if (Config::get('system', 'worker_linear_load', false)) { - /* The linear load calculation works fine if there is a low - * number of maximum queues and a high load base level. - * This can be present at shared hosters. - */ - $tinyload = 1; - - if ($load > $maxsysload) { - $queues = 0; - } elseif ($load > $tinyload) { - //Provide $queues number between 1 (below max load) and $maxqueues - 1 (above tiny load). - $range = $maxsysload - $tinyload; - $slope = 1.00 - (($load - $tinyload) / $range); - $target = $slope * ($maxqueues - 1); - $queues = intval(ceil($target)); - } - } else { - /* The exponentional load calculation respects the load behaviour - * of Linux systems with regular hardware that normally idles - * with load values near 0. - */ - $maxworkers = $queues; - - // Some magical mathemathics to reduce the workers - $exponent = 3; - $slope = $maxworkers / pow($maxsysload, $exponent); - $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent)); - } + /* Default exponent 3 causes queues to rapidly decrease as load increases. + * If you have 20 max queues at idle, then you get only 5 queues at 37.1% of $maxsysload. + * For some environments, this rapid decrease is not needed. + * With exponent 1, you could have 20 max queues at idle and 13 at 37% of $maxsysload. + */ + $exponent = intval(Config::get('system', 'worker_load_exponent', 3)); + $slope = pow(max(0, $maxsysload - $load) / $maxsysload, $exponent); + $queues = intval(ceil($slope * $maxqueues)); $processlist = ''; @@ -903,7 +879,7 @@ class Worker dba::close($r); $stamp = (float)microtime(true); - if (!Lock::set('worker_process')) { + if (!Lock::acquire('worker_process')) { return false; } self::$lock_duration = (microtime(true) - $stamp); @@ -912,7 +888,7 @@ class Worker $found = self::findWorkerProcesses($passing_slow); self::$db_duration += (microtime(true) - $stamp); - Lock::remove('worker_process'); + Lock::release('worker_process'); if ($found) { $r = dba::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]); @@ -1117,13 +1093,13 @@ class Worker } // If there is a lock then we don't have to check for too much worker - if (!Lock::set('worker', 0)) { + if (!Lock::acquire('worker', 0)) { return true; } // If there are already enough workers running, don't fork another one $quit = self::tooMuchWorkers(); - Lock::remove('worker'); + Lock::release('worker'); if ($quit) { return true;