* [PATCH] cpupower: Implement powercap enabled setters
@ 2026-05-08 22:35 Mateusz Jaśkiewicz
2026-05-11 21:01 ` Shuah Khan
0 siblings, 1 reply; 3+ messages in thread
From: Mateusz Jaśkiewicz @ 2026-05-08 22:35 UTC (permalink / raw)
To: Thomas Renninger, Shuah Khan, John B . Wyatt IV, John Kacur
Cc: linux-pm, linux-kernel, Mateusz Jaśkiewicz
powercap_set_enabled() and powercap_zone_set_enabled() are part of the
public libcpupower API, but both currently return success without
updating sysfs.
Write the requested value to the matching enabled attribute so callers
can actually enable or disable the powercap control type or zone, and
report write failures back to the caller.
Signed-off-by: Mateusz Jaśkiewicz <jaskiewiczteo@gmail.com>
---
tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++------
1 file changed, 35 insertions(+), 8 deletions(-)
diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
index 94a0c69e5..6d8b83428 100644
--- a/tools/power/cpupower/lib/powercap.c
+++ b/tools/power/cpupower/lib/powercap.c
@@ -70,6 +70,27 @@ static int sysfs_get_enabled(char *path, int *mode)
return ret;
}
+static int sysfs_set_enabled(const char *path, int mode)
+{
+ char yes_no;
+ int fd;
+ ssize_t ret;
+
+ if (mode != 0 && mode != 1)
+ return -1;
+
+ yes_no = mode ? '1' : '0';
+ fd = open(path, O_WRONLY);
+ if (-1 == fd)
+ return -1;
+ ret = write(fd, &yes_no, 1);
+ if (ret != 1) {
+ close(fd);
+ return -1;
+ }
+ return close(fd);
+}
+
int powercap_get_enabled(int *mode)
{
char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,17 +98,13 @@ int powercap_get_enabled(int *mode)
return sysfs_get_enabled(path, mode);
}
-/*
- * TODO: implement function. Returns dummy 0 for now.
- */
int powercap_set_enabled(int mode)
{
- return 0;
+ return sysfs_set_enabled(PATH_TO_RAPL "/enabled", mode);
}
-
/*
* Hardcoded, because rapl is the only powercap implementation
-- * this needs to get more generic if more powercap implementations
+ * this needs to get more generic if more powercap implementations
* should show up
*/
int powercap_get_driver(char *driver, int buflen)
@@ -180,8 +197,18 @@ int powercap_zone_get_enabled(struct powercap_zone *zone, int *mode)
int powercap_zone_set_enabled(struct powercap_zone *zone, int mode)
{
- /* To be done if needed */
- return 0;
+ char path[SYSFS_PATH_MAX];
+ int ret;
+
+ if (!zone)
+ return -1;
+
+ ret = snprintf(path, sizeof(path), "%s/%s/enabled",
+ PATH_TO_POWERCAP, zone->sys_name);
+ if (ret < 0 || ret >= (int)sizeof(path))
+ return -1;
+
+ return sysfs_set_enabled(path, mode);
}
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] cpupower: Implement powercap enabled setters
2026-05-08 22:35 [PATCH] cpupower: Implement powercap enabled setters Mateusz Jaśkiewicz
@ 2026-05-11 21:01 ` Shuah Khan
2026-05-12 16:11 ` [PATCH v2] " Mateusz Jaśkiewicz
0 siblings, 1 reply; 3+ messages in thread
From: Shuah Khan @ 2026-05-11 21:01 UTC (permalink / raw)
To: Mateusz Jaśkiewicz, Thomas Renninger, Shuah Khan,
John B . Wyatt IV, John Kacur
Cc: linux-pm, linux-kernel, Shuah Khan
On 5/8/26 16:35, Mateusz Jaśkiewicz wrote:
> powercap_set_enabled() and powercap_zone_set_enabled() are part of the
> public libcpupower API, but both currently return success without
> updating sysfs.
>
> Write the requested value to the matching enabled attribute so callers
> can actually enable or disable the powercap control type or zone, and
> report write failures back to the caller.
>
> Signed-off-by: Mateusz Jaśkiewicz <jaskiewiczteo@gmail.com>
> ---
> tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++------
> 1 file changed, 35 insertions(+), 8 deletions(-)
>
> diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
> index 94a0c69e5..6d8b83428 100644
> --- a/tools/power/cpupower/lib/powercap.c
> +++ b/tools/power/cpupower/lib/powercap.c
> @@ -70,6 +70,27 @@ static int sysfs_get_enabled(char *path, int *mode)
> return ret;
> }
>
> +static int sysfs_set_enabled(const char *path, int mode)
> +{
> + char yes_no;
> + int fd;
> + ssize_t ret;
> +
> + if (mode != 0 && mode != 1)
> + return -1;
The default is enabled. You could check the current value
and then do a write.
> +
> + yes_no = mode ? '1' : '0';
> + fd = open(path, O_WRONLY);
> + if (-1 == fd)
I prefer fd == value convention. Also checking fd < 0 is
better than == -1 and printing error message calling perror()
> + return -1;
> + ret = write(fd, &yes_no, 1);
> + if (ret != 1) {
> + close(fd);
> + return -1;
> + }
> + return close(fd);
> +}
> +
> int powercap_get_enabled(int *mode)
> {
> char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
> @@ -77,17 +98,13 @@ int powercap_get_enabled(int *mode)
> return sysfs_get_enabled(path, mode);
> }
>
> -/*
> - * TODO: implement function. Returns dummy 0 for now.
> - */
> int powercap_set_enabled(int mode)
> {
> - return 0;
> + return sysfs_set_enabled(PATH_TO_RAPL "/enabled", mode);
> }
> -
> /*
> * Hardcoded, because rapl is the only powercap implementation
> -- * this needs to get more generic if more powercap implementations
> + * this needs to get more generic if more powercap implementations
> * should show up
> */
> int powercap_get_driver(char *driver, int buflen)
> @@ -180,8 +197,18 @@ int powercap_zone_get_enabled(struct powercap_zone *zone, int *mode)
>
> int powercap_zone_set_enabled(struct powercap_zone *zone, int mode)
> {
> - /* To be done if needed */
> - return 0;
> + char path[SYSFS_PATH_MAX];
> + int ret;
> +
> + if (!zone)
> + return -1;
> +
> + ret = snprintf(path, sizeof(path), "%s/%s/enabled",
> + PATH_TO_POWERCAP, zone->sys_name);
> + if (ret < 0 || ret >= (int)sizeof(path))
Drop the typecast
> + return -1;
> +
> + return sysfs_set_enabled(path, mode);
> }
>
>
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2] cpupower: Implement powercap enabled setters
2026-05-11 21:01 ` Shuah Khan
@ 2026-05-12 16:11 ` Mateusz Jaśkiewicz
0 siblings, 0 replies; 3+ messages in thread
From: Mateusz Jaśkiewicz @ 2026-05-12 16:11 UTC (permalink / raw)
To: skhan
Cc: Thomas Renninger, Shuah Khan, John B. Wyatt IV, John Kacur,
Mateusz Jaśkiewicz, linux-pm, linux-kernel
powercap_set_enabled() and powercap_zone_set_enabled() are part of the
public libcpupower API, but both currently return success without
updating sysfs.
Write the requested value to the matching enabled attribute so callers
can actually enable or disable the powercap control type or zone, and
report write failures back to the caller.
---
Changes in v2:
- Check current enabled value before writing.
- Use fd < 0 style for open() failures.
- Print sysfs open/write failures with perror().
- Drop the sizeof(path) typecast.
Signed-off-by: Mateusz Jaśkiewicz <jaskiewiczteo@gmail.com>
---
tools/power/cpupower/lib/powercap.c | 58 +++++++++++++++++++++++------
1 file changed, 47 insertions(+), 11 deletions(-)
diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
index 94a0c69e5..80af65287 100644
--- a/tools/power/cpupower/lib/powercap.c
+++ b/tools/power/cpupower/lib/powercap.c
@@ -21,7 +21,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
ssize_t numread;
fd = open(path, O_RDONLY);
- if (fd == -1)
+ if (fd < 0)
return 0;
numread = read(fd, buf, buflen - 1);
@@ -36,7 +36,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
return (unsigned int) numread;
}
-static int sysfs_get_enabled(char *path, int *mode)
+static int sysfs_get_enabled(const char *path, int *mode)
{
int fd;
char yes_no;
@@ -45,7 +45,7 @@ static int sysfs_get_enabled(char *path, int *mode)
*mode = 0;
fd = open(path, O_RDONLY);
- if (fd == -1) {
+ if (fd < 0) {
ret = -1;
goto out;
}
@@ -70,6 +70,36 @@ static int sysfs_get_enabled(char *path, int *mode)
return ret;
}
+static int sysfs_set_enabled(const char *path, int mode)
+{
+ char yes_no;
+ int fd;
+ int current_mode;
+ ssize_t ret;
+
+ if (mode != 0 && mode != 1)
+ return -1;
+
+ if (sysfs_get_enabled(path, ¤t_mode) == 0 &&
+ current_mode == mode)
+ return 0;
+
+ yes_no = mode ? '1' : '0';
+ fd = open(path, O_WRONLY);
+ if (fd < 0) {
+ perror(path);
+ return -1;
+ }
+ ret = write(fd, &yes_no, 1);
+ if (ret != 1) {
+ if (ret < 0)
+ perror(path);
+ close(fd);
+ return -1;
+ }
+ return close(fd);
+}
+
int powercap_get_enabled(int *mode)
{
char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,17 +107,13 @@ int powercap_get_enabled(int *mode)
return sysfs_get_enabled(path, mode);
}
-/*
- * TODO: implement function. Returns dummy 0 for now.
- */
int powercap_set_enabled(int mode)
{
- return 0;
+ return sysfs_set_enabled(PATH_TO_RAPL "/enabled", mode);
}
-
/*
* Hardcoded, because rapl is the only powercap implementation
-- * this needs to get more generic if more powercap implementations
+ * this needs to get more generic if more powercap implementations
* should show up
*/
int powercap_get_driver(char *driver, int buflen)
@@ -180,8 +206,18 @@ int powercap_zone_get_enabled(struct powercap_zone *zone, int *mode)
int powercap_zone_set_enabled(struct powercap_zone *zone, int mode)
{
- /* To be done if needed */
- return 0;
+ char path[SYSFS_PATH_MAX];
+ int ret;
+
+ if (!zone)
+ return -1;
+
+ ret = snprintf(path, sizeof(path), "%s/%s/enabled",
+ PATH_TO_POWERCAP, zone->sys_name);
+ if (ret < 0 || ret >= sizeof(path))
+ return -1;
+
+ return sysfs_set_enabled(path, mode);
}
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-12 16:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-08 22:35 [PATCH] cpupower: Implement powercap enabled setters Mateusz Jaśkiewicz
2026-05-11 21:01 ` Shuah Khan
2026-05-12 16:11 ` [PATCH v2] " Mateusz Jaśkiewicz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox