From nobody Thu Apr 9 12:16:14 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 5798CC43219 for ; Sat, 5 Nov 2022 02:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229624AbiKECwd (ORCPT ); Fri, 4 Nov 2022 22:52:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbiKECw3 (ORCPT ); Fri, 4 Nov 2022 22:52:29 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 076943FB95 for ; Fri, 4 Nov 2022 19:52:28 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id x15so4353282qtv.9 for ; Fri, 04 Nov 2022 19:52:27 -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=YXNmxis4Zkl54qmET11cnpJJGJ2VNVNuEs1M8Jg/V1A=; b=J339RySFJwqav1kH4OJqtPfBCpgE5RHngr1ZByyAiQz4+/YWBABUyUVASMJomr0SYu +/F5foKscg1gwwsCfTKfij2Ydw0jz3qiioA0bCS3Yd3p567ij7S7h5eEAWB0i6OqnisH L7ryIg5jjZMeBqOGXon4/bnjr57Whk/rXmGM7lbqG/sr5mUcQUAlmX0gdAvzcoXjLa4Z 01GPAoepaSNkr50uTKvqsOTte64oVvI8R31TLdS+WN2+y5fZKDowIVv+NMbmCndD0v2k mIzt1voNCiZGEDHKIoDSezNIbrhhg8qL/Y3pWi57CmgFApIhP99sZTHv/Oe44F/HHV8d HWBA== 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=YXNmxis4Zkl54qmET11cnpJJGJ2VNVNuEs1M8Jg/V1A=; b=YW1UxUSIJc7M+3+TxEvI0tSt7nVOl/BbpsAaIsrR0hrmw1fEAwwx0cM+1CEFPoPDLq rUb9MlTfFVFiWGMMse2caQdPm0C4Y7VLozKCiWrCn+DTASCQ2Ow2lozisn16BJqLiOWf uaqmubbgo+HpqpHV1SADE/UDV83yOYHWyVsqhBXSUMhnE5PGDK6D3qIQT1L+CcTDyeqI OBddjChiUoITGZ2Z00BfPxB+NueuepqxxMGO2wrOIiQhQjmaDGXMuSkZ44puMXTDB+9g AZhOefhmUhvtW1GJ5PJCjB7UKBfPuGsZEdo90hs8E24UBb4+jQyFKb4IcEOr6rNZUh3V 1NzQ== X-Gm-Message-State: ACrzQf1tS+Ylkp1TrlLfWN8TstIeoat+0PETqie20yiuIofUzRjsi2fY IEWCOGtMOZd0sULL1kOkWeA96g== X-Google-Smtp-Source: AMsMyM4wFH0r7RRkGxN8+7U2AC2L41oJU/b03tdfA4/iUaRURAWce5cVa22rHQINfXrKhjSxw/2ISg== X-Received: by 2002:a05:622a:5c8d:b0:3a5:1fcb:8326 with SMTP id ge13-20020a05622a5c8d00b003a51fcb8326mr26739483qtb.498.1667616747053; 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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 19:52:26 -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 1/4] bpf: Support reporting BPF htab map's used size for monitoring Date: Sat, 5 Nov 2022 02:51:43 +0000 Message-Id: <20221105025146.238209-2-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" Expose BPF htab map's used size by counting accessed or allocated/freed elements to userspace. Leverage the htab->count value for both preallocated and dynamically allocated maps. Expose the value to a new field "used_entries" in a userspace struct bpf_map_info to allow monitoring. Support hash table type (BPF_MAP_TYPE_HASH). Signed-off-by: Ho-Ren (Jack) Chuang --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/hashtab.c | 19 +++++++++++++++++++ kernel/bpf/syscall.c | 2 ++ 4 files changed, 23 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 9e7d46d16032..82ee14139b69 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -97,6 +97,7 @@ struct bpf_map_ops { int (*map_pop_elem)(struct bpf_map *map, void *value); int (*map_peek_elem)(struct bpf_map *map, void *value); void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu); + u32 (*map_get_used_elem)(struct bpf_map *map); =20 /* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 17f61338f8f8..63659368cf0e 100644 --- a/include/uapi/linux/bpf.h +++ b/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]; diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index ed3f8a53603b..bc9c00b92e57 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -913,6 +913,7 @@ static void free_htab_elem(struct bpf_htab *htab, struc= t htab_elem *l) if (htab_is_prealloc(htab)) { check_and_free_fields(htab, l); __pcpu_freelist_push(&htab->freelist, &l->fnode); + dec_elem_count(htab); } else { dec_elem_count(htab); htab_elem_free(htab, l); @@ -994,6 +995,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_hta= b *htab, void *key, if (!l) return ERR_PTR(-E2BIG); l_new =3D container_of(l, struct htab_elem, fnode); + inc_elem_count(htab); } } else { if (is_map_full(htab)) @@ -2186,6 +2188,22 @@ static int bpf_for_each_hash_elem(struct bpf_map *ma= p, bpf_callback_t callback_f return num_elems; } =20 +u32 htab_map_get_used_elem(struct bpf_map *map) +{ + struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); + + /* The elem count may temporarily go beyond the max after + * inc_elem_count() but before dec_elem_count(). + */ + if (htab->use_percpu_counter) + return min_t(u32, htab->map.max_entries, + percpu_counter_sum(&htab->pcount) + + atomic_read(&htab->count)); + else + return min_t(u32, htab->map.max_entries, + atomic_read(&htab->count)); +} + BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab) const struct bpf_map_ops htab_map_ops =3D { .map_meta_equal =3D bpf_map_meta_equal, @@ -2202,6 +2220,7 @@ const struct bpf_map_ops htab_map_ops =3D { .map_seq_show_elem =3D htab_map_seq_show_elem, .map_set_for_each_callback_args =3D map_set_for_each_callback_args, .map_for_each_callback =3D bpf_for_each_hash_elem, + .map_get_used_elem =3D htab_map_get_used_elem, BATCH_OPS(htab), .map_btf_id =3D &htab_map_btf_ids[0], .iter_seq_info =3D &iter_seq_info, diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 7b373a5e861f..ea4828bb22ac 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4203,6 +4203,8 @@ static int bpf_map_get_info_by_fd(struct file *file, info.map_flags =3D map->map_flags; info.map_extra =3D map->map_extra; memcpy(info.name, map->name, sizeof(map->name)); + if (map->ops->map_get_used_elem) + info.used_entries =3D map->ops->map_get_used_elem(map); =20 if (map->btf) { info.btf_id =3D btf_obj_id(map->btf); --=20 Ho-Ren (Jack) Chuang From nobody Thu Apr 9 12:16:14 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 From nobody Thu Apr 9 12:16:14 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 B3A4DC4332F for ; Sat, 5 Nov 2022 02:52:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229672AbiKECwo (ORCPT ); Fri, 4 Nov 2022 22:52:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbiKECwa (ORCPT ); Fri, 4 Nov 2022 22:52:30 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 931773FB80 for ; Fri, 4 Nov 2022 19:52:29 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id hh9so4335936qtb.13 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=Wl8oQUpe8yXqSeuRtfUsJkpIGEQ98nwCnE5YcZtf2Pk=; b=D3Efmuf4WRWk4XBr8PaHosbwLBamqCB1Qq1UsUIMQ1cYAddKnpMt9REEdYbmcxPJfv mvu8px+Pp135sgK1Qrx/XyhjPfeEKOZQ2bWTNKLeAtKePRk312c/yRWK//4J1WuP3Rx7 Flmzd2PuZ3ZkysxXlU87KxkQz3vaE0Ldi5tRP4jUNvfPSf/tipTYPsGVQ+r+llHYbL78 sQvnXCg8kpohgS26Uk8QPY7Xcnt3mIcczIEnjl42LVuhQM8RYGLOer04tSFCrfeT5qPK dMTz5O5LicnnSdKZxl/36cwUf6fpIzRTo69ZPQl+xsz9nxmWyNyAW0NWAztu2irqpF52 OCew== 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=Wl8oQUpe8yXqSeuRtfUsJkpIGEQ98nwCnE5YcZtf2Pk=; b=uPH+ZqRDOP6OLXZHTzK8I8gH3iRuOqmuv8HiY9GlLuXu9cFDctT1Md4Mwk0LHboF9e 6hrbmMmmVbk2zg+BkDbdxODKJmbKG2WhhW3zOWEiBo24Z1seaxRRMpaoiQQz+1ak4EIC mkZUkpe+729o7qFn6yavQEuhMbW0ZZ1CK61fDfIrthA1ydnxNnbZmygBxXlGWSC1tk6j oHP5v9OAI75Lc0YYmpC5XSHPF6KFf11Qo0ueZ6NxKnBTlDMu2Mfi2IDpc0AGiT/uRCfS BicWwANsqKnJgyxr5NTGhtSVUyAdnF4kItAxb/hZ7sJ/cAQ+BBWsrTywh3BJsSXxHso0 GhAQ== X-Gm-Message-State: ACrzQf057c6muQCmOutbLdD7FaXB53JbD5g9/kbehMNtkSwsQ1s71B// g38dQBdbk/JCvbCRN84WytZFlQ== X-Google-Smtp-Source: AMsMyM5BzqY21BEcuHkVF4tSoDHZTYxblDnn8FA6zvN9Z3p8MctvtJZVXGyaQmL+lFTU/eXQarCXPw== X-Received: by 2002:ac8:6e86:0:b0:3a5:27f8:f3db with SMTP id c6-20020ac86e86000000b003a527f8f3dbmr23498116qtv.97.1667616748585; Fri, 04 Nov 2022 19:52:28 -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:28 -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 3/4] samples/bpf: Add concurrency testing for BPF htab map's used size Date: Sat, 5 Nov 2022 02:51:45 +0000 Message-Id: <20221105025146.238209-4-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 htab map's used_size test cases for concurrency testing. Support hash table type (BPF_MAP_TYPE_HASH). Signed-off-by: Ho-Ren (Jack) Chuang --- samples/bpf/Makefile | 4 + samples/bpf/test_map_used_kern.c | 65 ++++++++++ samples/bpf/test_map_used_user.c | 204 +++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 samples/bpf/test_map_used_kern.c create mode 100644 samples/bpf/test_map_used_user.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 727da3c5879b..8725d0d64a21 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -40,6 +40,7 @@ tprogs-y +=3D tc_l2_redirect tprogs-y +=3D lwt_len_hist tprogs-y +=3D xdp_tx_iptunnel tprogs-y +=3D test_map_in_map +tprogs-y +=3D test_map_used tprogs-y +=3D per_socket_stats_example tprogs-y +=3D xdp_rxq_info tprogs-y +=3D syscall_tp @@ -101,6 +102,7 @@ tc_l2_redirect-objs :=3D tc_l2_redirect_user.o lwt_len_hist-objs :=3D lwt_len_hist_user.o xdp_tx_iptunnel-objs :=3D xdp_tx_iptunnel_user.o test_map_in_map-objs :=3D test_map_in_map_user.o +test_map_used-objs :=3D test_map_used_user.o per_socket_stats_example-objs :=3D cookie_uid_helper_example.o xdp_rxq_info-objs :=3D xdp_rxq_info_user.o syscall_tp-objs :=3D syscall_tp_user.o @@ -153,6 +155,7 @@ always-y +=3D sampleip_kern.o always-y +=3D lwt_len_hist_kern.o always-y +=3D xdp_tx_iptunnel_kern.o always-y +=3D test_map_in_map_kern.o +always-y +=3D test_map_used_kern.o always-y +=3D tcp_synrto_kern.o always-y +=3D tcp_rwnd_kern.o always-y +=3D tcp_bufs_kern.o @@ -216,6 +219,7 @@ TPROGLDLIBS_xdp_router_ipv4 +=3D -lm -pthread TPROGLDLIBS_tracex4 +=3D -lrt TPROGLDLIBS_trace_output +=3D -lrt TPROGLDLIBS_map_perf_test +=3D -lrt +TPROGLDLIBS_test_map_used +=3D -lrt TPROGLDLIBS_test_overhead +=3D -lrt =20 # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine o= n cmdline: diff --git a/samples/bpf/test_map_used_kern.c b/samples/bpf/test_map_used_k= ern.c new file mode 100644 index 000000000000..e908593c1f09 --- /dev/null +++ b/samples/bpf/test_map_used_kern.c @@ -0,0 +1,65 @@ +/* Copyright (c) 2022 ByteDance + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ +#include +#include +#include +#include +#include +#include +#include "trace_common.h" + +#define MAX_ENTRIES 1000 + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, long); + __uint(max_entries, MAX_ENTRIES); + __uint(map_flags, BPF_F_NO_PREALLOC); +} touch_hash_no_prealloc SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, long); + __uint(max_entries, MAX_ENTRIES); +} touch_hash_prealloc SEC(".maps"); + +SEC("kprobe/" SYSCALL(sys_mount)) +int stress_hmap_alloc(struct pt_regs *ctx) +{ + u32 key, i; + long init_val =3D bpf_get_current_pid_tgid(); + +#pragma clang loop unroll(full) + for (i =3D 0; i < MAX_ENTRIES; ++i) { + key =3D i; + bpf_map_update_elem(&touch_hash_no_prealloc, + &key, &init_val, BPF_ANY); + } + + return 0; +} + +SEC("kprobe/" SYSCALL(sys_umount)) +int stress_hmap_prealloc(struct pt_regs *ctx) +{ + u32 key, i; + long init_val =3D bpf_get_current_pid_tgid(); + +#pragma clang loop unroll(full) + for (i =3D 0; i < MAX_ENTRIES; ++i) { + key =3D i; + bpf_map_update_elem(&touch_hash_prealloc, + &key, &init_val, BPF_ANY); + } + + return 0; +} + +char _license[] SEC("license") =3D "GPL"; +u32 _version SEC("version") =3D LINUX_VERSION_CODE; diff --git a/samples/bpf/test_map_used_user.c b/samples/bpf/test_map_used_u= ser.c new file mode 100644 index 000000000000..797f6ca7434d --- /dev/null +++ b/samples/bpf/test_map_used_user.c @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2022 ByteDance + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define TEST_BIT(t) (1U << (t)) +#define MAX_NR_CPUS 1024 + +static __u64 time_get_ns(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000000000ull + ts.tv_nsec; +} + +enum test_type { + HASH_TOUCH_PREALLOC, + HASH_TOUCH, + NR_TESTS, +}; + +const char *test_map_names[NR_TESTS] =3D { + [HASH_TOUCH_PREALLOC] =3D "hash_map", + [HASH_TOUCH] =3D "hash_map", +}; + +static int test_flags =3D ~0; +static __u32 num_map_entries; +static __u32 inner_lru_hash_size; +static __u32 max_cnt =3D 1000; + +static int check_test_flags(enum test_type t) +{ + return test_flags & TEST_BIT(t); +} + +static void test_hash_touch_prealloc(int cpu) +{ + __u64 start_time; + int i; + + start_time =3D time_get_ns(); + for (i =3D 0; i < max_cnt; i++) + syscall(__NR_umount2, NULL, 0); + printf("%d:hash_touch pre-alloc %lld touches per sec\n", + cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time)); +} + +static void test_hash_touch(int cpu) +{ + __u64 start_time; + int i; + + start_time =3D time_get_ns(); + for (i =3D 0; i < max_cnt; i++) + syscall(__NR_mount, NULL, NULL, NULL, 0, NULL); + printf("%d:hash_touch %lld touchess per sec\n", + cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time)); +} + +typedef void (*test_func)(int cpu); +const test_func test_funcs[] =3D { + [HASH_TOUCH_PREALLOC] =3D test_hash_touch_prealloc, + [HASH_TOUCH] =3D test_hash_touch, +}; + +static void loop(int cpu) +{ + cpu_set_t cpuset; + int i; + + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + sched_setaffinity(0, sizeof(cpuset), &cpuset); + + for (i =3D 0; i < NR_TESTS; i++) { + if (check_test_flags(i)) + test_funcs[i](cpu); + } +} + +static void run_perf_test(int tasks) +{ + pid_t pid[tasks]; + int i; + + for (i =3D 0; i < tasks; i++) { + pid[i] =3D fork(); + if (pid[i]) + printf("Spawn process #%d [%u]\n", i, pid[i]); + + if (pid[i] =3D=3D 0) { + loop(i); + exit(0); + } else if (pid[i] =3D=3D -1) { + printf("couldn't spawn #%d process\n", i); + exit(1); + } + } + for (i =3D 0; i < tasks; i++) { + int status; + + assert(waitpid(pid[i], &status, 0) =3D=3D pid[i]); + assert(status =3D=3D 0); + } +} + +static void fixup_map(struct bpf_object *obj) +{ + struct bpf_map *map; + int i; + + bpf_object__for_each_map(map, obj) { + const char *name =3D bpf_map__name(map); + + /* Only change the max_entries for the enabled test(s) */ + for (i =3D 0; i < NR_TESTS; i++) { + if (!strcmp(test_map_names[i], name) && + (check_test_flags(i))) { + bpf_map__set_max_entries(map, num_map_entries); + continue; + } + } + } + + inner_lru_hash_size =3D num_map_entries; +} + +int main(int argc, char **argv) +{ + int nr_cpus =3D sysconf(_SC_NPROCESSORS_ONLN); + struct bpf_link *links[8]; + struct bpf_program *prog; + struct bpf_object *obj; + char filename[256]; + int i =3D 0; + + if (argc > 1) + test_flags =3D atoi(argv[1]) ? : test_flags; + + if (argc > 2) + nr_cpus =3D atoi(argv[2]) ? : nr_cpus; + + if (argc > 3) + num_map_entries =3D atoi(argv[3]); + + if (argc > 4) + max_cnt =3D atoi(argv[4]); + + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + obj =3D bpf_object__open_file(filename, NULL); + if (libbpf_get_error(obj)) { + fprintf(stderr, "ERROR: opening BPF object file failed\n"); + return 0; + } + + /* resize BPF map prior to loading */ + if (num_map_entries > 0) + fixup_map(obj); + + /* load BPF program */ + if (bpf_object__load(obj)) { + fprintf(stderr, "ERROR: loading BPF object file failed\n"); + goto cleanup; + } + + bpf_object__for_each_program(prog, obj) { + links[i] =3D bpf_program__attach(prog); + if (libbpf_get_error(links[i])) { + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); + links[i] =3D NULL; + goto cleanup; + } + i++; + } + + run_perf_test(nr_cpus); + +cleanup: + for (i--; i >=3D 0; i--) + bpf_link__destroy(links[i]); + + bpf_object__close(obj); + return 0; +} --=20 Ho-Ren (Jack) Chuang From nobody Thu Apr 9 12:16:14 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 911F5C4332F for ; Sat, 5 Nov 2022 02:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229678AbiKECwq (ORCPT ); Fri, 4 Nov 2022 22:52:46 -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 S229610AbiKECwc (ORCPT ); Fri, 4 Nov 2022 22:52:32 -0400 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 764363FB95 for ; Fri, 4 Nov 2022 19:52:30 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id z30so4410436qkz.13 for ; Fri, 04 Nov 2022 19:52:30 -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=102zPobI2UCzZFYJfQ6W0YbWDm+eTOkX0gf/oCnQz2U=; b=BhkDD2m+dHxDTTE8PUg8n+8FDifwARJY/czahSUEz9t0qC/XYnI6eDSQvxa7uhHeGv l08NeotC1Xn9p98wFICv+UbKcRVQYXoIhe0yXVc0GWVq9rH7+pkF+jj1pIS7Hyd45Br2 3AQVwp/IB+SHvc+DhyFoH6lYq9HLAJIdT3/l96XmNmNmjusI9QuDKQj5kgDMOGfNHRM/ 6zL1O6bIOT2BnuZlVOhF1QyL0ZUpUs548iOfJWCDFE2zkoMVk9UXe2e6PMyGUvsEmzOJ yLKcRsOtw8NQfbgdQu5Ky4+h8EUjMm1hFDA7AUIanfg7gG+4tmQn65gWEb9jaSpN983A J/Fw== 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=102zPobI2UCzZFYJfQ6W0YbWDm+eTOkX0gf/oCnQz2U=; b=1i8BdziaDQv4woH6HMUILJWpW1y6OQ3PQPvfYYLcqpJ8m7AjNl6iRUYbLSD0qgpXXC pnMS0OiZdqx0XewfSvs+fe3H4gFqNneebe3Lskpz8a+GTkrEesCJ0l4KSppiL+o7pYLA AHNWaiSvZ5lpygajrsUg8MR+noegEw6xiL4uy5ogzGb1Nx9IZrgPLaBKdaWH0MKIm52z cauFSLxXSN7+xQghi+TykfaO7N+T1ABXc09TUEuPseBx7VwiDR9dqf3cyg7gcKyoRBCi 5yLwqQG3deczIc6Mw5gRf4A07u9Zz5cGr2f79IRd6X6nZlPYPOHlcTWHKbdAyt1jjMWh JNPQ== X-Gm-Message-State: ACrzQf2y5F96BVxL6dsXYsq5UqHC4slo9vQsK78S9esf8JeTNRYmBJ3q kWRxkCN4xt/3xCQ+AWFhC2k8PA== X-Google-Smtp-Source: AMsMyM5LsDyyCf9+Zw1mXGYA4ucuY6NTuWddGyD0n37vQWA/+Hl7Tl+10ZExPfW37GvB/rpQ5+iIiA== X-Received: by 2002:a37:69c5:0:b0:6fa:d6f:e848 with SMTP id e188-20020a3769c5000000b006fa0d6fe848mr27896590qkc.17.1667616749442; Fri, 04 Nov 2022 19:52:29 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 19:52:29 -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 4/4] selftests/bpf: Add unit tests for BPF htab map's used size Date: Sat, 5 Nov 2022 02:51:46 +0000 Message-Id: <20221105025146.238209-5-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" Integrate with existing unit tests such as basic, read/write-only htab maps, and concurrency testings for testing htab map's used_entires. Signed-off-by: Ho-Ren (Jack) Chuang --- tools/testing/selftests/bpf/test_maps.c | 74 ++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selfte= sts/bpf/test_maps.c index b73152822aa2..3bd202d27563 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -38,6 +38,8 @@ static void test_hashmap(unsigned int task, void *data) { long long key, next_key, first_key, value; int fd; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 fd =3D bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value)= , 2, &map_opts); if (fd < 0) { @@ -50,16 +52,32 @@ static void test_hashmap(unsigned int task, void *data) /* Insert key=3D1 element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) =3D=3D 0); =20 + /* Check used_entires is now 1. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 1); + value =3D 0; /* BPF_NOEXIST means add new element if it doesn't exist. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && /* key=3D1 already exists. */ errno =3D=3D EEXIST); =20 + /* Check used_entires is still 1 because we are updating + * an existing element. + */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 1); + /* -1 is an invalid flag. */ assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 && errno =3D=3D EINVAL); =20 + /* Check used_entires is still 1 because the last + * insertion was invalid. + */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 1); + /* Check that key=3D1 can be found. */ assert(bpf_map_lookup_elem(fd, &key, &value) =3D=3D 0 && value =3D=3D 123= 4); =20 @@ -68,6 +86,10 @@ static void test_hashmap(unsigned int task, void *data) /* Insert key=3D2 element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) =3D=3D 0); =20 + /* Check used_entires is now 2. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 2); + /* Check that key=3D2 matches the value and delete it */ assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) =3D=3D 0 && value= =3D=3D 1234); =20 @@ -89,6 +111,10 @@ static void test_hashmap(unsigned int task, void *data) assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && errno =3D=3D E2BIG); =20 + /* Check used_entires is now 2. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 2); + /* Update existing element, though the map is full. */ key =3D 1; assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) =3D=3D 0); @@ -102,6 +128,10 @@ static void test_hashmap(unsigned int task, void *data) key =3D 0; assert(bpf_map_delete_elem(fd, &key) < 0 && errno =3D=3D ENOENT); =20 + /* Check used_entires is now 2. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 2); + /* Iterate over two elements. */ assert(bpf_map_get_next_key(fd, NULL, &first_key) =3D=3D 0 && (first_key =3D=3D 1 || first_key =3D=3D 2)); @@ -127,6 +157,10 @@ static void test_hashmap(unsigned int task, void *data) assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && errno =3D=3D ENOENT); =20 + /* Check used_entires is now 0 because both elements were deleted. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 0); + close(fd); } =20 @@ -292,6 +326,8 @@ static void test_hashmap_walk(unsigned int task, void *= data) int fd, i, max_entries =3D 10000; long long key, value[VALUE_SIZE], next_key; bool next_key_valid =3D true; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 fd =3D helper_fill_hashmap(max_entries); =20 @@ -302,6 +338,9 @@ static void test_hashmap_walk(unsigned int task, void *= data) } =20 assert(i =3D=3D max_entries); + /* Check used_entires is now max_entries. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D max_entries); =20 assert(bpf_map_get_next_key(fd, NULL, &key) =3D=3D 0); for (i =3D 0; next_key_valid; i++) { @@ -313,6 +352,9 @@ static void test_hashmap_walk(unsigned int task, void *= data) } =20 assert(i =3D=3D max_entries); + /* Check used_entires is now max_entries. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D max_entries); =20 for (i =3D 0; bpf_map_get_next_key(fd, !i ? NULL : &key, &next_key) =3D=3D 0; i++) { @@ -322,6 +364,9 @@ static void test_hashmap_walk(unsigned int task, void *= data) } =20 assert(i =3D=3D max_entries); + /* Check used_entires is now max_entries. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D max_entries); close(fd); } =20 @@ -1303,13 +1348,14 @@ static void test_map_in_map(void) =20 static void test_map_large(void) { - struct bigkey { int a; char b[4096]; long long c; } key; int fd, i, value; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 fd =3D bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), MAP_SIZE, &map_opts); @@ -1341,6 +1387,10 @@ static void test_map_large(void) key.a =3D 1; assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno =3D=3D ENOENT); =20 + /* Check used_entires is now MAP_SIZE. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D MAP_SIZE); + close(fd); } =20 @@ -1466,6 +1516,8 @@ static void test_map_parallel(void) { int i, fd, key =3D 0, value =3D 0, j =3D 0; int data[2]; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 fd =3D bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), MAP_SIZE, &map_opts); @@ -1504,6 +1556,10 @@ static void test_map_parallel(void) value =3D=3D key); } =20 + /* Check used_entires is now MAP_SIZE. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D MAP_SIZE); + /* Now let's delete all elemenets in parallel. */ data[1] =3D DO_DELETE; run_parallel(TASKS, test_update_delete, data); @@ -1513,6 +1569,10 @@ static void test_map_parallel(void) assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno =3D=3D ENOENT); assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno =3D=3D ENOENT); =20 + /* Check used_entires is now 0. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 0); + key =3D 0; bpf_map_delete_elem(fd, &key); if (j++ < 5) @@ -1524,6 +1584,8 @@ static void test_map_rdonly(void) { int fd, key =3D 0, value =3D 0; __u32 old_flags; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 old_flags =3D map_opts.map_flags; map_opts.map_flags |=3D BPF_F_RDONLY; @@ -1546,6 +1608,10 @@ static void test_map_rdonly(void) assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno =3D=3D ENOENT); assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno =3D=3D ENOENT); =20 + /* Check used_entires is now 0. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 0); + close(fd); } =20 @@ -1553,6 +1619,8 @@ static void test_map_wronly_hash(void) { int fd, key =3D 0, value =3D 0; __u32 old_flags; + struct bpf_map_info map_info =3D {}; + __u32 info_len =3D sizeof(map_info); =20 old_flags =3D map_opts.map_flags; map_opts.map_flags |=3D BPF_F_WRONLY; @@ -1574,6 +1642,10 @@ static void test_map_wronly_hash(void) assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno =3D=3D EPERM); assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno =3D=3D EPERM); =20 + /* Check used_entires is now 1. */ + assert(bpf_obj_get_info_by_fd(fd, &map_info, &info_len) =3D=3D 0); + assert(map_info.used_entries =3D=3D 1); + close(fd); } =20 --=20 Ho-Ren (Jack) Chuang