diff options
author | Petri Savolainen <petri.savolainen@linaro.org> | 2018-06-27 17:10:16 +0300 |
---|---|---|
committer | Maxim Uvarov <maxim.uvarov@linaro.org> | 2018-07-05 14:59:56 +0300 |
commit | a9e3b02a54cd9a18baf8042e389ffbdb9266a9c7 (patch) | |
tree | 51092d0e1c9b2fc0f2233427b4249cc49b01b852 | |
parent | ce5951842845b519c7887479c8e4e128e1951add (diff) |
linux-gen: sysinfo: use cpufreq for max freq by default
Usually, maximum CPU frequency is found from a cpufreq file.
Read that file first, if it's not found use cpuinfo instead.
If max freq cannot be found, use hard coded value and print
a warning.
Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org>
Reviewed-by: Bill Fischofer <bill.fischofer@linaro.org>
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
7 files changed, 38 insertions, 24 deletions
diff --git a/platform/linux-generic/arch/default/odp_sysinfo_parse.c b/platform/linux-generic/arch/default/odp_sysinfo_parse.c index 2c3bf8411..b93788872 100644 --- a/platform/linux-generic/arch/default/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/default/odp_sysinfo_parse.c @@ -10,13 +10,17 @@ #include <odp_debug_internal.h> #include <string.h> +#define DUMMY_MAX_MHZ 1400 + int cpuinfo_parser(FILE *file ODP_UNUSED, system_info_t *sysinfo) { int i; ODP_DBG("Warning: use dummy values for freq and model string\n"); for (i = 0; i < CONFIG_NUM_CPU; i++) { - sysinfo->cpu_hz_max[i] = 1400000000; + ODP_PRINT("WARN: cpu[%i] uses dummy max frequency %u MHz\n", + i, DUMMY_MAX_MHZ); + sysinfo->cpu_hz_max[i] = DUMMY_MAX_MHZ * 1000000; strcpy(sysinfo->model_str[i], "UNKNOWN"); } diff --git a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c index 927cb83fa..7b313b6d0 100644 --- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c @@ -41,7 +41,7 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) pos = strchr(str, ':'); strncpy(sysinfo->model_str[id], pos + 2, - sizeof(sysinfo->model_str[id]) - 1); + MODEL_STR_SIZE - 1); len = strlen(sysinfo->model_str[id]); sysinfo->model_str[id][len - 1] = 0; model = 1; diff --git a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c index daa72c24c..1fb1b4a3e 100644 --- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c @@ -40,7 +40,7 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) pos = strchr(str, ':'); strncpy(sysinfo->model_str[id], pos + 2, - sizeof(sysinfo->model_str[id]) - 1); + MODEL_STR_SIZE - 1); len = strlen(sysinfo->model_str[id]); sysinfo->model_str[id][len - 1] = 0; model = 1; diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c index f9a219752..504aa3efa 100644 --- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c +++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c @@ -13,7 +13,7 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) { char str[1024]; - char *pos; + char *pos, *pos_end; double ghz = 0.0; uint64_t hz; int id = 0; @@ -22,17 +22,25 @@ int cpuinfo_parser(FILE *file, system_info_t *sysinfo) while (fgets(str, sizeof(str), file) != NULL && id < CONFIG_NUM_CPU) { pos = strstr(str, "model name"); if (pos) { - pos = strchr(str, ':'); + /* Copy model name between : and @ characters */ + pos = strchr(str, ':'); + pos_end = strchr(str, '@'); + if (pos == NULL || pos_end == NULL) + continue; + + *(pos_end - 1) = '\0'; strncpy(sysinfo->model_str[id], pos + 2, - sizeof(sysinfo->model_str[id]) - 1); + MODEL_STR_SIZE - 1); + + if (sysinfo->cpu_hz_max[id]) { + id++; + continue; + } - pos = strchr(sysinfo->model_str[id], '@'); - if (pos) { - *(pos - 1) = '\0'; - if (sscanf(pos, "@ %lfGHz", &ghz) == 1) { - hz = (uint64_t)(ghz * 1000000000.0); - sysinfo->cpu_hz_max[id] = hz; - } + /* max frequency needs to be set */ + if (sscanf(pos_end, "@ %lfGHz", &ghz) == 1) { + hz = (uint64_t)(ghz * 1000000000.0); + sysinfo->cpu_hz_max[id] = hz; } id++; } diff --git a/platform/linux-generic/include/odp_global_data.h b/platform/linux-generic/include/odp_global_data.h index 4a8702a63..009862a8b 100644 --- a/platform/linux-generic/include/odp_global_data.h +++ b/platform/linux-generic/include/odp_global_data.h @@ -19,6 +19,7 @@ extern "C" { #include <libconfig.h> #include <odp_config_internal.h> +#define MODEL_STR_SIZE 128 #define UID_MAXLEN 30 typedef struct { @@ -27,7 +28,7 @@ typedef struct { int cache_line_size; int cpu_count; char cpu_arch_str[128]; - char model_str[CONFIG_NUM_CPU][128]; + char model_str[CONFIG_NUM_CPU][MODEL_STR_SIZE]; } system_info_t; typedef struct { diff --git a/platform/linux-generic/include/odp_sysinfo_internal.h b/platform/linux-generic/include/odp_sysinfo_internal.h index d34087e21..2f01d18e4 100644 --- a/platform/linux-generic/include/odp_sysinfo_internal.h +++ b/platform/linux-generic/include/odp_sysinfo_internal.h @@ -14,7 +14,6 @@ extern "C" { #include <odp_global_data.h> 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); diff --git a/platform/linux-generic/odp_system_info.c b/platform/linux-generic/odp_system_info.c index 09e184a79..bca02ba14 100644 --- a/platform/linux-generic/odp_system_info.c +++ b/platform/linux-generic/odp_system_info.c @@ -265,7 +265,7 @@ 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) +static uint64_t read_cpufreq(const char *filename, int id) { char path[256], buffer[256], *endptr = NULL; FILE *file; @@ -343,23 +343,25 @@ int odp_system_info_init(void) odp_global_data.system_info.page_size = ODP_PAGE_SIZE; + /* By default, read max frequency from a cpufreq file */ + for (i = 0; i < CONFIG_NUM_CPU; i++) { + uint64_t cpu_hz_max = read_cpufreq("cpuinfo_max_freq", i); + + if (cpu_hz_max) + odp_global_data.system_info.cpu_hz_max[i] = cpu_hz_max; + } + file = fopen("/proc/cpuinfo", "rt"); if (file == NULL) { ODP_ERR("Failed to open /proc/cpuinfo\n"); return -1; } + /* Read CPU model, and set max cpu frequency if not set from cpufreq. */ cpuinfo_parser(file, &odp_global_data.system_info); fclose(file); - for (i = 0; i < CONFIG_NUM_CPU; i++) { - uint64_t cpu_hz_max = odp_cpufreq_id("cpuinfo_max_freq", i); - - if (cpu_hz_max) - odp_global_data.system_info.cpu_hz_max[i] = cpu_hz_max; - } - if (systemcpu(&odp_global_data.system_info)) { ODP_ERR("systemcpu failed\n"); return -1; @@ -387,7 +389,7 @@ int odp_system_info_term(void) */ uint64_t odp_cpu_hz_current(int id) { - uint64_t cur_hz = odp_cpufreq_id("cpuinfo_cur_freq", id); + uint64_t cur_hz = read_cpufreq("cpuinfo_cur_freq", id); if (!cur_hz) cur_hz = odp_cpu_arch_hz_current(id); |