bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@linux.dev, song@kernel.org, yhs@fb.com,
	john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com
Cc: bpf@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names
Date: Thu,  8 Jun 2023 10:35:21 +0000	[thread overview]
Message-ID: <20230608103523.102267-10-laoar.shao@gmail.com> (raw)
In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com>

Add libbpf API to get generic perf event name.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 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


  parent reply	other threads:[~2023-06-08 10:35 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 10:35 [PATCH v2 bpf-next 00/11] bpf: Support ->fill_link_info for kprobe_multi and perf_event links Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi Yafang Shao
2023-06-08 23:05   ` Andrii Nakryiko
2023-06-09  9:13     ` Yafang Shao
2023-06-09 18:25       ` Andrii Nakryiko
2023-06-10  2:19         ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 02/11] bpftool: Add address filtering in kernel_syms_load() Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 03/11] bpftool: Show probed function in kprobe_multi link info Yafang Shao
2023-06-08 23:08   ` Andrii Nakryiko
2023-06-09  9:15     ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting Yafang Shao
2023-06-08 23:08   ` Andrii Nakryiko
2023-06-09  9:16     ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 05/11] bpf: Clear the probe_addr for uprobe Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 06/11] bpf: Expose symbol addresses for precise identification Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 07/11] bpf: Add a common helper bpf_copy_to_user() Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event Yafang Shao
2023-06-08 23:12   ` Andrii Nakryiko
2023-06-09  9:53     ` Yafang Shao
2023-06-09  9:56       ` Yafang Shao
2023-06-09 18:26         ` Andrii Nakryiko
2023-06-10  2:21           ` Yafang Shao
2023-06-08 10:35 ` Yafang Shao [this message]
2023-06-08 23:14   ` [PATCH v2 bpf-next 09/11] libbpf: Add perf event names Andrii Nakryiko
2023-06-09  4:36     ` Song Liu
2023-06-10  2:22       ` Yafang Shao
2023-06-10 20:34         ` Quentin Monnet
2023-06-11 15:52           ` Yafang Shao
2023-06-10 22:37       ` Jiri Olsa
2023-06-10 22:43   ` Jiri Olsa
2023-06-11 15:55     ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 10/11] bpftool: Move get_prog_info() into do_show_link() Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 11/11] bpftool: Show probed function in perf_event link info Yafang Shao

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=20230608103523.102267-10-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=quentin@isovalent.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yhs@fb.com \
    /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;
as well as URLs for NNTP newsgroup(s).