Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Usage:
./smctemp [options]
-c : list CPU temperatures (Celsius)
-g : list GPU temperatures (Celsius)
-C : list individual CPU core temperatures (Celsius)
-G : list individual GPU core temperatures (Celsius)
-h : help
-i : set interval in milliseconds (e.g. -i25, valid range is 20-1000, default: 1000)
-l : list all keys and values
Expand All @@ -40,6 +42,14 @@ $ smctemp -c

$ smctemp -g
36.2

$ smctemp -C
P-Core 1: 62.4°C
P-Core 2: 63.8°C
P-Core 3: 65.2°C
P-Core 4: 64.8°C
E-Core 1: 58.6°C
E-Core 2: 59.1°C
```

## Note for M2 Mac Users
Expand Down
110 changes: 85 additions & 25 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ void usage(char* prog) {
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 << " -C : list individual CPU core temperatures (Celsius)" << std::endl;
std::cout << " -G : list individual GPU core temperatures (Celsius)" << std::endl;
std::cout << " -h : help" << std::endl;
std::cout << " -i : set interval in milliseconds (e.g. -i25, valid range is 20-1000, default: 1000)"
<< std::endl;
Expand All @@ -29,14 +31,20 @@ int main(int argc, char *argv[]) {
int op = smctemp::kOpNone;
bool isFailSoft = false;

while ((c = getopt(argc, argv, "clvfhn:gi:")) != -1) {
while ((c = getopt(argc, argv, "clvfhn:gi:CG")) != -1) {
switch(c) {
case 'c':
op = smctemp::kOpReadCpuTemp;
break;
case 'g':
op = smctemp::kOpReadGpuTemp;
break;
case 'C':
op = smctemp::kOpReadIndividualCpuTemp;
break;
case 'G':
op = smctemp::kOpReadIndividualGpuTemp;
break;
case 'i':
if (optarg) {
unsigned int temp_interval;
Expand Down Expand Up @@ -81,6 +89,9 @@ int main(int argc, char *argv[]) {

smctemp::SmcAccessor smc_accessor = smctemp::SmcAccessor();
smctemp::SmcTemp smc_temp = smctemp::SmcTemp(isFailSoft);
// Common variables for temperature measurements
double temp = 0.0;
const std::pair<unsigned int, unsigned int> valid_temperature_limits{10, 120};

switch(op) {
case smctemp::kOpList:
Expand All @@ -94,39 +105,88 @@ int main(int argc, char *argv[]) {
break;
case smctemp::kOpReadGpuTemp:
case smctemp::kOpReadCpuTemp:
double temp = 0.0;
const std::pair<unsigned int, unsigned int> valid_temperature_limits{10, 120};
while (attempts > 0) {
if (op == smctemp::kOpReadCpuTemp) {
temp = smc_temp.GetCpuTemp();
} else if (op == smctemp::kOpReadGpuTemp) {
temp = smc_temp.GetGpuTemp();
{
while (attempts > 0) {
if (op == smctemp::kOpReadCpuTemp) {
temp = smc_temp.GetCpuTemp();
} else if (op == smctemp::kOpReadGpuTemp) {
temp = smc_temp.GetGpuTemp();
}
if (smc_temp.IsValidTemperature(temp, valid_temperature_limits)) {
break;
} else {
usleep(interval_ms * 1'000);
attempts--;
}
}
if (isFailSoft) {
if (!smc_temp.IsValidTemperature(temp, valid_temperature_limits)) {
if (op == smctemp::kOpReadCpuTemp) {
temp = smc_temp.GetLastValidCpuTemp();
} else if (op == smctemp::kOpReadGpuTemp) {
temp = smc_temp.GetLastValidGpuTemp();
}
}
}
if (smc_temp.IsValidTemperature(temp, valid_temperature_limits)) {
break;
} else {
usleep(interval_ms * 1'000);
attempts--;
std::cout << std::fixed << std::setprecision(1) << temp << std::endl;
if (temp == 0.0) {
std::cerr << "Could not get valid sensor value. Please use `-n` option and `-i` option." << std::endl;
std::cerr << "In M2 Mac, it would be work fine with `-i25 -n180 -f` options.`" << std::endl;
return 1;
}
}
if (isFailSoft) {
if (!smc_temp.IsValidTemperature(temp, valid_temperature_limits)) {
if (op == smctemp::kOpReadCpuTemp) {
temp = smc_temp.GetLastValidCpuTemp();
} else if (op == smctemp::kOpReadGpuTemp) {
temp = smc_temp.GetLastValidGpuTemp();
break;
case smctemp::kOpReadIndividualCpuTemp:
{
std::vector<std::pair<std::string, double>> temps;
unsigned int attempts_left = attempts;
while (attempts_left > 0) {
temps = smc_temp.GetIndividualCpuTemps();
if (!temps.empty()) {
break;
} else {
usleep(interval_ms * 1'000);
attempts_left--;
}
}

if (temps.empty()) {
std::cerr << "Could not get valid CPU core temperatures. Please use `-n` option and `-i` option." << std::endl;
std::cerr << "In M2 Mac, it would be work fine with `-i25 -n180 -f` options.`" << std::endl;
return 1;
}

for (const auto& temp_pair : temps) {
std::cout << temp_pair.first << ": " << std::fixed << std::setprecision(1) << temp_pair.second << "°C" << std::endl;
}
}
std::cout << std::fixed << std::setprecision(1) << temp << std::endl;
if (temp == 0.0) {
std::cerr << "Could not get valid sensor value. Please use `-n` option and `-i` option." << std::endl;
std::cerr << "In M2 Mac, it would be work fine with `-i25 -n180 -f` options.`" << std::endl;
return 1;
break;
case smctemp::kOpReadIndividualGpuTemp:
{
std::vector<std::pair<std::string, double>> temps;
unsigned int attempts_left = attempts;
while (attempts_left > 0) {
temps = smc_temp.GetIndividualGpuTemps();
if (!temps.empty()) {
break;
} else {
usleep(interval_ms * 1'000);
attempts_left--;
}
}

if (temps.empty()) {
std::cerr << "Could not get valid GPU core temperatures. Please use `-n` option and `-i` option." << std::endl;
std::cerr << "In M2 Mac, it would be work fine with `-i25 -n180 -f` options.`" << std::endl;
return 1;
}

for (const auto& temp_pair : temps) {
std::cout << temp_pair.first << ": " << std::fixed << std::setprecision(1) << temp_pair.second << "°C" << std::endl;
}
}
break;
}

return 0;
}

Loading