Add libbpf API to get generic perf event name. Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx> --- tools/lib/bpf/libbpf.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 56 +++++++++++++++++++++++++ tools/lib/bpf/libbpf.map | 6 +++ 3 files changed, 169 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 47632606..27d396f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -119,6 +119,64 @@ [BPF_STRUCT_OPS] = "struct_ops", }; +static const char * const perf_type_name[] = { + [PERF_TYPE_HARDWARE] = "hardware", + [PERF_TYPE_SOFTWARE] = "software", + [PERF_TYPE_TRACEPOINT] = "tracepoint", + [PERF_TYPE_HW_CACHE] = "hw_cache", + [PERF_TYPE_RAW] = "raw", + [PERF_TYPE_BREAKPOINT] = "breakpoint", +}; + +static const char * const perf_hw_name[] = { + [PERF_COUNT_HW_CPU_CYCLES] = "cpu_cycles", + [PERF_COUNT_HW_INSTRUCTIONS] = "instructions", + [PERF_COUNT_HW_CACHE_REFERENCES] = "cache_references", + [PERF_COUNT_HW_CACHE_MISSES] = "cache_misses", + [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = "branch_instructions", + [PERF_COUNT_HW_BRANCH_MISSES] = "branch_misses", + [PERF_COUNT_HW_BUS_CYCLES] = "bus_cycles", + [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = "stalled_cycles_frontend", + [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = "stalled_cycles_backend", + [PERF_COUNT_HW_REF_CPU_CYCLES] = "ref_cpu_cycles", +}; + +static const char * const perf_hw_cache_name[] = { + [PERF_COUNT_HW_CACHE_L1D] = "l1d", + [PERF_COUNT_HW_CACHE_L1I] = "l1i", + [PERF_COUNT_HW_CACHE_LL] = "ll", + [PERF_COUNT_HW_CACHE_DTLB] = "dtlb", + [PERF_COUNT_HW_CACHE_ITLB] = "itlb", + [PERF_COUNT_HW_CACHE_BPU] = "bpu", + [PERF_COUNT_HW_CACHE_NODE] = "node", +}; + +static const char * const perf_hw_cache_op_name[] = { + [PERF_COUNT_HW_CACHE_OP_READ] = "read", + [PERF_COUNT_HW_CACHE_OP_WRITE] = "write", + [PERF_COUNT_HW_CACHE_OP_PREFETCH] = "prefetch", +}; + +static const char * const perf_hw_cache_op_result_name[] = { + [PERF_COUNT_HW_CACHE_RESULT_ACCESS] = "access", + [PERF_COUNT_HW_CACHE_RESULT_MISS] = "miss", +}; + +static const char * const perf_sw_name[] = { + [PERF_COUNT_SW_CPU_CLOCK] = "cpu_clock", + [PERF_COUNT_SW_TASK_CLOCK] = "task_clock", + [PERF_COUNT_SW_PAGE_FAULTS] = "page_faults", + [PERF_COUNT_SW_CONTEXT_SWITCHES] = "context_switches", + [PERF_COUNT_SW_CPU_MIGRATIONS] = "cpu_migrations", + [PERF_COUNT_SW_PAGE_FAULTS_MIN] = "page_faults_min", + [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = "page_faults_maj", + [PERF_COUNT_SW_ALIGNMENT_FAULTS] = "alignment_faults", + [PERF_COUNT_SW_EMULATION_FAULTS] = "emulation_faults", + [PERF_COUNT_SW_DUMMY] = "dummy", + [PERF_COUNT_SW_BPF_OUTPUT] = "bpf_output", + [PERF_COUNT_SW_CGROUP_SWITCHES] = "cgroup_switches", +}; + static const char * const link_type_name[] = { [BPF_LINK_TYPE_UNSPEC] = "unspec", [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", @@ -8953,6 +9011,55 @@ const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t) return attach_type_name[t]; } +const char *libbpf_perf_type_str(enum perf_type_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_type_name)) + return NULL; + + return perf_type_name[t]; +} + +const char *libbpf_perf_hw_str(enum perf_hw_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_name)) + return NULL; + + return perf_hw_name[t]; +} + +const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_name)) + return NULL; + + return perf_hw_cache_name[t]; +} + +const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_name)) + return NULL; + + return perf_hw_cache_op_name[t]; +} + +const char * +libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_result_name)) + return NULL; + + return perf_hw_cache_op_result_name[t]; +} + +const char *libbpf_perf_sw_str(enum perf_sw_ids t) +{ + if (t < 0 || t >= ARRAY_SIZE(perf_sw_name)) + return NULL; + + return perf_sw_name[t]; +} + const char *libbpf_bpf_link_type_str(enum bpf_link_type t) { if (t < 0 || t >= ARRAY_SIZE(link_type_name)) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 754da73..4123e4c 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -16,6 +16,7 @@ #include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/perf_event.h> #include "libbpf_common.h" #include "libbpf_legacy.h" @@ -61,6 +62,61 @@ enum libbpf_errno { LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t); /** + * @brief **libbpf_perf_type_str()** converts the provided perf type value + * into a textual representation. + * @param t The perf type. + * @return Pointer to a static string identifying the perf type. NULL is + * returned for unknown **perf_type_id** values. + */ +LIBBPF_API const char *libbpf_perf_type_str(enum perf_type_id t); + +/** + * @brief **libbpf_perf_hw_str()** converts the provided perf hw id + * into a textual representation. + * @param t The perf hw id. + * @return Pointer to a static string identifying the perf hw id. NULL is + * returned for unknown **perf_hw_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_str(enum perf_hw_id t); + +/** + * @brief **libbpf_perf_hw_cache_str()** converts the provided perf hw cache + * id into a textual representation. + * @param t The perf hw cache id. + * @return Pointer to a static string identifying the perf hw cache id. + * NULL is returned for unknown **perf_hw_cache_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t); + +/** + * @brief **libbpf_perf_hw_cache_op_str()** converts the provided perf hw + * cache op id into a textual representation. + * @param t The perf hw cache op id. + * @return Pointer to a static string identifying the perf hw cache op id. + * NULL is returned for unknown **perf_hw_cache_op_id** values. + */ +LIBBPF_API const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t); + +/** + * @brief **libbpf_perf_hw_cache_op_result_str()** converts the provided + * perf hw cache op result id into a textual representation. + * @param t The perf hw cache op result id. + * @return Pointer to a static string identifying the perf hw cache op result + * id. NULL is returned for unknown **perf_hw_cache_op_result_id** values. + */ +LIBBPF_API const char * +libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t); + +/** + * @brief **libbpf_perf_sw_str()** converts the provided perf sw id + * into a textual representation. + * @param t The perf sw id. + * @return Pointer to a static string identifying the perf sw id. NULL is + * returned for unknown **perf_sw_ids** values. + */ +LIBBPF_API const char *libbpf_perf_sw_str(enum perf_sw_ids t); + +/** * @brief **libbpf_bpf_link_type_str()** converts the provided link type value * into a textual representation. * @param t The link type. diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 7521a2f..6ae0a36 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -395,4 +395,10 @@ LIBBPF_1.2.0 { LIBBPF_1.3.0 { global: bpf_obj_pin_opts; + libbpf_perf_hw_cache_op_result_str; + libbpf_perf_hw_cache_op_str; + libbpf_perf_hw_cache_str; + libbpf_perf_hw_str; + libbpf_perf_sw_str; + libbpf_perf_type_str; } LIBBPF_1.2.0; -- 1.8.3.1