From patchwork Sat Nov 5 02:51:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho-Ren (Jack) Chuang" X-Patchwork-Id: 13032810 X-Patchwork-Delegate: bpf@iogearbox.net 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 E032BC4167B for ; Sat, 5 Nov 2022 02:52:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229567AbiKECwb (ORCPT ); Fri, 4 Nov 2022 22:52:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229570AbiKECwa (ORCPT ); Fri, 4 Nov 2022 22:52:30 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23F32419B5 for ; Fri, 4 Nov 2022 19:52:29 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id i9so4421457qki.10 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=k8+i/dUdmml+rad/TXxIRyCYnQBTl3e8ElKKKTU/U8ONPDChfMw3XiWSoEh1QBhLe8 mnElwMVfhEB4Y7IqG3gU9eX13xKkihMNC7QuTLmwLwgTy8+nczA+cQZvri86hLLsVWlb sql1NWsY89yIQxaZXfbqbJESkm3Fv65mmlhOAS+GUfVsmryauhCo9WEHbhVnjy0TaJPR 02oo0O1Pgor0FkcfOCleymLsOn4aphew2kQzfi/qxldAefSnOML/XcZYchEq5HKjd5eh AitKAsXWJ5AZERJwet/QjJgD2hMHPKYWlPSRbNRmVPZ/J5+xLFk2EtSDdlPK65MwBbN5 /pBg== X-Gm-Message-State: ACrzQf2zyhgS5L71W0hRvGlMH6/VGfMOUGMhptSfGjMub/zHROw052qu d/nhCanj2XeM5wFnBZz8/mY5oA== 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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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_info *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 == BPF_MAP_TYPE_HASH) + jsonw_uint_field(json_wtr, "used_entries", info->used_entries); 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 = get_fdinfo(fd, "frozen"); 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 == BPF_MAP_TYPE_HASH) + printf(" used_entries %u", info->used_entries); 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];