]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge remote-tracking branch 'upstream/develop' into 1701-performance
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 use \Friendica\Core\Config;
14 use \Friendica\Core\PConfig;
15
16 require_once("boot.php");
17
18 function poller_run($argv, $argc){
19         global $a, $db;
20
21         if(is_null($a)) {
22                 $a = new App;
23         }
24
25         if(is_null($db)) {
26                 @include(".htconfig.php");
27                 require_once("include/dba.php");
28                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
29                 unset($db_host, $db_user, $db_pass, $db_data);
30         };
31
32         // Quit when in maintenance
33         if (Config::get('system', 'maintenance', true)) {
34                 return;
35         }
36
37         $a->start_process();
38
39         if (poller_max_connections_reached()) {
40                 return;
41         }
42
43         if ($a->maxload_reached()) {
44                 return;
45         }
46
47         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
48                 poller_run_cron();
49         }
50
51         if ($a->max_processes_reached()) {
52                 return;
53         }
54
55         // Checking the number of workers
56         if (poller_too_much_workers()) {
57                 poller_kill_stale_workers();
58                 return;
59         }
60
61         $starttime = time();
62
63         while ($r = poller_worker_process()) {
64
65                 // Count active workers and compare them with a maximum value that depends on the load
66                 if (poller_too_much_workers()) {
67                         return;
68                 }
69
70                 if (!poller_execute($r[0])) {
71                         return;
72                 }
73
74                 // Quit the poller once every hour
75                 if (time() > ($starttime + 3600))
76                         return;
77         }
78
79 }
80
81 /**
82  * @brief Execute a worker entry
83  *
84  * @param array $queue Workerqueue entry
85  *
86  * @return boolean "true" if further processing should be stopped
87  */
88 function poller_execute($queue) {
89
90         $a = get_app();
91
92         $mypid = getmypid();
93
94         // Quit when in maintenance
95         if (Config::get('system', 'maintenance', true)) {
96                 return false;
97         }
98
99         // Constantly check the number of parallel database processes
100         if ($a->max_processes_reached()) {
101                 return false;
102         }
103
104         // Constantly check the number of available database connections to let the frontend be accessible at any time
105         if (poller_max_connections_reached()) {
106                 return false;
107         }
108
109         $upd = q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `pid` = 0",
110                 dbesc(datetime_convert()),
111                 intval($mypid),
112                 intval($queue["id"]));
113
114         if (!$upd) {
115                 logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
116                 q("COMMIT");
117                 return true;
118         }
119
120         // Assure that there are no tasks executed twice
121         $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
122         if (!$id) {
123                 logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
124                 q("COMMIT");
125                 return true;
126         } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
127                 logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
128                 q("COMMIT");
129                 return true;
130         } elseif ($id[0]["pid"] != $mypid) {
131                 logger("Queue item ".$queue["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
132                 q("COMMIT");
133                 return true;
134         }
135         q("COMMIT");
136
137         $argv = json_decode($queue["parameter"]);
138
139         // Check for existance and validity of the include file
140         $include = $argv[0];
141
142         if (!validate_include($include)) {
143                 logger("Include file ".$argv[0]." is not valid!");
144                 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
145                 return true;
146         }
147
148         require_once($include);
149
150         $funcname = str_replace(".php", "", basename($argv[0]))."_run";
151
152         if (function_exists($funcname)) {
153
154                 poller_exec_function($queue, $funcname, $argv);
155
156                 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
157         } else {
158                 logger("Function ".$funcname." does not exist");
159         }
160
161         return true;
162 }
163
164 /**
165  * @brief Execute a function from the queue
166  *
167  * @param array $queue Workerqueue entry
168  * @param string $funcname name of the function
169  * @param array $argv Array of values to be passed to the function
170  */
171 function poller_exec_function($queue, $funcname, $argv) {
172
173         $a = get_app();
174
175         $mypid = getmypid();
176
177         $argc = count($argv);
178
179         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]);
180
181         $stamp = (float)microtime(true);
182
183         // We use the callstack here to analyze the performance of executed worker entries.
184         // For this reason the variables have to be initialized.
185         if (Config::get("system", "profiler")) {
186                 $a->performance["start"] = microtime(true);
187                 $a->performance["database"] = 0;
188                 $a->performance["database_write"] = 0;
189                 $a->performance["network"] = 0;
190                 $a->performance["file"] = 0;
191                 $a->performance["rendering"] = 0;
192                 $a->performance["parser"] = 0;
193                 $a->performance["marktime"] = 0;
194                 $a->performance["markstart"] = microtime(true);
195                 $a->callstack = array();
196         }
197
198         // For better logging create a new process id for every worker call
199         // But preserve the old one for the worker
200         $old_process_id = $a->process_id;
201         $a->process_id = uniqid("wrk", true);
202
203         $funcname($argv, $argc);
204
205         $a->process_id = $old_process_id;
206
207         $duration = number_format(microtime(true) - $stamp, 3);
208
209         if ($duration > 3600) {
210                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
211         } elseif ($duration > 600) {
212                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
213         } elseif ($duration > 300) {
214                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
215         } elseif ($duration > 120) {
216                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
217         }
218
219         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds.");
220
221         // Write down the performance values into the log
222         if (Config::get("system", "profiler")) {
223                 $duration = microtime(true)-$a->performance["start"];
224
225                 if (Config::get("rendertime", "callstack")) {
226                         if (isset($a->callstack["database"])) {
227                                 $o = "\nDatabase Read:\n";
228                                 foreach ($a->callstack["database"] AS $func => $time) {
229                                         $time = round($time, 3);
230                                         if ($time > 0)
231                                                 $o .= $func.": ".$time."\n";
232                                 }
233                         }
234                         if (isset($a->callstack["database_write"])) {
235                                 $o .= "\nDatabase Write:\n";
236                                 foreach ($a->callstack["database_write"] AS $func => $time) {
237                                         $time = round($time, 3);
238                                         if ($time > 0)
239                                                 $o .= $func.": ".$time."\n";
240                                 }
241                         }
242                         if (isset($a->callstack["network"])) {
243                                 $o .= "\nNetwork:\n";
244                                 foreach ($a->callstack["network"] AS $func => $time) {
245                                         $time = round($time, 3);
246                                         if ($time > 0)
247                                                 $o .= $func.": ".$time."\n";
248                                 }
249                         }
250                 } else {
251                         $o = '';
252                 }
253
254                 logger("ID ".$queue["id"].": ".$funcname.": ".sprintf("DB: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
255                         number_format($a->performance["database"] - $a->performance["database_write"], 2),
256                         number_format($a->performance["database_write"], 2),
257                         number_format($a->performance["network"], 2),
258                         number_format($a->performance["file"], 2),
259                         number_format($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2),
260                         number_format($duration, 2)),
261                         LOGGER_DEBUG);
262         }
263
264         $cooldown = Config::get("system", "worker_cooldown", 0);
265
266         if ($cooldown > 0) {
267                 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
268                 sleep($cooldown);
269         }
270 }
271
272 /**
273  * @brief Checks if the number of database connections has reached a critical limit.
274  *
275  * @return bool Are more than 3/4 of the maximum connections used?
276  */
277 function poller_max_connections_reached() {
278
279         // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
280         $max = Config::get("system", "max_connections");
281
282         // Fetch the percentage level where the poller will get active
283         $maxlevel = Config::get("system", "max_connections_level", 75);
284
285         if ($max == 0) {
286                 // the maximum number of possible user connections can be a system variable
287                 $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
288                 if ($r)
289                         $max = $r[0]["Value"];
290
291                 // Or it can be granted. This overrides the system variable
292                 $r = q("SHOW GRANTS");
293                 if ($r)
294                         foreach ($r AS $grants) {
295                                 $grant = array_pop($grants);
296                                 if (stristr($grant, "GRANT USAGE ON"))
297                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match))
298                                                 $max = $match[1];
299                         }
300         }
301
302         // If $max is set we will use the processlist to determine the current number of connections
303         // The processlist only shows entries of the current user
304         if ($max != 0) {
305                 $r = q("SHOW PROCESSLIST");
306                 if (!dbm::is_result($r))
307                         return false;
308
309                 $used = count($r);
310
311                 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
312
313                 $level = ($used / $max) * 100;
314
315                 if ($level >= $maxlevel) {
316                         logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
317                         return true;
318                 }
319         }
320
321         // We will now check for the system values.
322         // This limit could be reached although the user limits are fine.
323         $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
324         if (!$r)
325                 return false;
326
327         $max = intval($r[0]["Value"]);
328         if ($max == 0)
329                 return false;
330
331         $r = q("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
332         if (!$r)
333                 return false;
334
335         $used = intval($r[0]["Value"]);
336         if ($used == 0)
337                 return false;
338
339         logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
340
341         $level = $used / $max * 100;
342
343         if ($level < $maxlevel)
344                 return false;
345
346         logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
347         return true;
348 }
349
350 /**
351  * @brief fix the queue entry if the worker process died
352  *
353  */
354 function poller_kill_stale_workers() {
355         $r = q("SELECT `pid`, `executed`, `priority`, `parameter` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
356
357         if (!dbm::is_result($r)) {
358                 // No processing here needed
359                 return;
360         }
361
362         foreach($r AS $pid)
363                 if (!posix_kill($pid["pid"], 0))
364                         q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
365                                 intval($pid["pid"]));
366                 else {
367                         // Kill long running processes
368
369                         // Check if the priority is in a valid range
370                         if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE)))
371                                 $pid["priority"] = PRIORITY_MEDIUM;
372
373                         // Define the maximum durations
374                         $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
375                         $max_duration = $max_duration_defaults[$pid["priority"]];
376
377                         $argv = json_decode($pid["parameter"]);
378                         $argv[0] = basename($argv[0]);
379
380                         // How long is the process already running?
381                         $duration = (time() - strtotime($pid["executed"])) / 60;
382                         if ($duration > $max_duration) {
383                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
384                                 posix_kill($pid["pid"], SIGTERM);
385
386                                 // We killed the stale process.
387                                 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
388                                 // Additionally we are lowering the priority.
389                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `created` = '%s',
390                                                         `priority` = %d, `pid` = 0 WHERE `pid` = %d",
391                                         dbesc(datetime_convert()),
392                                         intval(PRIORITY_NEGLIGIBLE),
393                                         intval($pid["pid"]));
394                         } else
395                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
396                 }
397 }
398
399 /**
400  * @brief Checks if the number of active workers exceeds the given limits
401  *
402  * @return bool Are there too much workers running?
403  */
404 function poller_too_much_workers() {
405         $queues = Config::get("system", "worker_queues", 4);
406
407         $maxqueues = $queues;
408
409         $active = poller_active_workers();
410
411         // Decrease the number of workers at higher load
412         $load = current_load();
413         if($load) {
414                 $maxsysload = intval(Config::get("system", "maxloadavg", 50));
415
416                 $maxworkers = $queues;
417
418                 // Some magical mathemathics to reduce the workers
419                 $exponent = 3;
420                 $slope = $maxworkers / pow($maxsysload, $exponent);
421                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
422
423                 $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00'");
424                 $entries = $s[0]["total"];
425
426                 if (Config::get("system", "worker_fastlane", false) AND ($queues > 0) AND ($entries > 0) AND ($active >= $queues)) {
427                         $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority` LIMIT 1");
428                         $top_priority = $s[0]["priority"];
429
430                         $s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` != '0000-00-00 00:00:00' LIMIT 1",
431                                 intval($top_priority));
432                         $high_running = dbm::is_result($s);
433
434                         if (!$high_running AND ($top_priority > PRIORITY_UNDEFINED) AND ($top_priority < PRIORITY_NEGLIGIBLE)) {
435                                 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
436                                 $queues = $active + 1;
437                         }
438                 }
439
440                 // Create a list of queue entries grouped by their priority
441                 $running = array(PRIORITY_CRITICAL => 0,
442                                 PRIORITY_HIGH => 0,
443                                 PRIORITY_MEDIUM => 0,
444                                 PRIORITY_LOW => 0,
445                                 PRIORITY_NEGLIGIBLE => 0);
446
447                 $r = q("SELECT COUNT(*) AS `running`, `priority` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` GROUP BY `priority`");
448                 if (dbm::is_result($r))
449                         foreach ($r AS $process)
450                                 $running[$process["priority"]] = $process["running"];
451
452                 $processlist = "";
453                 $r = q("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` GROUP BY `priority`");
454                 if (dbm::is_result($r))
455                         foreach ($r as $entry) {
456                                 if ($processlist != "")
457                                         $processlist .= ", ";
458                                 $processlist .= $entry["priority"].":".$running[$entry["priority"]]."/".$entry["entries"];
459                         }
460
461                 logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries." (".$processlist.") - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
462
463                 // Are there fewer workers running as possible? Then fork a new one.
464                 if (!Config::get("system", "worker_dont_fork") AND ($queues > ($active + 1)) AND ($entries > 1)) {
465                         logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
466                         $args = array("php", "include/poller.php", "no_cron");
467                         $a = get_app();
468                         $a->proc_run($args);
469                 }
470         }
471
472         return($active >= $queues);
473 }
474
475 /**
476  * @brief Returns the number of active poller processes
477  *
478  * @return integer Number of active poller processes
479  */
480 function poller_active_workers() {
481         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
482
483         return($workers[0]["processes"]);
484 }
485
486 /**
487  * @brief Check if we should pass some slow processes
488  *
489  * When the active processes of the highest priority are using more than 2/3
490  * of all processes, we let pass slower processes.
491  *
492  * @param string $highest_priority Returns the currently highest priority
493  * @return bool We let pass a slower process than $highest_priority
494  */
495 function poller_passing_slow(&$highest_priority) {
496
497         $highest_priority = 0;
498
499         $r = q("SELECT `priority`
500                 FROM `process`
501                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid`");
502
503         // No active processes at all? Fine
504         if (!dbm::is_result($r))
505                 return(false);
506
507         $priorities = array();
508         foreach ($r AS $line)
509                 $priorities[] = $line["priority"];
510
511         // Should not happen
512         if (count($priorities) == 0)
513                 return(false);
514
515         $highest_priority = min($priorities);
516
517         // The highest process is already the slowest one?
518         // Then we quit
519         if ($highest_priority == PRIORITY_NEGLIGIBLE)
520                 return(false);
521
522         $high = 0;
523         foreach ($priorities AS $priority)
524                 if ($priority == $highest_priority)
525                         ++$high;
526
527         logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
528         $passing_slow = (($high/count($priorities)) > (2/3));
529
530         if ($passing_slow)
531                 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
532
533         return($passing_slow);
534 }
535
536 /**
537  * @brief Returns the next worker process
538  *
539  * @return string SQL statement
540  */
541 function poller_worker_process() {
542
543         q("START TRANSACTION;");
544
545         // Check if we should pass some low priority process
546         $highest_priority = 0;
547
548         if (poller_passing_slow($highest_priority)) {
549                 // Are there waiting processes with a higher priority than the currently highest?
550                 $r = q("SELECT * FROM `workerqueue`
551                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` < %d
552                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
553                 if (dbm::is_result($r))
554                         return $r;
555
556                 // Give slower processes some processing time
557                 $r = q("SELECT * FROM `workerqueue`
558                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` > %d
559                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
560         }
561
562         // If there is no result (or we shouldn't pass lower processes) we check without priority limit
563         if (($highest_priority == 0) OR !dbm::is_result($r))
564                 $r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority`, `created` LIMIT 1");
565
566         return $r;
567 }
568
569 /**
570  * @brief Call the front end worker
571  */
572 function call_worker() {
573         if (!Config::get("system", "frontend_worker") OR !Config::get("system", "worker")) {
574                 return;
575         }
576
577         $url = App::get_baseurl()."/worker";
578         fetch_url($url, false, $redirects, 1);
579 }
580
581 /**
582  * @brief Call the front end worker if there aren't any active
583  */
584 function call_worker_if_idle() {
585         if (!Config::get("system", "frontend_worker") OR !Config::get("system", "worker")) {
586                 return;
587         }
588
589         // Do we have "proc_open"? Then we can fork the poller
590         if (function_exists("proc_open")) {
591                 // When was the last time that we called the worker?
592                 // Less than one minute? Then we quit
593                 if ((time() - Config::get("system", "worker_started")) < 60) {
594                         return;
595                 }
596
597                 set_config("system", "worker_started", time());
598
599                 // Do we have enough running workers? Then we quit here.
600                 if (poller_too_much_workers()) {
601                         // Cleaning dead processes
602                         poller_kill_stale_workers();
603                         get_app()->remove_inactive_processes();
604
605                         return;
606                 }
607
608                 poller_run_cron();
609
610                 logger('Call poller', LOGGER_DEBUG);
611
612                 $args = array("php", "include/poller.php", "no_cron");
613                 $a = get_app();
614                 $a->proc_run($args);
615                 return;
616         }
617
618         // We cannot execute background processes.
619         // We now run the processes from the frontend.
620         // This won't work with long running processes.
621         poller_run_cron();
622
623         clear_worker_processes();
624
625         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
626
627         if ($workers[0]["processes"] == 0) {
628                 call_worker();
629         }
630 }
631
632 /**
633  * @brief Removes long running worker processes
634  */
635 function clear_worker_processes() {
636         $timeout = Config::get("system", "frontend_worker_timeout", 10);
637
638         /// @todo We should clean up the corresponding workerqueue entries as well
639         q("DELETE FROM `process` WHERE `created` < '%s' AND `command` = 'worker.php'",
640                 dbesc(datetime_convert('UTC','UTC',"now - ".$timeout." minutes")));
641 }
642
643 /**
644  * @brief Runs the cron processes
645  */
646 function poller_run_cron() {
647         logger('Add cron entries', LOGGER_DEBUG);
648
649         // Check for spooled items
650         proc_run(PRIORITY_HIGH, "include/spool_post.php");
651
652         // Run the cron job that calls all other jobs
653         proc_run(PRIORITY_MEDIUM, "include/cron.php");
654
655         // Run the cronhooks job separately from cron for being able to use a different timing
656         proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
657
658         // Cleaning dead processes
659         poller_kill_stale_workers();
660 }
661
662 if (array_search(__file__,get_included_files())===0){
663         poller_run($_SERVER["argv"],$_SERVER["argc"]);
664
665         get_app()->end_process();
666
667         killme();
668 }
669 ?>