From 17cb9aec580a83e67e3d138e25650d33d136f70f Mon Sep 17 00:00:00 2001 From: narugit Date: Wed, 14 Aug 2024 13:49:08 +0900 Subject: [PATCH] feat: add interval option --- README.md | 13 ++++++++++++- main.cc | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fbcfd87..32d90b6 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,11 @@ Usage: -c : list CPU temperatures (Celsius) -g : list GPU 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 -f : fail-soft mode to store recent valid sensor value -v : version - -n : tries to query the temperature sensors for n times (e.g. -n3) (1 second interval) until a valid value is returned + -n : tries to query the temperature sensors for n times (e.g. -n3) until a valid value is returned $ smctemp -c 64.2 @@ -39,3 +40,13 @@ $ smctemp -c $ smctemp -g 36.2 ``` + +## Note for M2 Mac Users +On M2 Macs, sensor values may be unstable as described in the following issue: +- https://github.com/narugit/smctemp/pull/14 +- https://github.com/narugit/smctemp/issues/32 + +For M2 Macs, using the `-n`, `-i`, and `-f` options can help obtain more stable sensor values. +Try tuning these options to get better results. + +The recommended settings are `-i25 -n40 -f` (See also https://github.com/narugit/smctemp/issues/32#issuecomment-2287304793). diff --git a/main.cc b/main.cc index d325d47..47292c9 100644 --- a/main.cc +++ b/main.cc @@ -11,6 +11,8 @@ void usage(char* prog) { 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 << " -i : set interval in milliseconds (e.g. -i25, valid range is 20-1000, default: 1000)" + << std::endl; std::cout << " -l : list all keys and values" << std::endl; std::cout << " -f : fail-soft mode to store recent valid sensor value" << std::endl; std::cout << " -v : version" << std::endl; @@ -21,12 +23,13 @@ void usage(char* prog) { int main(int argc, char *argv[]) { int c; unsigned int attempts = 1; + unsigned int interval_ms = 1'000; kern_return_t result; int op = smctemp::kOpNone; bool isFailSoft = false; - while ((c = getopt(argc, argv, "clvfhn:g")) != -1) { + while ((c = getopt(argc, argv, "clvfhn:gi:")) != -1) { switch(c) { case 'c': op = smctemp::kOpReadCpuTemp; @@ -34,6 +37,17 @@ int main(int argc, char *argv[]) { case 'g': op = smctemp::kOpReadGpuTemp; break; + case 'i': + if (optarg) { + unsigned int temp_interval; + auto [ptr, ec] = std::from_chars(optarg, optarg + strlen(optarg), temp_interval); + if (ec != std::errc() || temp_interval < 20 || temp_interval > 1000) { + std::cerr << "Invalid argument provided for -i (integer between 20 and 1000 is required)" << std::endl; + return 1; + } + interval_ms = temp_interval; + } + break; case 'n': if (optarg) { auto [ptr, ec] = std::from_chars(optarg, optarg + strlen(optarg), attempts); @@ -91,7 +105,7 @@ int main(int argc, char *argv[]) { if (smc_temp.IsValidTemperature(temp, valid_temperature_limits)) { break; } else { - usleep(1'000'000); + usleep(interval_ms * 1'000); attempts--; } } @@ -105,6 +119,11 @@ int main(int argc, char *argv[]) { } } 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 -n40 -f` options.`" << std::endl; + return 1; + } break; }