From nobody Fri Oct 18 08:37:45 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 EFB7C178CF6; Fri, 26 Jul 2024 10:28:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989719; cv=none; b=f0Y1fHK5ceppM8d9S3AzMpK8AkNkFL3l825ePmwcU67mSXLvAljmer7ZFfn9Fmz0HODu9agEJ4rJF47elBrvGYHJBTk3WEd9kJlOO0I2y2uYsh+06wyRyEDm1plx2s5Je/mfO5mmRD7f2oARhF/92OzWSZ6iPKRhuVW0Qq2fDjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989719; c=relaxed/simple; bh=MjhgwSqZimmdmD1dpQq5wlRkC5PeBJVr3Dq9nmm48Fw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QiAzySRxttbNFj83XopDfAGBsZEvrxwQdAPntj22eo4hiRNnOPxEWAsicckr3H6uV4vcQ88vmeRNhLV/Pii6dMSdoNUc0VjmCeEbRKcargdng2V8hGOAMRX04G8cFqqlMfEMMQwEA/MlBFp/6oKVi1g5GU2D0N5HXMC18b6Ex7I= 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=ZrTCzGnz; arc=none smtp.client-ip=209.85.210.177 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="ZrTCzGnz" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-70d19c525b5so605647b3a.2; Fri, 26 Jul 2024 03:28:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721989717; x=1722594517; 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=oVEF7D5qINKpDbwHrDwMFyfJ/l35hZL2O5jduliMP9M=; b=ZrTCzGnzNDu0lobzCFVuDS1McJFiQOJcw8PYprcsrQOPlXtE0PPXDQSBJaso5yF4TO vwJk0uKaKusbu8ZexP7IgWrTp3eHPZL32ozZFx0AGwPt5aQoEGWDjPVqZ1cm889LAsz3 qyAiEGWzgd1gfUPTAL7G6y6B3+LXrsdXmR1xfXxxbMMSNe6vbhQtVe86z1dDRYlygu5e P7/lx0W+9wZfsvoxrX9mYJ6BYxCzBjFvl4GTlA/VRJAaFqVPDvT7hhxitmZt0+CPkw1S ZzeG2ooCWX56hnyrJM8QRuTHEfjTTCSm+IWsbHqh2pZx3c/IRS5q/5pZU3euSppw7VuN B1IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721989717; x=1722594517; 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=oVEF7D5qINKpDbwHrDwMFyfJ/l35hZL2O5jduliMP9M=; b=tpZ84JLaxnv4GSXOqQsQ4I9Pq7ZikmGHrzNuj9OZm3skS8kANPpHqDqD/EOobgPccC J6ZMRXJMIUJc8E1bA5wJFQ3sV76MakdTtuifrAHk0vvYKNqu4JQ6TcIC58Vj1bvEU1he Dyj+XUh/uH1qV694E2NDrBgo4W0awbfN3KxlQkiO0ebAH87nhnIrpdWWm2OQykVfsn6C 5G2HCsFZTcN3GMjNUuzfMmZ3pTu8ISwkVjowqoFlF2wwhwZ/sO7ORIhqQTmZ/Ds0YsyM a3FW3xHy0plViPQagJUy+j9L4go5v9jLLbOgesvVASS6uiuo5t1DEzqvI1+qKUPYbTLP tcFw== X-Forwarded-Encrypted: i=1; AJvYcCU9KDsbY4OPpl9TwL5azazRpsGSWgpUR/ZMHUF7N/jgbFOsJMcyNyJqsCvbBR0hVYhM1TmynRkTfSsOc6r5vxn+ULMQ9HLE7JTt9eKTjSMPkduDH7Pqv1m95JlU4Rqy2NeNs7UPgNW02NKFAsRJVg== X-Gm-Message-State: AOJu0YyuACbrf9UOPzu+zkcGmLVHvrekSsXmrYPPKWgGVATkRG1qGFRU xfxIzucVTZBBSHJdsfazcYLYgV7MyoyZ9EUkx4a8ebOh/Bi+/jtIHCaItQEz X-Google-Smtp-Source: AGHT+IE//9XYv0NtcGzXg87opZmhH6Ab9BKIze4hU8Fji26nueGSX/rzi3uZCMxtyyXHAeboblFZwQ== X-Received: by 2002:a05:6a21:3982:b0:1c4:698f:c509 with SMTP id adf61e73a8af0-1c47b1db22cmr5014828637.31.1721989717103; Fri, 26 Jul 2024 03:28:37 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.244]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7a9f816da89sm2179964a12.20.2024.07.26.03.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 03:28:36 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/5] perf record off-cpu: Add direct off-cpu event Date: Fri, 26 Jul 2024 18:28:22 +0800 Message-ID: <20240726102826.787004-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726102826.787004-1-howardchu95@gmail.com> References: <20240726102826.787004-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 direct off-cpu event called "offcpu-time-direct". Add a threshold to dump direct off-cpu samples, "--off-cpu-thresh". Default value of --off-cpu-thresh is UULONG_MAX(no direct off-cpu samples), and --off-cpu-thresh's unit is milliseconds. Bind fds and sample_id in off_cpu_start() Note that we add "offcpu-time-direct" event using parse_event(), because we need to make it no-inherit, otherwise perf_event_open() will fail. Introduce sample_type_embed, indicating the sample_type of a sample embedded in BPF output. More discussions in later patches. Signed-off-by: Howard Chu Suggested-by: Ian Rogers --- tools/perf/builtin-record.c | 2 ++ tools/perf/util/bpf_off_cpu.c | 53 ++++++++++++++++++++++++++++++++++- tools/perf/util/off_cpu.h | 1 + tools/perf/util/record.h | 1 + 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index a94516e8c522..708d48d309d6 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3325,6 +3325,7 @@ static struct record record =3D { .ctl_fd =3D -1, .ctl_fd_ack =3D -1, .synth =3D PERF_SYNTH_ALL, + .off_cpu_thresh =3D ULLONG_MAX, }, .tool =3D { .sample =3D process_sample_event, @@ -3557,6 +3558,7 @@ static struct option __record_options[] =3D { "write collected trace data into several data files using parallel = threads", record__parse_threads), OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), + OPT_U64(0, "off-cpu-thresh", &record.opts.off_cpu_thresh, "time threshold= (in ms) for dumping off-cpu events"), OPT_END() }; =20 diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 6af36142dc5a..905a11c96c5b 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 @@ -45,10 +46,12 @@ static int off_cpu_config(struct evlist *evlist) .size =3D sizeof(attr), /* to capture ABI version */ }; char *evname =3D strdup(OFFCPU_EVENT); + char off_cpu_direct_event[64]; =20 if (evname =3D=3D NULL) return -ENOMEM; =20 + /* off-cpu event in the end */ evsel =3D evsel__new(&attr); if (!evsel) { free(evname); @@ -65,12 +68,22 @@ static int off_cpu_config(struct evlist *evlist) free(evsel->name); evsel->name =3D evname; =20 + /* direct off-cpu event */ + snprintf(off_cpu_direct_event, sizeof(off_cpu_direct_event), "bpf-output/= no-inherit=3D1,name=3D%s/", OFFCPU_EVENT_DIRECT); + if (parse_event(evlist, off_cpu_direct_event)) { + pr_err("Failed to open off-cpu event\n"); + return -1; + } + 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->bss->has_cpu && !skel->bss->has_task && @@ -86,6 +99,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_DIRECT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT_DIRECT); + 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 @@ -130,14 +164,24 @@ int off_cpu_prepare(struct evlist *evlist, struct tar= get *target, { int err, fd, i; int ncpus =3D 1, ntasks =3D 1, ncgrps =3D 1; + __u64 offcpu_thresh; 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_DIRECT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT_DIRECT); + return -1 ; + } + + evsel->sample_type_embed =3D OFFCPU_SAMPLE_TYPES; + skel =3D off_cpu_bpf__open(); if (!skel) { pr_err("Failed to open off-cpu BPF skeleton\n"); @@ -250,7 +294,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 skel->bss->has_cgroup =3D 1; @@ -272,6 +315,14 @@ int off_cpu_prepare(struct evlist *evlist, struct targ= et *target, } } =20 + offcpu_thresh =3D opts->off_cpu_thresh; + + if (opts->off_cpu_thresh !=3D ULLONG_MAX) + offcpu_thresh =3D opts->off_cpu_thresh * 1000000; /* off-cpu-thresh is i= n ms */ + + skel->bss->offcpu_thresh =3D offcpu_thresh; + skel->bss->sample_type =3D OFFCPU_SAMPLE_TYPES; + 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/off_cpu.h b/tools/perf/util/off_cpu.h index 2dd67c60f211..a349f8e300e0 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -9,6 +9,7 @@ struct perf_session; struct record_opts; =20 #define OFFCPU_EVENT "offcpu-time" +#define OFFCPU_EVENT_DIRECT "offcpu-time-direct" =20 #define OFFCPU_SAMPLE_TYPES (PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | \ PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ 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.45.2 From nobody Fri Oct 18 08:37:45 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 685E2179675; Fri, 26 Jul 2024 10:28:40 +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=1721989721; cv=none; b=XDfdGIzMRvwBDFOc86MGunkLdz5kEHa8AUQdK71jYAENHIhEUWu1+X+O00z8UgsDnXsEvtPFb+qr+WQwtukAGnggf/TWRmMnfBpoM0RSXCMtSRXgAFvp/n233OvymKRivVz8OyfU1FLn0ZX1cwB6wMoD81pnV9WtH02fkoph+EU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989721; c=relaxed/simple; bh=fS9SBM64AG0d14gzlJcmtqgK3QA3471WA3nuuLSXvNE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ds20IO7aCKDvhwncoQSrCiEnZIIq6Zi+j0lQPuYcdK91wFw22HyoHejloFubqT0KjMq7c1ngOYJBHe8OdmzGn24ZweXO0+yruAGeXmROn+prT+Hal+fm/OAGJKxfP2DxuQDs83vI3MhO7CZcThlv5t6YEDTZ4w6p7XjGE9U8CxA= 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=Z/w+pX9+; 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="Z/w+pX9+" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7a1215dd114so608426a12.1; Fri, 26 Jul 2024 03:28:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721989720; x=1722594520; 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=AQXEEa7o2N3nGNIfvLfIK7YtcqSm6PiBdbgGbpQNEDY=; b=Z/w+pX9+5EzAr6BQL2vyKjmi/ZfV9ndclCjp9LQOkPJIfc7r0RBPnKquOr0TU/YLbh N+peYIHyMmh1vJrLrk44skgs6gWHbuyXehtwSL6QouGQpaJjiIi5p/Yk8TjvekLH+6AG s9rH0QxF3Y1aS64+1Dzlo6su12Zfj9BtOzQlQV8jr8Q1i2jqje873dCMUpwLeOLi2jeb d4BMm2m5jQnjq0uuY4SNNYw68cmKvPKx8yIQeDSINqM44rdrcNnPdqBKW2xgQfJOqMpT OTbmTgyDjKibh96Ns6oAgXwaPzWZEJ0Ho7GqacK5ET9sD2e+VVgPem2tdsMliG/lqyrG wl0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721989720; x=1722594520; 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=AQXEEa7o2N3nGNIfvLfIK7YtcqSm6PiBdbgGbpQNEDY=; b=tqV51U7AURTuvA7HZoj1FD/cKub8eS+kuhoMpwHVriUZrh0A7EF8qCBcCKE7o8ESJl 58FK2emV3VzhliTjAK/Eb1wDC12jTAs7deM/I/FtVLmx0mviYS3u08QrPBHSn++8LR3G S5WWLF5cXyxQbf6FCZ2IzTtiUu5YyOTYt1SnxvXHKQQYNwqY4s72wuzyWar6e05xOMeu i7gQWdQ4bVm1c9v3E+E5ZWD1xj8QUjJnwhHDP/NEiMKVR0fdmKoudX/V6emrLHPod7ip R6hGrESm3FAvXnynwCIiVbOIXWjJsYOboyE1XeVaXnSKuyf+9n8x5jPJ6R3S4TXlkH9E mk8A== X-Forwarded-Encrypted: i=1; AJvYcCWyBxI9LGZWx7bY7ZrUIVHEPzVZXMKH3l4wxnBfa7kLGEXQLfz6uuFh0XR4x1qmfuh+EjWdXIYj+PYLJ6DcjtxWimqptb5vfE6JaXqStpA+DuF/JIr/499ijkpEmw4kfJb4OotEC4GTsXhgtWnh0g== X-Gm-Message-State: AOJu0YzAsoSrmCkY4zNV4wb4314l2q8he2U4WSFDoJq8+6vHt5e5wcpc duGBbtCQdt0hsSvGyS7KmMrPeQudIhxtLnN7QomVlrkMZLSLa7U7un2AmuBY X-Google-Smtp-Source: AGHT+IEFT1Upw9lF+F6Y8N7JGoI8iFl0O76iBsZ9yIIeSrBfVIJwpwinTXHDG72oqRSMQN0r80S6kA== X-Received: by 2002:a05:6a20:7fa4:b0:1c4:8da5:581b with SMTP id adf61e73a8af0-1c48da55a81mr2341258637.39.1721989719678; Fri, 26 Jul 2024 03:28:39 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.244]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7a9f816da89sm2179964a12.20.2024.07.26.03.28.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 03:28:39 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/5] perf record off-cpu: Dumping samples in BPF Date: Fri, 26 Jul 2024 18:28:23 +0800 Message-ID: <20240726102826.787004-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726102826.787004-1-howardchu95@gmail.com> References: <20240726102826.787004-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 in-the-end approach. Tons of checking before access to pass the BPF verifier. If off-cpu time (represented as delta) is greater than the output threshold, do the output. Signed-off-by: Howard Chu Suggested-by: Ian Rogers --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 143 +++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index d877a0a9731f..4b0412a7aa5c 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,45 @@ 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"); + +/* temporary stack data */ +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(struct stack_data)); + __uint(max_entries, 1); +} stack_tmp 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); @@ -96,6 +139,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: @@ -182,12 +227,87 @@ static inline int can_record(struct task_struct *t, i= nt state) return 1; } =20 +static inline bool check_bounds(int index) +{ + if (index >=3D 0 && index < MAX_OFFCPU_LEN) + return true; + + return false; +} + +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_IDENTIFIER && check_bounds(n)) + data->array[n++] =3D sample_id; + if (sample_type & PERF_SAMPLE_IP && check_bounds(n)) { + ip_pos =3D n; + data->array[n++] =3D 0; /* will be updated */ + } + if (sample_type & PERF_SAMPLE_TID && check_bounds(n)) + data->array[n++] =3D (u64)key->pid << 32 | key->tgid; + if (sample_type & PERF_SAMPLE_TIME && check_bounds(n)) + data->array[n++] =3D timestamp; + if (sample_type & PERF_SAMPLE_ID && check_bounds(n)) + data->array[n++] =3D sample_id; + if (sample_type & PERF_SAMPLE_CPU && check_bounds(n)) + data->array[n++] =3D 0; + 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_tmp_p, *stack_p; + int zero =3D 0, len =3D 0; =20 ts =3D bpf_ktime_get_ns(); =20 @@ -197,6 +317,22 @@ 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 + /* temporary stack data */ + stack_tmp_p =3D bpf_map_lookup_elem(&stack_tmp, &zero); + if (stack_tmp_p) + len =3D bpf_get_stack(ctx, stack_tmp_p->array, MAX_STACKS * sizeof(u64), + BPF_F_USER_STACK) / sizeof(u64); + + /* save stacks if collectable */ + if (len > 0) { + stack_p =3D bpf_task_storage_get(&stack_cache, prev, NULL, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (stack_p) { + for (int i =3D 0; i < len && i < MAX_STACKS; ++i) + stack_p->array[i] =3D stack_tmp_p->array[i]; + } + } + pelem =3D bpf_task_storage_get(&tstamp, prev, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE); if (!pelem) @@ -226,6 +362,13 @@ 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; } --=20 2.45.2 From nobody Fri Oct 18 08:37:45 2024 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (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 6359417A585; Fri, 26 Jul 2024 10:28:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989725; cv=none; b=GgyGoYqegNx916gczFVkfhofVD1tCgCEZFSvUDpjI0u517A4JobDTa3rF625AFo+1oAlgXNWhVL/KP1MJ1Nq3Bu87rtmXtm4d4Ba+/dC6b3hzWIEtjjvXyVeoiiefsjDtlnQVH4TkwL/lo2leG8ylVfz7o59X0slIaUeR/uamJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989725; c=relaxed/simple; bh=V16kSHPoUbY1EjjYptU8X0Q5N5N92bR1n6mtRXw/9Uc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=srlVpFso8TV/hTtb0zObemZ+SelOG4nePwPcuLG84zFuVgPqhkq6FQlplBlgr5v7h1cbfjt8YLUyMLtbfvn0yBuqs9ZlpjqUDwI/GY59RN/0pZDrwjj0OxiZkAMnvvBSTvqRLRzJL+KFDW3cltZPgrWrQTKffTZRnE+AWFFHV6I= 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=TaNuEknm; arc=none smtp.client-ip=209.85.210.48 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="TaNuEknm" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-703775e4d5dso455142a34.0; Fri, 26 Jul 2024 03:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721989722; x=1722594522; 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=Lk7eq/ExJtfJWXdlo7dQbuLEe/sXbvpJ766py9Gb44o=; b=TaNuEknmuAxWXi35VX3rp36Px8b6N43+/vSSK1YrwjFSfXYo8SrigluWVs13qnAo7e c+6JGcATppsXvCiy+33w5OJNmpupH1htJoa7Q+1OjRxLC+A5296FnNV0EOYaFpMp5j0U RM2ogxbWa84q0m0218hQq6VukaI35nCPYzwDTDlYLwJDnAHkAvaEZATnMjRWwICYiKvN 9LvO8Fyn7o2X2KmVcJdGOJW1XWh2GZT6Pf37TxAvqzAKrD/7fYUJzdcu9oixarFN0d7e nDkFWv3RsHr8nR4AAdVzviwNV8esZcodtN2ailyWeSNqs/TRl4TeVXMm0Z/AqU1tRIKU 3UDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721989722; x=1722594522; 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=Lk7eq/ExJtfJWXdlo7dQbuLEe/sXbvpJ766py9Gb44o=; b=ADonSoAkjV80hQ9Uvteh6uAcOrRsfRQpg70+CweinBxIW2kJaZyTqsDd/i81YwuwgH cHmUGeHD3hj8Q6uuC8xOm9RfkmRpCrTSG9+UY0by2uca5pt9NKnO1NXiKZgfjCoT0ldO fDjI2CAFRhsfti+jsnycIL7qIzTV1jlBq7oXXT61Yfrl8HNLKWsL31/zEQXfGx2Yf//W jmJAy5xKq8F6YOC1iwbSJpldffdoJspuF6TQkHp8N/fsUfBuES6KwkTScj0675POLsnJ NmtOLyKNawFApStrLVyjKAaPj8cn28pHVORIzcRGyZzLr7/Hj2zZxaHgA0A9oZ2uS2cw HQ7w== X-Forwarded-Encrypted: i=1; AJvYcCXDBSY8Sl612GzyM7VXGCmYjqgu4FPuJjAxKw9DrEgREYRBQVcFZVcSV7XHMsRi0C0ETbV1V1VnK2C3zC1alABaz/ttIv9vTQll0xdkbhozCxrJANsA5b2u2pWS5ZvnTYYx5cH3mn0l2fIHtE659A== X-Gm-Message-State: AOJu0YxfOnY1+qNMg9shl2XMrgRJ1AM8AhWTfNIXjNh3SaBQTEnwXl+f Ob/fDs7kBWN7xnR2sVbsxt0oAPKrkde2CKFs6aIOBfYO5qB0K9Mu X-Google-Smtp-Source: AGHT+IGgnyl97oeF2mpwkdujl8dUOvXmpcrgGtiP6kM8W2xqMfDuy6/w2TqmrW2XUYO95oGWmpjfww== X-Received: by 2002:a05:6830:6502:b0:704:47db:cd68 with SMTP id 46e09a7af769-7093210c4eamr5589125a34.4.1721989722230; Fri, 26 Jul 2024 03:28:42 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.244]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7a9f816da89sm2179964a12.20.2024.07.26.03.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 03:28:41 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/5] perf record off-cpu: processing of embedded sample Date: Fri, 26 Jul 2024 18:28:24 +0800 Message-ID: <20240726102826.787004-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726102826.787004-1-howardchu95@gmail.com> References: <20240726102826.787004-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" In the first evsel__parse_sample, we parse bpf output as raw samples. After getting the raw_data, parse it the second time to get the embedded samples. Also, because we rely on evlist to dump direct off-cpu samples, if we evsel__open() a bpf output event on a specific pid, it will fail. So pid = =3D -1 for every bpf output event. Signed-off-by: Howard Chu Suggested-by: Ian Rogers Suggested-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-script.c | 2 +- tools/perf/util/evsel.c | 16 ++++++++++++++-- tools/perf/util/evsel.h | 13 +++++++++++++ tools/perf/util/session.c | 23 ++++++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index c16224b1fef3..c142fb44817f 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -653,7 +653,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__embed_has_callchain(evsel)) { use_callchain =3D true; break; } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index bc603193c477..6c64ec475b80 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -298,6 +298,7 @@ void evsel__init(struct evsel *evsel, evsel->pmu_name =3D NULL; evsel->group_pmu_name =3D NULL; evsel->skippable =3D false; + evsel->sample_type_embed =3D 0; } =20 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) @@ -440,6 +441,7 @@ struct evsel *evsel__clone(struct evsel *orig) evsel->weak_group =3D orig->weak_group; evsel->use_config_name =3D orig->use_config_name; evsel->pmu =3D orig->pmu; + evsel->sample_type_embed =3D orig->sample_type_embed; =20 if (evsel__copy_config_terms(evsel, orig) < 0) goto out_err; @@ -2282,6 +2284,10 @@ static int evsel__open_cpu(struct evsel *evsel, stru= ct perf_cpu_map *cpus, =20 test_attr__ready(); =20 + /* BPF output event can only be system-wide, off-cpu filters tasks in B= PF */ + if (evsel__is_bpf_output(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); @@ -2592,6 +2598,14 @@ int evsel__parse_sample(struct evsel *evsel, union p= erf_event *event, */ union u64_swap u; =20 + array =3D event->sample.array; + + /* use raw_data passed in to read embedded data */ + if (evsel__has_embed(evsel) && evsel__is_bpf_output(evsel) && data->raw_d= ata) { + array =3D data->raw_data; + type =3D evsel->sample_type_embed; + } + 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; @@ -2607,8 +2621,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 diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 80b5f6dd868e..0d25e82c6154 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -184,6 +184,9 @@ struct evsel { }; /* Is the tool's fd for /proc/pid/stat or /proc/stat. */ bool pid_stat; + + /* for samples embedded in BPF output */ + __u64 sample_type_embed; }; =20 struct perf_missing_features { @@ -469,6 +472,11 @@ static inline bool evsel__is_bpf_output(struct evsel *= evsel) return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT); } =20 +static inline bool evsel__has_embed(struct evsel *evsel) +{ + return evsel->sample_type_embed !=3D 0; +} + static inline bool evsel__is_clock(const struct evsel *evsel) { return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || @@ -525,6 +533,11 @@ static inline bool evsel__has_callchain(const struct e= vsel *evsel) evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN; } =20 +static inline bool evsel__embed_has_callchain(const struct evsel *evsel) +{ + return evsel->sample_type_embed & PERF_SAMPLE_CALLCHAIN; +} + static inline bool evsel__has_br_stack(const struct evsel *evsel) { /* diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 5596bed1b8c8..d87d85a3e21e 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1528,6 +1528,23 @@ static int evlist__deliver_sample(struct evlist *evl= ist, struct perf_tool *tool, /* We know evsel !=3D NULL. */ u64 sample_type =3D evsel->core.attr.sample_type; u64 read_format =3D evsel->core.attr.read_format; + struct perf_sample sample_embed; + + if (evsel__is_bpf_output(evsel) && evsel__has_embed(evsel) && + sample->raw_data && sample->raw_size - sizeof(__u32) > sizeof(struct = perf_event_header)) { + int err; + + sample_embed.raw_data =3D sample->raw_data; + + err =3D evsel__parse_sample(evsel, event, &sample_embed); + if (err) { + pr_err("Can't parse BPF-embedded sample, err =3D %d\n", err); + return err; + } + + sample_type =3D evsel->sample_type_embed; + sample =3D &sample_embed; + } =20 /* Standard sample delivery. */ if (!(sample_type & PERF_SAMPLE_READ)) @@ -1639,8 +1656,12 @@ static int perf_session__deliver_event(struct perf_s= ession *session, const char *file_path) { struct perf_sample sample; - int ret =3D evlist__parse_sample(session->evlist, event, &sample); + int ret; + + /* set to NULL so we don't accidentally parse BPF-embedded sample */ + sample.raw_data =3D NULL; =20 + ret =3D evlist__parse_sample(session->evlist, event, &sample); if (ret) { pr_err("Can't parse sample, err =3D %d\n", ret); return ret; --=20 2.45.2 From nobody Fri Oct 18 08:37:45 2024 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.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 D25E517A59B; Fri, 26 Jul 2024 10:28:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989727; cv=none; b=QyrYA7MPN0dwBvHH2LGzTEBs3aHVxjVh0PXiZ3YpqPn///IdAl0RpHXD55gm49JFAAFQVxSOkdvnZ80p1kepBf15AZPfCPtb1fAr/wtHmQZmUERJz/hrHgevqcFfhHIgBxRlZQr+s0SzHEQUdpHYwxzYJMN4tLXea8PmTT1gEmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989727; c=relaxed/simple; bh=Ia3MRLUWTQlrHm0z5p4CuK/VDQhtQu6Xk/f4G1FPiAg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lK4qtV0chGCozVcIfoKwdVeypbk3SedI+Bo0+ETgLUDTp5/MoRZ/C+VjfNcnGs2b8fjwYNrRgVVhMonZ1QyOWYKtvD1feAkLZJzHq5QZ5pjdxok6aRW9LNS6cFUoUQ4Us0idArhF5uIujRiYpsXzjOaFPE15YpaojkVFtAnHm0M= 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=ItL07xh+; arc=none smtp.client-ip=209.85.167.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="ItL07xh+" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3db157cb959so515570b6e.0; Fri, 26 Jul 2024 03:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721989725; x=1722594525; 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=73+g1pIsHWTk4o1x0pXyZyWF5R08Ka6OMNY6SL6d0EE=; b=ItL07xh+br1uDaxQ/3Ws9ZfCs3mjUCSvMA38rQr3sne/eeBw9Ay4lQ2dyDE9OkHkPb AU9wqCDm07cz0MOqT9eNsNhkWFCwFN+OIEW5oIcjudcImhMcSxKLjxp7oSQO7fnQIv2m T3bwMhV91rATZLjot4TRzQRElXoDRrI6j9mxa0q2DQ0sgoyv9q0/MIcg1eQiKmH42O6r +D4oKXMoZNlPtwUdEKZ9rsTFJyj6mf9LS/bInXa/MqQpwOG8TIQgi0/KEsJq2S0sS/0k /Wk6OvnczMDd8y8mlXcxHMLdR1UOFQoihahs0gga9HrNFkwPGRRu6OLOP6M8AOmIodw6 +6kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721989725; x=1722594525; 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=73+g1pIsHWTk4o1x0pXyZyWF5R08Ka6OMNY6SL6d0EE=; b=YyjHvWxnf7sOBf4VE09oiBkZdoChlwQuvgGBgpSneNG2Cu12WoE8amk/Gxfzc6S7cB rIwXo2q92+gbkihttxQHwEdl17x5cc1EMuWERAZ+JQtkVf9HwmBSCifWaxAGXcoXX3b1 b8i1w39lksgvbOnZpIp5Rd6ccZWf0ctCP17Alqrb7+t2AVlh92J4B1OSV1ELRWcRlQ+0 6hO22Z4bJHO39K28hq/+d1avoFFJaJhmkDMqTIGBe3zryc30yDU587dNw+m9Vbgnnc1P Mly3WFGbvmdNni5q74ptBdol6NbGgP+TPEsYoE3AGGDJmAQGEWt6XUyVlC/8jQQXPi8+ PmGw== X-Forwarded-Encrypted: i=1; AJvYcCW3zQ8KKw+pVz6h6E727yEvIvgh7TAdNuKHrW6EGkY3e4yFwXuvd15AEW0l7TCkD1eQ3wiR5AKx88a2C8K1T2k5qKH4dBlh3oX1K6k74lr4EOt7zioPEFg1cofPntWtiKG4PTg2n4dSbols76akrQ== X-Gm-Message-State: AOJu0YyIyXRkgxcBFgQkDZn8KaKW3DIbrjdxwOvt/exzajEUd858XL2J XlXZvUbW//MDOgr36sqC+cYQAxeA1cud5fziDpmd2ZqFAK3sD+NT X-Google-Smtp-Source: AGHT+IGTwemDc2YGSkFCfBD/QacogrTzioFStiaPEmk2dbdM36TZrs8Ud73bvEhsJIqEssfjit7aag== X-Received: by 2002:a05:6808:1414:b0:3d9:38c4:e9be with SMTP id 5614622812f47-3db1419db0fmr5467060b6e.44.1721989724761; Fri, 26 Jul 2024 03:28:44 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.244]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7a9f816da89sm2179964a12.20.2024.07.26.03.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 03:28:44 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/5] perf record off-cpu: save embedded sample type Date: Fri, 26 Jul 2024 18:28:25 +0800 Message-ID: <20240726102826.787004-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726102826.787004-1-howardchu95@gmail.com> References: <20240726102826.787004-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" We have to save the embedded sample type for it to be consumed correctly by perf script or perf report. This is a bad approach because it most definitely will break some perf.data convertor. Another approach is to add this sample_type_embed to perf_event_attr, but changing perf api is above my pay grade, so please give me your suggestions! Signed-off-by: Howard Chu --- tools/perf/util/header.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 55e9553861d0..ccb5493dc515 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -80,6 +80,7 @@ const char perf_version_string[] =3D PERF_VERSION; =20 struct perf_file_attr { struct perf_event_attr attr; + __u64 embed; struct perf_file_section ids; }; =20 @@ -3713,6 +3714,7 @@ static int perf_session__do_write_header(struct perf_= session *session, } f_attr =3D (struct perf_file_attr){ .attr =3D evsel->core.attr, + .embed =3D evsel->sample_type_embed, .ids =3D { .offset =3D evsel->id_offset, .size =3D evsel->core.ids * sizeof(u64), @@ -4147,6 +4149,14 @@ static int read_attr(int fd, struct perf_header *ph, =20 ret =3D readn(fd, ptr, left); } + + ret =3D readn(fd, &f_attr->embed, sizeof(f_attr->embed)); + if (ret <=3D 0) { + pr_debug("cannot read %d bytes of embedded sample type\n", + PERF_ATTR_SIZE_VER0); + return -1; + } + /* read perf_file_section, ids are read in caller */ ret =3D readn(fd, &f_attr->ids, sizeof(f_attr->ids)); =20 @@ -4272,6 +4282,8 @@ int perf_session__read_header(struct perf_session *se= ssion, int repipe_fd) tmp =3D lseek(fd, 0, SEEK_CUR); evsel =3D evsel__new(&f_attr.attr); =20 + evsel->sample_type_embed =3D f_attr.embed; + if (evsel =3D=3D NULL) goto out_delete_evlist; =20 --=20 2.45.2 From nobody Fri Oct 18 08:37:45 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 0FA1017A5B0; Fri, 26 Jul 2024 10:28:47 +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=1721989729; cv=none; b=HNh52gYmO2jAKm3VpmCz003pu/xIDKUoJjuLVx3hgkfken2jQsTMAip16kAmiLEZHpddcpw85d/Ort9b7WdxOqG+jRRT85B9KvSytTDgKXZhFmBkCcrTcrWH/fr+kmmhDY5XOp69HnWbk4F0MNxFYebp8LSqUcE8Zw68iln0gYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721989729; c=relaxed/simple; bh=MAniNW6exOIfhgyVeDMNsX7sKp9KMJrR8w2MhqpQl8w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nS/Joexxn+aoKUhu8ToxqhR2QP+/qzw/gBjQJfhGSc6tdwCEHFNuIYPlErdtE/AT2APMpEz3Rt4KPtTDcSYeQmaCYB8fcgYBGOO5kFlvTMrKJr97Aa5hYS0S+v9X5yidWtsIZZ3yx7Zrg9tvbLKBX517yIjACMzgcqywbvTpTbU= 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=JisWuyFk; 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="JisWuyFk" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70d162eef54so618503b3a.3; Fri, 26 Jul 2024 03:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721989727; x=1722594527; 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=UAFOybRrHVVIIzrVmtJN/cXHf1xuv3xo8wBTAcQABMQ=; b=JisWuyFkTYnR9sjz1lIyfoi1eKB/WhKV6M3lijRHYs00gQNqwyh3/BeSLx67lg6klV sa4Ie4R+VliXozuA8eKyzmzSzGhJwtm8l0/ez/iWHKyccChTL6UYoduiD8dlFdn0kQQ2 sIbSxLCiX6OFXUtyA50kbPbroTLJxyGVy7S96CfZqR5YU8Zkwd8+HRSC7VuSAPEgNLm/ qMKd/6QqxVRMMAqr6aLSfxB85HdkGMcJtO0uAGHV/8Cjh/Y3gkiSLFhlPfmFDo5azzIR mudrQhtx3FhWT0iyVUyUsyNcfq47Oi7jdbszn3idgDKxbMz6mhukNa+f9ijtN8V8rzKA NNLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721989727; x=1722594527; 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=UAFOybRrHVVIIzrVmtJN/cXHf1xuv3xo8wBTAcQABMQ=; b=H86ukazBZuy8liKIeU8FUpVNvKt2vbdJMqOMmtjXBKuoP/1ocUQv61b6hx/LWjXe26 idOIqwqWa7RfCwYtREPF4oOLJGHK6MtbPFLzCuMWJWqAaSN2k+xeEd5BXnDJ+7bDxymH dfJJIkon+NCmfJT+8N7Yc4BKOlZwg8nyuT4/A2aP4WnZgbqUI+xz1d+b0BRSZSYyUITn 9HXbmhrkGEyfQtnPhsOA/eY/5IILuJP7EmVSjJJ+GcGdjx/VilB0D6SEBcLqlQxNPM+3 5T2SoruayXkET1EGBoZMkvnlcsoT1qpSG96HZjhwPEZGn407i1cYuvlzzAcNn48Q+5rJ S8mA== X-Forwarded-Encrypted: i=1; AJvYcCXkmIM2DwN8/QzpIrExby6iYnB+ovsvZ6OgZQWH/gC7Ys9TEL/UNPXTawxkxZooVFfyAy8cTj3k/42YH5Uzfx8PqvNhdlt6J4pIAcDAZn1wqv4ML4tWPtUs9HMv2Av/n1gp34cHumaq2vasg2B3Ng== X-Gm-Message-State: AOJu0YzKLnEg0HYXuHTbnm5/RWJWavgbEyhnxGyEq3/ZaoZj4MzaX/ZR vllzoApiBPeXikQ2AS1PCSLCazUthJLcwG8Rhaeba3YKeJYh5KuS X-Google-Smtp-Source: AGHT+IHXRSXS/mOd2+p5p5/ObMsJf4TTNl9biEclyY5h3umOpra/yhAdPpKRMwz8xRhMFyC4WjZW0w== X-Received: by 2002:a05:6a20:9f8d:b0:1c2:905c:dc2 with SMTP id adf61e73a8af0-1c47283dba2mr6842842637.15.1721989727211; Fri, 26 Jul 2024 03:28:47 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.244]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7a9f816da89sm2179964a12.20.2024.07.26.03.28.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 03:28:47 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 5/5] perf record off-cpu: Add direct off-cpu test Date: Fri, 26 Jul 2024 18:28:26 +0800 Message-ID: <20240726102826.787004-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726102826.787004-1-howardchu95@gmail.com> References: <20240726102826.787004-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. Signed-off-by: Howard Chu Suggested-by: Ian Rogers --- 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 c3d84b67ca8e..5062058ad17d 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -152,6 +152,7 @@ static struct test_workload *workloads[] =3D { &workload__sqrtloop, &workload__brstack, &workload__datasym, + &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..1ea0a44336e2 100755 --- a/tools/perf/tests/shell/record_offcpu.sh +++ b/tools/perf/tests/shell/record_offcpu.sh @@ -6,6 +6,7 @@ set -e =20 err=3D0 perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) +TEST_PROGRAM=3D"perf test -w offcpu" =20 cleanup() { rm -f ${perfdata} @@ -88,6 +89,30 @@ test_offcpu_child() { echo "Child task off-cpu test [Success]" } =20 +test_offcpu_direct() { + echo "Direct off-cpu test" + # dump off-cpu samples for tasks blocked for more than 1999ms (1.9s) + # -D for initial delay, which is necessary if we want to enable evlist + if ! perf record -F 1 -D 999 --off-cpu --off-cpu-thresh 1999 -o ${perfda= ta} ${TEST_PROGRAM} 2> /dev/null + then + echo "Direct off-cpu test [Failed record]" + err=3D1 + return + fi + if ! perf evlist -i ${perfdata} | grep -q "offcpu-time-direct" + then + echo "Direct off-cpu test [Failed no event]" + err=3D1 + return + fi + if ! perf script -i ${perfdata} | grep -q -E ".*2[0-9]{9}[ ]*offcpu-time= -direct" # 2 seconds (2,000,000,000) + then + echo "Direct off-cpu test [Failed missing output]" + 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 3aa7701ee0e9..84ab15683269 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -205,6 +205,7 @@ DECLARE_WORKLOAD(leafloop); DECLARE_WORKLOAD(sqrtloop); DECLARE_WORKLOAD(brstack); DECLARE_WORKLOAD(datasym); +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 48bf0d3b0f3d..f37e9be8b142 100644 --- a/tools/perf/tests/workloads/Build +++ b/tools/perf/tests/workloads/Build @@ -6,6 +6,7 @@ perf-test-y +=3D leafloop.o perf-test-y +=3D sqrtloop.o perf-test-y +=3D brstack.o perf-test-y +=3D datasym.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..02be3d05b06d --- /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 pass initial delay */ + sleep(1); + + /* what we want to collect as a direct sample */ + sleep(2); + + return 0; +} + +DEFINE_WORKLOAD(offcpu); --=20 2.45.2