From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 470E21843 for ; Mon, 6 Nov 2023 03:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=none Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26156F9; Sun, 5 Nov 2023 19:55:56 -0800 (PST) Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5b92b670f2aso3218210a12.2; Sun, 05 Nov 2023 19:55:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699242955; x=1699847755; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QfWESybjsduF9qwoLWKD5+nRstHYc4pbbbkyPkX7v3A=; b=OgztEGUK5KEaQMF2EViuF3gWUTTcpqFPwNBPUG40xh3/IZ7hNDFbaR1hqwg5gfCjVg iWlzY+tBxsdVY8A/dmxp3b31WAq6Ts76RYxQX7W6CU/mX8rWrEl4t3sObwTc7dmOdrtO BAwveymhv/USTGnnJseRBbbWI+wZaN4Cm8dmi4YnWufoDLMmwbqtuhfPeovpoo2/j+DI N5D3qHQSByktQH8TYj+GRT6+VV9m6SbmmrC230YzVs1wg3ISB0lPph5pm9bW8++RJV6A 03ekkbybgHbFxXs1cUZwZ36N1kfhroRZqFwZxxDuWlZ/aqoJnb11YxRtJThev3HkmqX+ BFDw== X-Gm-Message-State: AOJu0YwQhMs3FuuoiXHdMfaP1O7h9CdikJ0zOBI7CMenV0bShnrP6e6G 8Ur/ay0DelZJ3iSq/g1cigDlOclom9T9IVDkVXY= X-Google-Smtp-Source: AGHT+IGbzywoiCWSH7SJOoGMLj3HgkTdhIfrlAkfTOdxwAJ0kx0qGL3BL1wIafykBBczR49bXKwLpmnOoHl//fXz4SQ= X-Received: by 2002:a05:6300:8004:b0:181:61ad:3aeb with SMTP id an4-20020a056300800400b0018161ad3aebmr19226207pzc.43.1699242955563; Sun, 05 Nov 2023 19:55:55 -0800 (PST) Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> <20231102175735.2272696-8-irogers@google.com> In-Reply-To: <20231102175735.2272696-8-irogers@google.com> From: Namhyung Kim Date: Sun, 5 Nov 2023 19:55:44 -0800 Message-ID: Subject: Re: [PATCH v4 07/53] tools api fs: Avoid reading whole file for a 1 byte bool To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Nov 2, 2023 at 10:58=E2=80=AFAM Ian Rogers wro= te: > > 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 > --- > tools/lib/api/fs/fs.c | 18 +++++++++--------- > 1 file changed, 9 insertions(+), 9 deletions(-) > > diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c > index 496812b5f1d2..4c35a689d1fc 100644 > --- a/tools/lib/api/fs/fs.c > +++ b/tools/lib/api/fs/fs.c > @@ -447,15 +447,16 @@ 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 =3D 0; > > - ret =3D sysfs__read_str(entry, &buf, &size); > - if (ret < 0) > - return ret; > + io.fd =3D open(entry, O_RDONLY); The entry is a name in sysfs, so you need to get the full name. Thanks, Namhyung > + 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 +470,7 @@ int sysfs__read_bool(const char *entry, bool *value) > default: > ret =3D -1; > } > - > - free(buf); > + close(io.fd); > > return ret; > } > -- > 2.42.0.869.gea05f2083d-goog >