Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp2129314rwe; Fri, 2 Sep 2022 08:57:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR67u22S/6xqOPRof+2Ksiqgif5kmrWX0xauL5tZgYCfURQuvKrUV3afEWBYrPT8OkjfFI/F X-Received: by 2002:a05:6402:50d4:b0:448:c43f:2ee1 with SMTP id h20-20020a05640250d400b00448c43f2ee1mr16031540edb.185.1662134230943; Fri, 02 Sep 2022 08:57:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662134230; cv=none; d=google.com; s=arc-20160816; b=vQIlRB2WWjIkOwYO+AAoF/GAVNl2jjdxKpxYrm9V4rMmKvarDv49VN5Ezin1PAhHgp ZugVIrTZ7Thhs63aH597FS7t20NYjOVWan6hIlWaaoxSy8H2/FdNRHGD2rnntdsOP4dz RCoQa2hwpGhlYrnsOJacY65sotAH5dFpmTj6XPks5JnJwotuxVMij82NWoYonQ4lmRUI EFDpcdsda6NSwX2n78Ed+yMgDFj41bJ5nVlmbKHU4OSdVGImskkzKRu8/p+J6EiB7nw4 3yn/dHU6R+qvStOk0gvfrHo3tHn3/5aXcyo2d9U96D8q6oO6kEoJDgtqu+INEeVYroSR YhfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=MjwZXpbWvCy5ukWnGHu8xEuyTkDJiUO1/IiFaRAIB7o=; b=QL/7+eUBZtv/TXpG0vLFH9QuhHGgAyd4af718wpPeAj/hVkbAt8wH+TiFFQ23hzwyM bHDMaJlidIORt7eXCqYJlpJrV818ADTIc/lNDckCRxiavnZQ7Gwqwc1G/acgwGkmmbtm 2TJVlZ6cg3jICkEyVitE+OHCG0DVIY0O9RLISGgMlGOKgNGRxpOy6EjY144yLWCSDfq1 KmD6cYE0Qul4yE4VFJaE9ApnEZTQZHHaG8K4nuNuGKCN2OBCrRysN1TV4g8NyNSH4EK/ n6izyivnLcEnR/srLyvwlFOjX4uBC2c8CHZxA73Nc9eIDUElmiu2870dx036YXLq4zU4 2s3w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fs37-20020a170907602500b007330c59455fsi1763126ejc.938.2022.09.02.08.56.24; Fri, 02 Sep 2022 08:57:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236766AbiIBPt0 (ORCPT + 99 others); Fri, 2 Sep 2022 11:49:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236388AbiIBPtC (ORCPT ); Fri, 2 Sep 2022 11:49:02 -0400 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB6F7DA3CA; Fri, 2 Sep 2022 08:39:32 -0700 (PDT) Received: from sslproxy04.your-server.de ([78.46.152.42]) by www62.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1oU8kv-000AgN-B3; Fri, 02 Sep 2022 17:38:53 +0200 Received: from [85.1.206.226] (helo=linux-4.home) by sslproxy04.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oU8ku-00094m-Nl; Fri, 02 Sep 2022 17:38:52 +0200 Subject: Re: [PATCH] bpf: added the account of BPF running time To: Yunhui Cui , corbet@lwn.net, ast@kernel.org, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, akpm@linux-foundation.org, hannes@cmpxchg.org, david@redhat.com, mail@christoph.anton.mitterer.name, ccross@google.com, vincent.whitchurch@axis.com, paul.gortmaker@windriver.com, peterz@infradead.org, edumazet@google.com, joshdon@google.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, bpf@vger.kernel.org References: <20220902101217.1419-1-cuiyunhui@bytedance.com> From: Daniel Borkmann Message-ID: Date: Fri, 2 Sep 2022 17:38:51 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20220902101217.1419-1-cuiyunhui@bytedance.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.6/26646/Fri Sep 2 09:55:25 2022) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/2/22 12:12 PM, Yunhui Cui wrote: [...] > index a5f21dc3c432..9cb072f9e32b 100644 > --- a/include/linux/filter.h > +++ b/include/linux/filter.h > @@ -565,6 +565,12 @@ struct sk_filter { > struct bpf_prog *prog; > }; > > +struct bpf_account { > + u64_stats_t nsecs; > + struct u64_stats_sync syncp; > +}; > +DECLARE_PER_CPU(struct bpf_account, bpftime); > + > DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key); > > typedef unsigned int (*bpf_dispatcher_fn)(const void *ctx, > @@ -577,12 +583,14 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog, > bpf_dispatcher_fn dfunc) > { > u32 ret; > + struct bpf_account *bact; > + unsigned long flags; > + u64 start = 0; > > cant_migrate(); > + start = sched_clock(); > if (static_branch_unlikely(&bpf_stats_enabled_key)) { > struct bpf_prog_stats *stats; > - u64 start = sched_clock(); > - unsigned long flags; > > ret = dfunc(ctx, prog->insnsi, prog->bpf_func); > stats = this_cpu_ptr(prog->stats); > @@ -593,6 +601,11 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog, > } else { > ret = dfunc(ctx, prog->insnsi, prog->bpf_func); > } > + bact = this_cpu_ptr(&bpftime); > + flags = u64_stats_update_begin_irqsave(&bact->syncp); > + u64_stats_add(&bact->nsecs, sched_clock() - start); > + u64_stats_update_end_irqrestore(&bact->syncp, flags); > + > return ret; The overhead this adds unconditionally is no-go. Have you tried using/improving: commit 47c09d6a9f6794caface4ad50930460b82d7c670 Author: Song Liu Date: Mon Mar 9 10:32:15 2020 -0700 bpftool: Introduce "prog profile" command With fentry/fexit programs, it is possible to profile BPF program with hardware counters. Introduce bpftool "prog profile", which measures key metrics of a BPF program. bpftool prog profile command creates per-cpu perf events. Then it attaches fentry/fexit programs to the target BPF program. The fentry program saves perf event value to a map. The fexit program reads the perf event again, and calculates the difference, which is the instructions/cycles used by the target program. Example input and output: ./bpftool prog profile id 337 duration 3 cycles instructions llc_misses 4228 run_cnt 3403698 cycles (84.08%) 3525294 instructions # 1.04 insn per cycle (84.05%) 13 llc_misses # 3.69 LLC misses per million isns (83.50%) This command measures cycles and instructions for BPF program with id 337 for 3 seconds. The program has triggered 4228 times. The rest of the output is similar to perf-stat. [...] Thanks, Daniel