From nobody Thu Nov 28 19:52:56 2024 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 437E43218B; Fri, 27 Sep 2024 20:27:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468873; cv=none; b=kqJ8vuqwh+vav2NxI3xspBpYUw4IlVhUD7wOinm9c98+surpGsZyNdtzrXUpN6d1dW5dWvbJNaAOCcS6S5KMQvcMhGgXIkscagE+BeGQ/bD37ziFA5NbCBj5mj1LvES/LeWFZVjdkWMEGR0DLOvQFGM5Z4ZeE6j+YUrxkwpkokk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468873; c=relaxed/simple; bh=nIUiDxgqflD2SH7nvjL7lUKBxvllcX5JqrcgK+ywIMA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cbZmaWbluZHHT/To43Sve0BvlTuJUT+L5mER3HhP6IjS4z9u8MFr6HTuA6EJjGammYAPl0PFsDYESxGNup5nf/atPUyhv8ML6zrgQJezf5gTzJw4Y+4jpTSROH+6MrUDsbtkGQ7k5CNUBINyVal5rhuBwUPne4S06izoeVJkbKY= 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=T1DoLdIQ; arc=none smtp.client-ip=209.85.210.170 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="T1DoLdIQ" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-718d91eef2eso1876682b3a.1; Fri, 27 Sep 2024 13:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468871; x=1728073671; 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=T1DoLdIQl7wNwqizJAdb6GFuxSDzZ+JB+OSOSlpOl9l1PrcI5pVzO9aCNUx8gtzPV+ nccHnDhzZ6c/88zMpPOHYA2WjHA9oZuYY+U1YrkvZnhmYu3E103tcSosangsgCfY4w3d fg/i0l0eWaE7687e2adb/kPeZZxxjCbdYl6GqsYVJroMtoCsIdyfChE5C9CwRzKdsRbb 1cLRaI2NSfDkZexIOTgsxNP1ZlAT0OdgTP9pa/2Dack6iXlYmcDnNGaer52G/+X9BT6e DUiu/LwVz6/YLFf/YC25Ix9ozUWzT9FTJ6ePV+6Gi+ZYuj3Xy1nGShG1nBdMukhQnuW2 DIvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468871; x=1728073671; 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=XMSVGarIiDFBTItBeW6RFALpHJM+fbK7NCJ9/f3V/VWFPMhatroy0Y29zwazIpaeUr qyt8iudfquOIprssZQntMAgRXT3WIKR4nZHxVIH31Qc85Q53u60YxOHsz1izpuVSNzss Nyb9whSokvHNMb9jZEziILGkaw5WlxGjb29HQZAMiK55BzCw9GfBDWDGRslzv1lWmQqK Fs3YBwfoCt+5eWBI7JybTwmOgtvMKMXu3yAjV4nYH8ujVbatQA/PeyivBFf311IOKuA7 JtxeXKrg7ddDTmwh2uCo+2FPjvL9Pi+ZaXuEQXjQOleoVQTu6P6k7snQ50hZt/cglA85 mvzg== X-Forwarded-Encrypted: i=1; AJvYcCVKS2kO7R/MFH55cY2eWhTb5TOkCc3NzGUfTYIWaJPj0MSfN5TOQ4gL5yzcdqalWdjw2DRwIsHZ+Fb7A7E=@vger.kernel.org, AJvYcCVpGb0wNZ+ZdI5Wggb33IdqmZoGMridapsrYRX+BOosqP7oBdt/6QCnPofNpQYMhWNaptn3BOXI4p1is4ZGZJTSEw==@vger.kernel.org X-Gm-Message-State: AOJu0YzPpCjF/r8XNYPZ2oon0R1qdRUK8mn3rp21B3Kt3sv48bE1CwNM k0G6Faj8PB5t0ZXgeNZgXXoOfP+3YevTWMF5JFXrXVCav30vL+uy X-Google-Smtp-Source: AGHT+IGR8HfrA9zcE8hJGZd47QRkMo+fSaSORak8JzPEwKCORr3rbmc2RLaK7T+xvsudDocLOUlrjg== X-Received: by 2002:a05:6a00:17a6:b0:719:110e:fc8f with SMTP id d2e1a72fcca58-71b1924fd3amr14000413b3a.1.1727468871384; Fri, 27 Sep 2024 13:27:51 -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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27:51 -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 v6 1/8] perf evsel: Set off-cpu BPF output to system-wide Date: Fri, 27 Sep 2024 13:27:29 -0700 Message-ID: <20240927202736.767941-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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:52:56 2024 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 A6CCE1C2329; Fri, 27 Sep 2024 20:27:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468875; cv=none; b=P6LwPWyBsMLT7SmOQ0B21IR1TRLWdgdb98L8Nvnxb3LiodlsDw/i469Id4gZFn3uSgnIhbjnSpJ7DAXiyYk/SbI9ods/TMLW9md6AjyjT+hYUutBkI8Lr3e5+ABb2NNosFUAvBKOpA3w5tc8f/115qFkQLYLa5tQ+Ky8GliXc+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468875; c=relaxed/simple; bh=eK1FtnbY9Tri45jB5Cvw0onFqkVbxt9kUVvxXDph0Qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DzlLg586/X5H5qnZ+5sw3ohGawiR7GooPLSXmTbllnPHatIRK2V4j+ZGlDrTCcP+uyAZ8pZgbM7YuT1yglxBIkx8R6PG3glS2FHc0WI+JEcP+XIHlvjIy7DXFVVNyblTn7LdRta+S48YhUhuLpG8yKl+9OfRnGwLCPIm9CdzQUw= 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=cGScaCTx; arc=none smtp.client-ip=209.85.210.170 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="cGScaCTx" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-71b00a97734so2173266b3a.1; Fri, 27 Sep 2024 13:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468873; x=1728073673; 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=cGScaCTxObbrCYiJZtLsxTzPIW53lV++2cF4s2M7iaAKy/KrRL2aoRNmVPSYIjrHCv dujqKzLhMAcQ156dN6y7g28gbTRaJUyUbrv0G7W4oWwF3SlIgbay02qTexor4NOrNIT/ gWXsy7NmXk966VSVQb1/8EIsdtTKzXyv/W8QX1Adp2OFa/XLd5jOB6Ntq9y/j1dDzhA1 iD07/GOSMXlHOdpvSDAluVgNzh8sfdS0gqzvHl3+L8sbkQr1s4rVDKOUtNSBoau5jFd0 ohVpfhEDJdtptfIXT2UwXStJvUJzs4f1Vxhfox5q3Z+FTFGhrWzuIxFIxeIcmIra+Nd8 BALw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468873; x=1728073673; 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=Xl8zVX8xjObMKzCw5zqIAQ5ER/ZGoWsDEzrIr8arj1U9yNMkx8kDCze4pySHlqwNfG IPJsNiG6R9F9WaXXdsoSot858WWaPt5TyRENw2OGlKfHNCS6g1PUg5unGkseVSA1gL+E a+15ZfGg6dvH9zQmP4yBznDl+ZEE/YzpXUs2yAwXLKLtKQ6GpkkxVIkiyeD9jKUNuS+h UblZtvHAD6G1hSkoyGS8VZRyu/jmgEUaG4xjGwM9ApwB8ggPwCfcw+TLTEZx2fL0BTZe g6p6aZottKzA9/k5HAfLqUeLl4HYORDvxYABTrr9q4oxYjvHH4iWbZoRBqToxcanyU9M 4RuQ== X-Forwarded-Encrypted: i=1; AJvYcCV/5LEroh/TzLFu3D1fDhrcNjewkkamHM4J+R/JVqcgXpbFB86Ke+tP5qrgmpbzs+TDERgqStKSFrfS6+vgJypGXA==@vger.kernel.org, AJvYcCVuOHhAz0o0xU+jRcL3NJBvKap7SS/cRq2DmjE7p5JYBUW4WPrOXwmwA/BU5G4US961jZAbn1CUcO3o8xQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy38ykR9D6qQtFmY9ra8dFf8fT10Nj316415fWvCYYP89UyZR9M 9Q14/hsyvN9DAUbF9I2EJywtvLeaBPCt0XMWgNgSxPZXvqkh0av1idZa6dL2 X-Google-Smtp-Source: AGHT+IHJqXEKdYzDG09TMUZ3imL1DXHy6rViWAevy6VNRTYtBnlplaimxJzvT5OqpzBPeCJqF6i33g== X-Received: by 2002:a05:6a00:2d29:b0:714:1f6d:11e5 with SMTP id d2e1a72fcca58-71b25f6efb0mr7241083b3a.12.1727468872977; Fri, 27 Sep 2024 13:27: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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27: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 v6 2/8] perf record --off-cpu: Add --off-cpu-thresh Date: Fri, 27 Sep 2024 13:27:30 -0700 Message-ID: <20240927202736.767941-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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:52:56 2024 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 26EF91C2DDA; Fri, 27 Sep 2024 20:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468876; cv=none; b=ExeICDJGwD1KEEgYWtKfJB/LHhQUJwWI4jH1E88QWUv/vScqKO1UmLtpJ7NBEsRaHVLGf6/y9pb/O6DXy/Ikh4P30FSeXWbWcYAM8tkezwjXbyWMuV/RszHOgHA91CHdbEw34FeYbCHYpYxUFQtipb9jVlKMzTLMNDAR9879B+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468876; c=relaxed/simple; bh=IbeF3vvTFS3uXzbOh2z2k7t1WaKPLo/Or5yaygXV5PY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TVfgKFhHWEe3Y2vQXJk3L891PqonpqmRhBbnZNuseJ2hAfRGJ6ZgdP/eB+zaoJd4SWFrbSLrL4QfAOmoj3VI0WOiz2xpdF2ghq+oRoi2uFJWpwJgG4ESGkamKKePVpOXQWf2ttCVB7vuboy5nAW3NNtx44ZGVfNcBQ85QyfdCBI= 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=A8ncmt0w; arc=none smtp.client-ip=209.85.210.176 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="A8ncmt0w" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-718d6ad6050so2110082b3a.0; Fri, 27 Sep 2024 13:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468874; x=1728073674; 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=pcTojKKYJZOaO/dDLRwrljyuqPboNnN4q6IqpqSp2Jk=; b=A8ncmt0wOgGRKxsyb2JSyfi+/6ZYaAy4S4iXjUNr/wn09BZldODo1DraNfwgZFfhnL rFK5NBD5tCgandYccj2SDcmXqBKpDAUG6bW6rX1BYDLh+l/9HOiDqw5TlimYHcWmHb+y jNkEOMjPOE+NuFPZcaZW/Dvvl6FfdwzZ31BVDzJUOkzaqqD7mly9QYEDV4LHAL/vEeBe AW2vmb8HEk2RZwhh0Rg0O5inK78vEidupO7J7Wob6ej0bXawhv0RRhetrtyxPmoLa+Pv sLIuM4UXuktYoSrnrBJZcdZBO+AEcxrG9nr9uac1XpxCVEeGF/dMY7g4+xzQjGtvDMwO Dkag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468874; x=1728073674; 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=pcTojKKYJZOaO/dDLRwrljyuqPboNnN4q6IqpqSp2Jk=; b=MxMYaAvjYyGiKHEue0sgBhv92KduoedgyaDi5LHjKBVUZs2raRuQPLywQydEjyAg+9 2oMWdjL8y9CHnh862AqL2HPJxS+ibV5mxGcUSCrD6KISskRcxdzkPmrVEoqQa0vN50+3 OrbhWmaNzp3ckguhMewW6d9YdgA8pWnWnBhXVZYL9eKNwO0JBAtpVN3VPVdlRxSk+iUf bFmXts17UwfhTnFrY0ER68ONNhDkwZIzTxe2g9Lq0y+QWZlWLg65VsAUtR5fcBezP/nC kAWDP4sGqE3p16PqfQPiCQlWTJINVCkTg40H2AlKHGkAZBmXMi888pNv9cq/25r6+kDj ZWjA== X-Forwarded-Encrypted: i=1; AJvYcCW4lucvH7AsXAiM6ocGzmFa8POn4kqWdZ9MhoZGSNKFyZUD8XslXPang/i461fYDxQyDUX55OQTvmfd5YwhkeVR5g==@vger.kernel.org, AJvYcCXVY7NXLAMKESDLsRJ1jagFY6DIprS1sn4CEkG92cEGnQbFAiYHTxqCH3t9dzHgDS4GWv9sx0SVowArwXk=@vger.kernel.org X-Gm-Message-State: AOJu0YxMEV9/+NxpxQzBDAfrvnR6WOBzVh2oX0WmT7wQBD1RV8p+bpSK zcX1gbZZTKsHIQlp3XzKFTeRtLlBCSupwjoVgwJstt3H1lY9G/Tb X-Google-Smtp-Source: AGHT+IEsXIE4fvWhmOMoSWZDl4693YEoXWLviaEmLilmHw14TJwjSso+3+PTy7DOYNYYvTqPKhPzsA== X-Received: by 2002:aa7:8881:0:b0:717:94d2:43c3 with SMTP id d2e1a72fcca58-71b2604c297mr6914990b3a.18.1727468874356; Fri, 27 Sep 2024 13:27:54 -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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27:54 -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 v6 3/8] perf record --off-cpu: Parse offcpu-time event Date: Fri, 27 Sep 2024 13:27:31 -0700 Message-ID: <20240927202736.767941-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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 +++++++++++++++----------- tools/perf/util/bpf_skel/off_cpu.bpf.c | 9 +++++ 2 files changed, 41 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 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..e2a887228fd9 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -18,6 +18,8 @@ #define MAX_STACKS 32 #define MAX_ENTRIES 102400 =20 +#define MAX_CPUS 4096 + struct tstamp_data { __u32 stack_id; __u32 state; @@ -39,6 +41,13 @@ struct { __uint(max_entries, MAX_ENTRIES); } stacks SEC(".maps"); =20 +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"); + struct { __uint(type, BPF_MAP_TYPE_TASK_STORAGE); __uint(map_flags, BPF_F_NO_PREALLOC); --=20 2.43.0 From nobody Thu Nov 28 19:52:56 2024 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 8E7F61C32FF; Fri, 27 Sep 2024 20:27:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468878; cv=none; b=DDhHpDEdGOFtwQndsgykcQOm1Uh4e6tGcbU5j7eHmnf7kKYIaKfpreM9SHoxAwYlS0aXasl9chBFtzb8Ad8Pfq32VofZMOH2EcZVKzEIdNY1P+ImNYlLFRFsAG9TscUzPF8JrwyqcU58RRC4jqWUqCYk1nJ3yJ7HFFiiG5X+gro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468878; c=relaxed/simple; bh=PR5QVdCsEBfojftC0DRkQDTUh8AGD0Jd4venUPYzFpM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DeF6sqzW2bY6jPGx1JcTvuzMtKGAx2ir5MlmW5eCVYi9K5El+Noumpj4MII5hn/4GgL+ox4Xe6L86KBPhGnzjsLOSB7OpW718JZ2b7zQXL+bYtUzYqrlBXxCJwshMoXiZngFYJpOD+GYXkCCQ1FSEhqNpMVCxhJFj1Ox5mn0p9w= 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=gsdbC0r7; arc=none smtp.client-ip=209.85.210.173 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="gsdbC0r7" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-71b0d1c74bbso1902480b3a.0; Fri, 27 Sep 2024 13:27:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468876; x=1728073676; 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=1TV6gjPlCQH3VQLRZqkQzPkSAtLRPKMlMHfK8f39Wo4=; b=gsdbC0r7BHbtSOkRDBJ8MLQWskhrjzcpiFmRWEn0+eZ+MXZ2GnuCp3DCeXuXsKkEJO 2fIxIynrncK4A9Ht6S9LXOd6VPYz8+X6mSNyoJeI4cn/7BSmRM/MAQ9fhFwVeqeCebrw AzE3fXqLH67N8nUIrXtZlzVQrP4N3EypiahrFd/MCogfhYS2aN5PDvnR6fJsNek6pbaQ gIn8fC3OHBBn4gOhY2eAYtyn1ECrrLB5reNgeJDqJDHHAtxQBh/1HEbMBt3bo+UxjOP1 SnWH5blKnrlogcNUdAlwitkhKm4H08rHXYJD/hjeox/7XllUU2JjHP+/ffCwRYVlIpJu HoQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468876; x=1728073676; 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=1TV6gjPlCQH3VQLRZqkQzPkSAtLRPKMlMHfK8f39Wo4=; b=YiaM/N5SFJrhCXeES6vRRrdR4D8eks9/OZgbP8MYmizIk55uaPAjJAbiK0jkCptNCq NP3Xpk/G3eTSboJjdvhuvO3h5owEpDZxa8o+5EvW5CoDvrneYA7xxFStDMsEaZf6NL+N aFQOvVJ512ptWMVBt3ihgvLQoGl/afmOckgm+o+KCwWnd3FvYfotJuwBWi3B1AwM3pkq IdVcIlKK4QsqAbFfPMqLpAjxi+vz/necC2DwbHEnBTrYkmt0rxD3Wwrqe0XDmFgl0VA2 aEt52f9U5Ql4Pwdd5/hqt0AoXziLF4pvkhYCKpJFH/SIMJXHlaa6rFnLKhXzdTAKEAPL BUxA== X-Forwarded-Encrypted: i=1; AJvYcCVPPVzdpUXpRyNeVuziTR0df23lTcHHLJoUPqlQ2Cisr4xKKw2Gj+3CKDiswuAgFCenDwN1R09tl83ianA=@vger.kernel.org, AJvYcCWBpDFTqN+IoS6M8LHAgRrruK3lK04zvYJZ1anes6p91KKCtOET4rGBMtVQbK0MgJbGTU991AMKt3szMf+b6EXelQ==@vger.kernel.org X-Gm-Message-State: AOJu0YzCgqE8iS511nMNrOg/UK3phoKmrFxVQIZQ35BOto3NQc/lK/og 62YWmQUZEefmzS1Igh+UEfoECp1rj4VmxVQ3gH8fJuffgjNbwTNv X-Google-Smtp-Source: AGHT+IGoQFuFdARoegUbWTr6zfJO4xEpfy+96muD8WCUK/cqrwX4jlR6KEY7yU4zTQ+RG1aaLRdp9Q== X-Received: by 2002:a05:6a00:17a8:b0:70d:265a:eec6 with SMTP id d2e1a72fcca58-71b25f6efbbmr6708292b3a.13.1727468875772; Fri, 27 Sep 2024 13:27:55 -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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27:55 -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 v6 4/8] perf record off-cpu: Dump direct off-cpu samples in BPF Date: Fri, 27 Sep 2024 13:27:32 -0700 Message-ID: <20240927202736.767941-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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 | 112 +++++++++++++++++++++++++ tools/perf/util/off_cpu.h | 8 +- 2 files changed, 119 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 e2a887228fd9..c42d0e2d91d8 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -19,6 +19,7 @@ #define MAX_ENTRIES 102400 =20 #define MAX_CPUS 4096 +#define MAX_OFFCPU_LEN 128 =20 struct tstamp_data { __u32 stack_id; @@ -34,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)); @@ -41,6 +43,14 @@ 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)); @@ -48,6 +58,22 @@ struct { __uint(max_entries, MAX_CPUS); } offcpu_output SEC(".maps"); =20 +/* 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); @@ -194,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 @@ -209,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) @@ -238,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:52:56 2024 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 1774E1C3F1B; Fri, 27 Sep 2024 20:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468880; cv=none; b=BfSGoE64P4rzEESzTUfqUxceyLfh+V9LlH45iNw+FMubQduice8VBtypNYtqVQ53zyJ0H/pMmnNg/M+Hoo58b3S5reDaTDbGBImYAIGKP5Q+hsJL3QchvcoQGzT+6/N/7ddrcXA62BuvK+UlE7k91OMNr8GUT4AgPsPsKJzoaGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468880; c=relaxed/simple; bh=K1q2xLCuxeoesKIjk0h8yto/lXtLTYRLvejIlbxFPMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U8BhT8fEuNsOAeUgvOzgRN+3cKBhIRbclHoehkTuv/l80lcX1MYaOjOyCJRjSGC5Yw3y8O7KTgdMbM7CqI07xKLiLtL+cQ+T+QR+jUf+0SpKiM9hxcEV8ouO3WItwlmxOTM7raV35rSO4HNNqlJZ/fqHjA7x1ZOzbSlPzkIY83s= 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=VguJrTbB; arc=none smtp.client-ip=209.85.215.180 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="VguJrTbB" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7d50e7a3652so1739384a12.3; Fri, 27 Sep 2024 13:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468877; x=1728073677; 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=VguJrTbBG4prltPtd4PmfGeAejGaq0uZUMFfSK47OKUU3SFDSNrD/t6mOW0LsWYSvZ dQkU7ruFc7p0UtMSsMj8Z6MAwMLdLzGyPBk6zDzgiewrHWyfB7xayeQe9y2m/Bs2JZZ7 2m+CYzYQLv3frEcvb5iIbcMuQ+/cT7e/qdFr+fxKk2O/vtduBJ2T9AWjXcoXZ9TIAyWT SEgdRS7S/iFeb/pG9dJMjecupF+bigJfmRC32RLpvfRpN+sElS8VHkYcn/zaB/RhFi82 aIwqVuRxBM7jDLndTf2XT61L6l/VYETwgE85uCWkMhGsOIYrfanjqm6tx/hBbv720js8 QPJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468877; x=1728073677; 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=NY6U/aeMfDBsjpt6zFCsuVU+TtewkQwlOLmXx2I5+xeztDsix2EGgrIeJRyk45RdH1 bwOs0SJvdaTHgK04WSEUXl5RucnNTPU/PewPAVvBoUERgsOSQ42OyEjy2N6sZeWrMT9P JuUfBk/r0QE2fcHqL9Rz3g0/0YeSGIOAi99ilG78+M+CfWzLWHxpAGqI2CxftCKUNIKT EfLgM8pvmANq5nsSWDChaZtn8cqTmnVSQrB1vIlG/0io3gJCNEr6TcJ9XUanNZ3hBzA9 EnuAk8m1+wm0SWyEKaMAGlogs8rLk8V8g3V8yQuvhbX97bJ9C/bKZZWuD0DN8U2mALi1 327A== X-Forwarded-Encrypted: i=1; AJvYcCVTeYAGJc+2fdeXf3lXXKrbG/qKua/MM1VE4r9pL6rfxrfq9N4/XCLEVKljyPw7x3CIfBKG6XRq1gKMVkk=@vger.kernel.org, AJvYcCXPyJRPsTgLkksul1rovAUCrvKjoASwee4tou0LwI8jxT2f/MB7xzt0hALTAQU0ehdqx0XN18YhyVrUXboTpjHKdA==@vger.kernel.org X-Gm-Message-State: AOJu0Ywos40B1hcahuQKhNkA6a7RWfUsjUgco4S4I8rK2sA35tn7e9Ma xhSeV8Ze69de/Hj77Wa741KsWHthlOZeXvWnF0vWxOWeKpqR46ol X-Google-Smtp-Source: AGHT+IF5BzhffwVtfCcMIYtigPSk3mX5TJgQXW8I8SL08Q1nbWCkqQzQsMKN9vBzTg+HNcZn2FmaMg== X-Received: by 2002:a05:6a21:3418:b0:1cf:6c65:5dc with SMTP id adf61e73a8af0-1d4fa7eecb9mr6019746637.49.1727468877207; Fri, 27 Sep 2024 13:27: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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27: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 v6 5/8] perf record --off-cpu: Dump total off-cpu time at the end. Date: Fri, 27 Sep 2024 13:27:33 -0700 Message-ID: <20240927202736.767941-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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:52:56 2024 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 5C5FA1C3F1E; Fri, 27 Sep 2024 20:27:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468881; cv=none; b=k+dc8lUytW1LZs9G8qJMxWtQ7bnCcsIci/8FcK6OylJeNWq2sLVADLojqpBKqXFO4NtbE2Y8Z/qtFL3HnSciLjMof2XrBdi7ews96fl6xQUmIQz6zB7VnABER9lXD3XeEUjYNLzGzBByfFEkrp+XNaglY3iJifaE86jWIbssaDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468881; c=relaxed/simple; bh=BfKLXsdD+x//Y3ebfcycwHplNHYTY+Xgi8RSHLfMrc4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=km+SPBrMew/XpNMvFYtZqAgzS7aKSCRK9MVO36/ntKN0leAnehR/mDWMGhlBqgagrhyu0SxunAlRdST9XAgyMNvxJvjzilsF52d7e5B8+g1cwFv7Lrn11fKsqSgHCmST5OyzsX+g80qNBuz6cIKvcnm9cam7cuy5h97FiHPwuZg= 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=QXfcFomN; arc=none smtp.client-ip=209.85.215.182 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="QXfcFomN" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7db1f13b14aso2074865a12.1; Fri, 27 Sep 2024 13:27:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468879; x=1728073679; 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=QXfcFomNSoE6GHIgqn0vV0aWzSS4WA6W2xabvVGthNR1WbEZrXP5KIoE3nE4vwrEJm EHrfZjiBvsq/9qOAgveqqPq0atZUoZB0N1CxQL48fqnLYCWrezG/Fx90dXdQf8eYzipO pGDvmk5WWqcn+sN+AFPuh/N1EdfghvhcQZhiQZGdENnOJ30oHR+VRj3bPvCF4gydn8bD xmqnAFBmSy2CbviixpXXQ1sd1mvtiTOEDqcipaSRvXH+8iY1uvrU80cXeLtgDbvXpOIZ EguT9d1WGO66fdw7Enjb8W9eVqardgBmUw2QYY9t/yV+yuTE+7OrYiAlJ6arwKsKUOny bYiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468879; x=1728073679; 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=pntq9wNGIoVoYjnf70ZdazKdNO8SuHKsERkiMM+UAF3uv/b7+k5+l1I97wz30v8y0H xlnw1Y/lyKIs0ToqwUnpJlKqeYJK480iMCf6t4EDkBxkAHNNip7eYDZ67LBZZpcZU4mz JlRLEHYLVYDA/CXlbtnPhSWF2kFeESLC+HeVAU23XnurvfUrEkNnElUX/HDbKDL5okyO zIQWi260lmS5nJ1nIkOgv7g3zdh9r5bJm5BWTYidFV7SGnnYxWCbmAiDmbMGSqPx4D3J SgefQfPiaw19R3nnk3eruDNt5X4VAJyaozXc2Ohg9Zs+Bz6QL6ZG9komjwY5pcV2Gi97 07DA== X-Forwarded-Encrypted: i=1; AJvYcCUZXku/b/v9IjTTVTxv1FQNdw6P9EDfDc5wl+mTwZnsRdUk3CVLUAldmYEJxWPllY/wdHqmTymE1METky4=@vger.kernel.org, AJvYcCWqAp8nNXtHYKFbuwSMa9CAI1ft2SVzIcaJa2Dez/ARQNdVaDMLj8fom/CI5u/dxRZiLL08ARBJLtiMtSk02uFzig==@vger.kernel.org X-Gm-Message-State: AOJu0Yx+DNBZvAKQf69mIWBQTdMj9a9JHMtQVtPKKZYc5GvCQ74YREMR eXbndfCTs8IvXfuygNeR9YnsKDg/vll/hNQIqZ4HwPyFYzan8m+e X-Google-Smtp-Source: AGHT+IHncx/0ceG080D82c2JmisjmK8fYC9I9mNouHjQFAXaatZBnS6JlN27YttmodW0GNM9FdQeWA== X-Received: by 2002:a05:6a21:3a96:b0:1d3:b30:44a2 with SMTP id adf61e73a8af0-1d4fa6394c0mr6878101637.7.1727468878686; Fri, 27 Sep 2024 13:27:58 -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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27:58 -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 v6 6/8] perf evsel: Delete unnecessary = 0 Date: Fri, 27 Sep 2024 13:27:34 -0700 Message-ID: <20240927202736.767941-7-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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:52:56 2024 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.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 E6B101C3F36; Fri, 27 Sep 2024 20:28:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468882; cv=none; b=KIw9GwImwp548xwAsH19aT2cifyyHPa36Ch1FgaeaWDgWly7cvQVlYBxtobmDsV0DxJfL9hsNBApvszRCPsI2vt4J+nEwUnvVryAu6e09mhfl4Fm3xh9542Hcwh0FuUr+AsYNecNJamRXaZL8JUlYGOjR0t6wkdE9BDlb7+ngHY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468882; c=relaxed/simple; bh=adiSf7+Y53/FM4VGaERVGEcSkoSzxS48tNZljvrnWsc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aWy63BAdhNox432+yrfdaBziyX/Pph8FBp7dKC/JBbANoWxhLA85ucJbbEh4VzZ7jokO4fw+mpUEvNdeuQPDv9Th4+6sC5XII2LrRvpBaQmSv54hg5Z5MsPYpdrlCJ7fSHE3WOo0msks7x1IDNE2RDhDbvN/3Xe6f1dY1zcvmVs= 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=KwBGBqVm; arc=none smtp.client-ip=209.85.215.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="KwBGBqVm" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so2162710a12.3; Fri, 27 Sep 2024 13:28:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468880; x=1728073680; 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=KwBGBqVmhAZLhmKHjA/mQ2tMwMqDtgTh2TcqnevEMhYir5lxwY45Y53rweRQSEI1oZ TxiitktJbGSz7aw4hoRm8B3W/UtL2oMdPlVhCtq3xQ+uwQ6ZaEvcLDzD5AbQPE503hDn 8wI2iM/NTsKfS/uZ8OdJVQ2huysD3NWJsI24A2MFtGpMtQM1tdQqiIF9psmU5bMwtJSE IJ7DhFXPZyCSl9bs9TQcMEc0vRyVhwnkRRqYAl0oq9qHnGjc5rtAq9I2d2O/TOGEwYbe xvOTAdNdLDxoJniiqff/lws/YLT1Frrn0STqeGFIyA1bxh9+36mAXrsxmXiMMWCpmL+7 6JUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468880; x=1728073680; 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=ucBqdUYV4vdfryPOTiA+9tuBRahz4D2ojDckLbe32jG4jCYpDIDlHO4wqbsIY4gFIy fQtpYIWQNosLKsie97+Rm3OyecVcS5iHLGh25byfAwVKOb4+gyTXdS1Y5+aJ/m4oIlEo PG+ZtlMxNgVBoNu9G+EvDALoXwVSdtbusIVflxwaDv9gjQ0psS9Np0TrrWBRpHDD4zQ1 C3TAIJbXk+Fw9fUKRZ+9Pt5QVXbSCeeJo4txtGBJxsQowj84NEvlSx5xwwXhE3WCh0C8 2rg0saHA8rcofoeYPKJSzJe73FzzH/O4PaQzhdu/K0c44L7HYobxXX3EwNSBBMQo2hW2 oQjg== X-Forwarded-Encrypted: i=1; AJvYcCUMY0bjoHvdHZ5YOhbe69FSCGiJUh3n+0jgBYOIJnu1HIhp7kVmqyaAk5s65GFxPXz8ILuWZrbsGbdNnI4=@vger.kernel.org, AJvYcCWLB8v9g7ThmMh8WOHikbsgRK8P6x3u6Wxduqh8UHyL6xqPZBggjmIQd/NFnV7R4fMTs6x8SyBVmtCC7T9eE3s6Ow==@vger.kernel.org X-Gm-Message-State: AOJu0YzjsiwjqJRHiSyruLh4T3EuEsr6ipP/e+bK6wu3ZQ1nCwdT08NV v8IQkweTsOwEFfAptbDOVRrSeBF5nm+sVl/+7srQjRex3XD87fPc X-Google-Smtp-Source: AGHT+IEGPi3DNOAzPoEjyu8FXReiXEVofM8eZXvDz8ZUh3b/uTasfPSZVSGXMVXlvlV3q7vH07Si4g== X-Received: by 2002:a05:6a21:164e:b0:1d2:eaca:34ca with SMTP id adf61e73a8af0-1d4fa7b570bmr5880986637.42.1727468880111; Fri, 27 Sep 2024 13:28: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-71b26524c32sm1998410b3a.157.2024.09.27.13.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:27:59 -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 v6 7/8] perf record --off-cpu: Parse BPF output embedded data Date: Fri, 27 Sep 2024 13:27:35 -0700 Message-ID: <20240927202736.767941-8-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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:52:56 2024 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 31D881C460F; Fri, 27 Sep 2024 20:28:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468883; cv=none; b=qbV5KwH5OSXPr7ULvY/lkJ3ytUHI8WgxsIRZEu3qdy7A5iN5PRECKb8d0DyeLRaHDH0rBi4extInR0pI0s2/v/NudO27LqfD17r1SmL6XPlvTyJKbmYxGKUn/bDib5y2TqGvpOLCbkCqDqf/ebujGxlwKVQJ1K+AJ2DZzTfBKvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727468883; c=relaxed/simple; bh=X+JKJXC1a/9G1YNDO53mXwGlKy1Lamq4AKlhcURPFo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g3VcgyyV/GIw9u8wxZ2zegSMj8de3J1zrkbnvzCNGDbYmewavq9A8DlFJek/pljV+ep5lm+Q+td0CXyLVseNQ+5uzAgJUQ1DJn3Osvgo6wPU5+3MW6Ef6qNLSxWURHU09uPgDUCKLHti0Sd57Z+JXOIobNYVjoNOk6R0MNYoUtk= 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=Oy2gWn25; arc=none smtp.client-ip=209.85.210.173 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="Oy2gWn25" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-71b8d10e9b3so864473b3a.3; Fri, 27 Sep 2024 13:28:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727468881; x=1728073681; 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=Oy2gWn25K8n29HI3EcMX4SCsmpHNo8r2mQrU+ika3lIhehDZMi5wEXzQzj5Ag4zUqI 7F7e6ST011oUEswelVfV69oJDSq6zGOZMeu8Ag4QFLKzoop1xTyi5rwP8goRd8e/hihp tGn09y0ZIwFvNTadR8wSVMeVk2gyIMB3E1v4yKtPU4TXbR3VRZZDyhhtlzDY1U3kP6aI HtLJJ7rvGziu/BHzjU6nqE6kUDY2Rpv5wgEljbzU30GnNrVz3B1ZZ8Ow8AixeAQvBUKk GlxZpeMoH0E4UE8pSLsljfi3OpTWRvRicXGhL/pn7fmDEcYkIXmkLdJbYa753+8U0F/R VlMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727468881; x=1728073681; 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=Esj8IApoiYooKbZ2ZHO11/IMRNvD3GsQc9gAhC+J2RbbnMm0iNt68ElA75qOpJLooG noWe+RHJg8zUJXewdO3J6WTH/EWsdlWWzf32YA6DQMgoQZLQKZfVk/66o6rGHFWhweK4 7KBwuBzWKq2ojFlpnNxJR/d6BXDNhRQ6v4zwnOv2i06mElWDiRu/Mx5Osj3BDDhpqFwi DHajthwq8VIUD7sf35WalzNhwQLCgnYrpXDXySDpZPQwRXBHoa+b3HIxUkgToUY5mMnb 4Hit34kODVl92FVpxvklFhNaSdHt3ArPTG3gtD/pP2uZJurvFx6a0Pc/bsYsGY42hR6p x8tA== X-Forwarded-Encrypted: i=1; AJvYcCUYjcbLZg+3hXV7HAzM6D7efnzz5mS6tnzuYQKdvjB5nVjTGJcxwFFKW4FsoNfg2WhiVIXXaqT/lwx/gpU=@vger.kernel.org, AJvYcCXN9t4cCBmmjHIxhV6h/SuGQb01Qrt6BwVEvM0zUcgPhQEcwgbcMpQpzDNaH3PVrwbQascsaOI6iQ5Uj8FeOkLUCw==@vger.kernel.org X-Gm-Message-State: AOJu0YzV0RKucFvFVUa5657BjB4Uv+brUX4xqdH5ED3/Gy3bf6wcV/Pw kmldpzpty8dsp88dempDPLxQeDzYUiwS43TRwvJ1eOuoUk6koXjT X-Google-Smtp-Source: AGHT+IHuls8O3luaymooSSTfoyhCXkxxZZQryTG0EzUisbgROYAUH3+M+BNPDSYTlJpJe98y3D8MCg== X-Received: by 2002:a05:6a00:178d:b0:714:1a74:9953 with SMTP id d2e1a72fcca58-71b25f6ef48mr6566110b3a.16.1727468881483; Fri, 27 Sep 2024 13:28:01 -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-71b26524c32sm1998410b3a.157.2024.09.27.13.28.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:28:01 -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 v6 8/8] perf test: Add direct off-cpu dumping test Date: Fri, 27 Sep 2024 13:27:36 -0700 Message-ID: <20240927202736.767941-9-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240927202736.767941-1-howardchu95@gmail.com> References: <20240927202736.767941-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