[RFC 11/12] perf event action: Add parsing builtin comm expr support

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

Support parsing of builtin comm expression.

Testcase:
# perf record -e sched:sched_switch --action 'print("comm = %s\n", comm)' true
comm = perf
comm = swapper/7
comm = perf
comm = swapper/7
comm = swapper/1
comm = perf
comm = kworker/u40:0
comm = perf
comm = kworker/u40:0
...
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 | 19 +++++++++++++++++++
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, 27 insertions(+)

diff --git a/tools/perf/util/bpf_skel/bpf_record_action.h b/tools/perf/util/bpf_skel/bpf_record_action.h
index 1d2ffa3c260f..325964b7aac9 100644
--- a/tools/perf/util/bpf_skel/bpf_record_action.h
+++ b/tools/perf/util/bpf_skel/bpf_record_action.h
@@ -10,6 +10,7 @@ enum __output_format_type {
__OUTPUT_FORMAT_TYPE_CPU,
__OUTPUT_FORMAT_TYPE_PID,
__OUTPUT_FORMAT_TYPE_TID,
+ __OUTPUT_FORMAT_TYPE_COMM,
__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 b6049b9af055..f809305b16be 100644
--- a/tools/perf/util/bpf_skel/record_action.bpf.c
+++ b/tools/perf/util/bpf_skel/record_action.bpf.c
@@ -68,6 +68,22 @@ static inline int output_tid(__u8 *data, int size)
return sizeof(__u32);
}

+static inline int output_comm(__u8 *data, int size)
+{
+ struct task_struct *task = (struct task_struct *)bpf_get_current_task();
+
+ if (!task) {
+ data[0] = '\0';
+ return 0;
+ }
+
+ if (size < __TASK_COMM_MAX_SIZE)
+ return -1;
+
+ bpf_core_read_str(data, __TASK_COMM_MAX_SIZE, &task->comm);
+ return __TASK_COMM_MAX_SIZE;
+}
+
SEC("xxx")
int sample_output(u64 *ctx)
{
@@ -96,6 +112,9 @@ int sample_output(u64 *ctx)
case __OUTPUT_FORMAT_TYPE_TID:
ret = output_tid(data + total, __OUTPUT_DATA_MAX_SIZE - total);
break;
+ case __OUTPUT_FORMAT_TYPE_COMM:
+ ret = output_comm(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 6928990c9106..8bc1646abe27 100644
--- a/tools/perf/util/parse-action.c
+++ b/tools/perf/util/parse-action.c
@@ -15,6 +15,7 @@
* - cpu
* - pid
* - tid
+ * - comm
*/

#include <regex.h>
diff --git a/tools/perf/util/parse-action.h b/tools/perf/util/parse-action.h
index 817e2c4213e0..39fb521f3193 100644
--- a/tools/perf/util/parse-action.h
+++ b/tools/perf/util/parse-action.h
@@ -30,6 +30,7 @@ enum evtact_expr_builtin_type {
EVTACT_EXPR_BUILTIN_TYPE_CPU,
EVTACT_EXPR_BUILTIN_TYPE_PID,
EVTACT_EXPR_BUILTIN_TYPE_TID,
+ EVTACT_EXPR_BUILTIN_TYPE_COMM,
EVTACT_EXPR_BUILTIN_TYPE_MAX,
};

diff --git a/tools/perf/util/parse-action.l b/tools/perf/util/parse-action.l
index 2ada6b4bf091..ddac305c1ca4 100644
--- a/tools/perf/util/parse-action.l
+++ b/tools/perf/util/parse-action.l
@@ -106,6 +106,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); }
+comm { return builtin(EVTACT_EXPR_BUILTIN_TYPE_COMM); }

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