[RFC 10/12] perf event action: Add parsing builtin tid expr support

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

Support parsing of builtin tid expression.

Testcase:
# perf record -e sched:sched_switch --action 'print("tid = %d\n", tid)' true
tid = 134891
tid = 0
tid = 134891
tid = 0
tid = 0
tid = 134891
tid = 132778
tid = 134891
tid = 132778
tid = 134891
tid = 132778
tid = 134891
tid = 132778
tid = 134891
tid = 132778
...
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 a4817d62bce8..1d2ffa3c260f 100644
--- a/tools/perf/util/bpf_skel/bpf_record_action.h
+++ b/tools/perf/util/bpf_skel/bpf_record_action.h
@@ -9,6 +9,7 @@
enum __output_format_type {
__OUTPUT_FORMAT_TYPE_CPU,
__OUTPUT_FORMAT_TYPE_PID,
+ __OUTPUT_FORMAT_TYPE_TID,
__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 fd44bb3da663..b6049b9af055 100644
--- a/tools/perf/util/bpf_skel/record_action.bpf.c
+++ b/tools/perf/util/bpf_skel/record_action.bpf.c
@@ -57,6 +57,17 @@ static inline int output_pid(__u8 *data, int size)
return sizeof(__u32);
}

+static inline int output_tid(__u8 *data, int size)
+{
+ __u32 *pid = (__u32 *)data;
+
+ if (size < sizeof(__u32))
+ return -1;
+
+ *pid = bpf_get_current_pid_tgid() & 0xffffffff;
+ return sizeof(__u32);
+}
+
SEC("xxx")
int sample_output(u64 *ctx)
{
@@ -82,6 +93,9 @@ int sample_output(u64 *ctx)
case __OUTPUT_FORMAT_TYPE_PID:
ret = output_pid(data + total, __OUTPUT_DATA_MAX_SIZE - total);
break;
+ case __OUTPUT_FORMAT_TYPE_TID:
+ ret = output_tid(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 0ae998a5d286..6928990c9106 100644
--- a/tools/perf/util/parse-action.c
+++ b/tools/perf/util/parse-action.c
@@ -14,6 +14,7 @@
* - builtin:
* - cpu
* - pid
+ * - tid
*/

#include <regex.h>
diff --git a/tools/perf/util/parse-action.h b/tools/perf/util/parse-action.h
index e74d3b4e5de8..817e2c4213e0 100644
--- a/tools/perf/util/parse-action.h
+++ b/tools/perf/util/parse-action.h
@@ -29,6 +29,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_TID,
EVTACT_EXPR_BUILTIN_TYPE_MAX,
};

diff --git a/tools/perf/util/parse-action.l b/tools/perf/util/parse-action.l
index 5a5bbe3e1f94..2ada6b4bf091 100644
--- a/tools/perf/util/parse-action.l
+++ b/tools/perf/util/parse-action.l
@@ -105,6 +105,7 @@ ident [_a-zA-Z][_a-zA-Z0-9]*

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

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