From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (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 564DA3DB96B for ; Tue, 16 Jun 2026 06:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781590470; cv=none; b=ueFMJBn1VYY/nApgnoyABDMnGmIwV+0YXrR+C4zIEYTymPVxcJ2/mV75vataRsAuIxEi5OVDwYEg3FHR83UAUC4QQtYE96/sPReRdc1UC/DMTBiLqEuZ4vTwqTo3//68vUjlFkY52ZyC6GpFGIEo9xeUtSmNWRn0EHess9DxAeU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781590470; c=relaxed/simple; bh=AdmxAXzdZLbjPa1i4fpe7i1x5C5ut9Rnz2StLUgq0rk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hH31d2PlnW3TG1/fcA78fUSA4p5nNV7t8j+JQhn5Z7+jFPVLCl6L8ZOK4YRHSTG0AwVdQHq3hDD87D4FxMzYz0GAGmXR02sLVgPSFlcX/PzzrSq8UnMtQW7EEya9Mh4IqlYFU286v+yO0UScWzvNqBJUMVzL0GIiOUXbH0EvR04= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aoFdWepG; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aoFdWepG" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-308004a2c49so1141333eec.0 for ; Mon, 15 Jun 2026 23:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781590464; x=1782195264; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x29faCUZFXFl+CqH5OTbtaGw4Ap1z6lY226uBfjLrzw=; b=aoFdWepG0Gtpo/9DvF9ncSMJZRdxU2Mc5blboJQvCI0+AF3Fqn0jpOWE42JGscCHUM icnIWaOEel9BKoZ5a91HdHvPCE+G4NDuh5KzRMVp9/XeQssQwuuXup0kd2mnmv3r+Xe5 KQWc9ol5HKihx0X/VN4J+MUyJjSP1ej2hjLGAFDOXYG+Gw0gcG2wPiqCXAJZWupd14VG i2B5alMe/TMNaCQ58CPv295Uw5LgH6Nv9fQKegGLXLv1PaBlZdMrel8SFKuClHe9w5sD lwqSHiywxnBDosNnn1t3gb5HKs5LLv7BkNbrl9XaihI46ycRzxzpYbJkENtVfLsJ24WT d7cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781590464; x=1782195264; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x29faCUZFXFl+CqH5OTbtaGw4Ap1z6lY226uBfjLrzw=; b=GkSmCHb2y9VAur77iV3rxDq3NoFQXv8u+w8aOct4f2BqemYSM+K2g/sHN2JghWCXw0 BWIh2n/AclNeweIPfclePctdO9DPd2wJv5Ouc0IbW+qBI+ucZShqUtDbOxCSjjmjMr1b sMAeQV79BWkCz/6Hf8vuI/7ZE1JJInCQjiJvPOIlqCvJs+IXcxbZ5HiI2xATza9nA1tJ pcbjat802RepWzA2R2AOfCRzn843ew3ahB60Xb6uMsB2JYPMeqt2wYEnTQUqL+OuP25t XKgypF1or79yEcXmuK3L0HlNh4pQAgeOCmp/ycAEwPxvqu5dvga2TKDqGgWs3VZkbD2I tINA== X-Forwarded-Encrypted: i=1; AFNElJ96i1CyD45BAKlnZP1HcsT+tl6bPpq5AalQBm03bgKrBUDOy/ZYCfIwqr2Wfzag69p7/odvPkzvw7rpi+I=@vger.kernel.org X-Gm-Message-State: AOJu0YxqSLJ6Mwa1/b7G6NXqU/UxkM/3VopsQDMxm0Y2oMu/AFmU+Kz4 naE1fdPIDle2e4F4T/skWI1MLBDXLea4sTN8lOpNpY96uUk2Ou7mBsjGB/uz717rDkjuXpjA3DZ EBns0DVH8hA== X-Received: from dybmj14.prod.google.com ([2002:a05:7301:e0e:b0:304:dbcd:d155]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:8623:b0:2de:c5ca:c1f3 with SMTP id 5a478bee46e88-30ba5912e66mr1318069eec.4.1781590464067; Mon, 15 Jun 2026 23:14:24 -0700 (PDT) Date: Mon, 15 Jun 2026 23:14:00 -0700 In-Reply-To: <20260616061404.41929-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260616012744.4049193-1-irogers@google.com> <20260616061404.41929-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260616061404.41929-9-irogers@google.com> Subject: [PATCH v2 08/12] perf tests: Fix flakiness in BPF counters test on hybrid systems From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, leo.yan@arm.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, thomas.falcon@intel.com, tmricht@linux.ibm.com Content-Type: text/plain; charset="UTF-8" The `perf stat --bpf-counters test` fails intermittently on hybrid architectures or systems with dynamic frequency scaling (DVFS). This happens because the test workload (`sqrtloop`) runs for a fixed 1-second duration, and the CPU frequency can scale dynamically between idle and maximum frequency. As the first run runs on a cold CPU and the second run runs on a warmed-up CPU (or vice versa), the number of instructions executed in 1 second differs by up to 2.2x, violating the comparison tolerance. Also, when running as root, BPF tracepoints and scheduling programs trigger frequently. Since standard `perf stat -e instructions` measures both user and kernel space instructions, it counts BPF helper and program execution overheads, whereas the BPF counters themselves do not self-measure. This introduces a large kernel-space instruction count discrepancy between standard and BPF counters. Fix these issues by: 1. Switching the workload to a strictly deterministic, iteration-based workload: `awk 'BEGIN { for (i=0; i<10000000; i++) sum+=i }'`. We pin the workload to a single random allowed CPU using `taskset -c $CPU` via a bash array. 2. Restricting the counted event to user-space only (`instructions:u` or `/u`). 3. Tightening the comparison tolerance from 20% to 15%. These modifications isolate the measurements to user-space instructions of the deterministic loop, which executes a virtually identical number of instructions on both runs (with less than 0.001% variation), eliminating Dynamic Frequency Scaling (DVFS), kernel scheduling noise, and BPF helper self-measurement overheads. Fixes: 2c0cb9f56020 ("perf test: Add a shell test for 'perf stat --bpf-counters' new option") Signed-off-by: Ian Rogers --- tools/perf/tests/shell/stat_bpf_counters.sh | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tools/perf/tests/shell/stat_bpf_counters.sh b/tools/perf/tests/shell/stat_bpf_counters.sh index 35463358b273..11de77ee38ad 100755 --- a/tools/perf/tests/shell/stat_bpf_counters.sh +++ b/tools/perf/tests/shell/stat_bpf_counters.sh @@ -4,21 +4,26 @@ set -e -workload="perf test -w sqrtloop" +# Get the first allowed CPU +CPU=$(taskset -c -p $$ | awk -F': ' '{print $2}' | awk -F'[,-]' '{print $1}') +if [ -z "$CPU" ]; then + CPU=0 +fi +workload=(taskset -c "$CPU" awk 'BEGIN { for (i=0; i<10000000; i++) sum+=i }') -# check whether $2 is within +/- 20% of $1 +# check whether $2 is within +/- 15% of $1 compare_number() { first_num=$1 second_num=$2 - # upper bound is first_num * 120% - upper=$(expr $first_num + $first_num / 5 ) - # lower bound is first_num * 80% - lower=$(expr $first_num - $first_num / 5 ) + # upper bound is first_num * 115% + upper=$(expr $first_num + $first_num / 20 \* 3 ) + # lower bound is first_num * 85% + lower=$(expr $first_num - $first_num / 20 \* 3 ) if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then - echo "The difference between $first_num and $second_num are greater than 20%." + echo "The difference between $first_num and $second_num are greater than 15%." exit 1 fi } @@ -41,11 +46,12 @@ check_counts() test_bpf_counters() { printf "Testing --bpf-counters " - base_instructions=$(perf stat --no-big-num -e instructions -- $workload 2>&1 | \ + base_instructions=$(perf stat --no-big-num -e instructions:u -- "${workload[@]}" 2>&1 | \ awk -v i=0 -v c=0 '/instructions/ { \ if ($1 != " 0) printf "%.0f", c; else print "&1 | \ + bpf_instructions=$(perf stat --no-big-num --bpf-counters -e instructions:u \ + -- "${workload[@]}" 2>&1 | \ awk -v i=0 -v c=0 '/instructions/ { \ if ($1 != " 0) printf "%.0f", c; else print "&1) + stat_output=$(perf stat --no-big-num \ + -e instructions/name=base_instructions/u,instructions/name=bpf_instructions/bu \ + -- "${workload[@]}" 2>&1) base_instructions=$(echo "$stat_output"| \ awk -v i=0 -v c=0 '/base_instructions/ { \ if ($1 != "