Skip to contentSkip to conversation

tools/powercap: Implement powercap_set_enabled()

PATCH In review Suchit Karunakaran 2 participants 4 messages May 10, 2025 – May 14, 2025 CPU power monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/powercap: Implement powercap_set_enabled()

The powercap_set_enabled() function previously returned a dummy value and was marked with a TODO comment. This patch implements the function by writing the desired mode (0 or 1) to /sys/class/powercap/intel-rapl/enabled

---
 tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/powercap.c +18 -4
index 94a0c69e55ef..7947b9809239 100644
@@ -70,6 +70,22 @@ static int sysfs_get_enabled(char *path, int *mode)
 	return ret;
 }
 
+static int sysfs_set_enabled(const char *path, int mode)
+{
+	int fd;
+	char buf[2] = { mode ? '1' : '0', '\n' };
+	ssize_t ret;
+
+	fd = open(path, O_WRONLY);
+	if (fd == -1)
+		return -1;
+
+	ret = write(fd, buf, sizeof(buf));
+	close(fd);
+
+	return ret == sizeof(buf) ? 0 : -1;
+}
+
 int powercap_get_enabled(int *mode)
 {
 	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +93,10 @@ 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;
+	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
+	return sysfs_set_enabled(path, mode);
 }
 
 /*
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 3 lines · Suchit Karunakaran

The powercap_set_enabled() function previously returned a dummy value and was marked with a TODO comment. This patch implements the function by writing the desired mode (0 or 1) to /sys/class/powercap/intel-rapl/enabled

The short summary should say cpupower: Implement powercap_set_enabled()

Quoted 35 lines
---
  tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/powercap.c +16 -0
index 94a0c69e55ef..7947b9809239 100644
@@ -70,6 +70,22 @@ static int sysfs_get_enabled(char *path, int *mode)
  	return ret;
  }
 
+static int sysfs_set_enabled(const char *path, int mode)
+{
+	int fd;
+	char buf[2] = { mode ? '1' : '0', '\n' };
+	ssize_t ret;
+
+	fd = open(path, O_WRONLY);
+	if (fd == -1)
+		return -1;
+
+	ret = write(fd, buf, sizeof(buf));
+	close(fd);
+
+	return ret == sizeof(buf) ? 0 : -1;
+}
+
  int powercap_get_enabled(int *mode)
  {
  	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +93,10 @@ int powercap_get_enabled(int *mode)
  	return sysfs_get_enabled(path, mode);

tools/power/cpupower/lib/powercap.c int powercap_get_enabled(int *mode) · line 99

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +2 -4
  }
 
-/*
- * TODO: implement function. Returns dummy 0 for now.
- */
  int powercap_set_enabled(int mode)
  {
-	return 0;
+	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
+	return sysfs_set_enabled(path, mode);

Did you compile this?

thanks,
-- Shuah

author
#2 lore raw

Thanks for the suggestion, Shuah. And I did compile the kernel after making the changes.

Quoted 57 lines · Shuah Khan
Quoted 3 lines · Suchit Karunakaran

The powercap_set_enabled() function previously returned a dummy value and was marked with a TODO comment. This patch implements the function by writing the desired mode (0 or 1) to /sys/class/powercap/intel-rapl/enabled

The short summary should say cpupower: Implement powercap_set_enabled()

Quoted 35 lines
---
  tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/powercap.c +16 -0
index 94a0c69e55ef..7947b9809239 100644
@@ -70,6 +70,22 @@ static int sysfs_get_enabled(char *path, int *mode)
      return ret;
  }
 
+static int sysfs_set_enabled(const char *path, int mode)
+{
+     int fd;
+     char buf[2] = { mode ? '1' : '0', '\n' };
+     ssize_t ret;
+
+     fd = open(path, O_WRONLY);
+     if (fd == -1)
+             return -1;
+
+     ret = write(fd, buf, sizeof(buf));
+     close(fd);
+
+     return ret == sizeof(buf) ? 0 : -1;
+}
+
  int powercap_get_enabled(int *mode)
  {
      char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +93,10 @@ int powercap_get_enabled(int *mode)
      return sysfs_get_enabled(path, mode);

tools/power/cpupower/lib/powercap.c int powercap_get_enabled(int *mode) · line 99

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +2 -4
  }
 
-/*
- * TODO: implement function. Returns dummy 0 for now.
- */
  int powercap_set_enabled(int mode)
  {
-     return 0;
+     char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
+     return sysfs_set_enabled(path, mode);

Did you compile this?

thanks,
-- Shuah

maintainer
#3 lore raw
Quoted 4 lines · Suchit K

Thanks for the suggestion, Shuah. And I did compile the kernel after making the changes.

Please don't top post in kernel patch responses.

Quoted 8 lines · Shuah Khan
Quoted 7 lines · Suchit Karunakaran
Quoted 3 lines

The powercap_set_enabled() function previously returned a dummy value and was marked with a TODO comment. This patch implements the function by writing the desired mode (0 or 1) to /sys/class/powercap/intel-rapl/enabled

The short summary should say cpupower: Implement powercap_set_enabled()

I am not clear on how this can be used. Can you elaborate on the use-case for the set?

Send me v2 fixing the change log.

Quoted 50 lines
Quoted 50 lines
Quoted 35 lines
---
   tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++++++----
   1 file changed, 18 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/powercap.c +16 -0
index 94a0c69e55ef..7947b9809239 100644
@@ -70,6 +70,22 @@ static int sysfs_get_enabled(char *path, int *mode)
       return ret;
   }
 
+static int sysfs_set_enabled(const char *path, int mode)
+{
+     int fd;
+     char buf[2] = { mode ? '1' : '0', '\n' };
+     ssize_t ret;
+
+     fd = open(path, O_WRONLY);
+     if (fd == -1)
+             return -1;
+
+     ret = write(fd, buf, sizeof(buf));
+     close(fd);
+
+     return ret == sizeof(buf) ? 0 : -1;
+}
+
   int powercap_get_enabled(int *mode)
   {
       char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +93,10 @@ int powercap_get_enabled(int *mode)
       return sysfs_get_enabled(path, mode);

tools/power/cpupower/lib/powercap.c int powercap_get_enabled(int *mode) · line 99

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +2 -4
   }
 
-/*
- * TODO: implement function. Returns dummy 0 for now.
- */
   int powercap_set_enabled(int mode)
   {
-     return 0;
+     char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
+     return sysfs_set_enabled(path, mode);

Did you compile this?

thanks,
-- Shuah