[v5,05/50] tools api fs: Avoid reading whole file for a 1 byte bool

Message ID 20231127220902.1315692-6-irogers@google.com
State New
Headers
Series Improvements to memory use |

Commit Message

Ian Rogers Nov. 27, 2023, 10:08 p.m. UTC
  sysfs__read_bool used the first byte from a fully read file into a
string. It then looked at the first byte's value. Avoid doing this and
just read the first byte.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/api/fs/fs.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
  

Comments

Namhyung Kim Nov. 30, 2023, 1:42 a.m. UTC | #1
On Mon, Nov 27, 2023 at 2:09 PM Ian Rogers <irogers@google.com> wrote:
>
> sysfs__read_bool used the first byte from a fully read file into a
> string. It then looked at the first byte's value. Avoid doing this and
> just read the first byte.
>
> Signed-off-by: Ian Rogers <irogers@google.com>

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung

> ---
>  tools/lib/api/fs/fs.c | 24 +++++++++++++++---------
>  1 file changed, 15 insertions(+), 9 deletions(-)
>
> diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
> index 004f2af5504b..337fde770e45 100644
> --- a/tools/lib/api/fs/fs.c
> +++ b/tools/lib/api/fs/fs.c
> @@ -447,15 +447,22 @@ int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
>
>  int sysfs__read_bool(const char *entry, bool *value)
>  {
> -       char *buf;
> -       size_t size;
> -       int ret;
> +       struct io io;
> +       char bf[16];
> +       int ret = 0;
> +       char path[PATH_MAX];
> +       const char *sysfs = sysfs__mountpoint();
> +
> +       if (!sysfs)
> +               return -1;
>
> -       ret = sysfs__read_str(entry, &buf, &size);
> -       if (ret < 0)
> -               return ret;
> +       snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
> +       io.fd = open(path, O_RDONLY);
> +       if (io.fd < 0)
> +               return -errno;
>
> -       switch (buf[0]) {
> +       io__init(&io, io.fd, bf, sizeof(bf));
> +       switch (io__get_char(&io)) {
>         case '1':
>         case 'y':
>         case 'Y':
> @@ -469,8 +476,7 @@ int sysfs__read_bool(const char *entry, bool *value)
>         default:
>                 ret = -1;
>         }
> -
> -       free(buf);
> +       close(io.fd);
>
>         return ret;
>  }
> --
> 2.43.0.rc1.413.gea7ed67945-goog
>
  

Patch

diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
index 004f2af5504b..337fde770e45 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -447,15 +447,22 @@  int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
 
 int sysfs__read_bool(const char *entry, bool *value)
 {
-	char *buf;
-	size_t size;
-	int ret;
+	struct io io;
+	char bf[16];
+	int ret = 0;
+	char path[PATH_MAX];
+	const char *sysfs = sysfs__mountpoint();
+
+	if (!sysfs)
+		return -1;
 
-	ret = sysfs__read_str(entry, &buf, &size);
-	if (ret < 0)
-		return ret;
+	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+	io.fd = open(path, O_RDONLY);
+	if (io.fd < 0)
+		return -errno;
 
-	switch (buf[0]) {
+	io__init(&io, io.fd, bf, sizeof(bf));
+	switch (io__get_char(&io)) {
 	case '1':
 	case 'y':
 	case 'Y':
@@ -469,8 +476,7 @@  int sysfs__read_bool(const char *entry, bool *value)
 	default:
 		ret = -1;
 	}
-
-	free(buf);
+	close(io.fd);
 
 	return ret;
 }