From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 CAC5B12E1EE; Wed, 7 Aug 2024 15:39:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045144; cv=none; b=HFpOfvwP+GTpGRe6vYCgrkEF5N9+KFXLLwvOGi4g4ZQLTKg+ZXgpa6qY72WT+Fm4nyRIpFvNsKAzradsjwN0stYRv6BJGVd5EoHiJRkDBqSEo3DgBAAybcxCIuvO1lADe1z8+Em64MaUruC7G37qC8SkAtS4TS1VuiOqbbs++ZU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045144; c=relaxed/simple; bh=Wkz42sdxI/Avx1F0A+cttY5SSLGUofHxTrgXxVRGSD4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=izVYUnCpMT31qtw7BBwVWfHxeT1nbG6QVMzqzJhcTR97SWZxBScfS+ywiEBlZpoETZa3qo/m8vNfRTzcAHiLBt2MlIVgXOCr5olggQPENE82PemN1rVxdH1XnOmKYmOIB0B68g+ywl2l1WdUUPUFqD3J6/+WstbbOohyWAsrC3w= 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=LKzkdsdI; arc=none smtp.client-ip=209.85.215.175 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="LKzkdsdI" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7b0c9bbddb4so1524994a12.3; Wed, 07 Aug 2024 08:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045142; x=1723649942; 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=r6T4Ucops/gCfHJjCk9BfPYNLoz2EpIJyrtmSqai960=; b=LKzkdsdIJbro/EeoE+BaBUBSmSzGrJedvfv4vjYuKiOiupY5cDY6Lzz2Y/tNKABsxY iaJ4/Ysfx9jYAA1QdqL4fqiCnlOgbZxqq58UIDAKsExohsJIrK1MdDD6MaUTEcPScajI /hpocI/01WeintbHbvTwChRtdXBQ2P+ZgXb3CMKFmLNFTrG1KGBjZdDKlMsLfVHiW65m WGJ85FxdCf+pJRW77u3qVoNZWQ5FqDAs7IF2Tba2xzzu0e9pg1Ycs20siNQauRzdoG8j kzq3r2zw1keM072WRxOdbr5yQyBGdsgTbWxaV8rXYlrzqmIh1gOTl1rEwjW8R3dhTUTc ItFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045142; x=1723649942; 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=r6T4Ucops/gCfHJjCk9BfPYNLoz2EpIJyrtmSqai960=; b=Enr31Stb4OQH9K6cCyFFGiSkTVSw3MDhhV7FOYZ8eItAJ/1xF4HIolJdeJkWhNDv2T WT11QfgIWyUDyGDqiWkZNw2If0m6cmvsBeI4LOGguS+JnjmPI+xt0gwtLb/PqePwAUxj +p6ndDBcw6A8LF5eatjZpcBp9P+DlDwShLZMtDnntF/guOT8URL8Fsh2FWIVOgFJ+OxP kXsgOXy9zyO6J3pe24UfEnwFpvOLB19nuI/GyblDa3sYHM2j9T2WEOKozcEhlIfRmtQZ g9LR4PdkCS60eM+5+XAv9339sSlLLoxocfXyl38VwlhYdJhD0stA99BqgFPcT9lT+rm1 WYQg== X-Forwarded-Encrypted: i=1; AJvYcCVnGH/uFzivjRFaZunRm8DFsTNV7uA55Auc4jnjytIVU2Oz8EwHMnQn4lDu2hez6b6cZqrH1VDtDLjIAsO+8YwH4AAbv2vJlzyCNWQZwF10AG9NOauxDoCEsgU3ccuUoV93qPCnzuEVQ8J8yz57fA== X-Gm-Message-State: AOJu0YxAhqzS/tRy9txNKhgXiiYqC/ECYPoWLrrpyShFC7up4/QCrBY3 EfxDDPCLrirzbqf9uwUE7agCPYbKiAbJije0vC5tS+FOFUhA16Sz X-Google-Smtp-Source: AGHT+IGmAayc1r5VOLfqJ0tm2v7PNHU4D1mnrLOlMkzDjp4EmUClsXfj/xugr8d9XTnFhmOmcdAmBg== X-Received: by 2002:a05:6a21:3406:b0:1c4:afec:a7a7 with SMTP id adf61e73a8af0-1c69957a8e2mr18153974637.12.1723045138409; Wed, 07 Aug 2024 08:38:58 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:38:58 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/9] perf evsel: Set BPF output to system-wide Date: Wed, 7 Aug 2024 23:38:35 +0800 Message-ID: <20240807153843.3231451-2-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pid =3D -1 for bpf-output event. This makes perf record -p --off-cpu work. Otherwise bpf-output cannot be collected. Signed-off-by: Howard Chu --- tools/perf/util/evsel.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index bc603193c477..b961467133cf 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2282,6 +2282,10 @@ static int evsel__open_cpu(struct evsel *evsel, stru= ct perf_cpu_map *cpus, =20 test_attr__ready(); =20 + /* BPF output event can only be system-wide */ + if (evsel__is_bpf_output(evsel)) + pid =3D -1; + /* Debug message used by test scripts */ pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags = %#lx", pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags); --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.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 EAF9812C549; Wed, 7 Aug 2024 15:39:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045148; cv=none; b=kB3+1ZJrw6cgBe09lqKHjEQZdXTaM7oTGl+G6dZTM6XUlFaC8/5bztKRMcHFble2JJ4PUV+REpoPP6YKFKt9mRnOZbM8f6Z1v9G6/OS8fydzahVP8yMa+SDTQhKmH2hF5imgzF9z63Rb2EdoDR4JeFj9pbl02cu0uzrSyS9xB7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045148; c=relaxed/simple; bh=cMY9Ig50Ah0NrntV3IC9Hc0Iik17OIwSKC/iyfTHx74=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qqgHKKSlofLuL8YQ9vkq30tgNDl5mQCDQslVrTcFwSS4xxV0GvBg8BX1RWbH4P1glgRSB1Red3bTuat3njDVPen3UtN/mgmGmX+CtoCZyh84AxtcpKmWqDWfIvgiQQ+LL+qi9aKt37XC2eibDvGhd6iYWMaawAYo1Rcb8FU2Vpk= 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=FOZ5XuGr; arc=none smtp.client-ip=209.85.214.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="FOZ5XuGr" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1fc491f9b55so331485ad.3; Wed, 07 Aug 2024 08:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045146; x=1723649946; 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=4tZ51s1InZzXSfbxVagPKImUEHCYLFou2I7mGe5EjcQ=; b=FOZ5XuGrhmAKgxMGwktVO9vSQzlCOag6u0Y5BTUJnqkKyycAkN9QxmhOi+CpS+57s0 jj6AIXGpuInS6zH50B3cR0RzLivcg829CiRguD/RbdNz2+1laCmDt8tjktqFcqyFEzVt 1NfHdx66fECFDErlmzsdyBg3HL+hKRKGptajqqXHKLMpTAEpi0xz1IzZ0lhQ2RAGqvNG eoAmguTKJ0riwaMrpT/KtJj6zyk0zxPdmgKFoP3UQNWKkLQyX7362P+r2udBDk+rKUga h7IxgpwaNtqWsTB4OzZYum8m5W5QMz8Il56hT8fxkpCmv6KbtyrJOI7Wmq6C/Q5RNxs5 ihxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045146; x=1723649946; 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=4tZ51s1InZzXSfbxVagPKImUEHCYLFou2I7mGe5EjcQ=; b=Oc8PKD1cBIA4jCvkT0gNMCohkL7kbwR27Ed6hRq1Xr37Gm/ciDUiXSQsBJgckCjtHJ ZR/axG8sjJ+RplbdkRajnibbQ/w9u7+LNf/29oWn1kmm+sfuskbnxnqrdJuigLDMpzic /FZBGdla5TvvHtr7ogaGKY6W+VifDYJ0Z/rx/gpHDeTqxx8ohx2X9A4rQ47dUdFPLrdi MWcQZ3OxZSzu0/wUw1BdnSm9YdMGw0iB1qbfx0AW4epMFcl3Av1C3nppNaRuxrbStbNg m69swpVU5h5BFpYV/gEu6A3F32ziD45zjx0hBRZMGWQdsmHq0q13sEHZKRLgmEeL9P6/ olog== X-Forwarded-Encrypted: i=1; AJvYcCUJf9PNQljXpqcU9w/0pqo4SDmRqgKAaBQXQteTMkSOMefQ5DJeechvJbn6VlWwTIH1IwbHMi8G9161yqeu/jG7bv3wf9wjikTmLoDyc2ZiNSEsYHm+JFP66/l/Nx91QUqq0yVJ9+STtCqm2YWiZg== X-Gm-Message-State: AOJu0Yzmp21XJF/ywwUSXEhjKCzsRIRmh9lQoAMlUGDv4acW7mXCRiF/ Izs/+Dn544thSrVypvIbc6+IHM6g1x68SZxVt0awcB+1CdprEKZy X-Google-Smtp-Source: AGHT+IFpNk5jpD/lueIdcpc/BMbJag2YHK7eOJM3LxEZJwR71i4rba15G7VdQdcVt0U3lsrpAp249A== X-Received: by 2002:a17:902:ecd2:b0:1fd:93d2:fb94 with SMTP id d9443c01a7336-1ff573cc1f2mr195280415ad.48.1723045146188; Wed, 07 Aug 2024 08:39:06 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:05 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/9] perf record --off-cpu: Add --off-cpu-thresh Date: Wed, 7 Aug 2024 23:38:36 +0800 Message-ID: <20240807153843.3231451-3-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the --off-cpu-thresh argument to specify the off-cpu time threshold. If the off-cpu time exceeds this threshold, dump the off-cpu data directly. Suggested-by: Ian Rogers Signed-off-by: Howard Chu --- tools/perf/builtin-record.c | 26 ++++++++++++++++++++++++++ tools/perf/util/bpf_off_cpu.c | 2 ++ tools/perf/util/bpf_skel/off_cpu.bpf.c | 2 ++ tools/perf/util/record.h | 1 + 4 files changed, 31 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 72345d1e54b0..60c6fe7b4804 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3133,6 +3133,28 @@ static int record__parse_mmap_pages(const struct opt= ion *opt, return ret; } =20 +static int record__parse_off_cpu_thresh(const struct option *opt, + const char *str, + int unset __maybe_unused) +{ + struct record_opts *opts =3D opt->value; + char *endptr; + u64 off_cpu_thresh; + + if (!str) + return -EINVAL; + + off_cpu_thresh =3D strtoul(str, &endptr, 10); + + /* threshold isn't string "0", yet strtoull() returns 0, parsing failed. = */ + if (*endptr || (off_cpu_thresh =3D=3D 0 && strcmp(str, "0"))) + return -EINVAL; + else + opts->off_cpu_thresh =3D off_cpu_thresh; + + return 0; +} + void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __ma= ybe_unused) { } @@ -3326,6 +3348,7 @@ static struct record record =3D { .ctl_fd =3D -1, .ctl_fd_ack =3D -1, .synth =3D PERF_SYNTH_ALL, + .off_cpu_thresh =3D OFF_CPU_THRESH_DEFAULT, }, .tool =3D { .sample =3D process_sample_event, @@ -3560,6 +3583,9 @@ static struct option __record_options[] =3D { OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), OPT_STRING(0, "setup-filter", &record.filter_action, "pin|unpin", "BPF filter action"), + OPT_CALLBACK(0, "off-cpu-thresh", &record.opts, "us", + "Dump off-cpu samples if off-cpu time reaches this threshold. The u= nit is microseconds. (default: 500000)", + record__parse_off_cpu_thresh), OPT_END() }; =20 diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 6af36142dc5a..1e0e454bfb5e 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -272,6 +272,8 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } } =20 + skel->bss->offcpu_thresh =3D opts->off_cpu_thresh * 1000ull; + 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 d877a0a9731f..cca1b6990a57 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -96,6 +96,8 @@ const volatile bool uses_cgroup_v1 =3D false; =20 int perf_subsys_id =3D -1; =20 +__u64 sample_id, sample_type, offcpu_thresh; + /* * Old kernel used to call it task_struct->state and now it's '__state'. * Use BPF CO-RE "ignored suffix rule" to deal with it like below: diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index a6566134e09e..3c11416e6627 100644 --- a/tools/perf/util/record.h +++ b/tools/perf/util/record.h @@ -79,6 +79,7 @@ struct record_opts { int synth; int threads_spec; const char *threads_user_spec; + u64 off_cpu_thresh; }; =20 extern const char * const *record_usage; --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.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 2770913A276; Wed, 7 Aug 2024 15:39:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045152; cv=none; b=hztmzz6hcVCGsvvmMqdMayqnPzNmJd0Kp5gNlf99jEyBHuzffLR0dvHE8OP3yL1NOFYJ6JP3TW8NbNiOtl65eQOp8Sf1lLMuS4xrtQ2RQR9X8EW5713AF+i+0lVnNwLn+6nfUuKoCvnZyCAsBC0S+qI/ojwtTU5Py+JfblvjIZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045152; c=relaxed/simple; bh=POqXyFIlz2EObunHi26K+syLYamY1BPxYt+X4L0suqY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iVVD1cBBAkW4yZiiYaT5Ajllpx+RAJsST4nXB51Spf7xfmld9b1Z6w7wlKjDF4hTNY4N4icHkrJlPn0eQWWYbo6RQLAEQSFbJhUowU6NTmib9DzI5clNzgMHieLa2kB8j4WBN16WBKG487ibhhdVQkSPApWhZSxF/uoRaQRk0Jc= 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=eWIc0zn5; arc=none smtp.client-ip=209.85.214.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="eWIc0zn5" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1fc66fc35f2so8061385ad.0; Wed, 07 Aug 2024 08:39:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045150; x=1723649950; 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=FB1CGIEHJSZZ9KdFY8HItW4iTLpXpK/3aHFpRN33YZo=; b=eWIc0zn5Xg0b88vyCKqUlygoAT8s4p5hAIsKDfIZpfWGNd9ef/bktb7f0ZaSPdx1m2 ot6uxjqdsNbohLvrtJzNAdTDbYgl33g5lGb7sjgOHlKuan5Bu6akQpl03qb0rjEyKJ66 2k/2LrO5juEqHJEiZoSQISO4kInlyuasIfbm7VIR2sbM/XPXfRL04HDIqaRzQA2MyutO c/HjZNL9uwYDsYBTNG/R3oSdFE4TSmD2HaXPkfzruPg64zZG2+4MDzYlzKqspuS+1UXt a5zJ+N77phXW6oi5eWs52ZQsl2sdpzSfrRa0LwJSLNFH/RLIF9EJFTyvEzqc10kCz98l 5LVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045150; x=1723649950; 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=FB1CGIEHJSZZ9KdFY8HItW4iTLpXpK/3aHFpRN33YZo=; b=pvrBbuW68qwgEMIIJZ6N+UxEE4tmmFjm0Jcp1GqeTiBjLzwZXOklsId0JBjZruCd/F 0QKYdMCedZd1LfxwPGQaU0C7uhKPpBiJper7Iv9Rvzitgq7dwnkYOpqcR+7THMR+9C2b PtkJo0QAo/fwnxtJvxj0jHparAXyA30CgqrLvKgwsKg8yYDxVq+0iUtKzbayqMFXG7uT uErIdmP+bbZuz3TzxhHuVOJIKXnyLVvT6o5w5DgVjZMoXsPMFArGJ8SEfbLTCeLA140p fddiocDlp1+i5IuVYuTO9tGfyLhStCVi2KPd37y8ka1rT1DAnoUUkvK/ltcnIKz/AdP2 IJjw== X-Forwarded-Encrypted: i=1; AJvYcCWzznJF4J3Z9rAzowjFxUC2JSdUpHdT4tz7SH4Qi7ZfGNliJk8rn8jNgn8U380aHRSaOZqxeSPBIbtPhuCRJGkHcMHQvwPGTvpCWeu96BoLXgxTugRpZU+fdPtQTov0nZHqJZUHg3AJim3kyldaIQ== X-Gm-Message-State: AOJu0YyuLLHUQAgVycsebWFBKKQLkV44KXaKX61j3PkOJfeZsZfXZsep 0XNoJif9VIof/lh4lD/sPBxbaJXSjd5K3W5ZJY2heHewZXdq9VkO X-Google-Smtp-Source: AGHT+IFBc95ZWZ1Xhdvrudq6BQGqUmBmqgVCrN8NZXvJHKDm3s4BeoPGn7Y9inAQ1Fj8xVrV5RrF4A== X-Received: by 2002:a17:902:ced1:b0:1fc:54c4:61a7 with SMTP id d9443c01a7336-2008552d025mr37190455ad.23.1723045150308; Wed, 07 Aug 2024 08:39:10 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:10 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/9] perf record --off-cpu: Parse offcpu-time event Date: Wed, 7 Aug 2024 23:38:37 +0800 Message-ID: <20240807153843.3231451-4-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Parse offcpu-time event using parse_event, in off_cpu_start(), write evlist fds got from evlist__open() to perf_event_array BPF map. Signed-off-by: Howard Chu --- tools/perf/util/bpf_off_cpu.c | 55 ++++++++++++++++++++--------------- tools/perf/util/evsel.c | 2 +- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 1e0e454bfb5e..fae0bb8aaa13 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 @@ -38,39 +39,24 @@ union off_cpu_data { =20 static int off_cpu_config(struct evlist *evlist) { - struct evsel *evsel; - struct perf_event_attr attr =3D { - .type =3D PERF_TYPE_SOFTWARE, - .config =3D PERF_COUNT_SW_BPF_OUTPUT, - .size =3D sizeof(attr), /* to capture ABI version */ - }; - char *evname =3D strdup(OFFCPU_EVENT); + char off_cpu_event[64]; =20 - if (evname =3D=3D NULL) - return -ENOMEM; - - evsel =3D evsel__new(&attr); - if (!evsel) { - free(evname); - return -ENOMEM; + /* after parsing off-cpu event, we'll specify its sample_type in evsel__c= onfig() */ + scnprintf(off_cpu_event, sizeof(off_cpu_event), "bpf-output/no-inherit=3D= 1,name=3D%s/", OFFCPU_EVENT); + if (parse_event(evlist, off_cpu_event)) { + pr_err("Failed to open off-cpu event\n"); + return -1; } =20 - evsel->core.attr.freq =3D 1; - evsel->core.attr.sample_period =3D 1; - /* off-cpu analysis depends on stack trace */ - evsel->core.attr.sample_type =3D PERF_SAMPLE_CALLCHAIN; - - evlist__add(evlist, evsel); - - free(evsel->name); - evsel->name =3D evname; - return 0; } =20 static void off_cpu_start(void *arg) { struct evlist *evlist =3D arg; + struct evsel *evsel; + struct perf_cpu pcpu; + int i, err; =20 /* update task filter for the given workload */ if (!skel->bss->has_cpu && !skel->bss->has_task && @@ -86,6 +72,27 @@ static void off_cpu_start(void *arg) bpf_map_update_elem(fd, &pid, &val, BPF_ANY); } =20 + /* sample id and fds in BPF's perf_event_array can only be set after reco= rd__open() */ + evsel =3D evlist__find_evsel_by_str(evlist, OFFCPU_EVENT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT); + return; + } + + if (evsel->core.id) + skel->bss->sample_id =3D evsel->core.id[0]; + + perf_cpu_map__for_each_cpu(pcpu, i, evsel->core.cpus) { + err =3D bpf_map__update_elem(skel->maps.offcpu_output, + &pcpu.cpu, sizeof(__u32), + xyarray__entry(evsel->core.fd, pcpu.cpu, 0), + sizeof(__u32), BPF_ANY); + if (err) { + pr_err("Failed to update perf event map for direct off-cpu dumping\n"); + return; + } + } + skel->bss->enabled =3D 1; } =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index b961467133cf..ccd3bda02b5d 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1379,7 +1379,7 @@ void evsel__config(struct evsel *evsel, struct record= _opts *opts, evsel__reset_sample_bit(evsel, BRANCH_STACK); =20 if (evsel__is_offcpu_event(evsel)) - evsel->core.attr.sample_type &=3D OFFCPU_SAMPLE_TYPES; + evsel->core.attr.sample_type =3D OFFCPU_SAMPLE_TYPES; =20 arch__post_evsel_config(evsel, attr); } --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 2937613AD1D; Wed, 7 Aug 2024 15:39:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045156; cv=none; b=ItLjYBLj38s26RfmU8bo42w/AonqOkTX7PTvwPKQ+fAcAWIgDFrRwBJE2HJa8HtJPHgrARsqWUirLLLUK9zc+oKa4o6sdUjPXbSurQEh/1TNLQiApT+rg4QtKwO1BtbqWWNkXgoX66Aa6GKyfe7g9q9UeuxeOpPslBkzq/9RnZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045156; c=relaxed/simple; bh=blq8IKKWWvrPrqVs1Yb9VttWa84QButnzElZfOfk1Vw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I6vSU/Qy3fs85R3vSy6MF327lbQr+iKZd0T498UJ+SUU2nwOxi4psz4LV17C0TyJmOfdxHkwU+FBXXIttYyV38/i9Fi7Q8N+D+ax+O/t1M/5RsS+4CWLFJ4WFasSXcKKk1wtGHsl8xy5d/9tZCj6jKFpHlad40ne5bwXMVa9cbE= 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=EqUeTtEy; arc=none smtp.client-ip=209.85.214.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="EqUeTtEy" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fc49c0aaffso360555ad.3; Wed, 07 Aug 2024 08:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045154; x=1723649954; 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=4PXh5CjG8YZ+eBmVxe++koknN2SUxUpkitH4m2ykKfM=; b=EqUeTtEyOt8vFr9a3EflfxuYYN2TITF6Rznqn2xfEzKT6BTv8vjfIVjJlZzADF95mF SsuIjZA3UQIU0weT7x6/oy5JV6nO9TanCBnOZHqq734aCPoxMGtBzCUKF5wxwiOqwu/f iltE63xmbtGiB+r0tdBI6YWrQULq9Wxyl/wUEMjuUA+vsbGTexrDWzr1GCWGezZDeDVm wJ0YTIyQGrfH6UEV0ynkKYikubMr6X9py+LmodDGx5l3Nc7eedEvywlZ/A7zUyTcvUUI n9L3NdqlW4ngnRjVYaJvk2egKtTFFOz5qqSPHz5GVSHXtyB8asBDKHEo2twcbvFeE6KB 0/HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045154; x=1723649954; 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=4PXh5CjG8YZ+eBmVxe++koknN2SUxUpkitH4m2ykKfM=; b=Wl9WfpBvcJDU/RGFCEUfGItfHqGYfPH9yr8VtfX/N0GRY9icNTSEO6xW7CX7iFPpEH 6jFXw9nZERKNtTYuLZwmtM9rCdhA5gNQbH7BTbwam1iCTfALAHH3Sr91YV5EqlQzwgpV p3Rd07eIwo4F1/JPw9DS4HlmlyFUM81W9w8LSVIMJSqS7/nQNbPKNWLc1aS39dB7t2z9 FyAqeOgIMpP09I4tshQ2izNfHA7gQmqfsiErBWGa7dZfAfXxKyYYBzUtNhLY+i/m/fqJ ZSewcVIR5r5jvISah2pj96PrT49yKRo+ggdonWch8mCH8qgQ2LhmCtp/ohJW162JyayQ r66w== X-Forwarded-Encrypted: i=1; AJvYcCW4++PZScw5FbKIXiKXUtrSBxNYJAJCmrVhSs9FFP0KalMfXpO/+x4iS8i21I7JpQ498l2bGGW5YwVYTZh5Ha7guiAnw0rGzGIUEoosIFLLdu0fLob2XwWgs8BKHf2Uc7M+2583IvoCgTC4Ii+yng== X-Gm-Message-State: AOJu0YyXXYPLJ85N8VSpldV2Eod4cxgq/8gk3LnWnaW0dGfYyzmkhH6l L49wAAgmAVMOrCjsg4V6DUsJjOUXJZ4TJdoMe0KQf9yLfEdha/83vEEw/aoN X-Google-Smtp-Source: AGHT+IE9swvFXbSaxaUilQQA21OlEPiC6Dsq6ZL2DUUHOK8zERyAaHtp3l30pgnfZj3CSPSpqMXtWQ== X-Received: by 2002:a17:902:d501:b0:1fd:9105:7dd3 with SMTP id d9443c01a7336-1ff5750acd3mr208258825ad.64.1723045154374; Wed, 07 Aug 2024 08:39:14 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:14 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/9] perf record off-cpu: Dump direct off-cpu samples in BPF Date: Wed, 7 Aug 2024 23:38:38 +0800 Message-ID: <20240807153843.3231451-5-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-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" Output PERF_SAMPLE_TID, PERF_SAMPLE_PERIOD, PERF_SAMPLE_CALLCHAIN, and PERF_SAMPLE_CGROUP in BPF. Ideally, we should only output PERF_SAMPLE_PERIOD (off-cpu time) and PERF_SAMPLE_CALLCHAIN (sched_in process's callchain). One only needs to set PERF_SAMPLE_TID and PERF_SAMPLE_CGROUP, and perf_event will do everything for us. But in reality, that's not the case. Setting PERF_SAMPLE_TID will mostly give us TID of 0. We might get the correct TID for offcpu-time event from time to time, but it is really rare. swapper 0 [000] offcpu-time: / :1321819 1321819 [002] offcpu-time: /user.slice/user-1000.slice/se= ssion-2.scope swapper 0 [001] offcpu-time: / swapper 0 [003] offcpu-time: / And setting PERF_SAMPLE_CGROUP doesn't work properly either. tmux: server 3701 [003] offcpu-time: / blueman-tray 1064 [001] offcpu-time: / bash 1350867 [001] offcpu-time: / bash 1350844 [000] offcpu-time: / We need to retrieve PERF_SAMPLE_TID, PERF_SAMPLE_PERIOD, PERF_SAMPLE_CALLCHAIN, and PERF_SAMPLE_CGROUP using BPF and output these four fields. Add perf_event_array map for dumping direct off-cpu samples, but keep the at-the-end approach. Tons of checking before access, to pass the BPF verifier. If off-cpu time (represented as delta) exceeds the off-cpu threshold, do output. Suggested-by: Namhyung Kim Signed-off-by: Howard Chu --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 133 +++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index cca1b6990a57..95cc130bb67e 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -18,6 +18,9 @@ #define MAX_STACKS 32 #define MAX_ENTRIES 102400 =20 +#define MAX_CPUS 4096 +#define MAX_OFFCPU_LEN 128 + struct tstamp_data { __u32 stack_id; __u32 state; @@ -32,6 +35,7 @@ struct offcpu_key { __u64 cgroup_id; }; =20 +/* for dumping at the end */ struct { __uint(type, BPF_MAP_TYPE_STACK_TRACE); __uint(key_size, sizeof(__u32)); @@ -39,6 +43,45 @@ struct { __uint(max_entries, MAX_ENTRIES); } stacks SEC(".maps"); =20 +struct offcpu_data { + u64 array[MAX_OFFCPU_LEN]; +}; + +struct stack_data { + u64 array[MAX_STACKS]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); + __uint(max_entries, MAX_CPUS); +} offcpu_output SEC(".maps"); + +/* temporary offcpu sample */ +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(struct offcpu_data)); + __uint(max_entries, 1); +} offcpu_payload SEC(".maps"); + +/* temporary stack data */ +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(struct stack_data)); + __uint(max_entries, 1); +} stack_tmp SEC(".maps"); + +/* cached stack per task storage */ +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct stack_data); +} stack_cache SEC(".maps"); + struct { __uint(type, BPF_MAP_TYPE_TASK_STORAGE); __uint(map_flags, BPF_F_NO_PREALLOC); @@ -184,12 +227,75 @@ static inline int can_record(struct task_struct *t, i= nt state) return 1; } =20 +static inline bool check_bounds(int index) +{ + if (index >=3D 0 && index < MAX_OFFCPU_LEN) + return true; + + return false; +} + +static inline int copy_stack(struct stack_data *from, + struct offcpu_data *to, int n) +{ + int max_stacks =3D MAX_STACKS, len =3D 0; + + if (!from) + return len; + + for (int i =3D 0; i < max_stacks && from->array[i]; ++i) { + if (check_bounds(n + 2 + i)) { + to->array[n + 2 + i] =3D from->array[i]; + ++len; + } + } + return len; +} + +static int off_cpu_dump(void *ctx, struct offcpu_data *data, struct offcpu= _key *key, + struct stack_data *stack_p, __u64 delta, __u64 timestamp) +{ + int size, n =3D 0, ip_pos =3D -1, len =3D 0; + + if (sample_type & PERF_SAMPLE_TID && check_bounds(n)) + data->array[n++] =3D (u64)key->tgid << 32 | key->pid; + if (sample_type & PERF_SAMPLE_PERIOD && check_bounds(n)) + data->array[n++] =3D delta; + if (sample_type & PERF_SAMPLE_CALLCHAIN && check_bounds(n + 2)) { + /* data->array[n] is callchain->nr (updated later) */ + data->array[n + 1] =3D PERF_CONTEXT_USER; + data->array[n + 2] =3D 0; + + len =3D copy_stack(stack_p, data, n); + + /* update length of callchain */ + data->array[n] =3D len + 1; + + /* update sample ip with the first callchain entry */ + if (ip_pos >=3D 0) + data->array[ip_pos] =3D data->array[n + 2]; + + /* calculate sample callchain data->array length */ + n +=3D len + 2; + } + if (sample_type & PERF_SAMPLE_CGROUP && check_bounds(n)) + data->array[n++] =3D key->cgroup_id; + + size =3D n * sizeof(u64); + if (size >=3D 0 && size <=3D MAX_OFFCPU_LEN * sizeof(u64)) + bpf_perf_event_output(ctx, &offcpu_output, BPF_F_CURRENT_CPU, data, size= ); + + return 0; +} + static int off_cpu_stat(u64 *ctx, struct task_struct *prev, struct task_struct *next, int state) { __u64 ts; __u32 stack_id; struct tstamp_data *pelem; + struct stack_data *stack_tmp_p, *stack_p; + int zero =3D 0, len =3D 0; =20 ts =3D bpf_ktime_get_ns(); =20 @@ -199,6 +305,25 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *= prev, stack_id =3D bpf_get_stackid(ctx, &stacks, BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK); =20 + /* cache stacks */ + stack_tmp_p =3D bpf_map_lookup_elem(&stack_tmp, &zero); + if (stack_tmp_p) + len =3D bpf_get_stack(ctx, stack_tmp_p->array, MAX_STACKS * sizeof(u64), + BPF_F_USER_STACK) / sizeof(u64); + + /* + * if stacks are successfully collected, cache them to task_storage, they= are then + * dumped if the off-cpu time hits the threshold. + */ + if (len > 0) { + stack_p =3D bpf_task_storage_get(&stack_cache, prev, NULL, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (stack_p) { + for (int i =3D 0; i < len && i < MAX_STACKS; ++i) + stack_p->array[i] =3D stack_tmp_p->array[i]; + } + } + pelem =3D bpf_task_storage_get(&tstamp, prev, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE); if (!pelem) @@ -228,6 +353,14 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *= prev, else bpf_map_update_elem(&off_cpu, &key, &delta, BPF_ANY); =20 + if (delta >=3D offcpu_thresh) { + struct offcpu_data *data =3D bpf_map_lookup_elem(&offcpu_payload, &zero= ); + + stack_p =3D bpf_task_storage_get(&stack_cache, next, NULL, 0); + if (data && stack_p) + off_cpu_dump(ctx, data, &key, stack_p, delta, pelem->timestamp); + } + /* prevent to reuse the timestamp later */ pelem->timestamp =3D 0; } --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 570E613B783; Wed, 7 Aug 2024 15:39:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045160; cv=none; b=t7Hazl4mA850VcZiNSMyYiS9O51ozvm0Ru35fc3Fo9GTEsj3j7u9JZf8LQnRO0YfykPoktEBhm2sDIpKBExnhFygoxr8w0PfEIAbA3/jnEWZZa3WNZyDQAzK7SdDMzJ/aqumDykM5lVRodzkCveU1BuRhxZK9y2l/XAR2Sdu2aA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045160; c=relaxed/simple; bh=RI+3yVkIbTsBaDX4k5Qtq/bNNlaLMwk2mVtlK99zP9Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sUllEuN+iW5M1vZTW7NwpAjKKwlITH2StSGF1SO3tofQ/nqAo/c/6HSTFmEgxS+pdaEvYxgrEv4JrKvtTNEhpsUxVbJACteukSAAakMHkYa5CFJ5Ht+pqJkqB0VZofy6YM8tFVAxV+dBsWt7iyfjPh6G3aKtd8P767wg9WyvxCI= 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=k8RwrJfo; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k8RwrJfo" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1fda7fa60a9so376405ad.3; Wed, 07 Aug 2024 08:39:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045159; x=1723649959; 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=9CbD8V8xBx+xYu1GXsRQcpvSfOCRofW7N4CgCXoEGn4=; b=k8RwrJfoA1SqpiiO+eX0duUa2GOHuLfGR9qOM4xflfXqHmxA1Yy5yRFUtwvS/oGQYT a1a6AoI5Rhus/G/pTw8pmN8IKpynMJFESeWPAs7R21a0+VODZW95tj8gq0SXs8CIZTBV 3KUef3lt9MAL6MN/1PX2vcwplb5N3CDo/dQ4mlyNfuuwGZ+CzVvT3j3Kh9wf8th05jAW 2/HvK4YwHb70Jf/LlBYEthtiDvdiLOdF6H2aN0MXmgmFjoJ/Smzc7UN79cmLYOem47uz du/rYfSHxMnOUFOueeC3CHhmEQqg2dA3DX0LBoxbN//gynbIQfcpiqjhTh6SD04imHZv rcDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045159; x=1723649959; 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=9CbD8V8xBx+xYu1GXsRQcpvSfOCRofW7N4CgCXoEGn4=; b=TmhZ4uMZC+M3dZ3BkJEsCG+gr0OvKnwZ6g5jnjewUHaBPNauvv10DV1cuJwlKncWfc 8ZU8H6j8xIcAwDzimMuEtUNtUD3bh18cECrrrX1AUvkk0ynJTl42BFmCsHDo9NBpQiPO ZIV621VNegZEjUSTU2b8NvRWMTQtEBGOsIFSUctW39HWR14zyk8k2eCoIk7jf2WO4UFS hvQmhXepmV1bm3huuYSa1X7tfpfEYVjxBzKl9FMCCgkB0NZLSq0+n8c5L+iQFUs+nKmd KAvT0p6fxWKZ8kz7g/3yJey/7/hI7I15df75zlup9u9br9xuQ3hZlFmTqYGa7m4OSD5a rc9A== X-Forwarded-Encrypted: i=1; AJvYcCVK6YcAKSuxS0Qed4A1ehDaOqDCn9K5BJTiPYR1m4r8MayWHSASsYFn8W8AZyj+r2w0aE5+jzZc9goIyfCSvAnGRIHtzNrV7bHCoT+RIB9lhnFSG9W4UealfdpyZ4L7fsPFUMq2iCupTSpP0vzh/w== X-Gm-Message-State: AOJu0Yx9je3OU/wIMCEdQpX8mTuUE7zplNTJeae2APANLiE5zJa9LNzK klfZKbNtUQZSbqB5TG/dd5HsP6pKQE8wTvN7Dbt8gXPyzaoL8o1YKlaV6KWS X-Google-Smtp-Source: AGHT+IFArYTQyPIXgHZ96DWXBE3P7beBq4UuR0NqZH3P8mXjhi+5QCEvOTkuxIhIxizDcM4Ev3mbMQ== X-Received: by 2002:a17:903:41d2:b0:1fc:a869:7fb7 with SMTP id d9443c01a7336-1ff5746074cmr244965155ad.54.1723045158566; Wed, 07 Aug 2024 08:39:18 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:18 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/9] perf record --off-cpu: Dump total off-cpu time at the end. Date: Wed, 7 Aug 2024 23:38:39 +0800 Message-ID: <20240807153843.3231451-6-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" By setting a placeholder sample_type and then writing real data into raw_data, we mimic the direct sample method to write data at the end. Note that some data serve only as placeholders and will be overwritten by the data in raw_data. Additionally, since the IP will be updated in evsel__parse_sample(), there is no need to handle it in off_cpu_write(). Suggested-by: Namhyung Kim Signed-off-by: Howard Chu --- tools/perf/util/bpf_off_cpu.c | 119 +++++++++++++++++++++------------- tools/perf/util/off_cpu.h | 10 ++- 2 files changed, 84 insertions(+), 45 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index fae0bb8aaa13..24104d703242 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_data[1024 / sizeof(u64)]; + static int off_cpu_config(struct evlist *evlist) { char off_cpu_event[64]; @@ -139,12 +141,21 @@ int off_cpu_prepare(struct evlist *evlist, struct tar= get *target, int ncpus =3D 1, ntasks =3D 1, ncgrps =3D 1; struct strlist *pid_slist =3D NULL; struct str_node *pos; + struct evsel *evsel; =20 if (off_cpu_config(evlist) < 0) { pr_err("Failed to config off-cpu BPF event\n"); return -1; } =20 + evsel =3D evlist__find_evsel_by_str(evlist, OFFCPU_EVENT); + if (evsel =3D=3D NULL) { + pr_err("%s evsel not found\n", OFFCPU_EVENT); + return -1 ; + } + + evsel->sample_type_embed =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; + skel =3D off_cpu_bpf__open(); if (!skel) { pr_err("Failed to open off-cpu BPF skeleton\n"); @@ -257,7 +268,6 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } =20 if (evlist__first(evlist)->cgrp) { - struct evsel *evsel; u8 val =3D 1; =20 skel->bss->has_cgroup =3D 1; @@ -279,6 +289,7 @@ int off_cpu_prepare(struct evlist *evlist, struct targe= t *target, } } =20 + skel->bss->sample_type =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; skel->bss->offcpu_thresh =3D opts->off_cpu_thresh * 1000ull; =20 err =3D off_cpu_bpf__attach(skel); @@ -304,7 +315,8 @@ int off_cpu_write(struct perf_session *session) { int bytes =3D 0, size; int fd, stack; - u64 sample_type, val, sid =3D 0; + u32 raw_size; + u64 sample_type_off_cpu, sample_type_bpf_output, val, sid =3D 0, tstamp = =3D OFF_CPU_TIMESTAMP; struct evsel *evsel; struct perf_data_file *file =3D &session->data->file; struct off_cpu_key prev, key; @@ -314,7 +326,6 @@ int off_cpu_write(struct perf_session *session) .misc =3D PERF_RECORD_MISC_USER, }, }; - u64 tstamp =3D OFF_CPU_TIMESTAMP; =20 skel->bss->enabled =3D 0; =20 @@ -324,15 +335,10 @@ int off_cpu_write(struct perf_session *session) return 0; } =20 - sample_type =3D evsel->core.attr.sample_type; + sample_type_off_cpu =3D OFFCPU_EMBEDDED_SAMPLE_TYPES; + sample_type_bpf_output =3D evsel->core.attr.sample_type; =20 - if (sample_type & ~OFFCPU_SAMPLE_TYPES) { - pr_err("not supported sample type: %llx\n", - (unsigned long long)sample_type); - return -1; - } - - if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) { + if (sample_type_bpf_output & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) { if (evsel->core.id) sid =3D evsel->core.id[0]; } @@ -343,49 +349,74 @@ int off_cpu_write(struct perf_session *session) =20 while (!bpf_map_get_next_key(fd, &prev, &key)) { int n =3D 1; /* start from perf_event_header */ - int ip_pos =3D -1; + int i =3D 0; /* raw data index */ =20 bpf_map_lookup_elem(fd, &key, &val); =20 - if (sample_type & PERF_SAMPLE_IDENTIFIER) - data.array[n++] =3D sid; - if (sample_type & PERF_SAMPLE_IP) { - ip_pos =3D n; - data.array[n++] =3D 0; /* will be updated */ - } - if (sample_type & PERF_SAMPLE_TID) - data.array[n++] =3D (u64)key.pid << 32 | key.tgid; - if (sample_type & PERF_SAMPLE_TIME) - data.array[n++] =3D tstamp; - if (sample_type & PERF_SAMPLE_ID) + /* bpf output data */ + if (sample_type_bpf_output & PERF_SAMPLE_IDENTIFIER) data.array[n++] =3D sid; - if (sample_type & PERF_SAMPLE_CPU) + /* fill in an empty value, it will be overwritten by embedded data */ + if (sample_type_bpf_output & PERF_SAMPLE_IP) data.array[n++] =3D 0; - if (sample_type & PERF_SAMPLE_PERIOD) - data.array[n++] =3D val; - if (sample_type & PERF_SAMPLE_CALLCHAIN) { - int len =3D 0; - - /* data.array[n] is callchain->nr (updated later) */ - data.array[n + 1] =3D PERF_CONTEXT_USER; - data.array[n + 2] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_TID) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_TIME) + data.array[n++] =3D tstamp; /* we won't overwrite time */ + if (sample_type_bpf_output & PERF_SAMPLE_CPU) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_PERIOD) + data.array[n++] =3D 0; + if (sample_type_bpf_output & PERF_SAMPLE_RAW) { + /* + * the format of raw data is as follows: + * + * [ size ][ data ] + * [ data ] + * [ data ] + * [ data ] + * [ data ][ empty] + * + * because embedded data is in BPF output, perf script -F without bpf-o= utput + * field will not work properly + * + */ + if (sample_type_off_cpu & PERF_SAMPLE_TID) + off_cpu_raw_data[i++] =3D (u64)key.pid << 32 | key.tgid; + if (sample_type_off_cpu & PERF_SAMPLE_PERIOD) + off_cpu_raw_data[i++] =3D val; + if (sample_type_off_cpu & PERF_SAMPLE_CALLCHAIN) { + int len =3D 0; + + /* off_cpu_raw_data[n] is callchain->nr (updated later) */ + off_cpu_raw_data[i + 1] =3D PERF_CONTEXT_USER; + off_cpu_raw_data[i + 2] =3D 0; + + bpf_map_lookup_elem(stack, &key.stack_id, &off_cpu_raw_data[i + 2]); + while (off_cpu_raw_data[i + 2 + len]) + len++; + + /* update length of callchain */ + off_cpu_raw_data[i] =3D len + 1; + + /* calculate sample callchain off_cpu_raw_data length */ + i +=3D len + 2; + } + if (sample_type_off_cpu & PERF_SAMPLE_CGROUP) + off_cpu_raw_data[i++] =3D key.cgroup_id; =20 - bpf_map_lookup_elem(stack, &key.stack_id, &data.array[n + 2]); - while (data.array[n + 2 + len]) - len++; + raw_size =3D i * sizeof(u64) + sizeof(u32); /* 4 empty bytes for alignm= ent */ =20 - /* update length of callchain */ - data.array[n] =3D len + 1; + /* raw_size */ + memcpy((void *)data.array + n * sizeof(u64), &raw_size, sizeof(raw_size= )); =20 - /* update sample ip with the first callchain entry */ - if (ip_pos >=3D 0) - data.array[ip_pos] =3D data.array[n + 2]; + /* raw_data */ + memcpy((void *)data.array + n * sizeof(u64) + sizeof(u32), off_cpu_raw_= data, i * sizeof(u64)); =20 - /* calculate sample callchain data array length */ - n +=3D len + 2; + n +=3D i + 1; } - if (sample_type & PERF_SAMPLE_CGROUP) - data.array[n++] =3D key.cgroup_id; + if (sample_type_bpf_output & PERF_SAMPLE_CGROUP) + data.array[n++] =3D 0; =20 size =3D n * sizeof(u64); data.hdr.size =3D size; diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 2dd67c60f211..eaf7be92472d 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -10,12 +10,20 @@ struct record_opts; =20 #define OFFCPU_EVENT "offcpu-time" =20 +#define OFF_CPU_THRESH_DEFAULT 500000ull + #define OFFCPU_SAMPLE_TYPES (PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | \ PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ PERF_SAMPLE_ID | PERF_SAMPLE_CPU | \ - PERF_SAMPLE_PERIOD | PERF_SAMPLE_CALLCHAIN | \ + PERF_SAMPLE_PERIOD | PERF_SAMPLE_RAW | \ PERF_SAMPLE_CGROUP) =20 +/* + * for embedded data to overwrite the original sample, duplicated sample t= ypes + * must be set in the original OFFCPU_SAMPLE_TYPES, except for callchain. + */ +#define OFFCPU_EMBEDDED_SAMPLE_TYPES (PERF_SAMPLE_TID | PERF_SAMPLE_PERIO= D | \ + PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CGROUP) =20 #ifdef HAVE_BPF_SKEL int off_cpu_prepare(struct evlist *evlist, struct target *target, --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 48EC813C66F; Wed, 7 Aug 2024 15:39:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045164; cv=none; b=Sx2k7sHV8ozyremvLQntERa6HaYGMf8KilyP0cNXAcxy8U2XtelR/hnfVYKkPo3/pcxNnEuJqM3JZ3mv8+6RhbJBSYPBrZfE5zpH/qWcNC1OV5zP0gPUU9T/4a3qlw5OEEqsELllszVoft1eeEYskPLZYNFE8hW80JIdgpKtBKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045164; c=relaxed/simple; bh=MVIxRPaOswQa3Xdpi40A7xdtRH3kCEfhA40AFB41mEM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r3WAsTAW8fhoMNhq2boVxYS0SzHvP75Ec1A0Tj8lD6ZaHFxhcB5SoFxaFU4ULubZJZ0fTa1WUucfj53+MQ9/fGClsQ7+07VqOViTSP7cNI9ezUb+AGyqQ+fAWMcNoQdypPZ9JraZ8Zl8i5gaSn3bxpsE80IGvn4t9gieg0bf7dc= 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=UgVoIglG; arc=none smtp.client-ip=209.85.214.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="UgVoIglG" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fec34f94abso396525ad.2; Wed, 07 Aug 2024 08:39:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045163; x=1723649963; 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=Ge6280gYdYEUDrL5AI92yP7xEDZ/VciZzyHbENVq6rg=; b=UgVoIglGYAbaF1CCXEblyGaKngI4tl+TQk/dk7aWU6B6kR9FNk+sAlGajccO6FeouA MhppmONIyMcFRwJwGR201pbPdJDy1lm+M/W/S0EojZqYG1Lr0+iSnCnbBNtYUqOdvmji trPoyaC1GKaFNZ86KLU+Wl22bkDPC0YqQfFCg45kTZ7UNNkJa2YB8JHR/EcfxCsTN6/5 wy6x666KrLohoCsYVAfBqmPX5ZhAaOUehEUJObUSRo3nbpRuAPMscv92cPFSOrVxAmyT dg17v1vZ7Z6/lLTWD42Zl3BWI5bHOsYstNR3y29RDphm60QWbSTuJvinHlkxoXyBd8So RNNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045163; x=1723649963; 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=Ge6280gYdYEUDrL5AI92yP7xEDZ/VciZzyHbENVq6rg=; b=R17h+9jFVZyAnpWKHvs+3TgisS8/N6Yg1Ct7GDRQ9oC9x5qoHK/uJJ6hh6mbh9jtCQ zyk9V7KRKvqhbJ1UMh8z0yrBv7lsvBuH8GKSD+/MzdxmOWEt7VNFUUgdu8OCL8ofpWg1 ppM4BcAX5EY1G43vNZjnbTEFSPkWKkUUP14VMoKGp9+qP5W+c+TTu9M64/M9N3TYsZjX PxDVycS4i1UP1L93pSPjidImuaiswOjdMxaivrKSGTnDW9hjynxkzcHQf282BxCoG0Br JJFbY3SLWwXWptK2JDYyE+Akz1Uw6mOGUnl916SQcf/7dNVbnGp4JsEe8c2qQyzbnurC NiCg== X-Forwarded-Encrypted: i=1; AJvYcCUkKeO7+cpZdKiblJWr0WIUijEI1JLRiZc+MWWSB1mxNUnwvnnZvrJnRx6kIudlrUzohFgsmSQLbYU1lZNH5vpzCmOAH3CjOxdc0OTy9NxWK6jToHL8OgW1XprXUndIYcts1G1iIUNwK1YrqBoC/A== X-Gm-Message-State: AOJu0Yw6+dOcmL9s/H6uHiS0m1e64wgjmmI2r/X8wipsIR5BbwJQU3A9 2pEwdPHuQn1mdl1Y3z7BWQqscfqpY/cG6KiwZ4I0BuPD86kZObcL X-Google-Smtp-Source: AGHT+IGgpM9BGwxmxW5iCClXM0iwbOlva9Elci7CAkMF1EscX7uq8vp5Mb9jbgb6YqHTNkBgguBiOw== X-Received: by 2002:a17:902:ea08:b0:1f9:c3e4:4c0f with SMTP id d9443c01a7336-1ff572c56b4mr185015815ad.34.1723045162683; Wed, 07 Aug 2024 08:39:22 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:22 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/9] perf evsel: Delete unnecessary = 0 Date: Wed, 7 Aug 2024 23:38:40 +0800 Message-ID: <20240807153843.3231451-7-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete unnecessary zero-initializations because they are already set to zero at the top of evsel__parse_sample(). If we don't remove them, it becomes troublesome to overwrite the sample using data from raw_data. Signed-off-by: Howard Chu --- tools/perf/util/evsel.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index ccd3bda02b5d..c1aac09ad0a5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2611,8 +2611,6 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, return perf_evsel__parse_id_sample(evsel, event, data); } =20 - array =3D event->sample.array; - if (perf_event__check_size(event, evsel->sample_size)) return -EFAULT; =20 @@ -2879,7 +2877,6 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, array++; } =20 - data->intr_regs.abi =3D PERF_SAMPLE_REGS_ABI_NONE; if (type & PERF_SAMPLE_REGS_INTR) { OVERFLOW_CHECK_u64(array); data->intr_regs.abi =3D *array; @@ -2896,25 +2893,21 @@ int evsel__parse_sample(struct evsel *evsel, union = perf_event *event, } } =20 - data->phys_addr =3D 0; if (type & PERF_SAMPLE_PHYS_ADDR) { data->phys_addr =3D *array; array++; } =20 - data->cgroup =3D 0; if (type & PERF_SAMPLE_CGROUP) { data->cgroup =3D *array; array++; } =20 - data->data_page_size =3D 0; if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { data->data_page_size =3D *array; array++; } =20 - data->code_page_size =3D 0; if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { data->code_page_size =3D *array; array++; --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DCAC13D265; Wed, 7 Aug 2024 15:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045169; cv=none; b=imkcNrOcQ5x6UabPqh/CRMEOtEYjxgcLj2eMxbA6P4Br2GeVH0odz0jXhbd7d262Z4OD3vtE778Of8oaFyYjXVSuqQd06R+BNVS5LpeBznNg5tTs20bJTaasxTKZue8h5Mgv7/puvgUJaeO/gz62cRi2N+veilj3XJmF8LL/FLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045169; c=relaxed/simple; bh=uTLMPRQ7PpsmiGeFp0oCgRjEPLGy5/MRVfn8ELiaifw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lpFqDFXOIKejRnIwelaFTOzpiIIO8+ZZu/UMEisFGI9jWswxTLgA5QMBkiEn+9XSs8mAQ/thMGGIa6yPKeeDdcIsEyHLiPkU4/DwbSkwLqcQ/D0Ja3lWLFQLVgdpYMeVR0/mFUe+XRAZZc78YLs4yiJJJhoi5NiBiv9+wlK6+I8= 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=VZOB1acD; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VZOB1acD" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1fc49c0aaffso363125ad.3; Wed, 07 Aug 2024 08:39:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045167; x=1723649967; 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=AfYYj96hxoVP6jblDfVz2LtzuPs3R2O748Dxdho8r5M=; b=VZOB1acD61IkDgvN/kgzwTI5lzhqeySF/yRf5RTA+gtcAZalCE69LnWv8t0DWD6XdM UbJSl4e9/slC+6zMylh0K6BiBy2u1g89CYUVsuw4i7eWEwhrZW3TwKk8Op7D38mYaPy2 FY+YpG6gWGh21qGZACEBpkXm9qyAYbc239VbzGiQ+AiGiSDyzkYFo1Ad093njXtJY52f atTcKLBMuYBuBoPK0utY2fwl0fq63GbGUr1/jlramGD2OunGukKs/t62iwWf1o50LXZO GqOr+dVydscTQ7W6eGze+Q5iZOkzmVTvfubjKJ5EsFx/66batts33E+w9GFfIwmqzQtq AeRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045167; x=1723649967; 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=AfYYj96hxoVP6jblDfVz2LtzuPs3R2O748Dxdho8r5M=; b=tV9bK8ryVfNfxZDclsWPBlbHL/Qn6e8pZR/DFj/ySxR0lBN+QKPyO41H6HqPpd79Fu Cz39lApcsTXA+0hq+q120dSxz9jTGaEazUYwahhkRht/UsS1YUfjjJVnQbsiLrEBmUBk xq3AMZ3vG38KjDnTkei6GTTZB5PgBssmCE4QofsX8ePhysXxwXP8ClbnSYBP0FGbrAmJ L3riyykn+eTNbcWRVOCsbeNoBizxANrtrbqz5Zm3ofRUut8lJI8EGUZzIv06A/0SKC6e oup1ZNobMKzlejSVrbjcE9CFT/DN61GnI4eVnsFX8maJZsViXa2KTbV6WFMMORfkEss4 5+RQ== X-Forwarded-Encrypted: i=1; AJvYcCXQNsrOpFQtdbnbVhYvX9nZR6r9M56cVj0Ok23bcfK/5VmHePI/sksPSx+7pief+6pAE126ScwtzZs3Jfugw66FZCMxz/W9+KOByTU6qeOrtgTjGJUFcLaxpRu9+ZTrPw7cnQC+fsJvTScQOocCfA== X-Gm-Message-State: AOJu0Yz0BMawUwKSYm0iGORv1f8dsR/MbM4VSjqxKB7OOvPUbMHw0HBc HxD2TVugylFBkrw75CWTl0B3PpDJ+Qb+/h4upIXKxqKsw+Gj8XSn X-Google-Smtp-Source: AGHT+IHVjR96skc6WXo7h94wO9ITTMfQB+lyCbjRrmP+1sXu8qsrVi4PxYaBQXOiVJmcs88YwPrXTQ== X-Received: by 2002:a17:902:d2d2:b0:1fd:9d0c:998c with SMTP id d9443c01a7336-1ff574e2f50mr199507045ad.56.1723045166791; Wed, 07 Aug 2024 08:39:26 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:26 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 7/9] perf record --off-cpu: Parse BPF output embedded data Date: Wed, 7 Aug 2024 23:38:41 +0800 Message-ID: <20240807153843.3231451-8-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a sample_type_embed member to the struct evsel, along with a couple of helper functions. In session.c, we parse BPF output embedded samples in a two-step process. Initial Parsing: Treat the sample as a regular BPF-output event. Secondary Parsing: Extract data from raw_data and parse it according to the sample_type_embed specification. Since the second step relies on the raw_data obtained in the first step, we must avoid zero-initializing the sample data after the first step. Suggested-by: Ian Rogers Suggested-by: Arnaldo Carvalho de Melo Signed-off-by: Howard Chu --- tools/perf/builtin-script.c | 5 +++-- tools/perf/util/evsel.c | 36 ++++++++++++++++++++++++++++-------- tools/perf/util/evsel.h | 13 +++++++++++++ tools/perf/util/session.c | 16 +++++++++++++++- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index c16224b1fef3..4145e61b8d38 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -653,7 +653,8 @@ static int perf_session__check_output_opt(struct perf_s= ession *session) =20 evlist__for_each_entry(session->evlist, evsel) { not_pipe =3D true; - if (evsel__has_callchain(evsel)) { + if (evsel__has_callchain(evsel) || + evsel__embed_has_callchain(evsel)) { use_callchain =3D true; break; } @@ -2295,7 +2296,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__has_embed(evsel) && PRINT_FIEL= D(BPF_OUTPUT)) perf_sample__fprintf_bpf_output(sample, fp); perf_sample__fprintf_insn(sample, attr, thread, machine, fp, al); =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index c1aac09ad0a5..a5a5b3ff16e1 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -298,6 +298,7 @@ void evsel__init(struct evsel *evsel, evsel->pmu_name =3D NULL; evsel->group_pmu_name =3D NULL; evsel->skippable =3D false; + evsel->sample_type_embed =3D 0; } =20 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) @@ -440,6 +441,7 @@ struct evsel *evsel__clone(struct evsel *orig) evsel->weak_group =3D orig->weak_group; evsel->use_config_name =3D orig->use_config_name; evsel->pmu =3D orig->pmu; + evsel->sample_type_embed =3D orig->sample_type_embed; =20 if (evsel__copy_config_terms(evsel, orig) < 0) goto out_err; @@ -2589,6 +2591,7 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, u16 max_size =3D event->header.size; const void *endp =3D (void *)event + max_size; u64 sz; + bool ip_in_callchain =3D false; =20 /* * used for cross-endian analysis. See git commit 65014ab3 @@ -2596,14 +2599,27 @@ int evsel__parse_sample(struct evsel *evsel, union = perf_event *event, */ union u64_swap u; =20 - memset(data, 0, sizeof(*data)); - data->cpu =3D data->pid =3D data->tid =3D -1; - data->stream_id =3D data->id =3D data->time =3D -1ULL; - data->period =3D evsel->core.attr.sample_period; - data->cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; - data->misc =3D event->header.misc; - data->data_src =3D PERF_MEM_DATA_SRC_NONE; - data->vcpu =3D -1; + /* + * For sample data embedded in BPF output, don't clear the sample we read= in the first pass, + * and read the embedded data from raw_data in the second pass. + */ + if (evsel__has_embed(evsel) && evsel__is_bpf_output(evsel) && data->raw_d= ata) { + type =3D evsel->sample_type_embed; + array =3D data->raw_data; + /* if callchain is embedded in BPF output, so is ip */ + if (type & PERF_SAMPLE_CALLCHAIN) + ip_in_callchain =3D true; + } else { /* for normal samples, clear to zero before reading */ + array =3D event->sample.array; + memset(data, 0, sizeof(*data)); + data->cpu =3D data->pid =3D data->tid =3D -1; + data->stream_id =3D data->id =3D data->time =3D -1ULL; + data->period =3D evsel->core.attr.sample_period; + data->cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + data->misc =3D event->header.misc; + data->data_src =3D PERF_MEM_DATA_SRC_NONE; + data->vcpu =3D -1; + } =20 if (event->header.type !=3D PERF_RECORD_SAMPLE) { if (!evsel->core.attr.sample_id_all) @@ -2732,6 +2748,10 @@ int evsel__parse_sample(struct evsel *evsel, union p= erf_event *event, data->callchain =3D (struct ip_callchain *)array++; if (data->callchain->nr > max_callchain_nr) return -EFAULT; + + if (ip_in_callchain && data->callchain->nr > 1) + data->ip =3D data->callchain->ips[1]; + sz =3D data->callchain->nr * sizeof(u64); OVERFLOW_CHECK(array, sz, max_size); array =3D (void *)array + sz; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 80b5f6dd868e..0d25e82c6154 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -184,6 +184,9 @@ struct evsel { }; /* Is the tool's fd for /proc/pid/stat or /proc/stat. */ bool pid_stat; + + /* for samples embedded in BPF output */ + __u64 sample_type_embed; }; =20 struct perf_missing_features { @@ -469,6 +472,11 @@ static inline bool evsel__is_bpf_output(struct evsel *= evsel) return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT); } =20 +static inline bool evsel__has_embed(struct evsel *evsel) +{ + return evsel->sample_type_embed !=3D 0; +} + static inline bool evsel__is_clock(const struct evsel *evsel) { return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || @@ -525,6 +533,11 @@ static inline bool evsel__has_callchain(const struct e= vsel *evsel) evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN; } =20 +static inline bool evsel__embed_has_callchain(const struct evsel *evsel) +{ + return evsel->sample_type_embed & PERF_SAMPLE_CALLCHAIN; +} + static inline bool evsel__has_br_stack(const struct evsel *evsel) { /* diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 5596bed1b8c8..18aa5e0263b7 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1529,6 +1529,16 @@ static int evlist__deliver_sample(struct evlist *evl= ist, struct perf_tool *tool, u64 sample_type =3D evsel->core.attr.sample_type; u64 read_format =3D evsel->core.attr.read_format; =20 + /* parse sample the second time to get embedded data from raw_data */ + if (evsel__is_bpf_output(evsel) && evsel__has_embed(evsel) && sample->raw= _data) { + int err =3D evsel__parse_sample(evsel, event, sample); + + if (err) { + pr_err("failed to parse BPF ouput embedded data, err =3D %d\n", err); + return err; + } + } + /* Standard sample delivery. */ if (!(sample_type & PERF_SAMPLE_READ)) return tool->sample(tool, event, sample, evsel, machine); @@ -1639,8 +1649,12 @@ static int perf_session__deliver_event(struct perf_s= ession *session, const char *file_path) { struct perf_sample sample; - int ret =3D evlist__parse_sample(session->evlist, event, &sample); + int ret; + + /* avoid reading embedded data from raw_data */ + sample.raw_data =3D NULL; =20 + ret =3D evlist__parse_sample(session->evlist, event, &sample); if (ret) { pr_err("Can't parse sample, err =3D %d\n", ret); return ret; --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.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 8F7AA13D52E; Wed, 7 Aug 2024 15:39:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045172; cv=none; b=aFF0ml4ZKyBcvefEbX2cT/JcEZb5/yGj0z+JOEy5jb04ZO7/Hc4ecDxmecEi15tkgwjSGHPEQ5Pk2StFn6uTF/gHmLpIQAPXTIdcwzcUpyVVYwSWykN6Bn92U+l2oBb/jUOQ6H8BSxFSQLHPN6QftoO2gXK+ysx0mokpwoXX1vU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045172; c=relaxed/simple; bh=pN4gnZ+hCxtsiADzh1TO3N3CQfV5XYtooZ0ao6aE7p4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rOSBBd/j3BOmStrYkHi+x4iUnivc8gnksdbSqalZflPKRSkjnmo+FewHRptGsdJ7jQlDkB2QuX/oqmEXC/PFl2mIOt2IxB6QMVamu4q+fkspuryrKPdT8w3Y9aDi5qNNEG0OTFGF7EQh2tEwrJ9+8t90qxDt53yJQsqMgUahong= 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=H0cb12U/; arc=none smtp.client-ip=209.85.214.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="H0cb12U/" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1fc47abc040so541235ad.0; Wed, 07 Aug 2024 08:39:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045171; x=1723649971; 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=qMhvpr3kLsVd+Zp0Hs2NgM4ZnpjLzgRQxx2+bJ3JpEU=; b=H0cb12U/gu7T/g7rD2x2NW+xgJ/TzZ/7il5U0KgRnt9aXaWiy+TCol0MWWPEnTYSax M0OANO6Djszq2JxN25CcREw2ZfAFReR61EIX4GAIFNyvFAQahrGzT8htPdsKL18v+dtY 6Mv1T9M8LAfojHedlcilNfG+nKToMKDHSzJCtFtBaGCwk/QlksCMtAFj3GdcwDI5TlRh vDKoNE5A9jLZDeY9KEUgeBiXC2fGcWgQ5CiCXq7UUkrey8uD12iKEOaLgHt2qX4x7sTo xSqzndppJfHIEd7K6nla8/JhljjvSYMHMnyM6H+xLAapc6aHtRx3MH82mJ0mb/+vjlEd CEog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045171; x=1723649971; 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=qMhvpr3kLsVd+Zp0Hs2NgM4ZnpjLzgRQxx2+bJ3JpEU=; b=nXzJ6+Za/POpFVwomQKGb5z7/1qE9i4dlizDnET2TBCF3WHBT+yrpq4iwSE9Wnp7Ml /NVOVjKOA56blmZKgeW2PU8Y2Y7k/Zps9MDeMhVsx6rPvcJrGyhKRM2zx27JU53JAOi6 IOO5193U4G+r54zji1LhiFC/AifEwug9ai3N1Nb5ZZ1sux46e25Msnd3qBpIfDMbdc7Z +OeNn1P+leNTD/fyPosCiSKiMVR1DJ2ptXEgEfKWGUZVuns9voRt1alxGFKkWlNxyCrz sS4XCDclzYYkAD1OV1TVTqarcNpVC6dlH5SLhX8ypgNBzFwCpODkm0tZ9UTmfid1lMPl PndA== X-Forwarded-Encrypted: i=1; AJvYcCVBnN2YLuYNhZzOEBiSegqm9dBuzZbPvwrKV/IJsnwClmDkR/MKrWN0cIeWqP3VB6ZgD2suf6VYiUs1g/yJg67BrNUfACfpQSaz5RzDwBZVj66lIrNHDwGza7ST+1laUJkDtYgDZGOlUayGGCV7DA== X-Gm-Message-State: AOJu0YxTvk6xxtvg8165D8DD1tS2TUq5CPlICp0D8FfEwLH6/wkTa03W Gm48EETrTbPTZmlsxvXxCqnLQDLoawkXPjcVwBs1JO43E+bEoehD X-Google-Smtp-Source: AGHT+IEEDbCElzMRusO7fp7FA+SwJos67RxC7Bn4ApHghMBa+U/Rqz3Ov6xx9I9N6d/iOATD56itpw== X-Received: by 2002:a17:902:ea08:b0:1fd:9420:1048 with SMTP id d9443c01a7336-1ff572ecef4mr196368135ad.32.1723045170889; Wed, 07 Aug 2024 08:39:30 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:30 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 8/9] perf header: Add field 'embed' Date: Wed, 7 Aug 2024 23:38:42 +0800 Message-ID: <20240807153843.3231451-9-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We have to save the embedded data's sample type for it to be consumed correctly by perf script or perf report. This will approach most definitely break some perf.data convertor. Signed-off-by: Howard Chu --- tools/perf/util/header.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 55e9553861d0..d60e77d5c25c 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -80,6 +80,7 @@ const char perf_version_string[] =3D PERF_VERSION; =20 struct perf_file_attr { struct perf_event_attr attr; + __u64 embed; struct perf_file_section ids; }; =20 @@ -3713,6 +3714,7 @@ static int perf_session__do_write_header(struct perf_= session *session, } f_attr =3D (struct perf_file_attr){ .attr =3D evsel->core.attr, + .embed =3D evsel->sample_type_embed, .ids =3D { .offset =3D evsel->id_offset, .size =3D evsel->core.ids * sizeof(u64), @@ -4147,6 +4149,14 @@ static int read_attr(int fd, struct perf_header *ph, =20 ret =3D readn(fd, ptr, left); } + + ret =3D readn(fd, &f_attr->embed, sizeof(f_attr->embed)); + if (ret <=3D 0) { + pr_debug("failed to read %d bytes of embedded sample type\n", + (int)sizeof(f_attr->embed)); + return -1; + } + /* read perf_file_section, ids are read in caller */ ret =3D readn(fd, &f_attr->ids, sizeof(f_attr->ids)); =20 @@ -4272,6 +4282,8 @@ int perf_session__read_header(struct perf_session *se= ssion, int repipe_fd) tmp =3D lseek(fd, 0, SEEK_CUR); evsel =3D evsel__new(&f_attr.attr); =20 + evsel->sample_type_embed =3D f_attr.embed; + if (evsel =3D=3D NULL) goto out_delete_evlist; =20 --=20 2.45.2 From nobody Sun Feb 8 14:20:49 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F25E713DBBC; Wed, 7 Aug 2024 15:39:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045177; cv=none; b=dV9qBf5GvssqRK2q+6vimtD1xUqrxaojMI+DDPVX9NXw7j7CzXDlMXvxUX9+iheXAK/p1lyuhQ5UjyWXpPyePFB9bnfzdQvnSj9/ZZ2LgAR+C8Lxf8O3UWcjKFcTtNRitP5QXqMrzC6jlF6X+HMMXwBh2NHDaI+t/SeBqAde3Y0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723045177; c=relaxed/simple; bh=v/lW54jVExiECPHOK5dymKyF8qjzPdktdFdL9OyTjGs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E7KysMQzkikt/YxkxkMvYFfXfjOy3DtIosW1x4ZADvbe9PI9kRfVH0o7BhByQemHhUciMLngtqOoR/IkhbgJA3Su/Hov8z/6HPjxdl7ZmBesWAsFH5EUVJ4wMpQcGP+1yohqGoR+oh7SpKAbbTz3lE4S80s7kKUjYFgDk1UKyuI= 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=AVgt/sGW; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AVgt/sGW" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fd66cddd07so491325ad.2; Wed, 07 Aug 2024 08:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723045175; x=1723649975; 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=soyrr8oKFRhnn8yi4F2C3vzDC9dotJSg6I+qdK/nTSw=; b=AVgt/sGWmBcaRz9kERRWNhsSbxcN5xv7vI7JT6CTZX1FAXgh8GNaYZvDfvxwMVUZC6 AE+I37rO4mSFJhd1Cz0xr6FpC8/P0zDWFXUuy1gQQ74SgSvUSZu//pSJdokaBcRB8WEJ muN8FyM0SyWDKxekvEb02EKoR4CxKssIXOWRbzLEWn1VoqD/0VmM2d/0nO3ex7aXo7lt MZJJEdaGdsst0J42UMEpiqrFIPb03XQu/IYYZGQ8TQ2QjqeEPZ5JmlvViD/u87NWfkP0 MdMZBJP1kEDErRNyjhWbDvNbMovVVddYv3i3KJIhMCMLp66KGtU+wSJfQJLN24AH9m5+ Fd4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045175; x=1723649975; 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=soyrr8oKFRhnn8yi4F2C3vzDC9dotJSg6I+qdK/nTSw=; b=jh1J14c5X3w5lRnEa6uerb89XsuDINYiGILDEvzHGkAd2eAfNQbwOHR1m97LRuYRsm ohW4oSKZe0xlvjYoKZo8QJ2YkJy1siZhvm5y+rbZrueFKzqVpuLeVFcJ5QuC5y1i2nYj IIs+smc7n4skGLy6kminfZ35sI5RBgejnkkvkMYG3krZRhDiy866Wc8szZ5gvgLy4QIz s4MxzexulP4Zaqa1mgUnqttvvjPiPSgmz/lWZIt0pQVJoFpkvhpPtouxrPJ2s3cVzxSE bbtO++80OYS7bpQTFxPMeiEx+7ZLJQhTHREnHfoxlmJZ7cUjaifutddLZ23hIutItYAh dG7Q== X-Forwarded-Encrypted: i=1; AJvYcCUt+Ojam2fH7vWiPMc3TwBY4e4Cct9yCjrvUqyyYKK6kl5yzpUEAb4Ha1LDIYKlQHEyiUQ3T6zhi/PdLwrNzL7PNg==@vger.kernel.org, AJvYcCVmCuVphXAORG/Ni8m5fH/+xnhq7gXCbiSxSkOVqCxL1+4unCKyRbV1GXjfNDFukEd/zHg6o/y/XhuGLnw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxrf2kUTigUgaFMQgb+dNY5HR8YCACvT1k4/1cKdKFdfORTS5KR l0AHIeiXJU/+SXEhwXWCtmBcRua11HYgVJXMbGzvOWFGiLpd3Pg7 X-Google-Smtp-Source: AGHT+IEx4uXbTNouB5jgch/yIjkA+bVZUvAy0eKlMM3V8hQFzx2NaFGhwvvDMQFE3cWwXEC6Nz7MTg== X-Received: by 2002:a17:903:11ce:b0:1fb:4f7f:3b59 with SMTP id d9443c01a7336-1ff5723e593mr201091205ad.3.1723045175227; Wed, 07 Aug 2024 08:39:35 -0700 (PDT) Received: from localhost.localdomain ([120.229.49.55]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297329sm108097375ad.247.2024.08.07.08.39.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:39:34 -0700 (PDT) From: Howard Chu To: namhyung@kernel.org Cc: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, jolsa@kernel.org, kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 9/9] perf test: Add direct off-cpu dumping test Date: Wed, 7 Aug 2024 23:38:43 +0800 Message-ID: <20240807153843.3231451-10-howardchu95@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240807153843.3231451-1-howardchu95@gmail.com> References: <20240807153843.3231451-1-howardchu95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a simple workload(offcpu.c) to create the scenario for direct off-cpu dumping. Please run this test with 'perf test offcpu' Suggested-by: Ian Rogers Signed-off-by: Howard Chu --- tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/shell/record_offcpu.sh | 27 +++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/tests/workloads/Build | 1 + tools/perf/tests/workloads/offcpu.c | 16 +++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 tools/perf/tests/workloads/offcpu.c diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 470a9709427d..aa33beaf58c8 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -153,6 +153,7 @@ static struct test_workload *workloads[] =3D { &workload__brstack, &workload__datasym, &workload__landlock, + &workload__offcpu, }; =20 static int num_subtests(const struct test_suite *t) diff --git a/tools/perf/tests/shell/record_offcpu.sh b/tools/perf/tests/she= ll/record_offcpu.sh index 67c925f3a15a..6c26f541a09a 100755 --- a/tools/perf/tests/shell/record_offcpu.sh +++ b/tools/perf/tests/shell/record_offcpu.sh @@ -6,6 +6,8 @@ set -e =20 err=3D0 perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) +TEST_PROGRAM=3D"perf test -w offcpu" +dummy_timestamp=3D18446744069414584 =20 cleanup() { rm -f ${perfdata} @@ -88,6 +90,27 @@ test_offcpu_child() { echo "Child task off-cpu test [Success]" } =20 +test_offcpu_direct() { + echo "Direct off-cpu test" + + # dump off-cpu samples for task blocked for more than 1.999999s + # -D for initial delay, to enable evlist + if ! perf record -e dummy -D 500 --off-cpu --off-cpu-thresh 1999999 -o $= {perfdata} ${TEST_PROGRAM} 2> /dev/null + then + echo "Direct off-cpu test [Failed record]" + err=3D1 + return + fi + # Direct sample's timestamp should be lower than the dummy_timestamp of = the at-the-end sample. + if ! perf script -i ${perfdata} -F time,period | sed "s/[\.:]//g" | \ + awk "{ if (\$1 < ${dummy_timestamp} && \$2 > 1999999999) exit 0; el= se exit 1; }" + then + echo "Direct off-cpu test [Failed missing direct sample]" + err=3D1 + return + fi + echo "Direct off-cpu test [Success]" +} =20 test_offcpu_priv =20 @@ -99,5 +122,9 @@ if [ $err =3D 0 ]; then test_offcpu_child fi =20 +if [ $err =3D 0 ]; then + test_offcpu_direct +fi + cleanup exit $err diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 6ea2be86b7bf..c7a5e27c4567 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -206,6 +206,7 @@ DECLARE_WORKLOAD(sqrtloop); DECLARE_WORKLOAD(brstack); DECLARE_WORKLOAD(datasym); DECLARE_WORKLOAD(landlock); +DECLARE_WORKLOAD(offcpu); =20 extern const char *dso_to_test; extern const char *test_objdump_path; diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/= Build index 5af17206f04d..0e78fd01eaf1 100644 --- a/tools/perf/tests/workloads/Build +++ b/tools/perf/tests/workloads/Build @@ -7,6 +7,7 @@ perf-test-y +=3D sqrtloop.o perf-test-y +=3D brstack.o perf-test-y +=3D datasym.o perf-test-y +=3D landlock.o +perf-test-y +=3D offcpu.o =20 CFLAGS_sqrtloop.o =3D -g -O0 -fno-inline -U_FORTIFY_SOURCE CFLAGS_leafloop.o =3D -g -O0 -fno-inline -fno-omit-frame-pointer -= U_FORTIFY_SOURCE diff --git a/tools/perf/tests/workloads/offcpu.c b/tools/perf/tests/workloa= ds/offcpu.c new file mode 100644 index 000000000000..57cee201a4c3 --- /dev/null +++ b/tools/perf/tests/workloads/offcpu.c @@ -0,0 +1,16 @@ +#include +#include +#include "../tests.h" + +static int offcpu(int argc __maybe_unused, const char **argv __maybe_unuse= d) +{ + /* get past the initial delay */ + sleep(1); + + /* what we want to collect as a direct sample */ + sleep(2); + + return 0; +} + +DEFINE_WORKLOAD(offcpu); --=20 2.45.2