mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yang Jihong <yangjihong@bytedance.com>
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	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, james.clark@arm.com,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: yangjihong@bytedance.com
Subject: [RFC 08/12] perf event action: Add parsing builtin cpu expr support
Date: Thu, 28 Nov 2024 21:35:49 +0800	[thread overview]
Message-ID: <20241128133553.823722-9-yangjihong@bytedance.com> (raw)
In-Reply-To: <20241128133553.823722-1-yangjihong@bytedance.com>

Support parsing of builtin cpu expression.

Testcase:
  # perf record -e sched:sched_switch --action 'print("cpu = %d\n", cpu)'
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  cpu = 7
  ...
Signed-off-by: Yang Jihong <yangjihong@bytedance.com>
---
 tools/perf/util/bpf_skel/bpf_record_action.h |  1 +
 tools/perf/util/bpf_skel/record_action.bpf.c | 16 ++++++++++++++++
 tools/perf/util/parse-action.c               |  1 +
 tools/perf/util/parse-action.h               |  1 +
 tools/perf/util/parse-action.l               |  1 +
 tools/perf/util/record_action.c              |  7 +++++--
 6 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/bpf_skel/bpf_record_action.h b/tools/perf/util/bpf_skel/bpf_record_action.h
index ee4d03848e58..91a307b89f85 100644
--- a/tools/perf/util/bpf_skel/bpf_record_action.h
+++ b/tools/perf/util/bpf_skel/bpf_record_action.h
@@ -7,6 +7,7 @@
 #define __OUTPUT_FORMATS_MAX_NUM 8
 
 enum __output_format_type {
+	__OUTPUT_FORMAT_TYPE_CPU,
 	__OUTPUT_FORMAT_TYPE_MAX,
 };
 
diff --git a/tools/perf/util/bpf_skel/record_action.bpf.c b/tools/perf/util/bpf_skel/record_action.bpf.c
index 424fa8c3e6f1..4b7698554ead 100644
--- a/tools/perf/util/bpf_skel/record_action.bpf.c
+++ b/tools/perf/util/bpf_skel/record_action.bpf.c
@@ -35,10 +35,22 @@ static inline struct __output_data_payload *sample_data_payload(void)
 	return bpf_map_lookup_elem(&sample_data_tmp, &key);
 }
 
