From nobody Thu Apr 9 13:37:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C823FC433FE for ; Sat, 5 Nov 2022 02:52:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229628AbiKECwl (ORCPT ); Fri, 4 Nov 2022 22:52:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbiKECwa (ORCPT ); Fri, 4 Nov 2022 22:52:30 -0400 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B095419B8 for ; Fri, 4 Nov 2022 19:52:29 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id k4so4435242qkj.8 for ; Fri, 04 Nov 2022 19:52:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8AM+TWzrl1LA62BHe0rCl520VllgtghOrkiBcfVyQb4=; b=k0Jh0XkldmG8RANLS2K0+hj/5w/Kw+FdUYVrURoqA7WDh0Cm+uv9mOX10JkCNnS8NJ HnB4UGIL5WLZvvMH3MsOcD+Rf5i9kIMyTb0m/u7WvBBMjDxVshVSlBXaVUxbQYaorf4Z OLcdorfXmCYAdSAhlx0C89D8FytECOj9mSMNpOvT6YfMUIkDht2u5TNrPzYPjjFbeVDb v0mIxpscJB/OfPsp7LQOW2k22IH6mPkz5lPZgj4qFhS8ib1LgKpnXpkxSKLhoewt0uT9 iKao+31zZbLgnTvRyZWUSR+EzAhR2j5PPyZ1MVEAZt8LKQab3ub+qFmnTFYeJiF49ZJi RXZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8AM+TWzrl1LA62BHe0rCl520VllgtghOrkiBcfVyQb4=; b=U5AmWMmlkflynj0TTFU12R2XxNw3RyJYj6qPorJ49RzyPu/8cZJlCnwoMPfQ7jjDB5 7hYmILVtDeefJTg8ABKow+hxV9clWnNfSgxOyx4XXTfqDIwecLRJ/1kTF8kJRr4YuV7t FGZGaWH6enx5FaTmvHvWtW3fuRg8QrxbmIhlxgqavHL5UXoJWVrZMgeccHHd4Gs/Qr62 yo1VnfFj7yAvu3GVIdP05ptyQURTu6xtBTzQMAaT0UQ0ORmZw60cN29pEF8mL83/a+/A bnXERC2OgzveA3gcn1W38Omx1SwNZ0hLEHbSK25vFcBJNGs2DUTPq5LJgQzaXYe6jUsp 9g3w== X-Gm-Message-State: ACrzQf3gLU9oZDrWbAxIkvNI+O77z1+8lAHyrOnXLej9SlAzsUnORUp7 GEqazd8uCfA4yhtgOM6JsIo5NA== X-Google-Smtp-Source: AMsMyM4MQv6UKhI6oTX/XUK87tqEv89kBUPr7uFlabbYsRKtpfFOFV6tyueovJAuZtJwRodaCLiNpQ== X-Received: by 2002:a05:620a:21c9:b0:6fa:3046:42c0 with SMTP id h9-20020a05620a21c900b006fa304642c0mr21689269qka.628.1667616747823; Fri, 04 Nov 2022 19:52:27 -0700 (PDT) Received: from 192-168-53-12.byted.org ([130.44.212.119]) by smtp.gmail.com with ESMTPSA id ay14-20020a05620a178e00b006bb366779a4sm805905qkb.6.2022.11.04.19.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 19:52:27 -0700 (PDT) From: "Ho-Ren (Jack) Chuang" To: Alexei Starovoitov , Alexei Starovoitov , Hao Luo , Jiri Olsa , Jiri Olsa , Andrii Nakryiko , Daniel Borkmann , John Fastabend , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Quentin Monnet , Mykola Lysenko , Shuah Khan , Nathan Chancellor , Nick Desaulniers , Tom Rix , Joanne Koong , Kui-Feng Lee , Lorenzo Bianconi , Maxim Mikityanskiy , Hao Xiang , Punit Agrawal , Yifei Ma , Xiaoning Ding , bpf@vger.kernel.org Cc: Ho-Ren Chuang , Ho-Ren Chuang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH bpf-next v1 2/4] bpftool: Add tools support to show BPF htab map's used size Date: Sat, 5 Nov 2022 02:51:44 +0000 Message-Id: <20221105025146.238209-3-horenchuang@bytedance.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221105025146.238209-1-horenchuang@bytedance.com> References: <20221105025146.238209-1-horenchuang@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add bpftool support for reporting the number of used entries of htab maps by leveraging the newly added "used_entries" field in struct bpf_map_info. It works with JSON as well. To better understand actual used memory size of a htab map, pre-allocated maps are now marked with "*" behind the "max_entries" size. Signed-off-by: Ho-Ren (Jack) Chuang --- tools/bpf/bpftool/map.c | 9 +++++++-- tools/include/uapi/linux/bpf.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 9a6ca9f31133..0b07abae7309 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -475,6 +475,8 @@ static int show_map_close_json(int fd, struct bpf_map_i= nfo *info) jsonw_uint_field(json_wtr, "bytes_key", info->key_size); jsonw_uint_field(json_wtr, "bytes_value", info->value_size); jsonw_uint_field(json_wtr, "max_entries", info->max_entries); + if (info->type =3D=3D BPF_MAP_TYPE_HASH) + jsonw_uint_field(json_wtr, "used_entries", info->used_entries); =20 if (memlock) jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock)); @@ -561,8 +563,11 @@ static int show_map_close_plain(int fd, struct bpf_map= _info *info) frozen_str =3D get_fdinfo(fd, "frozen"); =20 show_map_header_plain(info); - printf("\tkey %uB value %uB max_entries %u", - info->key_size, info->value_size, info->max_entries); + printf("\tkey %uB value %uB max_entries %u%1s", + info->key_size, info->value_size, info->max_entries, + !(info->map_flags & BPF_F_NO_PREALLOC) ? "*" : ""); + if (info->type =3D=3D BPF_MAP_TYPE_HASH) + printf(" used_entries %u", info->used_entries); =20 if (memlock) printf(" memlock %sB", memlock); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 17f61338f8f8..63659368cf0e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6215,6 +6215,7 @@ struct bpf_map_info { __u32 id; __u32 key_size; __u32 value_size; + __u32 used_entries; __u32 max_entries; __u32 map_flags; char name[BPF_OBJ_NAME_LEN]; --=20 Ho-Ren (Jack) Chuang