Skip to content

Commit

Permalink
Add GPU temperature sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
enen92 committed Sep 5, 2023
1 parent ce6b0aa commit 3c948c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void usage(char* prog) {
std::cout << "Usage:" << std::endl;
std::cout << prog << " [options]" << std::endl;
std::cout << " -c : list CPU temperatures (Celsius)" << std::endl;
std::cout << " -g : list GPU temperatures (Celsius)" << std::endl;
std::cout << " -h : help" << std::endl;
std::cout << " -l : list all keys and values" << std::endl;
std::cout << " -v : version" << std::endl;
Expand All @@ -25,11 +26,14 @@ int main(int argc, char *argv[]) {
smctemp::UInt32Char_t key = { 0 };
smctemp::SmcVal_t val;

while ((c = getopt(argc, argv, "clvhn:")) != -1) {
while ((c = getopt(argc, argv, "clvhn:g")) != -1) {
switch(c) {
case 'c':
op = smctemp::kOpReadCpuTemp;
break;
case 'g':
op = smctemp::kOpReadGpuTemp;
break;
case 'n':
if (optarg) {
auto [ptr, ec] = std::from_chars(optarg, optarg + strlen(optarg), attempts);
Expand Down Expand Up @@ -71,10 +75,15 @@ int main(int argc, char *argv[]) {
std::cerr.flags(ef);
}
break;
case smctemp::kOpReadGpuTemp:
case smctemp::kOpReadCpuTemp:
double temp = 0.0;
while (attempts > 0) {
temp = smc_temp.GetCpuTemp();
if (op == smctemp::kOpReadCpuTemp)
temp = smc_temp.GetCpuTemp();
else if (op == smctemp::kOpReadGpuTemp)
temp = smc_temp.GetGpuTemp();

if (temp > 0.0) {
break;
} else {
Expand Down
25 changes: 25 additions & 0 deletions smctemp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,5 +485,30 @@ double SmcTemp::GetCpuTemp() {
return temp;
}

double SmcTemp::GetGpuTemp() {
double temp = 0.0;
#if defined(ARCH_TYPE_X86_64)
temp = smc_accessor_.ReadValue(kSensorTg0d);
#elif defined(ARCH_TYPE_ARM64)
std::vector<std::string> sensors;
const std::pair<unsigned int, unsigned int> valid_temperature_limits{10, 120};
const std::string cpumodel = getCPUModel();
if (cpumodel.find("m2") != std::string::npos) { // Apple M2
sensors.emplace_back(static_cast<std::string>(kSensorTg0f));
sensors.emplace_back(static_cast<std::string>(kSensorTg0j));
} else if (cpumodel.find("m1") != std::string::npos) { // Apple M1
sensors.emplace_back(static_cast<std::string>(kSensorTg05));
sensors.emplace_back(static_cast<std::string>(kSensorTg0D));
sensors.emplace_back(static_cast<std::string>(kSensorTg0L));
sensors.emplace_back(static_cast<std::string>(kSensorTg0T));
} else {
// not supported
return temp;
}
temp = CalculateAverageTemperature(sensors, valid_temperature_limits);
#endif
return temp;
}

}

13 changes: 13 additions & 0 deletions smctemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@ constexpr uint32_t kKernelIndexSmc = 2;
constexpr int kOpNone = 0;
constexpr int kOpList = 1;
constexpr int kOpReadCpuTemp = 2;
constexpr int kOpReadGpuTemp = 3;

// List of key and name:
// - https://github.com/exelban/stats/blob/0e2e13c626b650ac7743ef620869d2b7857665cd/Modules/Sensors/values.swift
// - https://github.com/acidanthera/VirtualSMC/blob/632fec680d996a5dd015afd9acf0ba40f75e69e2/Docs/SMCSensorKeys.txt
#if defined(ARCH_TYPE_X86_64)
// CPU
constexpr UInt32Char_t kSensorTc0d = "TC0D"; // CPU die temperature
constexpr UInt32Char_t kSensorTc0e = "TC0E"; // CPU PECI die filtered temperature
constexpr UInt32Char_t kSensorTc0f = "TC0F"; // CPU PECI die temperature filtered then adjusted
constexpr UInt32Char_t kSensorTc0p = "TC0P"; // CPU proximity temperature
// GPU
constexpr UInt32Char_t kSensorTg0d = "TG0D"; // GPU die temperature
#elif defined(ARCH_TYPE_ARM64)
// CPU
constexpr UInt32Char_t kSensorTc0a = "Tc0a";
constexpr UInt32Char_t kSensorTc0b = "Tc0b";
constexpr UInt32Char_t kSensorTc0x = "Tc0x";
Expand All @@ -70,6 +75,13 @@ constexpr UInt32Char_t kSensorTp0j = "Tp0j";
constexpr UInt32Char_t kSensorTp0r = "Tp0r";
constexpr UInt32Char_t kSensorTp0f = "Tp0f";
constexpr UInt32Char_t kSensorTp0n = "Tp0n";
// GPU
constexpr UInt32Char_t kSensorTg05 = "Tg05";
constexpr UInt32Char_t kSensorTg0D = "Tg0D";
constexpr UInt32Char_t kSensorTg0L = "Tg0L";
constexpr UInt32Char_t kSensorTg0T = "Tg0T";
constexpr UInt32Char_t kSensorTg0f = "Tg0f";
constexpr UInt32Char_t kSensorTg0j = "Tg0j";
#endif

class SmcAccessor {
Expand Down Expand Up @@ -102,6 +114,7 @@ class SmcTemp {
SmcTemp() = default;
~SmcTemp() = default;
double GetCpuTemp();
double GetGpuTemp();
};

typedef struct {
Expand Down

0 comments on commit 3c948c0

Please sign in to comment.