[RFC 09/12] perf event action: Add parsing builtin pid expr support

Yang Jihong
2024-11-28T13:37:04Z

Support parsing of builtin pid expression.

Testcase:
# perf record -e sched:sched_switch --action 'print("pid = %d\n", pid)'
pid = 132784
pid = 0
pid = 132784
pid = 132778
pid = 0
pid = 0
pid = 132784
pid = 0
pid = 132784
pid = 0
pid = 132784
pid = 0
pid = 132784
...
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 | 14 ++++++++++++++
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 | 4 ++++
6 files changed, 22 insertions(+)

diff --git a/tools/perf/util/bpf_skel/bpf_record_action.h b/tools/perf/util/bpf_skel/bpf_record_action.h
index 91a307b89f85..a4817d62bce8 100644
--- a/tools/perf/util/bpf_skel/bpf_record_action.h
+++ b/tools/perf/util/bpf_skel/bpf_record_action.h
@@ -8,6 +8,7 @@

enum __output_format_type {
__OUTPUT_FORMAT_TYPE_CPU,
+ __OUTPUT_FORMAT_TYPE_PID,
__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 4b7698554ead..fd44bb3da663 100644
--- a/tools/perf/util/bpf_skel/record_action.bpf.c
+++ b/tools/perf/util/bpf_skel/record_action.bpf.c
@@ -46,6 +46,17 @@ static inline int output_cpu(__u8 *data, int size)
return sizeof(__u32);
}

+static inline int output_pid(__u8 *data, int size)
+{
+ __u32 *pid = (__u32 *)data;
+
+ if (size < sizeof(__u32))
+ return -1;
+
+ *pid = bpf_get_current_pid_tgid() >> 32;
+ return sizeof(__u32);
+}
+
SEC("xxx")
int sample_output(u64 *ctx)
{
@@ -68,6 +79,9 @@ int sample_output(u64 *ctx)
case __OUTPUT_FORMAT_TYPE_CPU:
ret = output_cpu(data + total, __OUTPUT_DATA_MAX_SIZE - total);
break;
+ case __OUTPUT_FORMAT_TYPE_PID:
+ ret = output_pid(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 2bb32cc16ae7..0ae998a5d286 100644
--- a/tools/perf/util/parse-action.c
+++ b/tools/perf/util/parse-action.c
@@ -13,6 +13,7 @@
* - print
* - builtin:
* - cpu
+ * - pid
*/

#include <regex.h>
diff --git a/tools/perf/util/parse-action.h b/tools/perf/util/parse-action.h
index ee949ed07111..e74d3b4e5de8 100644
--- a/tools/perf/util/parse-action.h
+++ b/tools/perf/util/parse-action.h
@@ -28,6 +28,7 @@ enum evtact_expr_call_type {

enum evtact_expr_builtin_type {
EVTACT_EXPR_BUILTIN_TYPE_CPU,
+ EVTACT_EXPR_BUILTIN_TYPE_PID,
EVTACT_EXPR_BUILTIN_TYPE_MAX,
};

diff --git a/tools/perf/util/parse-action.l b/tools/perf/util/parse-action.l
index dab8066a1038..5a5bbe3e1f94 100644
--- a/tools/perf/util/parse-action.l
+++ b/tools/perf/util/parse-action.l
@@ -104,6 +104,7 @@ ident [_a-zA-Z][_a-zA-Z0-9]*
{space} { }

cpu { return builtin(EVTACT_EXPR_BUILTIN_TYPE_CPU); }
+pid { return builtin(EVTACT_EXPR_BUILTIN_TYPE_PID); }

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 15e8b1740e32..1d7b603713d1 100644
--- a/tools/perf/util/record_action.c
+++ b/tools/perf/util/record_action.c
@@ -96,6 +96,10 @@ static int set_expr_builtin_output_format(struct evtact_expr *expr,
*format = __OUTPUT_FORMAT_TYPE_CPU;
size = sizeof(u32);
break;
+ case EVTACT_EXPR_BUILTIN_TYPE_PID:
+ *format = __OUTPUT_FORMAT_TYPE_PID;
+ size = sizeof(u32);
+ break;
default:
pr_err("set expr builtin output format unknown opcode: %u\n", opcode);
return -1;
--
2.25.1