diff mbox series

implement set_enabled functions on powercap.c

Message ID 20241019124233.194140-2-t.v.s10123@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Shuah Khan
Headers show
Series implement set_enabled functions on powercap.c | expand

Commit Message

Vishnu Sanal T Oct. 19, 2024, 12:42 p.m. UTC
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>
---
 tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 6 deletions(-)

Comments

Shuah Khan Oct. 21, 2024, 8:36 p.m. UTC | #1
On 10/19/24 06:42, 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>
> ---
>   tools/power/cpupower/lib/powercap.c | 43 +++++++++++++++++++++++++----
>   1 file changed, 37 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
> index 94a0c69e55ef..1cf2b0de5536 100644
> --- a/tools/power/cpupower/lib/powercap.c
> +++ b/tools/power/cpupower/lib/powercap.c
> @@ -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;
> +}
> +

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

That goes for existing get routines.

>   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);
>   }
>   
>   

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
index 94a0c69e55ef..1cf2b0de5536 100644
--- a/tools/power/cpupower/lib/powercap.c
+++ b/tools/power/cpupower/lib/powercap.c
@@ -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);
 }