From 54d5f2df4313a93abaa098d99a8603f9bd096379 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 15 Mar 2023 21:16:21 +0000 Subject: [PATCH] More reliable system variables --- src/Core/System.php | 40 +++++++++++++++++----------------------- src/Core/Worker.php | 26 +++++++++++++++++--------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/Core/System.php b/src/Core/System.php index 107303d6a..bf3f247dd 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -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; } /** diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 9d1fde85c..70ba0ffe4 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -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;