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.
This commit is contained in:
Brent M. Spell 2020-10-07 16:41:49 -04:00
parent 68a6fe0d98
commit f6ed328c63
No known key found for this signature in database
GPG Key ID: C19F01B68713F920

View File

@ -23,6 +23,10 @@ limitations under the License.
#include <windows.h>
#endif
#if defined(__APPLE__)
#include <sys/sysctl.h>
#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;
}