Subversion Repositories ALCASAR

Rev

Rev 3037 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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