Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1892485rdf; Sun, 5 Nov 2023 19:56:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFyV47IlC1FrR7anbLww8NXkvqaQme2tiP8mkPh9faviZdLW+ZGwSSSRL/L1ML7pRfYH9wp X-Received: by 2002:a05:6a00:b8e:b0:68a:5395:7aa5 with SMTP id g14-20020a056a000b8e00b0068a53957aa5mr32746034pfj.17.1699242968960; Sun, 05 Nov 2023 19:56:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699242968; cv=none; d=google.com; s=arc-20160816; b=s2HhGUEzpB0UNeZ/L9rMrcbwHOBfCxd3q69LFFYeyVXSMXM8gGW4fY043eR7TgvbrQ qGdMDO5UsHu9rB6iefd5XDgXBvW7NL0LRO4EE5AsKJbSlFNRmn4U0+PEz8nTcCGjcjX3 iKRb5XXfsl7K4RiDK0kE82Su0p2yaYsmEVyAXKc7Sb/D5/oKL7RxdRoQ4u7x0ueNFpny Se+bL8ZcIvfyQ0bT5+IKNMRZvzkbJXIU3/QB9fHwGOcPeS1uy63azjv0gjnzdHipTpzR 4dfEsxOg9Sl82C5yPQA4k5Zg/DsxpgbW/uK4OfiDGa31WQNfBUivxNCKCsKJ5BBTEJ5m g/1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=/DJy+/mELaaRrgZdhbRe6xxPliCvYeBMFHwJRis0Xx8=; fh=mapERlogJ/cQt0xtLiOgnrZ5zpp0wMkpfbiUTcLZi4s=; b=ookb+TLwG7jKqAIU2um9zqr+XQzMYIZot2qcFfBdydRARy9LGjMkg9VBtvGN69lOFA pmpvFu+WzIs7bC33MzYOx0Z1DeB9o9ulTxoMnueSBi7OOF1W0nkhoirq5pCJ60nTS93L Kjfh7bt5pApG7XmbdG/3nrzB82mCWUP+9Rru46HG4FT42yyC95Ftu0687p58zauNLQUd UueGxDiNw+GgEMycosBlBLRrQNYslV/XIvYlEdyN7N+r258JdJPDjKSsN1FB80u7vQT/ pUL+nqFenBIo0+VKIkqdBgvAnfZ2ieZ4+xh43bHZEh99UH7OmZLFnuVL80opi7DCoevj YrMw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id l184-20020a633ec1000000b0057ab7f46381si6488663pga.76.2023.11.05.19.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 19:56:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 83DA2804C6CA; Sun, 5 Nov 2023 19:56:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230145AbjKFDz7 convert rfc822-to-8bit (ORCPT + 99 others); Sun, 5 Nov 2023 22:55:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjKFDz6 (ORCPT ); Sun, 5 Nov 2023 22:55:58 -0500 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-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) 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: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 05 Nov 2023 19:56:06 -0800 (PST) On Thu, Nov 2, 2023 at 10:58 AM Ian Rogers 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 > --- > 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 = 0; > > - ret = sysfs__read_str(entry, &buf, &size); > - if (ret < 0) > - return ret; > + io.fd = 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 = -1; > } > - > - free(buf); > + close(io.fd); > > return ret; > } > -- > 2.42.0.869.gea05f2083d-goog >