[PATCH] tools/power x86_energy_perf_policy: Add Android support for MSR device path

Kaushlendra Kumar posted 1 patch 6 months, 3 weeks ago
.../x86_energy_perf_policy.c                  | 28 +++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
[PATCH] tools/power x86_energy_perf_policy: Add Android support for MSR device path
Posted by Kaushlendra Kumar 6 months, 3 weeks ago
This patch adds support for Android by updating the MSR device path
handling in x86_energy_perf_policy. The code now uses /dev/msrN on
Android systems instead of the default /dev/cpu/N/msr path. Error
messages and modprobe instructions are also updated accordingly to
improve clarity for Android users.

Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
 .../x86_energy_perf_policy.c                  | 28 +++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
index ebda9c366b2b..0c42cecca6a3 100644
--- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
+++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
@@ -682,11 +682,19 @@ int get_msr(int cpu, int offset, unsigned long long *msr)
 	int retval;
 	char pathname[32];
 	int fd;
-
+#if defined(ANDROID)
+	sprintf(pathname, "/dev/msr%d", cpu);
+#else
 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
+#endif
 	fd = open(pathname, O_RDONLY);
 	if (fd < 0)
+#if defined(ANDROID)
+		err(-1, "%s open failed, try chown or chmod +r /dev/msr*, or run as root",
+		    pathname);
+#else
 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
+#endif
 
 	retval = pread(fd, msr, sizeof(*msr), offset);
 	if (retval != sizeof(*msr)) {
@@ -706,11 +714,19 @@ int put_msr(int cpu, int offset, unsigned long long new_msr)
 	char pathname[32];
 	int retval;
 	int fd;
-
+#if defined(ANDROID)
+	sprintf(pathname, "/dev/msr%d", cpu);
+#else
 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
+#endif
 	fd = open(pathname, O_RDWR);
 	if (fd < 0)
+#if defined(ANDROID)
+		err(-1, "%s open failed, try chown or chmod +r /dev//msr*, or run as root",
+		    pathname);
+#else
 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
+#endif
 
 	retval = pwrite(fd, &new_msr, sizeof(new_msr), offset);
 	if (retval != sizeof(new_msr))
@@ -1385,10 +1401,18 @@ void probe_dev_msr(void)
 	struct stat sb;
 	char pathname[32];
 
+#if defined(ANDROID)
+	sprintf(pathname, "/dev/msr%d", base_cpu);
+#else
 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
+#endif
 	if (stat(pathname, &sb))
 		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
+#if defined(ANDROID)
+			err(-5, "no /dev/msr0, Try \"# modprobe msr\" ");
+#else
 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
+#endif
 }
 
 static void get_cpuid_or_exit(unsigned int leaf,
-- 
2.34.1
Re: [PATCH] tools/power x86_energy_perf_policy: Add Android support for MSR device path
Posted by Len Brown 4 months, 1 week ago
I regret applying the analogous turbostat patch, and I don't want to
apply this patch.

So Android moved /dev/cpu/0/msr to /dev/msr0 ...
Assuming that they had a really really good reason to break
compatibility (perhaps for the rest of us, you could share that
reason?)...

I would rather the tools probed the compatible/incompatible paths at
initialization time than littering the code with #ifdef ANDROID.

thanks,
-Len

On Fri, May 23, 2025 at 1:21 AM Kaushlendra Kumar
<kaushlendra.kumar@intel.com> wrote:
>
> This patch adds support for Android by updating the MSR device path
> handling in x86_energy_perf_policy. The code now uses /dev/msrN on
> Android systems instead of the default /dev/cpu/N/msr path. Error
> messages and modprobe instructions are also updated accordingly to
> improve clarity for Android users.
>
> Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
> ---
>  .../x86_energy_perf_policy.c                  | 28 +++++++++++++++++--
>  1 file changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> index ebda9c366b2b..0c42cecca6a3 100644
> --- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> +++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> @@ -682,11 +682,19 @@ int get_msr(int cpu, int offset, unsigned long long *msr)
>         int retval;
>         char pathname[32];
>         int fd;
> -
> +#if defined(ANDROID)
> +       sprintf(pathname, "/dev/msr%d", cpu);
> +#else
>         sprintf(pathname, "/dev/cpu/%d/msr", cpu);
> +#endif
>         fd = open(pathname, O_RDONLY);
>         if (fd < 0)
> +#if defined(ANDROID)
> +               err(-1, "%s open failed, try chown or chmod +r /dev/msr*, or run as root",
> +                   pathname);
> +#else
>                 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
> +#endif
>
>         retval = pread(fd, msr, sizeof(*msr), offset);
>         if (retval != sizeof(*msr)) {
> @@ -706,11 +714,19 @@ int put_msr(int cpu, int offset, unsigned long long new_msr)
>         char pathname[32];
>         int retval;
>         int fd;
> -
> +#if defined(ANDROID)
> +       sprintf(pathname, "/dev/msr%d", cpu);
> +#else
>         sprintf(pathname, "/dev/cpu/%d/msr", cpu);
> +#endif
>         fd = open(pathname, O_RDWR);
>         if (fd < 0)
> +#if defined(ANDROID)
> +               err(-1, "%s open failed, try chown or chmod +r /dev//msr*, or run as root",
> +                   pathname);
> +#else
>                 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
> +#endif
>
>         retval = pwrite(fd, &new_msr, sizeof(new_msr), offset);
>         if (retval != sizeof(new_msr))
> @@ -1385,10 +1401,18 @@ void probe_dev_msr(void)
>         struct stat sb;
>         char pathname[32];
>
> +#if defined(ANDROID)
> +       sprintf(pathname, "/dev/msr%d", base_cpu);
> +#else
>         sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
> +#endif
>         if (stat(pathname, &sb))
>                 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
> +#if defined(ANDROID)
> +                       err(-5, "no /dev/msr0, Try \"# modprobe msr\" ");
> +#else
>                         err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
> +#endif
>  }
>
>  static void get_cpuid_or_exit(unsigned int leaf,
> --
> 2.34.1
>


-- 
Len Brown, Intel Open Source Technology Center