Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp6672rdb; Wed, 29 Nov 2023 17:43:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGhd0tHgHLUsLwfuL1QI8jjeFQEL9S6SnF8nl83h3o0Q3rtH5ggH7H0mwl99nJ9HsGNQkg6 X-Received: by 2002:a05:6a00:8d87:b0:6cd:e65f:f6c9 with SMTP id im7-20020a056a008d8700b006cde65ff6c9mr446512pfb.2.1701308595147; Wed, 29 Nov 2023 17:43:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701308595; cv=none; d=google.com; s=arc-20160816; b=krLKh/YPSUzG0ZckCcNM484j9nz2QhogRQO9R5vEOkV3ypHOcEIHU0yy1PqnUqPwg1 uxbHDagNAfTfq74cgFeVZEz2W88kqIm/jA8ZO3tg7Q66o+FcSxESoVyQfCajUr9VSTFD gGYa8ZNbt0XrXAeCtXufR+tsHTSDqd0UDrjhQpyHKEa7BwGpN4lodtPs3n4CulE0WAKU 6Wsy/27qBCQVcFjAGKQu9GJQBGbYkcTrGUqetKbDFE+PiotarXviiUwUXMkmZdNeFIvL Lhalr+PfzQO3y77ogqfvXkjIhQ5rvJeXBHxzk5sL9jd78VRCXFjkpaoL0kC2mkRsncJv zynA== 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=m2d0pXHawm5ofNwhlGrTqmgmXQXftIYyfQm7Pdrs8qI=; fh=aWa9tyepd0GWyBFjkZa5ys7uNwpZ1AjfSj0Mu1iuQbE=; b=HJxbNXqRAXWQ6JR89dt3lwdR2X6foxNN7wyRAI8rxeYlb2YEB70hMmdLjxQMWXa+z0 njR/cHJH4vLK/kwPIh5X5Z7eTEt1O/p/XZJR6ohs5Rpof58z8F7WZTfAIMjY/+Lk4FQ/ OQY2/HKCePEMD9z7yIGUTmoYJoVJgnpl3gmfOoALM4r4g+czE2y5UT39UwuD8QsPIcSL yf/sI2BnNbznvN9Mp9jP2Zpvzi5Su881NKwW34TorHxJcCKFANPRFfIqo5QYk/E0/j5Y id080slA6ud/CtNc4U+Opz1qFzXShUfasHG1uHAHwdsF5n5l2c8/XNxcJ2MkVQXpxRQw IQBw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id j12-20020a056a00174c00b006c1691263cesi89676pfc.291.2023.11.29.17.43.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 17:43:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 37F5981F378B; Wed, 29 Nov 2023 17:43:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231574AbjK3Bmx convert rfc822-to-8bit (ORCPT + 99 others); Wed, 29 Nov 2023 20:42:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231477AbjK3Bmv (ORCPT ); Wed, 29 Nov 2023 20:42:51 -0500 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BF3395; Wed, 29 Nov 2023 17:42:58 -0800 (PST) Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2856254bd74so456968a91.2; Wed, 29 Nov 2023 17:42:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701308578; x=1701913378; 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=ivoJxkCdzt3z1KRXcXZxqZP8Xkb6cmsg3m3L6Nbeo2w=; b=QTglPJA3ByKUKhnDH3eWl5l1kKBfj3Xv1XhDXQUyhK3Fua3BS5BryUg+7RAFPUCPpn /IZY3VbJHUR5qfIJqAdtMMqIsLj03h9Hav2Yu0uH50AJjLCBdDu5AXkHi3bviejepPai iQwWIWG7pQZKuQDyK0n0dVWb3ewo3y1enif/7x28tHReWUUqm3TuhzZpzuXjquCFkCyG FSnWb13WCyfdCF1MVpShcD5tG4SP1WfsyQiUyuk6KB455nuyBxXXExdmjTuUZEhHD4IM p+bTIefiz5mHjY7FJsXG3jW6UUpWd718luz54PLLcmT611am57ZCpdTZPHMxo52g8b/P qu/A== X-Gm-Message-State: AOJu0Yxpv4+et9z31x7foeS1H8cS7fYyZNPXAdtv26/lqHVYooLByI+I FVTSCGQhJr3s7Oxl2R4O0H9hIC6x5ja3/Tqiqms= X-Received: by 2002:a17:90b:17cf:b0:285:a18e:7565 with SMTP id me15-20020a17090b17cf00b00285a18e7565mr15721075pjb.5.1701308577748; Wed, 29 Nov 2023 17:42:57 -0800 (PST) MIME-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> <20231127220902.1315692-6-irogers@google.com> In-Reply-To: <20231127220902.1315692-6-irogers@google.com> From: Namhyung Kim Date: Wed, 29 Nov 2023 17:42:46 -0800 Message-ID: Subject: Re: [PATCH v5 05/50] 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, Guilherme Amadio 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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 29 Nov 2023 17:43:12 -0800 (PST) On Mon, Nov 27, 2023 at 2:09 PM 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 Acked-by: Namhyung Kim 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 >