Subversion Repositories ALCASAR

Rev

Rev 3037 | Details | Compare with Previous | 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();
3100 rexy 30
        if (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT')) switch (defined('PSI_SENSOR_NVIDIASMI_ACCESS')?strtolower(PSI_SENSOR_NVIDIASMI_ACCESS):'command') {
2976 rexy 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':
3100 rexy 46
            if (!defined('PSI_EMU_PORT') && CommonFunctions::rftsdata('nvidiasmi.tmp', $lines)) {
2976 rexy 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
        }
53
    }
54
 
55
    /**
56
     * get the information
57
     *
58
     * @see PSI_Interface_Sensor::build()
59
     *
3037 rexy 60
     * @return void
2976 rexy 61
     */
62
    public function build()
63
    {
64
        $gpuc=count($this->_gpus);
65
        switch ($gpuc) {
3037 rexy 66
        case 0:
67
            $this->error->addError("nvidia-smi", "No values");
68
            break;
69
        case 1:
70
            $this->error->addError("nvidia-smi", "Error: ".$this->_gpus[0]);
71
            break;
72
        default:
73
            for ($c = 0; $c < $gpuc; $c++) {
74
                if (preg_match("/^\s+GPU Current Temp\s+:\s*(\d+)\s*C\s*$/m", $this->_gpus[$c], $out)) {
75
                    $dev = new SensorDevice();
76
                    $dev->setName("GPU ".($c)." (nvidiasmi)");
77
                    $dev->setValue($out[1]);
78
                    if (preg_match("/^\s+GPU Shutdown Temp\s+:\s*(\d+)\s*C\s*$/m", $this->_gpus[$c], $out)) {
79
                        $dev->setMax($out[1]);
2976 rexy 80
                    }
3037 rexy 81
                    $this->mbinfo->setMbTemp($dev);
82
                }
83
                if (preg_match("/^\s+Power Draw\s+:\s*([\d\.]+)\s*W\s*$/m", $this->_gpus[$c], $out)) {
84
                    $dev = new SensorDevice();
85
                    $dev->setName("GPU ".($c)." (nvidiasmi)");
86
                    $dev->setValue($out[1]);
87
                    if (preg_match("/^\s+Power Limit\s+:\s*([\d\.]+)\s*W\s*$/m", $this->_gpus[$c], $out)) {
88
                        $dev->setMax($out[1]);
2976 rexy 89
                    }
3037 rexy 90
                    $this->mbinfo->setMbPower($dev);
2976 rexy 91
                }
3037 rexy 92
                if (preg_match("/^\s+Fan Speed\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
93
                    $dev = new SensorDevice();
94
                    $dev->setName("GPU ".($c)." (nvidiasmi)");
95
                    $dev->setValue($out[1]);
96
                    $dev->setUnit("%");
97
                    $this->mbinfo->setMbFan($dev);
98
                }
99
                if (preg_match("/^\s+Performance State\s+:\s*(\S+)\s*$/m", $this->_gpus[$c], $out)) {
100
                    $dev = new SensorDevice();
101
                    $dev->setName("GPU ".($c)." Performance State (nvidiasmi)");
102
                    $dev->setValue($out[1]);
103
                    $this->mbinfo->setMbOther($dev);
104
                }
105
                if (preg_match("/^\s+Gpu\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
106
                    $dev = new SensorDevice();
107
                    $dev->setName("GPU ".($c)." Utilization (nvidiasmi)");
108
                    $dev->setValue($out[1]);
109
                    $dev->setUnit("%");
110
                    $this->mbinfo->setMbOther($dev);
111
                }
112
                if (preg_match("/^\s+Memory\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
113
                    $dev = new SensorDevice();
114
                    $dev->setName("GPU ".($c)." Memory Utilization (nvidiasmi)");
115
                    $dev->setValue($out[1]);
116
                    $dev->setUnit("%");
117
                    $this->mbinfo->setMbOther($dev);
118
                }
119
                if (preg_match("/^\s+Encoder\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
120
                    $dev = new SensorDevice();
121
                    $dev->setName("GPU ".($c)." Encoder Utilization (nvidiasmi)");
122
                    $dev->setValue($out[1]);
123
                    $dev->setUnit("%");
124
                    $this->mbinfo->setMbOther($dev);
125
                }
126
                if (preg_match("/^\s+Decoder\s+:\s*(\d+)\s*%\s*$/m", $this->_gpus[$c], $out)) {
127
                    $dev = new SensorDevice();
128
                    $dev->setName("GPU ".($c)." Decoder Utilization (nvidiasmi)");
129
                    $dev->setValue($out[1]);
130
                    $dev->setUnit("%");
131
                    $this->mbinfo->setMbOther($dev);
132
                }
133
            }
2976 rexy 134
        }
135
    }
136
}