]> git.mxchange.org Git - friendica.git/commitdiff
More reliable system variables
authorMichael <heluecht@pirati.ca>
Wed, 15 Mar 2023 21:16:21 +0000 (21:16 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 15 Mar 2023 21:16:21 +0000 (21:16 +0000)
src/Core/System.php
src/Core/Worker.php

index 107303d6a6c6b3a6bf8f9b41562663e590f3073d..bf3f247ddd41d65f14b6ad65f4aba2af8f05f682 100644 (file)
@@ -445,34 +445,28 @@ class System
         */
        public static function getLoadAvg(bool $get_processes = true): array
        {
+               $load_arr = sys_getloadavg();
+               if (empty($load_arr)) {
+                       return [];
+               }
+
+               $load = [
+                       'average1'  => $load_arr[0],
+                       'average5'  => $load_arr[1],
+                       'average15' => $load_arr[2],
+                       'runnable'  => 0,
+                       'scheduled' => 0
+               ];
+
                if ($get_processes && @is_readable('/proc/loadavg')) {
                        $content = @file_get_contents('/proc/loadavg');
-                       if (empty($content)) {
-                               $content = shell_exec('uptime | sed "s/.*averages*: //" | sed "s/,//g"');
+                       if (!empty($content) && preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
+                               $load['runnable']  = (float)$matches[4];
+                               $load['scheduled'] = (float)$matches[5];
                        }
                }
 
-               if (empty($content) || !preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
-                       $load_arr = sys_getloadavg();
-                       if (empty($load_arr)) {
-                               return [];
-                       }
-                       return [
-                               'average1'  => $load_arr[0],
-                               'average5'  => $load_arr[1],
-                               'average15' => $load_arr[2],
-                               'runnable'  => 0,
-                               'scheduled' => 0
-                       ];
-               }
-
-               return [
-                       'average1'  => (float)$matches[1],
-                       'average5'  => (float)$matches[2],
-                       'average15' => (float)$matches[3],
-                       'runnable'  => (float)$matches[4],
-                       'scheduled' => (float)$matches[5]
-               ];
+               return $load;
        }
 
        /**
index 9d1fde85c903a97847972716ae027f490df46c4d..70ba0ffe4930371bf9375979fefedbbbbc3b1952 100644 (file)
@@ -662,16 +662,24 @@ class Worker
                        DBA::close($r);
                }
 
+               $stamp = (float)microtime(true);
+               $used  = 0;
+               $sleep = 0;
+               $data = DBA::p("SHOW PROCESSLIST");
+               while ($row = DBA::fetch($data)) {
+                       if ($row['Command'] != 'Sleep') {
+                               ++$used;
+                       } else {
+                               ++$sleep;
+                       }
+               }
+               DBA::close($data);
+               self::$db_duration += (microtime(true) - $stamp);
+
                // If $max is set we will use the processlist to determine the current number of connections
                // The processlist only shows entries of the current user
                if ($max != 0) {
-                       $stamp = (float)microtime(true);
-                       $r = DBA::p('SHOW PROCESSLIST');
-                       self::$db_duration += (microtime(true) - $stamp);
-                       $used = DBA::numRows($r);
-                       DBA::close($r);
-
-                       Logger::info('Connection usage (user values)', ['usage' => $used, 'max' => $max]);
+                       Logger::info('Connection usage (user values)', ['working' => $used, 'sleeping' => $sleep, 'max' => $max]);
 
                        $level = ($used / $max) * 100;
 
@@ -695,11 +703,11 @@ class Worker
                if (!DBA::isResult($r)) {
                        return false;
                }
-               $used = intval($r['Value']);
+               $used = max($used, intval($r['Value'])) - $sleep;
                if ($used == 0) {
                        return false;
                }
-               Logger::info('Connection usage (system values)', ['used' => $used, 'max' => $max]);
+               Logger::info('Connection usage (system values)', ['working' => $used, 'sleeping' => $sleep, 'max' => $max]);
 
                $level = $used / $max * 100;