From nobody Wed Dec 17 17:58:21 2025 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.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 ABAF3149C41; Sun, 15 Dec 2024 18:12:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286360; cv=none; b=J8Tw93Ghg5hpeKdnF/kT1gMy1s96t9AA/MCcYKobB0PZ53+WxQlJC3/bGaSuazLJR/qSSmFpYYoKk+vV+1CR4oWNyF5h191dtbxujih1nv4VZaOGvVwMafJsrXAM0igumvFtq358HJO5mCeBx9yFmuV3UHd6nUsB/f/NX4o2/3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286360; c=relaxed/simple; bh=L/pLYwMbeoRQRd9+SvwZS29uLUby76c9EPlwh3l3AIY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HhAn1njMjWBYJCr9w/HoJ73JQacPisTCBWpeRQRYyb5GEOPTI18wLkjOaIPgZSlHF6l33rUJYuw0tgHm9GSKDAT6YlM5B3kTPq+RyvPes93xgBBRcRq3AQ7mVXxzS3ohZBxAEhHMC3z3838sztisGKjEObkY6kxT+V/rhRK2PJs= 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=fcF0hBy+; arc=none smtp.client-ip=209.85.210.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="fcF0hBy+" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-72764c995e5so1986538b3a.2; Sun, 15 Dec 2024 10:12:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286358; x=1734891158; 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=k1n8aol4fFd1J8Jn704GnLxaE4etozmEifbq2ZMZVg0=; b=fcF0hBy+gVU03i6SRnbYNPdwovXHHA3qKGKK0vKulDxR+njR1mbT6QkWs/vYToL5n1 S5MOt9rgsZjkerUPJbGknV9vhI64hmngQtGxzRcvta5eGn48SRtqbeBBdR1i0/o+4kb3 5GDk0GkUxgQvUu8Wf3tAEfaPL43rGEQVLrQwio0xdHdOXYEds2jeX/b3AS1N9ILdIfIF m8Z1xzVyVOnRM2xUyizuVmuSggd5gk6cwFHrcc7CrWqqujV5kd4pD95W8LSmFnYRb988 4Y7NDmdsAxbrwBGiiKiRMPfBJ2i2nXjsiY4v8v1osSfXgLNyAyBN1pkFt9Xzmw2b7Lwb tgSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286358; x=1734891158; 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=k1n8aol4fFd1J8Jn704GnLxaE4etozmEifbq2ZMZVg0=; b=YSN2sZz50FlkGaeplPbR8+O6Of2K0J8LPrYGZRHbA/2X3rURdjj3cqRAU8jpvARl+n tKrG1EzFGifPpTPIXLQUVBvkboz38qqMhvBNAte7AJJ6tuI+uHpRHzBTVF4uruQ0AVPT HZn92c9TQ+0vZ1GZrxTavjcT/qxq2pJhwBD+QOTZD+xpeEVFYDSk2XN2H+v4TEsbq+kD HlXEqFu4ZsdWFMSYL83E853ROHY7Zt1NBHE2a3v4U1eMqJm6HMkMVj+FBTbZE1cLUo5H dfP6MED6NAyj8HOBf/azyeJybeXUTYkWw6Fz74jfVYIBKEBiM2fBS0XDPNt2rsqBOEyf rRFg== X-Forwarded-Encrypted: i=1; AJvYcCXlu71GNdoN85nQmmKTeeXO+hik84vn8eDKW02PCugjomf14oRU38eifTdjdf/RfbJZlk5+pb40ShDwzrM=@vger.kernel.org, AJvYcCXnRane/G+VXHNoGm3gM1fptL6Nw9bIn7cW4hQ9HEWTSeaEGzSSWprftUGrYSdB1wah0Zy3bb1MDS6QpL0BT9umXA==@vger.kernel.org X-Gm-Message-State: AOJu0Yzwq8VLhGKx0mN8XChwyaNpVYkxU+OHzybXzyFcI+wcRtqgy0YL ELVn5V0Kzr53tuEgNaILUPFzhbKMxh1zbCLPSF7WztAha5+V/kxe X-Gm-Gg: ASbGncubplEo42Ns+cv18QcMpu0WPa3p2PQEp5yh0WU6Z9sGCY8IBDnBg2t055NFmXx UI9KbaKqHYAvYFDPnIeT+ioMzu9WP+j1tl3UEkz2dGHo8sNypDahpyjVQAAYoAvzi0CmDp3c6rd xSjnE+E4GTihH7yaseCiW7oZm7QXsQWckxyHTTGmMhessjxtEds78wffeelVFgWGJVwIS2UDRfj AZMksYyICg/FS2eh4Olw1+bHqu7psveR2oxAn1xVR0WJcb4jmEUqIMkfOhbCqxF808mJA5sc2yC AM7pGaJHRlrQ X-Google-Smtp-Source: AGHT+IHpSrlqrSP8HjQdDaP7RoJWB7L/J+VaQapQzhQJc4z10kagenH+Y4uQ2ELJGW1L6O8JmKBjcw== X-Received: by 2002:a05:6a21:4988:b0:1e2:5c9:6791 with SMTP id adf61e73a8af0-1e205c98b45mr5549334637.15.1734286357863; Sun, 15 Dec 2024 10:12: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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:37 -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 v14 01/10] perf evsel: Expose evsel__is_offcpu_event() for future use Date: Sun, 15 Dec 2024 10:12:11 -0800 Message-ID: <20241215181220.754822-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 c93311e502cf..94eb3dc034f7 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 17:58:21 2025 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 6A3211B6CE0; Sun, 15 Dec 2024 18:12:40 +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=1734286361; cv=none; b=QGwKl4QmBToHa/SmnKJYN8q4zoAXCMeuIO2M5FDeQjXPuPsfpPNMM65HHpmLGJJKTkqQzhVkColhC3MkIMgWAPsoMNBLUOWzm9yyLu8Wf328xVV70ziXwWbvCzR+gQGZDeOAHmEhJWD+psmMuav3md1rzPlQ/wwpzE1QIi+lbOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286361; c=relaxed/simple; bh=Qh3Clmjgmz54xp/kaBCMKH52HOZLKIMHH2zdRPXjk94=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hyGCCoAPL6RULT0sFIDkUzkq/eSZkibJ2kHii/CG8NaQNFsBEBe13VJT+PgM9SajmGWxZkyvXC7ESemF2jQcCWEt1IRjlSPltA3mVdOOICzmTErPR6FZGcqySyfvLcoYZPhD/ITj3n/ZPA3i1OQ8uvbvXZe0v0SY/3nrKshivNI= 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=XNet4ecb; 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="XNet4ecb" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-728ea1e0bdbso2454000b3a.0; Sun, 15 Dec 2024 10:12:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286360; x=1734891160; 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=eCP+wB6UaKJTN48gmJSi1my/e/aavCUaAb8y+M+Z0cY=; b=XNet4ecb9KsTpHG8nvw3KYawdybF2JqKD4jd+F+1nuKgSqrRRgXqCA6uM5EyNNoQqC qeonDU+/6xcjBdFlCVDDVAMwlfdSZ7nV1KqZIPsHamgJuzDDd218ob38vgkFo+GLUfsQ IsuEFus6Y4aGKj48PUNu/B9Tid9MKCHLj5F0CQkamGtrVLnuUH0Lh0STI6PVBQR9HKEa 6ma/FmF/0c9ZhP9PHh7FOkBKZg3yiwo8/fCfWErkJP+3FOFwiLTurkA+2P80MbGgnNk5 NlJkqqQHRsmW6HlD0bok1tqL5JmPPqv8goB2InrswmFba2e4st/Tg2C7gteMwrqieSja GARg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286360; x=1734891160; 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=eCP+wB6UaKJTN48gmJSi1my/e/aavCUaAb8y+M+Z0cY=; b=b/pLFKwcXPxQQ8dA1WejDJEMlJQ7/i16j2HpQpyaXTxwl3CuykSC44X3QNtu2xSRXi wXkyVUv+3fUFjDKevovoB1DsctZOCgzWtsyZaAveqPXy9Kmj17HbJzr76puXhDgpJDAm MyxTG3uEbQPEpQg4HLOSbvFaYavEqRvYirez/bSJV6h74h/2Q3JAd6xUB72xBgMabVBX zJrZYy8172HSjxc6eBWnrCdEFT72GSs62q5e2q7ZK3NzRmJovVeafle1+8La2k3yM3no 3yMAZ6kUyGZBSDRQR9SDF0EihT0Q4Na7IsoiDgRjeWfAvFDfvkLXbtvOPs7O0m6s7fLt WhEw== X-Forwarded-Encrypted: i=1; AJvYcCW42rUK9N5ymR483JWxCwigtbXdSRLLBJjrarcv8uSRlKMQh97PeuRpKg1+eZkCQLWv2PnYOimq1CqfyCTycIPIiA==@vger.kernel.org, AJvYcCWu5+tO7xHuwKO3g8nfEeshDs35Tmc9jkVDaZ2uO7N2lw+px1BEhs7v1vA7/mxXURu3AyJ/RXaxxiXlFOw=@vger.kernel.org X-Gm-Message-State: AOJu0YypPQwUeiXXDPhbjuwnsK8pgo78qmSaxYiW+GcETp9RdDw0eGiB KVDyB8w85yBOhsDqwn1oetPc1waV3u+08h7qBchXYRwRXxac1rmVHauvinpQ X-Gm-Gg: ASbGncskc45tFkAP0EkFEYwg7n/pZO9iovRX7Qyj/lpHQh8DCMcjPty3rXsqdwGbhGB gRdIX8V0xlSTd13nsl1ccHAhm+Gnnx3FYydl6UTs3/H/MHPmNUVJ+qTL4rAf+fOCbDGsepNRg9J KzmqDqMq3JQ2R0kZe/Nr4QR0VFy00wnOVrdudxPr5OoOJ8790vNQbWaG9rsZktH+ZdtOSqxZdlz Wz32a1B3sI2zsjQFgLGCGy2MOdHCC9DDrC16LZKRfTz2w8kqEQyjjCddYkMUL2oIfeokrDQDRy9 7pM0oh3j++J6 X-Google-Smtp-Source: AGHT+IFHTG3BFoQJ4qYajKKHnr059EdGrjgScubnNDvojubTnxPAr+sbeklR8+rkwgkZM7lpZ64ZSA== X-Received: by 2002:a05:6a20:a106:b0:1e1:ae9a:6316 with SMTP id adf61e73a8af0-1e1dfe0003amr15655277637.35.1734286359658; Sun, 15 Dec 2024 10:12:39 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:38 -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 v14 02/10] perf record --off-cpu: Parse off-cpu event Date: Sun, 15 Dec 2024 10:12:12 -0800 Message-ID: <20241215181220.754822-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 94eb3dc034f7..fc33104cd659 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 17:58:21 2025 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 87BB71B87D6; Sun, 15 Dec 2024 18:12:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286364; cv=none; b=uXhMG9eq+U601UQFz2cUG3C8VoGwYyl6JvTI2p8+47zRPIkr+4sHdg8wUVDyEjpaAcEJEsKYrsn7U+d91Q5qFJCeKuDVXOhbpWF/iCeLlYNOaBOLORl51BQY5eydKZep4SdWBZSUczAHBW4uRZ4CvtcZKVoC1kVHkW37K5RBIIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286364; c=relaxed/simple; bh=aSoKRjTAYvF6lwpOwLrBmfF/+YBy6tjV5MtsYW5Lufc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LJzrytdhkQF2Itl34WragH6vpPKuehUvn3uCqkGvrt/Y/bPGyVaYUEhfQ5nGdpiIslT9aFU2HBuVGeDOystYwCNExUfEwuoiHDxy2WVy1ApbcYI1ukGQbSlXzU2pJIMLFLMAUWwLETpsHI7piHXmjnNM8eD8elHr8OtfvzgrHBk= 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=bpPSQgyP; arc=none smtp.client-ip=209.85.210.174 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="bpPSQgyP" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-72909c459c4so1881176b3a.1; Sun, 15 Dec 2024 10:12:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286362; x=1734891162; 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=bpPSQgyP+pyfgdDf8pRP3g1laO7Tnaqwwg9zS/AETqZ+s8LQYmWdeA0ZmBwY/lfbBO r9Th5BqU0KArhRHHFnNk+rgn2NiTt7cMIb0XZsgmcomXydxADlYCounAqV18vs4DARh3 d93TZLTZsffiZPHfnJRwcGsEvLG8SZEGhIxi6M0KYy5wC1LEUdKje8IdMw366qcMHBWo 3y7NncvyyMZeVIyd+uRQvK3ggc5XzMuUJ3iMNSITH/x/yyLIuR9Sa8k0sIdyu0WFYDIX r+HiJ0kOduQbRQb3LaJTbTxqNJbl4N4C2VEg23wyyhQhOS+mHsq1GQXjEK3HdpEaUG7G 9pxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286362; x=1734891162; 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=LSYaOfVME2gvg/DE1HKx0/VdaQFQONJb2A2CqesPX09YHmEdI66FGZaEJFjy7z3CdG MaOQJDTFyy9eP8gUJK0qrc4muvNYrLjWZHBCV3PxLzLWGenfMJimbdlw23BLp2GVAT0Y /TieyRQ1vNsTlPNqFz6IkyO+fXvQWDTFHvqgTW1FudymxNS830AgGyKddhgL7vw+h76m PyjYTdkBNijGww3ibNoJy9ZRZkRawAIKgGBKrhOgLeF+0EHrlCwPseMVX5UTWCQDvad2 0zSeFkZn3z+dNrTGpbSxF79e9BALKKqLMZuJgHA/g0xMoalE1wBAeZrdQtYhefVSVWTZ VaJw== X-Forwarded-Encrypted: i=1; AJvYcCVKLvarepVvw9Hg3Jm6ojN8o7fc/6j3NporvKC4vdzVsk62JyB+ADyVgK2iVyP36zJukdBC7AVPixBuH4CpZupzZw==@vger.kernel.org, AJvYcCXOuq+QZ4Mr0J+YYwsHYcjjecTBjOsQeTdJtzKNrzeI266dste8fIAoP8C2zIbCDb8gQI3JyhqahcMeHVk=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2m5JM8DCadBiV2IdMHNAkfx+Qs6zOL/uaFHRHLpKM7O6oS69j I8UL2crcE+DD8EmCS19MO53W5mRTub1WHNarZ6kvY/58LyFmf5hT X-Gm-Gg: ASbGncsLaAHuWOZHcU3v3nXI1nNayfrZkldkQsmBYH2fZgXSZACmZhznlOf4ZV0gL/S 7hGHfM8NQTMbjUkDumN2s+8/bYY8a/KHdibAtE0hczzP3ZhAU86p6mP3vD+SsXYt/FBu5Ig2JTn Edk9rR1GtQsLp2GFAUdxx98ZPEq0ZOZbpIbm+CUzCwYnmr1yaiYPbFwGsEsfpESjT8LPIN+vdZ7 3qMuBVXG+wMqvikaZ2XbvCODC7GexIGJtZsGwblV9YDro1zVIhi9NlPEK8NjiBjVHVODaB4looW WDFWiBWHqhTk X-Google-Smtp-Source: AGHT+IEH3UX6IGKWcsotxYYat/9d2h91vDhlDoj32ujW+KjFmp/d1tL5foLw0YUbv6SM/fCtzaVEsA== X-Received: by 2002:a05:6a20:2d14:b0:1e1:c03c:b420 with SMTP id adf61e73a8af0-1e1dfe127admr15811916637.31.1734286361885; Sun, 15 Dec 2024 10:12:41 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:41 -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 v14 03/10] perf record --off-cpu: Preparation of off-cpu BPF program Date: Sun, 15 Dec 2024 10:12:13 -0800 Message-ID: <20241215181220.754822-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 17:58:21 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 0F3FD1BBBD7; Sun, 15 Dec 2024 18:12:43 +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=1734286365; cv=none; b=IZQmPs13Rb/B6GoVVByDFYGwo77tYi955GgL6RKEUA7IJk34pHivsJaJp1gGhh5or+9gCQUFpZ3ZquVGDtIwHXG7+2vGqMlNqROZmfiwp46tulli2aumD2CwtXEUmFMGrqBAWzW1ZwVz6jbTHtI/T2U0bJ7zfKbpvThZ7ZP+/2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286365; c=relaxed/simple; bh=mUcS+ASYoZR9ZJIlcEk8OGmgnQDvwgRTk6WQBu8286g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aimrcen8BYil2EAGqv+JVwKbTWtgeKexvCqYuTMU+ahol+41TaT4i1AHw+thkxqmgskqEOR/E9FyJPDV+dIifpww2mXmH8L5E2iG6RVKNcweqC44EjgEqBea1IqA6O74z2dGzDnqSFpmgvCiTQSAG7C064SEHtNJX3tuLAZLnKY= 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=RXZIqpoU; 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="RXZIqpoU" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7265c18d79bso3630204b3a.3; Sun, 15 Dec 2024 10:12:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286363; x=1734891163; 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=RXZIqpoUamLl1G20uteh13OLB0/vd3oR84OQs2gAZCb2pkhKpdCqSqKCCsUsxSKZTI FYq42szhMM2d8cla6R20rqL29tCFlaLqZVt/2mmfG3xY/7SzaKL/+xl0QIzVXX+Ctb44 GCqunmtVKEQp4XnNyZbGXQ0mk5vl5c+wnQsKy2phclPdD/z6GN1NNu9ZAE75oKEL0hFN E+I6gsv1ferMmRnpCLRyqKQO/jHRiRIaA8KZbFumqSHvXdcfjVaXDvaV5MqQ7z/QK9wG QBkZVUgXLtj+1lJ3H0BRLtSyOfHJm83n3Rb/Ot0Bgo2ZWqunamnv8MuJfG93SQNIHpSX XQfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286363; x=1734891163; 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=wGFYjD2OVe7+fdoaIBdxWqlbbCfCn49xHp3Ie8iPlM+SLWhF1Hu8I5M5Yd62j0o6Kd Rrt3m9a1ofKWgxHOEel6F4xtsCRm51jR0iACmtwlyaq8kq8L+kGHzKj52nL1jxAaIysO ea3IGJ2D6g/dzl4TsNMOpf7PETdQzagtzmLSyMmkR8e8nDtdn8qfL0hl+UeHs+pPbk/w OBZhZZ766oN48XuLIjlIcrvzYIIsgJuMma2r8ix/ZIlWBWEqrCoZo/92pSClOdTB0/uu DtOIilS+rT4YxmXo6tkUwCJDsaeX2/KZBQOGu7fhKmcu3X6zeMj14hcJ10HLzQ7JmkCF iXdg== X-Forwarded-Encrypted: i=1; AJvYcCVn+qPOFeJmkzGZ5byO7et/40kKhwKkxuQ56r/n1PoCVBaCAWfWx0vyGsYmbbgYpn3uyOwu3a3DwJKsxuE=@vger.kernel.org, AJvYcCVxYG/GOcEIQysKDq+SBsaY2JhDgJK+dY+ddLY2urBGmQIkwRU2KeIn/VyQzqw8zgE+D/ErMB1NCUBAVjgWPH9OdQ==@vger.kernel.org X-Gm-Message-State: AOJu0YzFmsCSfq8ie5EU3wDJxDu/1SlZDxvci/o7IstK6McHB2+BOiyI Lu1dQQUgbSL5FI6ILnCn8dNfleW4tVRwHC/1TcqOaTEQYUTbNT0Q X-Gm-Gg: ASbGncum/pNmJJf3M4Bq9Te9BVI5gk1s7BxG68y2dcF8hO852mgK7jg2PHDmDxInm9y 4FxpbVFri4M/meIppAo58QS5lJEJFXH/TMQTBASS/ZNxCqtPHYwU0elELrHm7L/G5XP/88taKDS okDqtu37xSzyJFZZ1cw8d2j/SjOdVoDj6eWxoMgxYzKeGCugMwnCHmbpICizHvfimtwoCbNDH65 EcBAdbLbj0122KPEE+2JVPymG5HRYLlOEasY//hmSP6+0ba6vbU/extYg8WDzNfUliKHBRUIEGl CnHNjZFdFH6S X-Google-Smtp-Source: AGHT+IGI/yW3PNjS37CDQODaShZ/w+FlthTqMFK80FGqTOLG5N8HGbYrRskb6pzM9LFXRLZ/UYB/3g== X-Received: by 2002:a05:6a20:db0a:b0:1e1:a885:3e21 with SMTP id adf61e73a8af0-1e1dfc18486mr14701626637.7.1734286363285; Sun, 15 Dec 2024 10:12:43 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:42 -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 v14 04/10] perf record --off-cpu: Dump off-cpu samples in BPF Date: Sun, 15 Dec 2024 10:12:14 -0800 Message-ID: <20241215181220.754822-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 17:58:21 2025 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 356AA1BCA1B; Sun, 15 Dec 2024 18:12:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286366; cv=none; b=UM6Rqr+/Ak9Pu6r0NxKTh8R8KbGk5Xs2jjlniwsB3E0jeiCVyqXdf0kwMxCDnYAfDzedxK5DbqaC3TNwYr3eL0+qAD1sWx1zZqIkzg11L3ndI9A4G4vtlvBUSrrkfGnKO4XtFsClltY+X+1fhjUCagt+QZJIr/Ko8n/YMzGx+nQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286366; c=relaxed/simple; bh=u9g7+7bvhdlDEFFYtq1CIea8iEKMWDTyn0VCkU9fehs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=or7fVRLgv+TDY6Cpkt6b4sD08qRYaznX6zUhQMIAA8FX0LjdS6jX7jz85OThkEV3ISoBm92BRaejcgpfTsUK7NzNBORWJFNxaql2QQAXKL3XfOLcZbC2kT1AXbAGFMI8BjcSui9N/NsdNQdDBHIYo3RS+roWVjmRR7KMLtXYBhc= 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=OnxzmYZO; arc=none smtp.client-ip=209.85.210.174 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="OnxzmYZO" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-728f337a921so3470029b3a.3; Sun, 15 Dec 2024 10:12:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286364; x=1734891164; 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=z+5hxkFVHfua/URBhTBN71iEJ316cdDFM1X15VlseMo=; b=OnxzmYZOT4UGHF4/b+SI0IyJt4BQfUvluy1YgwMjxAFT+4GRA+90cdN+7kpKqBxDfW eOp0I1Qs4fgYoJF4A+kt/dSrvDfCxzQep8H83m/1HjnowQLA2IwXcYLF34Nrq3wrYZv+ SP6Plmc2Wvo3tmFccqEsr9wmdB5uwWF45UPIy4zRGMRuOtopGyjPV6gIq7/n+ywTaKq9 BhfCpcym+ut5PNbps0zcb/isnZaF6E3CChc65jqYebFwNLT4jYMPQwJ7Bp/jnl33tiCh 6AQobssRGEiBaa7oWEE+sEXAR2MF50206GAkSvD9V2UIWuzmo05Jzq1gtNDUZqglbU+2 rsdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286364; x=1734891164; 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=z+5hxkFVHfua/URBhTBN71iEJ316cdDFM1X15VlseMo=; b=LBEByQcOstJdb6t+BPeR7o+qW5JRzfFyR9YJbwAQYYW6BPIbMR2fFBGytSW151qtJS 2qbFJdswdWYAMyjdZVjB4Spv2odKbP+1+4WfF7lEPSO94FD+PwGnOiOQzwuwJnzDxShy 0gig/Xfxb85o7gKbAmi597b5zXDi/cc+PTY+Z7kO5t4X4VqQ6rAkI6G6qHflHaKZ1MzC xGxLgCx+PBWrXH41qywVpTnsIRjg8OJnpQtF5A7/JxuQZn/m/sWVd4YkXSowLLftT/Na jearefxi6eLXin5JKoL9uKfXbbkiz19TY0acjIBcWLBr9Z2nZOWLbTuTVmg0N/DONTdA 1w2w== X-Forwarded-Encrypted: i=1; AJvYcCVdQAioinnsDyWPp88JywzEv6eMPjUOA7bmbYCeHym5qB7Y8+5zKq72LWXAVzCyBsB/yDBFkgWl67WpTFO9gEW6eg==@vger.kernel.org, AJvYcCWdmQnKv1Q9cGGc/glZVsPTAd6J3u9qb3LnG5tmPp5/FcLbx7zlyjKbyS6zWY3nb8sIqUJz9UTHPtAQOxI=@vger.kernel.org X-Gm-Message-State: AOJu0YzuxTeY7An1WSsKYowfVYt4MfO0BvHEz5ltTgCCMbtYi4GHVAag Ljc1NIUBRLo3n3KzryNo2LDicLrP/0ssCHK2y0gSllqq6idUH12EIbtaCwh0 X-Gm-Gg: ASbGncv7jVTGuc8nVOktRMcw+PWgDrO/QOclKVJqcf8lvSoZxmp+4WKg/QjoRoVh1iE K7Xlf5mlRjq+OTheTrtWGomV8sA7MkQirrYj2r8OIBnH9ErFZNN5m96rhRWwO5Lyr/PDX/Z1F4o FXC6ymp2AmnfQpgAmTTLiecfX/Frvw5p+tQYFuuTrddZuqj6xfBFP9XcaEs0MYDS5pkhT9u23zl KN/61ZtUQ41X1SYf8/azXJ3KSX57PPwmcSDbyxeSNfftGmCxQgWWZ19+G2cUuGY8GBRkBszWWet ecCE58SAx0IB X-Google-Smtp-Source: AGHT+IGU+vbAKgR1SQ1dT8iqtnXTC0//K4nzFwzu0jnGveQ8YDveFWa72zQGLPmqhSmUVp8h67HAtQ== X-Received: by 2002:a05:6a00:22ce:b0:725:eacf:cfdb with SMTP id d2e1a72fcca58-7290c27d62cmr15721291b3a.24.1734286364598; Sun, 15 Dec 2024 10:12:44 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:44 -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 v14 05/10] perf evsel: Assemble offcpu samples Date: Sun, 15 Dec 2024 10:12:15 -0800 Message-ID: <20241215181220.754822-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 fc33104cd659..277b5babd63e 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 17:58:21 2025 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 9948D1BDA8F; Sun, 15 Dec 2024 18:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286368; cv=none; b=bfXuP/VBoTtN8XGQNGELtSUZgiC2wBi8lBbhpIIRWCsi3tPcUfw4WjI/Odr6FoPjeJgz61hZZHX7RlgTAN/01M1ocv1dz0enjY5wTMy9qZwqxxdGAAUFfISlRTfyedlSqEylKqxbHoFjyeH5Tlm73kNLtgMHXJh4YZbdSZ/Foz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286368; c=relaxed/simple; bh=e6fI2ZVl4IzPXnFdTWlY97syscDRQIiFgkHg3JcX4HA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iPVasE1QeyVtGUbjiDbNuDxVHxMg1RUetkNVTKKslZ0Ff27eGAW/euagMDZDzlcIwI8Qqz+KoCGFt0NQO204vbkBIi4litQIwdcsUep57B3Wt7yPwfgUNF2iSS2sZJeCsBOmvy8C890Y0Fzh3WZq5RY6OWo0X2Bvf1sOF04otds= 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=bmwYXs4L; arc=none smtp.client-ip=209.85.210.171 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="bmwYXs4L" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-725dc290c00so3406070b3a.0; Sun, 15 Dec 2024 10:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286366; x=1734891166; 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=bmwYXs4LVFCW+JAUMbothD++KiMNM62H8cC0bHzskYPZoidEVRZGxF2BrjeTPtLBCb WQBvV1NfqyfOjAErlZP+8manBBZXi6sq3Zd0o5z9GYuRukqDUFRWDwk8Rh+TKq116yKW D98GMa135/das4waPe1ZwEUHJLNR6dxToGWQ1bY9O8pbb3uuz44LCwCGFD4aSA61BmeN NYD9zNHtkCfJEHHLfk3L0g+DmgsiYf45AJrprvt/R8Qvmoszg5JLILT3+Dp0pL1Q3kJ3 sqzouua3v2ifI0LwQ6Z2GHukF4b0jGBu6bAhEwEqGp5+I15Ad2VAtV4O6XwKSHcvJApM lGNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286366; x=1734891166; 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=r5Y7UBi7o/PE4nTEdlZVSTqIv2I5Yb75mZegR6rFa0FZGEJZE2BhlKA8jh/kFwgEvK BCJ9fjXu6Lf9TD+6UJ2JzRT6mkQjofMUb/aH60RJLXafB5FswTV2igd+2oHEBhuYx8QS x5CAjf45NAK5FhZZISgGII3XoqjwiBrzIjR5A98mzBAfW4InJRvmsNUPnmnkDWBspHuB o/f0I9GEIi3ogBloMhp+uwCEwNTI2oEaUZOMIQreTAFvJOXFtyLV5YU1PHSrSKhifFkC pNeCX0nEAwhQpcherVl7kqTu8Nh+sUtHhIMp7Ws1CRVoowJ8yaVDlRQwa7NZl6ywbEGL jUXg== X-Forwarded-Encrypted: i=1; AJvYcCWW3yVqDtg+L04Nk9toQ3qcgowa3Ytt9di+evGwFbItn73QrItzBkmpUROdvxaar+az0leZbWQgTlth1UKwKspYLw==@vger.kernel.org, AJvYcCX4f8k1QOxwZWxVUxsBpp++oKqHsSbHuDOqked1CKftz3fiW+iehrOW/YklS3UW1WwziR7v397z4FSftYo=@vger.kernel.org X-Gm-Message-State: AOJu0Ywzpk14mxBSh8M/tM46A6LNvRJfKZYvmAbqhaUqWS8XR9xOhjF3 sbor1tmHJOxiJEDo7sHR7lWWbggzJfpcw0fJH/r0FbN8sPF60+fS X-Gm-Gg: ASbGncthJ9G3B4YcBOr29fqkYCljEq+xyog5LcVuSLfQWhMDLOpKuzh09QLDSLuVZ9s ymKLLQ+29gjACPrfxK8yEvtebxTelWLwEULStrFbT2gf2qrdRf1j9iPazf8tQIL57ihjevsJHlh G9rZIyvsy7wV4UWQ/Z1yG70K/mu3ivTmxLRTXUrZOzpvhHMgoFX/FtlGGIEl1jxKOSjpl+vGPtD zBxyovzqTd+veZwEvP5zfmrOLtL+HMZBtdbSpZLvPrq3zpe+s/Cms8/NWQHubu2sHaIGct93WdJ 89rL8uO4CCLB X-Google-Smtp-Source: AGHT+IHU4g71VmGhaZj2l7a6OLi55/HQDs+xEbMgzqXKtn47JamJvaAz6y3NloFSZX8Lewoip0P9HA== X-Received: by 2002:a05:6a20:1593:b0:1d7:e76:6040 with SMTP id adf61e73a8af0-1e1dab8d5afmr17463310637.4.1734286365966; Sun, 15 Dec 2024 10:12:45 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:45 -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 v14 06/10] perf record --off-cpu: Disable perf_event's callchain collection Date: Sun, 15 Dec 2024 10:12:16 -0800 Message-ID: <20241215181220.754822-7-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 17:58:21 2025 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DE2E1C07FA; Sun, 15 Dec 2024 18:12:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286369; cv=none; b=ReRnJQQqFaEjVnFaj/PHPkyAoAKqqIBxpN9kXyeNJYxLdeE8VpT4Yowek8T96a5Si3jnh5UZW2UxsWkgsJdPsKBb5yazGEK7IFh+ADt0K1vv3nlvx4S5WgtC1HDv4dpjEyqsh6r0np+p3jW3ZVOnqPKgwTtb4aDfVwXANSR3kQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286369; c=relaxed/simple; bh=jPBZDIKyif/LhOPbutvjTXCG2gTELlQErVsQ7/V8AdY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=imQbj+aP6Te1eiEXQ71o2Yk9G2ZNKfj/maklh4SB1vb5QHra/MVviKHZm4eBcqYpxEJteBLeFe0GAGYHzorXEl2VI565Xw2vNZ1X8gkTJndQZPDoxQMGSC3ncx8Bb7DcgQruKs5rLJY+OdWocRQguspUCn1Y07cG7SuqGxhGpmM= 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=gQV+uLwc; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gQV+uLwc" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-728f1e66418so2762628b3a.2; Sun, 15 Dec 2024 10:12:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286367; x=1734891167; 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=gQV+uLwcnFPDtcDLfR95sVwAzg2iicHBcq5YEM8eItpY72fuPh1PbqZamYRL92iNOb v7Y6RYcvytwEsneWuYBgRZ1TcRNrTuVUDfAdWsGv4BbDqwJa2N+CeytuG84AwDVMtUfl GX/oJDa6eTR2KD4Ot6BQlLz7tjf8Q0cIbIDr6DMJJn3DJ3wPPHYYSZaUf/BdAExe/GvE uGFvc8nSeMAM2615K2hW7KCkSasLvp4Wb7UjtLRH8U8qUCNFutHYdTMhlvwFXO0j3Fre 5T3TRV1d2fWO03OdA9nXuI46DN69ViGszL3cQY4+93ljjCzYmcMJtABOpHMYR3T/rPfR 3MlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286367; x=1734891167; 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=POwEqqhDhO9+YauIyH8v300y/039acsQeyI0P03KI0K1g421PnrcyG7JKTWwBOizZp MI58HYmmKnbHUCieWM1H4fXB5BC4FApoEv4XuRAuQPRWs7wltDB/P6U/JUQx2cS2oIZJ /2b+kvAAYyDE/nDOX3q3jyQckFBCwj1DUsKPjuMc/D4jdK5Izl8PeQ9BIhaN64h/MthU ny81aw5daQpO9qMh51Z6GxwMJm9Nd8pOUsPrJavuX1ZTfki8GJ4lXnvTSC1jhROHTrsY AT1ionHZHRdKAN6UPXdWH/A4Qs1oWV4n+KrbRDk+UDknwRBQ08/dhX4u/nTU8IwYPg7R C7Ww== X-Forwarded-Encrypted: i=1; AJvYcCU7L4Uq572KckHNHks9enkqTILOUC77n4Zh6K/BcsDSkqcBBMjWYg9SZCQzquUSXI+lghOOcbFiKaDJd9YGUR9xlQ==@vger.kernel.org, AJvYcCWp81mnqqWz0VTTaKasFaQF9NyVC46X/WscFOvKBe325pTFOrVh0vE97JLbTYdIYy7MJSzy2JgSf9hVIQQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxJF1oXhdJTRsCnK4WytMa8+3B3VAsOG8W9hHCzukhhTEngqcg9 bfJ4ryfYyvCEDNbx+2YFEXJkYiZPWWzvfTYivCvwBJTdKI4NWFwh X-Gm-Gg: ASbGncu+Lxv7k2vaGYV2SyIO5B5l0XbvXcydJ+ZirKxyxfCFbFDC2jj4NMw7oqEdraK dAMfuoiLU2dn4AgR+gZJl6BGdpV2F27aTplRKQvSrxGaDikPV4n6FmZXhoyXS2AAmQywRzD+1+C puFax/4DZ9z5zNXrZsGMulypAkHPr6aofahG7rbiTdOV4JXhPUvXRHBdWYwJkOYBvPO58/u9SSR Orp6Yxu+jNFrfew3dkhjxJmHmk2ez0IHWi2pj+kJu7oDtUUVw9hh8S7pffrKCeL8LgHdP/017zs OIg4DjvUM4r6 X-Google-Smtp-Source: AGHT+IGOm/4h49+gzRm6sLRpU7pZjbBTQQhRFxCBqA/vtKYUviMTEGq2+ipHTV184q8arJ21vp9veA== X-Received: by 2002:a05:6a00:929b:b0:71e:16b3:e5dc with SMTP id d2e1a72fcca58-7290c25b8cdmr14130520b3a.19.1734286367315; Sun, 15 Dec 2024 10:12:47 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:47 -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 v14 07/10] perf script: Display off-cpu samples correctly Date: Sun, 15 Dec 2024 10:12:17 -0800 Message-ID: <20241215181220.754822-8-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 17:58:21 2025 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.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 5B72B1C3C0B; Sun, 15 Dec 2024 18:12:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286370; cv=none; b=iyZNhyFEZ9W1HP+qF4RX+DgRhlY46pt2nWE6JHbZJlnIw8CSwGiCbaMYKJQHkFP5YjZ1Cgjkl8IVmgJl/NN+e8Ad/fgA7rqw3awFZmTvDfSBikOF3FX6guzo6UvwWkL3cgrI0f9rvfC5kTYY2irwKhB74eVhAk5RhwYutYha2IQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286370; c=relaxed/simple; bh=lwpItHlU96z4UGlv+o5kLmRFuWMmSjHuiiEw79vAX2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ICqEkCWDxyhwQ2Zyb8kBJUOiM3xyoZELD5We1m4/pmU0A0LMyHCSJ5ulkGWZDQNaDmjvYpVGEuTK6OT71/HXzXT46yPhHJLfWab3d7SjJC6uiWNdHBPF5t4d5i7GBv6IFVrCrZl4KrHriQMP3f1feD5B2GJb93qB/Rz7ySUaXYg= 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=NND+8Obn; arc=none smtp.client-ip=209.85.210.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="NND+8Obn" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-728eccf836bso2811818b3a.1; Sun, 15 Dec 2024 10:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286369; x=1734891169; 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=NND+8ObnRqwVIlrktzIJguuhx8V0l5rHmtqeTTssaAxufwyoNkeVkHzbNd0tJVqLd1 SU+o+iBML4UJhfN8ArWblgJK3N5/j5KJGzW8ceQb+FuBUIv0El2WEPZ1iPqnlWbbgI9i u3oVJXc0iSA0JXfl36DJ7jlIav517Q6SKr9GSFdGSpFMI07Q4wX7iZtKxaYkAsWQczcG EoGI7jtZcycB24IxzzMF9aPOOUOQq5EN34/E7kqWHOHifF3aC1wsnJ9JfVGCpyuPkXhT x0IFxSzVvTfJPItbaXvPkhCf6pNGGOda3HNoxk8mFec+AETlCbxBAxGEoQ+sDRZ4ciAe g2Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286369; x=1734891169; 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=Y5qOdTMyJMRuL6w7nnICupPLrtCB1y2/aGvtX/RATH9n7/BXwt7j4MUrMxHRu17mDj Ov+yM2M5w12vLAdn8Pp+nPoSzJpL5E7eVxFwHzfzWJOTRgW8wV2aik44sFX4T0aBiy5n Uod9qI0Jgx7pRVjtINpj3zUTe+fQnMRs/zMZinbow+MJE7SMXoCdUIXd6i7X5V/fzmCH +J6OOwXjNyDk2kCf2qQQTuoGC++lg+vfsklgWrSLsShYsZLW1gYXGTVTWiv6H3odJp6u qjBrLr/qIuAu4UVTZUuCofMHsoM2lJrPolfqY0ZmAwH3H66PZj9HmdonQ5G9Bazg4+ue lkZQ== X-Forwarded-Encrypted: i=1; AJvYcCULzEmI5I/hNe5GibEdfYwx7eHLNCdi03klNPabUBkyPnln6bzXx4jDgCuONV452Zwy9iVHZHUrp9Gj51E=@vger.kernel.org, AJvYcCX3AJCN376/1c05W//a/hbDCHiGwAsUVU+UJ6pYNOjs5Wh567THQ/kiiMwf/6SfICplAThO/5ij1h0LegRj8VXQDw==@vger.kernel.org X-Gm-Message-State: AOJu0YwERtvb8nVEGGqGHXNA9G/Quj2ey71/rCAYXXTwfnU3ldBmu8aH wTX7IWGpqvrYWomnDKNYtISqLN0Rs5EBoBMg+g3HZAWYgb40fT26 X-Gm-Gg: ASbGnct2rJ49FcAJblGATkl4Vp8i51/V8PxIHeyRqzkqEUDmvLI/jhLWvm90+1//ukb I05MYC7VDqKcC+DFSeHhSb8WBE/iVJY74zEbZXypykQi7mUvZXs5jsdCDBTbwaDjX/m5aE2+cjO xyzVdRAe/yxSTTqwJu2wLDXz6y/hVqfrnQ4uKXaZjdud2BtqAZ3WcFqG+gmTK6Rg9pqG33gxmeu ygnxkrBxbDiSqBNMO04zmpUC8X30WQqYL7v5K58gMWPoJQ5ec00yl8ynq4H+lcaV7mtV9Hp8lpz Rr85FCEhKdy0 X-Google-Smtp-Source: AGHT+IGDq+Cl8cPm2InhFiD3r0RmXa5zFSKWx4rdHL4kM/DaplXbXmpnvIJPlGqLGmpG2pdQOrKDwg== X-Received: by 2002:a05:6a00:b88:b0:727:3ccc:25b0 with SMTP id d2e1a72fcca58-7290c25cbf2mr13146523b3a.16.1734286368689; Sun, 15 Dec 2024 10:12:48 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:48 -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 v14 08/10] perf record --off-cpu: Dump the remaining samples in BPF's stack trace map Date: Sun, 15 Dec 2024 10:12:18 -0800 Message-ID: <20241215181220.754822-9-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 17:58:21 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 7F3371C54AB; Sun, 15 Dec 2024 18:12:51 +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=1734286373; cv=none; b=QymenCDDz6ci/2vtmPHw2WgaENmGTyJFIfIzeULRyXJikqFufF/AVSqR3kWaK2yQNuPjxYrNmWaRKC03ESvCqWprj6eBcE+g6ceelhfDQ+gxuJIe9UX6Y3o4SinaPqW19Zq6h6JL49NSTYzSB37GueAfyPkHMAWeF2YhZ/nXRXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286373; c=relaxed/simple; bh=7G9PrbUr4aNDwPyTVcy7eS9sbDpQA/EppNJB+9esUwI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p1eyzVhL76h7zJmc8mKVN9GI7q3Oc9edpYnHP8Fq629Sv3BUcPFxD2DrwrntWVOuDljDlTQpYUPcx22gJzUkFAb7P8y4r472q/KLe7zKpG5SFW4b+hcIFgPB9DeoVF9BaiDspGv7KqY7itzUA4iHQAKp7S8UzAEacf6SYwKpXv0= 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=KTEZV3Xh; 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="KTEZV3Xh" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-725ee6f56b4so2796277b3a.3; Sun, 15 Dec 2024 10:12:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286371; x=1734891171; 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=nGmz2xOkFY8ET+RhhuhkkszvozZPVmg0sg4UI4nprWU=; b=KTEZV3Xh2OHMKO6gpmu5wKzl9z/xhxwahOHlWmwqaXi1XbWU6zBAX4MIX1c0hj3soR McisNiXMExFbB4tHm7sR7nrfVPxFBhhpVOOqegfiVPR4ykUmAe8TJiV+hyNZqF7BFqKe fTSWucgkc/VLOTj6V+eaGZWncxdDUrNVSpVPzllAMNt3ZRnmpGcPzoZ33WpIweks4QzZ L49w5A0LCNqhNWRBxOoo9UuEMAnhyxxBmsHuldqcRxjHhx4UyOycB0bkfES5Vr7iMhvJ grutr3qTs5T4thDs/2WHjqYSD9L6nyfKdZj8152x93rXsbCgyKSB1CyLwt4827OH84qs b8UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286371; x=1734891171; 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=nGmz2xOkFY8ET+RhhuhkkszvozZPVmg0sg4UI4nprWU=; b=aImsR2hCE5uKvGainIInSexQbDTG0kpRk/9hHUqE1MibAX4NCTkO9sACjnoxAtK3xg Bcp7yj54wETqGUpt0uC31V6e1afX9YfrrVyl2V/xm3zl5rS3o7O2aYHSv3Y/+1wgEREv ZPb9Ct9VNHJ0Wzxqll+lsoTXybodXf1Wg7hv+NjxVqOZ9WByvBXEKNtMLLPQVEeKknMl UHmP+s/tzfBBvV5Id7j4AfT7v/w82NiHu1ITABv7QMog+uJnEdo+kyScBfLt6ww+/t5n /+KuYvqU4gBhWulhkARhnJMbjPdVmtebBvXyJbcCUFoBHr2ZVgwvxbNrYe7y3hrjBWii j5iw== X-Forwarded-Encrypted: i=1; AJvYcCVaSTfKqzy+MWlb1/6NnHgxzPtF8DVgdPhkvL6+DB6/9E1B4gqa59hQoIL35osAeEN2OkP2ZCqCOO2+XhU=@vger.kernel.org, AJvYcCXDbbfndKhcMsq9BF+wdxgwCVBi4+KU5NPrAYhAoPjjp67OV5WiAJgW8HXLh4qNgCI33xFq5uVUe/OmjcrGjhcaoQ==@vger.kernel.org X-Gm-Message-State: AOJu0YxlzrSo0ZuSDWdO+bBN79VneJHx2O9X0QZHqPOOcUdUzMGMxUby 3zQfXBmWs5vLg+IH8cu555xYCtDY4AZDBNv+SctPyijo6vsdbkaP X-Gm-Gg: ASbGnctDANkuP6mrBbI/ljbqNknpaGRaJiJNXNAsqjObbeSYDQ5ve+QFWORsTUnlfFV uG9yoO51mboJKnGDzjHCtqb0eEsBk0WrNINAjfuoArrz2NjwJ635fVeam5SbiEZQUXWa9zgZxPG Jj3xdBKryJXF/THtmFFbb3asYQ7OOrdyklx+sznmStQx0qVaI61iZa6rJ/NDGfaNM4VPR+Cem22 yM51l2k1DdHTeiKWhZmKjW51FLtMlxFYHceOe+7VblrmxBzY2BnLZus1G7SHE1M6WZNATreysCi 6djvHkzWkJqL X-Google-Smtp-Source: AGHT+IFHPDLKnXBjm1jwBun1/mtzQW/FIV7eGg7vpemqQ8OsUQ6MbQdVX3YTSUGCBgme0B1tNyjiuQ== X-Received: by 2002:a05:6a20:a104:b0:1e0:d123:7166 with SMTP id adf61e73a8af0-1e1dfd97ac7mr14511653637.14.1734286370853; Sun, 15 Dec 2024 10:12:50 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:50 -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 v14 09/10] perf record --off-cpu: Add --off-cpu-thresh option Date: Sun, 15 Dec 2024 10:12:19 -0800 Message-ID: <20241215181220.754822-10-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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..dca343b78825 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; + + 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_ns =3D off_cpu_thresh_ms * NSEC_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_ns =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..188cbb051470 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_ns; + 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..64bf763ddf50 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 500000000ULL =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..c82db4833b0a 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_ns; }; =20 extern const char * const *record_usage; --=20 2.43.0 From nobody Wed Dec 17 17:58:21 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 4A5631C82E2; Sun, 15 Dec 2024 18:12:53 +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=1734286374; cv=none; b=j3Sm4hIBEGPHYcd8GdjuRjsDbUkRdSXXsUiiv/FBQhSGVnWpvyoY2sXff/DWbJZMr3/PEk+Fh0RSHWzvFo/Lt+ffY6cU4yPWihz4iBUoZT3awOY34luOq9jMBDBJp7ZxQhYPFI1M/AyzsXq92+rSWkvRJneLDABytJ7FV5n0VPU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734286374; c=relaxed/simple; bh=4WAIAiXBlkGgyD9H4cZ0yDqhMm/Or5jQiLufWPGrXcQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gO6wqtcRTa5D/maNZLCXHUHHUPECGZW3xDcuu/kEY16LDWQYw4fC+CCLvH0PD3gXeNLKA+pv5UIlsZfyXFjY+cdEMian7WDjnPu/2/aWzpEcGP9fGyQRZ+89kC27iJor2gXeRrYgL3tUekdFk/BApWU3vilxFXDDHSf2OwtQaM0= 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=aiWN/AMd; 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="aiWN/AMd" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7242f559a9fso4250596b3a.1; Sun, 15 Dec 2024 10:12:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734286372; x=1734891172; 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=/qu3/uFK5/Qyd9VqlUJrIK5ot4oLtLxNVYvtxgrzh1U=; b=aiWN/AMdY5CcdBzGEi8UTvTTUE7KguhBX0fYvPZOMiH+j3iB78PtKmMZzhib0/ECgg R8bRExK/F/sCe5We6XaVAkJt0+g9dQ8XBfK1MAmLVpF7TbbVBcppe1u55DOcxWLhW8FX YpzDRxi9+pQ/xRSJ9JdQ2QwVl9w7DqbuqV++faJCQcnerhLbzwwmMyzKNdeXqqSc8NJr 7rS6HbJC8Ny1U7zcl6eA9mpUJiFr8oVlXCIsJYsCEjE/d0J9TBF1j32wVhM0Efq7VRYz Qy7Fen0yBGg0WS/PtDXFGqIfK4zxpgvdO8LQ7T2f0UuaxPbh+U7axRbiezZfPGUTTPSq LjoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734286372; x=1734891172; 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=/qu3/uFK5/Qyd9VqlUJrIK5ot4oLtLxNVYvtxgrzh1U=; b=EBVOrsODEGq4E41yirI2eby2TBCo+yqkaSp1cwZOj7T8rdZ7IBiHG7T7/cCV6iBWe6 LgaVDwrhWCm0Ad3AnIQuFr/UEXL6r4BgK7tBVJ6WNbWUL7zEFswso2lgzvVslfG1Oqwp jgcT7MpBGNpmOU9MQsnWsRw+3XCSqDXlgzYwye1rP51jMPLch8CEvdc/0/2NXiRmO7mt Bmp5dATMJRqVr1FAYFBzWWEvrGUX/n62ox84SeApTvRXKrva7cnZ04/evdsOvzGT8sVg cBXNccFkwqYwjInVaPJu6FXudWew+W9g9YOXf++JvcX2VZQv9lMxekLYK//MqagRphSA mUzg== X-Forwarded-Encrypted: i=1; AJvYcCXD55ifepLjIBLIXOOyoqrW2GJqpuLG74OJKxvSBzVAY2B60RZ6v7yMUrF4SMMK8WiXbovgSPg66ak2OYQ=@vger.kernel.org, AJvYcCXiyfcculhvZV0DacjNeaFM16yZQ9C6MomdE6/sY7zisGQ9Vys0rdueOy16fBlQnH4Gsw8eh9sZhrZGVLsv/vU26w==@vger.kernel.org X-Gm-Message-State: AOJu0YxSa+MVtJ89SMfdgsSuEoHbLKyJi+m+RWXW/518I+xm/BD7ysOt uvDNS9dQbN6pkbWZCV8igTyKWsU01S/rGuqVQ4muZGoGy5FUIIzA X-Gm-Gg: ASbGncvjzTVITnUknhZcaaGNSQq3dezvZLM8JJhTUZ8Vq4+GOUoQrqXo6wx5DGR6nhR uL77/Eaj1ur7WGAgG4ZyOlwR1IaAhUF4iK1CJlO84+3RTOJ6lVSYvIc0y1RTKQ07NkfLQEPJAy9 E/EWtHvQyXczK2UJ+Gnb1XVJr0OdI72j+jHXyqRR8SirFVc0dNrk4Apbs4lcVF+u2vaUJ8ob2T5 jJe0eaP7YXRn0R8n4+68y7HVQO+5P7Nc8yA/2ykYFktVmskwWT0OpQ7QYEQqfEVMYV1ZT0yGmYq Azae+KDigfGf X-Google-Smtp-Source: AGHT+IFrsN7TLzP3bPBN6JtqJa8ARsNkgxktZhcFQzRiiGDau6PYTZ+r7x/S1oalrZm+9Ho9fmTmkQ== X-Received: by 2002:a05:6a00:2e0f:b0:728:e182:a8b with SMTP id d2e1a72fcca58-7290c15eab3mr13924977b3a.9.1734286372576; Sun, 15 Dec 2024 10:12:52 -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-801d5a90ba0sm2808120a12.16.2024.12.15.10.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2024 10:12:51 -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 v14 10/10] perf test: Add direct off-cpu test Date: Sun, 15 Dec 2024 10:12:20 -0800 Message-ID: <20241215181220.754822-11-howardchu95@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241215181220.754822-1-howardchu95@gmail.com> References: <20241215181220.754822-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 Reviewed-by: Namhyung Kim --- 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 a5b9ccd0033a..261325a1088b 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