From: <sur...@am...> - 2011-05-30 19:26:34
|
From: Suravee Suthikulpanit <sur...@am...> Query the maximum cpu frequency from /sys/devices/system/cpu/cpu<n>/cpufreq/cpuinfo_max_freq by default. In case this is not available, fallback to use /proc/cpuinfo Signed-off-by: Suravee Suthikulpanit <sur...@am...> diff --git a/libutil/op_cpufreq.c b/libutil/op_cpufreq.c index c95cb58..453f0a9 100644 --- a/libutil/op_cpufreq.c +++ b/libutil/op_cpufreq.c @@ -2,11 +2,12 @@ * @file op_cpufreq.c * get cpu frequency definition * - * @remark Copyright 2003 OProfile authors + * @remark Copyright 2011 OProfile authors * @remark Read the file COPYING * * @author John Levon * @author Philippe Elie + * @author Suravee Suthikulpanit */ #include <stdio.h> @@ -14,7 +15,13 @@ #include "op_fileio.h" -double op_cpu_frequency(void) +/* + * Use frequency information from /proc/cpuinfo. This should be available in most case. + * However, this number is the current cpu frequency which could be in the idle state. + * We should actually report the max frequency because the cpu should switch into the + * highest power state when running. + */ +static double op_cpu_freq_cpuinfo(void) { double fval = 0.0; unsigned long uval; @@ -67,3 +74,52 @@ double op_cpu_frequency(void) return fval; } + + +/* + * Use frequency information from /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq + * This number is the maximum cpu frequency of the processor. + */ +static double op_cpu_freq_sys_devices(void) +{ + double fval = 0.0; + char * line = NULL; + + FILE * fp = op_try_open_file("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); + if (!fp) + return 0.0; + + while (1) { + line = op_get_line(fp); + + if (!line) + break; + + if (line[0] == '\0') { + free(line); + continue; + } + + if (sscanf(line, "%lf", &fval) == 1) + break; + + free(line); + } + + if (line) + free(line); + op_close_file(fp); + + return fval; +} + + +double op_cpu_frequency(void) +{ + // Use the max cpu frequency from /sys/devices when available + double freq = op_cpu_freq_sys_devices(); + if (freq == 0) { + freq = op_cpu_freq_cpuinfo(); + } + return freq; +} diff --git a/libutil/op_cpufreq.h b/libutil/op_cpufreq.h index 0a71172..0f87688 100644 --- a/libutil/op_cpufreq.h +++ b/libutil/op_cpufreq.h @@ -2,11 +2,12 @@ * @file op_cpufreq.h * get cpu frequency declaration * - * @remark Copyright 2003 OProfile authors + * @remark Copyright 2011 OProfile authors * @remark Read the file COPYING * * @author John Levon * @author Philippe Elie + * @author Suravee Suthikulpanit */ #ifndef OP_CPUFREQ_H @@ -17,8 +18,8 @@ extern "C" { #endif /* - * return the estimated cpu frequency in Mhz through - * parsing /proc/cpuinfo, return 0 if this information + * return the estimated cpu frequency in Mhz, + * return 0 if this information * is not avalaible e.g. sparc64 with a non SMP kernel */ double op_cpu_frequency(void); -- 1.7.4.1 |