summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/linux-health/src/Cpu.php
blob: 43f2e5089008eadb0d3c32437069f9e5e0c21906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace gipfl\LinuxHealth;

class Cpu
{
    public static function getCounters($procFile = '/proc/stat')
    {
        $info = [];
        $cpus = [];

        $cpuKeys = [     // From 'man proc':
            'user',      // Time spent in user mode.
            'nice',      // Time spent in user mode with low priority (nice).
            'system',    // Time spent in system mode.
            'idle',      // Time spent in the idle task.
            // This value should be USER_HZ times the second entry in the
            // /proc/uptime pseudo-file.
            'iowait',    // Time waiting for I/O to complete. (Linux >= 2.5.41)
            'irq',       // Time servicing interrupts. (Linux >= 2.6.0-test4)
            'softirq',   // Time servicing softirqs. (Linux >= 2.6.0-test4)
            'steal',     // Stolen time, which is the time spent in other operating
            // systems when running in a virtualized environment
            // (Linux >= 2.6.11)
            'guest',     // Time spent running a virtual CPU for guest operating systems
            // under the control of the Linux kernel. (Linux >= 2.6.24)
            'guest_nice' // Time spent running a niced guest (virtual CPU for guest
            // operating systems under the control of the Linux kernel).
            // (Linux >= 2.6.33)
        ];

        // TODO:
        // ctxt 891299797    -> The number of context switches that the system underwent
        // btime 1540828526  -> boot   time,  in  seconds  since  the  Epoch
        // processes 2079015 -> Number of forks since boot
        // procs_running 6   -> Number of processes in  runnable  state
        // procs_blocked 0   -> Number  of processes blocked waiting for I/O to complete

        foreach (file($procFile, FILE_IGNORE_NEW_LINES) as $line) {
            $parts = preg_split('/\s+/', $line);
            $key = array_shift($parts);
            if (substr($key, 0, 3) === 'cpu') {
                // TODO: handle count mismatch
                $cpus[$key] = array_combine(
                    array_slice($cpuKeys, 0, count($parts), true),
                    $parts
                );

                for ($i = count($cpus[$key]) - 1; $i < count($cpuKeys); $i++) {
                    $cpus[$key][$cpuKeys[$i]] = 0;
                }
            } else {
                $info[$key] = $parts;
            }
        }

        return $cpus;
    }
}