+static inline int output_cpu(__u8 *data, int size)
+{
+	__u32 *cpu = (__u32 *)data;
+
+	if (size < sizeof(__u32))
+		return -1;
+
+	*cpu = bpf_get_smp_processor_id();
+	return sizeof(__u32);
+}
+
 SEC("xxx")
 int sample_output(u64 *ctx)
 {
 	struct __output_data_payload *sample;
+	__u8 *data;
 	int i;
 	int total = 0;
 	int ret = 0;
@@ -50,8 +62,12 @@ int sample_output(u64 *ctx)
 	if (!sample)
 		return 0;
 
+	data = sample->__data;
 	for (i = 0; i < output_format_num && i < __OUTPUT_FORMATS_MAX_NUM; i++) {
 		switch (output_formats[i]) {
+		case __OUTPUT_FORMAT_TYPE_CPU:
+			ret = output_cpu(data + total, __OUTPUT_DATA_MAX_SIZE - total);
+			break;
 		default:
 			ret = -1;
 			break;
diff --git a/tools/perf/util/parse-action.c b/tools/perf/util/parse-action.c
index e6299de99bc5..2bb32cc16ae7 100644
--- a/tools/perf/util/parse-action.c
+++ b/tools/perf/util/parse-action.c
@@ -12,6 +12,7 @@
  *   - call:
  *     - print
  *   - builtin:
+ *     - cpu
  */
 
 #include <regex.h>
diff --git a/tools/perf/util/parse-action.h b/tools/perf/util/parse-action.h
index b1a33a77c558..ee949ed07111 100644
--- a/tools/perf/util/parse-action.h
+++ b/tools/perf/util/parse-action.h
@@ -27,6 +27,7 @@ enum evtact_expr_call_type {
 };
 
 enum evtact_expr_builtin_type {
+	EVTACT_EXPR_BUILTIN_TYPE_CPU,
 	EVTACT_EXPR_BUILTIN_TYPE_MAX,
 };
 
diff --git a/tools/perf/util/parse-action.l b/tools/perf/util/parse-action.l
index 1c729b7a0248..dab8066a1038 100644
--- a/tools/perf/util/parse-action.l
+++ b/tools/perf/util/parse-action.l
@@ -103,6 +103,7 @@ ident		[_a-zA-Z][_a-zA-Z0-9]*
 {num_hex}	{ return value(16); }
 {space}		{ }
 
+cpu		{ return builtin(EVTACT_EXPR_BUILTIN_TYPE_CPU);  }
 
 print		{ return call(EVTACT_EXPR_CALL_TYPE_PRINT); }
 
diff --git a/tools/perf/util/record_action.c b/tools/perf/util/record_action.c
index 634d6b199bae..15e8b1740e32 100644
--- a/tools/perf/util/record_action.c
+++ b/tools/perf/util/record_action.c
@@ -86,13 +86,16 @@ int bpf_perf_record_init(void)
 }
 
 static int set_expr_builtin_output_format(struct evtact_expr *expr,
-					  u32 opcode, int *offset,
-					  int *format __maybe_unused)
+					  u32 opcode, int *offset, int *format)
 {
 	int size = 0;
 	struct expr_builtin_output_priv *priv = expr->priv;
 
 	switch (opcode) {
+	case EVTACT_EXPR_BUILTIN_TYPE_CPU:
+		*format = __OUTPUT_FORMAT_TYPE_CPU;
+		size = sizeof(u32);
+		break;
 	default:
 		pr_err("set expr builtin output format unknown opcode: %u\n", opcode);
 		return -1;
-- 
2.25.1


  parent reply	other threads:[~2024-11-28 13:36 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-28 13:35 [RFC 00/12] perf record: Add event action support Yang Jihong
2024-11-28 13:35 ` [RFC 01/12] " Yang Jihong
2024-11-28 20:19   ` Arnaldo Carvalho de Melo
2024-12-04  8:24     ` [External] " Yang Jihong
2024-11-28 13:35 ` [RFC 02/12] perf event action: Add parsing const expr support Yang Jihong
2024-11-28 20:23   ` Arnaldo Carvalho de Melo
2024-12-04  8:29     ` [External] " Yang Jihong
2024-11-28 13:35 ` [RFC 03/12] perf event action: Add parsing const integer " Yang Jihong
2024-11-28 20:25   ` Arnaldo Carvalho de Melo
2024-12-04  8:32     ` [External] " Yang Jihong
2024-11-28 13:35 ` [RFC 04/12] perf event action: Add parsing const string " Yang Jihong
2024-11-28 13:35 ` [RFC 05/12] perf event action: Add parsing call " Yang Jihong
2024-11-28 13:35 ` [RFC 06/12] perf event action: Add parsing print() " Yang Jihong
2024-11-28 13:35 ` [RFC 07/12] perf event action: Add parsing builtin " Yang Jihong
2024-11-28 13:35 ` Yang Jihong [this message]
2024-11-28 13:35 ` [RFC 09/12] perf event action: Add parsing builtin pid " Yang Jihong
2024-11-28 13:35 ` [RFC 10/12] perf event action: Add parsing builtin tid " Yang Jihong
2024-11-28 13:35 ` [RFC 11/12] perf event action: Add parsing builtin comm " Yang Jihong
2024-11-28 13:35 ` [RFC 12/12] perf event action: Add parsing builtin time " Yang Jihong
2024-11-28 13:53 ` [RFC 00/12] perf record: Add event action support Adrian Hunter
2024-12-04  8:07   ` [External] " Yang Jihong
2024-11-28 20:14 ` Arnaldo Carvalho de Melo
2024-12-02 21:46   ` Namhyung Kim
2024-12-04  8:35     ` [External] " Yang Jihong
2024-12-04 19:54       ` Ian Rogers
2024-12-05 14:01         ` Yang Jihong
2024-12-04  8:21   ` Yang Jihong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241128133553.823722-9-yangjihong@bytedance.com \
    --to=yangjihong@bytedance.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@arm.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®