Message ID | 20241030152706.179779-2-t.v.s10123@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | [v2] implement set_enabled functions on powercap.c | expand |
On Wed, Oct 30, 2024 at 08:57:07PM +0530, Vishnu Sanal T wrote: > Implement the functions sysfs_set_enabled, powercap_set_enabled, > and powercap_zone_set_enabled on powercap.c. > > Signed-off-by: Vishnu Sanal T <t.v.s10123@gmail.com> > --- > 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? > --- > tools/power/cpupower/lib/powercap.c | 31 +++++++++++++++++++++++------ > 1 file changed, 25 insertions(+), 6 deletions(-) > > diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c > index 94a0c69e55ef..a2c0c33b7729 100644 > --- a/tools/power/cpupower/lib/powercap.c > +++ b/tools/power/cpupower/lib/powercap.c > @@ -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? > + > + 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); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > 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); > } 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); > > /* > @@ -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); > } > > > -- > 2.47.0 >
diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c index 94a0c69e55ef..a2c0c33b7729 100644 --- a/tools/power/cpupower/lib/powercap.c +++ b/tools/power/cpupower/lib/powercap.c @@ -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); }
Implement the functions sysfs_set_enabled, powercap_set_enabled, and powercap_zone_set_enabled on powercap.c. Signed-off-by: Vishnu Sanal T <t.v.s10123@gmail.com> --- 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(-)