[3/3] linux-generic: sysinfo: add API to get current CPU frequency

Message ID 1435223104-15434-4-git-send-email-hongbo.zhang@freescale.com
State New
Headers show

Commit Message

hongbo.zhang@freescale.com June 25, 2015, 9:05 a.m.
From: Hongbo Zhang <hongbo.zhang@linaro.org>

This patch add API to return the current frequency of the CPU on which
the thread is running.

Only x86 platform is implemented, others are to be done.

Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org>
---
 include/odp/api/system_info.h            |  7 ++++++
 platform/linux-generic/odp_system_info.c | 40 ++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

Comments

Mike Holmes June 29, 2015, 8:40 p.m. | #1
This touches include/odp/api so the subject must include API-NEXT

On 25 June 2015 at 05:05, <hongbo.zhang@freescale.com> wrote:

> From: Hongbo Zhang <hongbo.zhang@linaro.org>
>
> This patch add API to return the current frequency of the CPU on which
> the thread is running.
>
> Only x86 platform is implemented, others are to be done.
>

If accepted this patch would break the ARM regression for this function
because it is x86 only, need to add stubs for all platforms. (assuming the
needed tests are added to the validation suite)


>
> Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org>
> ---
>  include/odp/api/system_info.h            |  7 ++++++
>  platform/linux-generic/odp_system_info.c | 40
> ++++++++++++++++++++++++++++++++
>  2 files changed, 47 insertions(+)
>
> diff --git a/include/odp/api/system_info.h b/include/odp/api/system_info.h
> index 55a4180..02fabd3 100644
> --- a/include/odp/api/system_info.h
> +++ b/include/odp/api/system_info.h
> @@ -33,6 +33,13 @@ uint64_t odp_sys_cpu_hz(void);
>  uint64_t odp_sys_cpu_hz_amp(int cpu);
>
>  /**
> + * CPU current frequency in Hz
> + *
> + * @return CPU current frequency in Hz
>

Need to specify that -1 is returned on error


> + */
> +uint64_t odp_sys_cpu_hz_current(void);
> +
> +/**
>   * Huge page size in bytes
>   *
>   * @return Huge page size in bytes
> diff --git a/platform/linux-generic/odp_system_info.c
> b/platform/linux-generic/odp_system_info.c
> index 3ca8b27..624d75d 100644
> --- a/platform/linux-generic/odp_system_info.c
> +++ b/platform/linux-generic/odp_system_info.c
> @@ -145,6 +145,41 @@ static int cpuinfo_x86(FILE *file, odp_system_info_t
> *sysinfo)
>         return 0;
>  }
>
> +static uint64_t arch_cpu_hz_current(void)
> +{
> +       char str[1024];
> +       FILE *file;
> +       int cpu_curr, cpu;
> +       char *pos;
> +       double mhz = 0.0;
> +
> +       file = fopen("/proc/cpuinfo", "rt");
> +       cpu_curr = sched_getcpu();
> +
>

/* find the correct processor instance */


> +       while (fgets(str, sizeof(str), file) != NULL) {
> +               pos = strstr(str, "processor");
> +               if (pos) {
> +                       sscanf(pos, "processor : %d", &cpu);
> +                       if (cpu == cpu_curr)
> +                               break;
> +               }
> +       }
> +
>

/* extract the cpu current speed */


