aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/platform
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-07-10 09:29:30 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-10 09:32:53 -0700
commit17d5ea36fcc2e8914687be12e39ac9b97d4d8ce7 (patch)
tree92099724c6729402de1b36ef40434bc6ee16c3ba /tensorflow/core/platform
parent8a6ef2cb4f98bacc1f821f60c21914b4bd5faaef (diff)
[SE] Re-enable acquiring real cpu frequency
PiperOrigin-RevId: 203959955
Diffstat (limited to 'tensorflow/core/platform')
-rw-r--r--tensorflow/core/platform/profile_utils/cpu_utils.cc37
1 files changed, 24 insertions, 13 deletions
diff --git a/tensorflow/core/platform/profile_utils/cpu_utils.cc b/tensorflow/core/platform/profile_utils/cpu_utils.cc
index 02de7d1362..b0136b52f4 100644
--- a/tensorflow/core/platform/profile_utils/cpu_utils.cc
+++ b/tensorflow/core/platform/profile_utils/cpu_utils.cc
@@ -15,6 +15,7 @@ limitations under the License.
#include "tensorflow/core/platform/profile_utils/cpu_utils.h"
+#include <fstream>
#include <limits>
#include <mutex>
@@ -67,22 +68,32 @@ static ICpuUtilsHelper* cpu_utils_helper_instance_ = nullptr;
#if defined(__ANDROID__)
return GetCpuUtilsHelperSingletonInstance().CalculateCpuFrequency();
#elif defined(__linux__)
- double bogomips;
- FILE* fp = popen("grep '^bogomips' /proc/cpuinfo | head -1", "r");
- if (fp == nullptr) {
- return INVALID_FREQUENCY;
- }
- const int retval_of_bogomips = fscanf(fp, "bogomips : %lf", &bogomips);
- if (retval_of_bogomips <= 0) {
+ // Read the contents of /proc/cpuinfo.
+ std::ifstream cpuinfo("/proc/cpuinfo");
+ if (!cpuinfo) {
+ LOG(WARNING) << "Failed to open /proc/cpuinfo";
return INVALID_FREQUENCY;
}
- pclose(fp);
- const double freq_ghz = bogomips / 1000.0 / 2.0;
- if (retval_of_bogomips != 1 || freq_ghz < 0.01) {
- LOG(WARNING) << "Failed to get CPU frequency: " << freq_ghz << " Hz";
- return INVALID_FREQUENCY;
+ string line;
+ while (std::getline(cpuinfo, line)) {
+ double bogomips;
+ const int retval_of_bogomips =
+ sscanf(line.c_str(), "bogomips : %lf", &bogomips);
+ if (retval_of_bogomips > 0) {
+ const double freq_ghz = bogomips / 1000.0 / 2.0;
+ if (retval_of_bogomips != 1 || freq_ghz < 0.01) {
+ LOG(WARNING) << "Failed to get CPU frequency: " << freq_ghz << " Hz";
+ return INVALID_FREQUENCY;
+ }
+ const int64 freq_n =
+ static_cast<int64>(freq_ghz * 1000.0 * 1000.0 * 1000.0);
+ LOG(INFO) << "CPU Frequency: " << freq_n << " Hz";
+ return freq_n;
+ }
}
- return static_cast<int64>(freq_ghz * 1000.0 * 1000.0 * 1000.0);
+ LOG(WARNING) << "Failed to find bogomips in /proc/cpuinfo; cannot determine "
+ "CPU frequency";
+ return INVALID_FREQUENCY;
#elif defined(__APPLE__)
int64 freq_hz;
FILE* fp =