From: Kelvin Cheung <keguang.zh...@gmail.com> When CPU frequency info under /sys is available, use the real value instead of default value.
Signed-off-by: Kelvin Cheung <keguang.zh...@gmail.com> --- /** Email created from pull request 171 (kelvincheung:master) ** https://github.com/Linaro/odp/pull/171 ** Patch: https://github.com/Linaro/odp/pull/171.patch ** Base sha: fb3f36cec108ce9c55241d9f0e66d4832a552b8a ** Merge commit sha: 024162909820c5733b4d1766f15bc97d13c1004c **/ v5: Factor out a common functions to support both max and current frequency Use two different variables for path and buffer Use a unified odp_cpu_hz_current() instead of per-platform implementations Some minor fixes V4: Expand to all available architectures. --- /** Email created from pull request 171 (kelvincheung:master) ** https://github.com/Linaro/odp/pull/171 ** Patch: https://github.com/Linaro/odp/pull/171.patch ** Base sha: fb3f36cec108ce9c55241d9f0e66d4832a552b8a ** Merge commit sha: 024162909820c5733b4d1766f15bc97d13c1004c **/ .../linux-generic/arch/arm/odp_sysinfo_parse.c | 5 --- .../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 | 34 ++++++++++++++++++++ 7 files changed, 35 insertions(+), 56 deletions(-) diff --git a/platform/linux-generic/arch/arm/odp_sysinfo_parse.c b/platform/linux-generic/arch/arm/odp_sysinfo_parse.c index 1e9db418..1e2c729a 100644 --- a/platform/linux-generic/arch/arm/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/arm/odp_sysinfo_parse.c @@ -23,11 +23,6 @@ int cpuinfo_parser(FILE *file ODP_UNUSED, system_info_t *sysinfo) return 0; } -uint64_t odp_cpu_hz_current(int id ODP_UNUSED) -{ - return 0; -} - void sys_info_print_arch(void) { } diff --git a/platform/linux-generic/arch/default/odp_sysinfo_parse.c b/platform/linux-generic/arch/default/odp_sysinfo_parse.c index 1e9db418..1e2c729a 100644 --- a/platform/linux-generic/arch/default/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/default/odp_sysinfo_parse.c @@ -23,11 +23,6 @@ int cpuinfo_parser(FILE *file ODP_UNUSED, system_info_t *sysinfo) return 0; } -uint64_t odp_cpu_hz_current(int id ODP_UNUSED) -{ - return 0; -} - void sys_info_print_arch(void) { } diff --git a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c index 2a37ae70..5ed7a398 100644 --- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c @@ -60,11 +60,6 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) return 0; } -uint64_t odp_cpu_hz_current(int id ODP_UNUSED) -{ - return 0; -} - void sys_info_print_arch(void) { } diff --git a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c index 29c97613..02a83e51 100644 --- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c @@ -59,11 +59,6 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) return 0; } -uint64_t odp_cpu_hz_current(int id ODP_UNUSED) -{ - return 0; -} - void sys_info_print_arch(void) { } diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c index f7918d40..55bbda8c 100644 --- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c @@ -41,42 +41,6 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) return 0; } -uint64_t odp_cpu_hz_current(int id) -{ - 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; -} - void sys_info_print_arch(void) { cpu_flags_print_all(); diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h index 355e25f7..e0cc6dd4 100644 --- a/platform/linux-generic/include/odp_internal.h +++ b/platform/linux-generic/include/odp_internal.h @@ -131,6 +131,7 @@ int _odp_ishm_term_global(void); 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); void sys_info_print_arch(void); diff --git a/platform/linux-generic/odp_system_info.c b/platform/linux-generic/odp_system_info.c index d6eeb786..b53b74c3 100644 --- a/platform/linux-generic/odp_system_info.c +++ b/platform/linux-generic/odp_system_info.c @@ -261,6 +261,30 @@ static char *get_hugepage_dir(uint64_t hugepage_sz) } /* + * Analysis of /sys/devices/system/cpu/cpu%d/cpufreq/ files + */ +uint64_t odp_cpufreq_id(const char *filename, int id) +{ + char path[256], buffer[256], *endptr = NULL; + FILE *file; + uint64_t ret = 0; + + snprintf(path, sizeof(path), + "/sys/devices/system/cpu/cpu%d/cpufreq/%s", id, filename); + + file = fopen(path, "r"); + if (file == NULL) + return ret; + + if (fgets(buffer, sizeof(buffer), file) != NULL) + ret = strtoull(buffer, &endptr, 0); + + fclose(file); + + return ret; +} + +/* * Analysis of /sys/devices/system/cpu/ files */ static int systemcpu(system_info_t *sysinfo) @@ -310,6 +334,7 @@ static int system_hp(hugepage_info_t *hugeinfo) */ int odp_system_info_init(void) { + int i; FILE *file; memset(&odp_global_data.system_info, 0, sizeof(system_info_t)); @@ -326,6 +351,10 @@ int odp_system_info_init(void) fclose(file); + for (i = 0; i < MAX_CPU_NUMBER; i++) + odp_global_data.system_info.cpu_hz_max[i] = + odp_cpufreq_id("cpuinfo_max_freq", i); + if (systemcpu(&odp_global_data.system_info)) { ODP_ERR("systemcpu failed\n"); return -1; @@ -351,6 +380,11 @@ int odp_system_info_term(void) * Public access functions ************************* */ +uint64_t odp_cpu_hz_current(int id) +{ + return odp_cpufreq_id("cpuinfo_cur_freq", id); +} + uint64_t odp_cpu_hz(void) { int id = sched_getcpu();