Subversion Repositories ALCASAR

Rev

Rev 3037 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2976 rexy 1
<?php
2
/**
3
 * nvidiasmi sensor class, getting hardware temperature information and fan speed from nvidia-smi utility
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
10
 * @copyright 2020 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   Release: 3.0
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
class NvidiaSMI extends Sensors
16
{
17
    /**
18
     * content to parse
19
     *
20
     * @var array
21
     */
22
    private $_gpus = array();
23
 
24
    /**
25
     * fill the private array
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        if (!defined('PSI_EMU_HOSTNAME')) switch (defined('PSI_SENSOR_NVIDIASMI_ACCESS')?strtolower(PSI_SENSOR_NVIDIASMI_ACCESS):'command') {
31
        case 'command':
32
            if (PSI_OS == 'WINNT') {
33
                $winnt_exe = (defined('PSI_SENSOR_NVIDIASMI_EXE_PATH') && is_string(PSI_SENSOR_NVIDIASMI_EXE_PATH))?strtolower(PSI_SENSOR_NVIDIASMI_EXE_PATH):"c:\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi.exe";
34
                if (($_exe=realpath(trim($winnt_exe))) && preg_match("/^([a-zA-Z]:\\\\[^\\\\]+)/", $_exe, $out)) {
35
                    CommonFunctions::executeProgram('cmd', "/c set ProgramFiles=".$out[1]."^&\"".$_exe."\" -q", $lines);
36
                } else {
37
                    $this->error->addConfigError('__construct()', '[sensor_nvidiasmi] EXE_PATH="'.$winnt_exe.'"');
38
                }
39
            } else {
40
                CommonFunctions::executeProgram('nvidia-smi', '-q', $lines);
41
            }
42
 
43
            $this->_gpus = preg_split("/^(?=GPU )/m", $lines, -1, PREG_SPLIT_NO_EMPTY);
44
            break;
45
        case 'data':
46
            if (CommonFunctions::rfts(PSI_APP_ROOT.'/data/nvidiasmi.txt', $lines)) {
47
                $this->_gpus = preg_split("/^(?=GPU )/m", $lines, -1, PREG_SPLIT_NO_EMPTY);
48
            }
49
            break;
50
        default:
51
            $this->error->addConfigError('__construct()', '[sensor_nvidiasmi] ACCESS');
52
            break;
53
        }
54
    }
55
 
56
    /**
57
     * get the information
58
     *
59
     * @see PSI_Interface_Sensor::build()
60
     *
61
     * @return Void
62
     */
63
    public function build()
64
    {
65
        $gpuc=count($this->_gpus);
66
        switch ($gpuc) {
67
            case 0:
68
                $this->error->addError("nvidia-smi", "No values");
69
                break;
70
            case 1:
71
                $this->error->addError("nvidia-smi", "Error: ".$this->_gpus[0]);
72
                break;
73
            default:
74
                for ($c = 0; $c < $gpuc; $c++) {
75
                    if (preg_match("/^\s+GPU Current Temp\s+:\s*(\d+)\s*C\s*$/m", $this->_gpus[$c], $out)) {
76
                        $dev = new SensorDevice();
77
                        $dev->setName("GPU ".($c)." (nvidiasmi)");
78
                        $dev->setValue($out[1]);
79
                        if (preg_match("/^\s+GPU Shutdown Temp\s+:\s*(\d+)\s*C\s*$/m", $this->_gpus[$c], $out)) {
80
                            $dev->setMax($out[1]);
81
                        }
82
                        $this->mbinfo->setMbTemp($dev);
83
                    }
84
                    if (preg_match("/^\s+Power Draw\s+:\s*([\d\.]+)\s*W\s*$/m", $this->_gpus[$c], $out)) {
85
                        $dev = new SensorDevice();
86
                        $dev->setName("GPU ".($c)." (nvidiasmi)");
87
                        $dev->setValue($out[1]);
88
                        if (preg_match("/^\s+Power Limit\s+:\s*([\d\.]+)\s*W\s*$/m", $this->_gpus[$c], $out)) {
89
                            $dev->setMax($out[1]);
90
                        }
91
                        $this->mbinfo->setMbPower($dev);
92
                    }
93
                    if (preg_match("/^\s+Fan Speed\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
94
                        $dev = new SensorDevice();
95
                        $dev->setName("GPU ".($c)." (nvidiasmi)");
96
                        $dev->setValue($out[1]);
97
                        $dev->setUnit("%");
98
                        $this->mbinfo->setMbFan($dev);
99
                    }
100
                    if (preg_match("/^\s+Performance State\s+:\s*(\S+)\s*$/m", $this->_gpus[$c], $out)) {
101
                        $dev = new SensorDevice();
102
                        $dev->setName("GPU ".($c)." Performance State (nvidiasmi)");
103
                        $dev->setValue($out[1]);
104
                        $this->mbinfo->setMbOther($dev);
105
                    }
106
                    if (preg_match("/^\s+Gpu\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
107
                        $dev = new SensorDevice();
108
                        $dev->setName("GPU ".($c)." Utilization (nvidiasmi)");
109
                        $dev->setValue($out[1]);
110
                        $dev->setUnit("%");
111
                        $this->mbinfo->setMbOther($dev);
112
                    }
113
                    if (preg_match("/^\s+Memory\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
114
                        $dev = new SensorDevice();
115
                        $dev->setName("GPU ".($c)." Memory Utilization (nvidiasmi)");
116
                        $dev->setValue($out[1]);
117
                        $dev->setUnit("%");
118
                        $this->mbinfo->setMbOther($dev);
119
                    }
120
                    if (preg_match("/^\s+Encoder\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
121
                        $dev = new SensorDevice();
122
                        $dev->setName("GPU ".($c)." Encoder Utilization (nvidiasmi)");
123
                        $dev->setValue($out[1]);
124
                        $dev->setUnit("%");
125
                        $this->mbinfo->setMbOther($dev);
126
                    }
127
                    if (preg_match("/^\s+Decoder\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
128
                        $dev = new SensorDevice();
129
                        $dev->setName("GPU ".($c)." Decoder Utilization (nvidiasmi)");
130
                        $dev->setValue($out[1]);
131
                        $dev->setUnit("%");
132
                        $this->mbinfo->setMbOther($dev);
133
                    }
134
                }
135
                break;
136
        }
137
    }
138
}