Linux::_parseProcStat PHP Method

_parseProcStat() protected method

fill the load for a individual cpu, through parsing /proc/stat for the specified cpu
protected _parseProcStat ( String $cpuline ) : integer
$cpuline String cpu for which load should be meassured
return integer
    protected function _parseProcStat($cpuline)
    {
        if (is_null($this->_cpu_loads)) {
            $this->_cpu_loads = array();
            if (CommonFunctions::rfts('/proc/stat', $buf)) {
                if (preg_match_all('/^(cpu[0-9]*) (.*)/m', $buf, $matches, PREG_SET_ORDER)) {
                    foreach ($matches as $line) {
                        $cpu = $line[1];
                        $buf2 = $line[2];
                        $this->_cpu_loads[$cpu] = array();
                        $ab = 0;
                        $ac = 0;
                        $ad = 0;
                        $ae = 0;
                        sscanf($buf2, "%Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
                        $this->_cpu_loads[$cpu]['load'] = $ab + $ac + $ad;
                        // cpu.user + cpu.sys
                        $this->_cpu_loads[$cpu]['total'] = $ab + $ac + $ad + $ae;
                        // cpu.total
                    }
                }
            }
            // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
            if (PSI_LOAD_BAR) {
                sleep(1);
            }
            if (CommonFunctions::rfts('/proc/stat', $buf)) {
                if (preg_match_all('/^(cpu[0-9]*) (.*)/m', $buf, $matches, PREG_SET_ORDER)) {
                    foreach ($matches as $line) {
                        $cpu = $line[1];
                        $buf2 = $line[2];
                        $ab = 0;
                        $ac = 0;
                        $ad = 0;
                        $ae = 0;
                        sscanf($buf2, "%Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
                        $load2 = $ab + $ac + $ad;
                        // cpu.user + cpu.sys
                        $total2 = $ab + $ac + $ad + $ae;
                        // cpu.total
                        $total = $this->_cpu_loads[$cpu]['total'];
                        $load = $this->_cpu_loads[$cpu]['load'];
                        $this->_cpu_loads[$cpu] = 0;
                        if ($total > 0 && $total2 > 0 && $load > 0 && $load2 > 0 && $total2 != $total && $load2 != $load) {
                            $this->_cpu_loads[$cpu] = 100 * ($load2 - $load) / ($total2 - $total);
                        }
                    }
                }
            }
        }
        if (isset($this->_cpu_loads[$cpuline])) {
            return $this->_cpu_loads[$cpuline];
        }
        return 0;
    }