Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp555211rdb; Thu, 2 Nov 2023 11:01:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG1o4WKQ9AUFVadko+WNarP36iuZVJYT57ky74EHrepFqPSJtVC7Nq0SGZMKCdKrqdhdkPr X-Received: by 2002:a17:90b:238e:b0:280:4af4:1a41 with SMTP id mr14-20020a17090b238e00b002804af41a41mr259193pjb.15.1698948089950; Thu, 02 Nov 2023 11:01:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948089; cv=none; d=google.com; s=arc-20160816; b=cDCpvag1IYOC+Ix79ESEz5idCctIB3Yd2MQj79QRpPKH2103bMkMD4pg/jY/QTaluE XJ/fEMiJn7C4Wldit2D70p8i3lC1roceRYp1jL1aKf3+OPZDm5FVMfLueQkmfYjuE7w9 aUtJAIGeWwox8VMXavD6F8Y1704kX6YEcdpzEpquwpQ7jGCMf2BxfccQp7LgRaZC5Xyd 9ew8oG4dPSgprD4E0Cie3QGXELEtexlaMzvR6xxbW/xTyNMoZIUa80y7WE37BVjJqlY1 v2cZsNyA7KDFfrNKHjp/F2lXzBBy+FdJ8iG7FoALdqF5tTA1NXteJ6QI1GL50Lf+gSX9 AcQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=gQDO1z3HzTf9lqy8F57xj4IdkjAIJI4YmDCo/V9kva4=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=A2PNl9LuPZTu5s2WaW63SlLD8zTVIKBUFLK4RokKQfjMYrYGqkXO7hfD4KgcMztUM2 Zj/2zcpbNSZPxN5kzY37eP1cK7IZsyMRfyaPop6EoEwOwp0FWYj26yXSuoUavj1Z0ybH ys4xKAygTvDo+ToeNVnTRBf/u9sQgaiw50EOpXv/ptU/fVtuvCF3l5ZsPcYlEngqJdBe GjkAe4Nuo21wyupx3CPvhkWmmnF6x9Xe86X3b7BNTN1qO5BXRFgn3yK2XIEDiQj621JK gEm6o/pS2on54PE4BswHDEZM1Mm2oLuclqiO1TubjbM4cG24BGY0s1pvISy6rnsjTWak 3x9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=3W7iaMIP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id y10-20020a17090a86ca00b00280281c4acesi232337pjv.45.2023.11.02.11.01.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:01:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=3W7iaMIP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id CC63E837EA4D; Thu, 2 Nov 2023 11:00:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234295AbjKBR6k (ORCPT + 99 others); Thu, 2 Nov 2023 13:58:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345278AbjKBR6R (ORCPT ); Thu, 2 Nov 2023 13:58:17 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAD24198 for ; Thu, 2 Nov 2023 10:58:12 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7af53bde4so17604317b3.0 for ; Thu, 02 Nov 2023 10:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947892; x=1699552692; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=gQDO1z3HzTf9lqy8F57xj4IdkjAIJI4YmDCo/V9kva4=; b=3W7iaMIPuvUaKeYlP7mwPjF5hMihBjGyYmyG1XgN3UjeXdomAPaJrjxpmVOT7zxgbH pS6rRuE9sSNlNINxQAuX0ruABGrUmfu/+bu1q1gAeUkC2qR21K/aJaShq8EiNw3Lvxge xz/2jgbcH82CBXg5hJ9RQgoNmqScZe7p5CKg5iYWYkbNSknWKMqPMSpIqaaXdqAnks1H hAq8BTt/7C/DSkcuMktOmrosvbZA0CjDbj57dDoUBHXdV/SKl+PlI1K0SneetrBRCq0k a6VyRuBBZyl5o6ZoydVZ6zPjAuuvzq+JlttilJ0Nt1BGtPxIZdzdZgUVhdC7a2cd2k2h UJzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947892; x=1699552692; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gQDO1z3HzTf9lqy8F57xj4IdkjAIJI4YmDCo/V9kva4=; b=cyXHUm52Z6YcHsmWI4IgZERQKW5IlvPRUJikecQo3r1FSCfIodGoRKQkSJLJTSMvPY Af1mY1A9pLHkdnHIXpLwIL/dJaQPsfOC8mzvRhEOlWulj22imGYVvWbJhqGADn1Ng6V9 uxXahlk1CsO/1Jtb6frFg1ezdbGz7uMlUz7GdJ5+X0uzE7ylyMkLgoB83abCRM/JYYA+ Ixop4UHNG0WdlQVLjBaBr4ZYlOj9vsA+BSo17U1+FTn5eFSohHnnrHtuN3KPrWMFK9ts on7hkYOCgLN6rPlIoEAsYMFSu7s4G4ZaZqb65QGIzEXjkmeb8UlFqmfPY8+W0IMu99L/ sTtQ== X-Gm-Message-State: AOJu0YwXqvmBcnEeOtr9kLhavX/I5z1RjFXPcnlDqxXesUUUDQSdlj+g ecPG6bIq/H9KWClAHcfukzafpdmAN5vO X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a81:a14c:0:b0:59b:f138:c845 with SMTP id y73-20020a81a14c000000b0059bf138c845mr8439ywg.2.1698947891880; Thu, 02 Nov 2023 10:58:11 -0700 (PDT) Date: Thu, 2 Nov 2023 10:56:49 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-8-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 07/53] tools api fs: Avoid reading whole file for a 1 byte bool From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , 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" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:00:57 -0700 (PDT) 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); + 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