From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A633C1C242A; Fri, 27 Sep 2024 18:53:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463231; cv=none; b=f9yMT1RXDP9UC2rUr0YGQg7WJvWe45rsMoVX6KjVc3RU7oKvYFANWRc66gouzuQ+YEDr6pWlriiikhQHPkIQOHG8wu7ignHUrY6gM/lIc5HLXUCCvfF/0i41OKojeKe1HJKIm4gyIXOWl+143N+wbbJwmitrxGK8DOIJdZBgjKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463231; c=relaxed/simple; bh=nIUiDxgqflD2SH7nvjL7lUKBxvllcX5JqrcgK+ywIMA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LTk1ltSmIsBS3qv5JVN2ilzKzlA3ERnJYrI1+djhkDzweJPzVBBhMrGJ0TVixSbReixIIAiQkk/eqtCbg1JPC3+4+DL8SrcFaIanRsqjFk1VWYeBx5I9RFxyVyAAqHsvhVDs0tIbA3Zk1ubK+kK/KEjNCIllKGgHuG/rs4J0u6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mGWncTcC; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mGWncTcC" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71b0d1c74bbso1846284b3a.0; Fri, 27 Sep 2024 11:53:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463229; x=1728068029; darn=vger.kernel.org; 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=UKsZoLSE8Uo83LAMVBiqpWixpej/X0Qxsd++nXnThvw=; b=mGWncTcC9P1Gjaq3UG4BRn17fTtTyQ3iGTQ0Pc8mnEm0ZCcTxvjLvjzxk26g6vvOgC Ab2bwagFStduZv3WuN8wK6RvGdjPPTEUyXtvWugwFTchBJ6lAGOn2iarsVfxtwjweXxG GJgmA2lB13EyoZOLkGXn+q3WGLwu5Vd9LAYQ9TPMm9DWn2VsgcFbT8J4i4xZJkDFE/2Q edTfz64I3hImtW+JEvcua/gPTfDYZjn+pf1+7AkZKia1Y8aPoHfcO8XUzHY3rndVI0kg phrlSJSxR2VEkXgL9SY0aeFNPwusTG1ZhF6Zhwm0zs1SEgWPmydkSx/XWyq7+KKEdktw OXGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463229; x=1728068029; 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=UKsZoLSE8Uo83LAMVBiqpWixpej/X0Qxsd++nXnThvw=; b=Uu1vxFqKsgeHBisj5KDJixSrkYpx1kVV38xnPH0rZD+IxIFWj/Y9lRkouWl/oW439L p0+bGXngSJlw9D8w3hO48z7m66A8Ja8co9bTuS9NkPzB1sMzphGkEhH8vZxkTOl/Bvb2 6GqgiUekdz1Y1b1eth97+TQFAMNsIEU7lvLR9AyDqWLDz9R86H//kFrgs+GbUzxDqX/0 34WeM9JREEulbNM0nZ2EzdSruWnwBUjlI/EVm0vOR61mWLQE05p+lFYnplLDEYU9zw6P Sja0IwrSz4Kte03hHMly5AJ11NoXKl5+fE2L42I35XVgw8/d56+2Z1RMPqbSXsFegtf0 rywg== X-Forwarded-Encrypted: i=1; AJvYcCWuJQnE0pU27wn0/WpDmfPkCsHdTk+Z4KX1gw4RpLEzWL5b8A+QITNsj+KUdwUzQrutta+RFqJCzC1d639W6JGXCQ==@vger.kernel.org, AJvYcCX0BGSUAFOH/hBpe1dJr9tqq4dWookmU0CKlY5wDaY6BW8eb0uJ8lsKzu7mIeaj4F6CX4RligTl4F0/EYQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyNykIER3CESpHD3kxd6pmbHVDC2cN2+jBwbboh59esBaty/HT9 RlOYiTpMC+FkKAR6SqnXM6OrDBjTItWoLFZAoNyIU2zN6bg21EtO X-Google-Smtp-Source: AGHT+IEUGSpbq2772BukGCLqUiWTJ8oS1H8C3nAZ4vn3TPx4CPFpwquy5QxafUgBsPA536MzXYW7VA== X-Received: by 2002:a05:6a00:2e87:b0:717:87a1:786 with SMTP id d2e1a72fcca58-71b25f3aed3mr5994150b3a.9.1727463228815; Fri, 27 Sep 2024 11:53:48 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.53.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:53:48 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 1/8] perf evsel: Set off-cpu BPF output to system-wide Date: Fri, 27 Sep 2024 11:53:33 -0700 Message-ID: <20240927185340.658143-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pid =3D -1 for off-cpu's bpf-output event. This makes 'perf record -p --off-cpu', and 'perf record --off-cpu ' work. Otherwise bpf-output cannot be collected. The reason (conjecture): say if we open perf_event on pid =3D 11451, then in BPF, we call bpf_perf_event_output() when a direct sample is ready to be dumped. But currently the perf_event of pid 11451 is not __fully__ sched_in yet, so in kernel/trace/bpf_trace.c's __bpf_perf_event_output(), there will be event->oncpu !=3D cpu, thus return -EOPNOTSUPP, direct off-cpu sample output failed. if (unlikely(event->oncpu !=3D cpu)) return -EOPNOTSUPP; So I'm making it pid =3D -1, everybody can do bpf_perf_event_output() P.S. In perf trace this is not necessary, because it uses syscall tracepoints, instead of sched_switch. Signed-off-by: Howard Chu --- tools/perf/util/evsel.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index edfb376f0611..500ca62669cb 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2368,6 +2368,9 @@ static int evsel__open_cpu(struct evsel *evsel, struc= t perf_cpu_map *cpus, =20 test_attr__ready(); =20 + if (evsel__is_offcpu_event(evsel)) + pid =3D -1; + /* Debug message used by test scripts */ pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags = %#lx", pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags); --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E28F11C2DC4; Fri, 27 Sep 2024 18:53:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463235; cv=none; b=FZYg1BhDkikgIo8p8nAWYWZa6cV8r0Nh4mY82rslkzepil79ZVzj//VkI8emIyOg1LgwmNHXqbFx6NIf6ZMB6LzgJtdMvLFms4VZw+fU4mtviCQTJYgiALWs/UXU6TtWiuRsr/MO877QdwWOsBr7JwND9TdC9Ifr87MIj9kB+IA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463235; c=relaxed/simple; bh=eK1FtnbY9Tri45jB5Cvw0onFqkVbxt9kUVvxXDph0Qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fS0GWSiYPmvLKVWKfyuH+pxNHOZiRA1iTBEFzGW9qo9fqdN3sx9GNKEop0p5e9uBhnGzS7otbf2tBIqu3AJkNcukNyV3enL9+r/kCc0v8oPdnjw0zejTbXaiCS2pnUKMfG8Xk9N2fVarbxeWOrMSC27OEXQiBBc8PvMz886/37k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jqpwNfJA; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jqpwNfJA" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71b20ffd809so1631464b3a.0; Fri, 27 Sep 2024 11:53:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463233; x=1728068033; darn=vger.kernel.org; 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=YdYP7hS3yJ4h9q1j2wrX5tkUAb8rgBuRj2nZ/ENMYog=; b=jqpwNfJAS5MYxILO+zLEfXjappomSRezDQS2k2LNxx+eNNN9EHXvf/+p8QTJv67Sjd BA/KW1bwd15zg3OMKqV760nCIKfAaHBW66tRDUZRWr2ff8OmLp+kzRHQ3KqtFq2KaKfC 9pRD4xqg2Nk1rSmQNwTql/z4XgdcVwLUSHOvoGRYGRSecVNH45RCtclsAsT4HBMq4cNn ULYG5p6AeeWwP8luynD2gWypmujzT4d1uk2O043+bOAIv6QQYuEbKPqhysMJfxDPWAlx JBZMLk6PvPCAc3Hs/mqndRwHwaQlPnFUsRokoHtLojbJA8OzXVjQeB1FbCRizHlqWbC7 msvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463233; x=1728068033; 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=YdYP7hS3yJ4h9q1j2wrX5tkUAb8rgBuRj2nZ/ENMYog=; b=XlUWy2L2qYz/j4LcdBnlhEnJaLCXRyIoTyauYzAb210/V0bEvjcbW6IwcWJVYKwQwV jLmQpOne5LUccBpbVptR7/VuxKKWdwFPowdSTFoeLfFtR188ecLr5os+W5rIKtge6zJG /agNcbysKwpIezqF+fIfAfsUId8Y2y/daCnI8RRhvJOw7dSz0LikGnCYP9KtDzCgEiub nPAFseaPUfitkEjlyhqYvj+S1MViY7hCf38hgZB80tUtqFnZdZCRjvEX/x35bMglziw8 q3qlp2xR8BfqKJXOPnAB2RfaNa6kHxBV4NueLNdSfzf2wo46yU6pXHFTN6RXKr3IOAHv Xepw== X-Forwarded-Encrypted: i=1; AJvYcCVJRFkySc8LPX94SqfuaKSCAdA1mjv8KDkzrg7ba1gUvaIAOYcKZWVFIagSpdhlTcaNeaDVTsq8gEOhwcQ=@vger.kernel.org, AJvYcCWl3nNL5lFc2lUGxa65sTBm/hv5t67g7J/EKhsCJEgRT/5Rae91DG5iqsMd6+mjpTnYGBO8M7334CMKgO/KtcJkDw==@vger.kernel.org X-Gm-Message-State: AOJu0Yyyk4ib1ba3ZDjz6KS1VH6At3C84Dy24Ku5ZFq3JodIU8sUiyhL 17G/34jx9l6Hy4Y+Rq1YpRn1flvjBHK3zvGhRxdLV+A1DG1W/Nqb X-Google-Smtp-Source: AGHT+IHsh/KFEk/89+UhAfma9FVWoFVjvgKWPOSp2V4a36Rx5+9hWlUOVDZzMmBtoMbqKgLiUkdBBw== X-Received: by 2002:a05:6a00:2e87:b0:717:9154:b5b6 with SMTP id d2e1a72fcca58-71b25f3dae9mr6411075b3a.7.1727463232943; Fri, 27 Sep 2024 11:53:52 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.53.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:53:52 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 2/8] perf record --off-cpu: Add --off-cpu-thresh Date: Fri, 27 Sep 2024 11:53:34 -0700 Message-ID: <20240927185340.658143-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the --off-cpu-thresh argument to specify the off-cpu time threshold. If the off-cpu time exceeds this threshold, dump the off-cpu data directly. Suggested-by: Ian Rogers Signed-off-by: Howard Chu --- tools/perf/builtin-record.c | 26 ++++++++++++++++++++++++++ tools/perf/util/bpf_off_cpu.c | 2 ++ tools/perf/util/bpf_skel/off_cpu.bpf.c | 2 ++ tools/perf/util/off_cpu.h | 2 ++ tools/perf/util/record.h | 1 + 5 files changed, 33 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index adbaf80b398c..bd53fb3c98ec 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3149,6 +3149,28 @@ static int record__parse_mmap_pages(const struct opt= ion *opt, return ret; } =20 +static int record__parse_off_cpu_thresh(const struct option *opt, + const char *str, + int unset __maybe_unused) +{ + struct record_opts *opts =3D opt->value; + char *endptr; + u64 off_cpu_thresh; + + if (!str) + return -EINVAL; + + off_cpu_thresh =3D strtoul(str, &endptr, 10); + + /* threshold isn't string "0", yet strtoull() returns 0, parsing failed */ + if (*endptr || (off_cpu_thresh =3D=3D 0 && strcmp(str, "0"))) + return -EINVAL; + else + opts->off_cpu_thresh =3D off_cpu_thresh; + + return 0; +} + void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __ma= ybe_unused) { } @@ -3342,6 +3364,7 @@ static struct record record =3D { .ctl_fd =3D -1, .ctl_fd_ack =3D -1, .synth =3D PERF_SYNTH_ALL, + .off_cpu_thresh =3D OFF_CPU_THRESH_DEFAULT, }, }; =20 @@ -3564,6 +3587,9 @@ static struct option __record_options[] =3D { OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), OPT_STRING(0, "setup-filter", &record.filter_action, "pin|unpin", "BPF filter action"), + OPT_CALLBACK(0, "off-cpu-thresh", &record.opts, "us", + "Dump off-cpu samples if off-cpu time reaches this threshold. The u= nit is microsecond (default: 500000)", + record__parse_off_cpu_thresh), OPT_END() }; =20 diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index a590a8ac1f9d..eaef643f50e3 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -272,6 +272,8 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } } =20 + skel->bss->offcpu_thresh =3D opts->off_cpu_thresh * 1000; + err =3D off_cpu_bpf__attach(skel); if (err) { pr_err("Failed to attach off-cpu BPF skeleton\n"); diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index c152116df72f..5ea320aa9a53 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -97,6 +97,8 @@ const volatile bool uses_cgroup_v1 =3D false; =20 int perf_subsys_id =3D -1; =20 +__u64 sample_id, sample_type, offcpu_thresh; + /* * Old kernel used to call it task_struct->state and now it's '__state'. * Use BPF CO-RE "ignored suffix rule" to deal with it like below: diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 2dd67c60f211..357231cb1c38 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -10,6 +10,8 @@ struct record_opts; =20 #define OFFCPU_EVENT "offcpu-time" =20 +#define OFF_CPU_THRESH_DEFAULT 500000ull + #define OFFCPU_SAMPLE_TYPES (PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | \ PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ PERF_SAMPLE_ID | PERF_SAMPLE_CPU | \ diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index a6566134e09e..3c11416e6627 100644 --- a/tools/perf/util/record.h +++ b/tools/perf/util/record.h @@ -79,6 +79,7 @@ struct record_opts { int synth; int threads_spec; const char *threads_user_spec; + u64 off_cpu_thresh; }; =20 extern const char * const *record_usage; --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 285FD1C2439; Fri, 27 Sep 2024 18:53:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463240; cv=none; b=fUVX6ZDWLBIpFtcx0bS9IhEtQkbSbFl8FB2+BXoqNoDRAkL0CgVnHMteO7qxXqT9mjaNf8+rcsKYGd9z7cOr6op3Mdqs0gaG6WdHIw6q8VY/5gSGBxVCBxv5SVll4HOi0bFlPI+fqI+dOvn7qtDMY6WMAdJyNUSUUa4p3FMvpr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463240; c=relaxed/simple; bh=99KfpGlXB8W4BAGbDG/1/yCro9ZdfzMDY98xd7oKYaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vAmhGYNBVOq0W3UEww1FehKAnleGONxLE36xPVG4dIwu6JlXcLXNxs6pHb9AX6KpKPe8Ncf2oZwZiRYYYpdf3sNsyb31ItmB0wq8TECuYt/UOJMt8F0n2a2/AXSmCa0pcnj5boulwxc80yxUluaqcEn3lhJ/cxxBHhGNdkDQINk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YFzLxFsj; arc=none smtp.client-ip=209.85.215.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YFzLxFsj" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7db174f9050so651779a12.3; Fri, 27 Sep 2024 11:53:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463237; x=1728068037; darn=vger.kernel.org; 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=E3HDeCFK/m+LDhVXu5v4d5xbrEo+hRCk0bljD8Hp21Q=; b=YFzLxFsjreW2+e8icMaEj0rQq07YMJuMQEr/VWPOmn92KViyUmZJdpRi73Ro/eBveN aWaeputnBLr1kaigfGBQd78zk0Y24q81HIbeDAcIfpHvJQqGjXI70JcnkxLajBCQKvaJ 45pGwOCxJK4azcV4NO4316CCqggGOMoeaLJG6TTTcWCgeIhhRZUpIRwyqW8dj1RihGo7 jrTIweT/IEUs3Z9qifznKwKoogeQgXh/Mty6pOJ/8NjCQIJIyhAK3ScgWmkc8Pq7ShuD v5BTcH+dj1eH5c4yTMosZq6mPFuXAf0qxOj6MXO5i0gM4B7R/pV+OQC8jxrb3puVycvV 6eEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463237; x=1728068037; 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=E3HDeCFK/m+LDhVXu5v4d5xbrEo+hRCk0bljD8Hp21Q=; b=KSbnc6h17z1MuCK/HkV95wmSYesf+hbZmqkGS/s6e02lwtP6XKgoAihxAlT5DPAAI3 WgkPOU5+/gtaVh6rODhGM/NdEJqTaMdMQfmIvw6wxkilWipUY1Q6L1KgAekJsvluKJnK WsWcmhXNpfTWGWYs1UNGUmwZmWXZKxwV3jc/NA8jt384/xs3WCcopgVtFLCWtFZT4MGH aLDzz280PslTzQiUVk5SgB9IHIirD1QYGaTStYpMMjYMDXpPw9qMB7TMzCWFmNqDzYoU gQoCPoVZfnjusks2yDhZrKYmmvOWLcqywsElimslm8+PWakz32VNsfl0LOOX6Btj/Cit 8OAQ== X-Forwarded-Encrypted: i=1; AJvYcCVMacIe5JKTWGTK3bu/V5rc4Zhv3zjut0D2diQ/0FMwAdSJKTrJsdrnBm2ikX1x/S9YgkqHT3TsHfEZAdmv7Av5EA==@vger.kernel.org, AJvYcCWMCZQDNfek/DUPuJFUbjtTH76PjW1f1xUyGMSdeZQc+vKRuiYpTHo5FRIwC4tFL0FFDjkkxZ8vqWPVl2w=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5mHIw9sr8vzMawc+8OQoxtXyyGwgfvIj09FIMlyBtAbihnSif 0gztyLqoG4tqHEHDmNQsE39JxLov5MkscdWzoMsBHPf6AYeZBWf1 X-Google-Smtp-Source: AGHT+IGDS9iH7oiijkulaUfQ8MGLULEaVhiNL5HZBT3mkntZZUJyOD+zowobOZSOhbWfd4FGlgnVQA== X-Received: by 2002:a05:6a20:ce4d:b0:1d0:56b1:1c59 with SMTP id adf61e73a8af0-1d4fa7ab7c8mr5675187637.32.1727463237294; Fri, 27 Sep 2024 11:53:57 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.53.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:53:56 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 3/8] perf record --off-cpu: Parse offcpu-time event Date: Fri, 27 Sep 2024 11:53:35 -0700 Message-ID: <20240927185340.658143-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Parse offcpu-time event using parse_event, in off_cpu_start(), write evlist fds got from evlist__open() to perf_event_array BPF map. Signed-off-by: Howard Chu --- tools/perf/util/bpf_off_cpu.c | 56 ++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index eaef643f50e3..f7233a09ec77 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -13,6 +13,7 @@ #include "util/cgroup.h" #include "util/strlist.h" #include +#include =20 #include "bpf_skel/off_cpu.skel.h" =20 @@ -36,41 +37,27 @@ union off_cpu_data { u64 array[1024 / sizeof(u64)]; }; =20 +u64 off_cpu_raw_data[1024 / sizeof(u64)]; + static int off_cpu_config(struct evlist *evlist) { - struct evsel *evsel; - struct perf_event_attr attr =3D { - .type =3D PERF_TYPE_SOFTWARE, - .config =3D PERF_COUNT_SW_BPF_OUTPUT, - .size =3D sizeof(attr), /* to capture ABI version */ - }; - char *evname =3D strdup(OFFCPU_EVENT); - - if (evname =3D=3D NULL) - return -ENOMEM; + char off_cpu_event[64]; =20 - evsel =3D evsel__new(&attr); - if (!evsel) { - free(evname); - return -ENOMEM; + scnprintf(off_cpu_event, sizeof(off_cpu_event), "bpf-output/no-inherit=3D= 1,name=3D%s/", OFFCPU_EVENT); + if (parse_event(evlist, off_cpu_event)) { + pr_err("Failed to open off-cpu event\n"); + return -1; } =20 - evsel->core.attr.freq =3D 1; - evsel->core.attr.sample_period =3D 1; - /* off-cpu analysis depends on stack trace */ - evsel->core.attr.sample_type =3D PERF_SAMPLE_CALLCHAIN; - - evlist__add(evlist, evsel); - - free(evsel->name); - evsel->name =3D evname; - return 0; } =20 static void off_cpu_start(void *arg) { struct evlist *evlist =3D arg; + struct evsel *evsel; + struct perf_cpu pcpu; + int i, err; =20 /* update task filter for the given workload */ if (skel->rodata->has_task && skel->rodata->uses_tgid && @@ -84,6 +71,27 @@ static void off_cpu_start(void *arg) bpf_map_update_elem(fd, &pid, &val, BPF_ANY); } =20 + /* sample id and fds in BPF's perf_event_array can only be set after reco= rd__open() */ + evsel =3D evlist__find_evsel_by_str(evlist, OFFCPU_EVENT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT); + return; + } + + if (evsel->core.id) + skel->bss->sample_id =3D evsel->core.id[0]; + + perf_cpu_map__for_each_cpu(pcpu, i, evsel->core.cpus) { + err =3D bpf_map__update_elem(skel->maps.offcpu_output, + &pcpu.cpu, sizeof(__u32), + xyarray__entry(evsel->core.fd, pcpu.cpu, 0), + sizeof(__u32), BPF_ANY); + if (err) { + pr_err("Failed to update perf event map for direct off-cpu dumping\n"); + return; + } + } + skel->bss->enabled =3D 1; } =20 --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD6331C32E4; Fri, 27 Sep 2024 18:54:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463243; cv=none; b=IcCe2L4jWoE1gnggop9rySw7R47MUUZWD9Qeurq/yacUHvk0KEuC2XhCk1LiWLJ2CyfElFbgJdWvRzGOaA7n3wwT9n1cZnIAWEGu3yjGE8l/ggJ4ijEgPVyOg0f56W7NVKCYuUUoIYrm0WoVRAiVymq9gQjcvnwLiKmV8x2Yomw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463243; c=relaxed/simple; bh=ZP1MfqrrYWrJ0ctH6HxOMr4k4OgS0xIR/BE5dS8Y3CM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IBcX4nMITRnJXQiJhhp8dtHHqdSHE8IcnmjfGzKQzM/oGbpgxqVggGk7srBcc942pZUXE9TmKpq5Nja82XxNS7Y/R4WV6NDtvMK5oM2UOJvITa6vXFgW7fNF9QM34YfxpyifC+Epljah5f2gnrdrzo0SX1glGVg5Vc+2u40eZZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RLiusPYS; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RLiusPYS" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-719ba0654f9so2161941b3a.3; Fri, 27 Sep 2024 11:54:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463241; x=1728068041; darn=vger.kernel.org; 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=34MOf3DFjEdXzBK7xHuDyap27i2H+2ZryV/BZ+beF1M=; b=RLiusPYS6jtATAb5L2PsGIvNzVWeq1rS4GOb6RLu0fvCvm6RfHxPC5bTIum9Hao5t8 5aO3vNRBw0t55tbCZfDk3iKAH9zsQ2Rj578V1rPYnYvF3trw0THaOL6fGNPpeRXqZGM3 ZOu+0eDUFZOBZF+qpkidaquYzMJFx97WTV0xOW1C+CUTBHK6BG0VXxDlzaAa13qR5Ah1 5aCwCIzwKhqSQ4VFXItQsKOM7gxUL9Z18HtDXxx2Gv29+feaqBpwY8IF0JeJkKjagP1Z zOmRTgHKSILEAKZqgCY4gmkerw/3u1/mdW56ww1XMErZB4RXD1YrrSxm1z8jBN+hMwM+ hWtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463241; x=1728068041; 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=34MOf3DFjEdXzBK7xHuDyap27i2H+2ZryV/BZ+beF1M=; b=sZtXaW93o9JADkXpEvM0XxL9U09s2fe7LEWnQx/rMjF3ZgetThEzVICE5zaX0IxeSH z5IJj3zi9NCSxlETABVt1mp8RnctVtMKKjeZrSrCO1kP+3CEm9b7jHhLWTnkhbAm0c+b WorgzXGkYT0f/TvdvfCe7PKgVrzinyOgdMNiPSAj041xKNQgdWELE5xoqJrxV9yPhuPn H4NKvu+toLODe1gV5vSj+cPvb1gzB7jFWVijf1MHMn/ud5jSTUvFKSV466/3xDuRgkmu /SE8kMyRp3IQeXs6gEttAX+iDL/a3SAK0JCL/O2aTMdea1BGnIphEkYev7ZzAODrK64P QXhg== X-Forwarded-Encrypted: i=1; AJvYcCUhkKW9GchqnRZItGTtRVPU4dMVgVC8Ir9VMZtvNZVxRVGkv/J26OkdqA1tUyXzmxPmaWZRcu6pqk/C6KCT5kJqjA==@vger.kernel.org, AJvYcCVuFDy53EBPo17NDw28ZM+uFAtIUU/3JIETgy+5y5YUtf5C9BBvqUIyzw800AVfHGgAulkLf88ekvsP4cE=@vger.kernel.org X-Gm-Message-State: AOJu0YzOfVIQwe3uccoxxhdlNhOCh9vyLNx+7xtab6pCg/nbdsdHoSCs 1sezvV4xfVmNydtgKDGp7/zTXUvQG6C5w68HVRlb5KQgF9wrUx66 X-Google-Smtp-Source: AGHT+IEXJh+GR17BHiYGJ/Cav7SROPr6ZU+ZrdpowUOqEbVJL6bFk1N7eNkZzpUdQZeDKtXEGpnrXg== X-Received: by 2002:a05:6a00:4fcb:b0:70d:2725:ebe4 with SMTP id d2e1a72fcca58-71b25f85147mr6724688b3a.13.1727463240815; Fri, 27 Sep 2024 11:54:00 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:54:00 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 4/8] perf record off-cpu: Dump direct off-cpu samples in BPF Date: Fri, 27 Sep 2024 11:53:36 -0700 Message-ID: <20240927185340.658143-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add perf_event_array map for dumping direct off-cpu samples, but keep the at-the-end approach. Tons of checking before access, to pass the BPF verifier. If off-cpu time (represented as delta) exceeds the off-cpu threshold, do output. Output PERF_SAMPLE_TID, PERF_SAMPLE_PERIOD, PERF_SAMPLE_CALLCHAIN, and PERF_SAMPLE_CGROUP in bpf_perf_event_output(). Ideally, we should only output PERF_SAMPLE_PERIOD (off-cpu time) and PERF_SAMPLE_CALLCHAIN (sched_in process's callchain). One only needs to set PERF_SAMPLE_TID and PERF_SAMPLE_CGROUP, and perf_event will do everything for us. But in reality, that's not the case. Setting PERF_SAMPLE_TID will mostly give us TID of 0. We might get the correct TID for offcpu-time event from time to time, but it is really rare. swapper 0 [000] offcpu-time: / :1321819 1321819 [002] offcpu-time: /user.slice/user-1000.slice/se= ssion-2.scope swapper 0 [001] offcpu-time: / swapper 0 [003] offcpu-time: / And setting PERF_SAMPLE_CGROUP doesn't work properly either. tmux: server 3701 [003] offcpu-time: / blueman-tray 1064 [001] offcpu-time: / bash 1350867 [001] offcpu-time: / bash 1350844 [000] offcpu-time: / We need to retrieve PERF_SAMPLE_TID, PERF_SAMPLE_PERIOD, PERF_SAMPLE_CALLCHAIN, and PERF_SAMPLE_CGROUP using BPF and output these four fields. Suggested-by: Namhyung Kim Signed-off-by: Howard Chu --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 121 +++++++++++++++++++++++++ tools/perf/util/off_cpu.h | 8 +- 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index 5ea320aa9a53..c42d0e2d91d8 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -18,6 +18,9 @@ #define MAX_STACKS 32 #define MAX_ENTRIES 102400 =20 +#define MAX_CPUS 4096 +#define MAX_OFFCPU_LEN 128 + struct tstamp_data { __u32 stack_id; __u32 state; @@ -32,6 +35,7 @@ struct offcpu_key { __u64 cgroup_id; }; =20 +/* for dumping at the end */ struct { __uint(type, BPF_MAP_TYPE_STACK_TRACE); __uint(key_size, sizeof(__u32)); @@ -39,6 +43,37 @@ struct { __uint(max_entries, MAX_ENTRIES); } stacks SEC(".maps"); =20 +struct offcpu_data { + u64 array[MAX_OFFCPU_LEN]; +}; + +struct stack_data { + u64 array[MAX_STACKS]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); + __uint(max_entries, MAX_CPUS); +} offcpu_output SEC(".maps"); + +/* temporary offcpu sample */ +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(struct offcpu_data)); + __uint(max_entries, 1); +} offcpu_payload SEC(".maps"); + +/* cached stack per task storage */ +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct stack_data); +} stack_cache SEC(".maps"); + struct { __uint(type, BPF_MAP_TYPE_TASK_STORAGE); __uint(map_flags, BPF_F_NO_PREALLOC); @@ -185,12 +220,75 @@ static inline int can_record(struct task_struct *t, i= nt state) return 1; } =20 +static inline bool check_bounds(int index) +{ + if (index < 0 || index >=3D MAX_OFFCPU_LEN) + return false; + + return true; +} + +static inline int copy_stack(struct stack_data *from, + struct offcpu_data *to, int n) +{ + int max_stacks =3D MAX_STACKS, len =3D 0; + + if (!from) + return len; + + for (int i =3D 0; i < max_stacks && from->array[i]; ++i) { + if (check_bounds(n + 2 + i)) { + to->array[n + 2 + i] =3D from->array[i]; + ++len; + } + } + return len; +} + +static int off_cpu_dump(void *ctx, struct offcpu_data *data, struct offcpu= _key *key, + struct stack_data *stack_p, __u64 delta, __u64 timestamp) +{ + int size, n =3D 0, ip_pos =3D -1, len =3D 0; + + if (sample_type & PERF_SAMPLE_TID && check_bounds(n)) + data->array[n++] =3D (u64)key->tgid << 32 | key->pid; + if (sample_type & PERF_SAMPLE_PERIOD && check_bounds(n)) + data->array[n++] =3D delta; + if (sample_type & PERF_SAMPLE_CALLCHAIN && check_bounds(n + 2)) { + /* data->array[n] is callchain->nr (updated later) */ + data->array[n + 1] =3D PERF_CONTEXT_USER; + data->array[n + 2] =3D 0; + + len =3D copy_stack(stack_p, data, n); + + /* update length of callchain */ + data->array[n] =3D len + 1; + + /* update sample ip with the first callchain entry */ + if (ip_pos >=3D 0) + data->array[ip_pos] =3D data->array[n + 2]; + + /* calculate sample callchain data->array length */ + n +=3D len + 2; + } + if (sample_type & PERF_SAMPLE_CGROUP && check_bounds(n)) + data->array[n++] =3D key->cgroup_id; + + size =3D n * sizeof(u64); + if (size >=3D 0 && size <=3D MAX_OFFCPU_LEN * sizeof(u64)) + bpf_perf_event_output(ctx, &offcpu_output, BPF_F_CURRENT_CPU, data, size= ); + + return 0; +} + static int off_cpu_stat(u64 *ctx, struct task_struct *prev, struct task_struct *next, int state) { __u64 ts; __u32 stack_id; struct tstamp_data *pelem; + struct stack_data *stack_p; + int zero =3D 0; =20 ts =3D bpf_ktime_get_ns(); =20 @@ -200,6 +298,21 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *= prev, stack_id =3D bpf_get_stackid(ctx, &stacks, BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK); =20 + /* + * if stacks are successfully collected, cache them to task_storage, they= are then + * dumped if the off-cpu time hits the threshold. + */ + if (stack_id > 0) { + stack_p =3D bpf_task_storage_get(&stack_cache, prev, NULL, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (stack_p) { + /* to pass the clang result unused warning */ + int __attribute__((unused)) len; + len =3D bpf_get_stack(ctx, stack_p->array, MAX_STACKS * sizeof(u64), + BPF_F_USER_STACK) / sizeof(u64); + } + } + pelem =3D bpf_task_storage_get(&tstamp, prev, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE); if (!pelem) @@ -229,6 +342,14 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *= prev, else bpf_map_update_elem(&off_cpu, &key, &delta, BPF_ANY); =20 + if (delta >=3D offcpu_thresh) { + struct offcpu_data *data =3D bpf_map_lookup_elem(&offcpu_payload, &zero= ); + + stack_p =3D bpf_task_storage_get(&stack_cache, next, NULL, 0); + if (data && stack_p) + off_cpu_dump(ctx, data, &key, stack_p, delta, pelem->timestamp); + } + /* prevent to reuse the timestamp later */ pelem->timestamp =3D 0; } diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 357231cb1c38..eaf7be92472d 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -15,9 +15,15 @@ struct record_opts; #define OFFCPU_SAMPLE_TYPES (PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | \ PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ PERF_SAMPLE_ID | PERF_SAMPLE_CPU | \ - PERF_SAMPLE_PERIOD | PERF_SAMPLE_CALLCHAIN | \ + PERF_SAMPLE_PERIOD | PERF_SAMPLE_RAW | \ PERF_SAMPLE_CGROUP) =20 +/* + * for embedded data to overwrite the original sample, duplicated sample t= ypes + * must be set in the original OFFCPU_SAMPLE_TYPES, except for callchain. + */ +#define OFFCPU_EMBEDDED_SAMPLE_TYPES (PERF_SAMPLE_TID | PERF_SAMPLE_PERIO= D | \ + PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CGROUP) =20 #ifdef HAVE_BPF_SKEL int off_cpu_prepare(struct evlist *evlist, struct target *target, --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 202851C32FC; Fri, 27 Sep 2024 18:54:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463245; cv=none; b=kqFE6nwrejzjNv6Pgf769V1kFtVJ9fcIWE2dtd/L+Rwq+Ga98Cp7Z4IYTly9+x+ISsZxKA/MvP56D0ousR7Q4PUGUxUumtufjNobFvco3Cgigoj0paHgdi69s+MPOopttf80UJ2yZGnTEVraJjDGsW0ZTUI9fwi16+2dMvf5I7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463245; c=relaxed/simple; bh=K1q2xLCuxeoesKIjk0h8yto/lXtLTYRLvejIlbxFPMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IfNioCashD7JClScFurSq6mPgYW/X24rqevrPYZ8qIOeo98OydjGr0JayabQEAkqWSDzoX5Ihh2fcq/p3EXYRIONV4nqgWO9/rYM53GEQ3908yVItHWdj26LC/yDR9V9XDEZC47vOq0OqygTW1lMkeZxqLeliyK5UvVsdJ/4trs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AYxCJQGt; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AYxCJQGt" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-718e285544fso1998862b3a.1; Fri, 27 Sep 2024 11:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463243; x=1728068043; darn=vger.kernel.org; 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=EiQs2GbMjfrDlXFijeVJx4a8DvVOhQx26OFbZSLijn0=; b=AYxCJQGt9y2ii698llgHAaYtZxRzccWPTLPkTMP7dEgYkYg6Y843gcceiOEfpCqQPG GYlPkMqwlbYdTjtoTB9vcmZnmr9mjF9XxVFutRXtiSqyXnVFSH8qdvWcTvL5+RkhQ9t0 hb5O/PWdt5i0cS1c268TBQuBjxg4O1neMrdHDe0QEYIOxmfWTM6vsHfrGV88A24fRTYh yQrtpUUt4Edj1xGarvzY5KddQffAN2Z4NITXgqxD7Cv/Q2RU1mORwlVeMV+hzkY9J3Tp F28y/qkwEcetTuIgPbn4Uo0d91uM63WCbu4/LPNFyvgCNRCQolApTq9is7SI+WS9bdVL snnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463243; x=1728068043; 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=EiQs2GbMjfrDlXFijeVJx4a8DvVOhQx26OFbZSLijn0=; b=bMRKHDg+Q4L9tduHnlOiFka8hoXMAzQWIBkOxjjgsejYM6POCzW6Ryp2C5hSYa+Vup dPWV6JCsZOM2KNiEJzt7sNvcF7BFt3YS7M+7FbcNdktD/9wpwtmUT4U0MIu6G7hu24Ih RmCHLmsic/lEpRcfyeROZyEYwhPcBOf9y84WjpBta0gdmA//bBxbGEuCYh8EXhUOHfWN BrmxQW/zIrgkmlRcwq0SQPd0XPQSJCW2fHYTe4WeLWeV1jedgHCWw1LzTWUTdcpl4HKM 6VmBnVY78nT43T5v40cCOTfKViRMjuS9ep2r7UREWU1Ebt4LNKIwi8selJEQGrz3Qu5H Zd1g== X-Forwarded-Encrypted: i=1; AJvYcCVxYMBcJhVo/UV8+iuDv9ww9sznHLa9Wj2v4OC99ysKe7YMqM20/0bsaZy++Pjm+VHf3dCue5xqYGIxLf0=@vger.kernel.org, AJvYcCWxRBCMXglfw9M2jmsIvBNw89ucPyjpCwJppiXhDaRJU62RZ+NuOKae5IbkMgnVlnjIKauxMg4ZZtiOIW1/Cl72+A==@vger.kernel.org X-Gm-Message-State: AOJu0YxpJA0i9B0G2cqSEru9AtiahAvaKGr5agBgJA/jgK/mx7Tu5UIE ZYWFeWnijPRuBZHcRHt6VGoSizg0wm15wFNtD0KOuD1RFXjV2YRk X-Google-Smtp-Source: AGHT+IEvvDtpdoN3quf3yciXgoF6FrQWmoxfPNG6O6sXtZkqBDFkL4uAIV+fiUxAKDO4LeKCyGUHXQ== X-Received: by 2002:a05:6a00:2396:b0:717:869c:2c60 with SMTP id d2e1a72fcca58-71b2607039fmr6471824b3a.26.1727463243394; Fri, 27 Sep 2024 11:54:03 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.54.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:54:03 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 5/8] perf record --off-cpu: Dump total off-cpu time at the end. Date: Fri, 27 Sep 2024 11:53:37 -0700 Message-ID: <20240927185340.658143-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" By setting a placeholder sample_type and then writing real data into raw_data, we mimic the direct sample method to write data at the end. Note that some data serve only as placeholders and will be overwritten by the data in raw_data. Additionally, since the IP will be updated in evsel__parse_sample(), there is no need to handle it in off_cpu_write(). Suggested-by: Namhyung Kim Signed-off-by: Howard Chu --- tools/perf/util/bpf_off_cpu.c | 116 +++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 44 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index f7233a09ec77..2a1cfd7e0b09 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -138,12 +138,19 @@ int off_cpu_prepare(struct evlist *evlist, struct tar= get *target, int ncpus =3D 1, ntasks =3D 1, ncgrps =3D 1; struct strlist *pid_slist =3D NULL; struct str_node *pos; + struct evsel *evsel; =20 if (off_cpu_config(evlist) < 0) { pr_err("Failed to config off-cpu BPF event\n"); return -1; } =20 + evsel =3D evlist__find_evsel_by_str(evlist, OFFCPU_EVENT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT); + return -1 ; + } + skel =3D off_cpu_bpf__open(); if (!skel) { pr_err("Failed to open off-cpu BPF skeleton\n"); @@ -259,7 +266,6 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } =20 if (evlist__first(evlist)->cgrp) { - struct evsel *evsel; u8 val =3D 1; =20 fd =3D bpf_map__fd(skel->maps.cgroup_filter); @@ -280,6 +286,7 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } } =20 + skel->bss->sample_type =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; skel->bss->offcpu_thresh =3D opts->off_cpu_thresh * 1000; =20 err =3D off_cpu_bpf__attach(skel); @@ -305,7 +312,8 @@ int off_cpu_write(struct perf_session *session) { int bytes =3D 0, size; int fd, stack; - u64 sample_type, val, sid =3D 0; + u32 raw_size; + u64 sample_type_off_cpu, sample_type_bpf_output, val, sid =3D 0, tstamp = =3D OFF_CPU_TIMESTAMP; struct evsel *evsel; struct perf_data_file *file =3D &session->data->file; struct off_cpu_key prev, key; @@ -315,7 +323,6 @@ int off_cpu_write(struct perf_session *session) .misc =3D PERF_RECORD_MISC_USER, }, }; - u64 tstamp =3D OFF_CPU_TIMESTAMP; =20 skel->bss->enabled =3D 0; =20 @@ -325,15 +332,10 @@ int off_cpu_write(struct perf_session *session) return 0; } =20 - sample_type =3D evsel->core.attr.sample_type; - - if (sample_type & ~OFFCPU_SAMPLE_TYPES) { - pr_err("not supported sample type: %llx\n", - (unsigned long long)sample_type); - return -1; - } + sample_type_off_cpu =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; + sample_type_bpf_output =3D evsel->core.attr.sample_type; =20 - if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) { + if (sample_type_bpf_output & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) { if (evsel->core.id) sid =3D evsel->core.id[0]; } @@ -344,49 +346,75 @@ int off_cpu_write(struct perf_session *session) =20 while (!bpf_map_get_next_key(fd, &prev, &key)) { int n =3D 1; /* start from perf_event_header */ - int ip_pos =3D -1; + int i =3D 0; /* raw data index */ =20 bpf_map_lookup_elem(fd, &key, &val); =20 - if (sample_type & PERF_SAMPLE_IDENTIFIER) + /* + * Zero-fill some of these fields first, they will be overwritten by the= dummy + * embedded data (in raw_data) below, when parsing the samples. And beca= use embedded + * data is in BPF output, perf script -F without bpf-output field will n= ot work + * properly. + */ + if (sample_type_bpf_output & PERF_SAMPLE_IDENTIFIER) data.array[n++] =3D sid; - if (sample_type & PERF_SAMPLE_IP) { - ip_pos =3D n; - data.array[n++] =3D 0; /* will be updated */ - } - if (sample_type & PERF_SAMPLE_TID) - data.array[n++] =3D (u64)key.pid << 32 | key.tgid; - if (sample_type & PERF_SAMPLE_TIME) - data.array[n++] =3D tstamp; - if (sample_type & PERF_SAMPLE_ID) - data.array[n++] =3D sid; - if (sample_type & PERF_SAMPLE_CPU) + if (sample_type_bpf_output & PERF_SAMPLE_IP) data.array[n++] =3D 0; - if (sample_type & PERF_SAMPLE_PERIOD) - data.array[n++] =3D val; - if (sample_type & PERF_SAMPLE_CALLCHAIN) { - int len =3D 0; - - /* data.array[n] is callchain->nr (updated later) */ - data.array[n + 1] =3D PERF_CONTEXT_USER; - data.array[n + 2] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_TID) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_TIME) + data.array[n++] =3D tstamp; /* we won't overwrite time */ + if (sample_type_bpf_output & PERF_SAMPLE_CPU) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_PERIOD) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_RAW) { + /* + * the format of raw data is as follows: + * + * [ size ][ data ] + * [ data ] + * [ data ] + * [ data ] + * [ data ][ empty] + * + */ + if (sample_type_off_cpu & PERF_SAMPLE_TID) + off_cpu_raw_data[i++] =3D (u64)key.pid << 32 | key.tgid; + if (sample_type_off_cpu & PERF_SAMPLE_PERIOD) + off_cpu_raw_data[i++] =3D val; + if (sample_type_off_cpu & PERF_SAMPLE_CALLCHAIN) { + int len =3D 0; + + /* off_cpu_raw_data[n] is callchain->nr (updated later) */ + off_cpu_raw_data[i + 1] =3D PERF_CONTEXT_USER; + off_cpu_raw_data[i + 2] =3D 0; + + bpf_map_lookup_elem(stack, &key.stack_id, &off_cpu_raw_data[i + 2]); + while (off_cpu_raw_data[i + 2 + len]) + len++; + + /* update length of callchain */ + off_cpu_raw_data[i] =3D len + 1; + + /* calculate sample callchain off_cpu_raw_data length */ + i +=3D len + 2; + } + if (sample_type_off_cpu & PERF_SAMPLE_CGROUP) + off_cpu_raw_data[i++] =3D key.cgroup_id; =20 - bpf_map_lookup_elem(stack, &key.stack_id, &data.array[n + 2]); - while (data.array[n + 2 + len]) - len++; + raw_size =3D i * sizeof(u64) + sizeof(u32); /* 4 empty bytes for alignm= ent */ =20 - /* update length of callchain */ - data.array[n] =3D len + 1; + /* raw_size */ + memcpy((void *)data.array + n * sizeof(u64), &raw_size, sizeof(raw_size= )); =20 - /* update sample ip with the first callchain entry */ - if (ip_pos >=3D 0) - data.array[ip_pos] =3D data.array[n + 2]; + /* raw_data */ + memcpy((void *)data.array + n * sizeof(u64) + sizeof(u32), off_cpu_raw_= data, i * sizeof(u64)); =20 - /* calculate sample callchain data array length */ - n +=3D len + 2; + n +=3D i + 1; } - if (sample_type & PERF_SAMPLE_CGROUP) - data.array[n++] =3D key.cgroup_id; + if (sample_type_bpf_output & PERF_SAMPLE_CGROUP) + data.array[n++] =3D 0; =20 size =3D n * sizeof(u64); data.hdr.size =3D size; --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B33271C3311; Fri, 27 Sep 2024 18:54:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463248; cv=none; b=ExS4W3hUcxnMoxtmn40cKVFvaMkNGTmwERcUufjz85dY08T5+qCpJ7dDzZb3AXn7C6yDZT/YMfUwqaLLTcChcHy32yMm/MjdcauKbjQULAVKI2PeDlO8scKy5AiX7V7iLodbWEROzJsVTk3nRl1pdX4OkJ7U6HLGUS4qDbQ3A7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463248; c=relaxed/simple; bh=BfKLXsdD+x//Y3ebfcycwHplNHYTY+Xgi8RSHLfMrc4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jPE0TUCGTH8x98NMb6+jThSTu+h5dT3OdpIb9N2GMMs+dsKiIj/CwxBu/0psgNT520RJPV2xeP8x1TkT3UVK5wHfWcB0b7ABhay1MvVyJdjhL+fx+Nm3uRFjfdeVVsPiXGpTkib7FQNGmIq4iuK8e0nwLb3JOZXmiXJp2FQT1zs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EWhz9m0f; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EWhz9m0f" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7179069d029so1845292b3a.2; Fri, 27 Sep 2024 11:54:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463246; x=1728068046; darn=vger.kernel.org; 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=6D5uxLLs8a+eADmi1psHFkftjw0ias/dpesC5XMo5zY=; b=EWhz9m0fWuCmgj/el1egnxH5RyZJFK6C0yDrQWSI1XddrvfC4RudzqS5h7LdXqx2r+ i0Locl3uZLHPZYfS5GIYjWSpGcDOEHjuaZog3MG+C3gxoK4do4MsENku6MYd9YyRaQP5 T5c9AeenHkCO334GE3brQZUzKGcXZcT6Hi9IFgMdTQrwV1WDxkZM5HsOvl6n4KPFWXL8 3dtt3pwBFpscvw4+P+cE8CV2axxQ0ORGzmsgZjCa81t/uFQvE3UvvwMcRHXSX1icw7Gs ZXY3co4hqpwcAP1AcmaCye2+mO+Yc+Sq+2lVyjdaav2h0oEvoLlu9nuBMePvMSDuZIEV sdTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463246; x=1728068046; 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=6D5uxLLs8a+eADmi1psHFkftjw0ias/dpesC5XMo5zY=; b=M/kdLmjLE6xRz40SUZ8pxwsPgWGLQ+QWCjZeHMUTKqSF4cpN+sr82NpR5ijGdp5CdV 6Xrs1KAm672rUnrH34F2+tDbSFhoI6rGhHZz3TlschhRyb/2qqH2XwBUqacLmPOlzFG0 /u+pdDvnzseZ9XX4GGWAXn1fXgXGrFpNeD0HLd+s93bUwv99C8cNtzfjUiBfT3ox8MKn XqbmNQs0oaFGPuYFBMvs5QDE3x8cDKYlKVoNuFXFuvpAK6Al5RECLfT1sGust3r1L7UC mxtShw1WP5fF469nFlmQiaNK2393tJCE4aOeq/h17ZzCHkPZTo5B0NhkPttXi1Q3wUwu 56Bg== X-Forwarded-Encrypted: i=1; AJvYcCWrj3Ux0yBgE/RTfFTux6m8wt6JcZeScw9D6yNCnuUDqeo3VR4OR/Pw47PkVOZtSoCgQ/LhCxca/x0f8PHSc4IZkg==@vger.kernel.org, AJvYcCX5qEnU6RPAG/XrK1dCOYU7xL1yHQP4SzZQCUQzuKZL9RW3FwF8bj4KRmtA3NuGAOGEjH6APPHIJpUHxys=@vger.kernel.org X-Gm-Message-State: AOJu0YzetOcWfTohKFhFGWzBlopFeZz3ArOmkSyJ3I+w5MI7rM7/Uynr SchDAUW0/Oq2kcpSH9HygIwH2NzUvbEN04ZOdufTYZ+eBau+aszK X-Google-Smtp-Source: AGHT+IHrbCVEEqGTZ8bZC2jc1QIYHcrzuf434sFSyUvR+qhiT47QXKTbyasJ/1lOz6W6LlrFacpQ/A== X-Received: by 2002:a05:6a00:1304:b0:714:1d96:e6bd with SMTP id d2e1a72fcca58-71b25f6a9d1mr6467553b3a.13.1727463245839; Fri, 27 Sep 2024 11:54:05 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.54.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:54:05 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 6/8] perf evsel: Delete unnecessary = 0 Date: Fri, 27 Sep 2024 11:53:38 -0700 Message-ID: <20240927185340.658143-7-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete unnecessary zero-initializations because they are already set to zero at the top of evsel__parse_sample(). If we don't remove them, it becomes troublesome to overwrite the sample using data from raw_data. Signed-off-by: Howard Chu --- tools/perf/util/evsel.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 500ca62669cb..32196e4f0637 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2965,7 +2965,6 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, array++; } =20 - data->intr_regs.abi =3D PERF_SAMPLE_REGS_ABI_NONE; if (type & PERF_SAMPLE_REGS_INTR) { OVERFLOW_CHECK_u64(array); data->intr_regs.abi =3D *array; @@ -2982,25 +2981,21 @@ int evsel__parse_sample(struct evsel *evsel, union = perf_event *event, } } =20 - data->phys_addr =3D 0; if (type & PERF_SAMPLE_PHYS_ADDR) { data->phys_addr =3D *array; array++; } =20 - data->cgroup =3D 0; if (type & PERF_SAMPLE_CGROUP) { data->cgroup =3D *array; array++; } =20 - data->data_page_size =3D 0; if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { data->data_page_size =3D *array; array++; } =20 - data->code_page_size =3D 0; if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { data->code_page_size =3D *array; array++; --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79C4C1C3F35; Fri, 27 Sep 2024 18:54:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463252; cv=none; b=DBMIdeIeTFOzSLu3jHIc9tNcI9BZdGAYCRoU4iAcqa/V3b75uFAv2VZnIDPBEHdBqS+vuA8Q+nzOoQDgXNYYsvuWLuk5mOD2sWEBDz1JHJJ30rLdDsvKagkbsxTdGCt3xXOD82a8PWgar9ga05jJVq5UIoJ/xMAMIqdNaeFaWVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463252; c=relaxed/simple; bh=adiSf7+Y53/FM4VGaERVGEcSkoSzxS48tNZljvrnWsc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d1aejU0goG2mJz1qusdId9z4jicXqpPh24pqPLVDcRMPTEFrqTtsyDfgLI6ZR3DFvPfQi9kt/7NBcSQVAJnwJJLUc2Rp0DZ6jrG4gQ2AoA528WamgOb1hJhZOEMK08+G96VEzqDJBbWLek4SHqag6g1pNVUNmhBGpc3NJJvcVvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PF+OITGf; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PF+OITGf" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-71970655611so2241041b3a.0; Fri, 27 Sep 2024 11:54:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463249; x=1728068049; darn=vger.kernel.org; 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=JAwY2+33GW304kJV0kB3faCFhgqS9trOgDYnXV+VZt0=; b=PF+OITGfE56IqRIzjS5Yjt+cewob77sIo3vJEzUSqaeUsxjs6dLFgpQxT03SFU43sB TH7uokbPgw4hxXj0MJg0mOGERQQ3jJ5xlLvHMzeADDrFV74iQQzX/jodUKq46tqydqqO DXmBm2ykCD0lEryeqK8Ixxt0ZAAcRixwL6pgIS083ll9W+TZEYQeVIW3nAmbXG+fw/mQ BWQdEpgJlSGCvri1nR+ZFyQPYqBzJJEgh+bL81XibC97Zjlvbh9Ya4Sp/K8gGLvLjUnP WH1ms6eNPWfQH4JDi3qkanlO7p7DGXWNRcGPVWr//gjOlPo2VYoBONgb+5Brpf7vNpoi kWpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463249; x=1728068049; 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=JAwY2+33GW304kJV0kB3faCFhgqS9trOgDYnXV+VZt0=; b=jd9hdX7xfc87Ixytrjeh8Et9nc2/0APN3o5sfU8lwUz1k24xKfwLIOOVk7Jr1w7h83 i5Comk91CrW9LhYILzc96YR5ZbhQLDZdIc24ZcqC/H0sjJL3LNnlBdskTx4w0CYSWFS7 6qleQ75vU4kmFLNFN2uHzh+aip+lXlYdkzPuLkyt+a+3l4i54ETMONpri6k3HqSq8gq1 3r5jGmk8bGIhO+z8j6CAtiESB0HOOw5Ba6IyAyOjIDiZPg0sMiPUJeIxmxFywVSixGEP +Tls+HpSL8JA0WVe/6qFVKURSNeWnKaMEcAQWYjwS4VG1VMJJ5p1wKlL0Xn3EKSH1wOI 6xzA== X-Forwarded-Encrypted: i=1; AJvYcCWSuDyy51D4eu+pXgCr5mBsMX9QwdzME3koMAZajzl+1BHxt4SuNim4l4FdcGJDprFQ4LfyGc8kNlFiwAw=@vger.kernel.org, AJvYcCX5P0GGHDiuyY3yrZGFZQNOYVE+TlhKoAOmE81Sgbm5a9MxIZicjLpXMXgPDVTXy3Z3I5CkG2kw611fUCEqJ4S7bg==@vger.kernel.org X-Gm-Message-State: AOJu0YwE3f8kXCASeoHfbcAHMiLqcUe4fFZwdZu1EZbsGzwWPqSc8Ug8 G2ZBQUpPuH5hf6YhZIDWTZg2dqtKvSymjibITrAkqQn7LTDr/Kni X-Google-Smtp-Source: AGHT+IFJH1vmFyC+n59Iz0RHu3CWG+QVgdx9LQyeuNYMI2SUrPSq5iNPtBrb/ra4qkTHcQhbXqI6hg== X-Received: by 2002:a05:6a00:1304:b0:70d:2e24:af75 with SMTP id d2e1a72fcca58-71b26072fc3mr7231715b3a.24.1727463248861; Fri, 27 Sep 2024 11:54:08 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.54.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:54:08 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 7/8] perf record --off-cpu: Parse BPF output embedded data Date: Fri, 27 Sep 2024 11:53:39 -0700 Message-ID: <20240927185340.658143-8-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move evsel__is_offcpu_event() to evsel.h Add a sample_type_embed member to the struct evsel, along with a couple of helper functions. In session.c, we parse BPF output embedded samples in a two-step process. Initial Parsing: Treat the sample as a regular BPF-output event. Secondary Parsing: Extract data from raw_data and parse it according to the sample_type_embed specification. Since the second step relies on the raw_data obtained in the first step, we must avoid zero-initializing the sample data after the first step. Suggested-by: Ian Rogers Suggested-by: Arnaldo Carvalho de Melo Signed-off-by: Howard Chu --- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/evsel.c | 39 +++++++++++++++++++++++-------------- tools/perf/util/evsel.h | 6 ++++++ tools/perf/util/session.c | 12 +++++++++++- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index a644787fa9e1..9719ffae45d5 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -662,7 +662,7 @@ static int perf_session__check_output_opt(struct perf_s= ession *session) =20 evlist__for_each_entry(session->evlist, evsel) { not_pipe =3D true; - if (evsel__has_callchain(evsel)) { + if (evsel__has_callchain(evsel) || evsel__is_offcpu_event(evsel)) { use_callchain =3D true; break; } @@ -2352,7 +2352,7 @@ static void process_event(struct perf_script *script, else if (PRINT_FIELD(BRSTACKOFF)) perf_sample__fprintf_brstackoff(sample, thread, attr, fp); =20 - if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT)) + if (evsel__is_bpf_output(evsel) && !evsel__is_offcpu_event(evsel) && PRIN= T_FIELD(BPF_OUTPUT)) perf_sample__fprintf_bpf_output(sample, fp); perf_sample__fprintf_insn(sample, evsel, attr, thread, machine, fp, al); =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 32196e4f0637..4199a1e409f7 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1105,11 +1105,6 @@ static void evsel__set_default_freq_period(struct re= cord_opts *opts, } } =20 -static bool evsel__is_offcpu_event(struct evsel *evsel) -{ - return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT); -} - /* * The enable_on_exec/disabled value strategy: * @@ -2677,6 +2672,7 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, u16 max_size =3D event->header.size; const void *endp =3D (void *)event + max_size; u64 sz; + bool ip_in_callchain =3D false; =20 /* * used for cross-endian analysis. See git commit 65014ab3 @@ -2684,14 +2680,25 @@ int evsel__parse_sample(struct evsel *evsel, union = perf_event *event, */ union u64_swap u; =20 - memset(data, 0, sizeof(*data)); - data->cpu =3D data->pid =3D data->tid =3D -1; - data->stream_id =3D data->id =3D data->time =3D -1ULL; - data->period =3D evsel->core.attr.sample_period; - data->cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; - data->misc =3D event->header.misc; - data->data_src =3D PERF_MEM_DATA_SRC_NONE; - data->vcpu =3D -1; + /* + * For sample data embedded in BPF output, don't clear the sample we read= in the first pass, + * and read the embedded data from raw_data in the second pass. + */ + if (evsel__is_offcpu_event(evsel) && data->raw_data) { + type =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; + array =3D data->raw_data; + ip_in_callchain =3D true; + } else { /* for normal samples, clear to zero before reading */ + array =3D event->sample.array; + memset(data, 0, sizeof(*data)); + data->cpu =3D data->pid =3D data->tid =3D -1; + data->stream_id =3D data->id =3D data->time =3D -1ULL; + data->period =3D evsel->core.attr.sample_period; + data->cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + data->misc =3D event->header.misc; + data->data_src =3D PERF_MEM_DATA_SRC_NONE; + data->vcpu =3D -1; + } =20 if (event->header.type !=3D PERF_RECORD_SAMPLE) { if (!evsel->core.attr.sample_id_all) @@ -2699,8 +2706,6 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, return perf_evsel__parse_id_sample(evsel, event, data); } =20 - array =3D event->sample.array; - if (perf_event__check_size(event, evsel->sample_size)) return -EFAULT; =20 @@ -2822,6 +2827,10 @@ int evsel__parse_sample(struct evsel *evsel, union p= erf_event *event, data->callchain =3D (struct ip_callchain *)array++; if (data->callchain->nr > max_callchain_nr) return -EFAULT; + + if (ip_in_callchain && data->callchain->nr > 1) + data->ip =3D data->callchain->ips[1]; + sz =3D data->callchain->nr * sizeof(u64); OVERFLOW_CHECK(array, sz, max_size); array =3D (void *)array + sz; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 3e751ea769ac..6fbf5d4219d1 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -11,6 +11,7 @@ #include #include "symbol_conf.h" #include "pmus.h" +#include "off_cpu.h" =20 struct bpf_object; struct cgroup; @@ -580,4 +581,9 @@ u64 evsel__bitfield_swap_branch_flags(u64 value); void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, const char *config_name, u64 val); =20 +static inline bool evsel__is_offcpu_event(struct evsel *evsel) +{ + return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT); +} + #endif /* __PERF_EVSEL_H */ diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index dbaf07bf6c5f..d481bc466131 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1229,6 +1229,16 @@ static int evlist__deliver_sample(struct evlist *evl= ist, const struct perf_tool u64 sample_type =3D evsel->core.attr.sample_type; u64 read_format =3D evsel->core.attr.read_format; =20 + /* parse sample the second time to get embedded data from raw_data */ + if (evsel__is_offcpu_event(evsel) && sample->raw_data) { + int err =3D evsel__parse_sample(evsel, event, sample); + + if (err) { + pr_err("Failed to parse BPF ouput embedded data, err =3D %d\n", err); + return err; + } + } + /* Standard sample delivery. */ if (!(sample_type & PERF_SAMPLE_READ)) return tool->sample(tool, event, sample, evsel, machine); @@ -1339,7 +1349,7 @@ static int perf_session__deliver_event(struct perf_se= ssion *session, u64 file_offset, const char *file_path) { - struct perf_sample sample; + struct perf_sample sample =3D { .raw_data =3D NULL }; /* avoid accidental= read of embedded data */ int ret =3D evlist__parse_sample(session->evlist, event, &sample); =20 if (ret) { --=20 2.43.0 From nobody Thu Nov 28 19:55:14 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 082E61C2304; Fri, 27 Sep 2024 18:54:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463253; cv=none; b=i0wnIkonv4obbGg18713M5YJy11mp8cbqvme1iMPWvQLbEvDjLtgzlYGBK+VRGVf5td8dfSFWWh07LBeVsSo0LCAQ5njWEU63I0uKQk+63ngFDlFZF4A5QSYpVoGeRXElvOKiq3HE25SHIscAiHR0MOElUrfuqo7UFQZY+vcnFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727463253; c=relaxed/simple; bh=X+JKJXC1a/9G1YNDO53mXwGlKy1Lamq4AKlhcURPFo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c5ABRGIqOufO8YZ7fnSiauM0jJPNseQKq5oMUh4OqLUaiOzmvpnux+PRtTMx1kzplSAb1iYxIH1CddV2EnzXFGzMc1DHPOG0jBjJdgPWRYCixFQtxbJZuiyd/uLZx2xY1qktQZuYoDfYydwi9tt2SwmFCye5jMAYK67CEBCNues= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Fg4F0tJw; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Fg4F0tJw" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71c63a43146so162989b3a.3; Fri, 27 Sep 2024 11:54:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727463251; x=1728068051; darn=vger.kernel.org; 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=rQIfGB7He5t4n0D5C8kX1a3R4H2i7SsPsgMHkzIUBDk=; b=Fg4F0tJwvlUbReoDhn4yE9pkT7tecZsJhfARUBkMXkfuYejVAwdt/e1VGlOMAplSQ/ 3PP6hzCFBXK6JT5UjWT6WRzav3h0Q6DJj8G8IozAaYOdM0vXBKmtU+Ufaz4+4FhwWsU6 +II3WsOCEyRWmOce/aDL105yB/5R50JbGHS5QeumDt8PENP+umZ8WcTfWWV9JKh/4tNx o/oA1YAurdJ2T2sSJZV94D9j0uri3ojz1HEZnTI4+AvQPPP8AMb9qQKGEJLfPNXT+6ce jAk1Y7tqGig/0Q9IYA91U6nPj9KYFeQfTZd3RnnX+OV+1v7iM77FQcl8n/hzuTdaH2Gq YybA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727463251; x=1728068051; 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=rQIfGB7He5t4n0D5C8kX1a3R4H2i7SsPsgMHkzIUBDk=; b=CDRjubb6wMPQy/f4p7pgAghtIdsDFezHsmgdjcoWt2m/T5JuRgWs1hk8pX3yXYzcma RJaR8NYjyVGS7K+ZBKTy6O1O8UMa2oD1AJDynZRngiGz0+cOhAMlrYxS3iFTc+ccEy+A XU9ZnJChxTojbimkb2qzCM2VYMN7s4N/ZzStY+iLCGW8775lD9mvp4J2VQn+WVR2PIKl JvGEkyOlzLOHswz6b7BiWoj3FYsU5AW/CluguAkg+HnD7vHWJHyQuMDCUFGugYBVeMr/ nRzfiTSyHi4zcu6+llcvjuydXeGBHVZA0I4awzo8KxeiDhKfJI40t+vGGI1bkXcuLzOs 4zrA== X-Forwarded-Encrypted: i=1; AJvYcCWW1oyNlWk8f2Mn4y5bavFIla55Ar97BxeUww44V27pSqj8OzTri12EVFouqPGqIuBSjKQHZ6cxlT3v0QkooceZHg==@vger.kernel.org, AJvYcCXdzlX23OqBnTSnW1QXhYx5eWCJHKzYy7ocC1WM4luN4yMb4R98fWnL95pDLMNb7/kkFT7GrQuxmjj25zg=@vger.kernel.org X-Gm-Message-State: AOJu0YwYYDR1Fcn43JsmYl7To0Wc3rCaiHUI3WC0zp20nqsLObFoRJi8 ErsgnZxFB9yogXdFXp03qk7xdiyKE1LOy8Jk6p1aBYhE4eOra26o X-Google-Smtp-Source: AGHT+IGv7A8SJeT/RZ+teJHR1dJlL0YCz2iTt7Efr60ulXkrDL3WWMcnEPYOYTV8nyHfSj7049qFcA== X-Received: by 2002:a05:6a00:cc1:b0:719:7475:f073 with SMTP id d2e1a72fcca58-71b25f01de6mr6784585b3a.1.1727463251467; Fri, 27 Sep 2024 11:54:11 -0700 (PDT) Received: from mbp.lan (c-67-174-206-244.hsd1.ca.comcast.net. [67.174.206.244]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264c23b6sm1930244b3a.88.2024.09.27.11.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 11:54:11 -0700 (PDT) From: Howard Chu To: peterz@infradead.org Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Howard Chu Subject: [PATCH v5 8/8] perf test: Add direct off-cpu dumping test Date: Fri, 27 Sep 2024 11:53:40 -0700 Message-ID: <20240927185340.658143-9-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927185340.658143-1-howardchu95@gmail.com> References: <20240927185340.658143-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a simple workload(offcpu.c) to create the scenario for direct off-cpu dumping. Please run this test with 'perf test offcpu' Suggested-by: Ian Rogers Signed-off-by: Howard Chu --- tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/shell/record_offcpu.sh | 29 +++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/tests/workloads/Build | 1 + tools/perf/tests/workloads/offcpu.c | 16 ++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 tools/perf/tests/workloads/offcpu.c diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 470a9709427d..aa33beaf58c8 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -153,6 +153,7 @@ static struct test_workload *workloads[] =3D { &workload__brstack, &workload__datasym, &workload__landlock, + &workload__offcpu, }; =20 static int num_subtests(const struct test_suite *t) diff --git a/tools/perf/tests/shell/record_offcpu.sh b/tools/perf/tests/she= ll/record_offcpu.sh index 67c925f3a15a..69a9324ad7f5 100755 --- a/tools/perf/tests/shell/record_offcpu.sh +++ b/tools/perf/tests/shell/record_offcpu.sh @@ -6,6 +6,10 @@ set -e =20 err=3D0 perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) +TEST_PROGRAM=3D"perf test -w offcpu" + +ts=3D$(printf "%u" $((~0 << 32))) # same as the OFF_CPU_TIMESTAMP +dummy_timestamp=3D${ts%???} # remove the last 3 digits, like perf script =20 cleanup() { rm -f ${perfdata} @@ -88,6 +92,27 @@ test_offcpu_child() { echo "Child task off-cpu test [Success]" } =20 +test_offcpu_direct() { + echo "Direct off-cpu test" + + # dump off-cpu samples for task blocked for more than 1.999999s + # -D for initial delay, to enable evlist + if ! perf record -e dummy -D 500 --off-cpu --off-cpu-thresh 1999999 -o $= {perfdata} ${TEST_PROGRAM} 2> /dev/null + then + echo "Direct off-cpu test [Failed record]" + err=3D1 + return + fi + # Direct sample's timestamp should be lower than the dummy_timestamp of = the at-the-end sample. + if ! perf script -i ${perfdata} -F time,period | sed "s/[\.:]//g" | \ + awk "{ if (\$1 < ${dummy_timestamp} && \$2 > 1999999999) exit 0; el= se exit 1; }" + then + echo "Direct off-cpu test [Failed missing direct sample]" + err=3D1 + return + fi + echo "Direct off-cpu test [Success]" +} =20 test_offcpu_priv =20 @@ -99,5 +124,9 @@ if [ $err =3D 0 ]; then test_offcpu_child fi =20 +if [ $err =3D 0 ]; then + test_offcpu_direct +fi + cleanup exit $err diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 6ea2be86b7bf..c7a5e27c4567 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -206,6 +206,7 @@ DECLARE_WORKLOAD(sqrtloop); DECLARE_WORKLOAD(brstack); DECLARE_WORKLOAD(datasym); DECLARE_WORKLOAD(landlock); +DECLARE_WORKLOAD(offcpu); =20 extern const char *dso_to_test; extern const char *test_objdump_path; diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/= Build index 5af17206f04d..0e78fd01eaf1 100644 --- a/tools/perf/tests/workloads/Build +++ b/tools/perf/tests/workloads/Build @@ -7,6 +7,7 @@ perf-test-y +=3D sqrtloop.o perf-test-y +=3D brstack.o perf-test-y +=3D datasym.o perf-test-y +=3D landlock.o +perf-test-y +=3D offcpu.o =20 CFLAGS_sqrtloop.o =3D -g -O0 -fno-inline -U_FORTIFY_SOURCE CFLAGS_leafloop.o =3D -g -O0 -fno-inline -fno-omit-frame-pointer -= U_FORTIFY_SOURCE diff --git a/tools/perf/tests/workloads/offcpu.c b/tools/perf/tests/workloa= ds/offcpu.c new file mode 100644 index 000000000000..57cee201a4c3 --- /dev/null +++ b/tools/perf/tests/workloads/offcpu.c @@ -0,0 +1,16 @@ +#include +#include +#include "../tests.h" + +static int offcpu(int argc __maybe_unused, const char **argv __maybe_unuse= d) +{ + /* get past the initial delay */ + sleep(1); + + /* what we want to collect as a direct sample */ + sleep(2); + + return 0; +} + +DEFINE_WORKLOAD(offcpu); --=20 2.43.0