diff mbox series

[v4,1/2] linux-gen: Use /proc/cpuinfo, if sysfs is not available to get cpu info.

Message ID 1507309214-28483-2-git-send-email-odpbot@yandex.ru
State New
Headers show
Series [v4,1/2] linux-gen: Use /proc/cpuinfo, if sysfs is not available to get cpu info. | expand

Commit Message

Github ODP bot Oct. 6, 2017, 5 p.m. UTC
From: Ilias Apalodimas <ilias.apalodimas@linaro.org>


PR#171 broke some of the functionality we had on getting cpu speed.
This patch reverts parts of that commit adding a fallback when
/sys/devices/system/cpu/cpuX/cpufreq/ is not available.

Solves https://bugs.linaro.org/show_bug.cgi?id=3249

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

---
/** Email created from pull request 214 (apalos:bug_3249)
 ** https://github.com/Linaro/odp/pull/214
 ** Patch: https://github.com/Linaro/odp/pull/214.patch
 ** Base sha: a63f25ff2994b2df78c24f1f8b63d0e06628eb68
 ** Merge commit sha: 57fff95425e76f7cec228f7a9471ca07c25cf029
 **/
 .../linux-generic/arch/default/odp_sysinfo_parse.c |  5 +++
 .../linux-generic/arch/mips64/odp_sysinfo_parse.c  |  5 +++
 .../linux-generic/arch/powerpc/odp_sysinfo_parse.c |  5 +++
 .../linux-generic/arch/x86/odp_sysinfo_parse.c     | 36 ++++++++++++++++++++++
 platform/linux-generic/include/odp_internal.h      |  1 +
 platform/linux-generic/odp_system_info.c           |  7 ++++-
 6 files changed, 58 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/platform/linux-generic/arch/default/odp_sysinfo_parse.c b/platform/linux-generic/arch/default/odp_sysinfo_parse.c
index 1e2c729a7..68d3d9caf 100644
--- a/platform/linux-generic/arch/default/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/default/odp_sysinfo_parse.c
@@ -26,3 +26,8 @@  int cpuinfo_parser(FILE *file ODP_UNUSED, system_info_t *sysinfo)
 void sys_info_print_arch(void)
 {
 }
+
+uint64_t odp_cpu_arch_hz_current(int id ODP_UNUSED)
+{
+	return 0;
+}
diff --git a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
index 5ed7a398e..313e58e88 100644
--- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
@@ -63,3 +63,8 @@  int cpuinfo_parser(FILE *file, system_info_t *sysinfo)
 void sys_info_print_arch(void)
 {
 }
+
+uint64_t odp_cpu_arch_hz_current(int id ODP_UNUSED)
+{
+	return 0;
+}
diff --git a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
index 02a83e514..461edf9d9 100644
--- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
@@ -62,3 +62,8 @@  int cpuinfo_parser(FILE *file, system_info_t *sysinfo)
 void sys_info_print_arch(void)
 {
 }
+
+uint64_t odp_cpu_arch_hz_current(int id ODP_UNUSED)
+{
+	return 0;
+}
diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
index 55bbda8c4..f3fd8c146 100644
--- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
@@ -45,3 +45,39 @@  void sys_info_print_arch(void)
 {
 	cpu_flags_print_all();
 }
+
+uint64_t odp_cpu_arch_hz_current(int id ODP_UNUSED)
+{
+	char str[1024];
+	FILE *file;
+	int cpu;
+	char *pos;
+	double mhz = 0.0;
+
+	file = fopen("/proc/cpuinfo", "rt");
+
+	/* find the correct processor instance */
+	while (fgets(str, sizeof(str), file) != NULL) {
+		pos = strstr(str, "processor");
+		if (pos) {
+			if (sscanf(pos, "processor : %d", &cpu) == 1)
+				if (cpu == id)
+					break;
+		}
+	}
+
+	/* extract the cpu current speed */
+	while (fgets(str, sizeof(str), file) != NULL) {
+		pos = strstr(str, "cpu MHz");
+		if (pos) {
+			if (sscanf(pos, "cpu MHz : %lf", &mhz) == 1)
+				break;
+		}
+	}
+
+	fclose(file);
+	if (mhz)
+		return (uint64_t)(mhz * 1000000.0);
+
+	return 0;
+}
diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index e0cc6dd4a..fc69cd0c5 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -133,6 +133,7 @@  int _odp_ishm_term_local(void);
 int cpuinfo_parser(FILE *file, system_info_t *sysinfo);
 uint64_t odp_cpufreq_id(const char *filename, int id);
 uint64_t odp_cpu_hz_current(int id);
+uint64_t odp_cpu_arch_hz_current(int id);
 void sys_info_print_arch(void);
 
 #ifdef __cplusplus
diff --git a/platform/linux-generic/odp_system_info.c b/platform/linux-generic/odp_system_info.c
index d310d7790..1303d7937 100644
--- a/platform/linux-generic/odp_system_info.c
+++ b/platform/linux-generic/odp_system_info.c
@@ -385,7 +385,12 @@  int odp_system_info_term(void)
  */
 uint64_t odp_cpu_hz_current(int id)
 {
-	return odp_cpufreq_id("cpuinfo_cur_freq", id);
+	uint64_t cur_hz = odp_cpufreq_id("cpuinfo_cur_freq", id);
+
+	if (!cur_hz)
+		cur_hz = odp_cpu_arch_hz_current(id);
+
+	return cur_hz;
 }
 
 uint64_t odp_cpu_hz(void)