From f6ed328c634f7900d839d730e088d2b8901c9cd9 Mon Sep 17 00:00:00 2001 From: "Brent M. Spell" Date: Wed, 7 Oct 2020 16:41:49 -0400 Subject: [PATCH] use sysctl for cpu frequency on macos This change uses the sysctl system call instead of popen to retrieve the maximum CPU frequency on MacOS. On some platforms (Erlang in our case) using popen to query a value from the shell can deadlock the VM. Using the sysctl system call to retrieve this value avoids the deadlock in Erlang on MacOS. --- .../core/platform/profile_utils/cpu_utils.cc | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tensorflow/core/platform/profile_utils/cpu_utils.cc b/tensorflow/core/platform/profile_utils/cpu_utils.cc index 7cd1c4de88f..b76b3377397 100644 --- a/tensorflow/core/platform/profile_utils/cpu_utils.cc +++ b/tensorflow/core/platform/profile_utils/cpu_utils.cc @@ -23,6 +23,10 @@ limitations under the License. #include #endif +#if defined(__APPLE__) +#include +#endif + #include "absl/base/call_once.h" #include "tensorflow/core/platform/logging.h" #include "tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.h" @@ -114,17 +118,11 @@ static ICpuUtilsHelper* cpu_utils_helper_instance_ = nullptr; "CPU frequency"; return INVALID_FREQUENCY; #elif defined(__APPLE__) - int64 freq_hz; - FILE* fp = - popen("sysctl hw | grep hw.cpufrequency_max: | cut -d' ' -f 2", "r"); - if (fp == nullptr) { - return INVALID_FREQUENCY; - } - if (fscanf(fp, "%lld", &freq_hz) != 1) { - return INVALID_FREQUENCY; - } - pclose(fp); - if (freq_hz < 1e6) { + int64 freq_hz = 0; + size_t freq_size = sizeof(freq_hz); + int retval = + sysctlbyname("hw.cpufrequency_max", &freq_hz, &freq_size, NULL, 0); + if (retval != 0 || freq_hz < 1e6) { LOG(WARNING) << "Failed to get CPU frequency: " << freq_hz << " Hz"; return INVALID_FREQUENCY; }