Re: [PATCH v2 3/4] perf tools: Add printing perf_event_attr type symbol in perf_event_attr__fprintf()

From: Yang Jihong
Date: Tue May 16 2023 - 03:01:14 EST


Hello,

On 2023/5/15 16:56, Adrian Hunter wrote:
On 15/05/23 11:48, Yang Jihong wrote:
Hello,

On 2023/5/12 18:34, Adrian Hunter wrote:
On 11/05/23 10:51, Yang Jihong wrote:
When printing perf_event_attr, always display perf_event_attr type and its symbol
to improve the readability of debugging information.

Before:

   # perf --debug verbose=2 record -e cycles,cpu-clock,sched:sched_switch,branch-load-misses,r101,mem:0x0 -C 0 true
   <SNIP>
   ------------------------------------------------------------
   perf_event_attr:
     size                             136
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 5
   ------------------------------------------------------------
   perf_event_attr:
     type                             1
     size                             136
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 6
   ------------------------------------------------------------
   perf_event_attr:
     type                             2
     size                             136
     config                           0x143
     { sample_period, sample_freq }   1
     sample_type                      IP|TID|TIME|CPU|PERIOD|RAW|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 7
   ------------------------------------------------------------
   perf_event_attr:
     type                             3
     size                             136
     config                           0x10005
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 9
   ------------------------------------------------------------
   perf_event_attr:
     type                             4
     size                             136
     config                           0x101
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 10
   ------------------------------------------------------------
   perf_event_attr:
     type                             5
     size                             136
     { sample_period, sample_freq }   1
     sample_type                      IP|TID|TIME|CPU|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     sample_id_all                    1
     exclude_guest                    1
     bp_type                          3
     { bp_len, config2 }              0x4
   ------------------------------------------------------------
   <SNIP>

After:

   # perf --debug verbose=2 record -e cycles,cpu-clock,sched:sched_switch,branch-load-misses,r101,mem:0x0 -C 0 true
   <SNIP>
   ------------------------------------------------------------
   perf_event_attr:
     type                             0 (PERF_TYPE_HARDWARE)
     size                             136
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 5
   ------------------------------------------------------------
   perf_event_attr:
     type                             1 (PERF_TYPE_SOFTWARE)
     size                             136
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 6
   ------------------------------------------------------------
   perf_event_attr:
     type                             2 (PERF_TYPE_TRACEPOINT)
     size                             136
     config                           0x143
     { sample_period, sample_freq }   1
     sample_type                      IP|TID|TIME|CPU|PERIOD|RAW|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 7
   ------------------------------------------------------------
   perf_event_attr:
     type                             3 (PERF_TYPE_HW_CACHE)
     size                             136
     config                           0x10005
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 9
   ------------------------------------------------------------
   perf_event_attr:
     type                             4 (PERF_TYPE_RAW)
     size                             136
     config                           0x101
     { sample_period, sample_freq }   4000
     sample_type                      IP|TID|TIME|CPU|PERIOD|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     freq                             1
     sample_id_all                    1
     exclude_guest                    1
   ------------------------------------------------------------
   sys_perf_event_open: pid -1  cpu 0  group_fd -1  flags 0x8 = 10
   ------------------------------------------------------------
   perf_event_attr:
     type                             5 (PERF_TYPE_BREAKPOINT)
     size                             136
     { sample_period, sample_freq }   1
     sample_type                      IP|TID|TIME|CPU|IDENTIFIER
     read_format                      ID
     disabled                         1
     inherit                          1
     sample_id_all                    1
     exclude_guest                    1
     bp_type                          3
     { bp_len, config2 }              0x4
   ------------------------------------------------------------
   <SNIP>

Signed-off-by: Yang Jihong <yangjihong1@xxxxxxxxxx>
---
  tools/perf/util/perf_event_attr_fprintf.c | 30 ++++++++++++++++++++++-
  1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c
index 433029c6afc5..cd0905d8cb7a 100644
--- a/tools/perf/util/perf_event_attr_fprintf.c
+++ b/tools/perf/util/perf_event_attr_fprintf.c
@@ -71,6 +71,33 @@ static void __p_read_format(char *buf, size_t size, u64 value)
      __p_bits(buf, size, value, bits);
  }
  +#define ENUM_ID_TO_STR_CASE(x) case x: return (#x);
+static const char *stringify_perf_type_id(u64 value)
+{
+    /* sync with enum perf_type_id in perf_event.h */

Everything in this file syncs with perf_event.h, so the comment
does not seem very useful.
OK, will fix in v3.

+    switch (value) {
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_HARDWARE)
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_SOFTWARE)
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_TRACEPOINT)
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_HW_CACHE)
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_RAW)
+    ENUM_ID_TO_STR_CASE(PERF_TYPE_BREAKPOINT)
+    default:
+        return NULL;
+    }
+}
+#undef ENUM_ID_TO_STR_CASE
+
+static void __p_type_id(char *buf, size_t size, u64 value)
+{
+    const char *str = stringify_perf_type_id(value);
+
+    if (str == NULL)
+        snprintf(buf, size, "%"PRIu64, value);
+    else
+        snprintf(buf, size, "%"PRIu64" (%s)", value, str);

These 4 lines end up getting used again about 3 times in the next
patch, so might as well be a separate function e.g.

    print_id(buf, size, value, stringify_perf_type_id(value));

where:

static void print_id(char *buf, size_t size, u64 value, const char *str)
{
    if (str == NULL)
        snprintf(buf, size, "%"PRIu64, value);
    else
        snprintf(buf, size, "%"PRIu64" (%s)", value, str);
}

OK, will add print_id() helper in v3.
If print_id() helper is also used to print config id, is printed in the decimal format. In the past, it is printed in the hexadecimal format,
there may be some changes here.

Before:
  config                           0x143

After:
  config                           323 (sched:sched_switch)

Right I didn't notice the difference. Best keep print_id() just for the hex cases.

v4 has been sent, print_id() helper is changed to a macro function to support printing of both 'hex' and 'unsigned' formats.
Please see if it's OK.

Thanks,
Yang