Skip to contentSkip to conversation

implement set_enabled functions on powercap.c

PATCH Superseded Vishnu Sanal T 4 participants 5 messages Oct 19, 2024 – Nov 3, 2024 CPU power monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 implement set_enabled functions on powercap.c

Implement the functions sysfs_set_enabled, powercap_set_enabled, and powercap_zone_set_enabled on powercap.c.

---
 tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 6 deletions(-)
tools/power/cpupower/lib/powercap.c +37 -6
index 94a0c69e55ef..1cf2b0de5536 100644
@@ -70,6 +70,29 @@ static int sysfs_get_enabled(char *path, int *mode)
 	return ret;
 }
 
+static int sysfs_set_enabled(char *path, int mode)
+{
+	int fd;
+	char yes_no = (char) (mode + '0');
+	int ret = 0;
+
+	fd = open(path, O_RDWR);
+	if (fd == -1) {
+		ret = -1;
+		goto out;
+	}
+
+	if (write(fd, &yes_no, 1) != 1) {
+		ret = -1;
+		goto out_close;
+	}
+
+out_close:
+	close(fd);
+out:
+	return ret;
+}
+
 int powercap_get_enabled(int *mode)
 {
 	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +100,11 @@ 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);
 }
 
 /*
@@ -180,8 +202,17 @@ 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] = PATH_TO_POWERCAP;
+
+	if ((strlen(PATH_TO_POWERCAP) + strlen(zone->sys_name)) +
+	    strlen("/enabled") + 1 >= SYSFS_PATH_MAX)
+		return -1;
+
+	strcat(path, "/");
+	strcat(path, zone->sys_name);
+	strcat(path, "/enabled");
+
+	return sysfs_set_enabled(path, mode);
 }
 
 
Signature · 1 line
2.47.0
maintainer
#1 lore raw
Quoted 29 lines · Vishnu Sanal T

Implement the functions sysfs_set_enabled, powercap_set_enabled, and powercap_zone_set_enabled on powercap.c.

---
  tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++++----
  1 file changed, 37 insertions(+), 6 deletions(-)
tools/power/cpupower/lib/powercap.c +13 -0
index 94a0c69e55ef..1cf2b0de5536 100644
@@ -70,6 +70,29 @@ static int sysfs_get_enabled(char *path, int *mode)
  	return ret;
  }
 
+static int sysfs_set_enabled(char *path, int mode)
+{
+	int fd;
+	char yes_no = (char) (mode + '0');
+	int ret = 0;
+
+	fd = open(path, O_RDWR);
+	if (fd == -1) {
+		ret = -1;
+		goto out;
+	}
+
+	if (write(fd, &yes_no, 1) != 1) {

tools/power/cpupower/lib/powercap.c static int sysfs_get_enabled(char *path, int *mode) · line 95

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +10 -0
+		ret = -1;
+		goto out_close;
+	}
+
+out_close:
+	close(fd);
+out:
+	return ret;
+}
+

Why can't this all be simplified using system("echo 1 filename")

That goes for existing get routines.

Quoted 29 lines
tools/power/cpupower/lib/powercap.c +7 -6
  int powercap_get_enabled(int *mode)
  {
  	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +100,11 @@ 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);
  }
 
  /*
@@ -180,8 +202,17 @@ 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] = PATH_TO_POWERCAP;
+
+	if ((strlen(PATH_TO_POWERCAP) + strlen(zone->sys_name)) +
+	    strlen("/enabled") + 1 >= SYSFS_PATH_MAX)

tools/power/cpupower/lib/powercap.c int powercap_zone_get_enabled(struct powercap_zone *zone, int *mode) · line 218

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +7 -0
+		return -1;
+
+	strcat(path, "/");
+	strcat(path, zone->sys_name);
+	strcat(path, "/enabled");
+
+	return sysfs_set_enabled(path, mode);
  }
 
 

thanks,
-- Shuah

#2 lore raw

PATCH v2 1/1 implement set_enabled functions on powercap.c

In reply to Vishnu Sanal T

Implement the functions sysfs_set_enabled, powercap_set_enabled, and powercap_zone_set_enabled on powercap.c.

---
Changes in v2:

- simplify the implementation of sysfs_set_enabled using system() function.

---
 tools/power/cpupower/lib/powercap.c | 31 +++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
tools/power/cpupower/lib/powercap.c +25 -6
index 94a0c69e55ef..a2c0c33b7729 100644
@@ -70,6 +70,17 @@ static int sysfs_get_enabled(char *path, int *mode)
 	return ret;
 }
 
+static int sysfs_set_enabled(char *path, int mode)
+{
+	char command[SYSFS_PATH_MAX + 10];
+
+	char yes_no = (char)(mode + '0');
+
+	sprintf(command, "echo -n %c > %s", yes_no, path);
+
+	return system(command);
+}
+
 int powercap_get_enabled(int *mode)
 {
 	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +88,11 @@ 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);
 }
 
 /*
@@ -180,8 +190,17 @@ 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] = PATH_TO_POWERCAP;
+
+	if ((strlen(PATH_TO_POWERCAP) + strlen(zone->sys_name)) +
+	    strlen("/enabled") + 1 >= SYSFS_PATH_MAX)
+		return -1;
+
+	strcat(path, "/");
+	strcat(path, zone->sys_name);
+	strcat(path, "/enabled");
+
+	return sysfs_set_enabled(path, mode);
 }
 
 
Signature · 1 line
2.47.0
maintainer
#3 lore raw
Quoted 9 lines · Vishnu Sanal T

Implement the functions sysfs_set_enabled, powercap_set_enabled, and powercap_zone_set_enabled on powercap.c.

---
Changes in v2:

- simplify the implementation of sysfs_set_enabled using system() function.

Hello Vishnu,

Ty for sending a patch to implement this.

I got several warnings trying to compile your patch. Please resolve them. I have not tested your code yet. How are you testing your code?

Quoted 9 lines
---
 tools/power/cpupower/lib/powercap.c | 31 +++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
tools/power/cpupower/lib/powercap.c +0 -0
index 94a0c69e55ef..a2c0c33b7729 100644

tools/power/cpupower/lib/powercap.c static int sysfs_get_enabled(char *path, int *mode) · line 77

Quoted 8 lines
tools/power/cpupower/lib/powercap.c +5 -0
@@ -70,6 +70,17 @@ static int sysfs_get_enabled(char *path, int *mode)
 	return ret;
 }
 
+static int sysfs_set_enabled(char *path, int mode)
+{
+	char command[SYSFS_PATH_MAX + 10];
+
+	char yes_no = (char)(mode + '0');

What does yes or no mean for setting a value?

is_set or using something with the word 'mode' would be a better variable name?

tools/power/cpupower/lib/powercap.c static int sysfs_get_enabled(char *path, int *mode) · line 83

Quoted 6 lines
tools/power/cpupower/lib/powercap.c +6 -0
+
+	sprintf(command, "echo -n %c > %s", yes_no, path);
+
+	return system(command);
+}
+

In function ‘sysfs_set_enabled’,
inlined from ‘powercap_set_enabled’ at lib/powercap.c:95:9:
lib/powercap.c:79:9: note: ‘sprintf’ output between 13 and 267 bytes into a destination of size 265

   79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lib/powercap.c: In function ‘powercap_zone_set_enabled’:
lib/powercap.c:79:40: warning: ‘%s’ directive writing up to 254 bytes into a region of size 253 [-Wformat-overflow=]
79 | sprintf(command, "echo -n %c > %s", yes_no, path);
| ^~
......
203 | return sysfs_set_enabled(path, mode);
| ~~~~
In function ‘sysfs_set_enabled’,
inlined from ‘powercap_zone_set_enabled’ at lib/powercap.c:203:9:
lib/powercap.c:79:9: note: ‘sprintf’ output between 13 and 267 bytes into a destination of size 265

   79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines
tools/power/cpupower/lib/powercap.c +0 -0
 int powercap_get_enabled(int *mode)
 {
 	char path[SYSFS_PATH_MAX] = PATH_TO_POWERCAP "/intel-rapl/enabled";
@@ -77,12 +88,11 @@ 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 96

Quoted 10 lines
tools/power/cpupower/lib/powercap.c +3 -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);
 }

lib/powercap.c: In function ‘powercap_set_enabled’:
lib/powercap.c:79:40: warning: ‘%s’ directive writing up to 254 bytes into a region of size 253 [-Wformat-overflow=]
79 | sprintf(command, "echo -n %c > %s", yes_no, path);
| ^~
......
95 | return sysfs_set_enabled(path, mode);

Quoted 26 lines
tools/power/cpupower/lib/powercap.c +11 -2
 /*
@@ -180,8 +190,17 @@ 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] = PATH_TO_POWERCAP;
+
+	if ((strlen(PATH_TO_POWERCAP) + strlen(zone->sys_name)) +
+	    strlen("/enabled") + 1 >= SYSFS_PATH_MAX)
+		return -1;
+
+	strcat(path, "/");
+	strcat(path, zone->sys_name);
+	strcat(path, "/enabled");
+
+	return sysfs_set_enabled(path, mode);
 }
 
 
Signature · 2 lines
-- 
2.47.0
Signature · 4 lines
Sincerely,
John Wyatt
Software Engineer, Core Kernel
Red Hat
#4 lore raw
In reply to Vishnu Sanal T
kernel test robot noticed the following build warnings:

Hi Vishnu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on amd-pstate/linux-next]
[also build test WARNING on amd-pstate/bleeding-edge linus/master v6.12-rc5 next-20241101] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vishnu-Sanal-T/implement-set_enabled-functions-on-powercap-c/20241030-233021
base:   https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next
patch link:    https://lore.kernel.org/r/20241030152706.179779-2-t.v.s10123%40gmail.com
patch subject: [PATCH v2] implement set_enabled functions on powercap.c
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241104/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

lib/powercap.c: In function 'powercap_set_enabled':

>> lib/powercap.c:79:40: warning: '%s' directive writing up to 254 bytes into a region of size 253 [-Wformat-overflow=]
      79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
         |                                        ^~
   ......
      95 |         return sysfs_set_enabled(path, mode);
         |                                  ~~~~   
   In function 'sysfs_set_enabled',
       inlined from 'powercap_set_enabled' at lib/powercap.c:95:9:
   lib/powercap.c:79:9: note: 'sprintf' output between 13 and 267 bytes into a destination of size 265
      79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/powercap.c: In function 'powercap_zone_set_enabled':
>> lib/powercap.c:79:40: warning: '%s' directive writing up to 254 bytes into a region of size 253 [-Wformat-overflow=]
      79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
         |                                        ^~
   ......
     203 |         return sysfs_set_enabled(path, mode);
         |                                  ~~~~   
   In function 'sysfs_set_enabled',
       inlined from 'powercap_zone_set_enabled' at lib/powercap.c:203:9:
   lib/powercap.c:79:9: note: 'sprintf' output between 13 and 267 bytes into a destination of size 265
      79 |         sprintf(command, "echo -n %c > %s", yes_no, path);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki