|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import { execCommand } from './utils'; |
| 3 | + |
| 4 | +export type Temperatures = { |
| 5 | + /** Overall battery temperature in degrees Celsius (°C). */ |
| 6 | + battery?: number; |
| 7 | + |
| 8 | + /** Overall CPU temperature in degrees Celsius (°C). */ |
| 9 | + cpu?: number; |
| 10 | + |
| 11 | + /** |
| 12 | + * Temperature of each CPU core in degrees Celsius (°C). |
| 13 | + * If present contains at least one value. |
| 14 | + */ |
| 15 | + cpuCores?: number[]; |
| 16 | + |
| 17 | + /** |
| 18 | + * Temperature of each CPU socket in degrees Celsius (°C). |
| 19 | + * If present contains at least one value. |
| 20 | + */ |
| 21 | + cpuSockets?: number[]; |
| 22 | + |
| 23 | + /** Overall GPU temperature in degrees Celsius (°C). */ |
| 24 | + gpu?: number; |
| 25 | + |
| 26 | + /** |
| 27 | + * Temperature of each GPU in degrees Celsius (°C). |
| 28 | + * If present contains at least one value. |
| 29 | + */ |
| 30 | + gpus?: number[]; |
| 31 | + |
| 32 | + /** Temperature of the motherboard in degrees Celsius (°C). */ |
| 33 | + motherboard?: number; |
| 34 | + |
| 35 | + /** Temperature of the Wi-Fi adapter in degrees Celsius (°C). */ |
| 36 | + wifi?: number; |
| 37 | +}; |
| 38 | + |
| 39 | +export async function getTemperatures(): Promise<Temperatures> { |
| 40 | + const temperatures: Temperatures = {}; |
| 41 | + |
| 42 | + switch (process.platform) { |
| 43 | + case 'linux': { |
| 44 | + const thermalFiles = await fs.readdir('/sys/class/thermal'); |
| 45 | + const hwmonFiles = await fs.readdir('/sys/class/hwmon'); |
| 46 | + await Promise.allSettled([ |
| 47 | + ...thermalFiles.map(async (file) => { |
| 48 | + const temp = await fs |
| 49 | + .readFile(`/sys/class/thermal/${file}/temp`, 'utf8') |
| 50 | + .then((tempStr) => { |
| 51 | + const t = parseInt(tempStr, 10); |
| 52 | + return !Number.isNaN(t) ? t / 1000 : null; // millidegrees Celsius to degrees Celsius |
| 53 | + }) |
| 54 | + .catch(() => null); |
| 55 | + if (temp === null) return; |
| 56 | + const type = (await fs.readFile(`/sys/class/thermal/${file}/type`, 'utf8').catch(() => '')).toLowerCase(); |
| 57 | + if (type.includes('core')) { |
| 58 | + temperatures.cpuCores = temperatures.cpuCores || []; |
| 59 | + temperatures.cpuCores.push(temp); |
| 60 | + } else if (type.includes('x86') || type.includes('soc_thermal')) { |
| 61 | + temperatures.cpuSockets = temperatures.cpuSockets || []; |
| 62 | + temperatures.cpuSockets.push(temp); |
| 63 | + } else if (type.startsWith('acp') || type.startsWith('pch')) { |
| 64 | + temperatures.motherboard = temp; |
| 65 | + } else if (type.includes('gpu') || type.includes('graphics')) { |
| 66 | + temperatures.gpus = temperatures.gpus || []; |
| 67 | + temperatures.gpus.push(temp); |
| 68 | + } else if (type.includes('wifi')) { |
| 69 | + temperatures.wifi = temp; |
| 70 | + } else if (type.includes('battery')) { |
| 71 | + temperatures.battery = temp; |
| 72 | + } |
| 73 | + }), |
| 74 | + ...hwmonFiles.map(async (file) => { |
| 75 | + const subFiles = await fs.readdir('/sys/class/hwmon/' + file); |
| 76 | + let defaultName: string | null = null; |
| 77 | + const names: { [key: string]: string } = {}; |
| 78 | + const temps: { [key: string]: number } = {}; |
| 79 | + |
| 80 | + await Promise.allSettled( |
| 81 | + subFiles.map(async (subFile) => { |
| 82 | + if (subFile === 'name') |
| 83 | + defaultName = await fs.readFile('/sys/class/hwmon/' + file + '/' + subFile, 'utf8').catch(() => null); |
| 84 | + if (subFile.endsWith('_label')) { |
| 85 | + const key = subFile.slice(0, -6); |
| 86 | + names[key] = await fs.readFile('/sys/class/hwmon/' + file + '/' + subFile, 'utf8').catch(() => ''); |
| 87 | + } |
| 88 | + if (subFile.endsWith('_input')) { |
| 89 | + const key = subFile.slice(0, -6); |
| 90 | + const temp = await fs |
| 91 | + .readFile('/sys/class/hwmon/' + file + '/' + subFile, 'utf8') |
| 92 | + .then((tempStr) => { |
| 93 | + const t = parseInt(tempStr, 10); |
| 94 | + return !Number.isNaN(t) ? t / 1000 : null; // millidegrees Celsius to degrees Celsius |
| 95 | + }) |
| 96 | + .catch(() => null); |
| 97 | + if (temp !== null) temps[key] = temp; |
| 98 | + } |
| 99 | + }), |
| 100 | + ); |
| 101 | + |
| 102 | + for (const [key, temp] of Object.entries(temps)) { |
| 103 | + const name = (names[key] || defaultName || '').toLowerCase(); |
| 104 | + if (name.includes('core')) { |
| 105 | + temperatures.cpuCores = temperatures.cpuCores || []; |
| 106 | + temperatures.cpuCores.push(temp); |
| 107 | + } else if (name.includes('socket') || name.includes('package')) { |
| 108 | + temperatures.cpuSockets = temperatures.cpuSockets || []; |
| 109 | + temperatures.cpuSockets.push(temp); |
| 110 | + } else if (name.includes('gpu') || name.includes('graphics')) { |
| 111 | + temperatures.gpus = temperatures.gpus || []; |
| 112 | + temperatures.gpus.push(temp); |
| 113 | + } else if (name.includes('motherboard') || name.includes('mainboard') || name.includes('mb')) { |
| 114 | + temperatures.motherboard = temp; |
| 115 | + } else if (name.includes('wifi')) { |
| 116 | + temperatures.wifi = temp; |
| 117 | + } else if (name.includes('battery')) { |
| 118 | + temperatures.battery = temp; |
| 119 | + } |
| 120 | + } |
| 121 | + }), |
| 122 | + ]); |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + case 'win32': { |
| 127 | + const output = await execCommand( |
| 128 | + 'powershell -Command "Get-CimInstance MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" | Select CurrentTemperature | Format-List"', |
| 129 | + ).catch(() => ''); // only successful if administrator rights are available |
| 130 | + const lines = output.split('\n'); |
| 131 | + // tslint:disable-next-line:prefer-for-of |
| 132 | + for (let i = 0; i < lines.length; i++) { |
| 133 | + const [key, value] = lines[i].split(' : ').map((s) => s.trim()); |
| 134 | + if (key === 'CurrentTemperature') { |
| 135 | + const temperature = parseInt(value, 10); |
| 136 | + if (!Number.isNaN(temperature)) { |
| 137 | + temperatures.cpu = (temperature - 2732) / 10; // Convert from Kelvin to Celsius |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // backup nvidia-smi |
| 146 | + if (temperatures.gpus === undefined) { |
| 147 | + const output = await execCommand('nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader').catch(() => ''); |
| 148 | + const lines = output.split('\n').filter((line) => line.trim() !== ''); |
| 149 | + if (lines.length > 0) { |
| 150 | + temperatures.gpus = lines |
| 151 | + .map((line) => { |
| 152 | + const temp = parseInt(line.trim(), 10); |
| 153 | + return !Number.isNaN(temp) ? temp : null; |
| 154 | + }) |
| 155 | + .filter((temp) => temp !== null); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // post processing |
| 160 | + if (temperatures.cpu === undefined) { |
| 161 | + if (temperatures.cpuSockets && temperatures.cpuSockets.length > 0) { |
| 162 | + temperatures.cpu = temperatures.cpuSockets.reduce((a, b) => a + b, 0) / temperatures.cpuSockets.length; |
| 163 | + } else if (temperatures.cpuCores && temperatures.cpuCores.length > 0) { |
| 164 | + temperatures.cpu = temperatures.cpuCores.reduce((a, b) => a + b, 0) / temperatures.cpuCores.length; |
| 165 | + } |
| 166 | + } |
| 167 | + if (temperatures.gpu === undefined && temperatures.gpus && temperatures.gpus.length > 0) { |
| 168 | + temperatures.gpu = temperatures.gpus.reduce((a, b) => a + b, 0) / temperatures.gpus.length; |
| 169 | + } |
| 170 | + |
| 171 | + return temperatures; |
| 172 | +} |
0 commit comments