Merge pull request #43855 from brentspell:cpu_freq_macos

PiperOrigin-RevId: 337986862
Change-Id: Icd5438acfdfe713d2b276076af58e579ffc86969
This commit is contained in:
TensorFlower Gardener 2020-10-19 20:32:25 -07:00
commit ecd5184dd2

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