> +       while (fgets(str, sizeof(str), file) != NULL) {
> +               pos = strstr(str, "cpu MHz");
> +               if (pos) {
> +                       sscanf(pos, "cpu MHz : %lf", &mhz);
> +                       break;
> +               }
> +       }
> +
> +       fclose(file);
> +       if (mhz)
> +               return (uint64_t)(mhz * 1000000.0);
> +
> +       return -1;
> +}
> +
>  #elif defined __arm__ || defined __aarch64__
>
>  static int cpuinfo_arm(FILE *file ODP_UNUSED,
> @@ -378,6 +413,11 @@ uint64_t odp_sys_cpu_hz_amp(int cpu)
>                 return -1;
>  }
>
> +uint64_t odp_sys_cpu_hz_current(void)
> +{
> +       return arch_cpu_hz_current();
> +}
> +
>  uint64_t odp_sys_huge_page_size(void)
>  {
>         return odp_global_data.system_info.huge_page_size;
> --
> 1.9.1
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> https://lists.linaro.org/mailman/listinfo/lng-odp
>
Hongbo Zhang July 1, 2015, 10:22 a.m. | #2
On 30 June 2015 at 04:40, Mike Holmes <mike.holmes@linaro.org> wrote:
> This touches include/odp/api so the subject must include API-NEXT
>
> On 25 June 2015 at 05:05, <hongbo.zhang@freescale.com> wrote:
>>
>> From: Hongbo Zhang <hongbo.zhang@linaro.org>
>>
>> This patch add API to return the current frequency of the CPU on which
>> the thread is running.
>>
>> Only x86 platform is implemented, others are to be done.
>
>
> If accepted this patch would break the ARM regression for this function
> because it is x86 only, need to add stubs for all platforms. (assuming the
> needed tests are added to the validation suite)
>

Good catch, will add stubs.
And let's implement other platforms later, but for ARM there is more
thing to do since there isn't enough info in /proc/cpuinfo as x86's.

ALL OK for the other comments, thanks for review.


>>
>>
>> Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org>
>> ---
>>  include/odp/api/system_info.h            |  7 ++++++
>>  platform/linux-generic/odp_system_info.c | 40
>> ++++++++++++++++++++++++++++++++
>>  2 files changed, 47 insertions(+)
>>
>> diff --git a/include/odp/api/system_info.h b/include/odp/api/system_info.h
>> index 55a4180..02fabd3 100644
>> --- a/include/odp/api/system_info.h
>> +++ b/include/odp/api/system_info.h
>> @@ -33,6 +33,13 @@ uint64_t odp_sys_cpu_hz(void);
>>  uint64_t odp_sys_cpu_hz_amp(int cpu);
>>
>>  /**
>> + * CPU current frequency in Hz
>> + *
>> + * @return CPU current frequency in Hz
>
>
> Need to specify that -1 is returned on error
>
>>
>> + */
>> +uint64_t odp_sys_cpu_hz_current(void);
>> +
>> +/**
>>   * Huge page size in bytes
>>   *
>>   * @return Huge page size in bytes
>> diff --git a/platform/linux-generic/odp_system_info.c
>> b/platform/linux-generic/odp_system_info.c
>> index 3ca8b27..624d75d 100644
>> --- a/platform/linux-generic/odp_system_info.c
>> +++ b/platform/linux-generic/odp_system_info.c
>> @@ -145,6 +145,41 @@ static int cpuinfo_x86(FILE *file, odp_system_info_t
>> *sysinfo)
>>         return 0;
>>  }
>>
>> +static uint64_t arch_cpu_hz_current(void)
>> +{
>> +       char str[1024];
>> +       FILE *file;
>> +       int cpu_curr, cpu;
>> +       char *pos;
>> +       double mhz = 0.0;
>> +
>> +       file = fopen("/proc/cpuinfo", "rt");
>> +       cpu_curr = sched_getcpu();
>> +
>
>
> /* find the correct processor instance */
>
>>
>> +       while (fgets(str, sizeof(str), file) != NULL) {
>> +               pos = strstr(str, "processor");
>> +               if (pos) {
>> +                       sscanf(pos, "processor : %d", &cpu);
>> +                       if (cpu == cpu_curr)
>> +                               break;
>> +               }
>> +       }
>> +
>
>
> /* extract the cpu current speed */
>
>>
>> +       while (fgets(str, sizeof(str), file) != NULL) {
>> +               pos = strstr(str, "cpu MHz");
>> +               if (pos) {
>> +                       sscanf(pos, "cpu MHz : %lf", &mhz);
>> +                       break;
>> +               }
>> +       }
>> +
>> +       fclose(file);
>> +       if (mhz)
>> +               return (uint64_t)(mhz * 1000000.0);
>> +
>> +       return -1;
>> +}
>> +
>>  #elif defined __arm__ || defined __aarch64__
>>
>>  static int cpuinfo_arm(FILE *file ODP_UNUSED,
>> @@ -378,6 +413,11 @@ uint64_t odp_sys_cpu_hz_amp(int cpu)
>>                 return -1;
>>  }
>>
>> +uint64_t odp_sys_cpu_hz_current(void)
>> +{
>> +       return arch_cpu_hz_current();
>> +}
>> +
>>  uint64_t odp_sys_huge_page_size(void)
>>  {
>>         return odp_global_data.system_info.huge_page_size;
>> --
>> 1.9.1
>>
>> _______________________________________________
>> lng-odp mailing list
>> lng-odp@lists.linaro.org
>> https://lists.linaro.org/mailman/listinfo/lng-odp
>
>
>
>
> --
> Mike Holmes
> Technical Manager - Linaro Networking Group
> Linaro.org │ Open source software for ARM SoCs
>
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> https://lists.linaro.org/mailman/listinfo/lng-odp
>

Patch

diff --git a/include/odp/api/system_info.h b/include/odp/api/system_info.h
index 55a4180..02fabd3 100644
--- a/include/odp/api/system_info.h
+++ b/include/odp/api/system_info.h
@@ -33,6 +33,13 @@  uint64_t odp_sys_cpu_hz(void);
 uint64_t odp_sys_cpu_hz_amp(int cpu);
 
 /**
+ * CPU current frequency in Hz
+ *
+ * @return CPU current frequency in Hz
+ */
+uint64_t odp_sys_cpu_hz_current(void);
+
+/**
  * Huge page size in bytes
  *
  * @return Huge page size in bytes
diff --git a/platform/linux-generic/odp_system_info.c b/platform/linux-generic/odp_system_info.c
index 3ca8b27..624d75d 100644
--- a/platform/linux-generic/odp_system_info.c
+++ b/platform/linux-generic/odp_system_info.c
@@ -145,6 +145,41 @@  static int cpuinfo_x86(FILE *file, odp_system_info_t *sysinfo)
 	return 0;
 }
 
+static uint64_t arch_cpu_hz_current(void)
+{
+	char str[1024];
+	FILE *file;
+	int cpu_curr, cpu;
+	char *pos;
+	double mhz = 0.0;
+
+	file = fopen("/proc/cpuinfo", "rt");
+	cpu_curr = sched_getcpu();
+
+	while (fgets(str, sizeof(str), file) != NULL) {
+		pos = strstr(str, "processor");
+		if (pos) {
+			sscanf(pos, "processor : %d", &cpu);
+			if (cpu == cpu_curr)
+				break;
+		}
+	}
+
+	while (fgets(str, sizeof(str), file) != NULL) {
+		pos = strstr(str, "cpu MHz");
+		if (pos) {
+			sscanf(pos, "cpu MHz : %lf", &mhz);
+			break;
+		}
+	}
+
+	fclose(file);
+	if (mhz)
+		return (uint64_t)(mhz * 1000000.0);
+
+	return -1;
+}
+
 #elif defined __arm__ || defined __aarch64__
 
 static int cpuinfo_arm(FILE *file ODP_UNUSED,
@@ -378,6 +413,11 @@  uint64_t odp_sys_cpu_hz_amp(int cpu)
 		return -1;
 }
 
+uint64_t odp_sys_cpu_hz_current(void)
+{
+	return arch_cpu_hz_current();
+}
+
 uint64_t odp_sys_huge_page_size(void)
 {
 	return odp_global_data.system_info.huge_page_size;