More reliable system variables

This commit is contained in:
Michael 2023-03-15 21:16:21 +00:00
parent a41e2eb50b
commit 54d5f2df43
2 changed files with 34 additions and 32 deletions

View 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;
}
/**

View 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;