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; }