Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp205856ybh; Tue, 14 Jul 2020 23:10:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEZ+ChNI/TkV6X23sHieMVCmMCQxZ7nLiHxkvHa+p2MqvgRmESdroSXX+66UMN91MuU3tE X-Received: by 2002:a05:6402:559:: with SMTP id i25mr7828468edx.35.1594793406872; Tue, 14 Jul 2020 23:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594793406; cv=none; d=google.com; s=arc-20160816; b=SSqBWU7aqzlylLXBCx2aOSAHaaGNWexBvSZMjUSadLyi4tXCoPHD5f0gGpC5wmqPpP itThHA5hyDEkO9qADRDAtVG47IVaEfj2/nkaENEw7gQuVFF9WRxQyW5IfExn5KOGWEB8 /xU0u5iZO07kS72AeU7MVTuSQuvRf7vdbD6yVuBe2vSIF6yatVEFNnzcfMFW7f1QVSqg meInr9xOAXFEUUcwlgNBRl/QHqHor8CuiLiNAEP4efGSmuYdG2yUkssOVklRGOyUzqeQ OnOmy6CU9xtDb3BDzxB2kbAmSQgij/OUlfL4gxBmoPbdisU0B33A0o4fQwf7HNdF23e7 rn4w== 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=EQVhyN0Yhc5N1dItjWL5u9vys6FyokTu/QB730ry8+A=; b=o4GqdYDB+efSHrrAYgoclg3i7MfRzTHtQoBQjGpXLPeSANCIyL50WUvX2I+w4LojDE dhic886Mp0j4kJR+hPPmO5sooakdjBZzsAmKdtPo+1ujw3YwOWzwK1r7Cp0TMpNej9QD ctPMIm/RF68lf422ZqlvGInIjMxT5U/rL+NmMCgqe9u+NQ/nVjmU+Z2lo+rYLYg/d1V0 c4K8WTpotPM/Yi+DHTgUI1D0PMnHeyaVUEasTwhTtoTz0afQ53SQ8vu1NVU3cj4B2r8O KUFVJ3dFSyWmn2sL99ga/MowWw+t8e0kp9/JrFZIuOMZ9ruyo/ls1ev3OBjAtNi/TTG+ MDaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="Q2/5Mmkw"; 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 v14si684563eje.123.2020.07.14.23.09.41; Tue, 14 Jul 2020 23:10:06 -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="Q2/5Mmkw"; 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 S1728504AbgGOF30 (ORCPT + 99 others); Wed, 15 Jul 2020 01:29:26 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:55622 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728495AbgGOF3Z (ORCPT ); Wed, 15 Jul 2020 01:29:25 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 06F5TMOn000582 for ; Tue, 14 Jul 2020 22:29:24 -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=EQVhyN0Yhc5N1dItjWL5u9vys6FyokTu/QB730ry8+A=; b=Q2/5Mmkw95IEcR2dnpZl4CCfVn9tiybHZ8VWzGKbAFwVrphTPa8i9F0e/4n3kNr8a7Pm GwD/gGOup/dkY4b8GDjMlT1GifOH2RCbShN01ztmMM87zAggZHO+kpDQIoI/Bqcltr8d LmzJsBV9fhG7T+0c4dUFLLeu9WcyqlSRmy0= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 327b8j12pn-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 14 Jul 2020 22:29:24 -0700 Received: from intmgw003.08.frc2.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 14 Jul 2020 22:29:04 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 3121E62E52E7; Tue, 14 Jul 2020 22:26:10 -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 v2 bpf-next 2/2] selftests/bpf: add callchain_stackid Date: Tue, 14 Jul 2020 22:26:01 -0700 Message-ID: <20200715052601.2404533-3-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200715052601.2404533-1-songliubraving@fb.com> References: <20200715052601.2404533-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-15_02:2020-07-15,2020-07-15 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 suspectscore=0 mlxlogscore=999 phishscore=0 impostorscore=0 clxscore=1015 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 lowpriorityscore=0 priorityscore=1501 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007150046 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_stackid_pe and bpf_get_stack_pe. These two helpers have different implementation for perf_event with PEB entries. Signed-off-by: Song Liu --- .../bpf/prog_tests/perf_event_stackmap.c | 120 ++++++++++++++++++ .../selftests/bpf/progs/perf_event_stackmap.c | 64 ++++++++++ 2 files changed, 184 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_event_sta= ckmap.c create mode 100644 tools/testing/selftests/bpf/progs/perf_event_stackmap= .c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_event_stackmap.c= b/tools/testing/selftests/bpf/prog_tests/perf_event_stackmap.c new file mode 100644 index 0000000000000..6dcc67572afc7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_event_stackmap.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook +#define _GNU_SOURCE +#include +#include +#include +#include "perf_event_stackmap.skel.h" + +#ifndef noinline +#define noinline __attribute__((noinline)) +#endif + +noinline int func_1(void) +{ + static int val =3D 1; + + val +=3D 1; + + usleep(100); + return val; +} + +noinline int func_2(void) +{ + return func_1(); +} + +noinline int func_3(void) +{ + return func_2(); +} + +noinline int func_4(void) +{ + return func_3(); +} + +noinline int func_5(void) +{ + return func_4(); +} + +noinline int func_6(void) +{ + int i, val =3D 1; + + for (i =3D 0; i < 100; i++) + val +=3D func_5(); + + return val; +} + +void test_perf_event_stackmap(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 perf_event_stackmap *skel; + __u32 duration =3D 0; + cpu_set_t cpu_set; + int pmu_fd, err; + + skel =3D perf_event_stackmap__open(); + + if (CHECK(!skel, "skel_open", "skeleton open failed\n")) + return; + + /* override program type */ + bpf_program__set_perf_event(skel->progs.oncpu); + + err =3D perf_event_stackmap__load(skel); + if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err)) + goto cleanup; + + CPU_ZERO(&cpu_set); + CPU_SET(0, &cpu_set); + err =3D pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_se= t); + if (CHECK(err, "set_affinity", "err %d, errno %d\n", err, errno)) + 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; + } + + /* create kernel and user stack traces for testing */ + func_6(); + + CHECK(skel->data->stackid_kernel !=3D 2, "get_stackid_kernel", "failed\= n"); + CHECK(skel->data->stackid_user !=3D 2, "get_stackid_user", "failed\n"); + CHECK(skel->data->stack_kernel !=3D 2, "get_stack_kernel", "failed\n"); + CHECK(skel->data->stack_user !=3D 2, "get_stack_user", "failed\n"); + close(pmu_fd); + +cleanup: + perf_event_stackmap__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/perf_event_stackmap.c b/to= ols/testing/selftests/bpf/progs/perf_event_stackmap.c new file mode 100644 index 0000000000000..1b0457efeedec --- /dev/null +++ b/tools/testing/selftests/bpf/progs/perf_event_stackmap.c @@ -0,0 +1,64 @@ +// 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"); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, stack_trace_t); +} stackdata_map SEC(".maps"); + +long stackid_kernel =3D 1; +long stackid_user =3D 1; +long stack_kernel =3D 1; +long stack_user =3D 1; + +SEC("perf_event") +int oncpu(void *ctx) +{ + int max_len =3D PERF_MAX_STACK_DEPTH * sizeof(__u64); + stack_trace_t *trace; + __u32 key =3D 0; + long val; + + val =3D bpf_get_stackid(ctx, &stackmap, 0); + if (val > 0) + stackid_kernel =3D 2; + val =3D bpf_get_stackid(ctx, &stackmap, BPF_F_USER_STACK); + if (val > 0) + stackid_user =3D 2; + + trace =3D bpf_map_lookup_elem(&stackdata_map, &key); + if (!trace) + return 0; + + val =3D bpf_get_stack(ctx, trace, max_len, 0); + if (val > 0) + stack_kernel =3D 2; + + val =3D bpf_get_stack(ctx, trace, max_len, BPF_F_USER_STACK); + if (val > 0) + stack_user =3D 2; + + return 0; +} + +char LICENSE[] SEC("license") =3D "GPL"; --=20 2.24.1