Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp983052ybt; Fri, 10 Jul 2020 18:30:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpsLjV9eA6RXm1tbrXs8WX4QXckv2tTsD0frGR/OiaFs71IyZbWDk092Ml9FUJnSnldQHI X-Received: by 2002:a05:6402:1494:: with SMTP id e20mr67617208edv.2.1594431001902; Fri, 10 Jul 2020 18:30:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594431001; cv=none; d=google.com; s=arc-20160816; b=RHmiMxsQUGaMmZ/TVYBA0LBJxeL/kMjpXUVn0YvHL1j5QPygGplByDyvB4WUk75uyz OV9oTF6M03jD3IjduBLSlR8OmjEmQhndr3IpZy+QqHdn3WsBudcuyjvDiS+j1TmN6IsA Y5yUpaaeUXga5A0opmwVnxJmwQWMFqG/W67XTlLI3SEbfyT7iTQesREWpv7eFAAAUbjb si4tYeYNYFWKTt4c1L0a/1pvKXeLWSmPGl9QOpMZBuskrmnIQ63RGxstFuTtnyzoVm0r 7svh1i6K1HwkyAfEUHjS0m/USekNTO055+2eqd+5c7dYEnIabIBvLyXZLoAU7594knnw PVpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:smtp-origin-cluster :cc:to:smtp-origin-hostname:from:smtp-origin-hostprefix :dkim-signature; bh=Zt9u2EpjWjLtKzNqOLuW2yd8dLixE48PVNDjLnQBOGM=; b=kkQI+q78FXKsAnEkRGwzHmZRysd9mhwNeh5skdBNDjIXYeS0cNPgAQqrYQ7jyaVvMZ 3TOf98fz8XISvnmmVkFXIMqgtu5fTEBy91q2CvjdXqP3B/eIfS4o0WiWCjdkEAgQCDNh tacv4U/bgCs0pSceKn3+uCySCPc9ReLTxHbFYV19R4bok29TfQ/DxPSHwfii1EUpVrvc HOOTSTj9cKADualcWjp/8ZzQNOCmO1AP2UhDKPc+1iDmheOUv7CNzR39H6CkG8q3SUKM XasAsPzOiW/hlwfewZuYuAyNhIvCp5gSw7J3yf7RMSmvkGxEi7UjOsqbv0Iud4JTghPP 0Gig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=cwzPqJzF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d14si4896979eje.95.2020.07.10.18.29.38; Fri, 10 Jul 2020 18:30:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=cwzPqJzF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727049AbgGKB3R (ORCPT + 99 others); Fri, 10 Jul 2020 21:29:17 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:37718 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726991AbgGKB3O (ORCPT ); Fri, 10 Jul 2020 21:29:14 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 06B1OTFc027668 for ; Fri, 10 Jul 2020 18:29:13 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=Zt9u2EpjWjLtKzNqOLuW2yd8dLixE48PVNDjLnQBOGM=; b=cwzPqJzFj3nKDLXek/ZNIQ/9oxUu1edCuWpV3UbI6SQ3J12UrHs9ZJAHSADpnl9oMBoh kgl/ynnFLB0FjLoGiA8OFYivfDc69+gPJtRY5CChtOM6JW6e6C9USV94A0WzaMeLolBf Q7Nwj9uq1bzrNcTsjx79Jhv6CULRu39q5zU= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 325k2cd84t-16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 10 Jul 2020 18:29:13 -0700 Received: from intmgw004.03.ash8.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Fri, 10 Jul 2020 18:29:10 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 56F4662E5296; Fri, 10 Jul 2020 18:27:00 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , , , , , , , Song Liu Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 5/5] selftests/bpf: add callchain_stackid Date: Fri, 10 Jul 2020 18:26:39 -0700 Message-ID: <20200711012639.3429622-6-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200711012639.3429622-1-songliubraving@fb.com> References: <20200711012639.3429622-1-songliubraving@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-10_14:2020-07-10,2020-07-10 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 suspectscore=0 priorityscore=1501 impostorscore=0 adultscore=0 mlxscore=0 bulkscore=0 phishscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxlogscore=999 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007110006 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This tests new helper function bpf_get_callchain_stackid(), which is the alternative to bpf_get_stackid() for perf_event with PEBS entry. Signed-off-by: Song Liu --- .../bpf/prog_tests/callchain_stackid.c | 61 +++++++++++++++++++ .../selftests/bpf/progs/callchain_stackid.c | 37 +++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/callchain_stac= kid.c create mode 100644 tools/testing/selftests/bpf/progs/callchain_stackid.c diff --git a/tools/testing/selftests/bpf/prog_tests/callchain_stackid.c b= /tools/testing/selftests/bpf/prog_tests/callchain_stackid.c new file mode 100644 index 0000000000000..ebe6251324a1a --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/callchain_stackid.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook +#include +#include "callchain_stackid.skel.h" + +void test_callchain_stackid(void) +{ + struct perf_event_attr attr =3D { + /* .type =3D PERF_TYPE_SOFTWARE, */ + .type =3D PERF_TYPE_HARDWARE, + .config =3D PERF_COUNT_HW_CPU_CYCLES, + .precise_ip =3D 2, + .sample_type =3D PERF_SAMPLE_IP | PERF_SAMPLE_BRANCH_STACK | + PERF_SAMPLE_CALLCHAIN, + .branch_sample_type =3D PERF_SAMPLE_BRANCH_USER | + PERF_SAMPLE_BRANCH_NO_FLAGS | + PERF_SAMPLE_BRANCH_NO_CYCLES | + PERF_SAMPLE_BRANCH_CALL_STACK, + .sample_period =3D 5000, + .size =3D sizeof(struct perf_event_attr), + }; + struct callchain_stackid *skel; + __u32 duration =3D 0; + int pmu_fd, err; + + skel =3D callchain_stackid__open(); + + if (CHECK(!skel, "skel_open", "skeleton open failed\n")) + return; + + /* override program type */ + bpf_program__set_perf_event(skel->progs.oncpu); + + err =3D callchain_stackid__load(skel); + if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err)) + goto cleanup; + + pmu_fd =3D syscall(__NR_perf_event_open, &attr, -1 /* pid */, + 0 /* cpu 0 */, -1 /* group id */, + 0 /* flags */); + if (pmu_fd < 0) { + printf("%s:SKIP:cpu doesn't support the event\n", __func__); + test__skip(); + goto cleanup; + } + + skel->links.oncpu =3D bpf_program__attach_perf_event(skel->progs.oncpu, + pmu_fd); + if (CHECK(IS_ERR(skel->links.oncpu), "attach_perf_event", + "err %ld\n", PTR_ERR(skel->links.oncpu))) { + close(pmu_fd); + goto cleanup; + } + usleep(500000); + + CHECK(skel->data->total_val =3D=3D 1, "get_callchain_stack", "failed\n"= ); + close(pmu_fd); + +cleanup: + callchain_stackid__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/callchain_stackid.c b/tool= s/testing/selftests/bpf/progs/callchain_stackid.c new file mode 100644 index 0000000000000..aab2c736a0a45 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/callchain_stackid.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook +#include "vmlinux.h" +#include + +#ifndef PERF_MAX_STACK_DEPTH +#define PERF_MAX_STACK_DEPTH 127 +#endif + +#ifndef BPF_F_USER_STACK +#define BPF_F_USER_STACK (1ULL << 8) +#endif + +typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH]; +struct { + __uint(type, BPF_MAP_TYPE_STACK_TRACE); + __uint(max_entries, 16384); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(stack_trace_t)); +} stackmap SEC(".maps"); + +long total_val =3D 1; + +SEC("perf_event") +int oncpu(struct bpf_perf_event_data *ctx) +{ + long val; + + val =3D bpf_get_callchain_stackid(ctx->callchain, &stackmap, 0); + + if (val > 0) + total_val +=3D val; + + return 0; +} + +char LICENSE[] SEC("license") =3D "GPL"; --=20 2.24.1