From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D06C31D8A14; Thu, 12 Dec 2024 23:27:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046045; cv=none; b=py8AxHhZf6gPdB7YG0wWf6XefTLG6DodAxaaT045IhAf7f0WdsbpSiQ4qN9Sa/Io1921qhh1C+z0VmUNvOI0RBuCS3bOOy8T68tJCh5onHOM4XTVVJwpawuGxacfzFxckyypScV46TGMmE0ELAMDohoMipR4oVI2zh/WzNwBO1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046045; c=relaxed/simple; bh=5zwcrkCXWGsnxL/5mROAo6TlhYsXoTqe91xEFV/fnQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D+Yp09c82EvV6RgyFHB3fL5S0Cl+1vNweHQC08zf4dwBUNtNvzCxMNz1VE9JrFy/+CvyYpUuQGNr6RdbOFn+OKPYmIeAO8Zh4jBDMYN/Llvj3/FDv34eu45tUQJDG45Ih2zCUAhgo/GTGpdPJlDPTIuClHMi5CINFzUY7u8WXPQ= 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=ZM4JxHEG; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZM4JxHEG" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-725ee27e905so1491149b3a.2; Thu, 12 Dec 2024 15:27:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046043; x=1734650843; 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=wABlX+oSl9ibR4U3vdXkWwVEy/La7xWsUaZ6DFvspKw=; b=ZM4JxHEGlYZQJWmIaDl3gR99BMZeQ94u6wLyMp5Dwy1lW69uHX9OJvqA9zTtbASuKS vbf5ef9GVuuCUirk9sFEXml6/sES6xXV4IbPY79k0/d7zZ06V/CFLS0cl7iVW3sCEVLw 2EQLbQy4a6evg5mzW39GwHnC/euTN2rjeK69hADZz90EeWhwC1oKYpdNxqvaLQyXJN6p LgDyWQ1XlkwkbmkK5ZVhdgeXv9Pa/PLxTGxZg1P7niVYjeGwQkMKEAWUCCZjI7Kzv934 3b8jF6WSheJCQauR2+wCtwGpEJVYWsZmepZuW2KLEqbouC+DRC+zo3ahfH45fNF8m4vK kLkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046043; x=1734650843; 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=wABlX+oSl9ibR4U3vdXkWwVEy/La7xWsUaZ6DFvspKw=; b=B5CyFZ5d8M5Be4Ac55W9w0loUFpUGF8imo+At8mw0Xv0FdfN0tENOt8Jq0rcBsIYG4 Ktj/Qa1scsHw2DnxlOkCrYkxFXKaV8zVXPnwR4Ei8kCivhEaBQPTgdazg7UnZzoCw8Nd akpQb0s/Wzglq4IkGcRVtbEMPu5XBUhUwVtuokEL3Q3+W6+ZKDNz29IA33lGyzaixL8t Bk2rvEGQDQmFfpwuP7lm0gQMlDVC5E6i1DUSPHSPtxNsUZP6Wz8nl6KGKw9rbhHbAj15 82WMS/CuG5pFt3lVVaRMSeBbklgfz/z/Ro4qjDs5xmC+OvbA5eQvIf7bNUmZkBF2Y66D fg8A== X-Forwarded-Encrypted: i=1; AJvYcCV9oGVxR+Vf3uhGhfJLrIEHTRjUklc6xBgb/wgr9vM8sLwWEpOzge05fluAQeEz4aQsuIE8KGgw3Iyjxfc=@vger.kernel.org, AJvYcCXbyD/NRfWQdRGEVapCoSBaBrOb/xlL4wywlvKJqnjUr16WSKpsLbeA9ESXoG1+v4ltSTVYtxYg5f/z2wtE8H4BTw==@vger.kernel.org X-Gm-Message-State: AOJu0Yxz/8JYzXSNW94bs+CHJop5uHWMwSgzpCFAI77dyp62E4potcj9 WrYTprpp/317jtDFGr+YX5NYZ05bMYNUmX97T+jo6pI80jsBwP2MKzuO2nuc X-Gm-Gg: ASbGncuMuc2QxYH6r49dzOPnorI+m2GmfwvaDkA7E6J+JyB8eQlDIMyu5hE6cPU7T8b eDhUJHBQuc3GwYqry9ct/F4BixPWyXhmS7QKBDcmrldmKy6xJhIENa3Tza9b83usJ/Wa7YIpcYY k9Txu4e9GCTWG9Le6IP+Ns2kkPAnGrgPVrPsdCJYhAtK4MRfSz8pmE5Z0Uec4/TT4Xo8cQyg4yw Qi+mu8ZTKuKWHRz1tIxj5w5fLio36ZsT4yWK+A7zmK1H0PDFWaRW25N5+PliBa5vUd9a1I2aVY/ qF+eP5IyOt5I X-Google-Smtp-Source: AGHT+IFomYMUsTk5vraP3j6BMcwABo2ugwSt1BaqR/pLFGLm4HLoPFIf9AP3Zf8BvwgpRoVwmuFzPA== X-Received: by 2002:a05:6a20:9f05:b0:1e1:b1e4:e750 with SMTP id adf61e73a8af0-1e1dfd543bcmr760042637.18.1734046043121; Thu, 12 Dec 2024 15:27:23 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:22 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 01/10] perf evsel: Expose evsel__is_offcpu_event() for future use Date: Thu, 12 Dec 2024 15:27:08 -0800 Message-ID: <20241212232717.232507-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Expose evsel__is_offcpu_event() so it can be used in off_cpu_config(), evsel__parse_sample(), and perf script. Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-3-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/evsel.c | 2 +- tools/perf/util/evsel.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 6cffba96477e..867da727a486 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1242,7 +1242,7 @@ static void evsel__set_default_freq_period(struct rec= ord_opts *opts, } } =20 -static bool evsel__is_offcpu_event(struct evsel *evsel) +bool evsel__is_offcpu_event(struct evsel *evsel) { return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT); } diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 93b6244ec302..f5004f92f0c8 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -553,4 +553,6 @@ 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 +bool evsel__is_offcpu_event(struct evsel *evsel); + #endif /* __PERF_EVSEL_H */ --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 892AF1DED53; Thu, 12 Dec 2024 23:27:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046047; cv=none; b=BX78IxQvoF+SMdBRViAS65ryXauqhTbZes2xS1QsralWSsDwJ2FCmn7FumK21zMkVj/liz4dZ+4E3kqUnzCstvqdQMf2hZ3iH5aO9y6TQRbPVG6fZzURuCAeo4fWF9Jg7a/hzbik7MKX/hBts97c+L6wTbLm9Ijmj2pLab+j+6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046047; c=relaxed/simple; bh=CtXvBuHZ10l+NOVrLXSjeix2nXsWB6v1qq32icek0/4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b/Prb11bouEvxL7x2pCTrN33bWQgCNglpstSywkKL8HSEoN7j5VmovyD2GXcw4TdJt2G1UP3fIRUSJDwQP2Z2h2DB7pCRUBh8GZuv/zJSanjZTQhZWjCThXHXMrRB0UyCf+6O9ZU7ffxlJ2mRhe7dd7VFpZseX1jPeaHSK2w9jU= 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=VkhEVZfa; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VkhEVZfa" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-725d9f57d90so853859b3a.1; Thu, 12 Dec 2024 15:27:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046045; x=1734650845; 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=MZXNpS9ontjCFvt+AYY61fDK03PJL203Z521VxrjSJ4=; b=VkhEVZfavJuBIfLbpiVduJvF2vnPXE+9bskgJyhY0o2DQ8XKWlTQqd3L+bj4mdCLcH /ElAKYhKuLy8QTSc9BwlIYxhJic0YmEVqps6SjkHfR/j7BiPfkUq1uTS/B6gfBK+nwFH 8biaMqCkHea9AbkX+65z3dsH6wC9ZHoUEy3/jbfXmb+CNFmE3AfVRCT9nLZ/cC159/3k k4jwI/5JtCzLz5gOWbEL4raOYGjjhDi6wngQB/bsYkDqtZZr+RaSNuQpTM+gsAZFNyfB khWiZLFQ8l0P7upcI/WdNNKFFuHLH0r0cHfelJv8en5TOsB6sB/j4sjTARzJl4TdJXbP 38LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046045; x=1734650845; 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=MZXNpS9ontjCFvt+AYY61fDK03PJL203Z521VxrjSJ4=; b=ZvoNsGtkQtLsvz0aVlu7lkvyYUPEvMkFensd6pOSkdnNbBJLVnP0c3kjaGIe/7cBt0 xmkOTI7JR2gVgUvR289VMJuF/3gv3r2ZRevW03zmEG5SAjir2UQYdNxB0eDcZkjRoDii a3oIm9e9s6eCttofwni11v+iyXvRtzvjPP6cqXLN/QBQkU9zgfe1pU0KRRP7T1uPtTqH NmbgLjMmjgh7u6b0WydlCwgyzzZkh+7BJIRcdboDk63Zz3RAIs0l0czhfJIoP0krbc5E fAbKg1RJXrqFFGNFhKX5rdaIsdq6h+E86jSJOi5+4t4r48Ik6hl0wYpCmWJ/2dMdcwaF k2rQ== X-Forwarded-Encrypted: i=1; AJvYcCVWkTIRbYhp04rKQbhvwoKRKa/yKJOuzP6XSDKTxw9X0kggnOyVsjECgbQgK9YoXm9XSpAHCPiNVIQIrtV6kDxQVQ==@vger.kernel.org, AJvYcCWoiGHtEFwIwyHE9u31iL5A+2DwUA25iiy98Of9QKf6uDZoy0IFZN6iWNpgWvubQ75M1pP5k8Lx3GRN4XE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxf1pqU3wXsgfJoeDW/HtZRcms/xE1XxsIZx5WLsQsBed1L7Ddo UVaqSl/JIAj27OaxCyRgugNvD1xE/I1IbyzB3C4DrfJIdxxSWwVI X-Gm-Gg: ASbGnctadEOZJxoAAXSVSIJDtn8nmGRDGyEYcYp5pyFRw42vIEAMIHNyr+TeCMdnaWN waYLHBYqNJeF0FHa+v4YS6VMIc6mmxFyuEH9NDAtozQB9RtHqUP5M+OXYhRcuEAsPDm8Wv+B3Cd Ft5NXhaYic39S1OCyKy2hbpe7WYotje6axZ7E0oUOx5gxTLJu78KKViUsWIytEsbHJ3Ul7oSOZ6 t4royaG7I/PE5/HVti5xPUTMXxSriD5qaO4muyZzgL4wTN7cWyoS900HkR5UixV7Bs1Inne1ynB zDPIHr/wTavw X-Google-Smtp-Source: AGHT+IEU2I8adpOKcVVGkrTZnoXF7BDglGV3uOfQOUePhv0fbA98Hbb4/gUZiLfYBdKukmBAnj2Tkw== X-Received: by 2002:a05:6a20:6a11:b0:1e1:bf3d:a190 with SMTP id adf61e73a8af0-1e1dfe6a59fmr610748637.30.1734046044697; Thu, 12 Dec 2024 15:27:24 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:24 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 02/10] perf record --off-cpu: Parse off-cpu event Date: Thu, 12 Dec 2024 15:27:09 -0800 Message-ID: <20241212232717.232507-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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 the off-cpu event using parse_event(), as bpf-output. no-inherit should be set to 1, here's the reason: We update the BPF perf_event map for direct off-cpu sample dumping (in following patches), it executes as follows: bpf_map_update_value() bpf_fd_array_map_update_elem() perf_event_fd_array_get_ptr() perf_event_read_local() In perf_event_read_local(), there is: int perf_event_read_local(struct perf_event *event, u64 *value, u64 *enabled, u64 *running) { ... /* * It must not be an event with inherit set, we cannot read * all child counters from atomic context. */ if (event->attr.inherit) { ret =3D -EOPNOTSUPP; goto out; } Which means no-inherit has to be true for updating the BPF perf_event map. Moreover, for bpf-output events, we primarily want a system-wide event instead of a per-task event. The reason is that in BPF's bpf_perf_event_output(), BPF uses the CPU index to retrieve the perf_event file descriptor it outputs to. Making a bpf-output event system-wide naturally satisfies this requirement by mapping CPU appropriately. Suggested-by: Namhyung Kim Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-4-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf_off_cpu.c | 33 +++++++++++---------------------- tools/perf/util/evsel.c | 4 +++- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index a590a8ac1f9d..9275b022b2ea 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -38,32 +38,21 @@ union off_cpu_data { =20 static int off_cpu_config(struct evlist *evlist) { + char off_cpu_event[64]; 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; =20 - evsel =3D evsel__new(&attr); - if (!evsel) { - free(evname); - return -ENOMEM; + scnprintf(off_cpu_event, sizeof(off_cpu_event), "bpf-output/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; + evlist__for_each_entry(evlist, evsel) { + if (evsel__is_offcpu_event(evsel)) { + evsel->core.system_wide =3D true; + break; + } + } =20 return 0; } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 867da727a486..d0f2d9e8ce6c 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1521,8 +1521,10 @@ void evsel__config(struct evsel *evsel, struct recor= d_opts *opts, if (evsel__is_dummy_event(evsel)) evsel__reset_sample_bit(evsel, BRANCH_STACK); =20 - if (evsel__is_offcpu_event(evsel)) + if (evsel__is_offcpu_event(evsel)) { evsel->core.attr.sample_type &=3D OFFCPU_SAMPLE_TYPES; + attr->inherit =3D 0; + } =20 arch__post_evsel_config(evsel, attr); } --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0778D1DED7C; Thu, 12 Dec 2024 23:27:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046048; cv=none; b=JASzeqtoXUahcWn0N7iAQxbRilAEQfvDMmm4yI1HhrngwYpMf6ieEhdbR09yjkUI7nPVrpdTJFWZZ6HegYBOhRULdv4+eSPv3HlX2quI/Q6QXczVTR/HYlqs9dyQiXH24o8emeRPARSiwzfLxLVkkjtBOp1XMd0kqcLT3h0ABqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046048; c=relaxed/simple; bh=aSoKRjTAYvF6lwpOwLrBmfF/+YBy6tjV5MtsYW5Lufc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bqxrl0pDzWdqdluBefEqrbYrr0ZRq9wzT/KevbjCxsP8FeAMbZmgy/Gz+JRbCLtS8f95U2u4cLqhzJqS9f63npjveexuAAcHP9Y9wFvUCQAJXjyZdVqSpQfxQjYdXexR2Np8AY5WR2Y/yjx97e74OrG9H6wS/79LzFxJ8Sme3+Y= 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=g4sO0IWX; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g4sO0IWX" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-725f3594965so910279b3a.3; Thu, 12 Dec 2024 15:27:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046046; x=1734650846; 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=q63QEvX+BQjt0MQ+JeXke1lOVFwn9vKhYJT1IdWoMcM=; b=g4sO0IWXoruQ9tY5xyfs6ZckOtLswdfzORHTcccPZ1Yg/ZcRBQWxYGzeKDC75dxN2c QtaLDyQQYhiedxfM8Piy20m5lLWET1BgP+RoBgV28YTcdczA4KofFT89aZQEsjfOl52c xWFlfExImbdaLw69atATVFvXMtRfBdcQ+I2XFfA+I99wQFPO9YJFUiskQzfae8YKDk2J X6BNs1JVflgts347VVt2ruxpDwjz7a9NTySkeh5jKKzvFcZPurPZOmWYyLUH45TrhUXe C7oAiHyFPp5FDMX3m9jsZe2e5VzefwYMOLx/KYowVVqwljQ6rlNxINQq4EgnNBHJGbsc Fbgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046046; x=1734650846; 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=q63QEvX+BQjt0MQ+JeXke1lOVFwn9vKhYJT1IdWoMcM=; b=sDk5zSdLSNDIIt6ODq255UK9DoVyvp1RoANwn6IxaTcQbl1MNW3RgmspIxpEtoVM5J T+Er8hiv3GH1tYeGi2FyVL3xE36yHsMJ3gj7/Kd1dEJDklPlVlCT6lFaCcWubOWLxWMc yfx4ZMUQMEJk/R1rmZrintx2mzYSZD8Ce1Rr9Zu9+yfhuW3mmlNPzeIYdwh1dT1Xj8Y0 mgAecbDL8HUBgzh6MtakBzlYy8MFISvA8dHda1bkrEKneKZ9GnntKtRWqjBClRqYTfki uyxyKEEkqjwlLRESQQVoFQ2Jv8QJJRhgnRORhBrNhDoUcJilq2/Nc7tD4cRF/aBn3mFT Ebgw== X-Forwarded-Encrypted: i=1; AJvYcCW+rU1dYr27Ig18pO5wxFdNtsxdJMwwPoVx+rSWBOWj9Sfp6QVNOa7KnF2c2SZXXn9ifyhf3LEXLMdH+xw=@vger.kernel.org, AJvYcCWtSz0MvzZlLxBLFPiDdzoOgeuYikdUcDf5+SUkrYyE2g6188EJjhoe6JWoJF3SSLMuvzBVDPMXHjQuR2xlh55rDw==@vger.kernel.org X-Gm-Message-State: AOJu0YymJpsS2bK3VBGoFtXXFceMLwtPjoC9dT21ulSPI998dp9kIJ8P 4jVXGVJedM8t+ugD+LxHVlSm+RyONWIw4b82oYaYKB55Hv3/8Zhtr9d+IN18 X-Gm-Gg: ASbGncute0bNbK14Si+LBbMzTkXLvkGGD6sxbIMB13W2gnUWwcRET6detTmhxDwbysR J3IWCfknCFZEix3y8JNAcDnzl6wvhWuCET4VdAgB8Hu9gRUH7cvoGCVokOWdZANyoK58CqmjcPc nFKMHjHAnMgKBag7H6ams7Vz1HJ19t3vIw5U/iQ9IXwCgA1qHvgi56QK8SAnSFjUXeKkdbCn+Tn Kw6FserbiiMBCfiOmcvU51bxr+tDJd68nnQ14G7CY0QVUKJ0XYbf1kd9S+8AS4fMSFjry6aX7X8 9NlMShfaerRS X-Google-Smtp-Source: AGHT+IF3bWy4/7+rA4tsl8I0t4eHn4NBoxq/qjxO+2Ac6Y6BNOplm7T0fvnZ2+ABeBRnWPP4/R9lNw== X-Received: by 2002:a05:6a20:c99a:b0:1e0:c954:ea85 with SMTP id adf61e73a8af0-1e1dfd359a5mr740163637.13.1734046046249; Thu, 12 Dec 2024 15:27:26 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:25 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 03/10] perf record --off-cpu: Preparation of off-cpu BPF program Date: Thu, 12 Dec 2024 15:27:10 -0800 Message-ID: <20241212232717.232507-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Set the perf_event map in BPF for dumping off-cpu samples. Set the offcpu_thresh to specify the threshold. Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-5-howardchu95@gmail.= com [ Added some missing iteration variables to off_cpu_config() and fixed up a manually edited patch hunk line boundary line ] Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf_off_cpu.c | 23 +++++++++++++++++++++++ tools/perf/util/bpf_skel/off_cpu.bpf.c | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 9275b022b2ea..84c3bf6a9614 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 @@ -60,6 +61,9 @@ static int off_cpu_config(struct evlist *evlist) static void off_cpu_start(void *arg) { struct evlist *evlist =3D arg; + struct evsel *evsel; + struct perf_cpu pcpu; + int i; =20 /* update task filter for the given workload */ if (skel->rodata->has_task && skel->rodata->uses_tgid && @@ -73,6 +77,25 @@ static void off_cpu_start(void *arg) bpf_map_update_elem(fd, &pid, &val, BPF_ANY); } =20 + /* update BPF perf_event map */ + 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; + } + + perf_cpu_map__for_each_cpu(pcpu, i, evsel->core.cpus) { + int err; + + err =3D bpf_map__update_elem(skel->maps.offcpu_output, &pcpu.cpu, sizeof= (__u32), + xyarray__entry(evsel->core.fd, i, 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 c152116df72f..1cdd4d63ea92 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 Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 683681DF277; Thu, 12 Dec 2024 23:27:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046050; cv=none; b=JtxURKqpuf4msPABRD4OVgHNCLBOP0XeqQYxe001/GHAkPfKsEVzjmNLUvtrWTgGz/YOgdf4d+jIMMa/b9MUBLTcGI5HCnkytbi1kUYTmeXVZ8itK1A8szDy3mJSmH7drbNZlu+0KHoKo8BQPx7xftSubGqi2QPym4rc1DrxE/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046050; c=relaxed/simple; bh=mUcS+ASYoZR9ZJIlcEk8OGmgnQDvwgRTk6WQBu8286g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qr+DXer5bFEHKhYFJ9FFCBfhbl69v8yN3qZmtovvN4GtbRleO7yKn/vOGj221N061LWQluVYyK33PMLNGJada33U9fRNj/oLxH8uqw5DbjArVIS8ChqhjhHblFgFSsrlC/7WDzqaDgHF5AZJ+chKn6kwPY3c3P1m14qjBsB/Juo= 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=efK8vamJ; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="efK8vamJ" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-728eedfca37so1235586b3a.2; Thu, 12 Dec 2024 15:27:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046048; x=1734650848; 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=kNFLCf1qgtNxbc2CCINQbIdRIjwFbT1I23jfba115fw=; b=efK8vamJiONwvIrvbGQc5dij6+9SFvNLK8iENdv4XD3Mk6BdKiXPSbFyJHvj+DkRVu x9/VS+Cx+SljVgmbZxZCjYXC6hTGlhFSb+3MM3jdnquDXWFQlsPsyqrERARiSdhj3kBO QkBNa16WKmDoL/73huzFntgK76SiT3hlDmoe7wJ3RYmtRAEGZvbhpfLkj/tzmVpaS1W7 PxjF6FqncvfVI5P/TtEjy6yIO1KoycgzJLH2f9Io7sFf+dcu3+dRITStWmmSjhD0fAvA DbG8ibFOZgedlwrB3RXIRR9PYdLGCtRxpTMthiJgiPUwWDc80VMNV7Nugty9TsqVQslX LLzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046048; x=1734650848; 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=kNFLCf1qgtNxbc2CCINQbIdRIjwFbT1I23jfba115fw=; b=GObwi2WH2bn1Z2CvIFb/FXNkooSzWC6uNNdAkqJLauypMWq+v0dg+AdWpMZwEEBAjl 0zz2dXUIjC+9FIUKPILo6MszVG972khNQhbA0c/ipnJEB4mInydPgEv2jKexnnb4nZGt +MMdL/bjdgCpeIKAyWNRLkYjd+rEr0rwR5qBgNAnSDSYZEbsZ2F+uQhA5IvMUpfymRvb cjVuvLTYG3fwn2Ge5XcfQh1bCe8luXfzb753vEf+47l6AzWVeDJbW34y4uJK3inWZmkI j/0SmsEKrJU70aN/Gykew3IDmDMYhpEGsSGvyv0z/DvWVLDhwMPzqgx4SPUYQ56Vqp+s f50Q== X-Forwarded-Encrypted: i=1; AJvYcCVohLP6lfjv3I7J5igpDFRzlaxXmrvis3FPAaMNaKuOeZzvcaz//jOtHPPofC0K2b/7wyFIRb+NP4M6FjKchBjVOQ==@vger.kernel.org, AJvYcCXEPPZ6DnbToqxCNskEU1p0aGs8uUwWc6RiUQAqJjl8IdDo89an3F1sferqM+NN3rgvcij7WnTq6nV6lCo=@vger.kernel.org X-Gm-Message-State: AOJu0YwddxHI/s8J0y5QxpyRN3QRoFai6omjLZLMkJB5WytUqoU7VGea 1wVPja8cBX8qWf0CIcjNwEPVQo4NNnPWfMVQXLSYdgJjfXtLlc1m X-Gm-Gg: ASbGncuhkxgO9m9vNzpFkJn7j6CsDZp/9mXNEnBhV/4o4EZWNno6NQj6vrbiugzw5Wv rjM8nKMPjddWRRHos8Gt5wmHahzbUqgxMFFTbq3k8/M2s7RISBgggFTWZ2TI8uYQHWzwxPDhi4/ XKv8zKpO8UTKsLzV8MiqZu5qJ3p+lMBwOEjOChbVwoBSbySLzJzaoHIiv/jWQeJFQOxhHv5nyOp LhAlmyQKGxTOfBqEdJXDgAEL2tCoy7hTDKci6pBiXqbFKOJMZoLlX2Asttt7obSNAHzt27POAcO OaqtmSBojJX2 X-Google-Smtp-Source: AGHT+IG1ZJMPrKSDMv45FoBGhnsXUR2C+l0tshftfzn0OBSgzgHHqrNr7PJ8rWFbXGGaqihYb+gkdA== X-Received: by 2002:a05:6a21:66c7:b0:1e1:bee3:50ea with SMTP id adf61e73a8af0-1e1dfd3dab2mr584296637.11.1734046047729; Thu, 12 Dec 2024 15:27:27 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:27 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 04/10] perf record --off-cpu: Dump off-cpu samples in BPF Date: Thu, 12 Dec 2024 15:27:11 -0800 Message-ID: <20241212232717.232507-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Collect tid, period, callchain, and cgroup id and dump them when off-cpu time threshold is reached. We don't collect the off-cpu time twice (the delta), it's either in direct samples, or accumulated samples that are dumped at the end of perf.data. Suggested-by: Namhyung Kim Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-6-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 88 ++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index 1cdd4d63ea92..77fdc9e81db3 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -19,11 +19,17 @@ #define MAX_ENTRIES 102400 =20 #define MAX_CPUS 4096 +#define MAX_OFFCPU_LEN 37 + +struct stack { + u64 array[MAX_STACKS]; +}; =20 struct tstamp_data { __u32 stack_id; __u32 state; __u64 timestamp; + struct stack stack; }; =20 struct offcpu_key { @@ -41,6 +47,10 @@ struct { __uint(max_entries, MAX_ENTRIES); } stacks SEC(".maps"); =20 +struct offcpu_data { + u64 array[MAX_OFFCPU_LEN]; +}; + struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); __uint(key_size, sizeof(__u32)); @@ -48,6 +58,13 @@ struct { __uint(max_entries, MAX_CPUS); } offcpu_output SEC(".maps"); =20 +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"); + struct { __uint(type, BPF_MAP_TYPE_TASK_STORAGE); __uint(map_flags, BPF_F_NO_PREALLOC); @@ -106,6 +123,8 @@ const volatile bool uses_cgroup_v1 =3D false; =20 int perf_subsys_id =3D -1; =20 +__u64 offcpu_thresh_ns =3D 500000000ull; + /* * 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: @@ -192,6 +211,47 @@ static inline int can_record(struct task_struct *t, in= t state) return 1; } =20 +static inline int copy_stack(struct stack *from, struct offcpu_data *to, i= nt n) +{ + int len =3D 0; + + for (int i =3D 0; i < MAX_STACKS && from->array[i]; ++i, ++len) + to->array[n + 2 + i] =3D from->array[i]; + + return len; +} + +/** + * off_cpu_dump - dump off-cpu samples to ring buffer + * @data: payload for dumping off-cpu samples + * @key: off-cpu data + * @stack: stack trace of the task before being scheduled out + * + * If the threshold of off-cpu time is reached, acquire tid, period, callc= hain, and cgroup id + * information of the task, and dump it as a raw sample to perf ring buffer + */ +static int off_cpu_dump(void *ctx, struct offcpu_data *data, struct offcpu= _key *key, + struct stack *stack, __u64 delta) +{ + int n =3D 0, len =3D 0; + + data->array[n++] =3D (u64)key->tgid << 32 | key->pid; + data->array[n++] =3D delta; + + /* 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, data, n); + + /* update length of callchain */ + data->array[n] =3D len + 1; + n +=3D len + 2; + + data->array[n++] =3D key->cgroup_id; + + return bpf_perf_event_output(ctx, &offcpu_output, BPF_F_CURRENT_CPU, data= , n * sizeof(u64)); +} + static int off_cpu_stat(u64 *ctx, struct task_struct *prev, struct task_struct *next, int state) { @@ -216,6 +276,16 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *= prev, pelem->state =3D state; pelem->stack_id =3D stack_id; =20 + /* + * If stacks are successfully collected by bpf_get_stackid(), collect the= m once more + * in task_storage for direct off-cpu sample dumping + */ + if (stack_id > 0 && bpf_get_stack(ctx, &pelem->stack, MAX_STACKS * sizeof= (u64), BPF_F_USER_STACK)) { + /* + * This empty if block is used to avoid 'result unused warning' from bpf= _get_stack(). + * If the collection fails, continue with the logic for the next task. + */ + } next: pelem =3D bpf_task_storage_get(&tstamp, next, NULL, 0); =20 @@ -230,11 +300,19 @@ static int off_cpu_stat(u64 *ctx, struct task_struct = *prev, __u64 delta =3D ts - pelem->timestamp; __u64 *total; =20 - total =3D bpf_map_lookup_elem(&off_cpu, &key); - if (total) - *total +=3D delta; - else - bpf_map_update_elem(&off_cpu, &key, &delta, BPF_ANY); + if (delta >=3D offcpu_thresh_ns) { + int zero =3D 0; + struct offcpu_data *data =3D bpf_map_lookup_elem(&offcpu_payload, &zero= ); + + if (data) + off_cpu_dump(ctx, data, &key, &pelem->stack, delta); + } else { + total =3D bpf_map_lookup_elem(&off_cpu, &key); + if (total) + *total +=3D delta; + else + bpf_map_update_elem(&off_cpu, &key, &delta, BPF_ANY); + } =20 /* prevent to reuse the timestamp later */ pelem->timestamp =3D 0; --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 702361DF735; Thu, 12 Dec 2024 23:27:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046052; cv=none; b=f2yGGtv+/vSr2kA+2KKFU8jmg/eb8BUV5Jz6Z6VukD6bd9ndo7Jo6aKElaSJaaWW/MpObh0puWBqdmmCEeN6Q/4te1dJO4BQisNThsEAsd3OEvWgjmw6G/UD30arWRn5eTxTvC1/xQ+zF9u4LV3rHIv3zn9I9UcD9H05e8J3VkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046052; c=relaxed/simple; bh=qS0cOrCuQAGNC6iJUTgJsbjM3SMEBq/GFor1cJW9EjI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ugIlNt1LoH8OnD3xaE6iNu3yk5/lM+j8+YE4O6BSFdpRF/TTdP7kQQxmzMbdpI8NnXIye/8u9Cj0wb+m2IZ/a6hxzJcLpzWEStk5W0jl55wAmF4Dp/m3zXIlp8vRkLVYcN4Aitr5ZpAaYeCovY3sgfQNxgNyo5E02otx+UVuCOE= 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=imOaAyqG; arc=none smtp.client-ip=209.85.214.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="imOaAyqG" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21669fd5c7cso10663595ad.3; Thu, 12 Dec 2024 15:27:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046050; x=1734650850; 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=eLwbvn6RlQVfrWwC3KGmGRTCbuyGiAEY+Px29G/ghSE=; b=imOaAyqG2T0qaazUVEJMBR1siI2452bE5Owk+NV7E2gFoItWtYSzrZQoMrNX+qniMG JVmJIwhHLHl9qiRMbNMAx+2YwhFcshGs0HkRvye5nW1gShqtE7QCm10UHJ40CRe7o8k2 Jy0CbLHwakIgDwqV81a/Xyqcg30cZtWN7o6rXc0uZiotXuzRL3wMwD6WJFsm/j92DNX7 QOG08KJ+Dw2GxgX9JTUlsMRaInrgL1JnN4yqGdxqFwHlZ6V+EYfpA+gEzu0+ehdaCxUf zDyw8ss1SBrQJn1tFjm6uvTJxIhq0QOXxrEB+YBR1mj0s49hc3CI1g5UiwLFnpWM9dgp drsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046050; x=1734650850; 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=eLwbvn6RlQVfrWwC3KGmGRTCbuyGiAEY+Px29G/ghSE=; b=Ff7UtVlD69iVnlQV+WJIBoTtoNR7cmmRGrQLzqKzRNqWKQtVM356LhQwn/Hpc2Gix0 mB7ss0mHO210o545HEjIH0QAEChZB0xqEWn9f1tWWH9KDt/vHHcK8JMJ5qSvCqpwGNAU 7Q5KdWmTuISR7sOiU7PIzwc3KT3vWoEGAlehgvlsGxc+ojPVf/mqjIRIv3o4AWWNfs3E SlNwvc9CUlbTLLK3EMHe2fPizB4P22S+txNsUfMz5wvRMdnardddAFZUS2Eta+t/uISS ROhphaD+oQ0aJoqYkfQ6Sn0N37zTIDqlfzJHgVurh5tB9yXuQaIx4GI3wF8A8oYPrPqn wWHg== X-Forwarded-Encrypted: i=1; AJvYcCVk+qhj33a0gYvM2QdzDfaRUaPs5vpsEKTNCQpp7Fnr/Xsxp+jHMjqgJutqIm9mWnj/znBqNGygdNQ/R+Q=@vger.kernel.org, AJvYcCXgEQZV0eYEVEeNBwvOcfHweNfkmrDcG5ua0LxQAvAEBUqT/Sm+qI0GFgN9oXSre/hcaTJ49X26jLOzmqDb25ba3A==@vger.kernel.org X-Gm-Message-State: AOJu0YxrlQUv09dW1ebQcGDJ3opkNa1Q805vYfgM1pWtJUIJKcTKYvpS wMcnozpAafRmBaVyEBRQrjghJXSt931CMKFsfEnfNwS0JDHzc1qJ X-Gm-Gg: ASbGnct6Kvywl/mAZo1FB52g1WtmGk256TrHI6GSoFoX6sTUdtwCSxChCJ0wwcuqC4U dN0kM5QvEjWXc51UxPGcaudpP3dw4BW2QCF7QPC/41dpeUHsalBG92dhRiFIyxN0F0u/MhiU5GH zDXWOqBIsGivo2g+2PtPIiafxHo93b0MzZsWOXg8bpEjqlRsK5Yg6QsLAFjh0QCvkbz1qkJnAiP K6Fj3zwdr1XbgwKIOiTpvJ/RQjpy8LL3h0cqzNwrVRMppMSxLxXGXn6WBGFoTpcvjeVocIA8qmk dam1Y8wA/ygV X-Google-Smtp-Source: AGHT+IFnkLsqFR+lZnVLsgI1YwL9bb/cgTuwgWM9b6icsut5B67VKs5LyOJQcPcSTp4dPKoO4RNNgg== X-Received: by 2002:a17:903:41cb:b0:216:393b:23e0 with SMTP id d9443c01a7336-21892a5a683mr7939435ad.36.1734046049224; Thu, 12 Dec 2024 15:27:29 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:28 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 05/10] perf evsel: Assemble offcpu samples Date: Thu, 12 Dec 2024 15:27:12 -0800 Message-ID: <20241212232717.232507-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Use the data in bpf-output samples, to assemble offcpu samples. In evsel__is_offcpu_event(), Check if sample_type is PERF_SAMPLE_RAW to support off-cpu sample data created by an older version of perf. Testing compatibility on offcpu samples collected by perf before this patch= series: See below, the sample_type still uses PERF_SAMPLE_CALLCHAIN $ perf script --header -i ./perf.data.ptn | grep "event : name =3D offcpu-t= ime" # event : name =3D offcpu-time, , id =3D { 237917, 237918, 237919, 237920 = }, type =3D 1 (software), size =3D 136, config =3D 0xa (PERF_COUNT_SW_BPF_O= UTPUT), { sample_period, sample_freq } =3D 1, sample_type =3D IP|TID|TIME|C= ALLCHAIN|CPU|PERIOD|IDENTIFIER, read_format =3D ID|LOST, disabled =3D 1, fr= eq =3D 1, sample_id_all =3D 1 The output is correct. $ perf script -i ./perf.data.ptn | grep offcpu-time gmain 2173 [000] 18446744069.414584: 100102015 offcpu-time: NetworkManager 901 [000] 18446744069.414584: 5603579 offcpu-time: Web Content 1183550 [000] 18446744069.414584: 46278 offcpu-time: gnome-control-c 2200559 [000] 18446744069.414584: 11998247014 offcpu-time: And after this patch series: $ perf script --header -i ./perf.data.off-cpu-v9 | grep "event : name =3D o= ffcpu-time" # event : name =3D offcpu-time, , id =3D { 237959, 237960, 237961, 237962 = }, type =3D 1 (software), size =3D 136, config =3D 0xa (PERF_COUNT_SW_BPF_O= UTPUT), { sample_period, sample_freq } =3D 1, sample_type =3D IP|TID|TIME|C= PU|PERIOD|RAW|IDENTIFIER, read_format =3D ID|LOST, disabled =3D 1, freq =3D= 1, sample_id_all =3D 1 perf $ ./perf script -i ./perf.data.off-cpu-v9 | grep offcpu-time gnome-shell 1875 [001] 4789616.361225: 100097057 offcpu-time: gnome-shell 1875 [001] 4789616.461419: 100107463 offcpu-time: firefox 2206821 [002] 4789616.475690: 255257245 offcpu-time: Suggested-by: Namhyung Kim Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-7-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/evsel.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index d0f2d9e8ce6c..e3e352e91c89 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1244,7 +1244,8 @@ static void evsel__set_default_freq_period(struct rec= ord_opts *opts, =20 bool evsel__is_offcpu_event(struct evsel *evsel) { - return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT); + return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT)= && + evsel->core.attr.sample_type & PERF_SAMPLE_RAW; } =20 /* @@ -2846,6 +2847,35 @@ static inline bool evsel__has_branch_counters(const = struct evsel *evsel) return false; } =20 +static int __set_offcpu_sample(struct perf_sample *data) +{ + u64 *array =3D data->raw_data; + u32 max_size =3D data->raw_size, *p32; + const void *endp =3D (void *)array + max_size; + + if (array =3D=3D NULL) + return -EFAULT; + + OVERFLOW_CHECK_u64(array); + p32 =3D (void *)array++; + data->pid =3D p32[0]; + data->tid =3D p32[1]; + + OVERFLOW_CHECK_u64(array); + data->period =3D *array++; + + OVERFLOW_CHECK_u64(array); + data->callchain =3D (struct ip_callchain *)array++; + OVERFLOW_CHECK(array, data->callchain->nr * sizeof(u64), max_size); + data->ip =3D data->callchain->ips[1]; + array +=3D data->callchain->nr; + + OVERFLOW_CHECK_u64(array); + data->cgroup =3D *array; + + return 0; +} + int evsel__parse_sample(struct evsel *evsel, union perf_event *event, struct perf_sample *data) { @@ -3197,6 +3227,9 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, array =3D (void *)array + sz; } =20 + if (evsel__is_offcpu_event(evsel)) + return __set_offcpu_sample(data); + return 0; } =20 --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 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 7FD141DFD94; Thu, 12 Dec 2024 23:27:31 +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=1734046053; cv=none; b=BD70Z8HMnlk2sySb6y3XN/QkiCBalbEKBcssPQpwkw51kgz6WbAA6Efxyw3gXxGLB4gEIzjSCAytXA10FYvUwWqfMfkGQ3SfR8V9VHKIpDMhFajuGLwzWH75wj/N/YFys5/8uhH2eWn3M1AZPwA+l6UA2XGWR9ZhcigQxxUY06E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046053; c=relaxed/simple; bh=e6fI2ZVl4IzPXnFdTWlY97syscDRQIiFgkHg3JcX4HA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=syLQX5OCFL6uqMpNEtgXjrUcbfvZ9Bt9DdcIb5qEssFSznwh02o2Kc+yZAtyZnMd28cwCHRblCRm20uXHosL7CUcVHBBSctWPWZeFvpCf8NutCOG/EX83a0QCpgncjTj8UoXBut1vIjOR/ocyGAFhbOOiTF/341EAUv/Ql/7B40= 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=nGf0SYA0; 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="nGf0SYA0" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-725ee27e905so1491249b3a.2; Thu, 12 Dec 2024 15:27:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046051; x=1734650851; 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=QfGyJy7AZeOLJsFch884GnYaUHhnZ6+BK2uA6O0m4MI=; b=nGf0SYA0r11NH7tSl30VpVq0T0FARohybQNgz9nRNn49ymR3dn4Rs5V642Rl9Awgcf t2fdWJwSVwhno4EfOFa26a3TVYkDSdc4+VxFqp1TNwzxJ7SIMYPwdI6PM+tYyM7BKmLK HawdycqhQ8gVbQOo6aX58tRFCtYR9ujfsV3XUPAjSMOQVrxhizFl9TcZwhZwAF4Pwpq0 Ik/N3VXCWlWyxnbx/wfKe6nCSrKbDqXHIgTQYo8QL63mojSav4hHFFDUn8ETZgmgd4G8 ycWTVsPhZ7na4/8a+P1FPwONVlfdiHIhKJZfOoywUfrFmZaSCFBbU1DCSoLo6M+XvSR6 pkbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046051; x=1734650851; 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=QfGyJy7AZeOLJsFch884GnYaUHhnZ6+BK2uA6O0m4MI=; b=McE3Layh/245d1wbiOAK00Z1quX/so+voFUIeXs34M3rbAoowB+SDKXMTGEUSb1yPF p+RCBHPlKh/wnDVwX86MiHkQVhvCWgOIboBZ58KnImE/VMUfLRbj8XWlEtQ7CzUAKxcG 2B0kHfrwz/efW9J1Qce3awFmURVdeVsKAHZqk1haneV+kOICG7xE5rwkz1Kau6XH3fEq TsG5Bk/YJdzVwQsGK/0HDkxKpdqepIl0LlYJcQ31xB9ukeZrhsq4KTQgqO83I9bO1Dzb Urhkz3kAx+17UsYA4d0ZYuz98hGb8CMt7KZx6irUJbGWbmnIEh9tyQpyWK9ianWF0260 A99g== X-Forwarded-Encrypted: i=1; AJvYcCUD3L09It7fMJ2d8hewZmZs1fHYNoQqFwY76+nU9753ZEx9MaAMTh1p88Rzsw5o22Do3eYrPs7lfuctAypGGO67+w==@vger.kernel.org, AJvYcCXNgxiBEwRuRfa0bdk03a5WxjAufZFSFGjQ3e+KtXQkHLESXxqo6n5dGSJmJnpwDBI39qEQa1LGJm31LEo=@vger.kernel.org X-Gm-Message-State: AOJu0Yzsi+noJ78I4idgmc7VlOk6GyH3OMPiV97uMDSfXOsaGjNnRjwO YM2UwYEmutNdqJhEIgq7dBW+3XYxQ1+NuVS6Rcld7FE3MPoFYqt8 X-Gm-Gg: ASbGncuJnvqbvWs0naKSP4/uEmJ4bz7CtZ/N8TC6oxQAGAhx++vjPkzSdyqqUb22VdE lfIc7KSZmVYU5Q2T9YRBbSW+xy3gS46+fKBQdu+qO5zwp6GmcDiMQYMQjOaDmQMtL0jM/8emSPd LO2qYPZrUaWIscjsb9bKLoA4d2BSxG26vIpqSpgSrKNjlDbwfZOlowTliIsI17SPg/mUnztSq1T xqdTg8uxxirpLv0zIXhNfGefon5T881uwuuly0oZh4EsIHpRT36RReOxGp6IpPgPEaVyt2ZNMDX r0KWdW1hoJyr X-Google-Smtp-Source: AGHT+IHn0yxYnXxJoyiQhqbBWY571ljeVb1LkMlS4qdlT6g34S2uq+QTwMFlMnAJT84nVes1tPPW6Q== X-Received: by 2002:a05:6a20:734b:b0:1e1:9bea:659e with SMTP id adf61e73a8af0-1e1dfdda18emr720491637.35.1734046050821; Thu, 12 Dec 2024 15:27:30 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:30 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 06/10] perf record --off-cpu: Disable perf_event's callchain collection Date: Thu, 12 Dec 2024 15:27:13 -0800 Message-ID: <20241212232717.232507-7-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" There is a check in evsel.c that does this: if (evsel__is_offcpu_event(evsel)) evsel->core.attr.sample_type &=3D OFFCPU_SAMPLE_TYPES; This along with: #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_CGROUP) will tell perf_event to collect callchain. We don't need the callchain from perf_event when collecting off-cpu samples, because it's prev's callchain, not next's callchain. (perf_event) (task_storage) (needed) prev next | | ---sched_switch----> Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-8-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/off_cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 2dd67c60f211..2a4b7f9b2c4c 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -13,7 +13,7 @@ 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 =20 --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 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 3D9971DFE04; Thu, 12 Dec 2024 23:27:33 +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=1734046054; cv=none; b=lu6ZfQ667/saZ953uCtyFAxwdtE1xMwgxrQfFbA5cYNZt6boWLCDIZEzyXsMYdnnkmr3ZQ6iMbzDd7sj2c5sgVH051Pik+uawWcgzdXAcjDSF8VWZ20H2t0nQBbF2wBpdUnXAEnQ2InPixucATwHjwpGm1wjYpXDmeq939ATZ7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046054; c=relaxed/simple; bh=jPBZDIKyif/LhOPbutvjTXCG2gTELlQErVsQ7/V8AdY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hO27eKKSFTEaSgGzqltjN/L1R3hpWpha8rDRbe40Dmn+5WzpHguwTr61zAFUK4/CQBhknO2O4279PwHd4t+Y2bdMM/YtXJDbv6ddM30q7jpoATsbbMdSLRh5XLARbilVNwlsd/42SZGQPzxYJENXGEF8Y5y710VEplm4qgKKplA= 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=SkE+2F3a; 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="SkE+2F3a" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-725dac69699so1062619b3a.0; Thu, 12 Dec 2024 15:27:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046052; x=1734650852; 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=La5gJeu6ZE78YNZ93RwabxKj07+dQVqjVUHs1YuCsfc=; b=SkE+2F3aiUHks4TfDlY6VEMcjYa5rCUOLmzEeOqa9YSjQroA/Bpll5pNUC31jv7dFX hoLbgEfzUesf58sqPXj7QWrNSWJdaHAFx/ph7jvHBA2J3Aa/fHK5smiy0+M2wy5kX2Tz ouT672QjoivQ6aKQERNhTpJJfgY089CHhMmBvRYBCmxcPMox9NRdpkkPkbTiu3rW0QJB //s7woEQW/9BZovy/ZncWFMEhLSgruifzzIJB2G3Tb5r0enYhHRwXjOCUcw6Jm6ZT1Xi B5mWAvUBjGzldk4HmBXuAcxB9RtFnBU3gdo6tRcqn0nvPFhZ5KvqkX8u3FGkLK3xzZ00 Xn5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046052; x=1734650852; 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=La5gJeu6ZE78YNZ93RwabxKj07+dQVqjVUHs1YuCsfc=; b=ZxbwNU5w+EjhgU6qcs09PnQmK3baOCDE93Bemb02PxfJGhdMrFpdMJ6YuQdVHxxbto bR1w0iwiOx7i6U3j+E+zykIbALqPGzdf/hOFQazEiJkpUWWFaJ98F/J2/E514Otq6Ae6 F95tkeUHf9g3g1Rfw90lB/udP3ol8E8vlNrNh9CfEoXQD6i/q/hva/f3LXY5ec+9jP4i zbKz8wqR5xn8NkccyUgIMWG+JVNrC4YvNGtlJYlzv9V1ulERnCyPMvZ11XlgIqnDqwsU SKh9RGQFV1+vwq2+FXK0kXqWaHi76BNjkwobUzFv2pv/ogWXhpxbhQNH9yJBSJqPpZSM ajmg== X-Forwarded-Encrypted: i=1; AJvYcCW/A7RkDOxbzR+Rx2PK5Np7OZzLl+uTfRm8LO/bzlCJHH0evdeGU7r6o8GoPORaZGDCNU3J1grZ54fv0XGV9RRd7g==@vger.kernel.org, AJvYcCWsPJuzsg+0RXqvQxnIXxjdPYJav4WhEjmF+lY5USK0zy4XwyKf3JDDZyWsXFEf4iFDrD6fxaBRYFl4it0=@vger.kernel.org X-Gm-Message-State: AOJu0YzZMsETm3xqLCpLElofRwtM0Z0wX9T71Q5eMtXYiPbO+K8ednU6 rQou5MKRvSCe0+Qjt2mxb5YnzuBIgKT3nTKUG9pE8G7OwQqo0/Tb X-Gm-Gg: ASbGncvykOOsal+uBoOeXnIdnjq/4Jyt8p3Ygr3LULgCE+dX1poIAYWmPpLcrw2Z55x uHs/AC6mg4E4WloAyjyianwbP58YzkHSZUnwdANzaRT5vzOnBoZjsCMsSUMWONW0vHbXCJjIuQL Y3+sJgyCuRdeetZNmRadVILo38KLsghJWZyUEpWRKkQ57Y132lF85rTEFmmfnQZK+OtyDQOSzNz j9sUax4JurULvnjCfQYoA6004WXl3/WZlHm81HN4suayntpZhoy678wIONxNUc8hQslgNutq3QE ba2Zu/i45cjD X-Google-Smtp-Source: AGHT+IHz62lVCjXkqrysoOggnv6NrxcmBoTtdbWrAKGPr4umM9wk2u4CpD88nO6+UdLIDikgufY4XQ== X-Received: by 2002:a05:6a00:c8d:b0:724:e80a:33a with SMTP id d2e1a72fcca58-7290c270211mr651883b3a.23.1734046052330; Thu, 12 Dec 2024 15:27:32 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:32 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 07/10] perf script: Display off-cpu samples correctly Date: Thu, 12 Dec 2024 15:27:14 -0800 Message-ID: <20241212232717.232507-8-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable No PERF_SAMPLE_CALLCHAIN in sample_type, but I need perf script to display a callchain, have to specify manually. Also, prefer displaying a callchain: gvfs-afc-volume 2267 [001] 3829232.955656: 1001115340 offcpu-time: 77f05292603f __pselect+0xbf (/usr/lib/x86_64-linux-gnu/libc.so.= 6) 77f052a1801c [unknown] (/usr/lib/x86_64-linux-gnu/libusbmuxd-2.= 0.so.6.0.0) 77f052a18d45 [unknown] (/usr/lib/x86_64-linux-gnu/libusbmuxd-2.= 0.so.6.0.0) 77f05289ca94 start_thread+0x384 (/usr/lib/x86_64-linux-gnu/libc= .so.6) 77f052929c3c clone3+0x2c (/usr/lib/x86_64-linux-gnu/libc.so.6) to a raw binary BPF output: BPF output: 0000: dd 08 00 00 db 08 00 00
...... 0008: cc ce ab 3b 00 00 00 00 =CE=AB;.... 0010: 06 00 00 00 00 00 00 00 ........ 0018: 00 fe ff ff ff ff ff ff . 0020: 3f 60 92 52 f0 77 00 00 ?`.Rw.. 0028: 1c 80 a1 52 f0 77 00 00 ..Rw.. 0030: 45 8d a1 52 f0 77 00 00 E.Rw.. 0038: 94 ca 89 52 f0 77 00 00 ..Rw.. 0040: 3c 9c 92 52 f0 77 00 00 <..Rw.. 0048: 00 00 00 00 00 00 00 00 ........ 0050: 00 00 00 00 .... Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-9-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-script.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index aad607b8918f..87b2cedc1cbc 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; } @@ -2358,7 +2358,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 --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 776F01DFE36; Thu, 12 Dec 2024 23:27:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046056; cv=none; b=qUfe6rAgHyLlzNDazgLUPf0R58kLY5kCFfGgGRqZHt938I0/RkzWHv06RcXtfxZ10YRih4AQWyisOPiLwix8dVMD2/VMXTzy3Ehqd9UDO8/SXd4VhyMyAqM+1wOi/IEkQt/WP3h+9k/p8Ijl7+c+0lpd95iNXWSuknMoy2sSkz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046056; c=relaxed/simple; bh=lwpItHlU96z4UGlv+o5kLmRFuWMmSjHuiiEw79vAX2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e3r7fRuTbLL/VD7h/qJPP4K7ZdeevxwJmmD3L7G4zxyOoQNRV8GT5OozCQAL1jYsc5DOCq40DP6yiJrOgXFr1QuYkqZ2GLPlw6TZ8ibsGq5Jw/A09HI21VKP9w4e9A/dD6QQsSAHeJa+Wov4lK13/oh6BPJVkUtT4w/UzD3xO0k= 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=lt4n4/b3; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lt4n4/b3" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-728e81257bfso975557b3a.2; Thu, 12 Dec 2024 15:27:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046054; x=1734650854; 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=jcAu/W6kHIkTq88lSfam6h5x/9TA0/DITGh4yas2uXE=; b=lt4n4/b3p0COS4ob9Y8PHAOlvDJOW+h9vidKBeYfIlzLlbiaJFULeC2R+/OsyrVzIm OOC15vynVM1pVJB5T8/ELAJnc2CGibZcED0z5QIHSlSgMdPVu4EvkrUfRsVi1TMaBjYP 4z8pHvfvrYlg89ROqJf/8dqrExIpz5V9zmfbSRrwaCEavu4iJnCjo3Og8RIMLkLemrN0 P3xprrN9K9i0yeYZuxx2y88WCgx1tcdtLKjWjZJFpO+VTjQr9E9ePfDSAXBIfCAU+eA+ GxoL+nqR8TI80KAimFQtu23M+NiVtWsYrvY5j9SGIibJ1d1/lkIuYnxbguB9szO4PYhg qQHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046054; x=1734650854; 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=jcAu/W6kHIkTq88lSfam6h5x/9TA0/DITGh4yas2uXE=; b=k3QoabqjWXVmOHQwAEtATpbY+UKv9tefQ+HKicZPOw1sYSZV7HaEBZdPD1flv11RpT YSco9h6NIEw6SPuK0wpKDZ+QsqP+igt/gd+dIL8G6xnti4b7FfjyLri22OpqNB+YN2Zh 6/MtTSFaTzJaciZyScF9lWC2NJwVwmJtVL2M5SXjy0LXZmLKDtqWdQ4wpZ7zmkDNguCH 0DgQQEu3xAHeoW91EC+UfRNdZ6E1PIlQthW02E5NaIzcdcplEN1YuuyQfPILL8ZlufYJ gulGPkVSzM1OjPRi3BrGaZ6NKU0us5CkHSdGmS84efrZ6k93s3j+A0vLV6US3Udm2sYK 352Q== X-Forwarded-Encrypted: i=1; AJvYcCUATDl/DzrABMP/2fWLes2DnSQSXVhyAObuqkrGF2KxE0eQ4NH9u91eUA9Te0oG+ulDiuPqZ2/cFdfqO28=@vger.kernel.org, AJvYcCXBUMwivZmtAJIli7ON/i9N2x/ZkXNIQ4RswvhadratX/VBsORl2HIYkZ1xKqvivDQ0EhXRt2OEdLILqQghnq2kUA==@vger.kernel.org X-Gm-Message-State: AOJu0YzS2URF0XsKwgCir8iQ1xPBgPPh7kHCK06N0DXMYbw4mzVsXV0E +Mv5QDUN7Y4P2abTuXdK+fTQA4kcwmyQEAbs2XEeoVIlI0hHDk5/ X-Gm-Gg: ASbGncu2oLJpMwHmhXiDnFrn+zykj4powxv/d7RBWHFPmEygQdjqwPjEJ3M23u3u2O1 peI9ftdctNIZT+jHp/pnD/1ZWpuPLYbQoq4DtHEuI5BKsC11IDZ09m5a0z4zz0/t2uy3uuUlruF Sv9VO4Sf1tqao2W7cCj9ORxCwb5k71D0NEVaGRWABCzzMyC0+1mzxg5OGByNDZDFcb/XgSnD3pT XjgveGMKWg/S/CBg6ze5Pmyq8Du96UH4foQ/reXt7wQmZrGk2995TvVQ43cr0z5BCaMINBo/s+c vSx2GmQxHAhG X-Google-Smtp-Source: AGHT+IEFrdudXUfEVL/HF3cjqtD3KySyOxhbu+h17MlpMRZX/Bpli/Odok/KLQLPNQ1QMVbbqNEQ2g== X-Received: by 2002:a05:6a21:7803:b0:1e1:b35b:5132 with SMTP id adf61e73a8af0-1e1dfd6023bmr549961637.20.1734046053870; Thu, 12 Dec 2024 15:27:33 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:33 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 08/10] perf record --off-cpu: Dump the remaining samples in BPF's stack trace map Date: Thu, 12 Dec 2024 15:27:15 -0800 Message-ID: <20241212232717.232507-9-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Dump the remaining samples, as if it is dumping a direct sample. Put the stack trace, tid, off-cpu time and cgroup id into the raw_data section, just like a direct off-cpu sample coming from BPF's bpf_perf_event_output(). This ensures that evsel__parse_sample() correctly parses both direct samples and accumulated samples. Suggested-by: Namhyung Kim Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-10-howardchu95@gmail= .com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf_off_cpu.c | 59 +++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 84c3bf6a9614..2e7e4ae43ffc 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -37,6 +37,8 @@ union off_cpu_data { u64 array[1024 / sizeof(u64)]; }; =20 +u64 off_cpu_raw[MAX_STACKS + 5]; + static int off_cpu_config(struct evlist *evlist) { char off_cpu_event[64]; @@ -307,6 +309,7 @@ int off_cpu_write(struct perf_session *session) { int bytes =3D 0, size; int fd, stack; + u32 raw_size; u64 sample_type, val, sid =3D 0; struct evsel *evsel; struct perf_data_file *file =3D &session->data->file; @@ -346,46 +349,54 @@ 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; =20 bpf_map_lookup_elem(fd, &key, &val); =20 + /* zero-fill some of the fields, will be overwritten by raw_data when pa= rsing */ if (sample_type & PERF_SAMPLE_IDENTIFIER) data.array[n++] =3D sid; - if (sample_type & PERF_SAMPLE_IP) { - ip_pos =3D n; + if (sample_type & PERF_SAMPLE_IP) data.array[n++] =3D 0; /* will be updated */ - } if (sample_type & PERF_SAMPLE_TID) - data.array[n++] =3D (u64)key.pid << 32 | key.tgid; + data.array[n++] =3D 0; 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) 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; - - bpf_map_lookup_elem(stack, &key.stack_id, &data.array[n + 2]); - while (data.array[n + 2 + len]) + data.array[n++] =3D 0; + if (sample_type & PERF_SAMPLE_RAW) { + /* + * [ size ][ data ] + * [ data ] + * [ data ] + * [ data ] + * [ data ][ empty] + */ + int len =3D 0, i =3D 0; + void *raw_data =3D (void *)data.array + n * sizeof(u64); + + off_cpu_raw[i++] =3D (u64)key.pid << 32 | key.tgid; + off_cpu_raw[i++] =3D val; + + /* off_cpu_raw[i] is callchain->nr (updated later) */ + off_cpu_raw[i + 1] =3D PERF_CONTEXT_USER; + off_cpu_raw[i + 2] =3D 0; + + bpf_map_lookup_elem(stack, &key.stack_id, &off_cpu_raw[i + 2]); + while (off_cpu_raw[i + 2 + len]) len++; =20 - /* update length of callchain */ - data.array[n] =3D len + 1; + off_cpu_raw[i] =3D len + 1; + i +=3D len + 2; + + off_cpu_raw[i++] =3D key.cgroup_id; =20 - /* update sample ip with the first callchain entry */ - if (ip_pos >=3D 0) - data.array[ip_pos] =3D data.array[n + 2]; + raw_size =3D i * sizeof(u64) + sizeof(u32); /* 4 bytes for alignment */ + memcpy(raw_data, &raw_size, sizeof(raw_size)); + memcpy(raw_data + sizeof(u32), off_cpu_raw, 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; --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33F871E048F; Thu, 12 Dec 2024 23:27:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046057; cv=none; b=Ku6Tp2suIZfeOxufNiNe4z7p/8qPPisTfMeVjgjJTgBGIYhIEBfIusNO7hSVKjPCPtApZEjVnGSs3AglLohDEaOxzusp14LzmtmRytaT1N1Axn/mj7l/l1rbD+0oU95fKAXmSRIpqBtJ6P1FvGVT7D6fLWQdEyc2ODmtSUVn6AA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046057; c=relaxed/simple; bh=UnK7vv2vPf8cKT/ib5cXoNr4e2IONsMyFqLWNx8SqKg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d06OGrt5Wreaq2Ak5raWR2ZSMdlVf54B+Iab5JdiBL/ip3+Rtz+i1EWo4M6Lign/WXlAos8sNuPkPCC7rUYcIeMvUs84BhjK6nzV1rSvKvoRuW/MGXPxLG4/xKI0y2Gb0keciIvk+SSnqlQunnVH/uxRrNGdqjZx7j+TXUF8bmM= 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=bfWCSO3v; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bfWCSO3v" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7242f559a9fso1400603b3a.1; Thu, 12 Dec 2024 15:27:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046055; x=1734650855; 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=aGaXt7PekQ+SHh7VzC54OXmzl48njRQIufsJiYn7I/M=; b=bfWCSO3vKXORK6PPGGwdeuEyKWw64SZ9PvIo88T0ur2Fe7bc6ERlgQ7ps2v8a67DRE FQsoNZFxEqp6+1ToZ8oaI63G8jenXHD9OZ3GnmzadfhSuJnPYbDUwOr3E7vxgMYG9ZZt Sq1OMBMnwDjw7esFhd9MqGdG0iEHLoyirNBxGpI1uUVGcYg+93KoQF/tzZWxA06RnIk7 Z8fd+LKJx4xkTiLJmervXqso+2B2D7mhiZ1yfNJttKNILYlaZHcGMT72u6ARuJx8MfEy mhqzMwqIY9EBqczuChkCFK4JO1UhV7HMNNoC0cMWfwqGq56njXETbXgO2Dyy+D25nfuU txEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046055; x=1734650855; 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=aGaXt7PekQ+SHh7VzC54OXmzl48njRQIufsJiYn7I/M=; b=TyVwofOmJA6oVrLibYsbtFjn9MwuEt/suxuOF5DDU81PCgUhuXQhGQUmbAdlQS5CH+ Pp6BIgRGym2ZYgpt8HG6J8jZ6oNpF2pqaU0AJedl7S/gVGOAAzM0/G7eZ3Zosb0vD81i g7kjrfaXRX02RVnk3nsQbQ6exAvvGZwqbHXQb0u8zzzkBC7ryFokGFhuTOxr/Zn7Etfl v3x1WOoz4yj0aHFM2LReMy2whsWBtq+an/K+IhidpDsanHD00g1nazodQAErozRlkhAw vxdaNkYr9zvrPOzBee57x4Xou/cLxC4f8X+N8bYUhzVMA9dgvP4142zH/nUlXT4GFDAZ vLhg== X-Forwarded-Encrypted: i=1; AJvYcCWDtK5gBgRykizcLz9DpR2B+eS6o2y6OMxmQTD300b2PeQ1c8l6uQJ8iEkDZ0Wlv+/N5UBRMzwNHmUlbKM=@vger.kernel.org, AJvYcCWhSKf2QmY9fTANlBql1hL1amFfn9wIB5spDJiyXLPqOMWBsMrrIHTlHNIs/9v0KDVmWREww2Aq6T+Mlpj99cCqKQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yy7nUvAMP0pmPi961G4VS46Z/QI9MuZLHJIr0mUgSMvsEnu2+qB r+CS8TGZY3ZZr5z0vOgqBOcvIchoaJniA8seGEDsK8UUvJbsb7ip X-Gm-Gg: ASbGnctuXtBRXoghwa+keaqguIeUDDztGxRQFXcovZbvHO4wm5rp7zxsLl+296J/nHX p+5cBRY4Z+HBPrDk1ZoZRvbo6dDWgIOO+a4AANA64vyKob07/7hPuuXaW+OnsHMpWbkxqW2REW5 /b+5VBt2No14Ry1C8SU16eCgc2tY5lRHKcFcMmgeldSwofsVRl3k0wlPLdINGH4CQxjBYq8JgnE RuhhxPFJHtUKjLA16Fj90qo+u6u+nunrzdC5yVWSrHyZgj7xv4a26BT/h1matXcRGJTajFU41r1 c7PC2V+WlPBb X-Google-Smtp-Source: AGHT+IGWaft0g4FVBRb7KQ+4N997Pu9PRqaGKIQj4AInRjWHpTshfD/EKxU9BFFSFOsONdZvw1cxDw== X-Received: by 2002:a05:6a00:b88:b0:725:d1d5:6d86 with SMTP id d2e1a72fcca58-7290c25b1abmr563186b3a.19.1734046055405; Thu, 12 Dec 2024 15:27:35 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:35 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Arnaldo Carvalho de Melo , Ingo Molnar , James Clark , Peter Zijlstra Subject: [PATCH v13 09/10] perf record --off-cpu: Add --off-cpu-thresh option Date: Thu, 12 Dec 2024 15:27:16 -0800 Message-ID: <20241212232717.232507-10-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Specify the threshold for dumping offcpu samples with --off-cpu-thresh, the unit is milliseconds. Default value is 500ms. Example: perf record --off-cpu --off-cpu-thresh 824 The example above collects off-cpu samples where the off-cpu time is longer than 824ms Suggested-by: Ian Rogers Suggested-by: Namhyung Kim Suggested-by: Arnaldo Carvalho de Melo Reviewed-by: Ian Rogers Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-2-howardchu95@gmail.= com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/Documentation/perf-record.txt | 9 ++++++++ tools/perf/builtin-record.c | 26 ++++++++++++++++++++++++ tools/perf/util/bpf_off_cpu.c | 3 +++ tools/perf/util/bpf_skel/off_cpu.bpf.c | 2 +- tools/perf/util/off_cpu.h | 1 + tools/perf/util/record.h | 1 + 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Document= ation/perf-record.txt index 242223240a08..f3ac4c739d5f 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -829,6 +829,15 @@ filtered through the mask provided by -C option. only, as of now. So the applications built without the frame pointer might see bogus addresses. =20 + off-cpu profiling consists two types of samples: direct samples, which + share the same behavior as regular samples, and the accumulated + samples, stored in BPF stack trace map, presented after all the regular + samples. + +--off-cpu-thresh:: + Once a task's off-cpu time reaches this threshold (in milliseconds), it + generates a direct off-cpu sample. The default is 500ms. + --setup-filter=3D:: Prepare BPF filter to be used by regular users. The action should be either "pin" or "unpin". The filter can be used after it's pinned. diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0b637cea4850..62183a6857f2 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3147,6 +3147,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_ms; // converted to us for potential future improvemen= ts + + if (!str) + return -EINVAL; + + off_cpu_thresh_ms =3D strtoull(str, &endptr, 10); + + /* the threshold isn't string "0", yet strtoull() returns 0, parsing fail= ed */ + if (*endptr || (off_cpu_thresh_ms =3D=3D 0 && strcmp(str, "0"))) + return -EINVAL; + else + opts->off_cpu_thresh_us =3D off_cpu_thresh_ms * USEC_PER_MSEC; + + return 0; +} + void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __ma= ybe_unused) { } @@ -3340,6 +3362,7 @@ static struct record record =3D { .ctl_fd =3D -1, .ctl_fd_ack =3D -1, .synth =3D PERF_SYNTH_ALL, + .off_cpu_thresh_us =3D OFFCPU_THRESH, }, }; =20 @@ -3562,6 +3585,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, "ms", + "Dump off-cpu samples if off-cpu time exceeds this threshold (in mi= lliseconds). (Default: 500ms)", + 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 2e7e4ae43ffc..c3ac19393c0f 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -14,6 +14,7 @@ #include "util/strlist.h" #include #include +#include =20 #include "bpf_skel/off_cpu.skel.h" =20 @@ -286,6 +287,8 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } } =20 + skel->bss->offcpu_thresh_ns =3D opts->off_cpu_thresh_us * NSEC_PER_USEC; + 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 77fdc9e81db3..aae63d999abb 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -123,7 +123,7 @@ const volatile bool uses_cgroup_v1 =3D false; =20 int perf_subsys_id =3D -1; =20 -__u64 offcpu_thresh_ns =3D 500000000ull; +__u64 offcpu_thresh_ns; =20 /* * Old kernel used to call it task_struct->state and now it's '__state'. diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 2a4b7f9b2c4c..f07ab2e36317 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -16,6 +16,7 @@ struct record_opts; PERF_SAMPLE_PERIOD | PERF_SAMPLE_RAW | \ PERF_SAMPLE_CGROUP) =20 +#define OFFCPU_THRESH 500000ull =20 #ifdef HAVE_BPF_SKEL int off_cpu_prepare(struct evlist *evlist, struct target *target, diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index a6566134e09e..2ca74add26c0 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_us; }; =20 extern const char * const *record_usage; --=20 2.43.0 From nobody Wed Dec 17 14:23:32 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0283F1E0499; Thu, 12 Dec 2024 23:27:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046059; cv=none; b=EgTAkvELVOLoKNFqyz6jOrmuTfkKbXyyALTGLBEpqLM+hQ+Jr8sgPvgOFxFYPRXhLU2W/JmOSUg4/1iHeTtEt4OfQQu/nz8k2xjv4aBI1MKofDvvKAj24VzWn+0BNgFF9xiYKehRSOjfWG0hHkWx/nXvnU3urFFh2qvHdWXmJW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734046059; c=relaxed/simple; bh=fk3U5R0mb2SGsJQOBzOHol5ZzILTnf2AqVHElPU/gK0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XKgJzxrfN3I4o3ScKjYVipHMahVS/taCUPP2lJUYJf4frbHfVgiDPw5rXjafI3SWK8VFTBTQR4C+IbLhVjH2ks6KNQtToE8pHMa8jyAfjZQv3Xer4bQtKeYHblyzXql4Uj0ki/mraea6NzGuuAqFrkm91J2+ppeWHyboQm8NJZA= 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=UVTuzPC6; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UVTuzPC6" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-725f4623df7so962503b3a.2; Thu, 12 Dec 2024 15:27:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734046057; x=1734650857; 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=5O/mxMoka0idCukTIL5JDxkmARERO0QsLTM8t6jJJsc=; b=UVTuzPC62x2I63NRK/4Ss+eeuLR3fmgDoOLIoNZ18AJyQE1sXYFjuTL19kJf651h5k bfdcOVEnSH6hx585iqXwWC/qRugqcF/gBsJdMM92gubESqHoJtWQeFBtfeh5rC8v8VLI b6EZJ0WjzoU+r++BLPPmSRiUN18aaJxGN9kbkvlgNEV+iZw14925UcTHIVDyuOY4YaQg MFWP87iuTt21dLJGFxqWEZRZ+Nnom9wdpR2+WzdmutTgDjxjCclstQivspDR9DZlp9iL S4oxNmRODvxyKtdcKKcqZHPrygUnwoWL+pkT7WiloxtndSRFy61am794HbOHnDV1/knK lgTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734046057; x=1734650857; 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=5O/mxMoka0idCukTIL5JDxkmARERO0QsLTM8t6jJJsc=; b=NWf09K82DOGmPaZNTKEQZR20m4sYP9FXCt2Ps2ARPSjMfyMFFJMk4RMrfWvy/XeF/8 mzegJ8z3SPm42GMDPR8/jti6QCdV83nFZ/o0ljmPy5UiNNpMUbghZ+sgq5Gzp/ZBR5vI uSq6UALFv/JSlxZ3t+GqheYR4lJTBdISb+CDZWP9DX4c6lJrOyt5PgKGa1wmgyYpd25Z 5bncwXmnbV13uSlR6w74GJt4DXgEmNeoBRbFN0w0CQB3Pom20WYYJxx26zK2s8wwBtUX WLm47OOJi9I6vTsFJ8rREpHZLO5kJcSBc6vfi0ZwxMrbusdtyNMmZ2ASPsKuTF7GDuIG f9vA== X-Forwarded-Encrypted: i=1; AJvYcCVHcjb774CvwmlJILa5FeTQMTI6UozZcsP+l1FoVNqjnt/a0tcf7GZsI35hZcxJ38t2AQlyzXks/E2SYrNtyaP4Ww==@vger.kernel.org, AJvYcCX/b1A6K1aifZDfnP+biMxhK7515Rj8ZPBG2z4PYI50eA+WFKJjTxdz6l5kftZGOKn1xnG08v0YHeiTh7s=@vger.kernel.org X-Gm-Message-State: AOJu0YzmsfLHOfCbW1X54PqbaS7TLtFoz7zxMU9hJP54yhdWeSWF2uCW y2i3BZ+Uoe4TQxGTLaeIZ+Gn82RV6mLFadRUez5P005h0cQb9J2D X-Gm-Gg: ASbGncuXBPe2vbVd7R+lwSknMi0BxBrBismgR0z3cs8JHSlRoXlyuE/B8oENhos2VA4 S3oFnSILGnRqcp4WYi0zta+EgnldB6ct+cPtCwj/PbOsmUZe5qVQCGOC7LL+i8HuK6VDG07XEsw jKmFnXEL9nmUE7LBD1q+Hs7QoVKD/we3dmbzIu9vBFoBnbuey0mPkQf0rarksQzSQSeTnaOJKsu VMp9s9ZzpRrYFLuE5JVADB12RCDaQIIcsxooz6Egs/B1vtZrW8qUjDm1fcdhvTaya9biHXFEr20 JVR2YI8Cwj9F X-Google-Smtp-Source: AGHT+IEX178AqgV5eMiD3YAtcXT+gXvPnTRnRyqJsCltdUI4UMWfAPoa7WqsHUvVon6vXNy3wga6/w== X-Received: by 2002:a05:6a00:3e10:b0:725:1d37:ebff with SMTP id d2e1a72fcca58-7290c273eaamr487286b3a.22.1734046057126; Thu, 12 Dec 2024 15:27:37 -0800 (PST) Received: from mbp.lan (c-73-202-46-50.hsd1.ca.comcast.net. [73.202.46.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4e202e9asm7324674a12.72.2024.12.12.15.27.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 15:27:36 -0800 (PST) From: Howard Chu To: acme@kernel.org Cc: 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 , Ingo Molnar , James Clark , Peter Zijlstra , Arnaldo Carvalho de Melo Subject: [PATCH v13 10/10] perf test: Add direct off-cpu test Date: Thu, 12 Dec 2024 15:27:17 -0800 Message-ID: <20241212232717.232507-11-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241212232717.232507-1-howardchu95@gmail.com> References: <20241212232717.232507-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" Why is there a --off-cpu-thresh 2000? We collect an off-cpu period __ONLY ONCE__, either in direct sample form, or in accumulated form (in BPF stack trace map). If I don't add --off-cpu-thresh 2000, the sample in the original test goes into the ring buffer instead of the BPF stack trace map. Additionally, when using -e dummy, the ring buffer is not open, causing us to lose a sample. Signed-off-by: Howard Chu Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20241108204137.2444151-11-howardchu95@gmail= .com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/shell/record_offcpu.sh | 35 ++++++++++++++++++++++++- tools/perf/tests/tests.h | 1 + tools/perf/tests/workloads/Build | 1 + tools/perf/tests/workloads/offcpu.c | 16 +++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) 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 738ad32b2c8a..a79157593c8b 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -144,6 +144,7 @@ static struct test_workload *workloads[] =3D { &workload__brstack, &workload__datasym, &workload__landlock, + &workload__offcpu, }; =20 #define workloads__for_each(workload) \ diff --git a/tools/perf/tests/shell/record_offcpu.sh b/tools/perf/tests/she= ll/record_offcpu.sh index 678947fe69ee..73ce16d61b5d 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))) # OFF_CPU_TIMESTAMP +dummy_timestamp=3D${ts%???} # remove the last 3 digits to match perf script =20 cleanup() { rm -f ${perfdata} @@ -39,7 +43,11 @@ test_offcpu_priv() { test_offcpu_basic() { echo "Basic off-cpu test" =20 - if ! perf record --off-cpu -e dummy -o ${perfdata} sleep 1 2> /dev/null + # We collect an off-cpu period __ONLY ONCE__, either in direct sample fo= rm, or in accumulated form + # (in BPF stack trace map). Without the --off-cpu-thresh 2000 below, the= sample will go into the + # ring buffer instead of the BPF stack trace map. Additionally, when usi= ng -e dummy, the ring + # buffer is not enabled, resulting in a lost sample. + if ! perf record --off-cpu --off-cpu-thresh 2000 -e dummy -o ${perfdata}= sleep 1 2> /dev/null then echo "Basic off-cpu test [Failed record]" err=3D1 @@ -88,6 +96,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.999s + # -D for initial delay, to enable evlist + if ! perf record -e dummy -D 500 --off-cpu --off-cpu-thresh 1999 -o ${pe= rfdata} ${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 +128,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 cb58b43aa063..2e655a617b30 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -217,6 +217,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